<?php
/**
* Extention to create a custom article editing form.
*
* By Barry Brannan (http://www.mediawiki.org/wiki/User:Barrylb)
* and Florian Straub (http://www.mediawiki.org/wiki/User:Flominator)
*
* This example assumes you have a set of articles named Person/John_Smith, Person/John_Brown ... etc
*
* Whenever you are editing an article named Person/*, a form is shown with fields Sex, Age, Description.
* When you first create the article, the fields are put together with a template named Person, eg:
* {{Person|sex=M|age=60}}My description
*
* When you edit an article, this form extracts the fields from a template used in the text and allows
* the user to edit them.
*
* You also need to create a template called [[Template:Person]] to display the fields as you would like and
* to activate the templates this extension should use on [[MediaWiki:CustomEditTemplates]] before.
*
* To enable this extension place the following in LocalSettings.php:
* require_once("extensions/CustomEdit.php");
*/
error_reporting(E_ALL ^ E_NOTICE); //deactivate notices but not other messages
$wgExtensionCredits['other'][] = array(
'name' => 'CustomEdit',
'version' => 1.1,
'author' => array('Barry Brannan', 'Florian Straub'),
'url' => 'http://www.mediawiki.org/wiki/Extension:CustomEdit',
'description' => 'Example of how to create a custom article editing form',
);
if( defined( 'MEDIAWIKI' ) ) {
global $wgHooks;
$wgHooks['AlternateEdit'][] = 'fnMyCustomEdit';
} else {
echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" );
die( -1 );
}
function fnMyCustomEdit( &$editpage ) {
global $wgOut, $wgRequest, $wgParser, $wgUser;
$resultBoolean = true;
$possibleTemplates = getCustomEditTemplates();
//echo $possibleTemplates;
if( is_array($possibleTemplates) ) {
foreach( $possibleTemplates as $templateUsed ) {
$resultBoolean = tryCustomEdit($templateUsed, &$editpage);
if( !$resultBoolean ) {
break;
}
}
}
return $resultBoolean;
}
/**
* Does all the stuff Barry Brannan (http://www.mediawiki.org/wiki/User:Barrylb) introduced with
* http://www.mediawiki.org/wiki/User:Barrylb/Custom_article_editing_form_with_fields
* (public domain example of how to create a custom article editing form - created July 2006)
* Problem: {{Person}} was the only template Barry's version handled.
*
* Improvement made by Florian Straub (http://www.mediawiki.org/wiki/User:Flominator):
* It now uses getTemplateFields() from below to read the fields directly from the template
*
* @param potential template used on this page
* @return boolean
*/
function tryCustomEdit( $templateUsed, &$editpage ) {
global $wgOut, $wgRequest, $wgParser, $wgUser;
$fieldsInTemplate = getTemplateFields("Template:" . $templateUsed);
//echo "Templatefelder:".$fieldsInTemplate;
foreach( $fieldsInTemplate as $fieldInTemplate ) {
$templateFields[]['field'] = $fieldInTemplate;
}
// $templateFields[0]['field']="Age";
// $templateFields[0]['value']="";
// $templateFields[1]['field']="Sex";
// $templateFields[1]['value']="";
if( strpos($editpage->mTitle->getText(), $templateUsed.'/') === 0 ) {
$wgOut->setArticleFlag(false);
$wgOut->setArticleRelated(true);
if ( !$editpage->mTitle->userCanEdit() ) {
$wgOut->readOnlyPage( $editpage->mArticle->getContent(), true );
return false;
}
if ( !$wgUser->isAllowed('edit') ) {
if ( $wgUser->isAnon() ) {
$editpage->userNotLoggedInPage();
return false;
} else {
$wgOut->readOnlyPage( $editpage->mArticle->getContent(), true );
return false;
}
}
if ( !$editpage->mTitle->userCan( 'create' ) && !$editpage->mTitle->exists() ) {
$editpage->noCreatePermission();
return false;
}
$wgOut->setPageTitle( wfMsg( 'editing', $editpage->mTitle->getPrefixedText() ) );
if ( $wgRequest->wasPosted() ) {
$personDescription = rtrim ( $wgRequest->getText( 'wpDescription' ) );
$personSex = $wgRequest->getText( 'wpSex' );
for( $i = 0; $i < count($templateFields); $i++ ) {
$formFieldName = 'wp' . $templateUsed . $templateFields[$i]['field'];
$plainFieldName = $templateFields[$i]['field'];
$templateFields[$i]['value'] = $wgRequest->getText( $formFieldName );
// $personAge = $wgRequest->getText( 'wpAge' );
}
$summary = $wgRequest->getText( 'wpSummary' );
} else {
if( $editpage->mTitle->exists() ) {
$text = $editpage->mArticle->getContent();
$personDescription = preg_replace('/\{\{[^\}]*\}\}/', '', $text);
$max = count($templateFields);
for( $i = 0; $i < $max; $i++ ) {
if( $i == ($max-1) ) {
preg_match('/sex=(.*)\}\}/i', $text, $matches);
$regExp = '/' . $templateFields[$i]['field'] .'=(.*)\n\}\}/i';
} else {
$regExp = '/' . $templateFields[$i]['field'] .'=(.*)\n/i';
}
preg_match($regExp, $text, $matches);
$templateFields[$i]['value'] = $matches[1];
}
// preg_match('/age=(.*)\n/i', $text, $matches);
// $personAge = $matches[1];
// preg_match('/sex=(.*)\}\}/i', $text, $matches);
// $personSex = $matches[1];
} else {
$personDescription = '';
for( $i = 0; $i < count($templateFields); $i++ ) {
$formFieldValue[$i]= ' ';
}
$summary = '';
}
}
$editpage->textbox1 = "{{" . $templateUsed ."\n" ;
for( $i = 0; $i < count($templateFields); $i++ ) {
$editpage->textbox1.= '|';
$editpage->textbox1.= $templateFields[$i]['field'];
$editpage->textbox1.= '=';
$editpage->textbox1.= $templateFields[$i]['value'];
$editpage->textbox1.= "\n";
//"|age=$personAge\n" .
//"|sex=$personSex
}
$editpage->textbox1 = $editpage->textbox1 . "}}\n" .
$personDescription;
if( $wgRequest->getCheck( 'wpSave' ) ) {
if( $editpage->mTitle->exists() )
$editpage->mArticle->updateArticle( $editpage->textbox1, $summary, false, false, false, '' );
else
$editpage->mArticle->insertNewArticle( $editpage->textbox1, $summary, false, false, false, '');
return false;
} else {
if( $wgRequest->getCheck( 'wpDiff' ) ) {
//$wgOut->addHTML( $editpage->getDiff() ); Switch those lines for MediaWiki versions prior to 1.12
$wgOut->addHTML( $editpage->showDiff() );
} else {
//if( $editpage->previewOnOpen() || $wgRequest->wasPosted() ) {
if( $wgRequest->wasPosted() ) {
$parserOptions = ParserOptions::newFromUser( $wgUser );
$parserOptions->setEditSection( false );
$parserOptions->setTidy(true);
$parserOutput = $wgParser->parse( $editpage->textbox1, $editpage->mTitle, $parserOptions );
$previewHTML = $parserOutput->getText();
$wgOut->addHTML('<div style="border: 2px solid #ededdd; padding: 8px; margin: 8px;">');
$wgOut->addHTML($previewHTML);
$wgOut->addHTML('</div>');
}
$firstFormFieldName = 'wp' . $templateUsed . $templateFields[0]['field'];
$wgOut->setOnloadHandler( 'document.editform.'. $firstFormFieldName .'.focus()' );
}
$wgOut->addHTML('<form id="editform" name="editform" method="post" action="' . $editpage->mTitle->escapeLocalURL("action=submit") . '" enctype="multipart/form-data">');
$wgOut->addHTML('<table border="0">');
for( $i = 0; $i < count($templateFields); $i++ ) {
$plainFieldName = $templateFields[$i]['field'];
$formFieldName = 'wp' . $templateUsed . $templateFields[$i]['field'];
$formFieldValue = $templateFields[$i]['value'];
$wgOut->addHTML('<tr><td>'. $plainFieldName .'</td><td><input id="'.$formFieldName.'" name="'.$formFieldName.'" type="text" size="20" value="' . $formFieldValue . '"/></td></tr>' . "\n");
//$wgOut->addHTML('<tr><td>Sex:</td><td><input id="wpSex" name="wpSex" type="text" size="20" value="' . $personSex . '"/></td></tr>');
}
$wgOut->addHTML('<tr><td>Description:</td><td><textarea rows="10" cols="40" name="wpDescription">' . $personDescription . '</textarea></td></tr>');
$wgOut->addHTML('<tr><td>Edit summary:</td><td><input id="wpSummary" name="wpSummary" type="text" size="80" value="' . $summary . '"/></td></tr>');
$wgOut->addHTML('</table>');
$wgOut->addHTML('<input id="wpSave" name="wpSave" type="submit" value="' . wfMsg('savearticle') . '"/>');
$wgOut->addHTML('<input id="wpPreview" name="wpPreview" type="submit" value="' . wfMsg('showpreview') . '"/> ');
$wgOut->addHTML('<input id="wpDiff" name="wpDiff" type="submit" value="' . wfMsg('showdiff') . '"/> ');
$wgOut->addHTML('</form>');
}
return false;
}
//else
return true;
}
/**
* Retrieves text from an article
* @param name of the article as string
* @return text from article as string
*/
function getArticleText( $article = "Main Page" ) {
//ialex @ #mediawiki: you shouldn't create a title object with "new Title( ... )", always une a static method you can see at the top of Title.php
$t = Title::NewFromText($article);
$art = Revision::newFromTitle( $t );
if( $art != null ) {
return $art->getText();
} else {
return "";
}
}
/**
* Retrieves a list of fields available at a template (text surrounded by {{{X}}} or {{{X| )
* @param the template
* @return Simple array with field names
*/
function getTemplateFields( $template ) {
$templateText = getArticleText($template);
$parts = explode("{{{", $templateText);
$params = array();
foreach( $parts as $part ) {
$endOfBrackets = strpos($part, "}}}");
$positionOfPipe = strpos($part, "|");
if( ($positionOfPipe) && ($positionOfPipe < $endOfBrackets) ) {
$endOfBrackets = $positionOfPipe;
}
if( $endOfBrackets ) {
$parameterInTemplate = trim(substr($part, 0, $endOfBrackets));
if( ( $parameterInTemplate != "" ) && ( !in_array($parameterInTemplate, $params) ) ) {
$params[] = $parameterInTemplate;
}
}
}
return $params;
}
/**
* Retrieves a list of templates stored in [[MediaWiki:CustomEditTemplates]]
* @return Simple array with template names
*/
function getCustomEditTemplates() {
$templates = "";
$templateNamespaceAlias = getArticleText('MediaWiki:Nstab-template');
$templateList = getArticleText('MediaWiki:CustomEditTemplates');
$templateList = str_replace($templateNamespaceAlias.':', 'Template:', $templateList);
$parts = explode("\n", $templateList);
foreach( $parts as $part ) {
$beginnOfTemplate = strpos($part, 'Template:');
if( $beginnOfTemplate != -1 ) {
$endOfBrackets = strpos($part, ']]', $beginnOfTemplate);
$pipeInString = strpos($part, '|', $beginnOfTemplate);
if( $pipeInString < $endOfBrackets ) {
$pipeInString = $endOfBrackets;
}
$templateName = substr($part, $beginnOfTemplate + 9, $endOfBrackets - ($beginnOfTemplate + 9));
$templates[] = $templateName;
}
}
return $templates;
}