r106754 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106753‎ | r106754 | r106755 >
Date:04:15, 20 December 2011
Author:kaldari
Status:resolved (Comments)
Tags:
Comment:
adding sectiontitle/wgSectionTitle as a new parameter for page editing (so that it can be set separately from the edit summary). For right now, this is just for API use, and thus isnt used in the form. As soon as 1.19 is out the door, we should change the form to use this as well. The current implementation is designed to be completely backward-compatible and non-disruptive
Modified paths:
  • /trunk/phase3/includes/EditPage.php (modified) (history)
  • /trunk/phase3/includes/api/ApiEditPage.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/EditPage.php
@@ -91,7 +91,7 @@
9292 var $save = false, $preview = false, $diff = false;
9393 var $minoredit = false, $watchthis = false, $recreate = false;
9494 var $textbox1 = '', $textbox2 = '', $summary = '', $nosummary = false;
95 - var $edittime = '', $section = '', $starttime = '';
 95+ var $edittime = '', $section = '', $sectiontitle = '', $starttime = '';
9696 var $oldid = 0, $editintro = '', $scrolltop = null, $bot = true;
9797
9898 # Placeholders for text injection by hooks (must be HTML)
@@ -480,7 +480,7 @@
481481 }
482482
483483 /**
484 - * @todo document
 484+ * This function collects the form data and uses it to populate various member variables.
485485 * @param $request WebRequest
486486 */
487487 function importFormData( &$request ) {
@@ -510,15 +510,25 @@
511511 # Truncate for whole multibyte characters. +5 bytes for ellipsis
512512 $this->summary = $wgLang->truncate( $request->getText( 'wpSummary' ), 250 );
513513
514 - # Remove extra headings from summaries and new sections.
515 - $this->summary = preg_replace('/^\s*=+\s*(.*?)\s*=+\s*$/', '$1', $this->summary);
 514+ # If the summary consists of a heading, e.g. '==Foobar==', extract the title from the
 515+ # header syntax, e.g. 'Foobar'. This is mainly an issue when we are using wpSummary for
 516+ # section titles.
 517+ $this->summary = preg_replace( '/^\s*=+\s*(.*?)\s*=+\s*$/', '$1', $this->summary );
 518+
 519+ # Treat sectiontitle the same way as summary.
 520+ # Note that wpSectionTitle is not yet a part of the actual edit form, as wpSummary is
 521+ # currently doing double duty as both edit summary and section title. Right now this
 522+ # is just to allow API edits to work around this limitation, but this should be
 523+ # incorporated into the actual edit form when EditPage is rewritten (Bugs 18654, 26312).
 524+ $this->sectiontitle = $wgLang->truncate( $request->getText( 'wpSectionTitle' ), 250 );
 525+ $this->sectiontitle = preg_replace( '/^\s*=+\s*(.*?)\s*=+\s*$/', '$1', $this->sectiontitle );
516526
517527 $this->edittime = $request->getVal( 'wpEdittime' );
518528 $this->starttime = $request->getVal( 'wpStarttime' );
519529
520530 $this->scrolltop = $request->getIntOrNull( 'wpScrolltop' );
521531
522 - if ($this->textbox1 === '' && $request->getVal( 'wpTextbox1' ) === null) {
 532+ if ( $this->textbox1 === '' && $request->getVal( 'wpTextbox1' ) === null ) {
523533 // wpTextbox1 field is missing, possibly due to being "too big"
524534 // according to some filter rules such as Suhosin's setting for
525535 // suhosin.request.max_value_length (d'oh)
@@ -585,19 +595,24 @@
586596 } else {
587597 # Not a posted form? Start with nothing.
588598 wfDebug( __METHOD__ . ": Not a posted form.\n" );
589 - $this->textbox1 = '';
590 - $this->summary = '';
591 - $this->edittime = '';
592 - $this->starttime = wfTimestampNow();
593 - $this->edit = false;
594 - $this->preview = false;
595 - $this->save = false;
596 - $this->diff = false;
597 - $this->minoredit = false;
598 - $this->watchthis = $request->getBool( 'watchthis', false ); // Watch may be overriden by request parameters
599 - $this->recreate = false;
600 -
 599+ $this->textbox1 = '';
 600+ $this->summary = '';
 601+ $this->sectiontitle = '';
 602+ $this->edittime = '';
 603+ $this->starttime = wfTimestampNow();
 604+ $this->edit = false;
 605+ $this->preview = false;
 606+ $this->save = false;
 607+ $this->diff = false;
 608+ $this->minoredit = false;
 609+ $this->watchthis = $request->getBool( 'watchthis', false ); // Watch may be overriden by request parameters
 610+ $this->recreate = false;
 611+
 612+ // When creating a new section, we can preload a section title by passing it as the
 613+ // preloadtitle parameter in the URL (Bug 13100)
601614 if ( $this->section == 'new' && $request->getVal( 'preloadtitle' ) ) {
 615+ $this->sectiontitle = $request->getVal( 'preloadtitle' );
 616+ // Once wpSummary isn't being use for setting section titles, we should delete this.
602617 $this->summary = $request->getVal( 'preloadtitle' );
603618 }
604619 elseif ( $this->section != 'new' && $request->getVal( 'summary' ) ) {
@@ -1116,16 +1131,32 @@
11171132
11181133 $text = $this->textbox1;
11191134 $result['sectionanchor'] == '';
1120 - if ( $this->section == 'new' && $this->summary != '' ) {
1121 - $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $this->summary ) . "\n\n" . $text;
1122 -
1123 - # Jump to the new section
1124 - $result['sectionanchor'] = $wgParser->guessLegacySectionNameFromWikiText( $this->summary );
 1135+ if ( $this->section == 'new' ) {
 1136+ if ( $this->sectiontitle != '' ) {
 1137+ // Insert the section title above the content.
 1138+ $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $this->sectiontitle ) . "\n\n" . $text;
 1139+
 1140+ // Jump to the new section
 1141+ $result['sectionanchor'] = $wgParser->guessLegacySectionNameFromWikiText( $this->sectiontitle );
 1142+
 1143+ // If no edit summary was specified, create one automatically from the section
 1144+ // title and have it link to the new section. Otherwise, respect the summary as
 1145+ // passed.
 1146+ if ( $this->summary == '' ) {
 1147+ $cleanSectionTitle = $wgParser->stripSectionName( $this->sectiontitle );
 1148+ $this->summary = wfMsgForContent( 'newsectionsummary', $cleanSectionTitle );
 1149+ }
 1150+ } elseif ( $this->summary != '' ) {
 1151+ // Insert the section title above the content.
 1152+ $text = wfMsgForContent( 'newsectionheaderdefaultlevel', $this->summary ) . "\n\n" . $text;
 1153+
 1154+ // Jump to the new section
 1155+ $result['sectionanchor'] = $wgParser->guessLegacySectionNameFromWikiText( $this->summary );
11251156
1126 - # This is a new section, so create a link to the new section
1127 - # in the revision summary.
1128 - $cleanSummary = $wgParser->stripSectionName( $this->summary );
1129 - $this->summary = wfMsgForContent( 'newsectionsummary', $cleanSummary );
 1157+ // Create a link to the new section from the edit summary.
 1158+ $cleanSummary = $wgParser->stripSectionName( $this->summary );
 1159+ $this->summary = wfMsgForContent( 'newsectionsummary', $cleanSummary );
 1160+ }
11301161 }
11311162
11321163 $status->value = self::AS_SUCCESS_NEW_ARTICLE;
Index: trunk/phase3/includes/api/ApiEditPage.php
@@ -181,6 +181,10 @@
182182 if ( !is_null( $params['summary'] ) ) {
183183 $reqArr['wpSummary'] = $params['summary'];
184184 }
 185+
 186+ if ( !is_null( $params['sectiontitle'] ) ) {
 187+ $reqArr['wpSectionTitle'] = $params['sectiontitle'];
 188+ }
185189
186190 // Watch out for basetimestamp == ''
187191 // wfTimestamp() treats it as NOW, almost certainly causing an edit conflict
@@ -404,6 +408,10 @@
405409 ApiBase::PARAM_REQUIRED => true
406410 ),
407411 'section' => null,
 412+ 'sectiontitle' => array(
 413+ ApiBase::PARAM_TYPE => 'string',
 414+ ApiBase::PARAM_REQUIRED => false,
 415+ ),
408416 'text' => null,
409417 'token' => null,
410418 'summary' => null,
@@ -453,6 +461,7 @@
454462 return array(
455463 'title' => 'Page title',
456464 'section' => 'Section number. 0 for the top section, \'new\' for a new section',
 465+ 'sectiontitle' => 'The title for a new section',
457466 'text' => 'Page content',
458467 'token' => array( 'Edit token. You can get one of these through prop=info.',
459468 'The token should always be sent as the last parameter, or at least, after the text parameter'

Sign-offs

UserFlagDate
Krinkleinspected11:33, 29 December 2011

Follow-up revisions

RevisionCommit summaryAuthorDate
r106895follow-up to r106754 - using strict comparison, adding logic for editing exis...kaldari23:50, 20 December 2011

Comments

#Comment by Nikerabbit (talk | contribs)   10:56, 20 December 2011

Can we use === and !== ? It doesn't change anything here but == '' always triggers investigation mode in my brain.

#Comment by Kaldari (talk | contribs)   22:42, 20 December 2011

Good idea. Also it looks like I missed adding the logic for existing pages (rather than just new pages).

#Comment by Kaldari (talk | contribs)   23:54, 20 December 2011

Fixed in r106895.

#Comment by Catrope (talk | contribs)   18:02, 21 December 2011
+			$this->sectiontitle = $wgLang->truncate( $request->getText( 'wpSectionTitle' ), 250 );

Why would you truncate sectiontitle to 250 chars? This makes sense for the summary, but I don't see how it makes sense for the section title.

+			if ( $this->section == 'new' ) {
+				if ( $this->sectiontitle != '' ) {
+					// Insert the section title above the content.
+					$text = wfMsgForContent( 'newsectionheaderdefaultlevel', $this->sectiontitle ) . "\n\n" . $text;
+					
+					// Jump to the new section
+					$result['sectionanchor'] = $wgParser->guessLegacySectionNameFromWikiText( $this->sectiontitle );
+					
+					// If no edit summary was specified, create one automatically from the section
+					// title and have it link to the new section. Otherwise, respect the summary as
+					// passed.
+					if ( $this->summary == '' ) {
+						$cleanSectionTitle = $wgParser->stripSectionName( $this->sectiontitle );
+						$this->summary = wfMsgForContent( 'newsectionsummary', $cleanSectionTitle );
+					}
+				} elseif ( $this->summary != '' ) {
+					// Insert the section title above the content.
+					$text = wfMsgForContent( 'newsectionheaderdefaultlevel', $this->summary ) . "\n\n" . $text;
+					
+					// Jump to the new section
+					$result['sectionanchor'] = $wgParser->guessLegacySectionNameFromWikiText( $this->summary );
 
+					// Create a link to the new section from the edit summary.
+					$cleanSummary = $wgParser->stripSectionName( $this->summary );
+					$this->summary = wfMsgForContent( 'newsectionsummary', $cleanSummary );
+				}

There is a lot of code duplication between the if and the else branch, can't you condense this somehow?

Status & tagging log