Merged localisation-work branch:
[lhc/web/wiklou.git] / includes / Parser.php
index 1469b80..3ebe597 100644 (file)
@@ -6,10 +6,6 @@
  * @subpackage Parser
  */
 
-/** */
-require_once( 'Sanitizer.php' );
-require_once( 'HttpFunctions.php' );
-
 /**
  * Update this version number when the ParserOutput format
  * changes in an incompatible way, so the parser cache
@@ -37,6 +33,9 @@ define( 'OT_HTML', 1 );
 define( 'OT_WIKI', 2 );
 define( 'OT_MSG' , 3 );
 
+# Flags for setFunctionHook
+define( 'SFH_NO_HASH', 1 );
+
 # string parameter for extractTags which will cause it
 # to strip HTML comments in addition to regular
 # <XML>-style tags. This should not be anything we
@@ -47,17 +46,28 @@ define( 'STRIP_COMMENTS', 'HTMLCommentStrip' );
 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]' );
+# Including space, but excluding newlines
+define( 'EXT_LINK_TEXT_CLASS', '[^\]\\x0a\\x0d]' );
 define( 'EXT_IMAGE_FNAME_CLASS', '[A-Za-z0-9_.,~%\\-+&;#*?!=()@\\x80-\\xFF]' );
 define( 'EXT_IMAGE_EXTENSIONS', 'gif|png|jpg|jpeg' );
-define( 'EXT_LINK_BRACKETED',  '/\[(\b(' . wfUrlProtocols() . ')'.EXT_LINK_URL_CLASS.'+) *('.EXT_LINK_TEXT_CLASS.'*?)\]/S' );
+define( 'EXT_LINK_BRACKETED',  '/\[(\b(' . wfUrlProtocols() . ')'.
+       EXT_LINK_URL_CLASS.'+) *('.EXT_LINK_TEXT_CLASS.'*?)\]/S' );
 define( 'EXT_IMAGE_REGEX',
        '/^('.HTTP_PROTOCOLS.')'.  # Protocol
        '('.EXT_LINK_URL_CLASS.'+)\\/'.  # Hostname and path
        '('.EXT_IMAGE_FNAME_CLASS.'+)\\.((?i)'.EXT_IMAGE_EXTENSIONS.')$/S' # Filename
 );
 
+// State constants for the definition list colon extraction
+define( 'MW_COLON_STATE_TEXT', 0 );
+define( 'MW_COLON_STATE_TAG', 1 );
+define( 'MW_COLON_STATE_TAGSTART', 2 );
+define( 'MW_COLON_STATE_CLOSETAG', 3 );
+define( 'MW_COLON_STATE_TAGSLASH', 4 );
+define( 'MW_COLON_STATE_COMMENT', 5 );
+define( 'MW_COLON_STATE_COMMENTDASH', 6 );
+define( 'MW_COLON_STATE_COMMENTDASHDASH', 7 );
+
 /**
  * PHP Parser
  *
@@ -93,11 +103,11 @@ class Parser
         * @private
         */
        # Persistent:
-       var $mTagHooks, $mFunctionHooks;
+       var $mTagHooks, $mFunctionHooks, $mFunctionSynonyms, $mVariables;
 
        # Cleared with clearState():
        var $mOutput, $mAutonumber, $mDTopen, $mStripState = array();
-       var $mVariables, $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
+       var $mIncludeCount, $mArgStack, $mLastSection, $mInPre;
        var $mInterwikiLinkHolders, $mLinkHolders, $mUniqPrefix;
        var $mTemplates,        // cache of already loaded templates, avoids
                                // multiple SQL queries for the same string
@@ -121,20 +131,69 @@ class Parser
        function Parser() {
                $this->mTagHooks = array();
                $this->mFunctionHooks = array();
-               $this->clearState();
+               $this->mFunctionSynonyms = array( 0 => array(), 1 => array() );
+               $this->mFirstCall = true;
        }
 
+       /**
+        * Do various kinds of initialisation on the first call of the parser
+        */
+       function firstCallInit() {
+               if ( !$this->mFirstCall ) {
+                       return;
+               }
+
+               wfProfileIn( __METHOD__ );
+               global $wgAllowDisplayTitle, $wgAllowSlowParserFunctions;
+
+               $this->setHook( 'pre', array( $this, 'renderPreTag' ) );
+
+               $this->setFunctionHook( 'ns', array( 'CoreParserFunctions', 'ns' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'urlencode', array( 'CoreParserFunctions', 'urlencode' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'lcfirst', array( 'CoreParserFunctions', 'lcfirst' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'ucfirst', array( 'CoreParserFunctions', 'ucfirst' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'lc', array( 'CoreParserFunctions', 'lc' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'uc', array( 'CoreParserFunctions', 'uc' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'localurl', array( 'CoreParserFunctions', 'localurl' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'localurle', array( 'CoreParserFunctions', 'localurle' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'fullurl', array( 'CoreParserFunctions', 'fullurl' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'fullurle', array( 'CoreParserFunctions', 'fullurle' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'formatnum', array( 'CoreParserFunctions', 'formatnum' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'grammar', array( 'CoreParserFunctions', 'grammar' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'plural', array( 'CoreParserFunctions', 'plural' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'numberofpages', array( 'CoreParserFunctions', 'numberofpages' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'numberofusers', array( 'CoreParserFunctions', 'numberofusers' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'numberofarticles', array( 'CoreParserFunctions', 'numberofarticles' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'numberoffiles', array( 'CoreParserFunctions', 'numberoffiles' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'numberofadmins', array( 'CoreParserFunctions', 'numberofadmins' ), SFH_NO_HASH );
+               $this->setFunctionHook( 'language', array( 'CoreParserFunctions', 'language' ), SFH_NO_HASH );
+
+               if ( $wgAllowDisplayTitle ) {
+                       $this->setFunctionHook( 'displaytitle', array( 'CoreParserFunctions', 'displaytitle' ), SFH_NO_HASH );
+               }
+               if ( $wgAllowSlowParserFunctions ) {
+                       $this->setFunctionHook( 'pagesinnamespace', array( 'CoreParserFunctions', 'pagesinnamespace' ), SFH_NO_HASH );
+               }
+               
+               $this->initialiseVariables();
+
+               $this->mFirstCall = false;
+               wfProfileOut( __METHOD__ );
+       }               
+
        /**
         * Clear Parser state
         *
         * @private
         */
        function clearState() {
+               if ( $this->mFirstCall ) {
+                       $this->firstCallInit();
+               }
                $this->mOutput = new ParserOutput;
                $this->mAutonumber = 0;
                $this->mLastSection = '';
                $this->mDTopen = false;
-               $this->mVariables = false;
                $this->mIncludeCount = array();
                $this->mStripState = array();
                $this->mArgStack = array();
@@ -167,7 +226,7 @@ class Parser
 
                $this->mShowToc = true;
                $this->mForceTocPosition = false;
-               
+
                wfRunHooks( 'ParserClearState', array( &$this ) );
        }
 
@@ -209,7 +268,10 @@ class Parser
 
                $this->mOptions = $options;
                $this->mTitle =& $title;
-               $this->mRevisionId = $revid;
+               $oldRevisionId = $this->mRevisionId;
+               if( $revid !== null ) {
+                       $this->mRevisionId = $revid;
+               }
                $this->mOutputType = OT_HTML;
 
                //$text = $this->strip( $text, $this->mStripState );
@@ -237,7 +299,6 @@ class Parser
                        '/(.) (?=\\?|:|;|!|\\302\\273)/' => '\\1&nbsp;\\2',
                        # french spaces, Guillemet-right
                        '/(\\302\\253) /' => '\\1&nbsp;',
-                       '/<center *>(.*)<\\/center *>/i' => '<div class="center">\\1</div>',
                );
                $text = preg_replace( array_keys($fixtags), array_values($fixtags), $text );
 
@@ -263,8 +324,8 @@ class Parser
                } else {
                        # attempt to sanitize at least some nesting problems
                        # (bug #2702 and quite a few others)
-                       $tidyregs = array(      
-                               # ''Something [http://www.cool.com cool''] --> 
+                       $tidyregs = array(
+                               # ''Something [http://www.cool.com cool''] -->
                                # <i>Something</i><a href="http://www.cool.com"..><i>cool></i></a>
                                '/(<([bi])>)(<([bi])>)?([^<]*)(<\/?a[^<]*>)([^<]*)(<\/\\4>)?(<\/\\2>)/' =>
                                '\\1\\3\\5\\8\\9\\6\\1\\3\\7\\8\\9',
@@ -279,10 +340,10 @@ class Parser
                                '\\1\\3&lt;div\\5&gt;\\6&lt;/div&gt;\\8\\9',
                                # remove empty italic or bold tag pairs, some
                                # introduced by rules above
-                               '/<([bi])><\/\\1>/' => '' 
+                               '/<([bi])><\/\\1>/' => '',
                        );
 
-                       $text = preg_replace( 
+                       $text = preg_replace(
                                array_keys( $tidyregs ),
                                array_values( $tidyregs ),
                                $text );
@@ -291,6 +352,7 @@ class Parser
                wfRunHooks( 'ParserAfterTidy', array( &$this, &$text ) );
 
                $this->mOutput->setText( $text );
+               $this->mRevisionId = $oldRevisionId;
                wfProfileOut( $fname );
 
                return $this->mOutput;
@@ -309,100 +371,92 @@ class Parser
        function &getTitle() { return $this->mTitle; }
        function getOptions() { return $this->mOptions; }
 
+       function getFunctionLang() {
+               global $wgLang, $wgContLang;
+               return $this->mOptions->getInterfaceMessage() ? $wgLang : $wgContLang;
+       }
+
        /**
-        * 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.
+        * Replaces all occurrences of HTML-style comments and the given tags
+        * in the text with a random marker and returns teh next text. The output
+        * parameter $matches will be an associative array filled with data in
+        * the form:
+        *   'UNIQ-xxxxx' => array(
+        *     'element',
+        *     'tag content',
+        *     array( 'param' => 'x' ),
+        *     '<element param="x">tag content</element>' ) )
         *
-        * If $content is already set, the additional entries will be appended
-        * If $tag is set to STRIP_COMMENTS, the function will extract
-        * <!-- HTML comments -->
+        * @param $elements list of element names. Comments are always extracted.
+        * @param $text Source text string.
+        * @param $uniq_prefix
         *
         * @private
         * @static
         */
-       function extractTagsAndParams($tag, $text, &$content, &$tags, &$params, $uniq_prefix = ''){
-               $rnd = $uniq_prefix . '-' . $tag . Parser::getRandomString();
-               if ( !$content ) {
-                       $content = array( );
-               }
+       function extractTagsAndParams($elements, $text, &$matches, $uniq_prefix = ''){
+               $rand = Parser::getRandomString();
                $n = 1;
                $stripped = '';
+               $matches = array();
 
-               if ( !$tags ) {
-                       $tags = array( );
-               }
-
-               if ( !$params ) {
-                       $params = array( );
-               }
-
-               if( $tag == STRIP_COMMENTS ) {
-                       $start = '/<!--()/';
-                       $end   = '/-->/';
-               } else {
-                       $start = "/<$tag(\\s+[^>]*|\\s*\/?)>/i";
-                       $end   = "/<\\/$tag\\s*>/i";
-               }
+               $taglist = implode( '|', $elements );
+               $start = "/<($taglist)(\\s+[^>]*?|\\s*?)(\/?>)|<(!--)/i";
 
                while ( '' != $text ) {
                        $p = preg_split( $start, $text, 2, PREG_SPLIT_DELIM_CAPTURE );
                        $stripped .= $p[0];
-                       if( count( $p ) < 3 ) {
+                       if( count( $p ) < 5 ) {
                                break;
                        }
-                       $attributes = $p[1];
-                       $inside     = $p[2];
-
-                       // If $attributes ends with '/', we have an empty element tag, <tag />
-                       if( $tag != STRIP_COMMENTS && substr( $attributes, -1 ) == '/' ) {
-                               $attributes = substr( $attributes, 0, -1);
-                               $empty = '/';
+                       if( count( $p ) > 5 ) {
+                               // comment
+                               $element    = $p[4];
+                               $attributes = '';
+                               $close      = '';
+                               $inside     = $p[5];
                        } else {
-                               $empty = '';
+                               // tag
+                               $element    = $p[1];
+                               $attributes = $p[2];
+                               $close      = $p[3];
+                               $inside     = $p[4];
                        }
 
-                       $marker = $rnd . sprintf('%08X', $n++);
+                       $marker = "$uniq_prefix-$element-$rand" . sprintf('%08X', $n++) . '-QINU';
                        $stripped .= $marker;
 
-                       $tags[$marker] = "<$tag$attributes$empty>";
-                       $params[$marker] = Sanitizer::decodeTagAttributes( $attributes );
-
-                       if ( $empty === '/' ) {
+                       if ( $close === '/>' ) {
                                // Empty element tag, <tag />
-                               $content[$marker] = null;
+                               $content = null;
                                $text = $inside;
+                               $tail = null;
                        } else {
-                               $q = preg_split( $end, $inside, 2 );
-                               $content[$marker] = $q[0];
-                               if( count( $q ) < 2 ) {
+                               if( $element == '!--' ) {
+                                       $end = '/(-->)/';
+                               } else {
+                                       $end = "/(<\\/$element\\s*>)/i";
+                               }
+                               $q = preg_split( $end, $inside, 2, PREG_SPLIT_DELIM_CAPTURE );
+                               $content = $q[0];
+                               if( count( $q ) < 3 ) {
                                        # No end tag -- let it run out to the end of the text.
-                                       break;
+                                       $tail = '';
+                                       $text = '';
                                } else {
-                                       $text = $q[1];
+                                       $tail = $q[1];
+                                       $text = $q[2];
                                }
                        }
+                       
+                       $matches[$marker] = array( $element,
+                               $content,
+                               Sanitizer::decodeTagAttributes( $attributes ),
+                               "<$element$attributes$close$content$tail" );
                }
                return $stripped;
        }
 
-       /**
-        * Wrapper function for extractTagsAndParams
-        * for cases where $tags and $params isn't needed
-        * i.e. where tags will never have params, like <nowiki>
-        *
-        * @private
-        * @static
-        */
-       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
@@ -413,106 +467,86 @@ class Parser
         *  will be stripped in addition to other tags. This is important
         *  for section editing, where these comments cause confusion when
         *  counting the sections in the wikisource
+        * 
+        * @param array dontstrip contains tags which should not be stripped;
+        *  used to prevent stipping of <gallery> when saving (fixes bug 2700)
         *
         * @private
         */
-       function strip( $text, &$state, $stripcomments = false ) {
+       function strip( $text, &$state, $stripcomments = false , $dontstrip = array () ) {
                $render = ($this->mOutputType == OT_HTML);
-               $html_content = array();
-               $nowiki_content = array();
-               $math_content = array();
-               $pre_content = array();
-               $comment_content = array();
-               $ext_content = array();
-               $ext_tags = array();
-               $ext_params = array();
-               $gallery_content = array();
 
                # Replace any instances of the placeholders
                $uniq_prefix = $this->mUniqPrefix;
                #$text = str_replace( $uniq_prefix, wfHtmlEscapeFirst( $uniq_prefix ), $text );
-
-               # html
+               $commentState = array();
+               
+               $elements = array_merge(
+                       array( 'nowiki', 'gallery' ),
+                       array_keys( $this->mTagHooks ) );
                global $wgRawHtml;
                if( $wgRawHtml ) {
-                       $text = Parser::extractTags('html', $text, $html_content, $uniq_prefix);
-                       foreach( $html_content as $marker => $content ) {
-                               if ($render ) {
-                                       # Raw and unchecked for validity.
-                                       $html_content[$marker] = $content;
-                               } else {
-                                       $html_content[$marker] = '<html>'.$content.'</html>';
-                               }
-                       }
-               }
-
-               # nowiki
-               $text = Parser::extractTags('nowiki', $text, $nowiki_content, $uniq_prefix);
-               foreach( $nowiki_content as $marker => $content ) {
-                       if( $render ){
-                               $nowiki_content[$marker] = wfEscapeHTMLTagsOnly( $content );
-                       } else {
-                               $nowiki_content[$marker] = '<nowiki>'.$content.'</nowiki>';
-                       }
+                       $elements[] = 'html';
                }
-
-               # math
                if( $this->mOptions->getUseTeX() ) {
-                       $text = Parser::extractTags('math', $text, $math_content, $uniq_prefix);
-                       foreach( $math_content as $marker => $content ){
-                               if( $render ) {
-                                       $math_content[$marker] = renderMath( $content );
-                               } else {
-                                       $math_content[$marker] = '<math>'.$content.'</math>';
-                               }
-                       }
+                       $elements[] = 'math';
                }
-
-               # pre
-               $text = Parser::extractTags('pre', $text, $pre_content, $uniq_prefix);
-               foreach( $pre_content as $marker => $content ){
-                       if( $render ){
-                               $pre_content[$marker] = '<pre>' . wfEscapeHTMLTagsOnly( $content ) . '</pre>';
-                       } else {
-                               $pre_content[$marker] = '<pre>'.$content.'</pre>';
-                       }
-               }
-
-               # gallery
-               $text = Parser::extractTags('gallery', $text, $gallery_content, $uniq_prefix);
-               foreach( $gallery_content as $marker => $content ) {
-                       require_once( 'ImageGallery.php' );
-                       if ( $render ) {
-                               $gallery_content[$marker] = $this->renderImageGallery( $content );
-                       } else {
-                               $gallery_content[$marker] = '<gallery>'.$content.'</gallery>';
-                       }
-               }
-
-               # Comments
-               $text = Parser::extractTags(STRIP_COMMENTS, $text, $comment_content, $uniq_prefix);
-               foreach( $comment_content as $marker => $content ){
-                       $comment_content[$marker] = '<!--'.$content.'-->';
+               
+               # Removing $dontstrip tags from $elements list (currently only 'gallery', fixing bug 2700)
+               foreach ( $elements AS $k => $v ) {
+                       if ( !in_array ( $v , $dontstrip ) ) continue;
+                       unset ( $elements[$k] );
                }
-
-               # Extensions
-               foreach ( $this->mTagHooks as $tag => $callback ) {
-                       $ext_content[$tag] = array();
-                       $text = Parser::extractTagsAndParams( $tag, $text, $ext_content[$tag],
-                               $ext_tags[$tag], $ext_params[$tag], $uniq_prefix );
-                       foreach( $ext_content[$tag] as $marker => $content ) {
-                               $full_tag = $ext_tags[$tag][$marker];
-                               $params = $ext_params[$tag][$marker];
-                               if ( $render )
-                                       $ext_content[$tag][$marker] = call_user_func_array( $callback, array( $content, $params, &$this ) );
-                               else {
-                                       if ( is_null( $content ) ) {
-                                               // Empty element tag
-                                               $ext_content[$tag][$marker] = $full_tag;
+               
+               $matches = array();
+               $text = Parser::extractTagsAndParams( $elements, $text, $matches, $uniq_prefix );
+
+               foreach( $matches as $marker => $data ) {
+                       list( $element, $content, $params, $tag ) = $data;
+                       if( $render ) {
+                               $tagName = strtolower( $element );
+                               switch( $tagName ) {
+                               case '!--':
+                                       // Comment
+                                       if( substr( $tag, -3 ) == '-->' ) {
+                                               $output = $tag;
                                        } else {
-                                               $ext_content[$tag][$marker] = "$full_tag$content</$tag>";
+                                               // Unclosed comment in input.
+                                               // Close it so later stripping can remove it
+                                               $output = "$tag-->";
+                                       }
+                                       break;
+                               case 'html':
+                                       if( $wgRawHtml ) {
+                                               $output = $content;
+                                               break;
+                                       }
+                                       // Shouldn't happen otherwise. :)
+                               case 'nowiki':
+                                       $output = wfEscapeHTMLTagsOnly( $content );
+                                       break;
+                               case 'math':
+                                       $output = MathRenderer::renderMath( $content );
+                                       break;
+                               case 'gallery':
+                                       $output = $this->renderImageGallery( $content, $params );
+                                       break;
+                               default:
+                                       if( isset( $this->mTagHooks[$tagName] ) ) {
+                                               $output = call_user_func_array( $this->mTagHooks[$tagName],
+                                                       array( $content, $params, $this ) );
+                                       } else {
+                                               throw new MWException( "Invalid call hook $element" );
                                        }
                                }
+                       } else {
+                               // Just stripping tags; keep the source
+                               $output = $tag;
+                       }
+                       if( !$stripcomments && $element == '!--' ) {
+                               $commentState[$marker] = $output;
+                       } else {
+                               $state[$element][$marker] = $output;
                        }
                }
 
@@ -521,40 +555,15 @@ class Parser
                # not invoke any extension tags / parser hooks contained within
                # a comment.)
                if ( !$stripcomments ) {
-                       $tempstate = array( 'comment' => $comment_content );
-                       $text = $this->unstrip( $text, $tempstate );
-                       $comment_content = array();
+                       // Put them all back and forget them
+                       $text = strtr( $text, $commentState );
                }
 
-               # Merge state with the pre-existing state, if there is one
-               if ( $state ) {
-                       $state['html'] = $state['html'] + $html_content;
-                       $state['nowiki'] = $state['nowiki'] + $nowiki_content;
-                       $state['math'] = $state['math'] + $math_content;
-                       $state['pre'] = $state['pre'] + $pre_content;
-                       $state['gallery'] = $state['gallery'] + $gallery_content;
-                       $state['comment'] = $state['comment'] + $comment_content;
-
-                       foreach( $ext_content as $tag => $array ) {
-                               if ( array_key_exists( $tag, $state ) ) {
-                                       $state[$tag] = $state[$tag] + $array;
-                               }
-                       }
-               } else {
-                       $state = array(
-                         'html' => $html_content,
-                         'nowiki' => $nowiki_content,
-                         'math' => $math_content,
-                         'pre' => $pre_content,
-                         'gallery' => $gallery_content,
-                         'comment' => $comment_content,
-                       ) + $ext_content;
-               }
                return $text;
        }
 
        /**
-        * restores pre, math, and hiero removed by strip()
+        * Restores pre, math, and other extensions removed by strip()
         *
         * always call unstripNoWiki() after this one
         * @private
@@ -564,20 +573,21 @@ class Parser
                        return $text;
                }
 
-               # Must expand in reverse order, otherwise nested tags will be corrupted
-               foreach( array_reverse( $state, true ) as $tag => $contentDict ) {
+               $replacements = array();
+               foreach( $state as $tag => $contentDict ) {
                        if( $tag != 'nowiki' && $tag != 'html' ) {
-                               foreach( array_reverse( $contentDict, true ) as $uniq => $content ) {
-                                       $text = str_replace( $uniq, $content, $text );
+                               foreach( $contentDict as $uniq => $content ) {
+                                       $replacements[$uniq] = $content;
                                }
                        }
                }
+               $text = strtr( $text, $replacements );
 
                return $text;
        }
 
        /**
-        * always call this after unstrip() to preserve the order
+        * Always call this after unstrip() to preserve the order
         *
         * @private
         */
@@ -586,17 +596,15 @@ class Parser
                        return $text;
                }
 
-               # Must expand in reverse order, otherwise nested tags will be corrupted
-               foreach( array_reverse( $state['nowiki'], true ) as $uniq => $content ) {
-                       $text = str_replace( $uniq, $content, $text );
-               }
-
-               global $wgRawHtml;
-               if ($wgRawHtml) {
-                       foreach( array_reverse( $state['html'], true ) as $uniq => $content ) {
-                               $text = str_replace( $uniq, $content, $text );
+               $replacements = array();
+               foreach( $state as $tag => $contentDict ) {
+                       if( $tag == 'nowiki' || $tag == 'html' ) {
+                               foreach( $contentDict as $uniq => $content ) {
+                                       $replacements[$uniq] = $content;
+                               }
                        }
                }
+               $text = strtr( $text, $replacements );
 
                return $text;
        }
@@ -611,14 +619,7 @@ class Parser
        function insertStripItem( $text, &$state ) {
                $rnd = $this->mUniqPrefix . '-item' . Parser::getRandomString();
                if ( !$state ) {
-                       $state = array(
-                         'html' => array(),
-                         'nowiki' => array(),
-                         'math' => array(),
-                         'pre' => array(),
-                         'comment' => array(),
-                         'gallery' => array(),
-                       );
+                       $state = array();
                }
                $state['item'][$rnd] = $text;
                return $rnd;
@@ -800,13 +801,13 @@ class Parser
                                }
                                $after = substr ( $x , 1 ) ;
                                if ( $fc == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
-                               
+
                                // Split up multiple cells on the same line.
                                // FIXME: This can result in improper nesting of tags processed
                                // by earlier parser steps, but should avoid splitting up eg
                                // attribute values containing literal "||".
                                $after = wfExplodeMarkup( '||', $after );
-                               
+
                                $t[$k] = '' ;
 
                                # Loop through each table cell
@@ -884,7 +885,7 @@ class Parser
                $text = strtr( $text, array( '<onlyinclude>' => '' , '</onlyinclude>' => '' ) );
                $text = strtr( $text, array( '<noinclude>' => '', '</noinclude>' => '') );
                $text = preg_replace( '/<includeonly>.*?<\/includeonly>/s', '', $text );
-               
+
                $text = Sanitizer::removeHTMLtags( $text, array( &$this, 'attributeStripCallback' ) );
 
                $text = $this->replaceVariables( $text, $args );
@@ -898,6 +899,7 @@ class Parser
                $text = preg_replace( '/(^|\n)-----*/', '\\1<hr />', $text );
 
                $text = $this->stripToc( $text );
+               $this->stripNoGallery( $text );
                $text = $this->doHeadings( $text );
                if($this->mOptions->getUseDynamicDates()) {
                        $df =& DateFormatter::getInstance();
@@ -941,7 +943,7 @@ class Parser
                wfProfileIn( $fname );
                for ( $i = 6; $i >= 1; --$i ) {
                        $h = str_repeat( '=', $i );
-                       $text = preg_replace( "/^{$h}(.+){$h}(\\s|$)/m",
+                       $text = preg_replace( "/^{$h}(.+){$h}\\s*$/m",
                          "<h{$i}>\\1</h{$i}>\\2", $text );
                }
                wfProfileOut( $fname );
@@ -1195,10 +1197,8 @@ class Parser
                        }
 
                        $text = $wgContLang->markNoConversion($text);
-
-                       # Normalize any HTML entities in input. They will be
-                       # re-escaped by makeExternalLink().
-                       $url = Sanitizer::decodeCharReferences( $url );
+                       
+                       $url = Sanitizer::cleanUrl( $url );
 
                        # Process the trail (i.e. everything after this link up until start of the next link),
                        # replacing any non-bracketed links
@@ -1279,9 +1279,7 @@ class Parser
                                        $url = substr( $url, 0, -$numSepChars );
                                }
 
-                               # Normalize any HTML entities in input. They will be
-                               # re-escaped by makeExternalLink() or maybeMakeExternalImage()
-                               $url = Sanitizer::decodeCharReferences( $url );
+                               $url = Sanitizer::cleanUrl( $url );
 
                                # Is this an external image?
                                $text = $this->maybeMakeExternalImage( $url );
@@ -1312,7 +1310,7 @@ class Parser
         *        the URL differently; as a workaround, just use the output for
         *        statistical records, not for actual linking/output.
         */
-       function replaceUnusualEscapes( $url ) {
+       static function replaceUnusualEscapes( $url ) {
                return preg_replace_callback( '/%[0-9A-Fa-f]{2}/',
                        array( 'Parser', 'replaceUnusualEscapesCallback' ), $url );
        }
@@ -1323,7 +1321,7 @@ class Parser
         * @static
         * @private
         */
-       function replaceUnusualEscapesCallback( $matches ) {
+       private static function replaceUnusualEscapesCallback( $matches ) {
                $char = urldecode( $matches[0] );
                $ord = ord( $char );
                // Is it an unsafe or HTTP reserved character according to RFC 1738?
@@ -1393,7 +1391,7 @@ class Parser
                $useLinkPrefixExtension = $wgContLang->linkPrefixExtension();
 
                if( is_null( $this->mTitle ) ) {
-                       wfDebugDieBacktrace( 'nooo' );
+                       throw new MWException( 'nooo' );
                }
                $nottalk = !$this->mTitle->isTalkPage();
 
@@ -1453,7 +1451,7 @@ class Parser
                                        $m[3] = $n[1];
                                }
                                # fix up urlencoded title texts
-                               if(preg_match('/%/', $m[1] )) 
+                               if(preg_match('/%/', $m[1] ))
                                        # Should anchors '#' also be rejected?
                                        $m[1] = str_replace( array('<', '>'), array('&lt;', '&gt;'), urldecode($m[1]) );
                                $trail = $m[3];
@@ -1571,6 +1569,9 @@ class Parser
 
                                                wfProfileOut( "$fname-image" );
                                                continue;
+                                       } else {
+                                               # We still need to record the image's presence on the page
+                                               $this->mOutput->addImage( $nt->getDBkey() );
                                        }
                                        wfProfileOut( "$fname-image" );
 
@@ -1590,6 +1591,7 @@ class Parser
                                                $sortkey = $text;
                                        }
                                        $sortkey = Sanitizer::decodeCharReferences( $sortkey );
+                                       $sortkey = str_replace( "\n", '', $sortkey );
                                        $sortkey = $wgContLang->convertCategoryKey( $sortkey );
                                        $this->mOutput->addCategory( $nt->getDBkey(), $sortkey );
 
@@ -1622,7 +1624,7 @@ class Parser
                                $s .= $this->makeKnownLinkHolder( $nt, $text, '', $trail, $prefix );
                                continue;
                        } elseif( $ns == NS_IMAGE ) {
-                               $img = Image::newFromTitle( $nt );
+                               $img = new Image( $nt );
                                if( $img->exists() ) {
                                        // Force a blue link if the file exists; may be a remote
                                        // upload on the shared repository, and we want to see its
@@ -1954,10 +1956,10 @@ class Parser
                                wfProfileIn( "$fname-paragraph" );
                                # No prefix (not in list)--go to paragraph mode
                                // XXX: use a stack for nestable elements like span, table and div
-                               $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<li|<\\/tr|<\\/td|<\\/th)/iS', $t );
+                               $openmatch = preg_match('/(<table|<blockquote|<h1|<h2|<h3|<h4|<h5|<h6|<pre|<tr|<p|<ul|<ol|<li|<\\/center|<\\/tr|<\\/td|<\\/th)/iS', $t );
                                $closematch = preg_match(
                                        '/(<\\/table|<\\/blockquote|<\\/h1|<\\/h2|<\\/h3|<\\/h4|<\\/h5|<\\/h6|'.
-                                       '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$this->mUniqPrefix.'-pre|<\\/li|<\\/ul)/iS', $t );
+                                       '<td|<th|<div|<\\/div|<hr|<\\/pre|<\\/p|'.$this->mUniqPrefix.'-pre|<\\/li|<\\/ul|<\\/ol|<center)/iS', $t );
                                if ( $openmatch or $closematch ) {
                                        $paragraphStack = false;
                                        # TODO bug 5718: paragraph closed
@@ -2031,43 +2033,167 @@ class Parser
        }
 
        /**
-        * Split up a string on ':', ignoring any occurences inside
-        * <a>..</a> or <span>...</span>
+        * Split up a string on ':', ignoring any occurences inside tags
+        * to prevent illegal overlapping.
         * @param string $str the string to split
         * @param string &$before set to everything before the ':'
         * @param string &$after set to everything after the ':'
         * return string the position of the ':', or false if none found
         */
        function findColonNoLinks($str, &$before, &$after) {
-               # I wonder if we should make this count all tags, not just <a>
-               # and <span>. That would prevent us from matching a ':' that
-               # comes in the middle of italics other such formatting....
-               # -- Wil
                $fname = 'Parser::findColonNoLinks';
                wfProfileIn( $fname );
-               $pos = 0;
-               do {
-                       $colon = strpos($str, ':', $pos);
-
-                       if ($colon !== false) {
-                               $before = substr($str, 0, $colon);
-                               $after = substr($str, $colon + 1);
-
-                               # Skip any ':' within <a> or <span> pairs
-                               $a = substr_count($before, '<a');
-                               $s = substr_count($before, '<span');
-                               $ca = substr_count($before, '</a>');
-                               $cs = substr_count($before, '</span>');
-
-                               if ($a <= $ca and $s <= $cs) {
-                                       # Tags are balanced before ':'; ok
+
+               $pos = strpos( $str, ':' );
+               if( $pos === false ) {
+                       // Nothing to find!
+                       wfProfileOut( $fname );
+                       return false;
+               }
+
+               $lt = strpos( $str, '<' );
+               if( $lt === false || $lt > $pos ) {
+                       // Easy; no tag nesting to worry about
+                       $before = substr( $str, 0, $pos );
+                       $after = substr( $str, $pos+1 );
+                       wfProfileOut( $fname );
+                       return $pos;
+               }
+
+               // Ugly state machine to walk through avoiding tags.
+               $state = MW_COLON_STATE_TEXT;
+               $stack = 0;
+               $len = strlen( $str );
+               for( $i = 0; $i < $len; $i++ ) {
+                       $c = $str{$i};
+
+                       switch( $state ) {
+                       // (Using the number is a performance hack for common cases)
+                       case 0: // MW_COLON_STATE_TEXT:
+                               switch( $c ) {
+                               case "<":
+                                       // Could be either a <start> tag or an </end> tag
+                                       $state = MW_COLON_STATE_TAGSTART;
+                                       break;
+                               case ":":
+                                       if( $stack == 0 ) {
+                                               // We found it!
+                                               $before = substr( $str, 0, $i );
+                                               $after = substr( $str, $i + 1 );
+                                               wfProfileOut( $fname );
+                                               return $i;
+                                       }
+                                       // Embedded in a tag; don't break it.
+                                       break;
+                               default:
+                                       // Skip ahead looking for something interesting
+                                       $colon = strpos( $str, ':', $i );
+                                       if( $colon === false ) {
+                                               // Nothing else interesting
+                                               wfProfileOut( $fname );
+                                               return false;
+                                       }
+                                       $lt = strpos( $str, '<', $i );
+                                       if( $stack === 0 ) {
+                                               if( $lt === false || $colon < $lt ) {
+                                                       // We found it!
+                                                       $before = substr( $str, 0, $colon );
+                                                       $after = substr( $str, $colon + 1 );
+                                                       wfProfileOut( $fname );
+                                                       return $i;
+                                               }
+                                       }
+                                       if( $lt === false ) {
+                                               // Nothing else interesting to find; abort!
+                                               // We're nested, but there's no close tags left. Abort!
+                                               break 2;
+                                       }
+                                       // Skip ahead to next tag start
+                                       $i = $lt;
+                                       $state = MW_COLON_STATE_TAGSTART;
+                               }
+                               break;
+                       case 1: // MW_COLON_STATE_TAG:
+                               // In a <tag>
+                               switch( $c ) {
+                               case ">":
+                                       $stack++;
+                                       $state = MW_COLON_STATE_TEXT;
+                                       break;
+                               case "/":
+                                       // Slash may be followed by >?
+                                       $state = MW_COLON_STATE_TAGSLASH;
+                                       break;
+                               default:
+                                       // ignore
+                               }
+                               break;
+                       case 2: // MW_COLON_STATE_TAGSTART:
+                               switch( $c ) {
+                               case "/":
+                                       $state = MW_COLON_STATE_CLOSETAG;
+                                       break;
+                               case "!":
+                                       $state = MW_COLON_STATE_COMMENT;
                                        break;
+                               case ">":
+                                       // Illegal early close? This shouldn't happen D:
+                                       $state = MW_COLON_STATE_TEXT;
+                                       break;
+                               default:
+                                       $state = MW_COLON_STATE_TAG;
+                               }
+                               break;
+                       case 3: // MW_COLON_STATE_CLOSETAG:
+                               // In a </tag>
+                               if( $c == ">" ) {
+                                       $stack--;
+                                       if( $stack < 0 ) {
+                                               wfDebug( "Invalid input in $fname; too many close tags\n" );
+                                               wfProfileOut( $fname );
+                                               return false;
+                                       }
+                                       $state = MW_COLON_STATE_TEXT;
+                               }
+                               break;
+                       case MW_COLON_STATE_TAGSLASH:
+                               if( $c == ">" ) {
+                                       // Yes, a self-closed tag <blah/>
+                                       $state = MW_COLON_STATE_TEXT;
+                               } else {
+                                       // Probably we're jumping the gun, and this is an attribute
+                                       $state = MW_COLON_STATE_TAG;
+                               }
+                               break;
+                       case 5: // MW_COLON_STATE_COMMENT:
+                               if( $c == "-" ) {
+                                       $state = MW_COLON_STATE_COMMENTDASH;
+                               }
+                               break;
+                       case MW_COLON_STATE_COMMENTDASH:
+                               if( $c == "-" ) {
+                                       $state = MW_COLON_STATE_COMMENTDASHDASH;
+                               } else {
+                                       $state = MW_COLON_STATE_COMMENT;
+                               }
+                               break;
+                       case MW_COLON_STATE_COMMENTDASHDASH:
+                               if( $c == ">" ) {
+                                       $state = MW_COLON_STATE_TEXT;
+                               } else {
+                                       $state = MW_COLON_STATE_COMMENT;
                                }
-                               $pos = $colon + 1;
+                               break;
+                       default:
+                               throw new MWException( "State machine error in $fname" );
                        }
-               } while ($colon !== false);
+               }
+               if( $stack > 0 ) {
+                       wfDebug( "Invalid input in $fname; not enough close tags (stack $stack, state $state)\n" );
+                       return false;
+               }
                wfProfileOut( $fname );
-               return $colon;
+               return false;
        }
 
        /**
@@ -2091,103 +2217,108 @@ class Parser
                wfRunHooks( 'ParserGetVariableValueTs', array( &$this, &$ts ) );
 
                switch ( $index ) {
-                       case MAG_CURRENTMONTH:
+                       case 'currentmonth':
                                return $varCache[$index] = $wgContLang->formatNum( date( 'm', $ts ) );
-                       case MAG_CURRENTMONTHNAME:
+                       case 'currentmonthname':
                                return $varCache[$index] = $wgContLang->getMonthName( date( 'n', $ts ) );
-                       case MAG_CURRENTMONTHNAMEGEN:
+                       case 'currentmonthnamegen':
                                return $varCache[$index] = $wgContLang->getMonthNameGen( date( 'n', $ts ) );
-                       case MAG_CURRENTMONTHABBREV:
+                       case 'currentmonthabbrev':
                                return $varCache[$index] = $wgContLang->getMonthAbbreviation( date( 'n', $ts ) );
-                       case MAG_CURRENTDAY:
+                       case 'currentday':
                                return $varCache[$index] = $wgContLang->formatNum( date( 'j', $ts ) );
-                       case MAG_CURRENTDAY2:
+                       case 'currentday2':
                                return $varCache[$index] = $wgContLang->formatNum( date( 'd', $ts ) );
-                       case MAG_PAGENAME:
+                       case 'pagename':
                                return $this->mTitle->getText();
-                       case MAG_PAGENAMEE:
+                       case 'pagenamee':
                                return $this->mTitle->getPartialURL();
-                       case MAG_FULLPAGENAME:
+                       case 'fullpagename':
                                return $this->mTitle->getPrefixedText();
-                       case MAG_FULLPAGENAMEE:
+                       case 'fullpagenamee':
                                return $this->mTitle->getPrefixedURL();
-                       case MAG_SUBPAGENAME:
+                       case 'subpagename':
                                return $this->mTitle->getSubpageText();
-                       case MAG_SUBPAGENAMEE:
+                       case 'subpagenamee':
                                return $this->mTitle->getSubpageUrlForm();
-                       case MAG_BASEPAGENAME:
+                       case 'basepagename':
                                return $this->mTitle->getBaseText();
-                       case MAG_BASEPAGENAMEE:
+                       case 'basepagenamee':
                                return wfUrlEncode( str_replace( ' ', '_', $this->mTitle->getBaseText() ) );
-                       case MAG_TALKPAGENAME:
+                       case 'talkpagename':
                                if( $this->mTitle->canTalk() ) {
                                        $talkPage = $this->mTitle->getTalkPage();
                                        return $talkPage->getPrefixedText();
                                } else {
                                        return '';
                                }
-                       case MAG_TALKPAGENAMEE:
+                       case 'talkpagenamee':
                                if( $this->mTitle->canTalk() ) {
                                        $talkPage = $this->mTitle->getTalkPage();
                                        return $talkPage->getPrefixedUrl();
                                } else {
                                        return '';
                                }
-                       case MAG_SUBJECTPAGENAME:
+                       case 'subjectpagename':
                                $subjPage = $this->mTitle->getSubjectPage();
                                return $subjPage->getPrefixedText();
-                       case MAG_SUBJECTPAGENAMEE:
+                       case 'subjectpagenamee':
                                $subjPage = $this->mTitle->getSubjectPage();
                                return $subjPage->getPrefixedUrl();
-                       case MAG_REVISIONID:
+                       case 'revisionid':
                                return $this->mRevisionId;
-                       case MAG_NAMESPACE:
+                       case 'namespace':
                                return str_replace('_',' ',$wgContLang->getNsText( $this->mTitle->getNamespace() ) );
-                       case MAG_NAMESPACEE:
+                       case 'namespacee':
                                return wfUrlencode( $wgContLang->getNsText( $this->mTitle->getNamespace() ) );
-                       case MAG_TALKSPACE:
+                       case 'talkspace':
                                return $this->mTitle->canTalk() ? str_replace('_',' ',$this->mTitle->getTalkNsText()) : '';
-                       case MAG_TALKSPACEE:
+                       case 'talkspacee':
                                return $this->mTitle->canTalk() ? wfUrlencode( $this->mTitle->getTalkNsText() ) : '';
-                       case MAG_SUBJECTSPACE:
+                       case 'subjectspace':
                                return $this->mTitle->getSubjectNsText();
-                       case MAG_SUBJECTSPACEE:
+                       case 'subjectspacee':
                                return( wfUrlencode( $this->mTitle->getSubjectNsText() ) );
-                       case MAG_CURRENTDAYNAME:
+                       case 'currentdayname':
                                return $varCache[$index] = $wgContLang->getWeekdayName( date( 'w', $ts ) + 1 );
-                       case MAG_CURRENTYEAR:
+                       case 'currentyear':
                                return $varCache[$index] = $wgContLang->formatNum( date( 'Y', $ts ), true );
-                       case MAG_CURRENTTIME:
+                       case 'currenttime':
                                return $varCache[$index] = $wgContLang->time( wfTimestamp( TS_MW, $ts ), false, false );
-                       case MAG_CURRENTWEEK:
+                       case 'currentweek':
                                // @bug 4594 PHP5 has it zero padded, PHP4 does not, cast to
                                // int to remove the padding
                                return $varCache[$index] = $wgContLang->formatNum( (int)date( 'W', $ts ) );
-                       case MAG_CURRENTDOW:
+                       case 'currentdow':
                                return $varCache[$index] = $wgContLang->formatNum( date( 'w', $ts ) );
-                       case MAG_NUMBEROFARTICLES:
+                       case 'numberofarticles':
                                return $varCache[$index] = $wgContLang->formatNum( wfNumberOfArticles() );
-                       case MAG_NUMBEROFFILES:
+                       case 'numberoffiles':
                                return $varCache[$index] = $wgContLang->formatNum( wfNumberOfFiles() );
-                       case MAG_NUMBEROFUSERS:
+                       case 'numberofusers':
                                return $varCache[$index] = $wgContLang->formatNum( wfNumberOfUsers() );
-                       case MAG_NUMBEROFPAGES:
+                       case 'numberofpages':
                                return $varCache[$index] = $wgContLang->formatNum( wfNumberOfPages() );
-                       case MAG_CURRENTTIMESTAMP:
+                       case 'numberofadmins':
+                               return $varCache[$index]  = $wgContLang->formatNum( wfNumberOfAdmins() );
+                       case 'currenttimestamp':
                                return $varCache[$index] = wfTimestampNow();
-                       case MAG_CURRENTVERSION:
+                       case 'currentversion':
                                global $wgVersion;
                                return $wgVersion;
-                       case MAG_SITENAME:
+                       case 'sitename':
                                return $wgSitename;
-                       case MAG_SERVER:
+                       case 'server':
                                return $wgServer;
-                       case MAG_SERVERNAME:
+                       case 'servername':
                                return $wgServerName;
-                       case MAG_SCRIPTPATH:
+                       case 'scriptpath':
                                return $wgScriptPath;
-                       case MAG_DIRECTIONMARK:
+                       case 'directionmark':
                                return $wgContLang->getDirMark();
+                       case 'contentlanguage':
+                               global $wgContLanguageCode;
+                               return $wgContLanguageCode;
                        default:
                                $ret = null;
                                if ( wfRunHooks( 'ParserGetVariableValueSwitch', array( &$this, &$varCache, &$index, &$ret ) ) )
@@ -2205,9 +2336,10 @@ class Parser
        function initialiseVariables() {
                $fname = 'Parser::initialiseVariables';
                wfProfileIn( $fname );
-               global $wgVariableIDs;
+               $variableIDs = MagicWord::getVariableIDs();
+
                $this->mVariables = array();
-               foreach ( $wgVariableIDs as $id ) {
+               foreach ( $variableIDs as $id ) {
                        $mw =& MagicWord::get( $id );
                        $mw->addToArray( $this->mVariables, $id );
                }
@@ -2229,6 +2361,7 @@ class Parser
         * @private
         */
        function replace_callback ($text, $callbacks) {
+               wfProfileIn( __METHOD__ . '-self' );
                $openingBraceStack = array();   # this array will hold a stack of parentheses which are not closed yet
                $lastOpeningBrace = -1;                 # last not closed parentheses
 
@@ -2334,10 +2467,12 @@ class Parser
                                                                                 'text' => substr($text, $pieceStart, $pieceEnd - $pieceStart),
                                                                                 'title' => trim($openingBraceStack[$lastOpeningBrace]['title']),
                                                                                 'parts' => $openingBraceStack[$lastOpeningBrace]['parts'],
-                                                                                'lineStart' => (($pieceStart > 0) && ($text[$pieceStart-1] == '\n')),
+                                                                                'lineStart' => (($pieceStart > 0) && ($text[$pieceStart-1] == "\n")),
                                                                                 );
                                                # finally we can call a user callback and replace piece of text
+                                               wfProfileOut( __METHOD__ . '-self' );
                                                $replaceWith = call_user_func( $matchingCallback, $cbArgs );
+                                               wfProfileIn( __METHOD__ . '-self' );
                                                $text = substr($text, 0, $pieceStart) . $replaceWith . substr($text, $pieceEnd);
                                                $i = $pieceStart + strlen($replaceWith) - 1;
                                        }
@@ -2385,6 +2520,7 @@ class Parser
                        }
                }
 
+               wfProfileOut( __METHOD__ . '-self' );
                return $text;
        }
 
@@ -2432,7 +2568,7 @@ class Parser
                wfProfileOut( $fname );
                return $text;
        }
-       
+
        /**
         * Replace magic variables
         * @private
@@ -2441,13 +2577,10 @@ class Parser
                $fname = 'Parser::variableSubstitution';
                $varname = $matches[1];
                wfProfileIn( $fname );
-               if ( !$this->mVariables ) {
-                       $this->initialiseVariables();
-               }
                $skip = false;
                if ( $this->mOutputType == OT_WIKI ) {
                        # Do only magic variables prefixed by SUBST
-                       $mwSubst =& MagicWord::get( MAG_SUBST );
+                       $mwSubst =& MagicWord::get( 'subst' );
                        if (!$mwSubst->matchStartAndRemove( $varname ))
                                $skip = true;
                        # Note that if we don't substitute the variable below,
@@ -2541,7 +2674,7 @@ class Parser
 
                # SUBST
                if ( !$found ) {
-                       $mwSubst =& MagicWord::get( MAG_SUBST );
+                       $mwSubst =& MagicWord::get( 'subst' );
                        if ( $mwSubst->matchStartAndRemove( $part1 ) xor ($this->mOutputType == OT_WIKI) ) {
                                # One of two possibilities is true:
                                # 1) Found SUBST but not in the PST phase
@@ -2557,23 +2690,23 @@ class Parser
                # MSG, MSGNW, INT and RAW
                if ( !$found ) {
                        # Check for MSGNW:
-                       $mwMsgnw =& MagicWord::get( MAG_MSGNW );
+                       $mwMsgnw =& MagicWord::get( 'msgnw' );
                        if ( $mwMsgnw->matchStartAndRemove( $part1 ) ) {
                                $nowiki = true;
                        } else {
                                # Remove obsolete MSG:
-                               $mwMsg =& MagicWord::get( MAG_MSG );
+                               $mwMsg =& MagicWord::get( 'msg' );
                                $mwMsg->matchStartAndRemove( $part1 );
                        }
                        
                        # Check for RAW:
-                       $mwRaw =& MagicWord::get( MAG_RAW );
+                       $mwRaw =& MagicWord::get( 'raw' );
                        if ( $mwRaw->matchStartAndRemove( $part1 ) ) {
                                $forceRawInterwiki = true;
                        }
                        
                        # Check if it is an internal message
-                       $mwInt =& MagicWord::get( MAG_INT );
+                       $mwInt =& MagicWord::get( 'int' );
                        if ( $mwInt->matchStartAndRemove( $part1 ) ) {
                                if ( $this->incrementIncludeCount( 'int:'.$part1 ) ) {
                                        $text = $linestart . wfMsgReal( $part1, $args, true );
@@ -2582,161 +2715,26 @@ class Parser
                        }
                }
 
-               # NS
+               # Parser functions
                if ( !$found ) {
-                       # Check for NS: (namespace expansion)
-                       $mwNs = MagicWord::get( MAG_NS );
-                       if ( $mwNs->matchStartAndRemove( $part1 ) ) {
-                               if ( intval( $part1 ) || $part1 == "0" ) {
-                                       $text = $linestart . $wgContLang->getNsText( intval( $part1 ) );
-                                       $found = true;
+                       wfProfileIn( __METHOD__ . '-pfunc' );
+                       
+                       $colonPos = strpos( $part1, ':' );
+                       if ( $colonPos !== false ) {
+                               # Case sensitive functions
+                               $function = substr( $part1, 0, $colonPos );
+                               if ( isset( $this->mFunctionSynonyms[1][$function] ) ) {
+                                       $function = $this->mFunctionSynonyms[1][$function];
                                } else {
-                                       $index = Namespace::getCanonicalIndex( strtolower( $part1 ) );
-                                       if ( !is_null( $index ) ) {
-                                               $text = $linestart . $wgContLang->getNsText( $index );
-                                               $found = true;
-                                       }
-                               }
-                       }
-               }
-
-               # URLENCODE
-               if( !$found ) {
-                       $urlencode =& MagicWord::get( MAG_URLENCODE );
-                       if( $urlencode->matchStartAndRemove( $part1 ) ) {
-                               $text = $linestart . urlencode( $part1 );
-                               $found = true;
-                       }
-               }
-
-               # LCFIRST, UCFIRST, LC and UC
-               if ( !$found ) {
-                       $lcfirst =& MagicWord::get( MAG_LCFIRST );
-                       $ucfirst =& MagicWord::get( MAG_UCFIRST );
-                       $lc =& MagicWord::get( MAG_LC );
-                       $uc =& MagicWord::get( MAG_UC );
-                       if ( $lcfirst->matchStartAndRemove( $part1 ) ) {
-                               $text = $linestart . $wgContLang->lcfirst( $part1 );
-                               $found = true;
-                       } else if ( $ucfirst->matchStartAndRemove( $part1 ) ) {
-                               $text = $linestart . $wgContLang->ucfirst( $part1 );
-                               $found = true;
-                       } else if ( $lc->matchStartAndRemove( $part1 ) ) {
-                               $text = $linestart . $wgContLang->lc( $part1 );
-                               $found = true;
-                       } else if ( $uc->matchStartAndRemove( $part1 ) ) {
-                                $text = $linestart . $wgContLang->uc( $part1 );
-                                $found = true;
-                       }
-               }
-
-               # LOCALURL and FULLURL
-               if ( !$found ) {
-                       $mwLocal =& MagicWord::get( MAG_LOCALURL );
-                       $mwLocalE =& MagicWord::get( MAG_LOCALURLE );
-                       $mwFull =& MagicWord::get( MAG_FULLURL );
-                       $mwFullE =& MagicWord::get( MAG_FULLURLE );
-
-
-                       if ( $mwLocal->matchStartAndRemove( $part1 ) ) {
-                               $func = 'getLocalURL';
-                       } elseif ( $mwLocalE->matchStartAndRemove( $part1 ) ) {
-                               $func = 'escapeLocalURL';
-                       } elseif ( $mwFull->matchStartAndRemove( $part1 ) ) {
-                               $func = 'getFullURL';
-                       } elseif ( $mwFullE->matchStartAndRemove( $part1 ) ) {
-                               $func = 'escapeFullURL';
-                       } else {
-                               $func = false;
-                       }
-
-                       if ( $func !== false ) {
-                               $title = Title::newFromText( $part1 );
-                               # Due to order of execution of a lot of bits, the values might be encoded
-                               # before arriving here; if that's true, then the title can't be created
-                               # and the variable will fail. If we can't get a decent title from the first
-                               # attempt, url-decode and try for a second.
-                               if( is_null( $title ) )
-                                       $title = Title::newFromUrl( urldecode( $part1 ) );
-                               if ( !is_null( $title ) ) {
-                                       if ( $argc > 0 ) {
-                                               $text = $linestart . $title->$func( $args[0] );
+                                       # Case insensitive functions
+                                       $function = strtolower( $function );
+                                       if ( isset( $this->mFunctionSynonyms[0][$function] ) ) {
+                                               $function = $this->mFunctionSynonyms[0][$function];
                                        } else {
-                                               $text = $linestart . $title->$func();
+                                               $function = false;
                                        }
-                                       $found = true;
                                }
-                       }
-               }
-
-               $lang = $this->mOptions->getInterfaceMessage() ? $wgLang : $wgContLang;
-               # GRAMMAR
-               if ( !$found && $argc == 1 ) {
-                       $mwGrammar =& MagicWord::get( MAG_GRAMMAR );
-                       if ( $mwGrammar->matchStartAndRemove( $part1 ) ) {
-                               $text = $linestart . $lang->convertGrammar( $args[0], $part1 );
-                               $found = true;
-                       }
-               }
-
-               # PLURAL
-               if ( !$found && $argc >= 2 ) {
-                       $mwPluralForm =& MagicWord::get( MAG_PLURAL );
-                       if ( $mwPluralForm->matchStartAndRemove( $part1 ) ) {
-                               if ($argc==2) {$args[2]=$args[1];}
-                               $text = $linestart . $lang->convertPlural( $part1, $args[0], $args[1], $args[2]);
-                               $found = true;
-                       }
-               }
-               
-               # DISPLAYTITLE
-               if ( !$found && $argc == 1 && $wgAllowDisplayTitle ) {
-                       $mwDT =& MagicWord::get( MAG_DISPLAYTITLE );
-                       if ( $mwDT->matchStartAndRemove( $part1 ) ) {
-                               
-                               # Set title in parser output object
-                               $param = $args[0];
-                               $parserOptions = new ParserOptions;
-                               $local_parser = new Parser ();
-                               $t2 = $local_parser->parse ( $param, $this->mTitle, $parserOptions, false );
-                               $this->mOutput->mHTMLtitle = $t2->GetText();
-
-                               # Add subtitle
-                               $t = $this->mTitle->getPrefixedText();
-                               $this->mOutput->mSubtitle .= wfMsg('displaytitle', $t);
-                               $text = "" ;
-                               $found = true ;
-                       }
-               }               
-
-               # NUMBEROFPAGES, NUMBEROFUSERS, NUMBEROFARTICLES, and NUMBEROFFILES
-               if( !$found ) {
-                       $mwWordsToCheck = array( MAG_NUMBEROFPAGES => 'wfNumberOfPages',
-                                                                        MAG_NUMBEROFUSERS => 'wfNumberOfUsers',
-                                                                        MAG_NUMBEROFARTICLES => 'wfNumberOfArticles',
-                                                                        MAG_NUMBEROFFILES => 'wfNumberOfFiles' );
-                       foreach( $mwWordsToCheck as $word => $func ) {
-                               $mwCurrentWord =& MagicWord::get( $word );
-                               if( $mwCurrentWord->matchStartAndRemove( $part1 ) ) {
-                                       $mwRawSuffix =& MagicWord::get( MAG_RAWSUFFIX );
-                                       if( $mwRawSuffix->match( $args[0] ) ) {
-                                               # Raw and unformatted
-                                               $text = $linestart . call_user_func( $func );
-                                       } else {
-                                               # Formatted according to the content default
-                                               $text = $linestart . $wgContLang->formatNum( call_user_func( $func ) );
-                                       }
-                                       $found = true;
-                               }
-                       }
-               }
-
-               # Extensions
-               if ( !$found && substr( $part1, 0, 1 ) == '#' ) {
-                       $colonPos = strpos( $part1, ':' );
-                       if ( $colonPos !== false ) {
-                               $function = strtolower( substr( $part1, 1, $colonPos - 1 ) );
-                               if ( isset( $this->mFunctionHooks[$function] ) ) {
+                               if ( $function ) {
                                        $funcArgs = array_map( 'trim', $args );
                                        $funcArgs = array_merge( array( &$this, trim( substr( $part1, $colonPos + 1 ) ) ), $funcArgs );
                                        $result = call_user_func_array( $this->mFunctionHooks[$function], $funcArgs );
@@ -2745,10 +2743,12 @@ class Parser
                                        // The text is usually already parsed, doesn't need triple-brace tags expanded, etc.
                                        //$noargs = true;
                                        //$noparse = true;
-                                       
+
                                        if ( is_array( $result ) ) {
-                                               $text = $linestart . $result[0];
-                                               unset( $result[0] );
+                                               if ( isset( $result[0] ) ) {
+                                                       $text = $linestart . $result[0];
+                                                       unset( $result[0] );
+                                               }
 
                                                // Extract flags into the local scope
                                                // This allows callers to set flags such as nowiki, noparse, found, etc.
@@ -2758,6 +2758,7 @@ class Parser
                                        }
                                }
                        }
+                       wfProfileOut( __METHOD__ . '-pfunc' );
                }
 
                # Template table test
@@ -2785,6 +2786,7 @@ class Parser
                # Load from database
                $lastPathLevel = $this->mTemplatePath;
                if ( !$found ) {
+                       wfProfileIn( __METHOD__ . '-loadtpl' );
                        $ns = NS_TEMPLATE;
                        # declaring $subpage directly in the function call
                        # does not work correctly with references and breaks
@@ -2796,7 +2798,14 @@ class Parser
                        }
                        $title = Title::newFromText( $part1, $ns );
 
+
                        if ( !is_null( $title ) ) {
+                               $checkVariantLink = sizeof($wgContLang->getVariants())>1;
+                               # Check for language variants if the template is not found
+                               if($checkVariantLink && $title->getArticleID() == 0){
+                                       $wgContLang->findVariantLink($part1, $title);
+                               }
+
                                if ( !$title->isExternal() ) {
                                        # Check for excessive inclusion
                                        $dbk = $title->getPrefixedDBkey();
@@ -2837,15 +2846,20 @@ class Parser
                                        }
                                        $found = true;
                                }
-                               
+
                                # Template cache array insertion
                                # Use the original $piece['title'] not the mangled $part1, so that
                                # modifiers such as RAW: produce separate cache entries
                                if( $found ) {
-                                       $this->mTemplates[$piece['title']] = $text;
+                                       if( $isHTML ) {
+                                               // A special page; don't store it in the template cache.
+                                       } else {
+                                               $this->mTemplates[$piece['title']] = $text;
+                                       }
                                        $text = $linestart . $text;
                                }
                        }
+                       wfProfileOut( __METHOD__ . '-loadtpl' );
                }
 
                # Recursive parsing, escaping and link table handling
@@ -2917,6 +2931,7 @@ class Parser
                        wfProfileOut( $fname );
                        return $piece['text'];
                } else {
+                       wfProfileIn( __METHOD__ . '-placeholders' );
                        if ( $isHTML ) {
                                # Replace raw HTML by a placeholder
                                # Add a blank line preceding, to prevent it from mucking up
@@ -2950,6 +2965,7 @@ class Parser
                                        }
                                }
                        }
+                       wfProfileOut( __METHOD__ . '-placeholders' );
                }
 
                # Prune lower levels off the recursion check path
@@ -3024,7 +3040,7 @@ class Parser
                        }
                }
 
-               $text = wfGetHTTP($url);
+               $text = Http::get($url);
                if (!$text)
                        return wfMsg('scarytranscludefailed', $url);
 
@@ -3070,25 +3086,35 @@ class Parser
                }
        }
 
+       /**
+        * Detect __NOGALLERY__ magic word and set a placeholder
+        */
+       function stripNoGallery( &$text ) {
+               # if the string __NOGALLERY__ (not case-sensitive) occurs in the HTML,
+               # do not add TOC
+               $mw = MagicWord::get( 'nogallery' );
+               $this->mOutput->mNoGallery = $mw->matchAndRemove( $text ) ;
+       }
+
        /**
         * Detect __TOC__ magic word and set a placeholder
         */
        function stripToc( $text ) {
                # if the string __NOTOC__ (not case-sensitive) occurs in the HTML,
                # do not add TOC
-               $mw = MagicWord::get( MAG_NOTOC );
+               $mw = MagicWord::get( 'notoc' );
                if( $mw->matchAndRemove( $text ) ) {
                        $this->mShowToc = false;
                }
-               
-               $mw = MagicWord::get( MAG_TOC );
+
+               $mw = MagicWord::get( 'toc' );
                if( $mw->match( $text ) ) {
                        $this->mShowToc = true;
                        $this->mForceTocPosition = true;
-                       
+
                        // Set a placeholder. At the end we'll fill it in with the TOC.
                        $text = $mw->replace( '<!--MWTOC-->', $text, 1 );
-                       
+
                        // Only keep the first one.
                        $text = $mw->replace( '', $text );
                }
@@ -3120,7 +3146,7 @@ class Parser
                }
 
                # Inhibit editsection links if requested in the page
-               $esw =& MagicWord::get( MAG_NOEDITSECTION );
+               $esw =& MagicWord::get( 'noeditsection' );
                if( $esw->matchAndRemove( $text ) ) {
                        $showEditLink = 0;
                }
@@ -3136,13 +3162,13 @@ class Parser
 
                # Allow user to stipulate that a page should have a "new section"
                # link added via __NEWSECTIONLINK__
-               $mw =& MagicWord::get( MAG_NEWSECTIONLINK );
+               $mw =& MagicWord::get( 'newsectionlink' );
                if( $mw->matchAndRemove( $text ) )
                        $this->mOutput->setNewSection( true );
 
                # if the string __FORCETOC__ (not case-sensitive) occurs in the HTML,
                # override above conditions and always show TOC above first header
-               $mw =& MagicWord::get( MAG_FORCETOC );
+               $mw =& MagicWord::get( 'forcetoc' );
                if ($mw->matchAndRemove( $text ) ) {
                        $this->mShowToc = true;
                        $enoughToc = true;
@@ -3373,7 +3399,7 @@ class Parser
                        }
 
                        $isbn = $blank = '' ;
-                       while ( ' ' == $x{0} ) {
+                       while ( $x !== '' && ' ' == $x{0} ) {
                                $blank .= ' ';
                                $x = substr( $x, 1 );
                        }
@@ -3444,7 +3470,7 @@ class Parser
                                $text .= $keyword . $x;
                                continue;
                        }
-                       
+
                        $id = $blank = '' ;
 
                        /** remove and save whitespaces in $blank */
@@ -3470,7 +3496,7 @@ class Parser
                                $url = wfMsg( $urlmsg, $id);
                                $sk =& $this->mOptions->getSkin();
                                $la = $sk->getExternalLinkAttributes( $url, $keyword.$id );
-                               $text .= "<a href='{$url}'{$la}>{$keyword}{$id}</a>{$x}";
+                               $text .= "<a href=\"{$url}\"{$la}>{$keyword}{$id}</a>{$x}";
                        }
 
                        /* Check if the next RFC keyword is preceed by [[ */
@@ -3505,7 +3531,7 @@ class Parser
                        "\r\n" => "\n",
                );
                $text = str_replace( array_keys( $pairs ), array_values( $pairs ), $text );
-               $text = $this->strip( $text, $stripState, true );
+               $text = $this->strip( $text, $stripState, true, array( 'gallery' ) );
                $text = $this->pstPass2( $text, $stripState, $user );
                $text = $this->unstrip( $text, $stripState );
                $text = $this->unstripNoWiki( $text, $stripState );
@@ -3537,10 +3563,10 @@ class Parser
                # Variable replacement
                # Because mOutputType is OT_WIKI, this will only process {{subst:xxx}} type tags
                $text = $this->replaceVariables( $text );
-               
+
                # Strip out <nowiki> etc. added via replaceVariables
-               $text = $this->strip( $text, $stripState );
-       
+               $text = $this->strip( $text, $stripState, false, array( 'gallery' ) );
+
                # Signatures
                $sigText = $this->getUserSig( $user );
                $text = strtr( $text, array(
@@ -3578,10 +3604,10 @@ class Parser
                }
 
                # Trim trailing whitespace
-               # MAG_END (__END__) tag allows for trailing
+               # __END__ tag allows for trailing
                # whitespace to be deliberately included
                $text = rtrim( $text );
-               $mw =& MagicWord::get( MAG_END );
+               $mw =& MagicWord::get( 'end' );
                $mw->matchAndRemove( $text );
 
                return $text;
@@ -3599,7 +3625,7 @@ class Parser
                $username = $user->getName();
                $nickname = $user->getOption( 'nickname' );
                $nickname = $nickname === '' ? $username : $nickname;
-       
+
                if( $user->getBoolOption( 'fancysig' ) !== false ) {
                        # Sig. might contain markup; validate this
                        if( $this->validateSig( $nickname ) !== false ) {
@@ -3612,6 +3638,9 @@ class Parser
                        }
                }
 
+               // Make sure nickname doesnt get a sig in a sig
+               $nickname = $this->cleanSigInSig( $nickname );
+
                # If we're still here, make it a link to the user page
                $userpage = $user->getUserPage();
                return( '[[' . $userpage->getPrefixedText() . '|' . wfEscapeWikiText( $nickname ) . ']]' );
@@ -3626,11 +3655,11 @@ class Parser
        function validateSig( $text ) {
                return( wfIsWellFormedXmlFragment( $text ) ? $text : false );
        }
-       
+
        /**
         * Clean up signature text
         *
-        * 1) Strip ~~~, ~~~~ and ~~~~~ out of signatures
+        * 1) Strip ~~~, ~~~~ and ~~~~~ out of signatures @see cleanSigInSig
         * 2) Substitute all transclusions
         *
         * @param string $text
@@ -3640,19 +3669,29 @@ class Parser
        function cleanSig( $text, $parsing = false ) {
                global $wgTitle;
                $this->startExternalParse( $wgTitle, new ParserOptions(), $parsing ? OT_WIKI : OT_MSG );
-       
-               $substWord = MagicWord::get( MAG_SUBST );
+
+               $substWord = MagicWord::get( 'subst' );
                $substRegex = '/\{\{(?!(?:' . $substWord->getBaseRegex() . '))/x' . $substWord->getRegexCase();
                $substText = '{{' . $substWord->getSynonym( 0 );
 
                $text = preg_replace( $substRegex, $substText, $text );
-               $text = preg_replace( '/~{3,5}/', '', $text );
+               $text = $this->cleanSigInSig( $text );
                $text = $this->replaceVariables( $text );
-               
-               $this->clearState();    
+
+               $this->clearState();
                return $text;
        }
-       
+
+       /**
+        * Strip ~~~, ~~~~ and ~~~~~ out of signatures
+        * @param string $text
+        * @return string Signature text with /~{3,5}/ removed
+        */
+       function cleanSigInSig( $text ) {
+               $text = preg_replace( '/~{3,5}/', '', $text );
+               return $text;
+       }
+
        /**
         * Set up some variables which are usually set up in parse()
         * so that an external function can call some class members with confidence
@@ -3716,6 +3755,7 @@ class Parser
         * @return The old value of the mTagHooks array associated with the hook
         */
        function setHook( $tag, $callback ) {
+               $tag = strtolower( $tag );
                $oldVal = @$this->mTagHooks[$tag];
                $this->mTagHooks[$tag] = $callback;
 
@@ -3739,15 +3779,42 @@ class Parser
         *
         * @public
         *
-        * @param string $name The function name. Function names are case-insensitive.
+        * @param mixed $id The magic word ID
         * @param mixed $callback The callback function (and object) to use
+        * @param integer $flags a combination of the following flags: 
+        *                SFH_NO_HASH No leading hash, i.e. {{plural:...}} instead of {{#if:...}}
         *
         * @return The old callback function for this name, if any
         */
-       function setFunctionHook( $name, $callback ) {
-               $name = strtolower( $name );
-               $oldVal = @$this->mFunctionHooks[$name];
-               $this->mFunctionHooks[$name] = $callback;
+       function setFunctionHook( $id, $callback, $flags = 0 ) {
+               $oldVal = @$this->mFunctionHooks[$id];
+               $this->mFunctionHooks[$id] = $callback;
+
+               # Add to function cache
+               $mw = MagicWord::get( $id );
+               if ( !$mw ) {
+                       throw new MWException( 'The calling convention to Parser::setFunctionHook() has changed, ' .
+                               'it is now required to pass a MagicWord ID as the first parameter.' );
+               }
+
+               $synonyms = $mw->getSynonyms();
+               $sensitive = intval( $mw->isCaseSensitive() );
+
+               foreach ( $synonyms as $syn ) {
+                       # Case
+                       if ( !$sensitive ) {
+                               $syn = strtolower( $syn );
+                       }
+                       # Add leading hash
+                       if ( !( $flags & SFH_NO_HASH ) ) {
+                               $syn = '#' . $syn;
+                       }
+                       # Remove trailing colon
+                       if ( substr( $syn, -1, 1 ) == ':' ) {
+                               $syn = substr( $syn, 0, -1 );
+                       }
+                       $this->mFunctionSynonyms[$sensitive][$syn] = $id;
+               }
                return $oldVal;
        }
 
@@ -3951,6 +4018,19 @@ class Parser
                return $matches[0];
        }
 
+       /**
+        * Tag hook handler for 'pre'.
+        */
+       function renderPreTag( $text, $attribs, $parser ) {
+               // Backwards-compatibility hack
+               $content = preg_replace( '!<nowiki>(.*?)</nowiki>!is', '\\1', $text );
+
+               $attribs = Sanitizer::validateTagAttributes( $attribs, 'pre' );
+               return wfOpenElement( 'pre', $attribs ) .
+                       wfEscapeHTMLTagsOnly( $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.
@@ -3960,17 +4040,17 @@ class Parser
         * labeled 'The number "1"' and
         * 'A tree'.
         */
-       function renderImageGallery( $text ) {
-               # Setup the parser
-               $parserOptions = new ParserOptions;
-               $localParser = new Parser();
-
+       function renderImageGallery( $text, $params ) {
                $ig = new ImageGallery();
                $ig->setShowBytes( false );
                $ig->setShowFilename( false );
                $ig->setParsing();
-               $lines = explode( "\n", $text );
+               $ig->useSkin( $this->mOptions->getSkin() );
+
+               if( isset( $params['caption'] ) )
+                       $ig->setCaption( $params['caption'] );
 
+               $lines = explode( "\n", $text );
                foreach ( $lines as $line ) {
                        # match lines like these:
                        # Image:someimage.jpg|This is some image
@@ -3979,7 +4059,8 @@ class Parser
                        if ( count( $matches ) == 0 ) {
                                continue;
                        }
-                       $nt =& Title::newFromText( $matches[1] );
+                       $tp = Title::newFromText( $matches[1] );
+                       $nt =& $tp;
                        if( is_null( $nt ) ) {
                                # Bogus title. Ignore these so we don't bomb out later.
                                continue;
@@ -3990,7 +4071,12 @@ class Parser
                                $label = '';
                        }
 
-                       $pout = $localParser->parse( $label , $this->mTitle, $parserOptions );
+                       $pout = $this->parse( $label,
+                               $this->mTitle,
+                               $this->mOptions,
+                               false, // Strip whitespace...?
+                               false  // Don't clear state!
+                       );
                        $html = $pout->getText();
 
                        $ig->add( new Image( $nt ), $html );
@@ -3999,13 +4085,6 @@ class Parser
                        if ( $nt->getNamespace() == NS_IMAGE ) {
                                $this->mOutput->addImage( $nt->getDBkey() );
                        }
-                       
-                       # Register links with the parent parser
-                       foreach( $pout->getLinks() as $ns => $keys ) {
-                               foreach( $keys as $dbk => $id )
-                                       $this->mOutput->addLink( Title::makeTitle( $ns, $dbk ), $id );
-                       }
-                       
                }
                return $ig->toHTML();
        }
@@ -4030,14 +4109,14 @@ class Parser
 
                $part = explode( '|', $options);
 
-               $mwThumb  =& MagicWord::get( MAG_IMG_THUMBNAIL );
-               $mwManualThumb =& MagicWord::get( MAG_IMG_MANUALTHUMB );
-               $mwLeft   =& MagicWord::get( MAG_IMG_LEFT );
-               $mwRight  =& MagicWord::get( MAG_IMG_RIGHT );
-               $mwNone   =& MagicWord::get( MAG_IMG_NONE );
-               $mwWidth  =& MagicWord::get( MAG_IMG_WIDTH );
-               $mwCenter =& MagicWord::get( MAG_IMG_CENTER );
-               $mwFramed =& MagicWord::get( MAG_IMG_FRAMED );
+               $mwThumb  =& MagicWord::get( 'img_thumbnail' );
+               $mwManualThumb =& MagicWord::get( 'img_manualthumb' );
+               $mwLeft   =& MagicWord::get( 'img_left' );
+               $mwRight  =& MagicWord::get( 'img_right' );
+               $mwNone   =& MagicWord::get( 'img_none' );
+               $mwWidth  =& MagicWord::get( 'img_width' );
+               $mwCenter =& MagicWord::get( 'img_center' );
+               $mwFramed =& MagicWord::get( 'img_framed' );
                $caption = '';
 
                $width = $height = $framed = $thumb = false;
@@ -4063,7 +4142,7 @@ class Parser
                                # remember to set an alignment, don't render immediately
                                $align = 'none';
                        } elseif ( $wgUseImageResize && ! is_null( $match = $mwWidth->matchVariableStartToEnd($val) ) ) {
-                               wfDebug( "MAG_IMG_WIDTH match: $match\n" );
+                               wfDebug( "img_width match: $match\n" );
                                # $match is the image width in pixels
                                if ( preg_match( '/^([0-9]*)x([0-9]*)$/', $match, $m ) ) {
                                        $width = intval( $m[1] );
@@ -4134,6 +4213,165 @@ class Parser
         */
        function getTags() { return array_keys( $this->mTagHooks ); }
        /**#@-*/
+
+
+       /**
+        * Break wikitext input into sections, and either pull or replace
+        * some particular section's text.
+        *
+        * External callers should use the getSection and replaceSection methods.
+        *
+        * @param $text Page wikitext
+        * @param $section Numbered section. 0 pulls the text before the first
+        *                 heading; other numbers will pull the given section
+        *                 along with its lower-level subsections.
+        * @param $mode One of "get" or "replace"
+        * @param $newtext Replacement text for section data.
+        * @return string for "get", the extracted section text.
+        *                for "replace", the whole page with the section replaced.
+        */
+       private function extractSections( $text, $section, $mode, $newtext='' ) {
+               # strip NOWIKI etc. to avoid confusion (true-parameter causes HTML
+               # comments to be stripped as well)
+               $striparray = array();
+
+               $oldOutputType = $this->mOutputType;
+               $oldOptions = $this->mOptions;
+               $this->mOptions = new ParserOptions();
+               $this->mOutputType = OT_WIKI;
+
+               $striptext = $this->strip( $text, $striparray, true );
+
+               $this->mOutputType = $oldOutputType;
+               $this->mOptions = $oldOptions;
+
+               # now that we can be sure that no pseudo-sections are in the source,
+               # split it up by section
+               $uniq = preg_quote( $this->uniqPrefix(), '/' );
+               $comment = "(?:$uniq-!--.*?QINU)";
+               $secs = preg_split(
+               /*
+                       "/
+                       ^(
+                       (?:$comment|<\/?noinclude>)* # Initial comments will be stripped
+                       (?:
+                               (=+) # Should this be limited to 6?
+                               .+?  # Section title...
+                               \\2  # Ending = count must match start
+                       |
+                               ^
+                               <h([1-6])\b.*?>
+                               .*?
+                               <\/h\\3\s*>
+                       )
+                       (?:$comment|<\/?noinclude>|\s+)* # Trailing whitespace ok
+                       )$
+                       /mix",
+               */
+                       "/
+                       (
+                               ^
+                               (?:$comment|<\/?noinclude>)* # Initial comments will be stripped
+                               (=+) # Should this be limited to 6?
+                               .+?  # Section title...
+                               \\2  # Ending = count must match start
+                               (?:$comment|<\/?noinclude>|[ \\t]+)* # Trailing whitespace ok
+                               $
+                       |
+                               <h([1-6])\b.*?>
+                               .*?
+                               <\/h\\3\s*>
+                       )
+                       /mix",
+                       $striptext, -1,
+                       PREG_SPLIT_DELIM_CAPTURE);
+
+               if( $mode == "get" ) {
+                       if( $section == 0 ) {
+                               // "Section 0" returns the content before any other section.
+                               $rv = $secs[0];
+                       } else {
+                               $rv = "";
+                       }
+               } elseif( $mode == "replace" ) {
+                       if( $section == 0 ) {
+                               $rv = $newtext . "\n\n";
+                               $remainder = true;
+                       } else {
+                               $rv = $secs[0];
+                               $remainder = false;
+                       }
+               }
+               $count = 0;
+               $sectionLevel = 0;
+               for( $index = 1; $index < count( $secs ); ) {
+                       $headerLine = $secs[$index++];
+                       if( $secs[$index] ) {
+                               // A wiki header
+                               $headerLevel = strlen( $secs[$index++] );
+                       } else {
+                               // An HTML header
+                               $index++;
+                               $headerLevel = intval( $secs[$index++] );
+                       }
+                       $content = $secs[$index++];
+
+                       $count++;
+                       if( $mode == "get" ) {
+                               if( $count == $section ) {
+                                       $rv = $headerLine . $content;
+                                       $sectionLevel = $headerLevel;
+                               } elseif( $count > $section ) {
+                                       if( $sectionLevel && $headerLevel > $sectionLevel ) {
+                                               $rv .= $headerLine . $content;
+                                       } else {
+                                               // Broke out to a higher-level section
+                                               break;
+                                       }
+                               }
+                       } elseif( $mode == "replace" ) {
+                               if( $count < $section ) {
+                                       $rv .= $headerLine . $content;
+                               } elseif( $count == $section ) {
+                                       $rv .= $newtext . "\n\n";
+                                       $sectionLevel = $headerLevel;
+                               } elseif( $count > $section ) {
+                                       if( $headerLevel <= $sectionLevel ) {
+                                               // Passed the section's sub-parts.
+                                               $remainder = true;
+                                       }
+                                       if( $remainder ) {
+                                               $rv .= $headerLine . $content;
+                                       }
+                               }
+                       }
+               }
+               # reinsert stripped tags
+               $rv = $this->unstrip( $rv, $striparray );
+               $rv = $this->unstripNoWiki( $rv, $striparray );
+               $rv = trim( $rv );
+               return $rv;
+       }
+
+       /**
+        * This function returns the text of a section, specified by a number ($section).
+        * A section is text under a heading like == Heading == or \<h1\>Heading\</h1\>, or
+        * the first section before any such heading (section 0).
+        *
+        * If a section contains subsections, these are also returned.
+        *
+        * @param $text String: text to look in
+        * @param $section Integer: section number
+        * @return string text of the requested section
+        */
+       function getSection( $text, $section ) {
+               return $this->extractSections( $text, $section, "get" );
+       }
+
+       function replaceSection( $oldtext, $section, $text ) {
+               return $this->extractSections( $oldtext, $section, "replace", $text );
+       }
+
 }
 
 /**
@@ -4155,7 +4393,8 @@ class ParserOutput
                $mExternalLinks,    # External link URLs, in the key only
                $mHTMLtitle,            # Display HTML title
                $mSubtitle,                     # Additional subtitle
-               $mNewSection;           # Show a new section link?
+               $mNewSection,           # Show a new section link?
+               $mNoGallery;            # No gallery on category page? (__NOGALLERY__)
 
        function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
                $containsOldMagic = false, $titletext = '' )
@@ -4174,6 +4413,7 @@ class ParserOutput
                $this->mHTMLtitle = "" ;
                $this->mSubtitle = "" ;
                $this->mNewSection = false;
+               $this->mNoGallery = false;
        }
 
        function getText()                   { return $this->mText; }
@@ -4186,6 +4426,8 @@ class ParserOutput
        function &getTemplates()             { return $this->mTemplates; }
        function &getImages()                { return $this->mImages; }
        function &getExternalLinks()         { return $this->mExternalLinks; }
+       function getNoGallery()              { return $this->mNoGallery; }
+       function getSubtitle()               { return $this->mSubtitle; }
 
        function containsOldMagic()          { return $this->mContainsOldMagic; }
        function setText( $text )            { return wfSetVar( $this->mText, $text ); }
@@ -4193,7 +4435,8 @@ class ParserOutput
        function setCategoryLinks( $cl )     { return wfSetVar( $this->mCategories, $cl ); }
        function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
        function setCacheTime( $t )          { return wfSetVar( $this->mCacheTime, $t ); }
-       function setTitleText( $t )          { return wfSetVar ($this->mTitleText, $t); }
+       function setTitleText( $t )          { return wfSetVar($this->mTitleText, $t); }
+       function setSubtitle( $st )          { return wfSetVar( $this->mSubtitle, $st ); }
 
        function addCategory( $c, $sort )    { $this->mCategories[$c] = $sort; }
        function addImage( $name )           { $this->mImages[$name] = 1; }
@@ -4265,19 +4508,33 @@ class ParserOptions
        var $mTidy;                      # Ask for tidy cleanup
        var $mInterfaceMessage;          # Which lang to call for PLURAL and GRAMMAR
 
+       var $mUser;                      # Stored user object, just used to initialise the skin
+
        function getUseTeX()                        { return $this->mUseTeX; }
        function getUseDynamicDates()               { return $this->mUseDynamicDates; }
        function getInterwikiMagic()                { return $this->mInterwikiMagic; }
        function getAllowExternalImages()           { return $this->mAllowExternalImages; }
        function getAllowExternalImagesFrom()       { return $this->mAllowExternalImagesFrom; }
-       function &getSkin()                         { return $this->mSkin; }
-       function getDateFormat()                    { return $this->mDateFormat; }
        function getEditSection()                   { return $this->mEditSection; }
        function getNumberHeadings()                { return $this->mNumberHeadings; }
        function getAllowSpecialInclusion()         { return $this->mAllowSpecialInclusion; }
        function getTidy()                          { return $this->mTidy; }
        function getInterfaceMessage()              { return $this->mInterfaceMessage; }
 
+       function &getSkin() {
+               if ( !isset( $this->mSkin ) ) {
+                       $this->mSkin = $this->mUser->getSkin();
+               }
+               return $this->mSkin;
+       }
+
+       function getDateFormat() { 
+               if ( !isset( $this->mDateFormat ) ) {
+                       $this->mDateFormat = $this->mUser->getDatePreference();
+               }
+               return $this->mDateFormat;
+       }
+
        function setUseTeX( $x )                    { return wfSetVar( $this->mUseTeX, $x ); }
        function setUseDynamicDates( $x )           { return wfSetVar( $this->mUseDynamicDates, $x ); }
        function setInterwikiMagic( $x )            { return wfSetVar( $this->mInterwikiMagic, $x ); }
@@ -4291,43 +4548,45 @@ class ParserOptions
        function setSkin( &$x ) { $this->mSkin =& $x; }
        function setInterfaceMessage( $x )          { return wfSetVar( $this->mInterfaceMessage, $x); }
 
-       function ParserOptions() {
-               global $wgUser;
-               $this->initialiseFromUser( $wgUser );
+       function ParserOptions( $user = null ) {
+               $this->initialiseFromUser( $user );
        }
 
        /**
         * Get parser options
         * @static
         */
-       function newFromUser( &$user ) {
-               $popts = new ParserOptions;
-               $popts->initialiseFromUser( $user );
-               return $popts;
+       static function newFromUser( $user ) {
+               return new ParserOptions( $user );
        }
 
        /** Get user options */
-       function initialiseFromUser( &$userInput ) {
+       function initialiseFromUser( $userInput ) {
                global $wgUseTeX, $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
                global $wgAllowExternalImagesFrom, $wgAllowSpecialInclusion;
                $fname = 'ParserOptions::initialiseFromUser';
                wfProfileIn( $fname );
                if ( !$userInput ) {
-                       $user = new User;
-                       $user->setLoaded( true );
+                       global $wgUser;
+                       if ( isset( $wgUser ) ) {
+                               $user = $wgUser;
+                       } else {
+                               $user = new User;
+                               $user->setLoaded( true );
+                       }
                } else {
                        $user =& $userInput;
                }
 
+               $this->mUser = $user;
+
                $this->mUseTeX = $wgUseTeX;
                $this->mUseDynamicDates = $wgUseDynamicDates;
                $this->mInterwikiMagic = $wgInterwikiMagic;
                $this->mAllowExternalImages = $wgAllowExternalImages;
                $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
-               wfProfileIn( $fname.'-skin' );
-               $this->mSkin =& $user->getSkin();
-               wfProfileOut( $fname.'-skin' );
-               $this->mDateFormat = $user->getOption( 'date' );
+               $this->mSkin = null; # Deferred
+               $this->mDateFormat = null; # Deferred
                $this->mEditSection = true;
                $this->mNumberHeadings = $user->getOption( 'numberheadings' );
                $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
@@ -4394,6 +4653,39 @@ function wfNumberOfPages() {
        return (int)$count;
 }
 
+/**
+ * Return the total number of admins
+ *
+ * @return integer
+ */
+function wfNumberOfAdmins() {
+       static $admins = -1;
+       wfProfileIn( 'wfNumberOfAdmins' );
+       if( $admins == -1 ) {
+               $dbr =& wfGetDB( DB_SLAVE );
+               $admins = $dbr->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => 'sysop' ), 'wfNumberOfAdmins' );
+       }
+       wfProfileOut( 'wfNumberOfAdmins' );
+       return (int)$admins;
+}
+
+/**
+ * Count the number of pages in a particular namespace
+ *
+ * @param $ns Namespace
+ * @return integer
+ */
+function wfPagesInNs( $ns ) {
+       static $pageCount = array();
+       wfProfileIn( 'wfPagesInNs' );
+       if( !isset( $pageCount[$ns] ) ) {
+               $dbr =& wfGetDB( DB_SLAVE );
+               $pageCount[$ns] = $dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $ns ), 'wfPagesInNs' );
+       }
+       wfProfileOut( 'wfPagesInNs' );
+       return (int)$pageCount[$ns];
+}
+
 /**
  * Get various statistics from the database
  * @private