17use Wikimedia\Timestamp\TimestampFormat as TS;
43 $this->mExists = $exists;
54 $data =
$repo->fetchImageQuery( [
56 'iiprop' => self::getProps(),
57 'prop' =>
'imageinfo',
58 'iimetadataversion' => MediaHandler::getMetadataVersion(),
61 'iiextmetadatamultilang' => 1,
64 $info =
$repo->getImageInfo( $data );
67 $lastRedirect = count( $data[
'query'][
'redirects'] ?? [] ) - 1;
68 if ( $lastRedirect >= 0 ) {
70 $newtitle = Title::newFromText( $data[
'query'][
'redirects'][$lastRedirect][
'to'] );
71 $img =
new self( $newtitle,
$repo, $info, true );
88 return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
104 return $this->mExists;
122 return parent::transform( $params, $flags );
127 $otherParams = $this->handler->makeParamString( $params );
128 $width = $params[
'width'] ?? -1;
129 $height = $params[
'height'] ?? -1;
132 if ( $width > 0 || $height > 0 ) {
134 $thumbUrl = $this->repo->getThumbUrlFromCache(
144 if ( $thumbUrl ===
false ) {
145 return $this->repo->getThumbError(
150 $this->handler->getLanguage()->getCode()
154 return $this->handler->getTransform( $this,
'bogus', $thumbUrl, $params );
164 return (
int)( $this->mInfo[
'width'] ?? 0 );
172 return (
int)( $this->mInfo[
'height'] ?? 0 );
179 if ( isset( $this->mInfo[
'metadata'] ) ) {
180 return serialize( self::parseMetadata( $this->mInfo[
'metadata'] ) );
187 if ( isset( $this->mInfo[
'metadata'] ) ) {
199 return $this->mInfo[
'extmetadata'] ??
null;
207 if ( !is_array( $metadata ) ) {
208 return [
'_error' => $metadata ];
210 '@phan-var array[] $metadata';
212 foreach ( $metadata as $meta ) {
213 $ret[$meta[
'name']] = self::parseMetadataValue( $meta[
'value'] );
223 private static function parseMetadataValue( $metadata ) {
224 if ( !is_array( $metadata ) ) {
227 '@phan-var array[] $metadata';
229 foreach ( $metadata as $meta ) {
230 $ret[$meta[
'name']] = self::parseMetadataValue( $meta[
'value'] );
240 return isset( $this->mInfo[
'size'] ) ? intval( $this->mInfo[
'size'] ) :
null;
247 return isset( $this->mInfo[
'url'] ) ? strval( $this->mInfo[
'url'] ) :
null;
258 if ( isset( $this->mInfo[
'descriptionshorturl'] ) ) {
259 return $this->mInfo[
'descriptionshorturl'];
260 } elseif ( isset( $this->mInfo[
'pageid'] ) ) {
261 $url = $this->repo->makeUrl( [
'curid' => $this->mInfo[
'pageid'] ] );
262 if (
$url !==
false ) {
270 if ( isset( $this->mInfo[
'user'] ) ) {
271 return UserIdentityValue::newExternal( $this->getRepoName(), $this->mInfo[
'user'] );
282 return isset( $this->mInfo[
'comment'] ) ? strval( $this->mInfo[
'comment'] ) :
null;
289 return isset( $this->mInfo[
'sha1'] )
290 ? \Wikimedia\base_convert( strval( $this->mInfo[
'sha1'] ), 16, 36, 31 )
299 isset( $this->mInfo[
'timestamp'] )
300 ? strval( $this->mInfo[
'timestamp'] )
309 if ( !isset( $this->mInfo[
'mime'] ) ) {
310 $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
311 $this->mInfo[
'mime'] = $magic->getMimeTypeFromExtensionOrNull( $this->getExtension() ) ??
'unknown/unknown';
314 return $this->mInfo[
'mime'];
321 if ( isset( $this->mInfo[
'mediatype'] ) ) {
322 return $this->mInfo[
'mediatype'];
324 $magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
326 return $magic->getMediaType(
null, $this->getMimeType() );
333 return $this->mInfo[
'descriptionurl'] ??
false;
342 if ( !$this->repo->canCacheThumbs() ) {
346 $path = $this->repo->getZonePath(
'thumb' ) .
'/' . $this->getHashPath();
348 $path .= $suffix .
'/';
357 $dir = $this->getThumbPath( $this->getName() );
358 $iter = $this->repo->getBackend()->getFileList( [
'dir' => $dir ] );
362 foreach ( $iter as $file ) {
372 $this->purgeThumbnails( $options );
373 $this->purgeDescriptionPage();
376 private function purgeDescriptionPage() {
377 $services = MediaWikiServices::getInstance();
378 $langCode = $services->getContentLanguageCode()->toString();
381 $key = $this->repo->getLocalCacheKey(
'file-remote-description', $langCode, md5( $this->getName() ) );
382 $services->getMainWANObjectCache()->delete( $key );
389 $key = $this->repo->getLocalCacheKey(
'file-thumb-url', sha1( $this->getName() ) );
390 MediaWikiServices::getInstance()->getMainWANObjectCache()->delete( $key );
392 $files = $this->getThumbnails();
394 $handler = $this->getHandler();
396 $handler->filterThumbnailPurgeList( $files, $options );
399 $dir = $this->getThumbPath( $this->getName() );
401 foreach ( $files as $file ) {
402 $purgeList[] =
"{$dir}{$file}";
405 # Delete the thumbnails
406 $this->repo->quickPurgeBatch( $purgeList );
407 # Clear out the thumbnail directory if empty
408 $this->repo->quickCleanDir( $dir );
422class_alias( ForeignAPIFile::class,
'ForeignAPIFile' );
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))