merge latest master.
[lhc/web/wiklou.git] / includes / parser / Parser.php
index 28957df..bcf030f 100644 (file)
@@ -207,7 +207,7 @@ class Parser {
        public function __construct( $conf = array() ) {
                $this->mConf = $conf;
                $this->mUrlProtocols = wfUrlProtocols();
-               $this->mExtLinkBracketedRegex = '/\[((' . $this->mUrlProtocols . ')'.
+               $this->mExtLinkBracketedRegex = '/\[(((?i)' . $this->mUrlProtocols . ')'.
                        self::EXT_LINK_URL_CLASS.'+)\p{Zs}*([^\]\\x00-\\x08\\x0a-\\x1F]*?)\]/Su';
                if ( isset( $conf['preprocessorClass'] ) ) {
                        $this->mPreprocessorClass = $conf['preprocessorClass'];
@@ -735,16 +735,22 @@ class Parser {
        /**
         * Get the target language for the content being parsed. This is usually the
         * language that the content is in.
+        *
+        * @since 1.19
+        *
+        * @return Language|null
         */
-       function getTargetLanguage() {
+       public function getTargetLanguage() {
                $target = $this->mOptions->getTargetLanguage();
+
                if ( $target !== null ) {
                        return $target;
                } elseif( $this->mOptions->getInterfaceMessage() ) {
                        return $this->mOptions->getUserLangObj();
                } elseif( is_null( $this->mTitle ) ) {
-                       throw new MWException( __METHOD__.': $this->mTitle is null' );
+                       throw new MWException( __METHOD__ . ': $this->mTitle is null' );
                }
+
                return $this->mTitle->getPageLanguage();
        }
 
@@ -1181,7 +1187,7 @@ class Parser {
                        '!(?:                           # Start cases
                                (<a[ \t\r\n>].*?</a>) |     # m[1]: Skip link text
                                (<.*?>) |                   # m[2]: Skip stuff inside HTML elements' . "
-                               (\\b(?:$prots)$urlChar+) |  # m[3]: Free external links" . '
+                               (\\b(?i:$prots)$urlChar+) |  # m[3]: Free external links" . '
                                (?:RFC|PMID)\s+([0-9]+) |   # m[4]: RFC or PMID, capture number
                                ISBN\s+(\b                  # m[5]: ISBN, capture number
                                        (?: 97[89] [\ \-]? )?   # optional 13-digit ISBN prefix
@@ -1224,7 +1230,7 @@ class Parser {
                                throw new MWException( __METHOD__.': unrecognised match type "' .
                                        substr( $m[0], 0, 20 ) . '"' );
                        }
-                       $url = wfMsgForContent( $urlmsg, $id );
+                       $url = wfMessage( $urlmsg, $id )->inContentLanguage()->text();
                        return Linker::makeExternalLink( $url, "{$keyword} {$id}", true, $CssClass );
                } elseif ( isset( $m[5] ) && $m[5] !== '' ) {
                        # ISBN
@@ -1516,6 +1522,9 @@ class Parser {
                wfProfileIn( __METHOD__ );
 
                $bits = preg_split( $this->mExtLinkBracketedRegex, $text, -1, PREG_SPLIT_DELIM_CAPTURE );
+               if ( $bits === false ) {
+                       throw new MWException( "PCRE needs to be compiled with --enable-unicode-properties in order for MediaWiki to function" );
+               }
                $s = array_shift( $bits );
 
                $i = 0;
@@ -1678,7 +1687,7 @@ class Parser {
                }
                if ( !$text && $this->mOptions->getEnableImageWhitelist()
                         && preg_match( self::EXT_IMAGE_REGEX, $url ) ) {
-                       $whitelist = explode( "\n", wfMsgForContent( 'external_image_whitelist' ) );
+                       $whitelist = explode( "\n", wfMessage( 'external_image_whitelist' )->inContentLanguage()->text() );
                        foreach ( $whitelist as $entry ) {
                                # Sanitize the regex fragment, make it case-insensitive, ignore blank entries/comments
                                if ( strpos( $entry, '#' ) === 0 || $entry === '' ) {
@@ -1743,7 +1752,7 @@ class Parser {
                if ( $useLinkPrefixExtension ) {
                        # Match the end of a line for a word that's not followed by whitespace,
                        # e.g. in the case of 'The Arab al[[Razi]]', 'al' will be matched
-                       $e2 = wfMsgForContent( 'linkprefix' );
+                       $e2 = wfMessage( 'linkprefix' )->inContentLanguage()->text();
                }
 
                if ( is_null( $this->mTitle ) ) {
@@ -1844,7 +1853,7 @@ class Parser {
                        # Don't allow internal links to pages containing
                        # PROTO: where PROTO is a valid URL protocol; these
                        # should be external links.
-                       if ( preg_match( '/^(?:' . $this->mUrlProtocols . ')/', $m[1] ) ) {
+                       if ( preg_match( '/^(?i:' . $this->mUrlProtocols . ')/', $m[1] ) ) {
                                $s .= $prefix . '[[' . $line ;
                                wfProfileOut( __METHOD__."-misc" );
                                continue;
@@ -1937,6 +1946,7 @@ class Parser {
                                # Interwikis
                                wfProfileIn( __METHOD__."-interwiki" );
                                if ( $iw && $this->mOptions->getInterwikiMagic() && $nottalk && Language::fetchLanguageName( $iw, null, 'mw' ) ) {
+                                       // FIXME: the above check prevents links to sites with identifiers that are not language codes
                                        $this->mOutput->addLanguageLink( $nt->getFullText() );
                                        $s = rtrim( $s . $prefix );
                                        $s .= trim( $trail, "\n" ) == '' ? '': $prefix . $trail;
@@ -2081,7 +2091,7 @@ class Parser {
         * @return String: less-or-more HTML with NOPARSE bits
         */
        function armorLinks( $text ) {
-               return preg_replace( '/\b(' . $this->mUrlProtocols . ')/',
+               return preg_replace( '/\b((?i)' . $this->mUrlProtocols . ')/',
                        "{$this->mUniqPrefix}NOPARSE$1", $text );
        }
 
@@ -3077,9 +3087,10 @@ class Parser {
         * @param $max int|null Maximum allowed, when an explicit limit has been
         *       exceeded, provide the values (optional)
         */
-       function limitationWarn( $limitationType, $current = null, $max = null) {
+       function limitationWarn( $limitationType, $current = '', $max = '' ) {
                # does no harm if $current and $max are present but are unnecessary for the message
-               $warning = wfMsgExt( "$limitationType-warning", array( 'parsemag', 'escape' ), $current, $max );
+               $warning = wfMessage( "$limitationType-warning" )->numParams( $current, $max )
+                       ->inContentLanguage()->escaped();
                $this->mOutput->addWarning( $warning );
                $this->addTrackingCategory( "$limitationType-category" );
        }
@@ -3284,7 +3295,8 @@ class Parser {
                                if ( $frame->depth >= $limit ) {
                                        $found = true;
                                        $text = '<span class="error">'
-                                               . wfMsgForContent( 'parser-template-recursion-depth-warning', $limit )
+                                               . wfMessage( 'parser-template-recursion-depth-warning' )
+                                                       ->numParams( $limit )->inContentLanguage()->text()
                                                . '</span>';
                                }
                        }
@@ -3364,7 +3376,9 @@ class Parser {
                        # This has to be done after redirect resolution to avoid infinite loops via redirects
                        if ( !$frame->loopCheck( $title ) ) {
                                $found = true;
-                               $text = '<span class="error">' . wfMsgForContent( 'parser-template-loop-warning', $titleText ) . '</span>';
+                               $text = '<span class="error">'
+                                       . wfMessage( 'parser-template-loop-warning', $titleText )->inContentLanguage()->text()
+                                       . '</span>';
                                wfDebug( __METHOD__.": template loop broken at '$titleText'\n" );
                        }
                        wfProfileOut( __METHOD__ . '-loadtpl' );
@@ -3552,7 +3566,7 @@ class Parser {
                        # Get the revision
                        $rev = $id
                                ? Revision::newFromId( $id )
-                               : Revision::newFromTitle( $title, 0, Revision::AVOID_MASTER );
+                               : Revision::newFromTitle( $title, false, Revision::READ_NORMAL );
                        $rev_id = $rev ? $rev->getId() : 0;
                        # If there is no current revision, there is no page
                        if ( $id === false && !$rev ) {
@@ -3573,7 +3587,13 @@ class Parser {
                        }
 
                        if ( $rev ) {
-                               $text = $rev->getText();
+                               $content = $rev->getContent();
+                               $text = $content->getWikitextForTransclusion();
+
+                               if ( $text === false || $text === null ) {
+                                       $text = false;
+                                       break;
+                               }
                        } elseif ( $title->getNamespace() == NS_MEDIAWIKI ) {
                                global $wgContLang;
                                $message = wfMessage( $wgContLang->lcfirst( $title->getText() ) )->inContentLanguage();
@@ -3581,16 +3601,17 @@ class Parser {
                                        $text = false;
                                        break;
                                }
+                               $content = $message->content();
                                $text = $message->plain();
                        } else {
                                break;
                        }
-                       if ( $text === false ) {
+                       if ( !$content ) {
                                break;
                        }
                        # Redirect?
                        $finalTitle = $title;
-                       $title = Title::newFromRedirect( $text );
+                       $title = $content->getRedirectTarget();
                }
                return array(
                        'text' => $text,
@@ -3654,13 +3675,13 @@ class Parser {
                global $wgEnableScaryTranscluding;
 
                if ( !$wgEnableScaryTranscluding ) {
-                       return wfMsgForContent('scarytranscludedisabled');
+                       return wfMessage('scarytranscludedisabled')->inContentLanguage()->text();
                }
 
                $url = $title->getFullUrl( "action=$action" );
 
                if ( strlen( $url ) > 255 ) {
-                       return wfMsgForContent( 'scarytranscludetoolong' );
+                       return wfMessage( 'scarytranscludetoolong' )->inContentLanguage()->text();
                }
                return $this->fetchScaryTemplateMaybeFromCache( $url );
        }
@@ -3681,7 +3702,7 @@ class Parser {
 
                $text = Http::get( $url );
                if ( !$text ) {
-                       return wfMsgForContent( 'scarytranscludefailed', $url );
+                       return wfMessage( 'scarytranscludefailed', $url )->inContentLanguage()->text();
                }
 
                $dbw = wfGetDB( DB_MASTER );
@@ -4406,7 +4427,7 @@ class Parser {
                $text = $this->replaceVariables( $text );
 
                # This works almost by chance, as the replaceVariables are done before the getUserSig(),
-               # which may corrupt this parser instance via its wfMsgExt( parsemag ) call-
+               # which may corrupt this parser instance via its wfMessage()->text() call-
 
                # Signatures
                $sigText = $this->getUserSig( $user );
@@ -4949,7 +4970,7 @@ class Parser {
                                'vertAlign' => array( 'baseline', 'sub', 'super', 'top', 'text-top', 'middle',
                                        'bottom', 'text-bottom' ),
                                'frame' => array( 'thumbnail', 'manualthumb', 'framed', 'frameless',
-                                       'upright', 'border', 'link', 'alt' ),
+                                       'upright', 'border', 'link', 'alt', 'class' ),
                        );
                        static $internalParamMap;
                        if ( !$internalParamMap ) {
@@ -4999,6 +5020,7 @@ class Parser {
                #  * upright    reduce width for upright images, rounded to full __0 px
                #  * border     draw a 1px border around the image
                #  * alt        Text for HTML alt attribute (defaults to empty)
+               #  * class      Set a class for img node
                #  * link       Set the target of the image link. Can be external, interwiki, or local
                # vertical-align values (no % or length right now):
                #  * baseline
@@ -5067,6 +5089,7 @@ class Parser {
                                                switch( $paramName ) {
                                                case 'manualthumb':
                                                case 'alt':
+                                               case 'class':
                                                        # @todo FIXME: Possibly check validity here for
                                                        # manualthumb? downstream behavior seems odd with
                                                        # missing manual thumbs.
@@ -5080,8 +5103,8 @@ class Parser {
                                                                $paramName = 'no-link';
                                                                $value = true;
                                                                $validated = true;
-                                                       } elseif ( preg_match( "/^$prots/", $value ) ) {
-                                                               if ( preg_match( "/^($prots)$chars+$/u", $value, $m ) ) {
+                                                       } elseif ( preg_match( "/^(?i)$prots/", $value ) ) {
+                                                               if ( preg_match( "/^((?i)$prots)$chars+$/u", $value, $m ) ) {
                                                                        $paramName = 'link-url';
                                                                        $this->mOutput->addExternalLink( $value );
                                                                        if ( $this->mOptions->getExternalLinkTarget() ) {
@@ -5607,7 +5630,7 @@ class Parser {
                # @todo FIXME: Not tolerant to blank link text
                # I.E. [http://www.mediawiki.org] will render as [1] or something depending
                # on how many empty links there are on the page - need to figure that out.
-               $text = preg_replace( '/\[(?:' . $this->mUrlProtocols . ')([^ ]+?) ([^[]+)\]/', '$2', $text );
+               $text = preg_replace( '/\[(?i:' . $this->mUrlProtocols . ')([^ ]+?) ([^[]+)\]/', '$2', $text );
 
                # Parse wikitext quotes (italics & bold)
                $text = $this->doQuotes( $text );