fixes #22501 follow up r61101 remove superfluous, buggy code that was over-rideing...
[lhc/web/wiklou.git] / includes / parser / Parser.php
index a076c47..298aa3d 100644 (file)
@@ -15,7 +15,7 @@
  * (which in turn the browser understands, and can display).
  *
  * <pre>
- * There are five main entry points into the Parser class:
+ * There are six main entry points into the Parser class:
  * parse()
  *   produces HTML output
  * preSaveTransform().
@@ -26,6 +26,8 @@
  *   Cleans a signature before saving it to preferences
  * extractSections()
  *   Extracts sections from an article for section editing
+ * getTransclusionText()
+ *   Extracts the text of a template with only <includeonly>, etc., parsed
  *
  * Globals used:
  *    objects:   $wgLang, $wgContLang
@@ -82,6 +84,7 @@ class Parser
        const OT_WIKI = 2;
        const OT_PREPROCESS = 3;
        const OT_MSG = 3;
+       const OT_INCLUDES = 4;
 
        // Marker Suffix needs to be accessible staticly.
        const MARKER_SUFFIX = "-QINU\x7f";
@@ -91,9 +94,9 @@ class Parser
         */
        # Persistent:
        var $mTagHooks, $mTransparentTagHooks, $mFunctionHooks, $mFunctionSynonyms, $mVariables,
-               $mImageParams, $mImageParamsMagicArray, $mStripList, $mMarkerIndex, $mPreprocessor,
-               $mExtLinkBracketedRegex, $mUrlProtocols, $mDefaultStripList, $mVarCache, $mConf,
-               $mFunctionTagHooks;
+               $mSubstWords, $mImageParams, $mImageParamsMagicArray, $mStripList, $mMarkerIndex,
+               $mPreprocessor, $mExtLinkBracketedRegex, $mUrlProtocols, $mDefaultStripList,
+               $mVarCache, $mConf, $mFunctionTagHooks;
 
 
        # Cleared with clearState():
@@ -129,7 +132,7 @@ class Parser
                $this->mFunctionHooks = array();
                $this->mFunctionTagHooks = array();
                $this->mFunctionSynonyms = array( 0 => array(), 1 => array() );
-               $this->mDefaultStripList = $this->mStripList = array( 'nowiki', 'gallery' );
+               $this->mDefaultStripList = $this->mStripList = array();
                $this->mUrlProtocols = wfUrlProtocols();
                $this->mExtLinkBracketedRegex = '/\[(\b(' . wfUrlProtocols() . ')'.
                        '[^][<>"\\x00-\\x20\\x7F]+) *([^\]\\x0a\\x0d]*?)\]/S';
@@ -172,8 +175,8 @@ class Parser
 
                wfProfileIn( __METHOD__ );
 
-               $this->setHook( 'pre', array( $this, 'renderPreTag' ) );
                CoreParserFunctions::register( $this );
+               CoreTagHooks::register( $this );
                $this->initialiseVariables();
 
                wfRunHooks( 'ParserFirstCallInit', array( &$this ) );
@@ -256,9 +259,10 @@ class Parser
         * Set the context title
         */
        function setTitle( $t ) {
-               if ( !$t || $t instanceof FakeTitle ) {
-                       $t = Title::newFromText( 'NO TITLE' );
-               }
+               if ( !$t || $t instanceof FakeTitle ) {
+                       $t = Title::newFromText( 'NO TITLE' );
+               }
+
                if ( strval( $t->getFragment() ) !== '' ) {
                        # Strip the fragment to avoid various odd effects
                        $this->mTitle = clone $t;
@@ -275,7 +279,7 @@ class Parser
         */
        function uniqPrefix() {
                if( !isset( $this->mUniqPrefix ) ) {
-                       // @fixme this is probably *horribly wrong*
+                       // @todo Fixme: this is probably *horribly wrong*
                        // LanguageConverter seems to want $wgParser's uniqPrefix, however
                        // if this is called for a parser cache hit, the parser may not
                        // have ever been initialized in the first place.
@@ -304,7 +308,7 @@ class Parser
                 * to internalParse() which does all the real work.
                 */
 
-               global $wgUseTidy, $wgAlwaysUseTidy, $wgContLang;
+               global $wgUseTidy, $wgAlwaysUseTidy, $wgContLang, $wgDisableLangConversion;
                $fname = __METHOD__.'-' . wfGetCaller();
                wfProfileIn( __METHOD__ );
                wfProfileIn( $fname );
@@ -314,7 +318,8 @@ class Parser
                }
 
                $this->mOptions = $options;
-               $this->setTitle( $title );
+               $this->setTitle( $title ); // Page title has to be set for the pre-processor
+
                $oldRevisionId = $this->mRevisionId;
                $oldRevisionTimestamp = $this->mRevisionTimestamp;
                if( $revid !== null ) {
@@ -326,6 +331,7 @@ class Parser
                # No more strip!
                wfRunHooks( 'ParserAfterStrip', array( &$this, &$text, &$this->mStripState ) );
                $text = $this->internalParse( $text );
+
                $text = $this->mStripState->unstripGeneral( $text );
 
                # Clean up special characters, only run once, next-to-last before doBlockLevels
@@ -343,34 +349,51 @@ class Parser
 
                $this->replaceLinkHolders( $text );
 
-               # the position of the parserConvert() call should not be changed. it
-               # assumes that the links are all replaced and the only thing left
-               # is the <nowiki> mark.
-               # Side-effects: this calls $this->mOutput->setTitleText()
-               $text = $wgContLang->parserConvert( $text, $this );
+               // The position of the convert() call should not be changed. it
+               // assumes that the links are all replaced and the only thing left
+               // is the <nowiki> mark.
+               if ( !( $wgDisableLangConversion
+                               || isset( $this->mDoubleUnderscores['nocontentconvert'] )
+                               || $this->mTitle->isTalkPage()
+                               || $this->mTitle->isConversionTable() ) ) {
+                       $text = $wgContLang->convert( $text );
+               }
+
+               // A title may have been set in a conversion rule.
+               // Note that if a user tries to set a title in a conversion
+               // rule but content conversion was not done, then the parser
+               // won't pick it up.  This is probably expected behavior.
+               if ( $wgContLang->getConvRuleTitle() ) {
+                       $this->mOutput->setTitleText( $wgContLang->getConvRuleTitle() );
+               }
 
                $text = $this->mStripState->unstripNoWiki( $text );
 
                wfRunHooks( 'ParserBeforeTidy', array( &$this, &$text ) );
 
-//!JF Move to its own function
-
-               $uniq_prefix = $this->mUniqPrefix;
-               $matches = array();
-               $elements = array_keys( $this->mTransparentTagHooks );
-               $text = self::extractTagsAndParams( $elements, $text, $matches, $uniq_prefix );
-
-               foreach( $matches as $marker => $data ) {
-                       list( $element, $content, $params, $tag ) = $data;
-                       $tagName = strtolower( $element );
-                       if( isset( $this->mTransparentTagHooks[$tagName] ) ) {
-                               $output = call_user_func_array( $this->mTransparentTagHooks[$tagName],
-                                       array( $content, $params, $this ) );
-                       } else {
-                               $output = $tag;
+               if ( $this->mTransparentTagHooks ) {
+                       //!JF Move to its own function
+                       $uniq_prefix = $this->mUniqPrefix;
+                       $matches = array();
+                       $elements = array_keys( $this->mTransparentTagHooks );
+                       $text = self::extractTagsAndParams( $elements, $text, $matches, $uniq_prefix );
+
+                       foreach( $matches as $marker => $data ) {
+                               list( $element, $content, $params, $tag ) = $data;
+                               $tagName = strtolower( $element );
+                               if( isset( $this->mTransparentTagHooks[$tagName] ) ) {
+                                       $output = call_user_func_array( $this->mTransparentTagHooks[$tagName],
+                                               array( $content, $params, $this ) );
+                               } else {
+                                       $output = $tag;
+                               }
+                               $this->mStripState->general->setPair( $marker, $output );
                        }
-                       $this->mStripState->general->setPair( $marker, $output );
                }
+
+               # This was originally inserted for transparent tag hooks (now deprecated)
+               # but some extensions (notably <poem>) rely on the extra unstripGeneral()
+               # after unstripNoWiki() so they can modify the contents of <nowiki> tags.
                $text = $this->mStripState->unstripGeneral( $text );
 
                $text = Sanitizer::normalizeCharReferences( $text );
@@ -426,6 +449,7 @@ class Parser
                        $text .= "\n<!-- \n$limitReport-->\n";
                }
                $this->mOutput->setText( $text );
+
                $this->mRevisionId = $oldRevisionId;
                $this->mRevisionTimestamp = $oldRevisionTimestamp;
                wfProfileOut( $fname );
@@ -473,6 +497,26 @@ class Parser
                return $text;
        }
 
+       /**
+        * Get the wikitext of a page as though it was transcluded.
+        *
+        * Specifically <includeonly> etc. are parsed, redirects are followed, comments
+        * are removed, but templates arguments and parser functions are untouched.
+        *
+        * This is not called by the parser itself, see braceSubstitution for its transclusion. 
+        */
+       public function getTransclusionText( $title, $options ) {
+               // Must initialize first
+               $this->clearState();
+               $this->setOutputType( self::OT_INCLUDES );
+               $this->mOptions = $options;
+               $this->setTitle( new FakeTitle ); 
+
+               list( $text, $title ) = $this->getTemplateDom( $title );
+               $flags = PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES;
+               return $this->getPreprocessor()->newFrame()->expand( $text, $flags );
+       }
+
        /**
         * Get a random string
         *
@@ -537,7 +581,7 @@ class Parser
                $taglist = implode( '|', $elements );
                $start = "/<($taglist)(\\s+[^>]*?|\\s*?)(\/?" . ">)|<(!--)/i";
 
-               while ( '' != $text ) {
+               while ( $text != '' ) {
                        $p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE );
                        $stripped .= $p[0];
                        if( count( $p ) < 5 ) {
@@ -595,15 +639,7 @@ class Parser
         * Get a list of strippable XML-like elements
         */
        function getStripList() {
-               global $wgRawHtml;
-               $elements = $this->mStripList;
-               if( $wgRawHtml ) {
-                       $elements[] = 'html';
-               }
-               if( $this->mOptions->getUseTeX() ) {
-                       $elements[] = 'math';
-               }
-               return $elements;
+               return $this->mStripList;
        }
 
        /**
@@ -654,14 +690,14 @@ class Parser
                $this->mStripState->general->setPair( $rnd, $text );
                return $rnd;
        }
-       
+
        /**
         * Interface with html tidy
         * @deprecated Use MWTidy::tidy()
         */
        public static function tidy( $text ) {
                wfDeprecated( __METHOD__ );
-               return MWTidy::tidy( $text );   
+               return MWTidy::tidy( $text );
        }
 
        /**
@@ -699,11 +735,11 @@ class Parser
                                $attributes = Sanitizer::fixTagAttributes ( $attributes , 'table' );
 
                                $outLine = str_repeat( '<dl><dd>' , $indent_level ) . "<table{$attributes}>";
-                               array_push ( $td_history , false );
-                               array_push ( $last_tag_history , '' );
-                               array_push ( $tr_history , false );
-                               array_push ( $tr_attributes , '' );
-                               array_push ( $has_opened_tr , false );
+                               array_push( $td_history , false );
+                               array_push( $last_tag_history , '' );
+                               array_push( $tr_history , false );
+                               array_push( $tr_attributes , '' );
+                               array_push( $has_opened_tr , false );
                        } else if ( count ( $td_history ) == 0 ) {
                                // Don't do any of the following
                                $out .= $outLine."\n";
@@ -732,9 +768,9 @@ class Parser
 
                                // Whats after the tag is now only attributes
                                $attributes = $this->mStripState->unstripBoth( $line );
-                               $attributes = Sanitizer::fixTagAttributes ( $attributes , 'tr' );
-                               array_pop ( $tr_attributes );
-                               array_push ( $tr_attributes , $attributes );
+                               $attributes = Sanitizer::fixTagAttributes( $attributes, 'tr' );
+                               array_pop( $tr_attributes );
+                               array_push( $tr_attributes, $attributes );
 
                                $line = '';
                                $last_tag = array_pop ( $last_tag_history );
@@ -870,7 +906,7 @@ class Parser
         */
        function internalParse( $text, $isMain = true, $frame=false ) {
                wfProfileIn( __METHOD__ );
-               
+
                $origText = $text;
 
                # Hook to suspend the parser in this state
@@ -883,7 +919,7 @@ class Parser
                if ($frame) {
                        // use frame depth to infer how include/noinclude tags should be handled
                        // depth=0 means this is the top-level document; otherwise it's an included document
-                       if( !$frame->depth ) 
+                       if( !$frame->depth )
                                $flag = 0;
                        else
                                $flag = Parser::PTD_FOR_INCLUSION;
@@ -907,6 +943,7 @@ class Parser
                $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
 
                $text = $this->doDoubleUnderscore( $text );
+
                $text = $this->doHeadings( $text );
                if( $this->mOptions->getUseDynamicDates() ) {
                        $df = DateFormatter::getInstance();
@@ -930,7 +967,7 @@ class Parser
        /**
         * Replace special strings like "ISBN xxx" and "RFC xxx" with
         * magic external links.
-        * 
+        *
         * DML
         * @private
         */
@@ -940,7 +977,7 @@ class Parser
                $urlChar = self::EXT_LINK_URL_CLASS;
                $text = preg_replace_callback(
                        '!(?:                           # Start cases
-                               (<a.*?</a>) |               # m[1]: Skip link text 
+                               (<a.*?</a>) |               # m[1]: Skip link text
                                (<.*?>) |                   # m[2]: Skip stuff inside HTML elements' . "
                                (\\b(?:$prots)$urlChar+) |  # m[3]: Free external links" . '
                                (?:RFC|PMID)\s+([0-9]+) |   # m[4]: RFC or PMID, capture number
@@ -1042,7 +1079,7 @@ class Parser
                $text = $this->maybeMakeExternalImage( $url );
                if ( $text === false ) {
                        # Not an image, make a link
-                       $text = $sk->makeExternalLink( $url, $wgContLang->markNoConversion($url), true, 'free', 
+                       $text = $sk->makeExternalLink( $url, $wgContLang->markNoConversion($url), true, 'free',
                                $this->getExternalLinkAttribs( $url ) );
                        # Register it in the output object...
                        # Replace unnecessary URL escape codes with their equivalent characters
@@ -1482,10 +1519,10 @@ class Parser
                wfProfileIn( __METHOD__.'-setup' );
                static $tc = FALSE, $e1, $e1_img;
                # the % is needed to support urlencoded titles as well
-               if ( !$tc ) { 
+               if ( !$tc ) {
                        $tc = Title::legalChars() . '#%';
                        # Match a link having the form [[namespace:link|alternate]]trail
-                       $e1 = "/^([{$tc}]+)(?:\\|(.+?))?]](.*)\$/sD";
+                       $e1 = "/^([{$tc}]*)(\\|.*?)?]](.*)\$/sD";
                        # Match cases where there is no "]]", which might still be images
                        $e1_img = "/^([{$tc}]+)\\|(.*)\$/sD";
                }
@@ -1565,7 +1602,15 @@ class Parser
 
                        wfProfileIn( __METHOD__."-e1" );
                        if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
-                               $text = $m[2];
+
+                               if( $m[2] === '' ) {
+                                       $text = '';
+                               } elseif( $m[2] === '|' ) { 
+                                       $text = $this->getPipeTrickText( $m[1] );
+                               } else {
+                                       $text = substr( $m[2], 1 );
+                               }
+
                                # If we get a ] at the beginning of $m[3] that means we have a link that's something like:
                                # [[Image:Foo.jpg|[http://example.com desc]]] <- having three ] in a row fucks up,
                                # the real problem is with the $e1 regex
@@ -1582,18 +1627,20 @@ class Parser
                                        $text .= ']'; # so that replaceExternalLinks($text) works later
                                        $m[3] = substr( $m[3], 1 );
                                }
+
+                               # Handle pipe-trick for [[|<blah>]]
+                               $lnk = $m[1] === '' ? $this->getPipeTrickLink( $text ) : $m[1];
                                # fix up urlencoded title texts
-                               if( strpos( $m[1], '%' ) !== false ) {
+                               if( strpos( $lnk, '%' ) !== false ) {
                                        # Should anchors '#' also be rejected?
-                                       $m[1] = str_replace( array('<', '>'), array('&lt;', '&gt;'), urldecode($m[1]) );
+                                       $lnk = str_replace( array('<', '>'), array('&lt;', '&gt;'), urldecode($lnk) );
                                }
+
                                $trail = $m[3];
                        } elseif( preg_match($e1_img, $line, $m) ) { # Invalid, but might be an image with a link in its caption
                                $might_be_img = true;
                                $text = $m[2];
-                               if ( strpos( $m[1], '%' ) !== false ) {
-                                       $m[1] = urldecode($m[1]);
-                               }
+                               $lnk = strpos( $m[1], '%' ) === false ? $m[1] : urldecode( $m[1] );
                                $trail = "";
                        } else { # Invalid form; output directly
                                $s .= $prefix . '[[' . $line ;
@@ -1606,7 +1653,7 @@ class Parser
                        # Don't allow internal links to pages containing
                        # PROTO: where PROTO is a valid URL protocol; these
                        # should be external links.
-                       if ( preg_match( '/^\b(?:' . wfUrlProtocols() . ')/', $m[1] ) ) {
+                       if ( preg_match( '/^\b(?:' . wfUrlProtocols() . ')/', $lnk ) ) {
                                $s .= $prefix . '[[' . $line ;
                                wfProfileOut( __METHOD__."-misc" );
                                continue;
@@ -1614,12 +1661,12 @@ class Parser
 
                        # Make subpage if necessary
                        if ( $useSubpages ) {
-                               $link = $this->maybeDoSubpageLink( $m[1], $text );
+                               $link = $this->maybeDoSubpageLink( $lnk, $text );
                        } else {
-                               $link = $m[1];
+                               $link = $lnk;
                        }
 
-                       $noforce = (substr( $m[1], 0, 1 ) !== ':');
+                       $noforce = (substr( $lnk, 0, 1 ) !== ':');
                        if (!$noforce) {
                                # Strip off leading ':'
                                $link = substr( $link, 1 );
@@ -1628,7 +1675,7 @@ class Parser
                        wfProfileOut( __METHOD__."-misc" );
                        wfProfileIn( __METHOD__."-title" );
                        $nt = Title::newFromText( $this->mStripState->unstripNoWiki( $link ) );
-                       if ( $nt === NULL ) {
+                       if ( $nt === null ) {
                                $s .= $prefix . '[[' . $line;
                                wfProfileOut( __METHOD__."-title" );
                                continue;
@@ -1683,7 +1730,7 @@ class Parser
                                wfProfileOut( __METHOD__."-might_be_img" );
                        }
 
-                       $wasblank = ( '' == $text );
+                       $wasblank = ( $text  == '' );
                        if ( $wasblank ) $text = $link;
 
                        # Link not escaped by : , create the various objects
@@ -1718,6 +1765,8 @@ class Parser
                                                }
                                                # cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
                                                $s .= $prefix . $this->armorLinks( $this->makeImage( $nt, $text, $holders ) ) . $trail;
+                                       } else {
+                                               $s .= $prefix . $trail;
                                        }
                                        $this->mOutput->addImage( $nt->getDBkey() );
                                        wfProfileOut( __METHOD__."-image" );
@@ -1865,13 +1914,32 @@ class Parser
                return Linker::normalizeSubpageLink( $this->mTitle, $target, $text );
        }
 
+       /**
+        * From the [[title|]] return link-text as though the used typed [[title|link-text]]
+        * @param string $link from [[$link|]]
+        * @return string $text for [[$link|$text]]
+        */
+       function getPipeTrickText( $link ) {
+               return Linker::getPipeTrickText( $link );
+       }
+
+       /**
+        * From the [[|link-text]] return the title as though the user typed [[title|link-text]]
+        * @param string $text from [[|$text]]
+        * @param Title $title to resolve the link against
+        * @return string $link for [[$link|$text]]
+        */
+       function getPipeTrickLink( $text ) {
+               return Linker::getPipeTrickLink( $text, $this->mTitle );
+       }
+
        /**#@+
         * Used by doBlockLevels()
         * @private
         */
        /* private */ function closeParagraph() {
                $result = '';
-               if ( '' != $this->mLastSection ) {
+               if ( $this->mLastSection != '' ) {
                        $result = '</' . $this->mLastSection  . ">\n";
                }
                $this->mInPre = false;
@@ -2016,7 +2084,7 @@ class Parser
                                }
                        } elseif( $prefixLength || $lastPrefixLength ) {
                                // We need to open or close prefixes, or both.
-                               
+
                                # Either open or close a level...
                                $commonPrefixLength = $this->getCommon( $prefix, $lastPrefix );
                                $paragraphStack = false;
@@ -2026,12 +2094,12 @@ class Parser
                                        $output .= $this->closeList( $lastPrefix[$lastPrefixLength-1] );
                                        --$lastPrefixLength;
                                }
-                               
+
                                // Continue the current prefix if appropriate.
                                if ( $prefixLength <= $commonPrefixLength && $commonPrefixLength > 0 ) {
                                        $output .= $this->nextItem( $prefix[$commonPrefixLength-1] );
                                }
-                               
+
                                // Open prefixes where appropriate.
                                while ( $prefixLength > $commonPrefixLength ) {
                                        $char = substr( $prefix, $commonPrefixLength, 1 );
@@ -2048,7 +2116,7 @@ class Parser
                                }
                                $lastPrefix = $prefix2;
                        }
-                       
+
                        // If we have no prefixes, go to paragraph mode.
                        if( 0 == $prefixLength ) {
                                wfProfileIn( __METHOD__."-paragraph" );
@@ -2081,7 +2149,7 @@ class Parser
                                                $t = substr( $t, 1 );
                                        } else {
                                                // paragraph
-                                               if ( '' == trim($t) ) {
+                                               if ( trim($t) == '' ) {
                                                        if ( $paragraphStack ) {
                                                                $output .= $paragraphStack.'<br />';
                                                                $paragraphStack = false;
@@ -2121,7 +2189,7 @@ class Parser
                        $output .= $this->closeList( $prefix2[$prefixLength-1] );
                        --$prefixLength;
                }
-               if ( '' != $this->mLastSection ) {
+               if ( $this->mLastSection != '' ) {
                        $output .= '</' . $this->mLastSection . '>';
                        $this->mLastSection = '';
                }
@@ -2318,11 +2386,10 @@ class Parser
                # Use the time zone
                global $wgLocaltimezone;
                if ( isset( $wgLocaltimezone ) ) {
-                       $oldtz = getenv( 'TZ' );
-                       putenv( 'TZ='.$wgLocaltimezone );
+                       $oldtz = date_default_timezone_get();
+                       date_default_timezone_set( $wgLocaltimezone );
                }
 
-               wfSuppressWarnings(); // E_STRICT system time bitching
                $localTimestamp = date( 'YmdHis', $ts );
                $localMonth = date( 'm', $ts );
                $localMonth1 = date( 'n', $ts );
@@ -2334,9 +2401,8 @@ class Parser
                $localYear = date( 'Y', $ts );
                $localHour = date( 'H', $ts );
                if ( isset( $wgLocaltimezone ) ) {
-                       putenv( 'TZ='.$oldtz );
+                       date_default_timezone_set( $oldtz );
                }
-               wfRestoreWarnings();
 
                switch ( $index ) {
                        case 'currentmonth':
@@ -2429,6 +2495,14 @@ class Parser
                                $subjPage = $this->mTitle->getSubjectPage();
                                $value = $subjPage->getPrefixedUrl();
                                break;
+                       case 'pipetrick':
+                               $text = $this->mTitle->getText();
+                               $value = $this->getPipeTrickText( $text );
+                               break;
+                       case 'pipetricke':
+                               $text = $this->mTitle->getText();
+                               $value = wfUrlEncode( str_replace( ' ', '_', $this->getPipeTrickText( $text ) ) );
+                               break;
                        case 'revisionid':
                                // Let the edit saving system know we should parse the page
                                // *after* a revision ID has been assigned.
@@ -2577,6 +2651,8 @@ class Parser
                                return $wgServerName;
                        case 'scriptpath':
                                return $wgScriptPath;
+                       case 'stylepath':
+                               return $wgStylePath;
                        case 'directionmark':
                                return $wgContLang->getDirMark();
                        case 'contentlanguage':
@@ -2590,22 +2666,24 @@ class Parser
                                        return null;
                }
 
-               if ( $index ) 
+               if ( $index )
                        $this->mVarCache[$index] = $value;
 
                return $value;
        }
 
        /**
-        * initialise the magic variables (like CURRENTMONTHNAME)
+        * initialise the magic variables (like CURRENTMONTHNAME) and substitution modifiers 
         *
         * @private
         */
        function initialiseVariables() {
                wfProfileIn( __METHOD__ );
                $variableIDs = MagicWord::getVariableIDs();
+               $substIDs = MagicWord::getSubstIDs();
 
                $this->mVariables = new MagicWordArray( $variableIDs );
+               $this->mSubstWords = new MagicWordArray( $substIDs );
                wfProfileOut( __METHOD__ );
        }
 
@@ -2663,7 +2741,7 @@ class Parser
         *  self::OT_HTML: all templates and extension tags
         *
         * @param string $tex The text to transform
-        * @param PPFrame $frame Object describing the arguments passed to the template. 
+        * @param PPFrame $frame Object describing the arguments passed to the template.
         *        Arguments may also be provided as an associative array, as was the usual case before MW1.12.
         *        Providing arguments this way may be useful for extensions wishing to perform variable replacement explicitly.
         * @param bool $argsOnly Only do argument (triple-brace) expansion, not double-brace expansion
@@ -2726,14 +2804,10 @@ class Parser
         *       exceeded, provide the values (optional)
         */
        function limitationWarn( $limitationType, $current=null, $max=null) {
-               $msgName = $limitationType . '-warning';
                //does no harm if $current and $max are present but are unnecessary for the message
-               $warning = wfMsgExt( $msgName, array( 'parsemag', 'escape' ), $current, $max ); 
+               $warning = wfMsgExt( "$limitationType-warning", array( 'parsemag', 'escape' ), $current, $max );
                $this->mOutput->addWarning( $warning );
-               $cat = Title::makeTitleSafe( NS_CATEGORY, wfMsgForContent( $limitationType . '-category' ) );
-               if ( $cat ) {
-                       $this->mOutput->addCategory( $cat->getDBkey(), $this->getDefaultSort() );
-               }
+               $this->addTrackingCategory( "$limitationType-category" );
        }
 
        /**
@@ -2762,7 +2836,7 @@ class Parser
                $isLocalObj = false;        # $text is a DOM node needing expansion in the current frame
 
                # Title object, where $text came from
-               $title = NULL;
+               $title = null;
 
                # $part1 is the bit before the first |, and must contain only title characters.
                # Various prefixes will be stripped from it later.
@@ -2780,12 +2854,26 @@ class Parser
                # SUBST
                wfProfileIn( __METHOD__.'-modifiers' );
                if ( !$found ) {
-                       $mwSubst = MagicWord::get( 'subst' );
-                       if ( $mwSubst->matchStartAndRemove( $part1 ) xor $this->ot['wiki'] ) {
-                               # One of two possibilities is true:
-                               # 1) Found SUBST but not in the PST phase
-                               # 2) Didn't find SUBST and in the PST phase
-                               # In either case, return without further processing
+
+                       $substMatch = $this->mSubstWords->matchStartAndRemove( $part1 );
+
+                       # Possibilities for substMatch: "subst", "safesubst" or FALSE
+                       # Decide whether to expand template or keep wikitext as-is.
+                       if ( $this->ot['wiki'] )
+                       {
+                               if ( $substMatch === false ) {
+                                       $literal = true;  # literal when in PST with no prefix
+                               } else {
+                                       $literal = false; # expand when in PST with subst: or safesubst:
+                               }
+                       } else {
+                               if ( $substMatch == 'subst' ) {
+                                       $literal = true;  # literal when not in PST with plain subst:
+                               } else {
+                                       $literal = false; # expand when not in PST with safesubst: or no prefix
+                               }
+                       }
+                       if ( $literal ) {
                                $text = $frame->virtualBracketedImplode( '{{', '|', '}}', $titleWithSpaces, $args );
                                $isLocalObj = true;
                                $found = true;
@@ -2872,7 +2960,7 @@ class Parser
                                        $found = true;
                                        $noparse = true;
                                        $preprocessFlags = 0;
-                                       
+
                                        if ( is_array( $result ) ) {
                                                if ( isset( $result[0] ) ) {
                                                        $text = $result[0];
@@ -3176,14 +3264,11 @@ class Parser
        function fetchScaryTemplateMaybeFromCache($url) {
                global $wgTranscludeCacheExpiry;
                $dbr = wfGetDB(DB_SLAVE);
+               $tsCond = $dbr->timestamp( time() - $wgTranscludeCacheExpiry );
                $obj = $dbr->selectRow('transcache', array('tc_time', 'tc_contents'),
-                               array('tc_url' => $url));
+                               array('tc_url' => $url, "tc_time >= " . $dbr->addQuotes( $tsCond ) ) );
                if ($obj) {
-                       $time = $obj->tc_time;
-                       $text = $obj->tc_contents;
-                       if ($time && time() < $time + $wgTranscludeCacheExpiry ) {
-                               return $text;
-                       }
+                       return $obj->tc_contents;
                }
 
                $text = Http::get($url);
@@ -3193,7 +3278,7 @@ class Parser
                $dbw = wfGetDB(DB_MASTER);
                $dbw->replace('transcache', array('tc_url'), array(
                        'tc_url' => $url,
-                       'tc_time' => time(),
+                       'tc_time' => $dbw->timestamp( time() ),
                        'tc_contents' => $text));
                return $text;
        }
@@ -3262,55 +3347,47 @@ class Parser
                $name = $frame->expand( $params['name'] );
                $attrText = !isset( $params['attr'] ) ? null : $frame->expand( $params['attr'] );
                $content = !isset( $params['inner'] ) ? null : $frame->expand( $params['inner'] );
-
                $marker = "{$this->mUniqPrefix}-$name-" . sprintf('%08X', $this->mMarkerIndex++) . self::MARKER_SUFFIX;
 
                $isFunctionTag = isset( $this->mFunctionTagHooks[strtolower($name)] ) &&
                        ( $this->ot['html'] || $this->ot['pre'] );
+               if ( $isFunctionTag ) {
+                       $markerType = 'none';
+               } else {
+                       $markerType = 'general';
+               }
                if ( $this->ot['html'] || $isFunctionTag ) {
                        $name = strtolower( $name );
                        $attributes = Sanitizer::decodeTagAttributes( $attrText );
                        if ( isset( $params['attributes'] ) ) {
                                $attributes = $attributes + $params['attributes'];
                        }
-                       switch ( $name ) {
-                               case 'html':
-                                       if( $wgRawHtml ) {
-                                               $output = $content;
-                                               break;
-                                       } else {
-                                               throw new MWException( '<html> extension tag encountered unexpectedly' );
-                                       }
-                               case 'nowiki':
-                                       $content = strtr($content, array('-{' => '-&#123;', '}-' => '&#125;-'));
-                                       $output = Xml::escapeTagsOnly( $content );
-                                       break;
-                               case 'math':
-                                       $output = $wgContLang->armourMath(
-                                               MathRenderer::renderMath( $content, $attributes ) );
-                                       break;
-                               case 'gallery':
-                                       $output = $this->renderImageGallery( $content, $attributes );
-                                       break;
-                               default:
-                                       if( isset( $this->mTagHooks[$name] ) ) {
-                                               # Workaround for PHP bug 35229 and similar
-                                               if ( !is_callable( $this->mTagHooks[$name] ) ) {
-                                                       throw new MWException( "Tag hook for $name is not callable\n" );
-                                               }
-                                               $output = call_user_func_array( $this->mTagHooks[$name],
-                                                       array( $content, $attributes, $this, $frame ) );
-                                       } elseif( isset( $this->mFunctionTagHooks[$name] ) ) {
-                                               list( $callback, $flags ) = $this->mFunctionTagHooks[$name];
-                                               if( !is_callable( $callback ) )
-                                                       throw new MWException( "Tag hook for $name is not callable\n" );
-
-                                               $output = call_user_func_array( $callback,
-                                                       array( &$this, $frame, $content, $attributes ) );
-                                       } else {
-                                               $output = '<span class="error">Invalid tag extension name: ' .
-                                                       htmlspecialchars( $name ) . '</span>';
-                                       }
+
+                       if( isset( $this->mTagHooks[$name] ) ) {
+                               # Workaround for PHP bug 35229 and similar
+                               if ( !is_callable( $this->mTagHooks[$name] ) ) {
+                                       throw new MWException( "Tag hook for $name is not callable\n" );
+                               }
+                               $output = call_user_func_array( $this->mTagHooks[$name],
+                                       array( $content, $attributes, $this, $frame ) );
+                       } elseif( isset( $this->mFunctionTagHooks[$name] ) ) {
+                               list( $callback, $flags ) = $this->mFunctionTagHooks[$name];
+                               if( !is_callable( $callback ) )
+                                       throw new MWException( "Tag hook for $name is not callable\n" );
+
+                               $output = call_user_func_array( $callback,
+                                       array( &$this, $frame, $content, $attributes ) );
+                       } else {
+                               $output = '<span class="error">Invalid tag extension name: ' .
+                                       htmlspecialchars( $name ) . '</span>';
+                       }
+
+                       if ( is_array( $output ) ) {
+                               // Extract flags to local scope (to override $markerType)
+                               $flags = $output;
+                               $output = $flags[0];
+                               unset( $flags[0] );
+                               extract( $flags );
                        }
                } else {
                        if ( is_null( $attrText ) ) {
@@ -3330,12 +3407,14 @@ class Parser
                        }
                }
 
-               if( $isFunctionTag ) {
+               if( $markerType === 'none' ) {
                        return $output;
-               } elseif ( $name === 'html' || $name === 'nowiki' ) {
+               } elseif ( $markerType === 'nowiki' ) {
                        $this->mStripState->nowiki->setPair( $marker, $output );
-               } else {
+               } elseif ( $markerType === 'general' ) {
                        $this->mStripState->general->setPair( $marker, $output );
+               } else {
+                       throw new MWException( __METHOD__.': invalid marker type' );
                }
                return $marker;
        }
@@ -3376,6 +3455,7 @@ class Parser
         */
        function doDoubleUnderscore( $text ) {
                wfProfileIn( __METHOD__ );
+
                // The position of __TOC__ needs to be recorded
                $mw = MagicWord::get( 'toc' );
                if( $mw->match( $text ) ) {
@@ -3415,10 +3495,11 @@ class Parser
                        $this->mOutput->setIndexPolicy( 'index' );
                        $this->addTrackingCategory( 'index-category' );
                }
+
                wfProfileOut( __METHOD__ );
                return $text;
-       }       
-       
+       }
+
        /**
         * Add a tracking category, getting the title from a system message,
         * or print a debug message if the title is invalid.
@@ -3426,7 +3507,12 @@ class Parser
         * @return Bool whether the addition was successful
         */
        protected function addTrackingCategory( $msg ){
-               $containerCategory = Title::makeTitleSafe( NS_CATEGORY, wfMsgForContent( $msg ) );
+               $cat = wfMsgForContent( $msg );
+
+               # Allow tracking categories to be disabled by setting them to "-"
+               if( $cat === '-' ) return false;
+
+               $containerCategory = Title::makeTitleSafe( NS_CATEGORY, $cat );
                if ( $containerCategory ) {
                        $this->mOutput->addCategory( $containerCategory->getDBkey(), $this->getDefaultSort() );
                        return true;
@@ -3452,7 +3538,7 @@ class Parser
         * @private
         */
        function formatHeadings( $text, $origText, $isMain=true ) {
-               global $wgMaxTocLevel, $wgContLang, $wgEnforceHtmlIds;
+               global $wgMaxTocLevel, $wgContLang, $wgHtml5, $wgExperimentalHtmlIds;
 
                $doNumberHeadings = $this->mOptions->getNumberHeadings();
                $showEditLink = $this->mOptions->getEditSection();
@@ -3628,11 +3714,7 @@ class Parser
                        # Save headline for section edit hint before it's escaped
                        $headlineHint = $safeHeadline;
 
-                       if ( $wgEnforceHtmlIds ) {
-                               $legacyHeadline = false;
-                               $safeHeadline = Sanitizer::escapeId( $safeHeadline,
-                                       'noninitial' );
-                       } else {
+                       if ( $wgHtml5 && $wgExperimentalHtmlIds ) {
                                # For reverse compatibility, provide an id that's
                                # HTML4-compatible, like we used to.
                                #
@@ -3644,20 +3726,17 @@ class Parser
                                # to type in section names like "abc_.D7.93.D7.90.D7.A4"
                                # manually, so let's not bother worrying about it.
                                $legacyHeadline = Sanitizer::escapeId( $safeHeadline,
-                                       'noninitial' );
-                               $safeHeadline = Sanitizer::escapeId( $safeHeadline, 'xml' );
+                                       array( 'noninitial', 'legacy' ) );
+                               $safeHeadline = Sanitizer::escapeId( $safeHeadline );
 
                                if ( $legacyHeadline == $safeHeadline ) {
                                        # No reason to have both (in fact, we can't)
                                        $legacyHeadline = false;
-                               } elseif ( $legacyHeadline != Sanitizer::escapeId(
-                               $legacyHeadline, 'xml' ) ) {
-                                       # The legacy id is invalid XML.  We used to allow this, but
-                                       # there's no reason to do so anymore.  Backward
-                                       # compatibility will fail slightly in this case, but it's
-                                       # no big deal.
-                                       $legacyHeadline = false;
                                }
+                       } else {
+                               $legacyHeadline = false;
+                               $safeHeadline = Sanitizer::escapeId( $safeHeadline,
+                                       'noninitial' );
                        }
 
                        # HTML names must be case-insensitively unique (bug 10721).  FIXME:
@@ -3685,7 +3764,7 @@ class Parser
                        # Don't number the heading if it is the only one (looks silly)
                        if( $doNumberHeadings && count( $matches[3] ) > 1) {
                                # the two are different if the line contains a link
-                               $headline=$numbering . ' ' . $headline;
+                               $headline = $numbering . ' ' . $headline;
                        }
 
                        # Create the anchor for linking from the TOC to the section
@@ -3701,7 +3780,7 @@ class Parser
                                $toc .= $sk->tocLine($anchor, $tocline,
                                        $numbering, $toclevel, ($isTemplate ? false : $sectionIndex));
                        }
-                       
+
                        # Add the section to the section tree
                        # Find the DOM node for this header
                        while ( $node && !$isTemplate ) {
@@ -3710,11 +3789,11 @@ class Parser
                                        if ( $bits['i'] == $sectionIndex )
                                                break;
                                }
-                               $byteOffset += mb_strlen( $this->mStripState->unstripBoth( 
+                               $byteOffset += mb_strlen( $this->mStripState->unstripBoth(
                                        $frame->expand( $node, PPFrame::RECOVER_ORIG ) ) );
                                $node = $node->getNextSibling();
                        }
-                       $tocraw[] = array( 
+                       $tocraw[] = array(
                                'toclevel' => $toclevel,
                                'level' => $level,
                                'line' => $tocline,
@@ -3724,7 +3803,7 @@ class Parser
                                'byteoffset' => ( $isTemplate ? null : $byteOffset ),
                                'anchor' => $anchor,
                        );
-                       
+
                        # give headline the correct <h#> tag
                        if( $showEditLink && $sectionIndex !== false ) {
                                if( $isTemplate ) {
@@ -3758,7 +3837,7 @@ class Parser
                        $toc = $sk->tocList( $toc );
                        $this->mOutput->setTOCHTML( $toc );
                }
-               
+
                if ( $isMain ) {
                        $this->mOutput->setSections( $tocraw );
                }
@@ -3767,7 +3846,7 @@ class Parser
 
                $blocks = preg_split( '/<H[1-6].*?' . '>.*?<\/H[1-6]>/i', $text );
                $i = 0;
-               
+
                foreach( $blocks as $block ) {
                        if( $showEditLink && $headlineCount > 0 && $i == 0 && $block !== "\n" ) {
                                # This is the [edit] link that appears for the top block of text when
@@ -3794,7 +3873,7 @@ class Parser
                        return $full;
                }
        }
-       
+
        /**
         * Merge $tree2 into $tree1 by replacing the section with index
         * $section in $tree1 and its descendants with the sections in $tree2.
@@ -3817,7 +3896,7 @@ class Parser
                $numbering = array( 0 );
                $titletext = $title->getPrefixedDBkey();
                foreach ( $tree1 as $s ) {
-                       if ( $targetLevel !== false ) { 
+                       if ( $targetLevel !== false ) {
                                if ( $s['level'] <= $targetLevel )
                                        // We've skipped enough
                                        $targetLevel = false;
@@ -3828,7 +3907,7 @@ class Parser
                                        $s['fromtitle'] != $titletext ) {
                                self::incrementNumbering( $numbering,
                                        $s['toclevel'], $lastLevel );
-                               
+
                                // Rewrite index, byteoffset and number
                                if ( $s['fromtitle'] == $titletext ) {
                                        $s['index'] = $nextIndex++;
@@ -3850,7 +3929,7 @@ class Parser
                                        $s2['level'] += $s['level'] - 1;
                                        $s2['index'] = $nextIndex++;
                                        $s2['byteoffset'] += $s['byteoffset'];
-                                       
+
                                        self::incrementNumbering( $numbering,
                                                $s2['toclevel'], $lastLevel );
                                        $s2['number']  = implode( '.', array_map(
@@ -3866,7 +3945,7 @@ class Parser
                }
                return $newTree;
        }
-       
+
        /**
         * Increment a section number. Helper function for mergeSectionTrees()
         * @param $number array Array representing a section number
@@ -3884,7 +3963,7 @@ class Parser
                } else
                        $number[$level - 1]++;
        }
-       
+
        /**
         * Transform wiki markup when saving a page by doing \r\n -> \n
         * conversion, substitting signatures, {{subst:}} templates, etc.
@@ -3930,26 +4009,29 @@ class Parser
                 * (see also bug 12815)
                 */
                $ts = $this->mOptions->getTimestamp();
-               $tz = wfMsgForContent( 'timezone-utc' );
                if ( isset( $wgLocaltimezone ) ) {
-                       $unixts = wfTimestamp( TS_UNIX, $ts );
-                       $oldtz = getenv( 'TZ' );
-                       putenv( 'TZ='.$wgLocaltimezone );
-                       $ts = date( 'YmdHis', $unixts );
-                       $tz = date( 'T', $unixts );  # might vary on DST changeover!
+                       $tz = $wgLocaltimezone;
+               } else {
+                       $tz = date_default_timezone_get();
+               }
 
-                       /* Allow translation of timezones trough wiki. date() can return
-                        * whatever crap the system uses, localised or not, so we cannot
-                        * ship premade translations.
-                        */
-                       $key = 'timezone-' . strtolower( trim( $tz ) );
-                       $value = wfMsgForContent( $key );
-                       if ( !wfEmptyMsg( $key, $value ) ) $tz = $value;
+               $unixts = wfTimestamp( TS_UNIX, $ts );
+               $oldtz = date_default_timezone_get();
+               date_default_timezone_set( $tz );
+               $ts = date( 'YmdHis', $unixts );
+               $tzMsg = date( 'T', $unixts );  # might vary on DST changeover!
 
-                       putenv( 'TZ='.$oldtz );
-               }
+               /* Allow translation of timezones trough wiki. date() can return
+                * whatever crap the system uses, localised or not, so we cannot
+                * ship premade translations.
+                */
+               $key = 'timezone-' . strtolower( trim( $tzMsg ) );
+               $value = wfMsgForContent( $key );
+               if ( !wfEmptyMsg( $key, $value ) ) $tzMsg = $value;
 
-               $d = $wgContLang->timeanddate( $ts, false, false ) . " ($tz)";
+               date_default_timezone_set( $oldtz );
+
+               $d = $wgContLang->timeanddate( $ts, false, false ) . " ($tzMsg)";
 
                # Variable replacement
                # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
@@ -3963,32 +4045,11 @@ class Parser
                        '~~~' => $sigText
                ) );
 
-               # Context links: [[|name]] and [[name (context)|]]
-               #
+               # Links of the form [[|<blah>]] or [[<blah>|]] perform pipe tricks
+               # Note this only allows the # in the position it works.
                global $wgLegalTitleChars;
-               $tc = "[$wgLegalTitleChars]";
-               $nc = '[ _0-9A-Za-z\x80-\xff-]'; # Namespaces can use non-ascii!
-
-               $p1 = "/\[\[(:?$nc+:|:|)($tc+?)( \\($tc+\\))\\|]]/";            # [[ns:page (context)|]]
-               $p4 = "/\[\[(:?$nc+:|:|)($tc+?)(($tc+))\\|]]/";             # [[ns:page(context)|]]
-               $p3 = "/\[\[(:?$nc+:|:|)($tc+?)( \\($tc+\\)|)(, $tc+|)\\|]]/";  # [[ns:page (context), context|]]
-               $p2 = "/\[\[\\|($tc+)]]/";                                      # [[|page]]
-
-               # try $p1 first, to turn "[[A, B (C)|]]" into "[[A, B (C)|A, B]]"
-               $text = preg_replace( $p1, '[[\\1\\2\\3|\\2]]', $text );
-               $text = preg_replace( $p4, '[[\\1\\2\\3|\\2]]', $text );
-               $text = preg_replace( $p3, '[[\\1\\2\\3\\4|\\2]]', $text );
-
-               $t = $this->mTitle->getText();
-               $m = array();
-               if ( preg_match( "/^($nc+:|)$tc+?( \\($tc+\\))$/", $t, $m ) ) {
-                       $text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text );
-               } elseif ( preg_match( "/^($nc+:|)$tc+?(, $tc+|)$/", $t, $m ) && '' != "$m[1]$m[2]" ) {
-                       $text = preg_replace( $p2, "[[$m[1]\\1$m[2]|\\1]]", $text );
-               } else {
-                       # if there's no context, don't bother duplicating the title
-                       $text = preg_replace( $p2, '[[\\1]]', $text );
-               }
+               $pipeTrickRe = "/\[\[(?:(\\|)([$wgLegalTitleChars]+)|([#$wgLegalTitleChars]+)\\|)\]\]/";
+               $text = preg_replace_callback( $pipeTrickRe, array( $this, 'pstPipeTrickCallback' ), $text );
 
                # Trim trailing whitespace
                $text = rtrim( $text );
@@ -3996,6 +4057,25 @@ class Parser
                return $text;
        }
 
+       /**
+        * Called from pstPass2 to perform the pipe trick on links.
+        * Original was either [[|text]] or [[link|]]
+        *
+        * @param Array ("|" or "", text, link) $m
+        */
+       function pstPipeTrickCallback( $m )
+       {
+               if( $m[1] ) { # [[|<blah>]]
+                       $text = $m[2];
+                       $link = $this->getPipeTrickLink( $text );
+               } else { # [[<blah>|]]
+                       $link = $m[3];
+                       $text = $this->getPipeTrickText( $link );
+               }
+
+               return $link === $text ? "[[$link]]" : "[[$link|$text]]";
+       }
+
        /**
         * Fetch the user's signature text, if any, and normalize to
         * validated, ready-to-insert wikitext.
@@ -4009,14 +4089,14 @@ class Parser
                global $wgMaxSigChars;
 
                $username = $user->getName();
-               
+
                // If not given, retrieve from the user object.
                if ( $nickname === false )
                        $nickname = $user->getOption( 'nickname' );
-               
+
                if ( is_null( $fancySig ) )
                        $fancySig = $user->getBoolOption( 'fancysig' );
-                       
+
                $nickname = $nickname == null ? $username : $nickname;
 
                if( mb_strlen( $nickname ) > $wgMaxSigChars ) {
@@ -4176,7 +4256,9 @@ class Parser
                return $oldVal;
        }
 
+       /* An old work-around for bug 2257 - deprecated 2010-02-13 */
        function setTransparentTagHook( $tag, $callback ) {
+               wfDeprecated( __METHOD__ );
                $tag = strtolower( $tag );
                $oldVal = isset( $this->mTransparentTagHooks[$tag] ) ? $this->mTransparentTagHooks[$tag] : null;
                $this->mTransparentTagHooks[$tag] = $callback;
@@ -4215,23 +4297,23 @@ class Parser
         * @param integer $flags a combination of the following flags:
         *     SFH_NO_HASH   No leading hash, i.e. {{plural:...}} instead of {{#if:...}}
         *
-        *     SFH_OBJECT_ARGS   Pass the template arguments as PPNode objects instead of text. This 
+        *     SFH_OBJECT_ARGS   Pass the template arguments as PPNode objects instead of text. This
         *     allows for conditional expansion of the parse tree, allowing you to eliminate dead
-        *     branches and thus speed up parsing. It is also possible to analyse the parse tree of 
+        *     branches and thus speed up parsing. It is also possible to analyse the parse tree of
         *     the arguments, and to control the way they are expanded.
         *
         *     The $frame parameter is a PPFrame. This can be used to produce expanded text from the
         *     arguments, for instance:
         *         $text = isset( $args[0] ) ? $frame->expand( $args[0] ) : '';
         *
-        *     For technical reasons, $args[0] is pre-expanded and will be a string. This may change in 
+        *     For technical reasons, $args[0] is pre-expanded and will be a string. This may change in
         *     future versions. Please call $frame->expand() on it anyway so that your code keeps
         *     working if/when this is changed.
         *
         *     If you want whitespace to be trimmed from $args, you need to do it yourself, post-
         *     expansion.
         *
-        *     Please read the documentation in includes/parser/Preprocessor.php for more information 
+        *     Please read the documentation in includes/parser/Preprocessor.php for more information
         *     about the methods available in PPFrame and PPNode.
         *
         * @return The old callback function for this name, if any
@@ -4315,19 +4397,6 @@ class Parser
                return $this->mLinkHolders->replaceText( $text );
        }
 
-       /**
-        * Tag hook handler for 'pre'.
-        */
-       function renderPreTag( $text, $attribs ) {
-               // Backwards-compatibility hack
-               $content = StringUtils::delimiterReplace( '<nowiki>', '</nowiki>', '$1', $text, 'i' );
-
-               $attribs = Sanitizer::validateTagAttributes( $attribs, 'pre' );
-               return Xml::openElement( 'pre', $attribs ) .
-                       Xml::escapeTagsOnly( $content ) .
-                       '</pre>';
-       }
-
        /**
         * Renders an image gallery from a text with one line per image.
         * text labels may be given by using |-style alternative text. E.g.
@@ -4376,7 +4445,7 @@ class Parser
                        if ( count( $matches ) == 0 ) {
                                continue;
                        }
-                       
+
                        if ( strpos( $matches[0], '%' ) !== false )
                                $matches[1] = urldecode( $matches[1] );
                        $tp = Title::newFromText( $matches[1]/*, NS_FILE*/ );
@@ -4464,7 +4533,7 @@ class Parser
                #  * upright    reduce width for upright images, rounded to full __0 px
                #  * border     draw a 1px border around the image
                #  * alt        Text for HTML alt attribute (defaults to empty)
-               #  * link       Set the target of the image link. Can be external, interwiki, or local 
+               #  * link       Set the target of the image link. Can be external, interwiki, or local
                # vertical-align values (no % or length right now):
                #  * baseline
                #  * sub
@@ -4537,7 +4606,7 @@ class Parser
                                                switch( $paramName ) {
                                                case 'manualthumb':
                                                case 'alt':
-                                                       // @fixme - possibly check validity here for
+                                                       // @todo Fixme: possibly check validity here for
                                                        // manualthumb? downstream behavior seems odd with
                                                        // missing manual thumbs.
                                                        $validated = true;
@@ -4649,7 +4718,7 @@ class Parser
 
                return $ret;
        }
-       
+
        protected function stripAltText( $caption, $holders ) {
                # Strip bad stuff out of the title (tooltip).  We can't just use
                # replaceLinkHoldersText() here, because if this function is called
@@ -4665,7 +4734,7 @@ class Parser
                # remove the tags
                $tooltip = $this->mStripState->unstripBoth( $tooltip );
                $tooltip = Sanitizer::stripAllTags( $tooltip );
-               
+
                return $tooltip;
        }
 
@@ -4697,9 +4766,9 @@ class Parser
        /**#@+
         * Accessor/mutator
         */
-       function Title( $x = NULL ) { return wfSetVar( $this->mTitle, $x ); }
-       function Options( $x = NULL ) { return wfSetVar( $this->mOptions, $x ); }
-       function OutputType( $x = NULL ) { return wfSetVar( $this->mOutputType, $x ); }
+       function Title( $x = null ) { return wfSetVar( $this->mTitle, $x ); }
+       function Options( $x = null ) { return wfSetVar( $this->mOptions, $x ); }
+       function OutputType( $x = null ) { return wfSetVar( $this->mOutputType, $x ); }
        /**#@-*/
 
        /**#@+
@@ -5101,7 +5170,7 @@ class Parser
                        $links['interwiki'][] = $this->mLinkHolders->interwiki[$key];
                        $pos = $start_pos + strlen( "<!--IWLINK $key-->" );
                }
-               
+
                $data['linkholder'] = $links;
 
                return $data;
@@ -5110,7 +5179,7 @@ class Parser
        function unserialiseHalfParsedText( $data, $intPrefix = null /* Unique identifying prefix */ ) {
                if (!$intPrefix)
                        $intPrefix = $this->getRandomString();
-               
+
                // First, extract the strip state.
                $stripState = $data['stripstate'];
                $this->mStripState->general->merge( $stripState->general );