restore feature which loads all subsections of a section during section
authorErik Moeller <erik@users.mediawiki.org>
Thu, 13 May 2004 17:25:34 +0000 (17:25 +0000)
committerErik Moeller <erik@users.mediawiki.org>
Thu, 13 May 2004 17:25:34 +0000 (17:25 +0000)
editing

includes/Article.php

index 0ac58b9..2c3123b 100644 (file)
@@ -102,28 +102,7 @@ class Article {
 
                                                # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
                                                # comments to be stripped as well)
-                                               $striparray=array();
-                                               $parser=new Parser();
-                                               $parser->mOutputType=OT_WIKI;
-                                               $striptext=$parser->strip($this->mContent, $striparray, true);
-
-                                               # now that we can be sure that no pseudo-sections are in the source,
-                                               # split it up by section
-                                               $secs =
-                                                 preg_split(
-                                                 "/(^=+.*?=+|^<h[1-6].*?" . ">.*?<\/h[1-6].*?" . ">)/mi",
-                                                 $striptext, -1,
-                                                 PREG_SPLIT_DELIM_CAPTURE);
-
-                                               if($section==0) {
-                                                       $rv=$secs[0];
-                                               } else {
-                                                       $rv=$secs[$section*2-1] . $secs[$section*2];
-                                               }
-
-                                               # reinsert stripped tags
-                                               $rv=$parser->unstrip($rv,$striparray);
-                                               $rv=trim($rv);
+                                               $rv=$this->getSection($this->mContent,$section);
                                                wfProfileOut( $fname );
                                                return $rv;
                                        }
@@ -133,6 +112,70 @@ class Article {
                        }
                }
        }
+       
+       # This function returns the text of a section, specified by a number ($section).
+       # A section is text under a heading like == Heading == or <h1>Heading</h1>, or 
+       # the first section before any such heading (section 0).
+       #
+       # If a section contains subsections, these are also returned.
+       #
+       function getSection($text,$section)  {
+               
+               # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
+               # comments to be stripped as well)
+               $striparray=array();
+               $parser=new Parser();
+               $parser->mOutputType=OT_WIKI;
+               $striptext=$parser->strip($text, $striparray, true);
+
+               # now that we can be sure that no pseudo-sections are in the source,
+               # split it up by section
+               $secs =
+                 preg_split(
+                 "/(^=+.*?=+|^<h[1-6].*?" . ">.*?<\/h[1-6].*?" . ">)/mi",
+                 $striptext, -1,
+                 PREG_SPLIT_DELIM_CAPTURE);
+               if($section==0) {
+                       $rv=$secs[0];
+               } else {
+                       $headline=$secs[$section*2-1];
+                       preg_match( "/^(=+).*?=+|^<h([1-6]).*?>.*?<\/h[1-6].*?>/mi",$headline,$matches);
+                       $hlevel=$matches[1];
+                       
+                       # translate wiki heading into level
+                       if(strpos($hlevel,"=")!==false) {
+                               $hlevel=strlen($hlevel);                        
+                       }
+                       
+                       $rv=$headline. $secs[$section*2];
+                       $count=$section+1;
+                       
+                       $break=false;
+                       while(!empty($secs[$count*2-1]) && !$break) {
+                       
+                               $subheadline=$secs[$count*2-1];
+                               preg_match( "/^(=+).*?=+|^<h([1-6]).*?>.*?<\/h[1-6].*?>/mi",$subheadline,$matches);
+                               $subhlevel=$matches[1];
+                               if(strpos($subhlevel,"=")!==false) {
+                                       $subhlevel=strlen($subhlevel);                                          
+                               }
+                               if($subhlevel > $hlevel) {
+                                       $rv.=$subheadline.$secs[$count*2];
+                               }
+                               if($subhlevel <= $hlevel) {
+                                       $break=true;
+                               }
+                               $count++;
+                                               
+                       }
+               }
+               # reinsert stripped tags
+               $rv=$parser->unstrip($rv,$striparray);
+               $rv=trim($rv);
+               return $rv;
+
+       }
+       
 
        # Load the revision (including cur_text) into this object
        function loadContent( $noredir = false )
@@ -604,7 +647,7 @@ class Article {
        /* Side effects: loads last edit */
        function getTextOfLastEditWithSectionReplacedOrAdded($section, $text, $summary = ""){
                $this->loadLastEdit();
-               $oldtext = $this->getContent( true );
+               $oldtext = $this->getContent( true );           
                if ($section != "") {
                        if($section=="new") {
                                if($summary) $subject="== {$summary} ==\n\n";
@@ -620,15 +663,58 @@ class Article {
 
                                # now that we can be sure that no pseudo-sections are in the source,
                                # split it up
+                               # Unfortunately we can't simply do a preg_replace because that might
+                               # replace the wrong section, so we have to use the section counter instead
                                $secs=preg_split("/(^=+.*?=+|^<h[1-6].*?" . ">.*?<\/h[1-6].*?" . ">)/mi",
                                  $oldtext,-1,PREG_SPLIT_DELIM_CAPTURE);
                                $secs[$section*2]=$text."\n\n"; // replace with edited
-                               if($section) { $secs[$section*2-1]=""; } // erase old headline
+                               
+                               # section 0 is top (intro) section
+                               if($section!=0) { 
+                                       
+                                       # headline of old section - we need to go through this section
+                                       # to determine if there are any subsections that now need to
+                                       # be erased, as the mother section has been replaced with
+                                       # the text of all subsections.
+                                       $headline=$secs[$section*2-1];
+                                       preg_match( "/^(=+).*?=+|^<h([1-6]).*?>.*?<\/h[1-6].*?>/mi",$headline,$matches);
+                                       $hlevel=$matches[1];
+                                       
+                                       # determine headline level for wikimarkup headings
+                                       if(strpos($hlevel,"=")!==false) {
+                                               $hlevel=strlen($hlevel);                        
+                                       }
+                                       
+                                       $secs[$section*2-1]=""; // erase old headline
+                                       $count=$section+1;
+                                       $break=false;
+                                       while(!empty($secs[$count*2-1]) && !$break) {
+                       
+                                               $subheadline=$secs[$count*2-1];
+                                               preg_match(
+                                                "/^(=+).*?=+|^<h([1-6]).*?>.*?<\/h[1-6].*?>/mi",$subheadline,$matches);
+                                               $subhlevel=$matches[1];
+                                               if(strpos($subhlevel,"=")!==false) {
+                                                       $subhlevel=strlen($subhlevel);          
+                                               }
+                                               if($subhlevel > $hlevel) {
+                                                       // erase old subsections
+                                                       $secs[$count*2-1]="";
+                                                       $secs[$count*2]="";
+                                               }
+                                               if($subhlevel <= $hlevel) {
+                                                       $break=true;
+                                               }
+                                               $count++;
+                                               
+                                       }
+                                       
+                               }
                                $text=join("",$secs);
-
                                # reinsert the stuff that we stripped out earlier
-                               $text=$parser->unstrip($text,$striparray,true);
+                               $text=$parser->unstrip($text,$striparray);      
                        }
+                                                               
                }
                return $text;
        }