Go to the documentation of this file.
53 protected $fileFactory = [ ForeignAPIFile::class,
'newFromTitle' ];
71 parent::__construct( $info );
74 $this->mApiBase = $info[
'apibase'] ??
null;
76 if ( isset( $info[
'apiThumbCacheExpiry'] ) ) {
77 $this->apiThumbCacheExpiry = $info[
'apiThumbCacheExpiry'];
79 if ( isset( $info[
'fileCacheExpiry'] ) ) {
80 $this->fileCacheExpiry = $info[
'fileCacheExpiry'];
82 if ( !$this->scriptDirUrl ) {
84 $this->scriptDirUrl = dirname( $this->mApiBase );
91 $this->thumbUrl = $this->url .
'/thumb';
115 return parent::newFile(
$title, $time );
124 foreach ( $files as $k => $f ) {
125 if ( isset( $this->mFileExists[$f] ) ) {
126 $results[$k] = $this->mFileExists[$f];
128 } elseif ( self::isVirtualUrl( $f ) ) {
129 # @todo FIXME: We need to be able to handle virtual
130 # URLs better, at least when we know they refer to the
132 $results[$k] =
false;
135 $results[$k] =
false;
137 wfWarn(
"Got mwstore:// path '$f'." );
142 'titles' => implode(
'|', $files ),
143 'prop' =>
'imageinfo' ]
146 if ( isset( $data[
'query'][
'pages'] ) ) {
147 # First, get results from the query. Note we only care whether the image exists,
148 # not whether it has a description page.
149 foreach ( $data[
'query'][
'pages'] as $p ) {
150 $this->mFileExists[$p[
'title']] = ( $p[
'imagerepository'] !==
'' );
152 # Second, copy the results to any redirects that were queried
153 if ( isset( $data[
'query'][
'redirects'] ) ) {
154 foreach ( $data[
'query'][
'redirects'] as $r ) {
155 $this->mFileExists[$r[
'from']] = $this->mFileExists[$r[
'to']];
158 # Third, copy the results to any non-normalized titles that were queried
159 if ( isset( $data[
'query'][
'normalized'] ) ) {
160 foreach ( $data[
'query'][
'normalized'] as $n ) {
161 $this->mFileExists[$n[
'from']] = $this->mFileExists[$n[
'to']];
164 # Finally, copy the results to the output
165 foreach ( $files as $key =>
$file ) {
166 $results[$key] = $this->mFileExists[
$file];
188 $query = array_merge( $query,
192 'redirects' =>
'true'
195 if ( !isset( $query[
'uselang'] ) ) {
213 if ( $data && isset( $data[
'query'][
'pages'] ) ) {
214 foreach ( $data[
'query'][
'pages'] as $info ) {
215 if ( isset( $info[
'imageinfo'][0] ) ) {
216 $return = $info[
'imageinfo'][0];
217 if ( isset( $info[
'pageid'] ) ) {
218 $return[
'pageid'] = $info[
'pageid'];
234 'aisha1base36' => $hash,
236 'list' =>
'allimages',
239 if ( isset( $results[
'query'][
'allimages'] ) ) {
240 foreach ( $results[
'query'][
'allimages'] as $img ) {
242 if ( !isset( $img[
'name'] ) ) {
262 $name, $width = -1, $height = -1, &$result =
null, $otherParams =
''
265 'titles' =>
'File:' .
$name,
266 'iiprop' => self::getIIProps(),
267 'iiurlwidth' => $width,
268 'iiurlheight' => $height,
269 'iiurlparam' => $otherParams,
270 'prop' =>
'imageinfo' ] );
273 if ( $data && $info && isset( $info[
'thumburl'] ) ) {
274 wfDebug( __METHOD__ .
" got remote thumb " . $info[
'thumburl'] );
277 return $info[
'thumburl'];
293 $name, $width = -1, $height = -1, $otherParams =
'',
$lang =
null
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'] );
310 'thumbnail_error_remote',
346 $sizekey =
"$width:$height:$params";
349 $knownThumbUrls = $this->wanCache->get( $key );
350 if ( !$knownThumbUrls ) {
352 $knownThumbUrls = [];
353 } elseif ( isset( $knownThumbUrls[$sizekey] ) ) {
354 wfDebug( __METHOD__ .
': Got thumburl from local cache: ' .
355 "{$knownThumbUrls[$sizekey]}" );
357 return $knownThumbUrls[$sizekey];
361 $foreignUrl = $this->
getThumbUrl(
$name, $width, $height, $metadata, $params );
363 if ( !$foreignUrl ) {
364 wfDebug( __METHOD__ .
" Could not find thumburl" );
370 $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) );
372 wfDebug( __METHOD__ .
" The deduced filename $fileName is not safe" );
377 $localFilename = $localPath .
"/" . $fileName;
379 rawurlencode(
$name ) .
"/" . rawurlencode( $fileName );
382 && isset( $metadata[
'timestamp'] )
384 wfDebug( __METHOD__ .
" Thumbnail was already downloaded before" );
386 $remoteModified = strtotime( $metadata[
'timestamp'] );
388 $diff = abs( $modified - $current );
389 if ( $remoteModified < $modified && $diff < $this->fileCacheExpiry ) {
391 $knownThumbUrls[$sizekey] = $localUrl;
392 $this->wanCache->set( $key, $knownThumbUrls, $this->apiThumbCacheExpiry );
399 $thumb =
self::httpGet( $foreignUrl,
'default', [], $mtime );
401 wfDebug( __METHOD__ .
" Could not download thumb" );
406 # @todo FIXME: Delete old thumbs that aren't being used. Maintenance script?
408 $params = [
'dst' => $localFilename,
'content' => $thumb ];
410 wfDebug( __METHOD__ .
" could not write to thumb path '$localFilename'" );
414 $knownThumbUrls[$sizekey] = $localUrl;
417 ? $this->wanCache->adaptiveTTL( $mtime, $this->apiThumbCacheExpiry )
419 $this->wanCache->set( $key, $knownThumbUrls, $ttl );
420 wfDebug( __METHOD__ .
" got local thumb $localUrl, saving to cache" );
438 return parent::getZoneUrl( $zone,
$ext );
448 $supported = [
'public',
'thumb' ];
449 if ( in_array( $zone, $supported ) ) {
450 return parent::getZonePath( $zone );
461 return ( $this->apiThumbCacheExpiry > 0 );
479 $info = parent::getInfo();
485 'meta' =>
'siteinfo',
486 'siprop' =>
'general',
493 $general = $siteInfo[
'query'][
'general'];
495 $info[
'articlepath'] = $general[
'articlepath'];
496 $info[
'server'] = $general[
'server'];
498 if ( isset( $general[
'favicon'] ) ) {
499 $info[
'favicon'] = $general[
'favicon'];
517 $url, $timeout =
'default', $options = [], &$mtime =
false
519 $options[
'timeout'] = $timeout;
522 wfDebug(
"ForeignAPIRepo: HTTP GET: $url" );
523 $options[
'method'] =
"GET";
525 if ( !isset( $options[
'timeout'] ) ) {
526 $options[
'timeout'] =
'default';
530 $req->setUserAgent( self::getUserAgent() );
531 $status = $req->execute();
533 if ( $status->isOK() ) {
534 $lmod = $req->getResponseHeader(
'Last-Modified' );
535 $mtime = $lmod ?
wfTimestamp( TS_UNIX, $lmod ) :
false;
537 return $req->getContent();
539 $logger = LoggerFactory::getInstance(
'http' );
541 $status->getWikiText(
false,
false,
'en' ),
542 [
'caller' =>
'ForeignAPIRepo::httpGet' ]
554 return implode(
'|', self::IMAGE_INFO_PROPS );
565 if ( $this->mApiBase ) {
571 return $this->wanCache->getWithSetCallback(
574 function ( $curValue, &$ttl ) use (
$url ) {
576 if ( $html !==
false ) {
577 $ttl = $mtime ? $this->wanCache->adaptiveTTL( $mtime, $ttl ) : $ttl;
579 $ttl = $this->wanCache->adaptiveTTL( $mtime, $ttl );
585 [
'pcGroup' =>
'http-get:3',
'pcTTL' => WANObjectCache::TTL_PROC_LONG ]
594 throw new MWException(
'enumFiles is not supported by ' . static::class );
601 throw new MWException( static::class .
': write operations are not supported.' );
static httpGet( $url, $timeout='default', $options=[], &$mtime=false)
Like a HttpRequestFactory::get request, but with custom User-Agent.
int $apiThumbCacheExpiry
Check back with Commons after this expiry.
static getUserAgent()
The user agent the ForeignAPIRepo will use.
validateFilename( $filename)
Determine if a relative path is valid, i.e.
if(!isset( $args[0])) $lang
makeUrl( $query='', $entry='index')
Make an url to this repo.
int $fileCacheExpiry
Redownload thumbnail files after this expiry.
$wgLocalFileRepo
File repository structures.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
static userAgent()
A standard user-agent we can use for external requests.
string $thumbUrl
The base thumbnail URL.
const IMAGE_INFO_PROPS
List of iiprop values for the thumbnail fetch queries.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
getThumbUrlFromCache( $name, $width, $height, $params="")
Return the imageurl from cache if possible.
Base class for file repositories.
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
getFileTimestamp(array $params)
Get the last-modified timestamp of the file at a storage path.
static isStoragePath( $path)
Check if a given path is a "mwstore://" path.
getFileProps( $virtualUrl)
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
httpGetCached( $attribute, $query, $cacheTTL=3600)
HTTP GET request to a mediawiki API (with caching)
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
getLocalCacheKey( $kClassSuffix,... $components)
Get a site-local, repository-qualified, WAN cache key.
getDisplayName()
Get the human-readable name of the repo.
getThumbUrl( $name, $width=-1, $height=-1, &$result=null, $otherParams='')
$wgLanguageCode
Site language code.
fileExists(array $params)
Check if a file exists at a storage path in the backend.
fileExistsBatch(array $files)
quickCreate(array $params, array $opts=[])
Performs a single quick create operation.
prepare(array $params)
Prepare a storage directory for usage.
getBackend()
Get the file backend instance.
getZoneUrl( $zone, $ext=null)
getHashPath( $name)
Get a relative path including trailing slash, e.g.
A foreign repository for a remote MediaWiki accessible through api.php requests.
newFile( $title, $time=false)
Per docs in FileRepo, this needs to return false if we don't support versioned files.
getZonePath( $zone)
Get the local directory corresponding to one of the basic zones.
static getProps()
Get the property string for iiprop and aiprop.
if(!is_readable( $file)) $ext
wfWarn( $msg, $callerOffset=1, $level=E_USER_NOTICE)
Send a warning either to the debug log or in a PHP error depending on $wgDevelopmentWarnings.
Foreign file accessible through api.php requests.
getThumbError( $name, $width=-1, $height=-1, $otherParams='', $lang=null)
getInfo()
Get information about the repo - overrides/extends the parent class's information.
canCacheThumbs()
Are we locally caching the thumbnails?
string false $url
Public zone URL.
static factory( $url, array $options=null, $caller=__METHOD__)
Generate a new request object.
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.