Fix autonumbering that was broke following bug 787 fixing
[lhc/web/wiklou.git] / includes / Parser.php
index 20f9fbb..720c2da 100644 (file)
@@ -8,6 +8,7 @@
 
 /** */
 require_once( 'Sanitizer.php' );
+require_once( 'HttpFunctions.php' );
 
 /**
  * Update this version number when the ParserOutput format
@@ -46,24 +47,24 @@ define( 'STRIP_COMMENTS', 'HTMLCommentStrip' );
 define( 'UNIQ_PREFIX', 'NaodW29');
 
 # Constants needed for external link processing
-define( 'URL_PROTOCOLS', 'http|https|ftp|irc|gopher|news|mailto' );
-define( 'HTTP_PROTOCOLS', 'http|https' );
+define( 'URL_PROTOCOLS', 'http:\/\/|https:\/\/|ftp:\/\/|irc:\/\/|gopher:\/\/|news:|mailto:' );
+define( 'HTTP_PROTOCOLS', 'http:\/\/|https:\/\/' );
 # Everything except bracket, space, or control characters
 define( 'EXT_LINK_URL_CLASS', '[^]<>"\\x00-\\x20\\x7F]' );
 # Including space
 define( 'EXT_LINK_TEXT_CLASS', '[^\]\\x00-\\x1F\\x7F]' );
 define( 'EXT_IMAGE_FNAME_CLASS', '[A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]' );
 define( 'EXT_IMAGE_EXTENSIONS', 'gif|png|jpg|jpeg' );
-define( 'EXT_LINK_BRACKETED',  '/\[(\b('.URL_PROTOCOLS.'):'.EXT_LINK_URL_CLASS.'+) *('.EXT_LINK_TEXT_CLASS.'*?)\]/S' );
+define( 'EXT_LINK_BRACKETED',  '/\[(\b('.URL_PROTOCOLS.')'.EXT_LINK_URL_CLASS.'+) *('.EXT_LINK_TEXT_CLASS.'*?)\]/S' );
 define( 'EXT_IMAGE_REGEX',
-       '/^('.HTTP_PROTOCOLS.':)'.  # Protocol
+       '/^('.HTTP_PROTOCOLS.')'.  # Protocol
        '('.EXT_LINK_URL_CLASS.'+)\\/'.  # Hostname and path
        '('.EXT_IMAGE_FNAME_CLASS.'+)\\.((?i)'.EXT_IMAGE_EXTENSIONS.')$/S' # Filename
 );
 
 /**
  * PHP Parser
- * 
+ *
  * Processes wiki markup
  *
  * <pre>
@@ -110,11 +111,13 @@ class Parser
            $mTemplatePath;     // stores an unsorted hash of all the templates already loaded
                                // in this path. Used for loop detection.
 
+       var $mIWTransData = array();
+
        /**#@-*/
 
        /**
         * Constructor
-        * 
+        *
         * @access public
         */
        function Parser() {
@@ -179,17 +182,19 @@ class Parser
                $this->mOutputType = OT_HTML;
 
                $this->mStripState = NULL;
-               
+
                //$text = $this->strip( $text, $this->mStripState );
                // VOODOO MAGIC FIX! Sometimes the above segfaults in PHP5.
                $x =& $this->mStripState;
+
+               wfRunHooks( 'ParserBeforeStrip', array( &$this, &$text, &$x ) );
                $text = $this->strip( $text, $x );
+               wfRunHooks( 'ParserAfterStrip', array( &$this, &$text, &$x ) );
 
                $text = $this->internalParse( $text );
 
-               
                $text = $this->unstrip( $text, $this->mStripState );
-               
+
                # Clean up special characters, only run once, next-to-last before doBlockLevels
                $fixtags = array(
                        # french spaces, last one Guillemet-left
@@ -197,39 +202,34 @@ class Parser
                        '/(.) (?=\\?|:|;|!|\\302\\273)/' => '\\1&nbsp;\\2',
                        # french spaces, Guillemet-right
                        '/(\\302\\253) /' => '\\1&nbsp;',
-                       '/<hr *>/i' => '<hr />',
-                       '/<br *>/i' => '<br />',
                        '/<center *>/i' => '<div class="center">',
                        '/<\\/center *>/i' => '</div>',
                );
                $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
-               
+
                # only once and last
                $text = $this->doBlockLevels( $text, $linestart );
 
                $this->replaceLinkHolders( $text );
 
-               $dashReplace = array(
-                       '/ - /' => "&nbsp;&ndash; ", # N dash
-                       '/(?<=[\d])-(?=[\d])/' => "&ndash;", # N dash between numbers
-                       '/ -- /' => "&nbsp;&mdash; " # M dash
-               );
-               $text = preg_replace( array_keys($dashReplace), array_values($dashReplace), $text );
-
-               # the position of the convert() call should not be changed. it 
-               # assumes that the links are all replaces and the only thing left 
+               # the position of the convert() call should not be changed. it
+               # assumes that the links are all replaces and the only thing left
                # is the <nowiki> mark.
                $text = $wgContLang->convert($text);
                $this->mOutput->setTitleText($wgContLang->getParsedTitle());
 
                $text = $this->unstripNoWiki( $text, $this->mStripState );
-               
+
+               wfRunHooks( 'ParserBeforeTidy', array( &$this, &$text ) );
+
                $text = Sanitizer::normalizeCharReferences( $text );
                global $wgUseTidy;
                if ($wgUseTidy) {
                        $text = Parser::tidy($text);
                }
 
+               wfRunHooks( 'ParserAfterTidy', array( &$this, &$text ) );
+
                $this->mOutput->setText( $text );
                wfProfileOut( $fname );
                return $this->mOutput;
@@ -245,12 +245,12 @@ class Parser
                return dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff));
        }
 
-       /** 
+       /**
         * Replaces all occurrences of <$tag>content</$tag> in the text
         * with a random marker and returns the new text. the output parameter
         * $content will be an associative array filled with data on the form
         * $unique_marker => content.
-        * 
+        *
         * If $content is already set, the additional entries will be appended
         * If $tag is set to STRIP_COMMENTS, the function will extract
         * <!-- HTML comments -->
@@ -265,11 +265,11 @@ class Parser
                }
                $n = 1;
                $stripped = '';
-       
+
                if ( !$tags ) {
                        $tags = array( );
                }
-               
+
                if ( !$params ) {
                        $params = array( );
                }
@@ -278,7 +278,7 @@ class Parser
                        $start = '/<!--()/';
                        $end   = '/-->/';
                } else {
-                       $start = "/<$tag([^>]*)>/i";
+                       $start = "/<$tag(\\s+[^>]*|\\s*)>/i";
                        $end   = "/<\\/$tag\\s*>/i";
                }
 
@@ -290,13 +290,13 @@ class Parser
                        }
                        $attributes = $p[1];
                        $inside     = $p[2];
-                       
+
                        $marker = $rnd . sprintf('%08X', $n++);
                        $stripped .= $marker;
-                       
+
                        $tags[$marker] = "<$tag$attributes>";
                        $params[$marker] = Sanitizer::decodeTagAttributes( $attributes );
-                       
+
                        $q = preg_split( $end, $inside, 2 );
                        $content[$marker] = $q[0];
                        if( count( $q ) < 1 ) {
@@ -320,11 +320,11 @@ class Parser
        function extractTags( $tag, $text, &$content, $uniq_prefix = '' ) {
                $dummy_tags = array();
                $dummy_params = array();
-               
+
                return Parser::extractTagsAndParams( $tag, $text, $content,
                        $dummy_tags, $dummy_params, $uniq_prefix );
        }
-       
+
        /**
         * Strips and renders nowiki, pre, math, hiero
         * If $render is set, performs necessary rendering operations on plugins
@@ -355,8 +355,8 @@ class Parser
                #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
 
                # html
-               global $wgRawHtml, $wgWhitelistEdit;
-               if( $wgRawHtml && $wgWhitelistEdit ) {
+               global $wgRawHtml;
+               if( $wgRawHtml ) {
                        $text = Parser::extractTags('html', $text, $html_content, $uniq_prefix);
                        foreach( $html_content as $marker => $content ) {
                                if ($render ) {
@@ -519,7 +519,9 @@ class Parser
                          'html' => array(),
                          'nowiki' => array(),
                          'math' => array(),
-                         'pre' => array()
+                         'pre' => array(),
+                         'comment' => array(),
+                         'gallery' => array(),
                        );
                }
                $state['item'][$rnd] = $text;
@@ -556,7 +558,7 @@ class Parser
                }
                return $correctedtext;
        }
-       
+
        /**
         * Spawn an external HTML tidy process and get corrected markup back from it.
         *
@@ -613,7 +615,7 @@ class Parser
                global $wgTidyConf;
                $fname = 'Parser::internalTidy';
                wfProfileIn( $fname );
-               
+
                tidy_load_config( $wgTidyConf );
                tidy_set_encoding( 'utf8' );
                tidy_parse_string( $text );
@@ -765,20 +767,28 @@ class Parser
                }
                $text = $this->doAllQuotes( $text );
                $text = $this->replaceInternalLinks( $text );
-               $text = $this->replaceExternalLinks( $text );           
-               
+               $text = $this->replaceExternalLinks( $text );
+
                # replaceInternalLinks may sometimes leave behind
                # absolute URLs, which have to be masked to hide them from replaceExternalLinks
                $text = str_replace("http-noparse://","http://",$text);
-               
+
                $text = $this->doMagicLinks( $text );
                $text = $this->doTableStuff( $text );
                $text = $this->formatHeadings( $text, $isMain );
 
+               $regex = '/<!--IW_TRANSCLUDE (\d+)-->/';
+               $text = preg_replace_callback($regex, array(&$this, 'scarySubstitution'), $text);
+
                wfProfileOut( $fname );
                return $text;
        }
 
+       function scarySubstitution($matches) {
+#              return "[[".$matches[0]."]]";
+               return $this->mIWTransData[(int)$matches[0]];
+       }
+
        /**
         * Replace special strings like "ISBN xxx" and "RFC xxx" with
         * magic external links.
@@ -1019,7 +1029,7 @@ class Parser
                wfProfileIn( $fname );
 
                $sk =& $this->mOptions->getSkin();
-               
+
                $bits = preg_split( EXT_LINK_BRACKETED, $text, -1, PREG_SPLIT_DELIM_CAPTURE );
 
                $s = $this->replaceFreeExternalLinks( array_shift( $bits ) );
@@ -1054,7 +1064,7 @@ class Parser
                        # No link text, e.g. [http://domain.tld/some.link]
                        if ( $text == '' ) {
                                # Autonumber if allowed
-                               if ( strpos( HTTP_PROTOCOLS, $protocol ) !== false ) {
+                               if ( strpos( HTTP_PROTOCOLS, str_replace('/','\/', $protocol) ) !== false ) {
                                        $text = '[' . ++$this->mAutonumber . ']';
                                        $linktype = 'autonumber';
                                } else {
@@ -1099,8 +1109,8 @@ class Parser
                global $wgContLang;
                $fname = 'Parser::replaceFreeExternalLinks';
                wfProfileIn( $fname );
-               
-               $bits = preg_split( '/(\b(?:'.URL_PROTOCOLS.'):)/S', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
+
+               $bits = preg_split( '/(\b(?:'.URL_PROTOCOLS.'))/S', $text, -1, PREG_SPLIT_DELIM_CAPTURE );
                $s = array_shift( $bits );
                $i = 0;
 
@@ -1171,7 +1181,7 @@ class Parser
                }
                return $text;
        }
-       
+
        /**
         * Process [[ ]] wikilinks
         *
@@ -1187,7 +1197,7 @@ class Parser
                static $tc = FALSE;
                # the % is needed to support urlencoded titles as well
                if ( !$tc ) { $tc = Title::legalChars() . '#%'; }
-               
+
                $sk =& $this->mOptions->getSkin();
 
                #split the entire text string on occurences of [[
@@ -1229,7 +1239,7 @@ class Parser
 
                $checkVariantLink = sizeof($wgContLang->getVariants())>1;
                $useSubpages = $this->areSubpagesAllowed();
-               
+
                # Loop for each link
                for ($k = 0; isset( $a[$k] ); $k++) {
                        $line = $a[$k];
@@ -1250,7 +1260,7 @@ class Parser
                        }
 
                        $might_be_img = false;
-                       
+
                        if ( preg_match( $e1, $line, $m ) ) { # page with normal text or alt
                                $text = $m[2];
                                # If we get a ] at the beginning of $m[3] that means we have a link that's something like:
@@ -1281,7 +1291,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(?:'.URL_PROTOCOLS.'):)/', $m[1])) {
+                       if (preg_match('/^(\b(?:'.URL_PROTOCOLS.'))/', $m[1])) {
                                $s .= $prefix . '[[' . $line ;
                                continue;
                        }
@@ -1298,7 +1308,7 @@ class Parser
                                # Strip off leading ':'
                                $link = substr($link, 1);
                        }
-                       
+
                        $nt =& Title::newFromText( $this->unstripNoWiki($link, $this->mStripState) );
                        if( !$nt ) {
                                $s .= $prefix . '[[' . $line;
@@ -1314,7 +1324,7 @@ class Parser
 
                        $ns = $nt->getNamespace();
                        $iw = $nt->getInterWiki();
-                       
+
                        if ($might_be_img) { # if this is actually an invalid link
                                if ($ns == NS_IMAGE && $noforce) { #but might be an image
                                        $found = false;
@@ -1354,7 +1364,7 @@ class Parser
                        $wasblank = ( '' == $text );
                        if( $wasblank ) $text = $link;
 
-                       
+
                        # Link not escaped by : , create the various objects
                        if( $noforce ) {
 
@@ -1365,7 +1375,7 @@ class Parser
                                        $s .= trim($prefix . $trail, "\n") == '' ? '': $prefix . $trail;
                                        continue;
                                }
-                               
+
                                if ( $ns == NS_IMAGE ) {
                                        wfProfileIn( "$fname-image" );
                                        if ( !wfIsBadImage( $nt->getDBkey() ) ) {
@@ -1374,21 +1384,21 @@ class Parser
                                                # but it might be hard to fix that, and it doesn't matter ATM
                                                $text = $this->replaceExternalLinks($text);
                                                $text = $this->replaceInternalLinks($text);
-                                               
+
                                                # cloak any absolute URLs inside the image markup, so replaceExternalLinks() won't touch them
                                                $s .= $prefix . str_replace('http://', 'http-noparse://', $this->makeImage( $nt, $text ) ) . $trail;
                                                $wgLinkCache->addImageLinkObj( $nt );
-                                               
+
                                                wfProfileOut( "$fname-image" );
                                                continue;
                                        }
                                        wfProfileOut( "$fname-image" );
 
                                }
-                               
+
                                if ( $ns == NS_CATEGORY ) {
                                        wfProfileIn( "$fname-category" );
-                                       $t = $wgContLang->convert($nt->getText());
+                                       $t = $wgContLang->convertHtml( $nt->getText() );
                                        $s = rtrim($s . "\n"); # bug 87
 
                                        $wgLinkCache->suspend(); # Don't save in links/brokenlinks
@@ -1407,13 +1417,13 @@ class Parser
                                        $sortkey = $wgContLang->convertCategoryKey( $sortkey );
                                        $wgLinkCache->addCategoryLinkObj( $nt, $sortkey );
                                        $this->mOutput->addCategoryLink( $t );
-                                       
+
                                        /**
                                         * Strip the whitespace Category links produce, see bug 87
                                         * @todo We might want to use trim($tmp, "\n") here.
                                         */
                                        $s .= trim($prefix . $trail, "\n") == '' ? '': $prefix . $trail;
-                                       
+
                                        wfProfileOut( "$fname-category" );
                                        continue;
                                }
@@ -1435,12 +1445,18 @@ class Parser
                                $s .= $prefix . $sk->makeKnownLinkObj( $nt, $text, '', $trail );
                                continue;
                        }
-                       if ( $nt->isAlwaysKnown() ) {
+                       if( !$nt->isExternal() && $nt->isAlwaysKnown() ) {
+                               /**
+                                * Skip lookups for special pages and self-links.
+                                * External interwiki links are not included here because
+                                * the HTTP urls would break output in the next parse step;
+                                * they will have placeholders kept.
+                                */
                                $s .= $sk->makeKnownLinkObj( $nt, $text, '', $trail, $prefix );
                        } else {
                                /**
                                 * Add a link placeholder
-                                * Later, this will be replaced by a real link, after the existence or 
+                                * Later, this will be replaced by a real link, after the existence or
                                 * non-existence of all the links is known
                                 */
                                $s .= $this->makeLinkHolder( $nt, $text, '', $trail, $prefix );
@@ -1452,8 +1468,8 @@ class Parser
 
        /**
         * Make a link placeholder. The text returned can be later resolved to a real link with
-        * replaceLinkHolders(). This is done for two reasons: firstly to avoid further 
-        * parsing of interwiki links, and secondly to allow all extistence checks and 
+        * replaceLinkHolders(). This is done for two reasons: firstly to avoid further
+        * parsing of interwiki links, and secondly to allow all extistence checks and
         * article length checks (for stub links) to be bundled into a single query.
         *
         */
@@ -1464,17 +1480,17 @@ class Parser
                } else {
                        # Separate the link trail from the rest of the link
                        list( $inside, $trail ) = Linker::splitTrail( $trail );
-                       
+
                        if ( $nt->isExternal() ) {
                                $nr = array_push( $this->mInterwikiLinkHolders['texts'], $prefix.$text.$inside );
-                               $this->mInterwikiLinkHolders['titles'][] =& $nt;
+                               $this->mInterwikiLinkHolders['titles'][] = $nt;
                                $retVal = '<!--IWLINK '. ($nr-1) ."-->{$trail}";
                        } else {
                                $nr = array_push( $this->mLinkHolders['namespaces'], $nt->getNamespace() );
                                $this->mLinkHolders['dbkeys'][] = $nt->getDBkey();
                                $this->mLinkHolders['queries'][] = $query;
                                $this->mLinkHolders['texts'][] = $prefix.$text.$inside;
-                               $this->mLinkHolders['titles'][] =& $nt;
+                               $this->mLinkHolders['titles'][] = $nt;
 
                                $retVal = '<!--LINK '. ($nr-1) ."-->{$trail}";
                        }
@@ -1491,7 +1507,7 @@ class Parser
                global $wgNamespacesWithSubpages;
                return !empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()]);
        }
-       
+
        /**
         * Handle link to subpage if necessary
         * @param string $target the source of the link
@@ -1511,10 +1527,10 @@ class Parser
                $fname = 'Parser::maybeDoSubpageLink';
                wfProfileIn( $fname );
                $ret = $target; # default return value is no change
-                       
-               # Some namespaces don't allow subpages, 
+
+               # Some namespaces don't allow subpages,
                # so only perform processing if subpages are allowed
-               if( $this->areSubpagesAllowed() ) {             
+               if( $this->areSubpagesAllowed() ) {
                        # Look at the first character
                        if( $target != '' && $target{0} == '/' ) {
                                # / at end means we don't want the slash to be shown
@@ -1524,7 +1540,7 @@ class Parser
                                } else {
                                        $noslash = substr( $target, 1 );
                                }
-                               
+
                                $ret = $this->mTitle->getPrefixedText(). '/' . trim($noslash);
                                if( '' === $text ) {
                                        $text = $target;
@@ -1847,15 +1863,15 @@ class Parser
         * @access private
         */
        function getVariableValue( $index ) {
-               global $wgContLang, $wgSitename, $wgServer, $wgArticle;
-               
+               global $wgContLang, $wgSitename, $wgServer, $wgServerName, $wgArticle, $wgScriptPath;
+
                /**
                 * Some of these require message or data lookups and can be
                 * expensive to check many times.
                 */
                static $varCache = array();
                if( isset( $varCache[$index] ) ) return $varCache[$index];
-               
+
                switch ( $index ) {
                        case MAG_CURRENTMONTH:
                                return $varCache[$index] = $wgContLang->formatNum( date( 'm' ) );
@@ -1888,10 +1904,16 @@ class Parser
                                return $varCache[$index] = $wgContLang->formatNum( date('w') );
                        case MAG_NUMBEROFARTICLES:
                                return $varCache[$index] = $wgContLang->formatNum( wfNumberOfArticles() );
+                       case MAG_NUMBEROFFILES:
+                               return $varCache[$index] = $wgContLang->formatNum( wfNumberOfFiles() );
                        case MAG_SITENAME:
                                return $wgSitename;
                        case MAG_SERVER:
                                return $wgServer;
+                       case MAG_SERVERNAME:
+                               return $wgServerName;
+                       case MAG_SCRIPTPATH:
+                               return $wgScriptPath;
                        default:
                                return NULL;
                }
@@ -1923,7 +1945,7 @@ class Parser
         *  OT_WIKI: only {{subst:}} templates
         *  OT_MSG: only magic variables
         *  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
         * @access private
@@ -1945,7 +1967,7 @@ class Parser
 
                # Variable substitution
                $text = preg_replace_callback( "/{{([$titleChars]*?)}}/", array( &$this, 'variableSubstitution' ), $text );
-               
+
                if ( $this->mOutputType == OT_HTML || $this->mOutputType == OT_WIKI ) {
                        # Argument substitution
                        $text = preg_replace_callback( "/{{{([$titleChars]*?)}}}/", array( &$this, 'argSubstitution' ), $text );
@@ -2004,7 +2026,7 @@ class Parser
                # merged with the next arg because the '|' character between belongs
                # to the link syntax and not the template parameter syntax.
                $argc = count($args);
-               
+
                for ( $i = 0; $i < $argc-1; $i++ ) {
                        if ( substr_count ( $args[$i], '[[' ) != substr_count ( $args[$i], ']]' ) ) {
                                $args[$i] .= '|'.$args[$i+1];
@@ -2031,7 +2053,7 @@ class Parser
                global $wgLinkCache, $wgContLang;
                $fname = 'Parser::braceSubstitution';
                wfProfileIn( $fname );
-               
+
                $found = false;
                $nowiki = false;
                $noparse = false;
@@ -2079,9 +2101,13 @@ class Parser
                        $mwMsgnw =& MagicWord::get( MAG_MSGNW );
                        if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
                                $nowiki = true;
+                       } else {
+                               # Remove obsolete MSG:
+                               $mwMsg =& MagicWord::get( MAG_MSG );
+                               $mwMsg->matchStartAndRemove( $part1 );
                        }
 
-                       # int: is the wikitext equivalent of wfMsg()
+                       # Check if it is an internal message
                        $mwInt =& MagicWord::get( MAG_INT );
                        if ( $mwInt->matchStartAndRemove( $part1 ) ) {
                                if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
@@ -2089,15 +2115,6 @@ class Parser
                                        $found = true;
                                }
                        }
-
-                       # msg: is the wikitext equivalent of wfMsgForContent()
-                       $mwMsg =& MagicWord::get( MAG_MSG );
-                       if ( $mwMsg->matchStartAndRemove( $part1 ) ) {
-                               if ( $this->incrementIncludeCount( 'msg:'.$part1 ) ) {
-                                        $text = $linestart . wfMsgReal( $part1, $args, true, true );
-                                        $found = true;
-                               }
-                       }
                }
 
                # NS
@@ -2185,6 +2202,14 @@ class Parser
                                $ns = $this->mTitle->getNamespace();
                        }
                        $title = Title::newFromText( $part1, $ns );
+
+                        if ($title) {
+                            $interwiki = Title::getInterwikiLink($title->getInterwiki());
+                            if ($interwiki != '' && $title->isTrans()) {
+                                    return $this->scarytransclude($title, $interwiki);
+                            }
+                        }
+
                        if ( !is_null( $title ) && !$title->isExternal() ) {
                                # Check for excessive inclusion
                                $dbk = $title->getPrefixedDBkey();
@@ -2269,14 +2294,14 @@ class Parser
                }
                # Prune lower levels off the recursion check path
                $this->mTemplatePath = $lastPathLevel;
-               
+
                if ( !$found ) {
                        wfProfileOut( $fname );
                        return $matches[0];
                } else {
                        if ( $isHTML ) {
                                # Replace raw HTML by a placeholder
-                               # Add a blank line preceding, to prevent it from mucking up 
+                               # Add a blank line preceding, to prevent it from mucking up
                                # immediately preceding headings
                                $text = "\n\n" . $this->insertStripItem( $text, $this->mStripState );
                        } else {
@@ -2302,16 +2327,16 @@ class Parser
                                                preg_match('/^(={1,6})(.*?)(={1,6})\s*?$/m', $hl, $m2);
                                                $text .= $m2[1] . $m2[2] . "<!--MWTEMPLATESECTION="
                                                        . $encodedname . "&" . base64_encode("$nsec") . "-->" . $m2[3];
-                                               
+
                                                $nsec++;
                                        }
                                }
                        }
                }
-               
+
                # Prune lower levels off the recursion check path
                $this->mTemplatePath = $lastPathLevel;
-               
+
                if ( !$found ) {
                        wfProfileOut( $fname );
                        return $matches[0];
@@ -2321,6 +2346,49 @@ class Parser
                }
        }
 
+       /**
+        * Translude an interwiki link.
+        */
+       function scarytransclude($title, $interwiki) {
+               global $wgEnableScaryTranscluding;
+
+               if (!$wgEnableScaryTranscluding)
+                       return wfMsg('scarytranscludedisabled');
+
+               $articlename = "Template:" . $title->getDBkey();
+               $url = str_replace('$1', urlencode($articlename), $interwiki);
+               if (strlen($url) > 255)
+                       return wfMsg('scarytranscludetoolong');
+               $text = $this->fetchScaryTemplateMaybeFromCache($url);
+               $this->mIWTransData[] = $text;
+               return "<!--IW_TRANSCLUDE ".(count($this->mIWTransData) - 1)."-->";
+       }
+
+       function fetchScaryTemplateMaybeFromCache($url) {
+               $dbr = wfGetDB(DB_SLAVE);
+               $obj = $dbr->selectRow('transcache', array('tc_time', 'tc_contents'),
+                               array('tc_url' => $url));
+               if ($obj) {
+                       $time = $obj->tc_time;
+                       $text = $obj->tc_contents;
+                       if ($time && $time < (time() + (60*60))) {
+                               return $text;
+                       }
+               }
+
+               $text = wfGetHTTP($url . '?action=render');
+               if (!$text)
+                       return wfMsg('scarytranscludefailed', $url);
+
+               $dbw = wfGetDB(DB_MASTER);
+               $dbw->replace('transcache', array(), array(
+                       'tc_url' => $url,
+                       'tc_time' => time(),
+                       'tc_contents' => $text));
+               return $text;
+       }
+
+
        /**
         * Triple brace replacement -- used for template arguments
         * @access private
@@ -2358,10 +2426,10 @@ class Parser
         * 2) Add an [edit] link to sections for logged in users who have enabled the option
         * 3) Add a Table of contents on the top for users who have enabled the option
         * 4) Auto-anchor headings
-        *      
+        *
         * It loops through all headlines, collects the necessary data, then splits up the
         * string and re-inserts the newly formatted headlines.
-        * 
+        *
         * @param string $text
         * @param boolean $isMain
         * @access private
@@ -2458,9 +2526,9 @@ class Parser
                                $prevtoclevel = $toclevel;
                        }
                        $level = $matches[1][$headlineCount];
-                       
+
                        if( $doNumberHeadings || $doShowToc ) {
-                               
+
                                if ( $level > $prevlevel ) {
                                        # Increase TOC level
                                        $toclevel++;
@@ -2494,7 +2562,7 @@ class Parser
                                        # No change in level, end TOC line
                                        $toc .= $sk->tocLineEnd();
                                }
-                               
+
                                $levelCount[$toclevel] = $level;
 
                                # count number of headlines for each level
@@ -2518,7 +2586,7 @@ class Parser
 
                        # Remove link placeholders by the link text.
                        #     <!--LINK number-->
-                       # turns into 
+                       # turns into
                        #     link text with suffix
                        $canonized_headline = preg_replace( '/<!--LINK ([0-9]*)-->/e',
                                                            "\$this->mLinkHolders['texts'][\$1]",
@@ -2670,7 +2738,7 @@ class Parser
         * @return string
         */
        function magicRFC( $text, $keyword='RFC ', $urlmsg='rfcurl'  ) {
-               
+
                $valid = '0123456789';
                $internal = false;
 
@@ -2679,7 +2747,7 @@ class Parser
                        return $text;
                }
                $text = substr( array_shift( $a ), 1);
-               
+
                /* Check if keyword is preceed by [[.
                 * This test is made here cause of the array_shift above
                 * that prevent the test to be done in the foreach.
@@ -2722,7 +2790,7 @@ class Parser
                                $la = $sk->getExternalLinkAttributes( $url, $keyword.$id );
                                $text .= "<a href='{$url}'{$la}>{$keyword}{$id}</a>{$x}";
                        }
-                       
+
                        /* Check if the next RFC keyword is preceed by [[ */
                        $internal = ( substr($x,-2) == '[[' );
                }
@@ -2788,7 +2856,7 @@ class Parser
                 * everyone the same signiture and use the default one rather
                 * than the one selected in each users preferences.
                 */
-               $d = $wgContLang->timeanddate( wfTimestampNow(), false, false) .
+               $d = $wgContLang->timeanddate( date( 'YmdHis' ), false, false) .
                  ' (' . date( 'T' ) . ')';
                if ( isset( $wgLocaltimezone ) ) {
                        putenv( 'TZ='.$oldtz );
@@ -2855,7 +2923,7 @@ class Parser
 
        /**
         * Transform a MediaWiki message by replacing magic variables.
-        * 
+        *
         * @param string $text the text to transform
         * @param ParserOptions $options  options
         * @return string the text with variables substituted
@@ -2912,16 +2980,16 @@ class Parser
                $pdbks = array();
                $colours = array();
                $sk = $this->mOptions->getSkin();
-               
+
                if ( !empty( $this->mLinkHolders['namespaces'] ) ) {
                        wfProfileIn( $fname.'-check' );
                        $dbr =& wfGetDB( DB_SLAVE );
                        $page = $dbr->tableName( 'page' );
                        $threshold = $wgUser->getOption('stubthreshold');
-                       
+
                        # Sort by namespace
                        asort( $this->mLinkHolders['namespaces'] );
-       
+
                        # Generate query
                        $query = false;
                        foreach ( $this->mLinkHolders['namespaces'] as $key => $val ) {
@@ -2955,7 +3023,7 @@ class Parser
                                        } else {
                                                $query .= ', ';
                                        }
-                               
+
                                        $query .= $dbr->addQuotes( $this->mLinkHolders['dbkeys'][$key] );
                                }
                        }
@@ -2964,9 +3032,9 @@ class Parser
                                if ( $options & RLH_FOR_UPDATE ) {
                                        $query .= ' FOR UPDATE';
                                }
-                       
+
                                $res = $dbr->query( $query, $fname );
-                               
+
                                # Fetch data and form into an associative array
                                # non-existent = broken
                                # 1 = known
@@ -2975,7 +3043,7 @@ class Parser
                                        $title = Title::makeTitle( $s->page_namespace, $s->page_title );
                                        $pdbk = $title->getPrefixedDBkey();
                                        $wgLinkCache->addGoodLinkObj( $s->page_id, $title );
-                                       
+
                                        if ( $threshold >  0 ) {
                                                $size = $s->page_len;
                                                if ( $s->page_is_redirect || $s->page_namespace != 0 || $size >= $threshold ) {
@@ -2989,7 +3057,7 @@ class Parser
                                }
                        }
                        wfProfileOut( $fname.'-check' );
-                       
+
                        # Construct search and replace arrays
                        wfProfileIn( $fname.'-construct' );
                        $wgOutputReplace = array();
@@ -3017,7 +3085,7 @@ class Parser
 
                        # Do the thing
                        wfProfileIn( $fname.'-replace' );
-                       
+
                        $text = preg_replace_callback(
                                '/(<!--LINK .*?-->)/',
                                "wfOutputReplaceMatches",
@@ -3036,18 +3104,18 @@ class Parser
                                $title = $this->mInterwikiLinkHolders['titles'][$key];
                                $wgOutputReplace[$key] = $sk->makeLinkObj( $title, $link );
                        }
-                       
+
                        $text = preg_replace_callback(
                                '/<!--IWLINK (.*?)-->/',
                                "wfOutputReplaceMatches",
                                $text );
                        wfProfileOut( $fname.'-interwiki' );
                }
-               
+
                wfProfileOut( $fname );
                return $colours;
        }
-       
+
        /**
         * Replace <!--LINK--> link placeholders with plain text of links
         * (not HTML-formatted).
@@ -3065,11 +3133,11 @@ class Parser
                        '/<!--(LINK|IWLINK) (.*?)-->/',
                        array( &$this, 'replaceLinkHoldersTextCallback' ),
                        $text );
-               
+
                wfProfileOut( $fname );
                return $text;
        }
-       
+
        /**
         * @param array $matches
         * @return string
@@ -3103,9 +3171,10 @@ class Parser
         */
        function renderImageGallery( $text ) {
                # Setup the parser
-               global $wgUser, $wgParser, $wgTitle;
+               global $wgUser, $wgTitle;
                $parserOptions = ParserOptions::newFromUser( $wgUser );
-       
+               $localParser = new Parser();
+
                global $wgLinkCache;
                $ig = new ImageGallery();
                $ig->setShowBytes( false );
@@ -3130,10 +3199,10 @@ class Parser
                        } else {
                                $label = '';
                        }
-                       
-                       $html = $wgParser->parse( $label , $wgTitle, $parserOptions );
+
+                       $html = $localParser->parse( $label , $wgTitle, $parserOptions );
                        $html = $html->mText;
-                       
+
                        $ig->add( new Image( $nt ), $html );
                        $wgLinkCache->addImageLinkObj( $nt );
                }
@@ -3146,7 +3215,7 @@ class Parser
        function makeImage( &$nt, $options ) {
                global $wgContLang, $wgUseImageResize;
                global $wgUser, $wgThumbLimits;
-               
+
                $align = '';
 
                # Check if the options text is of the form "options|alt text"
@@ -3171,10 +3240,10 @@ class Parser
                $caption = '';
 
                $width = $height = $framed = $thumb = false;
-               $manual_thumb = "" ;
+               $manual_thumb = '' ;
 
                foreach( $part as $key => $val ) {
-                       $val_parts = explode ( "=" , $val , 2 ) ;
+                       $val_parts = explode ( '=' , $val , 2 ) ;
                        $left_part = array_shift ( $val_parts ) ;
                        if ( $wgUseImageResize && ! is_null( $mwThumb->matchVariableStartToEnd($val) ) ) {
                                $thumb=true;
@@ -3339,7 +3408,7 @@ class ParserOptions
 
        /** Get user options */
        function initialiseFromUser( &$userInput ) {
-               global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages, 
+               global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages,
                       $wgAllowSpecialInclusion;
                $fname = 'ParserOptions::initialiseFromUser';
                wfProfileIn( $fname );
@@ -3358,7 +3427,7 @@ class ParserOptions
                $this->mSkin =& $user->getSkin();
                wfProfileOut( $fname.'-skin' );
                $this->mDateFormat = $user->getOption( 'date' );
-               $this->mEditSection = $user->getOption( 'editsection' );
+               $this->mEditSection = true;
                $this->mNumberHeadings = $user->getOption( 'numberheadings' );
                $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
                wfProfileOut( $fname );
@@ -3384,6 +3453,20 @@ function wfNumberOfArticles() {
        return $wgNumberOfArticles;
 }
 
+/**
+ * Return the number of files
+ */
+function wfNumberOfFiles() {
+       $fname = 'Parser::wfNumberOfFiles';
+
+       wfProfileIn( $fname );
+       $dbr =& wfGetDB( DB_SLAVE );
+       $res = $dbr->selectField('image', 'COUNT(*)', array(), $fname );
+       wfProfileOut( $fname );
+
+       return $res;
+}
+
 /**
  * Get various statistics from the database
  * @private
@@ -3411,7 +3494,7 @@ function wfLoadSiteStats() {
 /**
  * Escape html tags
  * Basicly replacing " > and < with HTML entities ( &quot;, &gt;, &lt;)
- *  
+ *
  * @param string $in Text that might contain HTML tags
  * @return string Escaped string
  */