diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2010-07-28 11:52:48 +0200 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2010-07-28 11:52:48 +0200 |
commit | 222b01f5169f1c7e69762e0e8904c24f78f71882 (patch) | |
tree | 8e932e12546bb991357ec48eb1638d1770be7a35 /includes/filerepo/ForeignAPIRepo.php | |
parent | 00ab76a6b686e98a914afc1975812d2b1aaa7016 (diff) |
update to MediaWiki 1.16.0
Diffstat (limited to 'includes/filerepo/ForeignAPIRepo.php')
-rw-r--r-- | includes/filerepo/ForeignAPIRepo.php | 114 |
1 files changed, 88 insertions, 26 deletions
diff --git a/includes/filerepo/ForeignAPIRepo.php b/includes/filerepo/ForeignAPIRepo.php index e63e4a6b..264cb920 100644 --- a/includes/filerepo/ForeignAPIRepo.php +++ b/includes/filerepo/ForeignAPIRepo.php @@ -19,18 +19,30 @@ */ class ForeignAPIRepo extends FileRepo { var $fileFactory = array( 'ForeignAPIFile', 'newFromTitle' ); - var $apiThumbCacheExpiry = 0; + var $apiThumbCacheExpiry = 86400; protected $mQueryCache = array(); - + protected $mFileExists = array(); + function __construct( $info ) { parent::__construct( $info ); $this->mApiBase = $info['apibase']; // http://commons.wikimedia.org/w/api.php + if( isset( $info['apiThumbCacheExpiry'] ) ) { + $this->apiThumbCacheExpiry = $info['apiThumbCacheExpiry']; + } if( !$this->scriptDirUrl ) { // hack for description fetches $this->scriptDirUrl = dirname( $this->mApiBase ); } + // If we can cache thumbs we can guess sane defaults for these + if( $this->canCacheThumbs() && !$this->url ) { + global $wgLocalFileRepo; + $this->url = $wgLocalFileRepo['url']; + } + if( $this->canCacheThumbs() && !$this->thumbUrl ) { + $this->thumbUrl = $this->url . '/thumb'; + } } - + /** * Per docs in FileRepo, this needs to return false if we don't support versioned * files. Well, we don't. @@ -51,19 +63,49 @@ class ForeignAPIRepo extends FileRepo { function storeTemp( $originalName, $srcPath ) { return false; } + function append( $srcPath, $toAppendPath, $flags = 0 ){ + return false; + } function publishBatch( $triplets, $flags = 0 ) { return false; } function deleteBatch( $sourceDestPairs ) { return false; } + + + function fileExistsBatch( $files, $flags = 0 ) { + $results = array(); + foreach ( $files as $k => $f ) { + if ( isset( $this->mFileExists[$k] ) ) { + $results[$k] = true; + unset( $files[$k] ); + } elseif( self::isVirtualUrl( $f ) ) { + # TODO! FIXME! We need to be able to handle virtual + # URLs better, at least when we know they refer to the + # same repo. + $results[$k] = false; + unset( $files[$k] ); + } + } + + $results = $this->fetchImageQuery( array( 'titles' => implode( $files, '|' ), + 'prop' => 'imageinfo' ) ); + if( isset( $data['query']['pages'] ) ) { + $i = 0; + foreach( $files as $key => $file ) { + $results[$key] = $this->mFileExists[$key] = !isset( $data['query']['pages'][$i]['missing'] ); + $i++; + } + } + } function getFileProps( $virtualUrl ) { return false; } - + protected function queryImage( $query ) { $data = $this->fetchImageQuery( $query ); - + if( isset( $data['query']['pages'] ) ) { foreach( $data['query']['pages'] as $pageid => $info ) { if( isset( $info['imageinfo'][0] ) ) { @@ -73,10 +115,10 @@ class ForeignAPIRepo extends FileRepo { } return false; } - + protected function fetchImageQuery( $query ) { global $wgMemc; - + $url = $this->mApiBase . '?' . wfArrayToCgi( @@ -84,9 +126,9 @@ class ForeignAPIRepo extends FileRepo { array( 'format' => 'json', 'action' => 'query' ) ) ); - + if( !isset( $this->mQueryCache[$url] ) ) { - $key = wfMemcKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) ); + $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) ); $data = $wgMemc->get( $key ); if( !$data ) { $data = Http::get( $url ); @@ -102,16 +144,16 @@ class ForeignAPIRepo extends FileRepo { } $this->mQueryCache[$url] = $data; } - return json_decode( $this->mQueryCache[$url], true ); + return FormatJson::decode( $this->mQueryCache[$url], true ); } - + function getImageInfo( $title, $time = false ) { return $this->queryImage( array( 'titles' => 'Image:' . $title->getText(), 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime', 'prop' => 'imageinfo' ) ); } - + function findBySha1( $hash ) { $results = $this->fetchImageQuery( array( 'aisha1base36' => $hash, @@ -125,7 +167,7 @@ class ForeignAPIRepo extends FileRepo { } return $ret; } - + function getThumbUrl( $name, $width=-1, $height=-1 ) { $info = $this->queryImage( array( 'titles' => 'Image:' . $name, @@ -133,49 +175,69 @@ class ForeignAPIRepo extends FileRepo { 'iiurlwidth' => $width, 'iiurlheight' => $height, 'prop' => 'imageinfo' ) ); - if( $info ) { + if( $info && $info['thumburl'] ) { wfDebug( __METHOD__ . " got remote thumb " . $info['thumburl'] . "\n" ); return $info['thumburl']; } else { return false; } } - + function getThumbUrlFromCache( $name, $width, $height ) { global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory; - + if ( !$this->canCacheThumbs() ) { return $this->getThumbUrl( $name, $width, $height ); } - - $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $name ); + + $key = $this->getLocalCacheKey( 'ForeignAPIRepo', 'ThumbUrl', $name ); if ( $thumbUrl = $wgMemc->get($key) ) { wfDebug("Got thumb from local cache. $thumbUrl \n"); return $thumbUrl; } else { $foreignUrl = $this->getThumbUrl( $name, $width, $height ); - + if( !$foreignUrl ) { + wfDebug( __METHOD__ . " Could not find thumburl\n" ); + return false; + } + $thumb = Http::get( $foreignUrl ); + if( !$thumb ) { + wfDebug( __METHOD__ . " Could not download thumb\n" ); + return false; + } // We need the same filename as the remote one :) - $fileName = ltrim( substr( $foreignUrl, strrpos( $foreignUrl, '/' ) ), '/' ); + $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) ); $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/"; if ( !is_dir($wgUploadDirectory . '/' . $path) ) { wfMkdirParents($wgUploadDirectory . '/' . $path); } - if ( !is_writable( $wgUploadDirectory . '/' . $path . $fileName ) ) { + $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName; + # FIXME: Delete old thumbs that aren't being used. Maintenance script? + if( !file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb ) ) { wfDebug( __METHOD__ . " could not write to thumb path\n" ); return $foreignUrl; } - $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName; - $thumb = Http::get( $foreignUrl ); - # FIXME: Delete old thumbs that aren't being used. Maintenance script? - file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb ); $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry ); wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" ); return $localUrl; } } - + + /** + * @see FileRepo::getZoneUrl() + */ + function getZoneUrl( $zone ) { + switch ( $zone ) { + case 'public': + return $this->url; + case 'thumb': + return $this->thumbUrl; + default: + return parent::getZoneUrl( $zone ); + } + } + /** * Are we locally caching the thumbnails? * @return bool |