MaxSem has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/80578


Change subject: Make TOC hideable
......................................................................

Make TOC hideable

Currently, if an extension doesn't want a TOC, it has to remove it manually.
This change moves TOC addition to ParserOutput where it can be conveniently
disabled on demand without fragmenting the parser cache. The semantics of
ParserOutput's TOCHTML have slightly changed, however there are no extensions
using it.

Change-Id: I2889bcb9eb999c9049601e92440132118e1a8a41
---
M includes/OutputPage.php
M includes/parser/Parser.php
M includes/parser/ParserOutput.php
3 files changed, 46 insertions(+), 10 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/78/80578/1

diff --git a/includes/OutputPage.php b/includes/OutputPage.php
index 83a7d3f..7d755fc 100644
--- a/includes/OutputPage.php
+++ b/includes/OutputPage.php
@@ -256,6 +256,11 @@
        private $mTarget = null;
 
        /**
+        * @var bool: Whether output should contain TOC
+        */
+       private $mDisableTOC = false;
+
+       /**
         * Constructor for OutputPage. This should not be called directly.
         * Instead a new RequestContext should be created and it will 
implicitly create
         * a OutputPage tied to that context.
@@ -1606,6 +1611,7 @@
         */
        function addParserOutput( &$parserOutput ) {
                $this->addParserOutputNoText( $parserOutput );
+               $parserOutput->setTOCEnabled( !$this->mDisableTOC );
                $text = $parserOutput->getText();
                wfRunHooks( 'OutputPageBeforeHTML', array( &$this, &$text ) );
                $this->addHTML( $text );
@@ -3658,4 +3664,20 @@
                return array();
        }
 
+       /**
+        * Enables/disables TOC, doesn't override __NOTOC__
+        * @param bool $flag
+       * @since 1.22
+        */
+       public function disableTOC( $flag = true ) {
+               $this->mDisableTOC = $flag;
+       }
+
+       /**
+        * @return bool
+       * @since 1.22
+        */
+       public function isTOCDisabled() {
+               return $this->mDisableTOC;
+       }
 }
diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php
index a6823bf..5de5577 100644
--- a/includes/parser/Parser.php
+++ b/includes/parser/Parser.php
@@ -115,6 +115,9 @@
        # Marker Suffix needs to be accessible staticly.
        const MARKER_SUFFIX = "-QINU\x7f";
 
+       # Placeholder for TOC
+       const TOC_MARKER = '<!--MWTOC-->';
+
        # Persistent:
        var $mTagHooks = array();
        var $mTransparentTagHooks = array();
@@ -4083,7 +4086,7 @@
                        $this->mForceTocPosition = true;
 
                        # Set a placeholder. At the end we'll fill it in with 
the TOC.
-                       $text = $mw->replace( '<!--MWTOC-->', $text, 1 );
+                       $text = $mw->replace( self::TOC_MARKER, $text, 1 );
 
                        # Only keep the first one.
                        $text = $mw->replace( '', $text );
@@ -4490,7 +4493,6 @@
                                $toc .= Linker::tocUnindent( $prevtoclevel - 1 
);
                        }
                        $toc = Linker::tocList( $toc, 
$this->mOptions->getUserLangObj() );
-                       $this->mOutput->setTOCHTML( $toc );
                }
 
                if ( $isMain ) {
@@ -4534,11 +4536,11 @@
 
                $full .= join( '', $sections );
 
-               if ( $this->mForceTocPosition ) {
-                       return str_replace( '<!--MWTOC-->', $toc, $full );
-               } else {
-                       return $full;
+               if ( !$this->mForceTocPosition ) {
+                       $toc = '';
                }
+               $this->mOutput->setTOCHTML( $toc );
+               return $full;
        }
 
        /**
diff --git a/includes/parser/ParserOutput.php b/includes/parser/ParserOutput.php
index 5cb70cb..22cbb3c 100644
--- a/includes/parser/ParserOutput.php
+++ b/includes/parser/ParserOutput.php
@@ -47,7 +47,8 @@
                $mEditSectionTokens = false,  # prefix/suffix markers if edit 
sections were output as tokens
                $mProperties = array(),       # Name/value pairs to be cached 
in the DB
                $mTOCHTML = '',               # HTML of the TOC
-               $mTimestamp;                  # Timestamp of the revision
+               $mTimestamp,                  # Timestamp of the revision
+               $mTOCEnabled = true;          # Whether TOC should be shown, 
can't override __NOTOC__
                private $mIndexPolicy = '';       # 'index' or 'noindex'?  Any 
other value will result in no change.
                private $mAccessedOptions = array(); # List of ParserOptions 
(stored in the keys)
                private $mSecondaryDataUpdates = array(); # List of DataUpdate, 
used to save info from the page somewhere else.
@@ -68,11 +69,20 @@
        }
 
        function getText() {
+               wfProfileIn( __METHOD__ );
+               $text = $this->mText;
                if ( $this->mEditSectionTokens ) {
-                       return preg_replace_callback( 
ParserOutput::EDITSECTION_REGEX,
-                               array( &$this, 
'replaceEditSectionLinksCallback' ), $this->mText );
+                       $text = preg_replace_callback( 
ParserOutput::EDITSECTION_REGEX,
+                               array( &$this, 
'replaceEditSectionLinksCallback' ), $text );
+               } else {
+                       $text = preg_replace( ParserOutput::EDITSECTION_REGEX, 
'', $text );
                }
-               return preg_replace( ParserOutput::EDITSECTION_REGEX, '', 
$this->mText );
+               // If you have an old cached version of this class - sorry, you 
can't disable the TOC
+               if ( isset( $this->mTOCEnabled ) && $this->mTOCEnabled ) {
+                       $text = str_replace( Parser::TOC_MARKER, 
$this->mTOCHTML, $text );
+               }
+               wfProfileOut( __METHOD__ );
+               return $text;
        }
 
        /**
@@ -123,6 +133,7 @@
        function getTOCHTML()                { return $this->mTOCHTML; }
        function getTimestamp()              { return $this->mTimestamp; }
        function getLimitReportData()        { return $this->mLimitReportData; }
+       function getTOCEnabled()             { return $this->mTOCEnabled; }
 
        function setText( $text )            { return wfSetVar( $this->mText, 
$text ); }
        function setLanguageLinks( $ll )     { return wfSetVar( 
$this->mLanguageLinks, $ll ); }
@@ -134,6 +145,7 @@
        function setIndexPolicy( $policy )   { return wfSetVar( 
$this->mIndexPolicy, $policy ); }
        function setTOCHTML( $tochtml )      { return wfSetVar( 
$this->mTOCHTML, $tochtml ); }
        function setTimestamp( $timestamp )  { return wfSetVar( 
$this->mTimestamp, $timestamp ); }
+       function setTOCEnabled( $flag )      { return wfSetVar( 
$this->mTOCEnabled, $flag ); }
 
        function addCategory( $c, $sort )    { $this->mCategories[$c] = $sort; }
        function addLanguageLink( $t )       { $this->mLanguageLinks[] = $t; }

-- 
To view, visit https://gerrit.wikimedia.org/r/80578
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2889bcb9eb999c9049601e92440132118e1a8a41
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to