Merge "Handle $title === null in Title::newFromText"
[lhc/web/wiklou.git] / includes / Title.php
index 91d8de4..f6bc947 100644 (file)
@@ -37,7 +37,7 @@ use MediaWiki\MediaWikiServices;
  *       and does not rely on global state or the database.
  */
 class Title implements LinkTarget {
-       /** @var HashBagOStuff */
+       /** @var MapCacheLRU */
        static private $titleCache = null;
 
        /**
@@ -306,6 +306,10 @@ class Title implements LinkTarget {
        public static function newFromTextThrow( $text, $defaultNamespace = NS_MAIN ) {
                if ( is_object( $text ) ) {
                        throw new MWException( '$text must be a string, given an object' );
+               } elseif ( $text === null ) {
+                       // Legacy code relies on MalformedTitleException being thrown in this case
+                       // (happens when URL with no title in it is parsed). TODO fix
+                       throw new MalformedTitleException( 'title-invalid-empty' );
                }
 
                $titleCache = self::getTitleCache();
@@ -371,11 +375,11 @@ class Title implements LinkTarget {
        }
 
        /**
-        * @return HashBagOStuff
+        * @return MapCacheLRU
         */
        private static function getTitleCache() {
                if ( self::$titleCache == null ) {
-                       self::$titleCache = new HashBagOStuff( [ 'maxKeys' => self::CACHE_MAX ] );
+                       self::$titleCache = new MapCacheLRU( self::CACHE_MAX );
                }
                return self::$titleCache;
        }
@@ -979,7 +983,7 @@ class Title implements LinkTarget {
                        && ( !$this->mContentModel || $flags === self::GAID_FOR_UPDATE )
                        && $this->getArticleID( $flags )
                ) {
-                       $linkCache = LinkCache::singleton();
+                       $linkCache = MediaWikiServices::getInstance()->getLinkCache();
                        $linkCache->addLinkObj( $this ); # in case we already had an article ID
                        $this->mContentModel = $linkCache->getGoodLinkFieldObj( $this, 'model' );
                }
@@ -2086,7 +2090,7 @@ class Title implements LinkTarget {
         * May provide false positives, but should never provide a false negative.
         *
         * @param string $action Action that permission needs to be checked for
-        * @param User $user User to check (since 1.19); $wgUser will be used if not provided.
+        * @param User|null $user User to check (since 1.19); $wgUser will be used if not provided.
         * @return bool
         */
        public function quickUserCan( $action, $user = null ) {
@@ -2097,7 +2101,7 @@ class Title implements LinkTarget {
         * Can $user perform $action on this page?
         *
         * @param string $action Action that permission needs to be checked for
-        * @param User $user User to check (since 1.19); $wgUser will be used if not
+        * @param User|null $user User to check (since 1.19); $wgUser will be used if not
         *   provided.
         * @param string $rigor Same format as Title::getUserPermissionsErrors()
         * @return bool
@@ -3139,7 +3143,7 @@ class Title implements LinkTarget {
         * Public for usage by LiquidThreads.
         *
         * @param array $rows Array of db result objects
-        * @param string $oldFashionedRestrictions Comma-separated set of permission keys
+        * @param string|null $oldFashionedRestrictions Comma-separated set of permission keys
         * indicating who can move or edit the page from the page table, (pre 1.10) rows.
         * Edit and move sections are separated by a colon
         * Example: "edit=autoconfirmed,sysop:move=sysop"
@@ -3211,7 +3215,7 @@ class Title implements LinkTarget {
        /**
         * Load restrictions from the page_restrictions table
         *
-        * @param string $oldFashionedRestrictions Comma-separated set of permission keys
+        * @param string|null $oldFashionedRestrictions Comma-separated set of permission keys
         * indicating who can move or edit the page from the page table, (pre 1.10) rows.
         * Edit and move sections are separated by a colon
         * Example: "edit=autoconfirmed,sysop:move=sysop"
@@ -3433,7 +3437,7 @@ class Title implements LinkTarget {
                        $this->mArticleID = 0;
                        return $this->mArticleID;
                }
-               $linkCache = LinkCache::singleton();
+               $linkCache = MediaWikiServices::getInstance()->getLinkCache();
                if ( $flags & self::GAID_FOR_UPDATE ) {
                        $oldUpdate = $linkCache->forUpdate( true );
                        $linkCache->clearLink( $this );
@@ -3463,7 +3467,7 @@ class Title implements LinkTarget {
                        return $this->mRedirect;
                }
 
-               $linkCache = LinkCache::singleton();
+               $linkCache = MediaWikiServices::getInstance()->getLinkCache();
                $linkCache->addLinkObj( $this ); # in case we already had an article ID
                $cached = $linkCache->getGoodLinkFieldObj( $this, 'redirect' );
                if ( $cached === null ) {
@@ -3497,7 +3501,7 @@ class Title implements LinkTarget {
                        $this->mLength = 0;
                        return $this->mLength;
                }
-               $linkCache = LinkCache::singleton();
+               $linkCache = MediaWikiServices::getInstance()->getLinkCache();
                $linkCache->addLinkObj( $this ); # in case we already had an article ID
                $cached = $linkCache->getGoodLinkFieldObj( $this, 'length' );
                if ( $cached === null ) {
@@ -3525,7 +3529,7 @@ class Title implements LinkTarget {
                        $this->mLatestID = 0;
                        return $this->mLatestID;
                }
-               $linkCache = LinkCache::singleton();
+               $linkCache = MediaWikiServices::getInstance()->getLinkCache();
                $linkCache->addLinkObj( $this ); # in case we already had an article ID
                $cached = $linkCache->getGoodLinkFieldObj( $this, 'revision' );
                if ( $cached === null ) {
@@ -3550,7 +3554,7 @@ class Title implements LinkTarget {
         * @param int $newid The new Article ID
         */
        public function resetArticleID( $newid ) {
-               $linkCache = LinkCache::singleton();
+               $linkCache = MediaWikiServices::getInstance()->getLinkCache();
                $linkCache->clearLink( $this );
 
                if ( $newid === false ) {
@@ -3572,7 +3576,7 @@ class Title implements LinkTarget {
        }
 
        public static function clearCaches() {
-               $linkCache = LinkCache::singleton();
+               $linkCache = MediaWikiServices::getInstance()->getLinkCache();
                $linkCache->clear();
 
                $titleCache = self::getTitleCache();
@@ -3676,7 +3680,7 @@ class Title implements LinkTarget {
 
                $retVal = [];
                if ( $res->numRows() ) {
-                       $linkCache = LinkCache::singleton();
+                       $linkCache = MediaWikiServices::getInstance()->getLinkCache();
                        foreach ( $res as $row ) {
                                $titleObj = self::makeTitle( $row->page_namespace, $row->page_title );
                                if ( $titleObj ) {
@@ -3744,7 +3748,7 @@ class Title implements LinkTarget {
                );
 
                $retVal = [];
-               $linkCache = LinkCache::singleton();
+               $linkCache = MediaWikiServices::getInstance()->getLinkCache();
                foreach ( $res as $row ) {
                        if ( $row->page_id ) {
                                $titleObj = self::newFromRow( $row );
@@ -4681,7 +4685,7 @@ class Title implements LinkTarget {
        /**
         * Updates page_touched for this page; called from LinksUpdate.php
         *
-        * @param string $purgeTime [optional] TS_MW timestamp
+        * @param string|null $purgeTime [optional] TS_MW timestamp
         * @return bool True if the update succeeded
         */
        public function invalidateCache( $purgeTime = null ) {
@@ -4752,7 +4756,7 @@ class Title implements LinkTarget {
        /**
         * Get the timestamp when this page was updated since the user last saw it.
         *
-        * @param User $user
+        * @param User|null $user
         * @return string|null
         */
        public function getNotificationTimestamp( $user = null ) {
@@ -4943,7 +4947,7 @@ class Title implements LinkTarget {
                // check, if the page language could be saved in the database, and if so and
                // the value is not requested already, lookup the page language using LinkCache
                if ( $wgPageLanguageUseDB && $this->mDbPageLanguage === false ) {
-                       $linkCache = LinkCache::singleton();
+                       $linkCache = MediaWikiServices::getInstance()->getLinkCache();
                        $linkCache->addLinkObj( $this );
                        $this->mDbPageLanguage = $linkCache->getGoodLinkFieldObj( $this, 'lang' );
                }
@@ -4983,7 +4987,7 @@ class Title implements LinkTarget {
                        $langObj = $contentHandler->getPageLanguage( $this );
                        $this->mPageLanguage = [ $langObj->getCode(), $wgLanguageCode ];
                } else {
-                       $langObj = wfGetLangObj( $this->mPageLanguage[0] );
+                       $langObj = Language::factory( $this->mPageLanguage[0] );
                }
 
                return $langObj;