Update some minor type hints
[lhc/web/wiklou.git] / includes / Title.php
index 2582fe2..0f45839 100644 (file)
@@ -37,7 +37,7 @@ use MediaWiki\MediaWikiServices;
  *       and does not rely on global state or the database.
  */
 class Title implements LinkTarget, IDBAccessObject {
-       /** @var MapCacheLRU */
+       /** @var MapCacheLRU|null */
        private static $titleCache = null;
 
        /**
@@ -140,7 +140,7 @@ class Title implements LinkTarget, IDBAccessObject {
         * Only public to share cache with TitleFormatter
         *
         * @private
-        * @var string
+        * @var string|null
         */
        public $prefixedText = null;
 
@@ -173,10 +173,10 @@ class Title implements LinkTarget, IDBAccessObject {
         * the database or false if not loaded, yet. */
        private $mDbPageLanguage = false;
 
-       /** @var TitleValue A corresponding TitleValue object */
+       /** @var TitleValue|null A corresponding TitleValue object */
        private $mTitleValue = null;
 
-       /** @var bool Would deleting this page be a big deletion? */
+       /** @var bool|null Would deleting this page be a big deletion? */
        private $mIsBigDeletion = null;
        // @}
 
@@ -219,7 +219,7 @@ class Title implements LinkTarget, IDBAccessObject {
         * @return Title|null Title, or null on an error
         */
        public static function newFromDBkey( $key ) {
-               $t = new Title();
+               $t = new self();
                $t->mDbkeyform = $key;
 
                try {
@@ -287,7 +287,7 @@ class Title implements LinkTarget, IDBAccessObject {
                }
 
                try {
-                       return self::newFromTextThrow( strval( $text ), $defaultNamespace );
+                       return self::newFromTextThrow( (string)$text, $defaultNamespace );
                } catch ( MalformedTitleException $ex ) {
                        return null;
                }
@@ -337,7 +337,7 @@ class Title implements LinkTarget, IDBAccessObject {
 
                $t = new Title();
                $t->mDbkeyform = strtr( $filteredText, ' ', '_' );
-               $t->mDefaultNamespace = intval( $defaultNamespace );
+               $t->mDefaultNamespace = (int)$defaultNamespace;
 
                $t->secureAndSplit();
                if ( $defaultNamespace == NS_MAIN ) {
@@ -385,7 +385,7 @@ class Title implements LinkTarget, IDBAccessObject {
         * @return MapCacheLRU
         */
        private static function getTitleCache() {
-               if ( self::$titleCache == null ) {
+               if ( self::$titleCache === null ) {
                        self::$titleCache = new MapCacheLRU( self::CACHE_MAX );
                }
                return self::$titleCache;
@@ -437,6 +437,7 @@ class Title implements LinkTarget, IDBAccessObject {
                } else {
                        $title = null;
                }
+
                return $title;
        }
 
@@ -499,7 +500,7 @@ class Title implements LinkTarget, IDBAccessObject {
                                $this->mLatestID = (int)$row->page_latest;
                        }
                        if ( !$this->mForcedContentModel && isset( $row->page_content_model ) ) {
-                               $this->mContentModel = strval( $row->page_content_model );
+                               $this->mContentModel = (string)$row->page_content_model;
                        } elseif ( !$this->mForcedContentModel ) {
                                $this->mContentModel = false; # initialized lazily in getContentModel()
                        }
@@ -546,7 +547,7 @@ class Title implements LinkTarget, IDBAccessObject {
                $t = new Title();
                $t->mInterwiki = $interwiki;
                $t->mFragment = $fragment;
-               $t->mNamespace = $ns = intval( $ns );
+               $t->mNamespace = $ns = (int)$ns;
                $t->mDbkeyform = strtr( $title, ' ', '_' );
                $t->mArticleID = ( $ns >= 0 ) ? -1 : 0;
                $t->mUrlform = wfUrlencode( $t->mDbkeyform );
@@ -3254,8 +3255,9 @@ class Title implements LinkTarget, IDBAccessObject {
                }
 
                if ( $this->mOldRestrictions === false ) {
-                       $this->mOldRestrictions = $dbr->selectField( 'page', 'page_restrictions',
-                               [ 'page_id' => $this->getArticleID() ], __METHOD__ );
+                       $linkCache = MediaWikiServices::getInstance()->getLinkCache();
+                       $linkCache->addLinkObj( $this ); # in case we already had an article ID
+                       $this->mOldRestrictions = $linkCache->getGoodLinkFieldObj( $this, 'restrictions' );
                }
 
                if ( $this->mOldRestrictions != '' ) {
@@ -3340,12 +3342,17 @@ class Title implements LinkTarget, IDBAccessObject {
                                $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
                                $rows = $cache->getWithSetCallback(
                                        // Page protections always leave a new null revision
-                                       $cache->makeKey( 'page-restrictions', $id, $this->getLatestRevID() ),
+                                       $cache->makeKey( 'page-restrictions', 'v1', $id, $this->getLatestRevID() ),
                                        $cache::TTL_DAY,
                                        function ( $curValue, &$ttl, array &$setOpts ) use ( $loadRestrictionsFromDb ) {
                                                $dbr = wfGetDB( DB_REPLICA );
 
                                                $setOpts += Database::getCacheSetOptions( $dbr );
+                                               $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
+                                               if ( $lb->hasOrMadeRecentMasterChanges() ) {
+                                                       // @TODO: cleanup Title cache and caller assumption mess in general
+                                                       $ttl = WANObjectCache::TTL_UNCACHEABLE;
+                                               }
 
                                                return $loadRestrictionsFromDb( $dbr );
                                        }
@@ -3720,6 +3727,7 @@ class Title implements LinkTarget, IDBAccessObject {
                // @todo: get rid of secureAndSplit, refactor parsing code.
                // @note: getTitleParser() returns a TitleParser implementation which does not have a
                //        splitTitleString method, but the only implementation (MediaWikiTitleCodec) does
+               /** @var MediaWikiTitleCodec $titleCodec */
                $titleCodec = MediaWikiServices::getInstance()->getTitleParser();
                // MalformedTitleException can be thrown here
                $parts = $titleCodec->splitTitleString( $this->mDbkeyform, $this->mDefaultNamespace );
@@ -3983,28 +3991,6 @@ class Title implements LinkTarget, IDBAccessObject {
                return $errors ?: true;
        }
 
-       /**
-        * Check if the requested move target is a valid file move target
-        * @todo move this to MovePage
-        * @param Title $nt Target title
-        * @return array List of errors
-        */
-       protected function validateFileMoveOperation( $nt ) {
-               global $wgUser;
-
-               $errors = [];
-
-               $destFile = wfLocalFile( $nt );
-               $destFile->load( File::READ_LATEST );
-               if ( !$wgUser->isAllowed( 'reupload-shared' )
-                       && !$destFile->exists() && wfFindFile( $nt )
-               ) {
-                       $errors[] = [ 'file-exists-sharedrepo' ];
-               }
-
-               return $errors;
-       }
-
        /**
         * Move a title to a new location
         *
@@ -5017,9 +5003,7 @@ class Title implements LinkTarget, IDBAccessObject {
        public function canUseNoindex() {
                global $wgExemptFromUserRobotsControl;
 
-               $bannedNamespaces = is_null( $wgExemptFromUserRobotsControl )
-                       ? MWNamespace::getContentNamespaces()
-                       : $wgExemptFromUserRobotsControl;
+               $bannedNamespaces = $wgExemptFromUserRobotsControl ?? MWNamespace::getContentNamespaces();
 
                return !in_array( $this->mNamespace, $bannedNamespaces );
        }