Revert r34906, r34907, r34928 -- mixing high-level data into low-level storage functi...
[lhc/web/wiklou.git] / includes / Parser_OldPP.php
index 28564d0..840b1c6 100644 (file)
@@ -30,13 +30,20 @@ class Parser_OldPP
        const COLON_STATE_COMMENTDASH = 6;
        const COLON_STATE_COMMENTDASHDASH = 7;
 
+       // Allowed values for $this->mOutputType
+       // Parameter to startExternalParse().
+       const OT_HTML = 1;
+       const OT_WIKI = 2;
+       const OT_PREPROCESS = 3;
+       const OT_MSG = 4;
+
        /**#@+
         * @private
         */
        # Persistent:
        var $mTagHooks, $mTransparentTagHooks, $mFunctionHooks, $mFunctionSynonyms, $mVariables,
                $mImageParams, $mImageParamsMagicArray, $mExtLinkBracketedRegex;
-       
+
        # Cleared with clearState():
        var $mOutput, $mAutonumber, $mDTopen, $mStripState;
        var $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
@@ -73,7 +80,7 @@ class Parser_OldPP
                $this->mExtLinkBracketedRegex = '/\[(\b(' . wfUrlProtocols() . ')'.
                        '[^][<>"\\x00-\\x20\\x7F]+) *([^\]\\x0a\\x0d]*?)\]/S';
        }
-       
+
        /**
         * Do various kinds of initialisation on the first call of the parser
         */
@@ -81,12 +88,13 @@ class Parser_OldPP
                if ( !$this->mFirstCall ) {
                        return;
                }
-               
+               $this->mFirstCall = false;
+
                wfProfileIn( __METHOD__ );
                global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
-               
+
                $this->setHook( 'pre', array( $this, 'renderPreTag' ) );
-               
+
                # Syntax for arguments (see self::setFunctionHook):
                #  "name for lookup in localized magic words array",
                #  function callback,
@@ -128,7 +136,8 @@ class Parser_OldPP
                }
 
                $this->initialiseVariables();
-               $this->mFirstCall = false;
+
+               wfRunHooks( 'ParserFirstCallInit', array( &$this ) );
                wfProfileOut( __METHOD__ );
        }
 
@@ -193,10 +202,10 @@ class Parser_OldPP
                $this->mOutputType = $ot;
                // Shortcut alias
                $this->ot = array(
-                       'html' => $ot == OT_HTML,
-                       'wiki' => $ot == OT_WIKI,
-                       'msg' => $ot == OT_MSG,
-                       'pre' => $ot == OT_PREPROCESS,
+                       'html' => $ot == self::OT_HTML,
+                       'wiki' => $ot == self::OT_WIKI,
+                       'msg' => $ot == self::OT_MSG,
+                       'pre' => $ot == self::OT_PREPROCESS,
                );
        }
 
@@ -244,7 +253,7 @@ class Parser_OldPP
                        $this->mRevisionId = $revid;
                        $this->mRevisionTimestamp = null;
                }
-               $this->setOutputType( OT_HTML );
+               $this->setOutputType( self::OT_HTML );
                wfRunHooks( 'ParserBeforeStrip', array( &$this, &$text, &$this->mStripState ) );
                $text = $this->strip( $text, $this->mStripState );
                wfRunHooks( 'ParserAfterStrip', array( &$this, &$text, &$this->mStripState ) );
@@ -333,7 +342,7 @@ class Parser_OldPP
                # Information on include size limits, for the benefit of users who try to skirt them
                if ( $this->mOptions->getEnableLimitReport() ) {
                        $max = $this->mOptions->getMaxIncludeSize();
-                       $limitReport = 
+                       $limitReport =
                                "Pre-expand include size: {$this->mIncludeSizes['pre-expand']}/$max bytes\n" .
                                "Post-expand include size: {$this->mIncludeSizes['post-expand']}/$max bytes\n" .
                                "Template argument size: {$this->mIncludeSizes['arg']}/$max bytes\n";
@@ -370,7 +379,7 @@ class Parser_OldPP
        function preprocess( $text, $title, $options, $revid = null ) {
                wfProfileIn( __METHOD__ );
                $this->clearState();
-               $this->setOutputType( OT_PREPROCESS );
+               $this->setOutputType( self::OT_PREPROCESS );
                $this->mOptions = $options;
                $this->mTitle = $title;
                if( $revid !== null ) {
@@ -506,7 +515,7 @@ class Parser_OldPP
        function strip( $text, $state, $stripcomments = false , $dontstrip = array () ) {
                global $wgContLang;
                wfProfileIn( __METHOD__ );
-               $render = ($this->mOutputType == OT_HTML);
+               $render = ($this->mOutputType == self::OT_HTML);
 
                $uniq_prefix = $this->mUniqPrefix;
                $commentState = new ReplacementArray;
@@ -591,7 +600,7 @@ class Parser_OldPP
                        }
                }
                # Add the new items to the state
-               # We do this after the loop instead of during it to avoid slowing 
+               # We do this after the loop instead of during it to avoid slowing
                # down the recursive unstrip
                $state->nowiki->mergeArray( $nowikiItems );
                $state->general->mergeArray( $generalItems );
@@ -730,7 +739,7 @@ class Parser_OldPP
 
        /**
         * Use the HTML tidy PECL extension to use the tidy library in-process,
-        * saving the overhead of spawning a new process. 
+        * saving the overhead of spawning a new process.
         *
         * 'pear install tidy' should be able to compile the extension module.
         *
@@ -798,7 +807,7 @@ class Parser_OldPP
                        } else if ( count ( $td_history ) == 0 ) {
                                // Don't do any of the following
                                continue;
-                       } else if ( substr ( $line , 0 , 2 ) == '|}' ) { 
+                       } else if ( substr ( $line , 0 , 2 ) == '|}' ) {
                                // We are ending a table
                                $line = '</table>' . substr ( $line , 2 );
                                $last_tag = array_pop ( $last_tag_history );
@@ -1761,7 +1770,14 @@ class Parser_OldPP
 
                        # Special and Media are pseudo-namespaces; no pages actually exist in them
                        if( $ns == NS_MEDIA ) {
-                               $link = $sk->makeMediaLinkObj( $nt, $text );
+                               # Give extensions a chance to select the file revision for us
+                               $skip = $time = false;
+                               wfRunHooks( 'BeforeParserMakeImageLinkObj', array( &$this, &$nt, &$skip, &$time ) );
+                               if ( $skip ) {
+                                       $link = $sk->makeLinkObj( $nt );
+                               } else {
+                                       $link = $sk->makeMediaLinkObj( $nt, $text, $time );
+                               }
                                # Cloak with NOPARSE to avoid replacement in replaceExternalLinks
                                $s .= $prefix . $this->armorLinks( $link ) . $trail;
                                $this->mOutput->addImage( $nt->getDBkey() );
@@ -2137,7 +2153,7 @@ class Parser_OldPP
                                                $inBlockElem = true;
                                        }
                                } else if ( !$inBlockElem && !$this->mInPre ) {
-                                       if ( ' ' == $t{0} and ( $this->mLastSection == 'pre' or trim($t) != '' ) ) {
+                                       if ( '' != $t and ' ' == $t{0} and ( $this->mLastSection == 'pre' or trim($t) != '' ) ) {
                                                // pre
                                                if ($this->mLastSection != 'pre') {
                                                        $paragraphStack = false;
@@ -2388,7 +2404,7 @@ class Parser_OldPP
                        $oldtz = getenv( 'TZ' );
                        putenv( 'TZ='.$wgLocaltimezone );
                }
-               
+
                wfSuppressWarnings(); // E_STRICT system time bitching
                $localTimestamp = date( 'YmdHis', $ts );
                $localMonth = date( 'm', $ts );
@@ -2756,9 +2772,9 @@ class Parser_OldPP
         * taking care to avoid infinite loops.
         *
         * Note that the substitution depends on value of $mOutputType:
-        *  OT_WIKI: only {{subst:}} templates
-        *  OT_MSG: only magic variables
-        *  OT_HTML: all templates and magic variables
+        *  self::OT_WIKI: only {{subst:}} templates
+        *  self::OT_MSG: only magic variables
+        *  self::OT_HTML: all templates and magic variables
         *
         * @param string $tex The text to transform
         * @param array $args Key-value pairs representing template parameters to substitute
@@ -2781,7 +2797,7 @@ class Parser_OldPP
                if ( !$argsOnly ) {
                        $braceCallbacks[2] = array( &$this, 'braceSubstitution' );
                }
-               if ( $this->mOutputType != OT_MSG ) {
+               if ( $this->mOutputType != self::OT_MSG ) {
                        $braceCallbacks[3] = array( &$this, 'argSubstitution' );
                }
                if ( $braceCallbacks ) {
@@ -2817,7 +2833,7 @@ class Parser_OldPP
                $varname = $wgContLang->lc($matches[1]);
                wfProfileIn( $fname );
                $skip = false;
-               if ( $this->mOutputType == OT_WIKI ) {
+               if ( $this->mOutputType == self::OT_WIKI ) {
                        # Do only magic variables prefixed by SUBST
                        $mwSubst =& MagicWord::get( 'subst' );
                        if (!$mwSubst->matchStartAndRemove( $varname ))
@@ -2832,7 +2848,8 @@ class Parser_OldPP
                        $mw =& MagicWord::get( $id );
                        if ($mw->match($matches[1])) {
                                $text = $this->getVariableValue( $id );
-                               $this->mOutput->mContainsOldMagic = true;
+                               if (MagicWord::getCacheTTL($id)>-1)
+                                       $this->mOutput->mContainsOldMagic = true;
                        } else {
                                $text = $matches[0];
                        }
@@ -3034,7 +3051,7 @@ class Parser_OldPP
                                $titleText = $title->getPrefixedText();
                                //used by edit section links
                                $replaceHeadings = true;
-                               
+
                        }
                }
 
@@ -3141,7 +3158,7 @@ class Parser_OldPP
                                # If there are any <onlyinclude> tags, only include them
                                if ( in_string( '<onlyinclude>', $text ) && in_string( '</onlyinclude>', $text ) ) {
                                        $replacer = new OnlyIncludeReplacer;
-                                       StringUtils::delimiterReplaceCallback( '<onlyinclude>', '</onlyinclude>', 
+                                       StringUtils::delimiterReplaceCallback( '<onlyinclude>', '</onlyinclude>',
                                                array( &$replacer, 'replace' ), $text );
                                        $text = $replacer->output;
                                }
@@ -3275,13 +3292,13 @@ class Parser_OldPP
                $text = $skip = false;
                $finalTitle = $title;
                $deps = array();
-               
+
                // Loop to fetch the article, with up to 1 redirect
                for ( $i = 0; $i < 2 && is_object( $title ); $i++ ) {
                        # Give extensions a chance to select the revision instead
                        $id = false; // Assume current
                        wfRunHooks( 'BeforeParserFetchTemplateAndtitle', array( false, &$title, &$skip, &$id ) );
-                       
+
                        if( $skip ) {
                                $text = false;
                                $deps[] = array(
@@ -3293,9 +3310,9 @@ class Parser_OldPP
                        $rev = $id ? Revision::newFromId( $id ) : Revision::newFromTitle( $title );
                        $rev_id = $rev ? $rev->getId() : 0;
 
-                       $deps[] = array( 
-                               'title' => $title, 
-                               'page_id' => $title->getArticleID(), 
+                       $deps[] = array(
+                               'title' => $title,
+                               'page_id' => $title->getArticleID(),
                                'rev_id' => $rev_id );
 
                        if( $rev ) {
@@ -3377,7 +3394,7 @@ class Parser_OldPP
 
                if ( array_key_exists( $arg, $inputArgs ) ) {
                        $text = $inputArgs[$arg];
-               } else if (($this->mOutputType == OT_HTML || $this->mOutputType == OT_PREPROCESS ) &&
+               } else if (($this->mOutputType == self::OT_HTML || $this->mOutputType == self::OT_PREPROCESS ) &&
                null != $matches['parts'] && count($matches['parts']) > 0) {
                        $text = $matches['parts'][0];
                }
@@ -3678,7 +3695,7 @@ class Parser_OldPP
                if( $numVisible < 1 ) {
                        $enoughToc = false;
                }
-               
+
                if( $enoughToc ) {
                        if( $prevtoclevel > 0 && $prevtoclevel < $wgMaxTocLevel ) {
                                $toc .= $sk->tocUnindent( $prevtoclevel - 1 );
@@ -3733,7 +3750,7 @@ class Parser_OldPP
        function preSaveTransform( $text, &$title, $user, $options, $clearState = true ) {
                $this->mOptions = $options;
                $this->mTitle =& $title;
-               $this->setOutputType( OT_WIKI );
+               $this->setOutputType( self::OT_WIKI );
 
                if ( $clearState ) {
                        $this->clearState();
@@ -3828,11 +3845,11 @@ class Parser_OldPP
         */
        function getUserSig( &$user ) {
                global $wgMaxSigChars;
-               
+
                $username = $user->getName();
                $nickname = $user->getOption( 'nickname' );
                $nickname = $nickname === '' ? $username : $nickname;
-               
+
                if( mb_strlen( $nickname ) > $wgMaxSigChars ) {
                        $nickname = $username;
                        wfDebug( __METHOD__ . ": $username has overlong signature.\n" );
@@ -3883,7 +3900,7 @@ class Parser_OldPP
         */
        function cleanSig( $text, $parsing = false ) {
                global $wgTitle;
-               $this->startExternalParse( $wgTitle, new ParserOptions(), $parsing ? OT_WIKI : OT_MSG );
+               $this->startExternalParse( $this->mTitle, new ParserOptions(), $parsing ? self::OT_WIKI : self::OT_MSG );
 
                $substWord = MagicWord::get( 'subst' );
                $substRegex = '/\{\{(?!(?:' . $substWord->getBaseRegex() . '))/x' . $substWord->getRegexCase();
@@ -3949,7 +3966,7 @@ class Parser_OldPP
                        $this->mTitle = Title::newFromText('msg');
                }
                $this->mOptions = $options;
-               $this->setOutputType( OT_MSG );
+               $this->setOutputType( self::OT_MSG );
                $this->clearState();
                $text = $this->replaceVariables( $text );
 
@@ -4071,7 +4088,7 @@ class Parser_OldPP
                $pdbks = array();
                $colours = array();
                $sk = $this->mOptions->getSkin();
-               $linkCache =& LinkCache::singleton();
+               $linkCache = LinkCache::singleton();
 
                if ( !empty( $this->mLinkHolders['namespaces'] ) ) {
                        wfProfileIn( $fname.'-check' );
@@ -4110,10 +4127,7 @@ class Parser_OldPP
                                        # Not in the link cache, add it to the query
                                        if ( !isset( $current ) ) {
                                                $current = $ns;
-                                               $query =  "SELECT page_id, page_namespace, page_title";
-                                               if ( $threshold > 0 ) {
-                                                       $query .= ', page_len, page_is_redirect';
-                                               }
+                                               $query =  "SELECT page_id, page_namespace, page_title, page_len, page_is_redirect";
                                                $query .= " FROM $page WHERE (page_namespace=$ns AND page_title IN(";
                                        } elseif ( $current != $ns ) {
                                                $current = $ns;
@@ -4140,20 +4154,20 @@ class Parser_OldPP
                                while ( $s = $dbr->fetchObject($res) ) {
                                        $title = Title::makeTitle( $s->page_namespace, $s->page_title );
                                        $pdbk = $title->getPrefixedDBkey();
-                                       $linkCache->addGoodLinkObj( $s->page_id, $title );
+                                       $linkCache->addGoodLinkObj( $s->page_id, $title, $s->page_len, $s->page_is_redirect );
                                        $this->mOutput->addLink( $title, $s->page_id );
 
                                        $colours[$pdbk] = ( $threshold == 0 || (
                                                                $s->page_len >= $threshold || # always true if $threshold <= 0
                                                                $s->page_is_redirect ||
-                                                               !Namespace::isContent( $s->page_namespace ) )
+                                                               !MWNamespace::isContent( $s->page_namespace ) )
                                                            ? 1 : 2 );
                                }
                        }
                        wfProfileOut( $fname.'-check' );
 
                        # Do a second query for different language variants of links and categories
-                       if($wgContLang->hasVariants()){
+                       if( $wgContLang->hasVariants() ) {
                                $linkBatch = new LinkBatch();
                                $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
                                $categoryMap = array(); // maps $category_variant => $category (dbkeys)
@@ -4200,14 +4214,11 @@ class Parser_OldPP
                                }
 
 
-                               if(!$linkBatch->isEmpty()){
+                               if ( !$linkBatch->isEmpty() ){
                                        // construct query
                                        $titleClause = $linkBatch->constructSet('page', $dbr);
 
-                                       $variantQuery =  "SELECT page_id, page_namespace, page_title";
-                                       if ( $threshold > 0 ) {
-                                               $variantQuery .= ', page_len, page_is_redirect';
-                                       }
+                                       $variantQuery =  "SELECT page_id, page_namespace, page_title, page_len, page_is_redirect";
 
                                        $variantQuery .= " FROM $page WHERE $titleClause";
                                        if ( $options & RLH_FOR_UPDATE ) {
@@ -4226,7 +4237,7 @@ class Parser_OldPP
                                                $holderKeys = array();
                                                if(isset($variantMap[$varPdbk])){
                                                        $holderKeys = $variantMap[$varPdbk];
-                                                       $linkCache->addGoodLinkObj( $s->page_id, $variantTitle );
+                                                       $linkCache->addGoodLinkObj( $s->page_id, $variantTitle, $s->page_len, $s->page_is_redirect );
                                                        $this->mOutput->addLink( $variantTitle, $s->page_id );
                                                }
 
@@ -4428,7 +4439,7 @@ class Parser_OldPP
                if( isset( $params['heights'] ) ) {
                        $ig->setHeights( $params['heights'] );
                }
-               
+
                wfRunHooks( 'BeforeParserrenderImageGallery', array( &$this, &$ig ) );
 
                $lines = explode( "\n", $text );
@@ -4481,9 +4492,9 @@ class Parser_OldPP
                        // Initialise static lists
                        static $internalParamNames = array(
                                'horizAlign' => array( 'left', 'right', 'center', 'none' ),
-                               'vertAlign' => array( 'baseline', 'sub', 'super', 'top', 'text-top', 'middle', 
+                               'vertAlign' => array( 'baseline', 'sub', 'super', 'top', 'text-top', 'middle',
                                        'bottom', 'text-bottom' ),
-                               'frame' => array( 'thumbnail', 'manualthumb', 'framed', 'frameless', 
+                               'frame' => array( 'thumbnail', 'manualthumb', 'framed', 'frameless',
                                        'upright', 'border' ),
                        );
                        static $internalParamMap;
@@ -4538,7 +4549,7 @@ class Parser_OldPP
                #  * middle
                #  * bottom
                #  * text-bottom
-               
+
                $parts = array_map( 'trim', explode( '|', $options) );
                $sk = $this->mOptions->getSkin();
 
@@ -4558,14 +4569,14 @@ class Parser_OldPP
 
                # Process the input parameters
                $caption = '';
-               $params = array( 'frame' => array(), 'handler' => array(), 
+               $params = array( 'frame' => array(), 'handler' => array(),
                        'horizAlign' => array(), 'vertAlign' => array() );
                foreach( $parts as $part ) {
                        list( $magicName, $value ) = $mwArray->matchVariableStartToEnd( $part );
                        if ( isset( $paramMap[$magicName] ) ) {
                                list( $type, $paramName ) = $paramMap[$magicName];
                                $params[$type][$paramName] = $value;
-                               
+
                                // Special case; width and height come in one variable together
                                if( $type == 'handler' && $paramName == 'width' ) {
                                        $m = array();
@@ -4680,7 +4691,7 @@ class Parser_OldPP
                # I.... _hope_ this is right.
                # Otherwise, sometimes we don't have things initialized properly.
                $this->clearState();
-               
+
                # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
                # comments to be stripped as well)
                $stripState = new StripState;
@@ -4688,7 +4699,7 @@ class Parser_OldPP
                $oldOutputType = $this->mOutputType;
                $oldOptions = $this->mOptions;
                $this->mOptions = new ParserOptions();
-               $this->setOutputType( OT_WIKI );
+               $this->setOutputType( self::OT_WIKI );
 
                $striptext = $this->strip( $text, $stripState, true );
 
@@ -4863,8 +4874,8 @@ class Parser_OldPP
        }
 
        /**
-        * Try to guess the section anchor name based on a wikitext fragment 
-        * presumably extracted from a heading, for example "Header" from 
+        * Try to guess the section anchor name based on a wikitext fragment
+        * presumably extracted from a heading, for example "Header" from
         * "== Header ==".
         */
        public function guessSectionNameFromWikiText( $text ) {
@@ -4887,14 +4898,14 @@ class Parser_OldPP
 
        /**
         * Strips a text string of wikitext for use in a section anchor
-        * 
+        *
         * Accepts a text string and then removes all wikitext from the
         * string and leaves only the resultant text (i.e. the result of
         * [[User:WikiSysop|Sysop]] would be "Sysop" and the result of
         * [[User:WikiSysop]] would be "User:WikiSysop") - this is intended
         * to create valid section anchors by mimicing the output of the
         * parser when headings are parsed.
-        * 
+        *
         * @param $text string Text string to be stripped of wikitext
         * for use in a Section anchor
         * @return Filtered text string
@@ -4903,15 +4914,15 @@ class Parser_OldPP
                # Strip internal link markup
                $text = preg_replace('/\[\[:?([^[|]+)\|([^[]+)\]\]/','$2',$text);
                $text = preg_replace('/\[\[:?([^[]+)\|?\]\]/','$1',$text);
-               
+
                # Strip external link markup (FIXME: Not Tolerant to blank link text
                # I.E. [http://www.mediawiki.org] will render as [1] or something depending
                # on how many empty links there are on the page - need to figure that out.
                $text = preg_replace('/\[(?:' . wfUrlProtocols() . ')([^ ]+?) ([^[]+)\]/','$2',$text);
-               
+
                # Parse wikitext quotes (italics & bold)
                $text = $this->doQuotes($text);
-               
+
                # Strip HTML tags
                $text = StringUtils::delimiterReplace( '<', '>', '', $text );
                return $text;
@@ -4929,4 +4940,3 @@ class Parser_OldPP
                return $text;
        }
 }
-