* (bug 19006) {{REVISIONUSER}} no longer acts like {{CURRENTUSER}} in some cases
[lhc/web/wiklou.git] / includes / filerepo / LocalRepo.php
index 612fef9..e556887 100644 (file)
@@ -1,4 +1,12 @@
 <?php
+/**
+ * Local repository that stores files in the local filesystem and registers them
+ * in the wiki's own database.
+ *
+ * @file
+ * @ingroup FileRepo
+ */
+
 /**
  * A repository that stores files in the local filesystem and registers them
  * in the wiki's own database. This is the most commonly used repository class.
@@ -10,18 +18,6 @@ class LocalRepo extends FSRepo {
        var $fileFromRowFactory = array( 'LocalFile', 'newFromRow' );
        var $oldFileFromRowFactory = array( 'OldLocalFile', 'newFromRow' );
 
-       function getSlaveDB() {
-               return wfGetDB( DB_SLAVE );
-       }
-
-       function getMasterDB() {
-               return wfGetDB( DB_MASTER );
-       }
-
-       function getMemcKey( $key ) {
-               return wfWikiID( $this->getSlaveDB() ) . ":{$key}";
-       }
-
        function newFileFromRow( $row ) {
                if ( isset( $row->img_name ) ) {
                        return call_user_func( $this->fileFromRowFactory, $row, $this );
@@ -61,8 +57,8 @@ class LocalRepo extends FSRepo {
                                $ext = File::normalizeExtension($ext);
                                $inuse = $dbw->selectField( 'oldimage', '1',
                                        array( 'oi_sha1' => $sha1,
-                                               "oi_archive_name LIKE '%.{$ext}'",
-                                               'oi_deleted & '.File::DELETED_FILE => File::DELETED_FILE ),
+                                               'oi_archive_name ' . $dbw->buildLike( $dbw->anyString(), ".$ext" ),
+                                               $dbw->bitAnd('oi_deleted', File::DELETED_FILE) => File::DELETED_FILE ),
                                        __METHOD__, array( 'FOR UPDATE' ) );
                        }
                        if ( !$inuse ) {
@@ -79,28 +75,12 @@ class LocalRepo extends FSRepo {
                }
                return $status;
        }
-
+       
        /**
-        * Function link Title::getArticleID().
-        * We can't say Title object, what database it should use, so we duplicate that function here.
+        * Checks if there is a redirect named as $title
+        *
+        * @param $title Title of file
         */
-       protected function getArticleID( $title ) {
-               if( !$title instanceof Title ) {
-                       return 0;
-               }
-               $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;
-       }
-
        function checkRedirect( $title ) {
                global $wgMemc;
 
@@ -108,20 +88,27 @@ class LocalRepo extends FSRepo {
                        $title = Title::newFromTitle( $title );
                }
                if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
-                       $title = Title::makeTitle( NS_IMAGE, $title->getText() );
+                       $title = Title::makeTitle( NS_FILE, $title->getText() );
                }
 
-               $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
+               $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
+               if ( $memcKey === false ) {
+                       $memcKey = $this->getLocalCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
+                       $expiry = 300; // no invalidation, 5 minutes
+               } else {
+                       $expiry = 86400; // has invalidation, 1 day
+               }
                $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
+               if ( $cachedValue === ' '  || $cachedValue === '' ) {
+                       // Does not exist
                        return false;
-               }
+               } elseif ( strval( $cachedValue ) !== '' ) {
+                       return Title::newFromText( $cachedValue, NS_FILE );
+               } // else $cachedValue is false or null: cache miss
 
                $id = $this->getArticleID( $title );
                if( !$id ) {
-                       $wgMemc->set( $memcKey, " ", 9000 );
+                       $wgMemc->set( $memcKey, " ", $expiry );
                        return false;
                }
                $dbr = $this->getSlaveDB();
@@ -132,20 +119,42 @@ class LocalRepo extends FSRepo {
                        __METHOD__
                );
 
-               if( $row ) $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
-               $wgMemc->set( $memcKey, ($row ? $targetTitle->getPrefixedDBkey() : " "), 9000 );
-               if( !$row ) {
+               if( $row && $row->rd_namespace == NS_FILE ) {
+                       $targetTitle = Title::makeTitle( $row->rd_namespace, $row->rd_title );
+                       $wgMemc->set( $memcKey, $targetTitle->getDBkey(), $expiry );
+                       return $targetTitle;
+               } else {
+                       $wgMemc->set( $memcKey, '', $expiry );
                        return false;
                }
-               return $targetTitle;
        }
 
-       function invalidateImageRedirect( $title ) {
-               global $wgMemc;
-               $memcKey = $this->getMemcKey( "image_redirect:" . md5( $title->getPrefixedDBkey() ) );
-               $wgMemc->delete( $memcKey );
+
+       /**
+        * Function link Title::getArticleID().
+        * We can't say Title object, what database it should use, so we duplicate that function here.
+        */
+       protected function getArticleID( $title ) {
+               if( !$title instanceof Title ) {
+                       return 0;
+               }
+               $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;
        }
-       
+
+       /**
+        * Get an array or iterator of file objects for files that have a given 
+        * SHA-1 content hash.
+        */
        function findBySha1( $hash ) {
                $dbr = $this->getSlaveDB();
                $res = $dbr->select(
@@ -155,34 +164,49 @@ class LocalRepo extends FSRepo {
                );
                
                $result = array();
-               while ( $row = $res->fetchObject() )
+               foreach ( $res as $row ) {
                        $result[] = $this->newFileFromRow( $row );
+               }
                $res->free();
+
                return $result;
        }
-       
-       /*
-        * Find many files using one query
+
+       /**
+        * Get a connection to the slave DB
         */
-       function findFiles( $titles, $flags ) {
-               // FIXME: Comply with $flags
-               // FIXME: Only accepts a $titles array where the keys are the sanitized
-               // file names.
-                
-               if ( count( $titles ) == 0 ) return array();            
-       
-               $dbr = $this->getSlaveDB();
-               $res = $dbr->select(
-                       'image',
-                       LocalFile::selectFields(),
-                       array( 'img_name' => array_keys( $titles ) )            
-               );
-               
-               $result = array();
-               while ( $row = $res->fetchObject() ) {
-                       $result[$row->img_name] = $this->newFileFromRow( $row );
+       function getSlaveDB() {
+               return wfGetDB( DB_SLAVE );
+       }
+
+       /**
+        * Get a connection to the master DB
+        */
+       function getMasterDB() {
+               return wfGetDB( DB_MASTER );
+       }
+
+       /**
+        * Get a key on the primary cache for this repository.
+        * Returns false if the repository's cache is not accessible at this site. 
+        * The parameters are the parts of the key, as for wfMemcKey().
+        */
+       function getSharedCacheKey( /*...*/ ) {
+               $args = func_get_args();
+               return call_user_func_array( 'wfMemcKey', $args );
+       }
+
+       /**
+        * Invalidates image redirect cache related to that image
+        *
+        * @param $title Title of page
+        */
+       function invalidateImageRedirect( $title ) {
+               global $wgMemc;
+               $memcKey = $this->getSharedCacheKey( 'image_redirect', md5( $title->getDBkey() ) );
+               if ( $memcKey ) {
+                       $wgMemc->delete( $memcKey );
                }
-               $res->free();
-               return $result;
        }
 }
+