23use Wikimedia\Timestamp\ConvertibleTimestamp;
24use Wikimedia\Timestamp\TimestampFormat as TS;
48 private const VERSION =
"2.1";
51 protected $fileFactory = [ ForeignAPIFile::class,
'newFromTitle' ];
85 parent::__construct( $info );
88 $this->mApiBase = $info[
'apibase'] ??
null;
90 if ( isset( $info[
'apiThumbCacheExpiry'] ) ) {
91 $this->apiThumbCacheExpiry = $info[
'apiThumbCacheExpiry'];
93 if ( isset( $info[
'fileCacheExpiry'] ) ) {
94 $this->fileCacheExpiry = $info[
'fileCacheExpiry'];
96 if ( isset( $info[
'apiMetadataExpiry'] ) ) {
97 $this->apiMetadataExpiry = $info[
'apiMetadataExpiry'];
99 if ( isset( $info[
'userAgent'] ) ) {
100 $this->userAgent = $info[
'userAgent'];
102 if ( !$this->scriptDirUrl ) {
104 $this->scriptDirUrl = dirname( $this->mApiBase );
108 $this->url = $localFileRepo[
'url'];
111 $this->thumbUrl = $this->url .
'/thumb';
123 public function newFile( $title, $time =
false ) {
128 return parent::newFile( $title, $time );
137 foreach ( $files as $k => $f ) {
138 if ( isset( $this->mFileExists[$f] ) ) {
139 $results[$k] = $this->mFileExists[$f];
141 } elseif ( self::isVirtualUrl( $f ) ) {
142 # @todo FIXME: We need to be able to handle virtual
143 # URLs better, at least when we know they refer to the
145 $results[$k] =
false;
148 $results[$k] =
false;
150 wfWarn(
"Got mwstore:// path '$f'." );
155 'titles' => implode(
'|', $files ),
156 'prop' =>
'imageinfo' ]
159 if ( isset( $data[
'query'][
'pages'] ) ) {
160 # First, get results from the query. Note we only care whether the image exists,
161 # not whether it has a description page.
162 foreach ( $data[
'query'][
'pages'] as $p ) {
163 $this->mFileExists[$p[
'title']] = ( $p[
'imagerepository'] !==
'' );
165 # Second, copy the results to any redirects that were queried
166 if ( isset( $data[
'query'][
'redirects'] ) ) {
167 foreach ( $data[
'query'][
'redirects'] as $r ) {
168 $this->mFileExists[$r[
'from']] = $this->mFileExists[$r[
'to']];
171 # Third, copy the results to any non-normalized titles that were queried
172 if ( isset( $data[
'query'][
'normalized'] ) ) {
173 foreach ( $data[
'query'][
'normalized'] as $n ) {
174 $this->mFileExists[$n[
'from']] = $this->mFileExists[$n[
'to']];
177 # Finally, copy the results to the output
178 foreach ( $files as $key => $file ) {
179 $results[$key] = $this->mFileExists[$file];
201 $query = array_merge( $query, [
204 'redirects' =>
'true',
208 'uselang' =>
'content',
211 $data = $this->
httpGetCached(
'Metadata', $query, $this->apiMetadataExpiry );
214 return FormatJson::decode( $data,
true );
225 if ( $data && isset( $data[
'query'][
'pages'] ) ) {
226 foreach ( $data[
'query'][
'pages'] as $info ) {
227 if ( isset( $info[
'imageinfo'][0] ) ) {
228 $return = $info[
'imageinfo'][0];
229 if ( isset( $info[
'pageid'] ) ) {
230 $return[
'pageid'] = $info[
'pageid'];
246 'list' =>
'allimages',
248 'aisha1base36' => $hash,
251 if ( isset( $results[
'query'][
'allimages'] ) ) {
252 foreach ( $results[
'query'][
'allimages'] as $img ) {
254 if ( !isset( $img[
'name'] ) ) {
294 $sizekey =
"$width:$height:$params";
297 $knownThumbUrls = $this->wanCache->get( $key );
298 if ( !$knownThumbUrls ) {
300 $knownThumbUrls = [];
301 } elseif ( isset( $knownThumbUrls[$sizekey] ) ) {
302 wfDebug( __METHOD__ .
': Got thumburl from local cache: ' .
303 "{$knownThumbUrls[$sizekey]}" );
305 return $knownThumbUrls[$sizekey];
312 wfDebug( __METHOD__ .
" Could not find thumburl" );
318 $fileName = rawurldecode( pathinfo( $foreignUrl, PATHINFO_BASENAME ) );
320 wfDebug( __METHOD__ .
" The deduced filename $fileName is not safe" );
325 $localFilename = $localPath .
"/" . $fileName;
327 rawurlencode(
$name ) .
"/" . rawurlencode( $fileName );
330 && isset( $metadata[
'timestamp'] )
332 wfDebug( __METHOD__ .
" Thumbnail was already downloaded before" );
334 $remoteModified = (int)
wfTimestamp( TS::UNIX, $metadata[
'timestamp'] );
335 $current = (int)ConvertibleTimestamp::now( TS::UNIX );
336 $diff = abs( $modified - $current );
337 if ( $remoteModified < $modified && $diff < $this->fileCacheExpiry ) {
339 $knownThumbUrls[$sizekey] = $localUrl;
340 $this->wanCache->set( $key, $knownThumbUrls, $this->apiThumbCacheExpiry );
347 $thumb = $this->
httpGet( $foreignUrl,
'default', [], $mtime );
349 wfDebug( __METHOD__ .
" Could not download thumb" );
354 # @todo FIXME: Delete old thumbs that aren't being used. Maintenance script?
356 $params = [
'dst' => $localFilename,
'content' => $thumb ];
358 wfDebug( __METHOD__ .
" could not write to thumb path '$localFilename'" );
362 $knownThumbUrls[$sizekey] = $localUrl;
363 $this->wanCache->set( $key, $knownThumbUrls, $this->apiThumbCacheExpiry );
364 wfDebug( __METHOD__ .
" got local thumb $localUrl, saving to cache" );
382 return parent::getZoneUrl( $zone, $ext );
392 $supported = [
'public',
'thumb' ];
393 if ( in_array( $zone, $supported ) ) {
394 return parent::getZonePath( $zone );
405 return ( $this->apiThumbCacheExpiry > 0 );
414 $classVersion = self::VERSION;
415 $extra = $this->userAgent !==
null ?
' ' . $this->userAgent :
'';
416 return "$mediaWikiUA ForeignAPIRepo/$classVersion" . $extra;
426 $info = parent::getInfo();
427 $info[
'apiurl'] = $this->mApiBase;
432 'meta' =>
'siteinfo',
433 'siprop' =>
'general',
439 $siteInfo = FormatJson::decode( $data,
true );
440 $general = $siteInfo[
'query'][
'general'];
442 $info[
'articlepath'] = $general[
'articlepath'];
443 $info[
'server'] = $general[
'server'];
444 if ( !isset( $info[
'favicon'] ) && isset( $general[
'favicon'] ) ) {
445 $info[
'favicon'] = $general[
'favicon'];
460 $url, $timeout =
'default', $options = [], &$mtime =
false
465 $options[
'timeout'] = $timeout;
467 wfDebug(
"ForeignAPIRepo: HTTP GET: $url" );
471 $options[
'method'] =
"GET";
473 if ( !isset( $options[
'timeout'] ) ) {
474 $options[
'timeout'] =
'default';
479 $req = $requestFactory->create(
$url, $options, __METHOD__ );
480 $req->setHeader(
'Referer', $urlUtils->getCanonicalServer() );
481 $status = $req->execute();
483 if ( $status->isOK() ) {
484 $lmod = $req->getResponseHeader(
'Last-Modified' );
485 $mtime = $lmod ? (int)
wfTimestamp( TS::UNIX, $lmod ) :
false;
487 return $req->getContent();
489 $logger = LoggerFactory::getInstance(
'http' );
491 $status->getWikiText(
false,
false,
'en' ),
492 [
'caller' => __METHOD__ ]
507 if ( $this->mApiBase ) {
513 return $this->wanCache->getWithSetCallback(
517 $this->wanCache->makeGlobalKey(
"filerepo-$attribute", sha1(
$url ) ),
519 function () use (
$url ) {
520 $html = $this->
httpGet(
$url,
'default', [], $mtime );
521 if ( $html ===
false ) {
526 [
'pcGroup' =>
'http-get:3',
'pcTTL' => WANObjectCache::TTL_PROC_LONG ]
535 throw new RuntimeException(
'enumFiles is not supported by ' . static::class );
539 throw new LogicException( static::class .
': write operations are not supported.' );
544class_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...
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
A class containing constants representing the names of configuration variables.
const LocalFileRepo
Name constant for the LocalFileRepo setting, for use with Config::get()
Interface for objects (potentially) representing an editable wiki page.