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.0' );

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." ));

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

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

global $wgRequest, $wgOut; $title = $article->getTitle; $prefix = NULL; if($prefix = $wgRequest->getVal('prefix')) { $text = $wgRequest->getVal('title'); if(strpos($text, $prefix)!==0) { $title = Title::newFromText( $prefix . $text ); if(is_null($title)) { $wgTitle = Title::makeTitle( NS_SPECIAL, 'Badtitle' ); throw new ErrorPageError( 'badtitle', 'badtitletext' ); } else if($title->getArticleID == 0) { acRedirect($title, 'edit'); } else { acRedirect($title, 'create'); }           return false; }   }

if($wgRequest->getVal('section')=='new' || $article->getID == 0) { acRedirect($title, 'edit'); return false; }

$title = $title->getPrefixedText; $wgOut->setPageTitle( $title ); $wgOut->setHTMLTitle(wfMsg('pagetitle', $title.' - '.wfMsg('create'))); $text = wfMsg('create_exists'). "\n \ndefault="; if($prefix) $text .= substr($title,strlen($prefix))."\nprefix=". $prefix; else $text .= $title; if($arg = $wgRequest->getVal('preload')) $text .= "\npreload=". $arg; if($arg = $wgRequest->getVal('editintro')) $text .= "\neditintro=". $arg; $wgOut->addWikiText( $text . "\n " ); 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 $wgScript; $action = htmlspecialchars($wgScript); $width = acGetOption($input,'width',0); $prefix = acGetOption($input,'prefix'); $preload = acGetOption($input,'preload'); $editintro = acGetOption($input,'editintro'); $defaulttext = acGetOption($input,'default'); $align = acGetOption($input,'align','center'); $br = ((acGetOption($input,'break','no')=='no') ? '' : ' '); $buttonlabel = acGetOption($input,'buttonlabel'); if(!$buttonlabel) $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'); }

?>