50 private const VERSION =
"2.1";
55 private const IMAGE_INFO_PROPS = [
60 protected $fileFactory = [ ForeignAPIFile::class,
'newFromTitle' ];
90 $localFileRepo = MediaWikiServices::getInstance()->getMainConfig()
91 ->get( MainConfigNames::LocalFileRepo );
92 parent::__construct( $info );
95 $this->mApiBase = $info[
'apibase'] ??
null;
97 if ( isset( $info[
'apiThumbCacheExpiry'] ) ) {
98 $this->apiThumbCacheExpiry = $info[
'apiThumbCacheExpiry'];
100 if ( isset( $info[
'fileCacheExpiry'] ) ) {
101 $this->fileCacheExpiry = $info[
'fileCacheExpiry'];
103 if ( isset( $info[
'apiMetadataExpiry'] ) ) {
104 $this->apiMetadataExpiry = $info[
'apiMetadataExpiry'];
106 if ( !$this->scriptDirUrl ) {
108 $this->scriptDirUrl = dirname( $this->mApiBase );
112 $this->url = $localFileRepo[
'url'];
115 $this->thumbUrl = $this->url .
'/thumb';
127 public function newFile( $title, $time =
false ) {
132 return parent::newFile( $title, $time );
141 foreach ( $files as $k => $f ) {
142 if ( isset( $this->mFileExists[$f] ) ) {
143 $results[$k] = $this->mFileExists[$f];
145 } elseif ( self::isVirtualUrl( $f ) ) {
146 # @todo FIXME: We need to be able to handle virtual
147 # URLs better, at least when we know they refer to the
149 $results[$k] =
false;
151 } elseif ( FileBackend::isStoragePath( $f ) ) {
152 $results[$k] =
false;
154 wfWarn(
"Got mwstore:// path '$f'." );
159 'titles' => implode(
'|', $files ),
160 'prop' =>
'imageinfo' ]
163 if ( isset( $data[
'query'][
'pages'] ) ) {
164 # First, get results from the query. Note we only care whether the image exists,
165 # not whether it has a description page.
166 foreach ( $data[
'query'][
'pages'] as $p ) {
167 $this->mFileExists[$p[
'title']] = ( $p[
'imagerepository'] !==
'' );
169 # Second, copy the results to any redirects that were queried
170 if ( isset( $data[
'query'][
'redirects'] ) ) {
171 foreach ( $data[
'query'][
'redirects'] as $r ) {
172 $this->mFileExists[$r[
'from']] = $this->mFileExists[$r[
'to']];
175 # Third, copy the results to any non-normalized titles that were queried
176 if ( isset( $data[
'query'][
'normalized'] ) ) {
177 foreach ( $data[
'query'][
'normalized'] as $n ) {
178 $this->mFileExists[$n[
'from']] = $this->mFileExists[$n[
'to']];
181 # Finally, copy the results to the output
182 foreach ( $files as $key => $file ) {
183 $results[$key] = $this->mFileExists[$file];
205 $languageCode = MediaWikiServices::getInstance()->getMainConfig()
206 ->get( MainConfigNames::LanguageCode );
208 $query = array_merge( $query,
212 'redirects' =>
'true'
215 if ( !isset( $query[
'uselang'] ) ) {
216 $query[
'uselang'] = $languageCode;
219 $data = $this->
httpGetCached(
'Metadata', $query, $this->apiMetadataExpiry );
222 return FormatJson::decode( $data,
true );
233 if ( $data && isset( $data[
'query'][
'pages'] ) ) {
234 foreach ( $data[
'query'][
'pages'] as $info ) {
235 if ( isset( $info[
'imageinfo'][0] ) ) {
236 $return = $info[
'imageinfo'][0];
237 if ( isset( $info[
'pageid'] ) ) {
238 $return[
'pageid'] = $info[
'pageid'];
254 'aisha1base36' => $hash,
256 'list' =>
'allimages',
259 if ( isset( $results[
'query'][
'allimages'] ) ) {
260 foreach ( $results[
'query'][
'allimages'] as $img ) {
262 if ( !isset( $img[
'name'] ) ) {
281 private function getThumbUrl(
282 $name, $width = -1, $height = -1, &$result =
null, $otherParams =
''
285 'titles' =>
'File:' .
$name,
286 'iiprop' => self::getIIProps(),
287 'iiurlwidth' => $width,
288 'iiurlheight' => $height,
289 'iiurlparam' => $otherParams,
290 'prop' =>
'imageinfo' ] );
293 if ( $data && $info && isset( $info[
'thumburl'] ) ) {
294 wfDebug( __METHOD__ .
" got remote thumb " . $info[
'thumburl'] );
297 return $info[
'thumburl'];
313 $name, $width = -1, $height = -1, $otherParams =
'', $lang =
null
316 'titles' =>
'File:' .
$name,
317 'iiprop' => self::getIIProps(),
318 'iiurlwidth' => $width,
319 'iiurlheight' => $height,
320 'iiurlparam' => $otherParams,
321 'prop' =>
'imageinfo',
326 if ( $data && $info && isset( $info[
'thumberror'] ) ) {
327 wfDebug( __METHOD__ .
" got remote thumb error " . $info[
'thumberror'] );
330 'thumbnail_error_remote',
362 return $this->getThumbUrl(
$name, $width, $height, $result,
$params );
366 $sizekey =
"$width:$height:$params";
369 $knownThumbUrls = $this->wanCache->get( $key );
370 if ( !$knownThumbUrls ) {
372 $knownThumbUrls = [];
373 } elseif ( isset( $knownThumbUrls[$sizekey] ) ) {
374 wfDebug( __METHOD__ .
': Got thumburl from local cache: ' .
375 "{$knownThumbUrls[$sizekey]}" );
377 return $knownThumbUrls[$sizekey];
381 $foreignUrl = $this->getThumbUrl(
$name, $width, $height, $metadata,
$params );
383 if ( !$foreignUrl ) {
384 wfDebug( __METHOD__ .
" Could not find thumburl" );
390 $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) );
392 wfDebug( __METHOD__ .
" The deduced filename $fileName is not safe" );
397 $localFilename = $localPath .
"/" . $fileName;
399 rawurlencode(
$name ) .
"/" . rawurlencode( $fileName );
402 && isset( $metadata[
'timestamp'] )
404 wfDebug( __METHOD__ .
" Thumbnail was already downloaded before" );
406 $remoteModified = (int)
wfTimestamp( TS_UNIX, $metadata[
'timestamp'] );
408 $diff = abs( $modified - $current );
409 if ( $remoteModified < $modified && $diff < $this->fileCacheExpiry ) {
411 $knownThumbUrls[$sizekey] = $localUrl;
412 $this->wanCache->set( $key, $knownThumbUrls, $this->apiThumbCacheExpiry );
419 $thumb =
self::httpGet( $foreignUrl,
'default', [], $mtime );
421 wfDebug( __METHOD__ .
" Could not download thumb" );
426 # @todo FIXME: Delete old thumbs that aren't being used. Maintenance script?
428 $params = [
'dst' => $localFilename,
'content' => $thumb ];
430 wfDebug( __METHOD__ .
" could not write to thumb path '$localFilename'" );
434 $knownThumbUrls[$sizekey] = $localUrl;
437 ? $this->wanCache->adaptiveTTL( $mtime, $this->apiThumbCacheExpiry )
439 $this->wanCache->set( $key, $knownThumbUrls, $ttl );
440 wfDebug( __METHOD__ .
" got local thumb $localUrl, saving to cache" );
458 return parent::getZoneUrl( $zone, $ext );
468 $supported = [
'public',
'thumb' ];
469 if ( in_array( $zone, $supported ) ) {
470 return parent::getZonePath( $zone );
481 return ( $this->apiThumbCacheExpiry > 0 );
489 return MediaWikiServices::getInstance()->getHttpRequestFactory()->getUserAgent() .
490 " ForeignAPIRepo/" . self::VERSION;
500 $info = parent::getInfo();
501 $info[
'apiurl'] = $this->mApiBase;
506 'meta' =>
'siteinfo',
507 'siprop' =>
'general',
513 $siteInfo = FormatJson::decode( $data,
true );
514 $general = $siteInfo[
'query'][
'general'];
516 $info[
'articlepath'] = $general[
'articlepath'];
517 $info[
'server'] = $general[
'server'];
518 if ( !isset( $info[
'favicon'] ) && isset( $general[
'favicon'] ) ) {
519 $info[
'favicon'] = $general[
'favicon'];
534 $url, $timeout =
'default', $options = [], &$mtime =
false
536 $options[
'timeout'] = $timeout;
537 $url = MediaWikiServices::getInstance()->getUrlUtils()
539 wfDebug(
"ForeignAPIRepo: HTTP GET: $url" );
543 $options[
'method'] =
"GET";
545 if ( !isset( $options[
'timeout'] ) ) {
546 $options[
'timeout'] =
'default';
551 $req = MediaWikiServices::getInstance()->getHttpRequestFactory()
552 ->create(
$url, $options, __METHOD__ );
553 $status = $req->execute();
555 if ( $status->isOK() ) {
556 $lmod = $req->getResponseHeader(
'Last-Modified' );
557 $mtime = $lmod ? (int)
wfTimestamp( TS_UNIX, $lmod ) :
false;
559 return $req->getContent();
561 $logger = LoggerFactory::getInstance(
'http' );
563 $status->getWikiText(
false,
false,
'en' ),
564 [
'caller' =>
'ForeignAPIRepo::httpGet' ]
576 return implode(
'|', self::IMAGE_INFO_PROPS );
587 if ( $this->mApiBase ) {
593 return $this->wanCache->getWithSetCallback(
597 $this->wanCache->makeGlobalKey(
"filerepo-$attribute", sha1(
$url ) ),
599 function ( $curValue, &$ttl ) use (
$url ) {
603 if ( $html !==
false ) {
604 $ttl = $mtime ? $this->wanCache->adaptiveTTL( $mtime, $ttl ) : $ttl;
606 $ttl = $this->wanCache->adaptiveTTL( $mtime, $ttl );
612 [
'pcGroup' =>
'http-get:3',
'pcTTL' => WANObjectCache::TTL_PROC_LONG ]
621 throw new RuntimeException(
'enumFiles is not supported by ' . static::class );
628 throw new LogicException( static::class .
': write operations are not supported.' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
array $params
The job parameters.
Base class for file repositories.
getDisplayName()
Get the human-readable name of the repo.
getLocalCacheKey( $kClassSuffix,... $components)
Get a site-local, repository-qualified, WAN cache key.
makeUrl( $query='', $entry='index')
Make an url to this repo.
string false $url
Public zone URL.
validateFilename( $filename)
Determine if a relative path is valid, i.e.
string false $thumbUrl
The base thumbnail URL.
getHashPath( $name)
Get a relative path including trailing slash, e.g.
getBackend()
Get the file backend instance.
Foreign file accessible through api.php requests.
static getProps()
Get the property string for iiprop and aiprop.
A foreign repository for a remote MediaWiki accessible through api.php requests.
int $fileCacheExpiry
Redownload thumbnail files after this expiry.
newFile( $title, $time=false)
Per docs in FileRepo, this needs to return false if we don't support versioned files.
int $apiMetadataExpiry
API metadata cache time.
static httpGet( $url, $timeout='default', $options=[], &$mtime=false)
getInfo()
Get information about the repo - overrides/extends the parent class's information.
static getUserAgent()
The user agent the ForeignAPIRepo will use.
fetchImageQuery( $query)
Make an API query in the foreign repo, caching results.
getThumbError( $name, $width=-1, $height=-1, $otherParams='', $lang=null)
canCacheThumbs()
Are we locally caching the thumbnails?
int $apiThumbCacheExpiry
Check back with Commons after this expiry.
getFileProps( $virtualUrl)
getThumbUrlFromCache( $name, $width, $height, $params="")
Return the imageurl from cache if possible.
fileExistsBatch(array $files)
getZoneUrl( $zone, $ext=null)
getZonePath( $zone)
Get the local directory corresponding to one of the basic zones.
httpGetCached( $attribute, $query, $cacheTTL=3600)
HTTP GET request to a mediawiki API (with caching)
A class containing constants representing the names of configuration variables.
A foreign repo that implement support for API queries.
Interface for objects (potentially) representing an editable wiki page.