Don't assume newFileFromKey always returns a File object (some repos many not support...
[lhc/web/wiklou.git] / includes / filerepo / LocalRepo.php
index 96ec28f..662dfd9 100644 (file)
  */
 class LocalRepo extends FSRepo {
        var $fileFactory = array( 'LocalFile', 'newFromTitle' );
+       var $fileFactoryKey = array( 'LocalFile', 'newFromKey' );
        var $oldFileFactory = array( 'OldLocalFile', 'newFromTitle' );
+       var $oldFileFactoryKey = array( 'OldLocalFile', 'newFromKey' );
        var $fileFromRowFactory = array( 'LocalFile', 'newFromRow' );
        var $oldFileFromRowFactory = array( 'OldLocalFile', 'newFromRow' );
 
+       /**
+        * @throws MWException
+        * @param  $row
+        * @return File
+        */
        function newFileFromRow( $row ) {
                if ( isset( $row->img_name ) ) {
                        return call_user_func( $this->fileFromRowFactory, $row, $this );
@@ -52,8 +59,8 @@ class LocalRepo extends FSRepo {
                                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,'.') + 1 );
+                               $sha1 = self::getHashFromKey( $key );
+                               $ext = substr( $key, strcspn( $key, '.' ) + 1 );
                                $ext = File::normalizeExtension($ext);
                                $inuse = $dbw->selectField( 'oldimage', '1',
                                        array( 'oi_sha1' => $sha1,
@@ -75,6 +82,17 @@ class LocalRepo extends FSRepo {
                }
                return $status;
        }
+
+       /**
+        * Gets the SHA1 hash from a storage key
+        *
+        * @static
+        * @param string $key
+        * @return string
+        */
+       public static function getHashFromKey( $key ) {
+               return strtok( $key, '.' );
+       }
        
        /**
         * Checks if there is a redirect named as $title
@@ -85,7 +103,7 @@ class LocalRepo extends FSRepo {
                global $wgMemc;
 
                if( is_string( $title ) ) {
-                       $title = Title::newFromTitle( $title );
+                       $title = Title::newFromText( $title );
                }
                if( $title instanceof Title && $title->getNamespace() == NS_MEDIA ) {
                        $title = Title::makeTitle( NS_FILE, $title->getText() );
@@ -133,6 +151,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.
+        * @param $title Title
         */
        protected function getArticleID( $title ) {
                if( !$title instanceof Title ) {
@@ -164,9 +183,11 @@ class LocalRepo extends FSRepo {
                );
                
                $result = array();
-               while ( $row = $res->fetchObject() )
+               foreach ( $res as $row ) {
                        $result[] = $this->newFileFromRow( $row );
+               }
                $res->free();
+
                return $result;
        }