Try to output editsection markers instead of rendered editsection links and defer...
[lhc/web/wiklou.git] / includes / parser / Parser.php
index 95e0ff4..b723bea 100644 (file)
  *     produces altered wiki markup.
  * preprocess()
  *     removes HTML comments and expands templates
- * cleanSig()
+ * cleanSig() / cleanSigInSig()
  *     Cleans a signature before saving it to preferences
- * extractSections()
- *     Extracts sections from an article for section editing
+ * getSection()
+ *     Return the content of a section from an article for section editing
+ * replaceSection()
+ *     Replaces a section by number inside an article
  * getPreloadText()
  *     Removes <noinclude> sections, and <includeonly> tags.
  *
@@ -192,6 +194,7 @@ class Parser {
                        $this->firstCallInit();
                }
                $this->mOutput = new ParserOutput;
+               $this->mOptions->registerWatcher( array( $this->mOutput, 'recordOption' ) );
                $this->mAutonumber = 0;
                $this->mLastSection = '';
                $this->mDTopen = false;
@@ -268,12 +271,11 @@ class Parser {
                wfProfileIn( __METHOD__ );
                wfProfileIn( $fname );
 
+               $this->mOptions = $options;
                if ( $clearState ) {
                        $this->clearState();
                }
 
-               $options->resetUsage();
-               $this->mOptions = $options;
                $this->setTitle( $title ); # Page title has to be set for the pre-processor
 
                $oldRevisionId = $this->mRevisionId;
@@ -465,10 +467,9 @@ class Parser {
         */
        function preprocess( $text, $title, $options, $revid = null ) {
                wfProfileIn( __METHOD__ );
+               $this->mOptions = $options;
                $this->clearState();
                $this->setOutputType( self::OT_PREPROCESS );
-               $options->resetUsage();
-               $this->mOptions = $options;
                $this->setTitle( $title );
                if ( $revid !== null ) {
                        $this->mRevisionId = $revid;
@@ -489,10 +490,9 @@ class Parser {
         */
        public function getPreloadText( $text, $title, $options ) {
                # Parser (re)initialisation
+               $this->mOptions = $options;
                $this->clearState();
                $this->setOutputType( self::OT_PLAIN );
-               $options->resetUsage();
-               $this->mOptions = $options;
                $this->setTitle( $title );
 
                $flags = PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES;
@@ -3696,6 +3696,12 @@ class Parser {
                } else {
                        $showEditLink = $this->mOptions->getEditSection();
                }
+               if ( $showEditLink ) {
+                       $editLinkAsToken = $this->mOptions->getEditSectionTokens();
+                       if ( $editLinkAsToken ) {
+                               $this->mOutput->setEditSectionTokens( "{$this->mUniqPrefix}-editsection-", self::MARKER_SUFFIX );
+                       }
+               }
 
                # Get all headlines for numbering them and adding funky stuff like [edit]
                # links - this is for later, but we need the number of headlines right now
@@ -3949,12 +3955,28 @@ class Parser {
 
                        # give headline the correct <h#> tag
                        if ( $showEditLink && $sectionIndex !== false ) {
-                               if ( $isTemplate ) {
-                                       # Put a T flag in the section identifier, to indicate to extractSections()
-                                       # that sections inside <includeonly> should be counted.
-                                       $editlink = $sk->doEditSectionLink( Title::newFromText( $titleText ), "T-$sectionIndex", null, $this->mOptions->getUserLang() );
+                               if ( $editLinkAsToken ) {
+                                       // Output edit section links as markers with styles that can be customized by skins
+                                       if ( $isTemplate ) {
+                                               # Put a T flag in the section identifier, to indicate to extractSections()
+                                               # that sections inside <includeonly> should be counted.
+                                               $editlinkArgs = array( $titleText, "T-$sectionIndex", null );
+                                       } else {
+                                               $editlinkArgs = array( $this->mTitle->getPrefixedText(), $sectionIndex, $headlineHint );
+                                       }
+                                       // We use nearly the same structure as uniqPrefix and the marker stuffix (besides there being nothing random)
+                                       // However the this is output into the parser output itself not replaced early, so we hardcode this in case
+                                       // the constants change in a different version of MediaWiki, which would break this code.
+                                       $editlink = "{$this->mUniqPrefix}-editsection-" . serialize($editlinkArgs) . self::MARKER_SUFFIX;
                                } else {
-                                       $editlink = $sk->doEditSectionLink( $this->mTitle, $sectionIndex, $headlineHint, $this->mOptions->getUserLang() );
+                                       // Output edit section links directly as markup like we used to
+                                       if ( $isTemplate ) {
+                                               # Put a T flag in the section identifier, to indicate to extractSections()
+                                               # that sections inside <includeonly> should be counted.
+                                               $editlink = $sk->doEditSectionLink( Title::newFromText( $titleText ), "T-$sectionIndex", null, $this->mOptions->getUserLang() );
+                                       } else {
+                                               $editlink = $sk->doEditSectionLink( $this->mTitle, $sectionIndex, $headlineHint, $this->mOptions->getUserLang() );
+                                       }
                                }
                        } else {
                                $editlink = '';
@@ -4028,8 +4050,7 @@ class Parser {
         * @param $clearState Boolean: whether to clear the parser state first
         * @return String: the altered wiki markup
         */
-       public function preSaveTransform( $text, Title $title, $user, $options, $clearState = true ) {
-               $options->resetUsage();
+       public function preSaveTransform( $text, Title $title, User $user, ParserOptions $options, $clearState = true ) {
                $this->mOptions = $options;
                $this->setTitle( $title );
                $this->setUser( $user );
@@ -4211,9 +4232,9 @@ class Parser {
        function cleanSig( $text, $parsing = false ) {
                if ( !$parsing ) {
                        global $wgTitle;
+                       $this->mOptions = new ParserOptions;
                        $this->clearState();
                        $this->setTitle( $wgTitle );
-                       $this->mOptions = new ParserOptions;
                        $this->setOutputType = self::OT_PREPROCESS;
                }
 
@@ -4861,9 +4882,9 @@ class Parser {
         */
        private function extractSections( $text, $section, $mode, $newText='' ) {
                global $wgTitle;
+               $this->mOptions = new ParserOptions;
                $this->clearState();
                $this->setTitle( $wgTitle ); # not generally used but removes an ugly failure mode
-               $this->mOptions = new ParserOptions;
                $this->setOutputType( self::OT_PLAIN );
                $outText = '';
                $frame = $this->getPreprocessor()->newFrame();
@@ -4968,6 +4989,15 @@ class Parser {
                return $this->extractSections( $text, $section, "get", $deftext );
        }
 
+       /**
+        * This function returns $oldtext after the content of the section 
+        * specified by $section has been replaced with $text.
+        * 
+        * @param $text String: former text of the article
+        * @param $section Numeric: section identifier
+        * @param $text String: replacing text
+        * #return String: modified text
+        */
        public function replaceSection( $oldtext, $section, $text ) {
                return $this->extractSections( $oldtext, $section, "replace", $text );
        }
@@ -5143,13 +5173,13 @@ class Parser {
         * strip/replaceVariables/unstrip for preprocessor regression testing
         */
        function testSrvus( $text, $title, $options, $outputType = self::OT_HTML ) {
+               $this->mOptions = $options;
                $this->clearState();
                if ( !$title instanceof Title ) {
                        $title = Title::newFromText( $title );
                }
                $this->mTitle = $title;
                $options->resetUsage();
-               $this->mOptions = $options;
                $this->setOutputType( $outputType );
                $text = $this->replaceVariables( $text );
                $text = $this->mStripState->unstripBoth( $text );