User:Barrylb/Custom article editing form with fields/CustomEdit.php

<?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: *  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. */

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]; }			else { $personDescription = ''; $personAge = ''; $personSex = ''; $summary = ''; }							}		$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 ); }			else { if ($editpage->previewOnOpen || $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(' '); $wgOut->addHTML($previewHTML); $wgOut->addHTML(' '); }				$wgOut->setOnloadHandler( 'document.editform.wpAge.focus' ); }

$wgOut->addHTML('mTitle->escapeLocalURL("action=submit") . '" enctype="multipart/form-data">'); $wgOut->addHTML(' '); $wgOut->addHTML(''); $wgOut->addHTML(' '); $wgOut->addHTML(' '); $wgOut->addHTML(' '); }		return false; }		else return true;

}

} else { echo( "This is an extension to the MediaWiki package and cannot be run standalone.\n" ); die( -1 ); }

?>