00001 <?php
00002
00020 class ForeignAPIRepo extends FileRepo {
00021 var $fileFactory = array( 'ForeignAPIFile', 'newFromTitle' );
00022 var $apiThumbCacheExpiry = 0;
00023 protected $mQueryCache = array();
00024
00025 function __construct( $info ) {
00026 parent::__construct( $info );
00027 $this->mApiBase = $info['apibase'];
00028 if( !$this->scriptDirUrl ) {
00029
00030 $this->scriptDirUrl = dirname( $this->mApiBase );
00031 }
00032 }
00033
00038 function newFile( $title, $time = false ) {
00039 if ( $time ) {
00040 return false;
00041 }
00042 return parent::newFile( $title, $time );
00043 }
00044
00048 function storeBatch( $triplets, $flags = 0 ) {
00049 return false;
00050 }
00051 function storeTemp( $originalName, $srcPath ) {
00052 return false;
00053 }
00054 function publishBatch( $triplets, $flags = 0 ) {
00055 return false;
00056 }
00057 function deleteBatch( $sourceDestPairs ) {
00058 return false;
00059 }
00060 function getFileProps( $virtualUrl ) {
00061 return false;
00062 }
00063
00064 protected function queryImage( $query ) {
00065 $data = $this->fetchImageQuery( $query );
00066
00067 if( isset( $data['query']['pages'] ) ) {
00068 foreach( $data['query']['pages'] as $pageid => $info ) {
00069 if( isset( $info['imageinfo'][0] ) ) {
00070 return $info['imageinfo'][0];
00071 }
00072 }
00073 }
00074 return false;
00075 }
00076
00077 protected function fetchImageQuery( $query ) {
00078 global $wgMemc;
00079
00080 $url = $this->mApiBase .
00081 '?' .
00082 wfArrayToCgi(
00083 array_merge( $query,
00084 array(
00085 'format' => 'json',
00086 'action' => 'query' ) ) );
00087
00088 if( !isset( $this->mQueryCache[$url] ) ) {
00089 $key = wfMemcKey( 'ForeignAPIRepo', 'Metadata', md5( $url ) );
00090 $data = $wgMemc->get( $key );
00091 if( !$data ) {
00092 $data = Http::get( $url );
00093 if ( !$data ) {
00094 return null;
00095 }
00096 $wgMemc->set( $key, $data, 3600 );
00097 }
00098
00099 if( count( $this->mQueryCache ) > 100 ) {
00100
00101 $this->mQueryCache = array();
00102 }
00103 $this->mQueryCache[$url] = $data;
00104 }
00105 return json_decode( $this->mQueryCache[$url], true );
00106 }
00107
00108 function getImageInfo( $title, $time = false ) {
00109 return $this->queryImage( array(
00110 'titles' => 'Image:' . $title->getText(),
00111 'iiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
00112 'prop' => 'imageinfo' ) );
00113 }
00114
00115 function findBySha1( $hash ) {
00116 $results = $this->fetchImageQuery( array(
00117 'aisha1base36' => $hash,
00118 'aiprop' => 'timestamp|user|comment|url|size|sha1|metadata|mime',
00119 'list' => 'allimages', ) );
00120 $ret = array();
00121 if ( isset( $results['query']['allimages'] ) ) {
00122 foreach ( $results['query']['allimages'] as $img ) {
00123 $ret[] = new ForeignAPIFile( Title::makeTitle( NS_FILE, $img['name'] ), $this, $img );
00124 }
00125 }
00126 return $ret;
00127 }
00128
00129 function getThumbUrl( $name, $width=-1, $height=-1 ) {
00130 $info = $this->queryImage( array(
00131 'titles' => 'Image:' . $name,
00132 'iiprop' => 'url',
00133 'iiurlwidth' => $width,
00134 'iiurlheight' => $height,
00135 'prop' => 'imageinfo' ) );
00136 if( $info ) {
00137 wfDebug( __METHOD__ . " got remote thumb " . $info['thumburl'] . "\n" );
00138 return $info['thumburl'];
00139 } else {
00140 return false;
00141 }
00142 }
00143
00144 function getThumbUrlFromCache( $name, $width, $height ) {
00145 global $wgMemc, $wgUploadPath, $wgServer, $wgUploadDirectory;
00146
00147 if ( !$this->canCacheThumbs() ) {
00148 return $this->getThumbUrl( $name, $width, $height );
00149 }
00150
00151 $key = wfMemcKey( 'ForeignAPIRepo', 'ThumbUrl', $name );
00152 if ( $thumbUrl = $wgMemc->get($key) ) {
00153 wfDebug("Got thumb from local cache. $thumbUrl \n");
00154 return $thumbUrl;
00155 }
00156 else {
00157 $foreignUrl = $this->getThumbUrl( $name, $width, $height );
00158
00159
00160 $fileName = ltrim( substr( $foreignUrl, strrpos( $foreignUrl, '/' ) ), '/' );
00161 $path = 'thumb/' . $this->getHashPath( $name ) . $name . "/";
00162 if ( !is_dir($wgUploadDirectory . '/' . $path) ) {
00163 wfMkdirParents($wgUploadDirectory . '/' . $path);
00164 }
00165 if ( !is_writable( $wgUploadDirectory . '/' . $path . $fileName ) ) {
00166 wfDebug( __METHOD__ . " could not write to thumb path\n" );
00167 return $foreignUrl;
00168 }
00169 $localUrl = $wgServer . $wgUploadPath . '/' . $path . $fileName;
00170 $thumb = Http::get( $foreignUrl );
00171 # FIXME: Delete old thumbs that aren't being used. Maintenance script?
00172 file_put_contents($wgUploadDirectory . '/' . $path . $fileName, $thumb );
00173 $wgMemc->set( $key, $localUrl, $this->apiThumbCacheExpiry );
00174 wfDebug( __METHOD__ . " got local thumb $localUrl, saving to cache \n" );
00175 return $localUrl;
00176 }
00177 }
00178
00183 public function canCacheThumbs() {
00184 return ( $this->apiThumbCacheExpiry > 0 );
00185 }
00186 }