58 private const VERSION =
"2.1";
63 private const IMAGE_INFO_PROPS = [
69 protected $fileFactory = [ ForeignAPIFile::class,
'newFromTitle' ];
101 parent::__construct( $info );
104 $this->mApiBase = $info[
'apibase'] ??
null;
106 if ( isset( $info[
'apiThumbCacheExpiry'] ) ) {
107 $this->apiThumbCacheExpiry = $info[
'apiThumbCacheExpiry'];
109 if ( isset( $info[
'fileCacheExpiry'] ) ) {
110 $this->fileCacheExpiry = $info[
'fileCacheExpiry'];
112 if ( isset( $info[
'apiMetadataExpiry'] ) ) {
113 $this->apiMetadataExpiry = $info[
'apiMetadataExpiry'];
115 if ( !$this->scriptDirUrl ) {
117 $this->scriptDirUrl = dirname( $this->mApiBase );
121 $this->url = $localFileRepo[
'url'];
124 $this->thumbUrl = $this->url .
'/thumb';
136 public function newFile( $title, $time =
false ) {
141 return parent::newFile( $title, $time );
150 foreach ( $files as $k => $f ) {
151 if ( isset( $this->mFileExists[$f] ) ) {
152 $results[$k] = $this->mFileExists[$f];
154 } elseif ( self::isVirtualUrl( $f ) ) {
155 # @todo FIXME: We need to be able to handle virtual
156 # URLs better, at least when we know they refer to the
158 $results[$k] =
false;
161 $results[$k] =
false;
163 wfWarn(
"Got mwstore:// path '$f'." );
168 'titles' => implode(
'|', $files ),
169 'prop' =>
'imageinfo' ]
172 if ( isset( $data[
'query'][
'pages'] ) ) {
173 # First, get results from the query. Note we only care whether the image exists,
174 # not whether it has a description page.
175 foreach ( $data[
'query'][
'pages'] as $p ) {
176 $this->mFileExists[$p[
'title']] = ( $p[
'imagerepository'] !==
'' );
178 # Second, copy the results to any redirects that were queried
179 if ( isset( $data[
'query'][
'redirects'] ) ) {
180 foreach ( $data[
'query'][
'redirects'] as $r ) {
181 $this->mFileExists[$r[
'from']] = $this->mFileExists[$r[
'to']];
184 # Third, copy the results to any non-normalized titles that were queried
185 if ( isset( $data[
'query'][
'normalized'] ) ) {
186 foreach ( $data[
'query'][
'normalized'] as $n ) {
187 $this->mFileExists[$n[
'from']] = $this->mFileExists[$n[
'to']];
190 # Finally, copy the results to the output
191 foreach ( $files as $key => $file ) {
192 $results[$key] = $this->mFileExists[$file];
217 $query = array_merge( $query,
221 'redirects' =>
'true'
224 if ( !isset( $query[
'uselang'] ) ) {
225 $query[
'uselang'] = $languageCode;
228 $data = $this->
httpGetCached(
'Metadata', $query, $this->apiMetadataExpiry );
231 return FormatJson::decode( $data,
true );
242 if ( $data && isset( $data[
'query'][
'pages'] ) ) {
243 foreach ( $data[
'query'][
'pages'] as $info ) {
244 if ( isset( $info[
'imageinfo'][0] ) ) {
245 $return = $info[
'imageinfo'][0];
246 if ( isset( $info[
'pageid'] ) ) {
247 $return[
'pageid'] = $info[
'pageid'];
263 'aisha1base36' => $hash,
265 'list' =>
'allimages',
268 if ( isset( $results[
'query'][
'allimages'] ) ) {
269 foreach ( $results[
'query'][
'allimages'] as $img ) {
271 if ( !isset( $img[
'name'] ) ) {
290 private function getThumbUrl(
291 $name, $width = -1, $height = -1, &$result =
null, $otherParams =
''
294 'titles' =>
'File:' .
$name,
295 'iiprop' => self::getIIProps(),
296 'iiurlwidth' => $width,
297 'iiurlheight' => $height,
298 'iiurlparam' => $otherParams,
299 'prop' =>
'imageinfo' ] );
302 if ( $data && $info && isset( $info[
'thumburl'] ) ) {
303 wfDebug( __METHOD__ .
" got remote thumb " . $info[
'thumburl'] );
306 return $info[
'thumburl'];
322 $name, $width = -1, $height = -1, $otherParams =
'', $lang =
null
325 'titles' =>
'File:' .
$name,
326 'iiprop' => self::getIIProps(),
327 'iiurlwidth' => $width,
328 'iiurlheight' => $height,
329 'iiurlparam' => $otherParams,
330 'prop' =>
'imageinfo',
335 if ( $data && $info && isset( $info[
'thumberror'] ) ) {
336 wfDebug( __METHOD__ .
" got remote thumb error " . $info[
'thumberror'] );
339 'thumbnail_error_remote',
371 return $this->getThumbUrl(
$name, $width, $height, $result, $params );
375 $sizekey =
"$width:$height:$params";
378 $knownThumbUrls = $this->wanCache->get( $key );
379 if ( !$knownThumbUrls ) {
381 $knownThumbUrls = [];
382 } elseif ( isset( $knownThumbUrls[$sizekey] ) ) {
383 wfDebug( __METHOD__ .
': Got thumburl from local cache: ' .
384 "{$knownThumbUrls[$sizekey]}" );
386 return $knownThumbUrls[$sizekey];
390 $foreignUrl = $this->getThumbUrl(
$name, $width, $height, $metadata, $params );
392 if ( !$foreignUrl ) {
393 wfDebug( __METHOD__ .
" Could not find thumburl" );
399 $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) );
401 wfDebug( __METHOD__ .
" The deduced filename $fileName is not safe" );
406 $localFilename = $localPath .
"/" . $fileName;
408 rawurlencode(
$name ) .
"/" . rawurlencode( $fileName );
411 && isset( $metadata[
'timestamp'] )
413 wfDebug( __METHOD__ .
" Thumbnail was already downloaded before" );
415 $remoteModified = (int)
wfTimestamp( TS_UNIX, $metadata[
'timestamp'] );
417 $diff = abs( $modified - $current );
418 if ( $remoteModified < $modified && $diff < $this->fileCacheExpiry ) {
420 $knownThumbUrls[$sizekey] = $localUrl;
421 $this->wanCache->set( $key, $knownThumbUrls, $this->apiThumbCacheExpiry );
428 $thumb =
self::httpGet( $foreignUrl,
'default', [], $mtime );
430 wfDebug( __METHOD__ .
" Could not download thumb" );
435 # @todo FIXME: Delete old thumbs that aren't being used. Maintenance script?
437 $params = [
'dst' => $localFilename,
'content' => $thumb ];
439 wfDebug( __METHOD__ .
" could not write to thumb path '$localFilename'" );
443 $knownThumbUrls[$sizekey] = $localUrl;
446 ? $this->wanCache->adaptiveTTL( $mtime, $this->apiThumbCacheExpiry )
448 $this->wanCache->set( $key, $knownThumbUrls, $ttl );
449 wfDebug( __METHOD__ .
" got local thumb $localUrl, saving to cache" );
467 return parent::getZoneUrl( $zone, $ext );
477 $supported = [
'public',
'thumb' ];
478 if ( in_array( $zone, $supported ) ) {
479 return parent::getZonePath( $zone );
490 return ( $this->apiThumbCacheExpiry > 0 );
499 " ForeignAPIRepo/" . self::VERSION;
509 $info = parent::getInfo();
510 $info[
'apiurl'] = $this->mApiBase;
515 'meta' =>
'siteinfo',
516 'siprop' =>
'general',
522 $siteInfo = FormatJson::decode( $data,
true );
523 $general = $siteInfo[
'query'][
'general'];
525 $info[
'articlepath'] = $general[
'articlepath'];
526 $info[
'server'] = $general[
'server'];
527 if ( !isset( $info[
'favicon'] ) && isset( $general[
'favicon'] ) ) {
528 $info[
'favicon'] = $general[
'favicon'];
543 $url, $timeout =
'default', $options = [], &$mtime =
false
545 $options[
'timeout'] = $timeout;
548 wfDebug(
"ForeignAPIRepo: HTTP GET: $url" );
552 $options[
'method'] =
"GET";
554 if ( !isset( $options[
'timeout'] ) ) {
555 $options[
'timeout'] =
'default';
561 ->create(
$url, $options, __METHOD__ );
562 $status = $req->execute();
564 if ( $status->isOK() ) {
565 $lmod = $req->getResponseHeader(
'Last-Modified' );
566 $mtime = $lmod ? (int)
wfTimestamp( TS_UNIX, $lmod ) :
false;
568 return $req->getContent();
570 $logger = LoggerFactory::getInstance(
'http' );
572 $status->getWikiText(
false,
false,
'en' ),
573 [
'caller' =>
'ForeignAPIRepo::httpGet' ]
585 return implode(
'|', self::IMAGE_INFO_PROPS );
596 if ( $this->mApiBase ) {
602 return $this->wanCache->getWithSetCallback(
606 $this->wanCache->makeGlobalKey(
"filerepo-$attribute", sha1(
$url ) ),
608 function ( $curValue, &$ttl ) use (
$url ) {
612 if ( $html !==
false ) {
613 $ttl = $mtime ? $this->wanCache->adaptiveTTL( $mtime, $ttl ) : $ttl;
615 $ttl = $this->wanCache->adaptiveTTL( $mtime, $ttl );
621 [
'pcGroup' =>
'http-get:3',
'pcTTL' => WANObjectCache::TTL_PROC_LONG ]
630 throw new RuntimeException(
'enumFiles is not supported by ' . static::class );
637 throw new LogicException( static::class .
': write operations are not supported.' );
642class_alias( ForeignAPIRepo::class,
'ForeignAPIRepo' );
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.
A class containing constants representing the names of configuration variables.
const LocalFileRepo
Name constant for the LocalFileRepo setting, for use with Config::get()
const LanguageCode
Name constant for the LanguageCode setting, for use with Config::get()
Interface for objects (potentially) representing an editable wiki page.