Extension:CreateBox

CreateBox is a specialized version of Inputbox that focuses on page creation.

If a user were to attempt to create a page that already exists, Inputbox allows the user do so, and disregards any preloaded content or edit introductions. CreateBox, by contrast, directs the user to an error page where they can enter a different page name, and all specified properties are preserved.

Parameters
CreateBox handles parameters in the same manner as Inputbox. Example:

All parameters to CreateBox are optional.


 * align: Sets the alignment of the div which contains CreateBox. Defaults to center.
 * break: If set to "yes", this inserts a line break between the text field and button.
 * buttonlabel: Sets the text of the Create button.
 * default: Sets the default text of the input box.
 * editintro: Specifies a page title. The given page will be included prior to the submission form in the Edit screen.
 * prefix: Specifies a title fragment, which will prefix the submitted title.
 * preload: Specifies a page title. The contents of the given page will be preloaded into the submission form.
 * width: Sets the width of the input box.

MediaWiki messages
Createbox specifies two messages in the MediaWiki: namespace.


 * MediaWiki:Create - The word "Create".
 * MediaWiki:Create_exists - The text of the error page shown when an article already exists.

Code
This code has been tested in MediaWiki 1.9, and should run in versions 1.7 and above.



This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. http://www.gnu.org/copyleft/gpl.html

To install, add following to LocalSettings.php include("extensions/create.php");



$wgExtensionFunctions[] = "wfCreateBox"; $wgHooks['UnknownAction'][] = 'actionCreate'; $wgExtensionCredits['parserhook'][] = array( 'name' => 'CreateBox', 'url' => 'http://www.mediawiki.org/wiki/Extension:CreateBox', 'description' => 'Specialized Inputbox for page creation', 'author' => 'Ross McClure', 'version' => '1.5' );

function wfCreateBox {   global $wgParser, $wgMessageCache;

$wgMessageCache->addMessages( array( 'create' => "Create", 'create_exists' => "Sorry, \"\" already ". "exists.\n\nYou cannot create this page, but you can ". "[ edit it], ". "[ read it], or choose to ". "create a different page using the box below.\n\n". " break=no " ));

$wgParser->setHook( "createbox", "acMakeBox" ); }

function actionCreate($action, $article) {   if($action != 'create') return true;

global $wgRequest; $prefix = $wgRequest->getVal('prefix'); $text = $wgRequest->getVal('title'); if($prefix && strpos($text, $prefix)!==0) {       $title = Title::newFromText( $prefix . $text ); if(is_null($title)) {           global $wgTitle; $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle' ); throw new ErrorPageError( 'badtitle', 'badtitletext' ); }       else if($title->getArticleID == 0) acRedirect($title, 'edit'); else acRedirect($title, 'create'); }   else if($wgRequest->getVal('section')=='new' || $article->getID == 0) {       acRedirect($article->getTitle, 'edit'); }   else {       global $wgOut; $text = $article->getTitle->getPrefixedText; $wgOut->setPageTitle($text); $wgOut->setHTMLTitle(wfMsg('pagetitle', $text.' - '.wfMsg('create'))); $wgOut->addWikiText(wfMsg('create_exists')); }   return false; }

function acGetOption(&$input,$name,$value=NULL) {   if(preg_match("/^\s*$name\s*=\s*(.*)/mi",$input,$matches)) {       if(is_int($value)) return intval($matches[1]); else return htmlspecialchars($matches[1]); }   return $value; }

function acMakeBox($input, $argv, &$parser) {   global $wgRequest, $wgScript; if($wgRequest->getVal('action')=='create') {       $prefix = $wgRequest->getVal('prefix'); $preload = $wgRequest->getVal('preload'); $editintro = $wgRequest->getVal('editintro'); $text = $parser->getTitle->getPrefixedText; if($prefix && strpos($text, $prefix)===0) $text = substr($text, strlen($prefix)); }   else {       $prefix = acGetOption($input,'prefix'); $preload = acGetOption($input,'preload'); $editintro = acGetOption($input,'editintro'); $text = acGetOption($input,'default'); }   $submit = htmlspecialchars($wgScript); $width = acGetOption($input,'width',0); $align = acGetOption($input,'align','center'); $br = ((acGetOption($input,'break','no')=='no') ? '' : ' '); $label = acGetOption($input,'buttonlabel',wfMsgHtml("createarticle")); $output=<<      {$br}  ENDFORM; return $parser->replaceVariables($output); }

function acRedirect($title, $action) {   global $wgRequest, $wgOut; $query = "action={$action}&prefix=". $wgRequest->getVal('prefix'). "&preload=". $wgRequest->getVal('preload'). "&editintro=". $wgRequest->getVal('editintro'). "&section=". $wgRequest->getVal('section'); $wgOut->setSquidMaxage( 1200 ); $wgOut->redirect($title->getFullURL( $query ), '301'); }

?>