http://www.mediawiki.org/wiki/Special:Code/MediaWiki/100211

Revision: 100211
Author:   aaron
Date:     2011-10-19 04:06:16 +0000 (Wed, 19 Oct 2011)
Log Message:
-----------
* Removed newFileFromKey() which had a misleading description and a bug where 
giving a sha1,time could fail if two current version files existed with the 
same sha1 but different timestamps. Merged the code into findFileFromKey().
* Fixed bug in findFileFromKey() were it would bail out if it couldn't find a 
matching current file without even check for matching old files.
* Fixed timestamp format of oi_timestamp condition in newFromKey().
* Some code style cleanups.

Modified Paths:
--------------
    trunk/phase3/includes/filerepo/FileRepo.php
    trunk/phase3/includes/filerepo/LocalFile.php
    trunk/phase3/includes/filerepo/OldLocalFile.php

Modified: trunk/phase3/includes/filerepo/FileRepo.php
===================================================================
--- trunk/phase3/includes/filerepo/FileRepo.php 2011-10-19 03:20:19 UTC (rev 
100210)
+++ trunk/phase3/includes/filerepo/FileRepo.php 2011-10-19 04:06:16 UTC (rev 
100211)
@@ -74,7 +74,7 @@
         * @return File
         */
        function newFile( $title, $time = false ) {
-               if ( !($title instanceof Title) ) {
+               if ( !( $title instanceof Title ) ) {
                        $title = Title::makeTitleSafe( NS_FILE, $title );
                        if ( !is_object( $title ) ) {
                                return null;
@@ -130,10 +130,10 @@
                if ( $time !== false ) {
                        $img = $this->newFile( $title, $time );
                        if ( $img && $img->exists() ) {
-                               if ( !$img->isDeleted(File::DELETED_FILE) ) {
+                               if ( !$img->isDeleted( File::DELETED_FILE ) ) {
+                                       return $img; // always OK
+                               } elseif ( !empty( $options['private'] ) && 
$img->userCan( File::DELETED_FILE ) ) {
                                        return $img;
-                               } elseif ( !empty( $options['private'] )  && 
$img->userCan(File::DELETED_FILE) ) {
-                                       return $img;
                                }
                        }
                }
@@ -187,30 +187,6 @@
        }
 
        /**
-        * Create a new File object from the local repository
-        * @param $sha1 Mixed: base 36 SHA-1 hash
-        * @param $time Mixed: time at which the image was uploaded.
-        *              If this is specified, the returned object will be an
-        *              of the repository's old file class instead of a current
-        *              file. Repositories not supporting version control should
-        *              return false if this parameter is set.
-        *
-        * @return File
-        */
-       function newFileFromKey( $sha1, $time = false ) {
-               if ( $time ) {
-                       if ( $this->oldFileFactoryKey ) {
-                               return call_user_func( 
$this->oldFileFactoryKey, $sha1, $this, $time );
-                       }
-               } else {
-                       if ( $this->fileFactoryKey ) {
-                               return call_user_func( $this->fileFactoryKey, 
$sha1, $this );
-                       }
-               }
-               return false;
-       }
-
-       /**
         * Find an instance of the file with this key, created at the specified 
time
         * Returns false if the file does not exist. Repositories not supporting
         * version control should return false if the time is specified.
@@ -221,22 +197,23 @@
        function findFileFromKey( $sha1, $options = array() ) {
                $time = isset( $options['time'] ) ? $options['time'] : false;
 
-               # First try the current version of the file to see if it 
precedes the timestamp
-               $img = $this->newFileFromKey( $sha1 );
-               if ( !$img ) {
-                       return false;
+               # First try to find a matching current version of a file...
+               if ( $this->fileFactoryKey ) {
+                       $img = call_user_func( $this->fileFactoryKey, $sha1, 
$this, $time );
+               } else {
+                       return false; // find-by-sha1 not supported
                }
-               if ( $img->exists() && ( !$time || $img->getTimestamp() == 
$time ) ) {
+               if ( $img && $img->exists() ) {
                        return $img;
                }
-               # Now try an old version of the file
-               if ( $time !== false ) {
-                       $img = $this->newFileFromKey( $sha1, $time );
+               # Now try to find a matching old version of a file...
+               if ( $time !== false && $this->oldFileFactoryKey ) { // 
find-by-sha1 supported?
+                       $img = call_user_func( $this->oldFileFactoryKey, $sha1, 
$this, $time );
                        if ( $img && $img->exists() ) {
-                               if ( !$img->isDeleted(File::DELETED_FILE) ) {
+                               if ( !$img->isDeleted( File::DELETED_FILE ) ) {
+                                       return $img; // always OK
+                               } elseif ( !empty( $options['private'] ) && 
$img->userCan( File::DELETED_FILE ) ) {
                                        return $img;
-                               } elseif ( !empty( $options['private'] ) && 
$img->userCan(File::DELETED_FILE) ) {
-                                       return $img;
                                }
                        }
                }

Modified: trunk/phase3/includes/filerepo/LocalFile.php
===================================================================
--- trunk/phase3/includes/filerepo/LocalFile.php        2011-10-19 03:20:19 UTC 
(rev 100210)
+++ trunk/phase3/includes/filerepo/LocalFile.php        2011-10-19 04:06:16 UTC 
(rev 100211)
@@ -95,22 +95,21 @@
         * Create a LocalFile from a SHA-1 key
         * Do not call this except from inside a repo class.
         *
-        * @param $sha1 string
+        * @param $sha1 string base-36 SHA-1
         * @param $repo LocalRepo
-        * @param $timestamp string
+        * @param string|bool $timestamp MW_timestamp (optional)
         *
-        * @return LocalFile
+        * @return bool|LocalFile
         */
        static function newFromKey( $sha1, $repo, $timestamp = false ) {
-               $conds = array( 'img_sha1' => $sha1 );
+               $dbr = $repo->getSlaveDB();
 
+               $conds = array( 'img_sha1' => $sha1 );
                if ( $timestamp ) {
-                       $conds['img_timestamp'] = $timestamp;
+                       $conds['img_timestamp'] = $dbr->timestamp( $timestamp );
                }
 
-               $dbr = $repo->getSlaveDB();
                $row = $dbr->selectRow( 'image', self::selectFields(), $conds, 
__METHOD__ );
-
                if ( $row ) {
                        return self::newFromRow( $row, $repo );
                } else {

Modified: trunk/phase3/includes/filerepo/OldLocalFile.php
===================================================================
--- trunk/phase3/includes/filerepo/OldLocalFile.php     2011-10-19 03:20:19 UTC 
(rev 100210)
+++ trunk/phase3/includes/filerepo/OldLocalFile.php     2011-10-19 04:06:16 UTC 
(rev 100211)
@@ -19,8 +19,9 @@
 
        static function newFromTitle( $title, $repo, $time = null ) {
                # The null default value is only here to avoid an E_STRICT
-               if( $time === null )
+               if ( $time === null ) {
                        throw new MWException( __METHOD__.' got null for $time 
parameter' );
+               }
                return new self( $title, $repo, $time, null );
        }
 
@@ -36,19 +37,25 @@
        }
 
        /**
-        * @param  $sha1
+        * Create a OldLocalFile from a SHA-1 key
+        * Do not call this except from inside a repo class.
+        *
+        * @param $sha1 string base-36 SHA-1
         * @param $repo LocalRepo
-        * @param bool $timestamp
+        * @param string|bool $timestamp MW_timestamp (optional)
+        *
         * @return bool|OldLocalFile
         */
        static function newFromKey( $sha1, $repo, $timestamp = false ) {
+               $dbr = $repo->getSlaveDB();
+
                $conds = array( 'oi_sha1' => $sha1 );
-               if( $timestamp ) {
-                       $conds['oi_timestamp'] = $timestamp;
+               if ( $timestamp ) {
+                       $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
                }
-               $dbr = $repo->getSlaveDB();
+
                $row = $dbr->selectRow( 'oldimage', self::selectFields(), 
$conds, __METHOD__ );
-               if( $row ) {
+               if ( $row ) {
                        return self::newFromRow( $row, $repo );
                } else {
                        return false;


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to