56 protected $fileFactory = [ ForeignAPIFile::class,
'newFromTitle' ];
74 parent::__construct( $info );
77 $this->mApiBase = $info[
'apibase'] ??
null;
79 if ( isset( $info[
'apiThumbCacheExpiry'] ) ) {
80 $this->apiThumbCacheExpiry = $info[
'apiThumbCacheExpiry'];
82 if ( isset( $info[
'fileCacheExpiry'] ) ) {
83 $this->fileCacheExpiry = $info[
'fileCacheExpiry'];
85 if ( !$this->scriptDirUrl ) {
87 $this->scriptDirUrl = dirname( $this->mApiBase );
94 $this->thumbUrl = $this->url .
'/thumb';
119 return parent::newFile(
$title, $time );
128 foreach ( $files as $k => $f ) {
129 if ( isset( $this->mFileExists[$f] ) ) {
130 $results[$k] = $this->mFileExists[$f];
132 } elseif ( self::isVirtualUrl( $f ) ) {
133 # @todo FIXME: We need to be able to handle virtual
134 # URLs better, at least when we know they refer to the
136 $results[$k] =
false;
139 $results[$k] =
false;
141 wfWarn(
"Got mwstore:// path '$f'." );
146 'titles' => implode(
'|', $files ),
147 'prop' =>
'imageinfo' ]
150 if ( isset( $data[
'query'][
'pages'] ) ) {
151 # First, get results from the query. Note we only care whether the image exists,
152 # not whether it has a description page.
153 foreach ( $data[
'query'][
'pages'] as $p ) {
154 $this->mFileExists[$p[
'title']] = ( $p[
'imagerepository'] !==
'' );
156 # Second, copy the results to any redirects that were queried
157 if ( isset( $data[
'query'][
'redirects'] ) ) {
158 foreach ( $data[
'query'][
'redirects'] as $r ) {
159 $this->mFileExists[$r[
'from']] = $this->mFileExists[$r[
'to']];
162 # Third, copy the results to any non-normalized titles that were queried
163 if ( isset( $data[
'query'][
'normalized'] ) ) {
164 foreach ( $data[
'query'][
'normalized'] as $n ) {
165 $this->mFileExists[$n[
'from']] = $this->mFileExists[$n[
'to']];
168 # Finally, copy the results to the output
169 foreach ( $files as $key =>
$file ) {
170 $results[$key] = $this->mFileExists[
$file];
192 $query = array_merge( $query,
196 'redirects' =>
'true'
199 if ( !isset( $query[
'uselang'] ) ) {
217 if ( $data && isset( $data[
'query'][
'pages'] ) ) {
218 foreach ( $data[
'query'][
'pages'] as $info ) {
219 if ( isset( $info[
'imageinfo'][0] ) ) {
220 $return = $info[
'imageinfo'][0];
221 if ( isset( $info[
'pageid'] ) ) {
222 $return[
'pageid'] = $info[
'pageid'];
238 'aisha1base36' => $hash,
240 'list' =>
'allimages',
243 if ( isset( $results[
'query'][
'allimages'] ) ) {
244 foreach ( $results[
'query'][
'allimages'] as $img ) {
246 if ( !isset( $img[
'name'] ) ) {
265 function getThumbUrl(
$name, $width = -1, $height = -1, &$result =
null, $otherParams =
'' ) {
267 'titles' =>
'File:' .
$name,
268 'iiprop' => self::getIIProps(),
269 'iiurlwidth' => $width,
270 'iiurlheight' => $height,
271 'iiurlparam' => $otherParams,
272 'prop' =>
'imageinfo' ] );
275 if ( $data && $info && isset( $info[
'thumburl'] ) ) {
276 wfDebug( __METHOD__ .
" got remote thumb " . $info[
'thumburl'] .
"\n" );
279 return $info[
'thumburl'];
296 'titles' =>
'File:' .
$name,
297 'iiprop' => self::getIIProps(),
298 'iiurlwidth' => $width,
299 'iiurlheight' => $height,
300 'iiurlparam' => $otherParams,
301 'prop' =>
'imageinfo',
306 if ( $data && $info && isset( $info[
'thumberror'] ) ) {
307 wfDebug( __METHOD__ .
" got remote thumb error " . $info[
'thumberror'] .
"\n" );
310 'thumbnail_error_remote',
344 $sizekey =
"$width:$height:$params";
347 $knownThumbUrls = $this->wanCache->get( $key );
348 if ( !$knownThumbUrls ) {
350 $knownThumbUrls = [];
351 } elseif ( isset( $knownThumbUrls[$sizekey] ) ) {
352 wfDebug( __METHOD__ .
': Got thumburl from local cache: ' .
353 "{$knownThumbUrls[$sizekey]} \n" );
355 return $knownThumbUrls[$sizekey];
359 $foreignUrl = $this->
getThumbUrl(
$name, $width, $height, $metadata, $params );
361 if ( !$foreignUrl ) {
362 wfDebug( __METHOD__ .
" Could not find thumburl\n" );
368 $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) );
370 wfDebug( __METHOD__ .
" The deduced filename $fileName is not safe\n" );
375 $localFilename = $localPath .
"/" . $fileName;
377 rawurlencode(
$name ) .
"/" . rawurlencode( $fileName );
380 && isset( $metadata[
'timestamp'] )
382 wfDebug( __METHOD__ .
" Thumbnail was already downloaded before\n" );
384 $remoteModified = strtotime( $metadata[
'timestamp'] );
386 $diff = abs( $modified - $current );
387 if ( $remoteModified < $modified && $diff < $this->fileCacheExpiry ) {
389 $knownThumbUrls[$sizekey] = $localUrl;
390 $this->wanCache->set( $key, $knownThumbUrls, $this->apiThumbCacheExpiry );
397 $thumb =
self::httpGet( $foreignUrl,
'default', [], $mtime );
399 wfDebug( __METHOD__ .
" Could not download thumb\n" );
404 # @todo FIXME: Delete old thumbs that aren't being used. Maintenance script?
406 $params = [
'dst' => $localFilename,
'content' => $thumb ];
408 wfDebug( __METHOD__ .
" could not write to thumb path '$localFilename'\n" );
412 $knownThumbUrls[$sizekey] = $localUrl;
415 ? $this->wanCache->adaptiveTTL( $mtime, $this->apiThumbCacheExpiry )
417 $this->wanCache->set( $key, $knownThumbUrls, $ttl );
418 wfDebug( __METHOD__ .
" got local thumb $localUrl, saving to cache \n" );
436 return parent::getZoneUrl( $zone,
$ext );
446 $supported = [
'public',
'thumb' ];
447 if ( in_array( $zone, $supported ) ) {
448 return parent::getZonePath( $zone );
459 return ( $this->apiThumbCacheExpiry > 0 );
477 $info = parent::getInfo();
483 'meta' =>
'siteinfo',
484 'siprop' =>
'general',
491 $general = $siteInfo[
'query'][
'general'];
493 $info[
'articlepath'] = $general[
'articlepath'];
494 $info[
'server'] = $general[
'server'];
496 if ( isset( $general[
'favicon'] ) ) {
497 $info[
'favicon'] = $general[
'favicon'];
515 $url, $timeout =
'default', $options = [], &$mtime =
false
517 $options[
'timeout'] = $timeout;
520 wfDebug(
"ForeignAPIRepo: HTTP GET: $url\n" );
521 $options[
'method'] =
"GET";
523 if ( !isset( $options[
'timeout'] ) ) {
524 $options[
'timeout'] =
'default';
528 $req->setUserAgent( self::getUserAgent() );
532 $lmod = $req->getResponseHeader(
'Last-Modified' );
533 $mtime = $lmod ?
wfTimestamp( TS_UNIX, $lmod ) :
false;
535 return $req->getContent();
537 $logger = LoggerFactory::getInstance(
'http' );
539 $status->getWikiText(
false,
false,
'en' ),
540 [
'caller' =>
'ForeignAPIRepo::httpGet' ]
552 return implode(
'|', self::$imageInfoProps );
563 if ( $this->mApiBase ) {
569 return $this->wanCache->getWithSetCallback(
572 function ( $curValue, &$ttl ) use (
$url ) {
574 if ( $html !==
false ) {
575 $ttl = $mtime ? $this->wanCache->adaptiveTTL( $mtime, $ttl ) : $ttl;
577 $ttl = $this->wanCache->adaptiveTTL( $mtime, $ttl );
592 throw new MWException(
'enumFiles is not supported by ' . static::class );
599 throw new MWException( static::class .
': write operations are not supported.' );