r40016 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r40015‎ | r40016 | r40017 >
Date:14:07, 26 August 2008
Author:dantman
Status:old
Tags:
Comment:
Commit initial portion of EditPage refactoring.
This isn't complete, but nothing is broken.
The goal here is to make it possible for extensions to subclass the editpage and override certain areas of it while allowing the rest to keep it's presence without needing to copy code from core which may change.
* Use $this->action instead of hardcoded 'submit's so this may be overridden for subclasses that use a different action=...
* Move all the header setting into a new setHeaders() function. This way subclasses can tweak the title, this was previously impossible because of how scattered setPageTitle calls were.
* Move the previewarea stuff that was being duplicated in 2 places into it's own function.
* Move some inputs into a beforetext function
* Move wpTextbox1 into a special function. And create a textbox function to unify textarea creation. wpTextbox1 and wpTextbox2 now have a unified style due to this. (Though the metadata textarea could use this applied to it as well)
* Move edittools into a function.
* In getPreviewText;
** Don't use $wgOut
** Move template and other parseroutput related code outside of the function and simply set a $this->parserOutput variable while we're in here. (We should probably also move the $wgOut->addParserOutputNoText call somewhere else, and move most of the preview parsing into one of the initialization functions.
Modified paths:
  • /trunk/phase3/includes/EditPage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/EditPage.php
@@ -44,6 +44,7 @@
4545
4646 var $mArticle;
4747 var $mTitle;
 48+ var $action;
4849 var $mMetaData = '';
4950 var $isConflict = false;
5051 var $isCssJsSubpage = false;
@@ -61,7 +62,8 @@
6263 var $allowBlankSummary = false;
6364 var $autoSumm = '';
6465 var $hookError = '';
65 - var $mPreviewTemplates;
 66+ #var $mPreviewTemplates;
 67+ var $mParserOutput;
6668 var $mBaseRevision = false;
6769
6870 # Form values
@@ -92,6 +94,7 @@
9395 function EditPage( $article ) {
9496 $this->mArticle =& $article;
9597 $this->mTitle = $article->getTitle();
 98+ $this->action = 'submit';
9699
97100 # Placeholders for text injection by hooks (empty per default)
98101 $this->editFormPageTop =
@@ -1059,6 +1062,30 @@
10601063 return true;
10611064 }
10621065
 1066+ function setHeaders() {
 1067+ global $wgOut, $wgTitle;
 1068+ $wgOut->setRobotPolicy( 'noindex,nofollow' );
 1069+ if ( $this->formtype == 'preview' ) {
 1070+ $wgOut->setPageTitleActionText( wfMsg( 'preview' ) );
 1071+ }
 1072+ if ( $this->isConflict ) {
 1073+ $wgOut->setPageTitle( wfMsg( 'editconflict', $wgTitle->getPrefixedText() ) );
 1074+ }
 1075+ if( $this->section != '' ) {
 1076+ $msg = $this->section == 'new' ? 'editingcomment' : 'editingsection';
 1077+ $wgOut->setPageTitle( wfMsg( $msg, $wgTitle->getPrefixedText() ) );
 1078+ } else {
 1079+ # Use the title defined by DISPLAYTITLE magic word when present
 1080+ if( isset($this->mParserOutput)
 1081+ && ( $dt = $this->mParserOutput->getDisplayTitle() ) !== false ) {
 1082+ $title = $dt;
 1083+ } else {
 1084+ $title = $wgTitle->getPrefixedText();
 1085+ }
 1086+ $wgOut->setPageTitle( wfMsg( 'editing', $title ) );
 1087+ }
 1088+ }
 1089+
10631090 /**
10641091 * Send the edit form and related headers to $wgOut
10651092 * @param $formCallback Optional callable that takes an OutputPage
@@ -1082,46 +1109,41 @@
10831110
10841111 wfRunHooks( 'EditPage::showEditForm:initial', array( &$this ) ) ;
10851112
1086 - $wgOut->setRobotPolicy( 'noindex,nofollow' );
 1113+ #need to parse the preview early so that we know which templates are used,
 1114+ #otherwise users with "show preview after edit box" will get a blank list
 1115+ #we parse this near the beginning so that setHeaders can do the title
 1116+ #setting work instead of leaving it in getPreviewText
 1117+ $previewOutput = '';
 1118+ if ( $this->formtype == 'preview' ) {
 1119+ $previewOutput = $this->getPreviewText();
 1120+ }
10871121
 1122+ $this->setHeaders();
 1123+
10881124 # Enabled article-related sidebar, toplinks, etc.
10891125 $wgOut->setArticleRelated( true );
10901126
1091 - if ( $this->formtype == 'preview' ) {
1092 - $wgOut->setPageTitleActionText( wfMsg( 'preview' ) );
1093 - }
1094 -
10951127 if ( $this->isConflict ) {
1096 - $s = wfMsg( 'editconflict', $wgTitle->getPrefixedText() );
1097 - $wgOut->setPageTitle( $s );
10981128 $wgOut->addWikiMsg( 'explainconflict' );
10991129
11001130 $this->textbox2 = $this->textbox1;
11011131 $this->textbox1 = $this->getContent();
11021132 $this->edittime = $this->mArticle->getTimestamp();
11031133 } else {
1104 - if( $this->section != '' ) {
1105 - if( $this->section == 'new' ) {
1106 - $s = wfMsg('editingcomment', $wgTitle->getPrefixedText() );
1107 - } else {
1108 - $s = wfMsg('editingsection', $wgTitle->getPrefixedText() );
1109 - $matches = array();
1110 - if( !$this->summary && !$this->preview && !$this->diff ) {
1111 - preg_match( "/^(=+)(.+)\\1/mi",
1112 - $this->textbox1,
1113 - $matches );
1114 - if( !empty( $matches[2] ) ) {
1115 - global $wgParser;
1116 - $this->summary = "/* " .
1117 - $wgParser->stripSectionName(trim($matches[2])) .
1118 - " */ ";
1119 - }
 1134+ if( $this->section != '' && $this->section != 'new' ) {
 1135+ $matches = array();
 1136+ if( !$this->summary && !$this->preview && !$this->diff ) {
 1137+ preg_match( "/^(=+)(.+)\\1/mi",
 1138+ $this->textbox1,
 1139+ $matches );
 1140+ if( !empty( $matches[2] ) ) {
 1141+ global $wgParser;
 1142+ $this->summary = "/* " .
 1143+ $wgParser->stripSectionName(trim($matches[2])) .
 1144+ " */ ";
11201145 }
11211146 }
1122 - } else {
1123 - $s = wfMsg( 'editing', $wgTitle->getPrefixedText() );
11241147 }
1125 - $wgOut->setPageTitle( $s );
11261148
11271149 if ( $this->missingComment ) {
11281150 $wgOut->wrapWikiMsg( '<div id="mw-missingcommenttext">$1</div>', 'missingcommenttext' );
@@ -1217,20 +1239,7 @@
12181240 $wgOut->addHTML( "</div>\n" );
12191241 }
12201242
1221 - #need to parse the preview early so that we know which templates are used,
1222 - #otherwise users with "show preview after edit box" will get a blank list
1223 - if ( $this->formtype == 'preview' ) {
1224 - $previewOutput = $this->getPreviewText();
1225 - }
1226 -
1227 - $rows = $wgUser->getIntOption( 'rows' );
1228 - $cols = $wgUser->getIntOption( 'cols' );
1229 -
1230 - $ew = $wgUser->getOption( 'editwidth' );
1231 - if ( $ew ) $ew = " style=\"width:100%\"";
1232 - else $ew = '';
1233 -
1234 - $q = 'action=submit';
 1243+ $q = 'action='.$this->action;
12351244 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
12361245 $action = $wgTitle->escapeLocalURL( $q );
12371246
@@ -1282,16 +1291,9 @@
12831292 $wgOut->addHTML( $this->editFormPageTop );
12841293
12851294 if ( $wgUser->getOption( 'previewontop' ) ) {
1286 -
1287 - if ( 'preview' == $this->formtype ) {
1288 - $this->showPreview( $previewOutput );
1289 - } else {
1290 - $wgOut->addHTML( '<div id="wikiPreview"></div>' );
1291 - }
1292 -
1293 - if ( 'diff' == $this->formtype ) {
1294 - $this->showDiff();
1295 - }
 1295+ $this->displayPreviewArea( $previewOutput );
 1296+ // Spacer for the edit toolbar
 1297+ $wgOut->addHTML( '<p><br /></p>' );
12961298 }
12971299
12981300
@@ -1330,7 +1332,7 @@
13311333 if( !$this->preview && !$this->diff ) {
13321334 $wgOut->setOnloadHandler( 'document.editform.wpTextbox1.focus()' );
13331335 }
1334 - $templates = ($this->preview || $this->section != '') ? $this->mPreviewTemplates : $this->mArticle->getUsedTemplates();
 1336+ $templates = $this->getTemplates();
13351337 $formattedtemplates = $sk->formatTemplates( $templates, $this->preview, $this->section != '');
13361338
13371339 $hiddencats = $this->mArticle->getHiddenCategories();
@@ -1341,11 +1343,15 @@
13421344 $metadata = $this->mMetaData ;
13431345 $metadata = htmlspecialchars( $wgContLang->recodeForEdit( $metadata ) ) ;
13441346 $top = wfMsgWikiHtml( 'metadata_help' );
 1347+ /* ToDo: Replace with clean code */
 1348+ $ew = $wgUser->getOption( 'editwidth' );
 1349+ if ( $ew ) $ew = " style=\"width:100%\"";
 1350+ else $ew = '';
 1351+ /* /ToDo */
13451352 $metadata = $top . "<textarea name='metadata' rows='3' cols='{$cols}'{$ew}>{$metadata}</textarea>" ;
13461353 }
13471354 else $metadata = "" ;
13481355
1349 - $hidden = '';
13501356 $recreate = '';
13511357 if ($this->wasDeletedSinceLastEdit()) {
13521358 if ( 'save' != $this->formtype ) {
@@ -1354,7 +1360,6 @@
13551361 // Hide the toolbar and edit area, use can click preview to get it back
13561362 // Add an confirmation checkbox and explanation.
13571363 $toolbar = '';
1358 - $hidden = 'type="hidden" style="display:none;"';
13591364 $recreate = $wgOut->parse( wfMsg( 'confirmrecreate', $this->lastDelete->user_name , $this->lastDelete->log_comment ));
13601365 $recreate .=
13611366 "<br /><input tabindex='1' type='checkbox' value='1' name='wpRecreate' id='wpRecreate' />".
@@ -1388,40 +1393,27 @@
13891394 wfRunHooks( 'EditPage::showEditForm:fields', array( &$this, &$wgOut ) );
13901395
13911396 // Put these up at the top to ensure they aren't lost on early form submission
1392 - $wgOut->addHTML( "
1393 -<input type='hidden' value=\"" . htmlspecialchars( $this->section ) . "\" name=\"wpSection\" />
1394 -<input type='hidden' value=\"{$this->starttime}\" name=\"wpStarttime\" />\n
1395 -<input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n
1396 -<input type='hidden' value=\"{$this->scrolltop}\" name=\"wpScrolltop\" id=\"wpScrolltop\" />\n" );
 1397+ $this->showFormBeforeText();
13971398
1398 - $encodedtext = htmlspecialchars( $this->safeUnicodeOutput( $this->textbox1 ) );
1399 - if( $encodedtext !== '' ) {
1400 - // Ensure there's a newline at the end, otherwise adding lines
1401 - // is awkward.
1402 - // But don't add a newline if the ext is empty, or Firefox in XHTML
1403 - // mode will show an extra newline. A bit annoying.
1404 - $encodedtext .= "\n";
1405 - }
1406 -
14071399 $wgOut->addHTML( <<<END
1408 -$recreate
 1400+{$recreate}
14091401 {$commentsubject}
14101402 {$subjectpreview}
14111403 {$this->editFormTextBeforeContent}
1412 -<textarea tabindex='1' accesskey="," name="wpTextbox1" id="wpTextbox1" rows='{$rows}'
1413 -cols='{$cols}'{$ew} $hidden>{$encodedtext}</textarea>
14141404 END
14151405 );
 1406+ $this->showTextbox1();
14161407
14171408 $wgOut->wrapWikiMsg( "<div id=\"editpage-copywarn\">\n$1\n</div>", $copywarnMsg );
1418 - $wgOut->addHTML( $this->editFormTextAfterWarn );
1419 - $wgOut->addHTML( "
 1409+ $wgOut->addHTML( <<<END
 1410+{$this->editFormTextAfterWarn}
14201411 {$metadata}
14211412 {$editsummary}
14221413 {$summarypreview}
14231414 {$checkboxhtml}
14241415 {$safemodehtml}
1425 -");
 1416+END
 1417+);
14261418
14271419 $wgOut->addHTML(
14281420 "<div class='editButtons'>
@@ -1445,20 +1437,18 @@
14461438 $token = htmlspecialchars( $wgUser->editToken() );
14471439 $wgOut->addHTML( "\n<input type='hidden' value=\"$token\" name=\"wpEditToken\" />\n" );
14481440
1449 - $wgOut->addHtml( '<div class="mw-editTools">' );
1450 - $wgOut->addWikiMsgArray( 'edittools', array(), array( 'content' ) );
1451 - $wgOut->addHtml( '</div>' );
 1441+ $this->showEditTools();
14521442
1453 - $wgOut->addHTML( $this->editFormTextAfterTools );
1454 -
1455 - $wgOut->addHTML( "
 1443+ $wgOut->addHTML( <<<END
 1444+{$this->editFormTextAfterTools}
14561445 <div class='templatesUsed'>
14571446 {$formattedtemplates}
14581447 </div>
14591448 <div class='hiddencats'>
14601449 {$formattedhiddencats}
14611450 </div>
1462 -");
 1451+END
 1452+);
14631453
14641454 if ( $this->isConflict && wfRunHooks( 'EditPageBeforeConflictDiff', array( &$this, &$wgOut ) ) ) {
14651455 $wgOut->wrapWikiMsg( '==$1==', "yourdiff" );
@@ -1468,28 +1458,91 @@
14691459 $de->showDiff( wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
14701460
14711461 $wgOut->wrapWikiMsg( '==$1==', "yourtext" );
1472 - $wgOut->addHTML( "<textarea tabindex='6' id='wpTextbox2' name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}'>"
1473 - . htmlspecialchars( $this->safeUnicodeOutput( $this->textbox2 ) ) . "\n</textarea>" );
 1462+ $this->showTextbox2();
14741463 }
14751464 $wgOut->addHTML( $this->editFormTextBottom );
14761465 $wgOut->addHTML( "</form>\n" );
14771466 if ( !$wgUser->getOption( 'previewontop' ) ) {
 1467+ $this->displayPreviewArea( $previewOutput );
 1468+ }
14781469
1479 - if ( $this->formtype == 'preview') {
1480 - $this->showPreview( $previewOutput );
1481 - } else {
1482 - $wgOut->addHTML( '<div id="wikiPreview"></div>' );
1483 - }
 1470+ wfProfileOut( $fname );
 1471+ }
14841472
1485 - if ( $this->formtype == 'diff') {
1486 - $this->showDiff();
1487 - }
 1473+ protected function showFormBeforeText() {
 1474+ return "
 1475+<input type='hidden' value=\"" . htmlspecialchars( $this->section ) . "\" name=\"wpSection\" />
 1476+<input type='hidden' value=\"{$this->starttime}\" name=\"wpStarttime\" />\n
 1477+<input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n
 1478+<input type='hidden' value=\"{$this->scrolltop}\" name=\"wpScrolltop\" id=\"wpScrolltop\" />\n";
 1479+ }
 1480+
 1481+ protected function showTextbox1() {
 1482+ $attribs = array( 'tabindex' => 1 );
 1483+
 1484+ if( $this->wasDeletedSinceLastEdit() )
 1485+ $attribs['type'] = 'hidden';
 1486+
 1487+ $this->showTextbox( $this->textbox1, 'wpTextbox1', $attribs );
 1488+ }
 1489+
 1490+ protected function showTextbox2() {
 1491+ $this->showTextbox( $encodedtext, 'wpTextbox2', array( 'tabindex' => 6 ) );
 1492+ }
 1493+
 1494+ protected function showTextbox( $content, $name, $attribs = array() ) {
 1495+ global $wgOut, $wgUser;
 1496+
 1497+ $encodedtext = htmlspecialchars( $this->safeUnicodeOutput( $content ) );
 1498+ if( $encodedtext !== '' ) {
 1499+ // Ensure there's a newline at the end, otherwise adding lines
 1500+ // is awkward.
 1501+ // But don't add a newline if the ext is empty, or Firefox in XHTML
 1502+ // mode will show an extra newline. A bit annoying.
 1503+ $encodedtext .= "\n";
 1504+ }
 1505+
 1506+ $attribs = array(
 1507+ 'accesskey' => ',',
 1508+ 'id' => $name,
 1509+ );
 1510+
 1511+ if( $wgUser->getOption( 'editwidth' ) )
 1512+ $attribs['style'] = 'width: 100%';
 1513+
 1514+ $wgOut->addHTML( Xml::textarea(
 1515+ $name,
 1516+ $encodedtext,
 1517+ $wgUser->getIntOption( 'cols' ), $wgUser->getIntOption( 'rows' ),
 1518+ $attribs ) );
 1519+ }
14881520
 1521+ protected function displayPreviewArea( $previewOutput ) {
 1522+ global $wgOut;
 1523+ if ( $this->formtype == 'preview') {
 1524+ $this->showPreview( $previewOutput );
 1525+ } else {
 1526+ $wgOut->addHTML( '<div id="wikiPreview"></div>' );
14891527 }
14901528
1491 - wfProfileOut( $fname );
 1529+ if ( $this->formtype == 'diff') {
 1530+ $this->showDiff();
 1531+ }
14921532 }
 1533+
 1534+ protected function displayPreviewArea( $previewOutput ) {
 1535+ global $wgOut;
 1536+ if ( $this->formtype == 'preview') {
 1537+ $this->showPreview( $previewOutput );
 1538+ } else {
 1539+ $wgOut->addHTML( '<div id="wikiPreview"></div>' );
 1540+ }
14931541
 1542+ if ( $this->formtype == 'diff') {
 1543+ $this->showDiff();
 1544+ }
 1545+ }
 1546+
14941547 /**
14951548 * Append preview output to $wgOut.
14961549 * Includes category rendering if this is a category page.
@@ -1524,12 +1577,19 @@
15251578 function doLivePreviewScript() {
15261579 global $wgOut, $wgTitle;
15271580 $wgOut->addScriptFile( 'preview.js' );
1528 - $liveAction = $wgTitle->getLocalUrl( 'action=submit&wpPreview=true&live=true' );
 1581+ $liveAction = $wgTitle->getLocalUrl( "action={$this->action}&wpPreview=true&live=true" );
15291582 return "return !lpDoPreview(" .
15301583 "editform.wpTextbox1.value," .
15311584 '"' . $liveAction . '"' . ")";
15321585 }
15331586
 1587+ protected function showEditTools() {
 1588+ global $wgOut;
 1589+ $wgOut->addHtml( '<div class="mw-editTools">' );
 1590+ $wgOut->addWikiMsgArray( 'edittools', array(), array( 'content' ) );
 1591+ $wgOut->addHtml( '</div>' );
 1592+ }
 1593+
15341594 function getLastDelete() {
15351595 $dbr = wfGetDB( DB_SLAVE );
15361596 $fname = 'EditPage::getLastDelete';
@@ -1569,8 +1629,7 @@
15701630 function getPreviewText() {
15711631 global $wgOut, $wgUser, $wgTitle, $wgParser, $wgLang, $wgContLang;
15721632
1573 - $fname = 'EditPage::getPreviewText';
1574 - wfProfileIn( $fname );
 1633+ wfProfileIn( __METHOD__ );
15751634
15761635 if ( $this->mTriedSave && !$this->mTokenOk ) {
15771636 if ( $this->mTokenOkExceptSuffix ) {
@@ -1604,9 +1663,9 @@
16051664 }
16061665 $parserOptions->setTidy(true);
16071666 $parserOutput = $wgParser->parse( $previewtext , $this->mTitle, $parserOptions );
1608 - $wgOut->addHTML( $parserOutput->mText );
 1667+ //$wgOut->addHTML( $parserOutput->mText );
16091668 $previewHTML = '';
1610 - } else if( $rt = Title::newFromRedirect( $this->textbox1 ) ) {
 1669+ } elseif( $rt = Title::newFromRedirect( $this->textbox1 ) ) {
16111670 $previewHTML = $this->mArticle->viewRedirect( $rt, false );
16121671 } else {
16131672 $toparse = $this->textbox1;
@@ -1643,20 +1702,9 @@
16441703 $this->mTitle, $parserOptions );
16451704
16461705 $previewHTML = $parserOutput->getText();
 1706+ $this->mParserOutput = $parserOutput;
16471707 $wgOut->addParserOutputNoText( $parserOutput );
16481708
1649 - # ParserOutput might have altered the page title, so reset it
1650 - # Also, use the title defined by DISPLAYTITLE magic word when present
1651 - if( ( $dt = $parserOutput->getDisplayTitle() ) !== false ) {
1652 - $wgOut->setPageTitle( wfMsg( 'editing', $dt ) );
1653 - } else {
1654 - $wgOut->setPageTitle( wfMsg( 'editing', $wgTitle->getPrefixedText() ) );
1655 - }
1656 -
1657 - foreach ( $parserOutput->getTemplates() as $ns => $template)
1658 - foreach ( array_keys( $template ) as $dbk)
1659 - $this->mPreviewTemplates[] = Title::makeTitle($ns, $dbk);
1660 -
16611709 if ( count( $parserOutput->getWarnings() ) ) {
16621710 $note .= "\n\n" . implode( "\n\n", $parserOutput->getWarnings() );
16631711 }
@@ -1665,18 +1713,26 @@
16661714 $previewhead = '<h2>' . htmlspecialchars( wfMsg( 'preview' ) ) . "</h2>\n" .
16671715 "<div class='previewnote'>" . $wgOut->parse( $note ) . "</div>\n";
16681716 if ( $this->isConflict ) {
1669 - $previewhead.='<h2>' . htmlspecialchars( wfMsg( 'previewconflict' ) ) . "</h2>\n";
 1717+ $previewhead .='<h2>' . htmlspecialchars( wfMsg( 'previewconflict' ) ) . "</h2>\n";
16701718 }
16711719
1672 - if( $wgUser->getOption( 'previewontop' ) ) {
1673 - // Spacer for the edit toolbar
1674 - $previewfoot = '<p><br /></p>';
 1720+ wfProfileOut( __METHOD__ );
 1721+ return $previewhead . $previewHTML;
 1722+ }
 1723+
 1724+ function getTemplates() {
 1725+ if( $this->preview || $this->section != '' ) {
 1726+ $templates = array();
 1727+ if( !isset($this->mParserOutput) ) return $templates;
 1728+ foreach( $this->mParserOutput->getTemplates() as $ns => $template) {
 1729+ foreach( array_keys( $template ) as $dbk ) {
 1730+ $templates[] = Title::makeTitle($ns, $dbk);
 1731+ }
 1732+ }
 1733+ return $templates;
16751734 } else {
1676 - $previewfoot = '';
 1735+ return $this->mArticle->getUsedTemplates();
16771736 }
1678 -
1679 - wfProfileOut( $fname );
1680 - return $previewhead . $previewHTML . $previewfoot;
16811737 }
16821738
16831739 /**
@@ -1697,8 +1753,8 @@
16981754
16991755 # Spit out the source or the user's modified version
17001756 if( $source !== false ) {
1701 - $rows = $wgUser->getOption( 'rows' );
1702 - $cols = $wgUser->getOption( 'cols' );
 1757+ $rows = $wgUser->getIntOption( 'rows' );
 1758+ $cols = $wgUser->getIntOption( 'cols' );
17031759 $attribs = array( 'id' => 'wpTextbox1', 'name' => 'wpTextbox1', 'cols' => $cols, 'rows' => $rows, 'readonly' => 'readonly' );
17041760 $wgOut->addHtml( '<hr />' );
17051761 $wgOut->addWikiMsg( $first ? 'blockedoriginalsource' : 'blockededitsource', $this->mTitle->getPrefixedText() );

Status & tagging log