r76113 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76112‎ | r76113 | r76114 >
Date:16:31, 5 November 2010
Author:hartman
Status:ok
Tags:
Comment:
InstantCommons path and url cleanup
* Add getZonePath
* Return uri encoded URLs for cached thumbnails
* Check validity of deduced filename
* url and path cleanup.
Modified paths:
  • /trunk/phase3/includes/filerepo/ForeignAPIRepo.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/filerepo/ForeignAPIRepo.php
@@ -8,8 +8,6 @@
99
1010 /**
1111 * A foreign repository with a remote MediaWiki with an API thingy
12 - * Very hacky and inefficient
13 - * do not use except for testing :D
1412 *
1513 * Example config:
1614 *
@@ -29,6 +27,9 @@
3028 var $apiThumbCacheExpiry = 86400;
3129 /* Redownload thumbnail files after a month */
3230 var $fileCacheExpiry = 2629743;
 31+ /* Local image directory */
 32+ var $directory;
 33+ var $thumbDir;
3334
3435 protected $mQueryCache = array();
3536 protected $mFileExists = array();
@@ -38,10 +39,14 @@
3940
4041 // http://commons.wikimedia.org/w/api.php
4142 $this->mApiBase = isset( $info['apibase'] ) ? $info['apibase'] : null;
 43+ $this->directory = isset( $info['directory'] ) ? $info['directory'] : $wgUploadDirectory;
4244
4345 if( isset( $info['apiThumbCacheExpiry'] ) ) {
4446 $this->apiThumbCacheExpiry = $info['apiThumbCacheExpiry'];
4547 }
 48+ if( isset( $info['fileCacheExpiry'] ) ) {
 49+ $this->fileCacheExpiry = $info['fileCacheExpiry'];
 50+ }
4651 if( !$this->scriptDirUrl ) {
4752 // hack for description fetches
4853 $this->scriptDirUrl = dirname( $this->mApiBase );
@@ -54,6 +59,11 @@
5560 if( $this->canCacheThumbs() && !$this->thumbUrl ) {
5661 $this->thumbUrl = $this->url . '/thumb';
5762 }
 63+ if ( isset( $info['thumbDir'] ) ) {
 64+ $this->thumbDir = $info['thumbDir'];
 65+ } else {
 66+ $this->thumbDir = "{$this->directory}/thumb";
 67+ }
5868 }
5969
6070 /**
@@ -209,12 +219,11 @@
210220 * @param $height
211221 */
212222 function getThumbUrlFromCache( $name, $width, $height ) {
213 - global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
 223+ global $wgMemc;
214224
215225 if ( !$this->canCacheThumbs() ) {
216226 return $this->getThumbUrl( $name, $width, $height );
217227 }
218 -
219228 $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name, $width );
220229
221230 $thumbUrl = $wgMemc->get($key);
@@ -225,44 +234,49 @@
226235 else {
227236 $metadata = null;
228237 $foreignUrl = $this->getThumbUrl( $name, $width, $height, $metadata );
229 - // We need the same filename as the remote one :)
230 - $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) );
231 - $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/";
232 - $localFilename = $wgUploadDirectory . '/' . $path . $fileName;
233 - $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName;
234238
235239 if( !$foreignUrl ) {
236240 wfDebug( __METHOD__ . " Could not find thumburl\n" );
237241 return false;
238242 }
 243+
 244+ // We need the same filename as the remote one :)
 245+ $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) );
 246+ if( !$this->validateFilename( $fileName ) ) {
 247+ wfDebug( __METHOD__ . " The deduced filename $fileName is not safe\n" );
 248+ return false;
 249+ }
 250+ $localPath = $this->getZonePath( 'thumb' ) . "/" . $this->getHashPath( $name ) . $name;
 251+ $localFilename = $localPath . "/" . $fileName;
 252+ $localUrl = $this->getZoneUrl( 'thumb' ) . "/" . $this->getHashPath( $name ) . urlencode( $name ) . "/" . urlencode( $fileName );
 253+
239254 if( file_exists( $localFilename ) && isset( $metadata['timestamp'] ) ) {
240255 wfDebug( __METHOD__ . " Thumbnail was already downloaded before\n" );
241256 $modified = filemtime( $localFilename );
242257 $remoteModified = strtotime( $metadata['timestamp'] );
243258 $diff = abs( $modified - $remoteModified );
244259 if( $remoteModified < $modified && $diff < $this->fileCacheExpiry ) {
245 - /* Use our current already downloaded thumbnail */
 260+ /* Use our current and already downloaded thumbnail */
246261 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry );
247262 return $localUrl;
248263 }
249264 /* There is a new Commons file, or existing thumbnail older than a month */
250 -
251265 }
252266 $thumb = Http::get( $foreignUrl );
253267 if( !$thumb ) {
254268 wfDebug( __METHOD__ . " Could not download thumb\n" );
255269 return false;
256270 }
257 - if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
258 - if( !wfMkdirParents($wgUploadDirectory . '/' . $path) ) {
259 - wfDebug( __METHOD__ . " could not create directory for thumb\n" );
 271+ if ( !is_dir($localPath) ) {
 272+ if( !wfMkdirParents($localPath) ) {
 273+ wfDebug( __METHOD__ . " could not create directory $localPath for thumb\n" );
260274 return $foreignUrl;
261275 }
262276 }
263277
264278 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
265279 wfSuppressWarnings();
266 - if( !file_put_contents($localFilename, $thumb ) ) {
 280+ if( !file_put_contents( $localFilename, $thumb ) ) {
267281 wfRestoreWarnings();
268282 wfDebug( __METHOD__ . " could not write to thumb path\n" );
269283 return $foreignUrl;
@@ -289,6 +303,20 @@
290304 }
291305
292306 /**
 307+ * Get the local directory corresponding to one of the three basic zones
 308+ */
 309+ function getZonePath( $zone ) {
 310+ switch ( $zone ) {
 311+ case 'public':
 312+ return $this->directory;
 313+ case 'thumb':
 314+ return $this->thumbDir;
 315+ default:
 316+ return false;
 317+ }
 318+ }
 319+
 320+ /**
293321 * Are we locally caching the thumbnails?
294322 * @return bool
295323 */

Follow-up revisions

RevisionCommit summaryAuthorDate
r76118Follow-up r76113, r76114: undeclared global, unnecessary global + some whites...nikerabbit18:01, 5 November 2010

Status & tagging log