Mark ForeignDBFile as unmovable
[lhc/web/wiklou.git] / includes / filerepo / LocalRepo.php
index 9377b56..1e08268 100644 (file)
@@ -24,15 +24,15 @@ class LocalRepo extends FSRepo {
                        throw new MWException( __METHOD__.': invalid row' );
                }
        }
-       
+
        function newFromArchiveName( $title, $archiveName ) {
                return OldLocalFile::newFromArchiveName( $title, $this, $archiveName );
        }
 
        /**
-        * Delete files in the deleted directory if they are not referenced in the 
-        * filearchive table. This needs to be done in the repo because it needs to 
-        * interleave database locks with file operations, which is potentially a 
+        * Delete files in the deleted directory if they are not referenced in the
+        * filearchive table. This needs to be done in the repo because it needs to
+        * interleave database locks with file operations, which is potentially a
         * remote operation.
         * @return FileRepoStatus
         */
@@ -45,14 +45,16 @@ class LocalRepo extends FSRepo {
                        $hashPath = $this->getDeletedHashPath( $key );
                        $path = "$root/$hashPath$key";
                        $dbw->begin();
-                       $inuse = $dbw->selectField( 'filearchive', '1', 
+                       $inuse = $dbw->selectField( 'filearchive', '1',
                                array( 'fa_storage_group' => 'deleted', 'fa_storage_key' => $key ),
                                __METHOD__, array( 'FOR UPDATE' ) );
                        if( !$inuse ) {
-                               $sha1 = substr( $key, 0, strcspn($key,'.') );
-                               $ext = substr( $key, strcspn($key,'.') );
+                               $sha1 = substr( $key, 0, strcspn( $key, '.' ) );
+                               $ext = substr( $key, strcspn($key,'.') + 1 );
+                               $ext = File::normalizeExtension($ext);
                                $inuse = $dbw->selectField( 'oldimage', '1',
-                                       array( 'oi_sha1' => $sha1, "oi_archive_name LIKE '%{$ext}'",
+                                       array( 'oi_sha1' => $sha1,
+                                               "oi_archive_name LIKE '%.{$ext}'",
                                                'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
                                        __METHOD__, array( 'FOR UPDATE' ) );
                        }
@@ -75,7 +77,7 @@ class LocalRepo extends FSRepo {
         * Function link Title::getArticleID().
         * We can't say Title object, what database it should use, so we duplicate that function here.
         */
-       private function getArticleID( $title ) {
+       protected function getArticleID( $title ) {
                if( !$title instanceof Title ) {
                        return 0;
                }
@@ -93,17 +95,26 @@ class LocalRepo extends FSRepo {
        }
 
        function checkRedirect( $title ) {
-               global $wgFileRedirects;
-               if( !$wgFileRedirects ) {
-                       return false;
-               }
+               global $wgMemc;
 
+               if( is_string( $title ) ) {
+                       $title = Title::newFromTitle( $title );
+               }
                if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
                        $title = Title::makeTitle( NS_IMAGE, $title->getText() );
                }
-               
+
+               $memcKey = wfMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
+               $cachedValue = $wgMemc->get( $memcKey );
+               if( $cachedValue ) {
+                       return Title::newFromDbKey( $cachedValue );
+               } elseif( $cachedValue == ' ' ) { # FIXME: ugly hack, but BagOStuff caching seems to be weird and return false if !cachedValue, not only if it doesn't exist
+                       return false;
+               }
+
                $id = $this->getArticleID( $title );
                if( !$id ) {
+                       $wgMemc->set( $memcKey, " ", 9000 );
                        return false;
                }
                $dbr = $this->getSlaveDB();
@@ -113,9 +124,18 @@ class LocalRepo extends FSRepo {
                        array( 'rd_from' => $id ),
                        __METHOD__
                );
+
+               if( $row ) $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
+               $wgMemc->set( $memcKey, ($row ? $targetTitle->getPrefixedDBkey() : " "), 9000 );
                if( !$row ) {
                        return false;
                }
-               return Title::makeTitle( $row->rd_namespace, $row->rd_title );
+               return $targetTitle;
+       }
+
+       function invalidateImageRedirect( $title ) {
+               global $wgMemc;
+               $memcKey = wfMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
+               $wgMemc->delete( $memcKey );
        }
 }