r100211 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100210‎ | r100211 | r100212 >
Date:04:06, 19 October 2011
Author:aaron
Status:ok (Comments)
Tags:
Comment:
* 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 (modified) (history)
  • /trunk/phase3/includes/filerepo/LocalFile.php (modified) (history)
  • /trunk/phase3/includes/filerepo/OldLocalFile.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/filerepo/OldLocalFile.php
@@ -19,8 +19,9 @@
2020
2121 static function newFromTitle( $title, $repo, $time = null ) {
2222 # The null default value is only here to avoid an E_STRICT
23 - if( $time === null )
 23+ if ( $time === null ) {
2424 throw new MWException( __METHOD__.' got null for $time parameter' );
 25+ }
2526 return new self( $title, $repo, $time, null );
2627 }
2728
@@ -36,19 +37,25 @@
3738 }
3839
3940 /**
40 - * @param $sha1
 41+ * Create a OldLocalFile from a SHA-1 key
 42+ * Do not call this except from inside a repo class.
 43+ *
 44+ * @param $sha1 string base-36 SHA-1
4145 * @param $repo LocalRepo
42 - * @param bool $timestamp
 46+ * @param string|bool $timestamp MW_timestamp (optional)
 47+ *
4348 * @return bool|OldLocalFile
4449 */
4550 static function newFromKey( $sha1, $repo, $timestamp = false ) {
 51+ $dbr = $repo->getSlaveDB();
 52+
4653 $conds = array( 'oi_sha1' => $sha1 );
47 - if( $timestamp ) {
48 - $conds['oi_timestamp'] = $timestamp;
 54+ if ( $timestamp ) {
 55+ $conds['oi_timestamp'] = $dbr->timestamp( $timestamp );
4956 }
50 - $dbr = $repo->getSlaveDB();
 57+
5158 $row = $dbr->selectRow( 'oldimage', self::selectFields(), $conds, __METHOD__ );
52 - if( $row ) {
 59+ if ( $row ) {
5360 return self::newFromRow( $row, $repo );
5461 } else {
5562 return false;
Index: trunk/phase3/includes/filerepo/LocalFile.php
@@ -95,22 +95,21 @@
9696 * Create a LocalFile from a SHA-1 key
9797 * Do not call this except from inside a repo class.
9898 *
99 - * @param $sha1 string
 99+ * @param $sha1 string base-36 SHA-1
100100 * @param $repo LocalRepo
101 - * @param $timestamp string
 101+ * @param string|bool $timestamp MW_timestamp (optional)
102102 *
103 - * @return LocalFile
 103+ * @return bool|LocalFile
104104 */
105105 static function newFromKey( $sha1, $repo, $timestamp = false ) {
106 - $conds = array( 'img_sha1' => $sha1 );
 106+ $dbr = $repo->getSlaveDB();
107107
 108+ $conds = array( 'img_sha1' => $sha1 );
108109 if ( $timestamp ) {
109 - $conds['img_timestamp'] = $timestamp;
 110+ $conds['img_timestamp'] = $dbr->timestamp( $timestamp );
110111 }
111112
112 - $dbr = $repo->getSlaveDB();
113113 $row = $dbr->selectRow( 'image', self::selectFields(), $conds, __METHOD__ );
114 -
115114 if ( $row ) {
116115 return self::newFromRow( $row, $repo );
117116 } else {
Index: trunk/phase3/includes/filerepo/FileRepo.php
@@ -74,7 +74,7 @@
7575 * @return File
7676 */
7777 function newFile( $title, $time = false ) {
78 - if ( !($title instanceof Title) ) {
 78+ if ( !( $title instanceof Title ) ) {
7979 $title = Title::makeTitleSafe( NS_FILE, $title );
8080 if ( !is_object( $title ) ) {
8181 return null;
@@ -130,10 +130,10 @@
131131 if ( $time !== false ) {
132132 $img = $this->newFile( $title, $time );
133133 if ( $img && $img->exists() ) {
134 - if ( !$img->isDeleted(File::DELETED_FILE) ) {
 134+ if ( !$img->isDeleted( File::DELETED_FILE ) ) {
 135+ return $img; // always OK
 136+ } elseif ( !empty( $options['private'] ) && $img->userCan( File::DELETED_FILE ) ) {
135137 return $img;
136 - } elseif ( !empty( $options['private'] ) && $img->userCan(File::DELETED_FILE) ) {
137 - return $img;
138138 }
139139 }
140140 }
@@ -187,30 +187,6 @@
188188 }
189189
190190 /**
191 - * Create a new File object from the local repository
192 - * @param $sha1 Mixed: base 36 SHA-1 hash
193 - * @param $time Mixed: time at which the image was uploaded.
194 - * If this is specified, the returned object will be an
195 - * of the repository's old file class instead of a current
196 - * file. Repositories not supporting version control should
197 - * return false if this parameter is set.
198 - *
199 - * @return File
200 - */
201 - function newFileFromKey( $sha1, $time = false ) {
202 - if ( $time ) {
203 - if ( $this->oldFileFactoryKey ) {
204 - return call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
205 - }
206 - } else {
207 - if ( $this->fileFactoryKey ) {
208 - return call_user_func( $this->fileFactoryKey, $sha1, $this );
209 - }
210 - }
211 - return false;
212 - }
213 -
214 - /**
215191 * Find an instance of the file with this key, created at the specified time
216192 * Returns false if the file does not exist. Repositories not supporting
217193 * version control should return false if the time is specified.
@@ -221,22 +197,23 @@
222198 function findFileFromKey( $sha1, $options = array() ) {
223199 $time = isset( $options['time'] ) ? $options['time'] : false;
224200
225 - # First try the current version of the file to see if it precedes the timestamp
226 - $img = $this->newFileFromKey( $sha1 );
227 - if ( !$img ) {
228 - return false;
 201+ # First try to find a matching current version of a file...
 202+ if ( $this->fileFactoryKey ) {
 203+ $img = call_user_func( $this->fileFactoryKey, $sha1, $this, $time );
 204+ } else {
 205+ return false; // find-by-sha1 not supported
229206 }
230 - if ( $img->exists() && ( !$time || $img->getTimestamp() == $time ) ) {
 207+ if ( $img && $img->exists() ) {
231208 return $img;
232209 }
233 - # Now try an old version of the file
234 - if ( $time !== false ) {
235 - $img = $this->newFileFromKey( $sha1, $time );
 210+ # Now try to find a matching old version of a file...
 211+ if ( $time !== false && $this->oldFileFactoryKey ) { // find-by-sha1 supported?
 212+ $img = call_user_func( $this->oldFileFactoryKey, $sha1, $this, $time );
236213 if ( $img && $img->exists() ) {
237 - if ( !$img->isDeleted(File::DELETED_FILE) ) {
 214+ if ( !$img->isDeleted( File::DELETED_FILE ) ) {
 215+ return $img; // always OK
 216+ } elseif ( !empty( $options['private'] ) && $img->userCan( File::DELETED_FILE ) ) {
238217 return $img;
239 - } elseif ( !empty( $options['private'] ) && $img->userCan(File::DELETED_FILE) ) {
240 - return $img;
241218 }
242219 }
243220 }

Sign-offs

UserFlagDate
Reedyinspected22:39, 28 October 2011

Follow-up revisions

RevisionCommit summaryAuthorDate
r101227MFT r100211 to fix bug 31962aaron22:44, 28 October 2011
r102525REL1_18 MFT r100211, r100575reedy16:06, 9 November 2011

Comments

#Comment by Aaron Schulz (talk | contribs)   22:29, 28 October 2011

Also, newFileFromKey() had no outside callers.

Status & tagging log