Make mediawiki.special.pageLanguage work again
[lhc/web/wiklou.git] / includes / filerepo / LocalRepo.php
index 7aa7919..e93a4a8 100644 (file)
@@ -29,9 +29,6 @@
  * @ingroup FileRepo
  */
 class LocalRepo extends FileRepo {
-       /** @var bool */
-       protected $hasSha1Storage = false;
-
        /** @var array */
        protected $fileFactory = array( 'LocalFile', 'newFromTitle' );
 
@@ -192,8 +189,6 @@ class LocalRepo extends FileRepo {
         * @return bool|Title
         */
        function checkRedirect( Title $title ) {
-               $cache = ObjectCache::getMainWANInstance();
-
                $title = File::normalizeTitle( $title, 'exception' );
 
                $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
@@ -203,63 +198,44 @@ class LocalRepo extends FileRepo {
                } else {
                        $expiry = 86400; // has invalidation, 1 day
                }
-               $cachedValue = $cache->get( $memcKey );
-               if ( $cachedValue === ' ' || $cachedValue === '' ) {
-                       // Does not exist
-                       return false;
-               } elseif ( strval( $cachedValue ) !== '' && $cachedValue !== ' PURGED' ) {
-                       return Title::newFromText( $cachedValue, NS_FILE );
-               } // else $cachedValue is false or null: cache miss
-
-               $id = $this->getArticleID( $title );
-               if ( !$id ) {
-                       $cache->set( $memcKey, " ", $expiry );
-
-                       return false;
-               }
-               $dbr = $this->getSlaveDB();
-               $row = $dbr->selectRow(
-                       'redirect',
-                       array( 'rd_title', 'rd_namespace' ),
-                       array( 'rd_from' => $id ),
-                       __METHOD__
-               );
 
-               if ( $row && $row->rd_namespace == NS_FILE ) {
-                       $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
-                       $cache->set( $memcKey, $targetTitle->getDBkey(), $expiry );
-
-                       return $targetTitle;
-               } else {
-                       $cache->set( $memcKey, '', $expiry );
+               $that = $this;
+               $redirDbKey = ObjectCache::getMainWANInstance()->getWithSetCallback(
+                       $memcKey,
+                       $expiry,
+                       function ( $oldValue, &$ttl, array &$setOpts ) use ( $that, $title ) {
+                               $dbr = $that->getSlaveDB(); // possibly remote DB
+
+                               $setOpts += Database::getCacheSetOptions( $dbr );
+
+                               if ( $title instanceof Title ) {
+                                       $row = $dbr->selectRow(
+                                               array( 'page', 'redirect' ),
+                                               array( 'rd_namespace', 'rd_title' ),
+                                               array(
+                                                       'page_namespace' => $title->getNamespace(),
+                                                       'page_title' => $title->getDBkey(),
+                                                       'rd_from = page_id'
+                                               ),
+                                               __METHOD__
+                                       );
+                               } else {
+                                       $row = false;
+                               }
 
-                       return false;
-               }
-       }
+                               return ( $row && $row->rd_namespace == NS_FILE )
+                                       ? Title::makeTitle( $row->rd_namespace, $row->rd_title )->getDBkey()
+                                       : ''; // negative cache
+                       }
+               );
 
-       /**
-        * Function link Title::getArticleID().
-        * We can't say Title object, what database it should use, so we duplicate that function here.
-        *
-        * @param Title $title
-        * @return bool|int|mixed
-        */
-       protected function getArticleID( $title ) {
-               if ( !$title instanceof Title ) {
-                       return 0;
+               // @note: also checks " " for b/c
+               if ( $redirDbKey !== ' ' && strval( $redirDbKey ) !== '' ) {
+                       // Page is a redirect to another file
+                       return Title::newFromText( $redirDbKey, NS_FILE );
                }
-               $dbr = $this->getSlaveDB();
-               $id = $dbr->selectField(
-                       'page', // Table
-                       'page_id', // Field
-                       array( // Conditions
-                               'page_namespace' => $title->getNamespace(),
-                               'page_title' => $title->getDBkey(),
-                       ),
-                       __METHOD__ // Function name
-               );
 
-               return $id;
+               return false; // no redirect
        }
 
        public function findFiles( array $items, $flags = 0 ) {