23use Wikimedia\Timestamp\ConvertibleTimestamp;
24use Wikimedia\Timestamp\TimestampFormat as TS;
46 private const VERSION =
"2.1";
51 private const IMAGE_INFO_PROPS = [
57 protected $fileFactory = [ ForeignAPIFile::class,
'newFromTitle' ];
89 parent::__construct( $info );
92 $this->mApiBase = $info[
'apibase'] ??
null;
94 if ( isset( $info[
'apiThumbCacheExpiry'] ) ) {
95 $this->apiThumbCacheExpiry = $info[
'apiThumbCacheExpiry'];
97 if ( isset( $info[
'fileCacheExpiry'] ) ) {
98 $this->fileCacheExpiry = $info[
'fileCacheExpiry'];
100 if ( isset( $info[
'apiMetadataExpiry'] ) ) {
101 $this->apiMetadataExpiry = $info[
'apiMetadataExpiry'];
103 if ( !$this->scriptDirUrl ) {
105 $this->scriptDirUrl = dirname( $this->mApiBase );
109 $this->url = $localFileRepo[
'url'];
112 $this->thumbUrl = $this->url .
'/thumb';
124 public function newFile( $title, $time =
false ) {
129 return parent::newFile( $title, $time );
138 foreach ( $files as $k => $f ) {
139 if ( isset( $this->mFileExists[$f] ) ) {
140 $results[$k] = $this->mFileExists[$f];
142 } elseif ( self::isVirtualUrl( $f ) ) {
143 # @todo FIXME: We need to be able to handle virtual
144 # URLs better, at least when we know they refer to the
146 $results[$k] =
false;
149 $results[$k] =
false;
151 wfWarn(
"Got mwstore:// path '$f'." );
156 'titles' => implode(
'|', $files ),
157 'prop' =>
'imageinfo' ]
160 if ( isset( $data[
'query'][
'pages'] ) ) {
161 # First, get results from the query. Note we only care whether the image exists,
162 # not whether it has a description page.
163 foreach ( $data[
'query'][
'pages'] as $p ) {
164 $this->mFileExists[$p[
'title']] = ( $p[
'imagerepository'] !==
'' );
166 # Second, copy the results to any redirects that were queried
167 if ( isset( $data[
'query'][
'redirects'] ) ) {
168 foreach ( $data[
'query'][
'redirects'] as $r ) {
169 $this->mFileExists[$r[
'from']] = $this->mFileExists[$r[
'to']];
172 # Third, copy the results to any non-normalized titles that were queried
173 if ( isset( $data[
'query'][
'normalized'] ) ) {
174 foreach ( $data[
'query'][
'normalized'] as $n ) {
175 $this->mFileExists[$n[
'from']] = $this->mFileExists[$n[
'to']];
178 # Finally, copy the results to the output
179 foreach ( $files as $key => $file ) {
180 $results[$key] = $this->mFileExists[$file];
205 $query = array_merge( $query,
209 'redirects' =>
'true'
212 if ( !isset( $query[
'uselang'] ) ) {
213 $query[
'uselang'] = $languageCode;
216 $data = $this->
httpGetCached(
'Metadata', $query, $this->apiMetadataExpiry );
219 return FormatJson::decode( $data,
true );
230 if ( $data && isset( $data[
'query'][
'pages'] ) ) {
231 foreach ( $data[
'query'][
'pages'] as $info ) {
232 if ( isset( $info[
'imageinfo'][0] ) ) {
233 $return = $info[
'imageinfo'][0];
234 if ( isset( $info[
'pageid'] ) ) {
235 $return[
'pageid'] = $info[
'pageid'];
251 'aisha1base36' => $hash,
253 'list' =>
'allimages',
256 if ( isset( $results[
'query'][
'allimages'] ) ) {
257 foreach ( $results[
'query'][
'allimages'] as $img ) {
259 if ( !isset( $img[
'name'] ) ) {
278 private function getThumbUrl(
279 $name, $width = -1, $height = -1, &$result =
null, $otherParams =
''
282 'titles' =>
'File:' .
$name,
283 'iiprop' => self::getIIProps(),
284 'iiurlwidth' => $width,
285 'iiurlheight' => $height,
286 'iiurlparam' => $otherParams,
287 'prop' =>
'imageinfo' ] );
290 if ( $data && $info && isset( $info[
'thumburl'] ) ) {
291 wfDebug( __METHOD__ .
" got remote thumb " . $info[
'thumburl'] );
294 return $info[
'thumburl'];
310 $name, $width = -1, $height = -1, $otherParams =
'', $lang =
null
313 'titles' =>
'File:' .
$name,
314 'iiprop' => self::getIIProps(),
315 'iiurlwidth' => $width,
316 'iiurlheight' => $height,
317 'iiurlparam' => $otherParams,
318 'prop' =>
'imageinfo',
323 if ( $data && $info && isset( $info[
'thumberror'] ) ) {
324 wfDebug( __METHOD__ .
" got remote thumb error " . $info[
'thumberror'] );
327 'thumbnail_error_remote',
359 return $this->getThumbUrl(
$name, $width, $height, $result, $params );
363 $sizekey =
"$width:$height:$params";
366 $knownThumbUrls = $this->wanCache->get( $key );
367 if ( !$knownThumbUrls ) {
369 $knownThumbUrls = [];
370 } elseif ( isset( $knownThumbUrls[$sizekey] ) ) {
371 wfDebug( __METHOD__ .
': Got thumburl from local cache: ' .
372 "{$knownThumbUrls[$sizekey]}" );
374 return $knownThumbUrls[$sizekey];
378 $foreignUrl = $this->getThumbUrl(
$name, $width, $height, $metadata, $params );
380 if ( !$foreignUrl ) {
381 wfDebug( __METHOD__ .
" Could not find thumburl" );
387 $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) );
389 wfDebug( __METHOD__ .
" The deduced filename $fileName is not safe" );
394 $localFilename = $localPath .
"/" . $fileName;
396 rawurlencode(
$name ) .
"/" . rawurlencode( $fileName );
399 && isset( $metadata[
'timestamp'] )
401 wfDebug( __METHOD__ .
" Thumbnail was already downloaded before" );
403 $remoteModified = (int)
wfTimestamp( TS::UNIX, $metadata[
'timestamp'] );
404 $current = (int)ConvertibleTimestamp::now( TS::UNIX );
405 $diff = abs( $modified - $current );
406 if ( $remoteModified < $modified && $diff < $this->fileCacheExpiry ) {
408 $knownThumbUrls[$sizekey] = $localUrl;
409 $this->wanCache->set( $key, $knownThumbUrls, $this->apiThumbCacheExpiry );
416 $thumb =
self::httpGet( $foreignUrl,
'default', [], $mtime );
418 wfDebug( __METHOD__ .
" Could not download thumb" );
423 # @todo FIXME: Delete old thumbs that aren't being used. Maintenance script?
425 $params = [
'dst' => $localFilename,
'content' => $thumb ];
427 wfDebug( __METHOD__ .
" could not write to thumb path '$localFilename'" );
431 $knownThumbUrls[$sizekey] = $localUrl;
434 ? $this->wanCache->adaptiveTTL( $mtime, $this->apiThumbCacheExpiry )
436 $this->wanCache->set( $key, $knownThumbUrls, $ttl );
437 wfDebug( __METHOD__ .
" got local thumb $localUrl, saving to cache" );
455 return parent::getZoneUrl( $zone, $ext );
465 $supported = [
'public',
'thumb' ];
466 if ( in_array( $zone, $supported ) ) {
467 return parent::getZonePath( $zone );
478 return ( $this->apiThumbCacheExpiry > 0 );
487 $classVersion = self::VERSION;
489 return "$mediaWikiVersion ($contactUrl) ForeignAPIRepo/$classVersion";
499 $info = parent::getInfo();
500 $info[
'apiurl'] = $this->mApiBase;
505 'meta' =>
'siteinfo',
506 'siprop' =>
'general',
512 $siteInfo = FormatJson::decode( $data,
true );
513 $general = $siteInfo[
'query'][
'general'];
515 $info[
'articlepath'] = $general[
'articlepath'];
516 $info[
'server'] = $general[
'server'];
517 if ( !isset( $info[
'favicon'] ) && isset( $general[
'favicon'] ) ) {
518 $info[
'favicon'] = $general[
'favicon'];
533 $url, $timeout =
'default', $options = [], &$mtime =
false
538 $options[
'timeout'] = $timeout;
540 wfDebug(
"ForeignAPIRepo: HTTP GET: $url" );
544 $options[
'method'] =
"GET";
546 if ( !isset( $options[
'timeout'] ) ) {
547 $options[
'timeout'] =
'default';
552 $req = $requestFactory->create(
$url, $options, __METHOD__ );
553 $req->setHeader(
'Referer', $urlUtils->getCanonicalServer() );
554 $status = $req->execute();
556 if ( $status->isOK() ) {
557 $lmod = $req->getResponseHeader(
'Last-Modified' );
558 $mtime = $lmod ? (int)
wfTimestamp( TS::UNIX, $lmod ) :
false;
560 return $req->getContent();
562 $logger = LoggerFactory::getInstance(
'http' );
564 $status->getWikiText(
false,
false,
'en' ),
565 [
'caller' =>
'ForeignAPIRepo::httpGet' ]
577 return implode(
'|', self::IMAGE_INFO_PROPS );
588 if ( $this->mApiBase ) {
594 return $this->wanCache->getWithSetCallback(
598 $this->wanCache->makeGlobalKey(
"filerepo-$attribute", sha1(
$url ) ),
600 function ( $curValue, &$ttl ) use (
$url ) {
604 if ( $html !==
false ) {
605 $ttl = $mtime ? $this->wanCache->adaptiveTTL( $mtime, $ttl ) : $ttl;
607 $ttl = $this->wanCache->adaptiveTTL( $mtime, $ttl );
613 [
'pcGroup' =>
'http-get:3',
'pcTTL' => WANObjectCache::TTL_PROC_LONG ]
622 throw new RuntimeException(
'enumFiles is not supported by ' . static::class );
626 throw new LogicException( static::class .
': write operations are not supported.' );
631class_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.
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
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.