<?php
/**
* Example of how to create a custom article editing form.
* Created July 2006.
*
* By Barry Brannan (http://www.mediawiki.org/wiki/User:Barrylb)
*
* 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 next edit the article, this form extracts the fields from 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.
* To enable this extension place the following in LocalSettings.php:
* require_once("extensions/CustomEdit.php");
*
* This example is released to the public domain.
*/
$wgExtensionCredits['other'][] = array(
'name' => 'CustomEdit',
'version' => 0.2,
'author' => 'Barry Brannan',
'url' => 'http://www.mediawiki.org/wiki/User:Barrylb/Custom_article_editing_form_with_fields',
'description' => 'Example of how to create a custom article editing form',
);
if( defined( 'MEDIAWIKI' ) ) {
global $wgHooks;
$wgHooks['AlternateEdit'][] = 'fnMyCustomEdit';
function fnMyCustomEdit(&$editpage) {
global $wgOut, $wgRequest, $wgParser, $wgUser;
if (strpos($editpage->mTitle->getText(),'Person/') === 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' ) );
$personAge = $wgRequest->getText( 'wpAge' );
$personSex = $wgRequest->getText( 'wpSex' );
$summary = $wgRequest->getText( 'wpSummary' );
}
else {
if ($editpage->mTitle->exists()) {
$text = $editpage->mArticle->getContent();
$personDescription = preg_replace('/\{\{[^\}]*\}\}/', '', $text);
preg_match('/age=(.*)\n/i', $text, $matches);
$personAge = $matches[1];
preg_match('/sex=(.*)\}\}/i', $text, $matches);
$personSex = $matches[1];
$summary = '';
}
else {
$personDescription = '';
$personAge = '';
$personSex = '';
$summary = '';
}
}
$editpage->textbox1 = "{{person\n" .
"|age=$personAge\n" .
"|sex=$personSex}}\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' )) {
$editpage->showDiff();
}
else {
//if ($editpage->previewOnOpen() || $wgRequest->wasPosted() ) { removed by User:Flominator as reported on [[User_talk:Barrylb/Custom_article_editing_form_with_fields#Bug_with_previewOnOpen.3F]]
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>');
}
$wgOut->setOnloadHandler( 'document.editform.wpAge.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">');
$wgOut->addHTML('<tr><td>Age:</td><td><input id="wpAge" name="wpAge" type="text" size="20" value="' . $personAge . '"/></td></tr>');
$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;
}
}
else {
echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" );
die( -1 );
}