From 76f3bb9227a13e31c245adf1363e8d202a0f4aef Mon Sep 17 00:00:00 2001 From: Tim Starling Date: Mon, 5 Sep 2011 06:56:08 +0000 Subject: [PATCH] Reverted r84357 and r90461 and fixed the bug in a better way: in an empty document, consider section zero to exist, and to be empty, effectively representing the whole document. This allows section zero of a blank page to be viewed and edited with no updates to EditPage. --- includes/parser/Parser.php | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index e168eca376..6ff1e447f7 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -5291,12 +5291,14 @@ class Parser { * pull the given section along with its lower-level subsections. If the section is * not found, $mode=get will return $newtext, and $mode=replace will return $text. * + * Section 0 is always considered to exist, even if it only contains the empty + * string. If $text is the empty string and section 0 is replaced, $newText is + * returned. + * * @param $mode String: one of "get" or "replace" * @param $newText String: replacement text for section data. * @return String: for "get", the extracted section text. * for "replace", the whole page with the section replaced. - * If the page is empty and section 0 is requested, $text (as '') - * is returned */ private function extractSections( $text, $section, $mode, $newText='' ) { global $wgTitle; # not generally used but removes an ugly failure mode @@ -5313,6 +5315,25 @@ class Parser { $flags |= self::PTD_FOR_INCLUSION; } } + + # Check for empty input + if ( strval( $text ) === '' ) { + # Only sections 0 and T-0 exist in an empty document + if ( $sectionIndex == 0 ) { + if ( $mode === 'get' ) { + return ''; + } else { + return $newText; + } + } else { + if ( $mode === 'get' ) { + return $newText; + } else { + return $text; + } + } + } + # Preprocess the text $root = $this->preprocessToDom( $text, $flags ); @@ -5324,10 +5345,6 @@ class Parser { if ( $sectionIndex == 0 ) { # Section zero doesn't nest, level=big $targetLevel = 1000; - if ( !$node ) { - # The page definitely exists - we checked that earlier - so it must be blank: see bug #14005 - return $text; - } } else { while ( $node ) { if ( $node->getName() === 'h' ) { @@ -5410,7 +5427,8 @@ class Parser { /** * This function returns $oldtext after the content of the section - * specified by $section has been replaced with $text. + * specified by $section has been replaced with $text. If the target + * section does not exist, $oldtext is returned unchanged. * * @param $oldtext String: former text of the article * @param $section Numeric: section identifier -- 2.20.1