* 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
$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 );
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' ) {
/**
* 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