Merge "Fix various docs and version numbers from 84a2f570"
[lhc/web/wiklou.git] / includes / parser / CoreParserFunctions.php
index ca27112..3966b9e 100644 (file)
@@ -54,7 +54,7 @@ class CoreParserFunctions {
                        'talkpagename', 'talkpagenamee', 'subjectpagename',
                        'subjectpagenamee', 'pageid', 'revisionid', 'revisionday',
                        'revisionday2', 'revisionmonth', 'revisionmonth1', 'revisionyear',
-                       'revisiontimestamp', 'revisionuser',
+                       'revisiontimestamp', 'revisionuser', 'cascadingsources',
                );
                foreach ( $noHashFunctions as $func ) {
                        $parser->setFunctionHook( $func, array( __CLASS__, $func ), SFH_NO_HASH );
@@ -387,7 +387,7 @@ class CoreParserFunctions {
 
                if ( !$wgRestrictDisplayTitle ) {
                        $parser->mOutput->setDisplayTitle( $text );
-               } elseif ( $title instanceof Title && $title->getFragment() == '' && $title->equals( $parser->mTitle ) ) {
+               } elseif ( $title instanceof Title && !$title->hasFragment() && $title->equals( $parser->mTitle ) ) {
                        $parser->mOutput->setDisplayTitle( $text );
                }
 
@@ -402,11 +402,12 @@ class CoreParserFunctions {
         * @return boolean true on successful match
         */
        private static function matchAgainstMagicword( $magicword, $value ) {
-               if ( strval( $value ) === '' ) {
+               $value = trim( strval( $value ) );
+               if ( $value === '' ) {
                        return false;
                }
                $mwObject = MagicWord::get( $magicword );
-               return $mwObject->match( $value );
+               return $mwObject->matchStartToEnd( $value );
        }
 
        static function formatRaw( $num, $raw ) {
@@ -678,12 +679,6 @@ class CoreParserFunctions {
         * Return the size of the given page, or 0 if it's nonexistent.  This is an
         * expensive parser function and can't be called too many times per page.
         *
-        * @todo FIXME: Title::getLength() documentation claims that it adds things
-        *   to the link cache, so the local cache here should be unnecessary, but
-        *   in fact calling getLength() repeatedly for the same $page does seem to
-        *   run one query for each call?
-        * @todo Document parameters
-        *
         * @param $parser Parser
         * @param $page String Name of page to check (Default: empty string)
         * @param $raw String Should number be human readable with commas or just number
@@ -703,7 +698,10 @@ class CoreParserFunctions {
        }
 
        /**
-        * Returns the requested protection level for the current page
+        * Returns the requested protection level for the current page. This
+        * is an expensive parser function and can't be called too many times
+        * per page, unless the protection levels for the given title have
+        * already been retrieved
         *
         * @param Parser $parser
         * @param string $type
@@ -716,10 +714,13 @@ class CoreParserFunctions {
                if ( !( $titleObject instanceof Title ) ) {
                        $titleObject = $parser->mTitle;
                }
-               $restrictions = $titleObject->getRestrictions( strtolower( $type ) );
-               # Title::getRestrictions returns an array, its possible it may have
-               # multiple values in the future
-               return implode( $restrictions, ',' );
+               if ( $titleObject->areRestrictionsLoaded() || $parser->incrementExpensiveFunctionCount() ) {
+                       $restrictions = $titleObject->getRestrictions( strtolower( $type ) );
+                       # Title::getRestrictions returns an array, its possible it may have
+                       # multiple values in the future
+                       return implode( $restrictions, ',' );
+               }
+               return '';
        }
 
        /**
@@ -985,10 +986,34 @@ class CoreParserFunctions {
                // Use title from parser to have correct pageid after edit
                if ( $t->equals( $parser->getTitle() ) ) {
                        $t = $parser->getTitle();
+                       return $t->getArticleID();
+               }
+
+               // These can't have ids
+               if ( !$t->canExist() || $t->isExternal() ) {
+                       return 0;
+               }
+
+               // Check the link cache, maybe something already looked it up.
+               $linkCache = LinkCache::singleton();
+               $pdbk = $t->getPrefixedDBkey();
+               $id = $linkCache->getGoodLinkID( $pdbk );
+               if ( $id != 0 ) {
+                       $parser->mOutput->addLink( $t, $id );
+                       return $id;
+               }
+               if ( $linkCache->isBadLink( $pdbk ) ) {
+                       $parser->mOutput->addLink( $t, 0 );
+                       return $id;
                }
-               // fetch pageid from cache/database and return the value
-               $pageid = $t->getArticleID();
-               return $pageid ? $pageid : '';
+
+               // We need to load it from the DB, so mark expensive
+               if ( $parser->incrementExpensiveFunctionCount() ) {
+                       $id = $t->getArticleID();
+                       $parser->mOutput->addLink( $t, $id );
+                       return $id;
+               }
+               return null;
        }
 
        /**
@@ -1118,4 +1143,35 @@ class CoreParserFunctions {
                $rev = self::getCachedRevisionObject( $parser, $t );
                return $rev ? $rev->getUserText() : '';
        }
+
+       /**
+        * Returns the sources of any cascading protection acting on a specified page.
+        * Pages will not return their own title unless they transclude themselves.
+        * This is an expensive parser function and can't be called too many times per page,
+        * unless cascading protection sources for the page have already been loaded.
+        *
+        * @param Parser $parser
+        * @param string $title
+        *
+        * @return string
+        * @since 1.23
+        */
+       public static function cascadingsources( $parser, $title = '' ) {
+               $titleObject = Title::newFromText( $title );
+               if ( !( $titleObject instanceof Title ) ) {
+                       $titleObject = $parser->mTitle;
+               }
+               if ( $titleObject->areCascadeProtectionSourcesLoaded()
+                       || $parser->incrementExpensiveFunctionCount()
+               ) {
+                       $names = array();
+                       $sources = $titleObject->getCascadeProtectionSources();
+                       foreach ( $sources[0] as $sourceTitle ) {
+                               $names[] = $sourceTitle->getPrefixedText();
+                       }
+                       return implode( $names, '|' );
+               }
+               return '';
+       }
+
 }