1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
|
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
use MediaWiki\MediaWikiServices;
use MediaWiki\Permissions\Authority;
use MediaWiki\Title\Title;
use MediaWiki\User\UserIdentity;
use MediaWiki\User\UserIdentityValue;
/**
* Foreign file accessible through api.php requests.
*
* @ingroup FileAbstraction
*/
class ForeignAPIFile extends File {
/** @var bool */
private $mExists;
/** @var array */
private $mInfo;
/** @inheritDoc */
protected $repoClass = ForeignAPIRepo::class;
/**
* @param Title|string|false $title
* @param ForeignApiRepo $repo
* @param array $info
* @param bool $exists
*/
public function __construct( $title, $repo, $info, $exists = false ) {
parent::__construct( $title, $repo );
$this->mInfo = $info;
$this->mExists = $exists;
$this->assertRepoDefined();
}
/**
* @param Title $title
* @param ForeignApiRepo $repo
* @return ForeignAPIFile|null
*/
public static function newFromTitle( Title $title, $repo ) {
$data = $repo->fetchImageQuery( [
'titles' => 'File:' . $title->getDBkey(),
'iiprop' => self::getProps(),
'prop' => 'imageinfo',
'iimetadataversion' => MediaHandler::getMetadataVersion(),
// extmetadata is language-dependent, accessing the current language here
// would be problematic, so we just get them all
'iiextmetadatamultilang' => 1,
] );
$info = $repo->getImageInfo( $data );
if ( $info ) {
$lastRedirect = count( $data['query']['redirects'] ?? [] ) - 1;
if ( $lastRedirect >= 0 ) {
// @phan-suppress-next-line PhanTypeArraySuspiciousNullable
$newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] );
$img = new self( $newtitle, $repo, $info, true );
$img->redirectedFrom( $title->getDBkey() );
} else {
$img = new self( $title, $repo, $info, true );
}
return $img;
} else {
return null;
}
}
/**
* Get the property string for iiprop and aiprop
* @return string
*/
public static function getProps() {
return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata';
}
/**
* @return ForeignAPIRepo|false
*/
public function getRepo() {
return $this->repo;
}
// Dummy functions...
/**
* @return bool
*/
public function exists() {
return $this->mExists;
}
/**
* @return bool
*/
public function getPath() {
return false;
}
/**
* @param array $params
* @param int $flags
* @return MediaTransformOutput|false
*/
public function transform( $params, $flags = 0 ) {
if ( !$this->canRender() ) {
// show icon
return parent::transform( $params, $flags );
}
// Note, the this->canRender() check above implies
// that we have a handler, and it can do makeParamString.
$otherParams = $this->handler->makeParamString( $params );
$width = $params['width'] ?? -1;
$height = $params['height'] ?? -1;
$thumbUrl = false;
if ( $width > 0 || $height > 0 ) {
// Only query the remote if there are dimensions
$thumbUrl = $this->repo->getThumbUrlFromCache(
$this->getName(),
$width,
$height,
$otherParams
);
} elseif ( $this->getMediaType() === MEDIATYPE_AUDIO ) {
// This has no dimensions, but we still need to pass a value to getTransform()
$thumbUrl = '/';
}
if ( $thumbUrl === false ) {
global $wgLang;
return $this->repo->getThumbError(
$this->getName(),
$width,
$height,
$otherParams,
$wgLang->getCode()
);
}
return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params );
}
// Info we can get from API...
/**
* @param int $page
* @return int
*/
public function getWidth( $page = 1 ) {
return (int)( $this->mInfo['width'] ?? 0 );
}
/**
* @param int $page
* @return int
*/
public function getHeight( $page = 1 ) {
return (int)( $this->mInfo['height'] ?? 0 );
}
/**
* @return string|false
*/
public function getMetadata() {
if ( isset( $this->mInfo['metadata'] ) ) {
return serialize( self::parseMetadata( $this->mInfo['metadata'] ) );
}
return false;
}
/**
* @return array
*/
public function getMetadataArray(): array {
if ( isset( $this->mInfo['metadata'] ) ) {
return self::parseMetadata( $this->mInfo['metadata'] );
}
return [];
}
/**
* @return array|null Extended metadata (see imageinfo API for format) or
* null on error
*/
public function getExtendedMetadata() {
return $this->mInfo['extmetadata'] ?? null;
}
/**
* @param mixed $metadata
* @return array
*/
public static function parseMetadata( $metadata ) {
if ( !is_array( $metadata ) ) {
return [ '_error' => $metadata ];
}
'@phan-var array[] $metadata';
$ret = [];
foreach ( $metadata as $meta ) {
$ret[$meta['name']] = self::parseMetadataValue( $meta['value'] );
}
return $ret;
}
/**
* @param mixed $metadata
* @return mixed
*/
private static function parseMetadataValue( $metadata ) {
if ( !is_array( $metadata ) ) {
return $metadata;
}
'@phan-var array[] $metadata';
$ret = [];
foreach ( $metadata as $meta ) {
$ret[$meta['name']] = self::parseMetadataValue( $meta['value'] );
}
return $ret;
}
/**
* @return int|null|false
*/
public function getSize() {
return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null;
}
/**
* @return null|string
*/
public function getUrl() {
return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null;
}
/**
* Get short description URL for a file based on the foreign API response,
* or if unavailable, the short URL is constructed from the foreign page ID.
*
* @return null|string
* @since 1.27
*/
public function getDescriptionShortUrl() {
if ( isset( $this->mInfo['descriptionshorturl'] ) ) {
return $this->mInfo['descriptionshorturl'];
} elseif ( isset( $this->mInfo['pageid'] ) ) {
$url = $this->repo->makeUrl( [ 'curid' => $this->mInfo['pageid'] ] );
if ( $url !== false ) {
return $url;
}
}
return null;
}
public function getUploader( int $audience = self::FOR_PUBLIC, ?Authority $performer = null ): ?UserIdentity {
if ( isset( $this->mInfo['user'] ) ) {
return UserIdentityValue::newExternal( $this->getRepoName(), $this->mInfo['user'] );
}
return null;
}
/**
* @param int $audience
* @param Authority|null $performer
* @return null|string
*/
public function getDescription( $audience = self::FOR_PUBLIC, ?Authority $performer = null ) {
return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null;
}
/**
* @return null|string
*/
public function getSha1() {
return isset( $this->mInfo['sha1'] )
? Wikimedia\base_convert( strval( $this->mInfo['sha1'] ), 16, 36, 31 )
: null;
}
/**
* @return string|false
*/
public function getTimestamp() {
return wfTimestamp( TS_MW,
isset( $this->mInfo['timestamp'] )
? strval( $this->mInfo['timestamp'] )
: null
);
}
/**
* @return string
*/
public function getMimeType() {
if ( !isset( $this->mInfo['mime'] ) ) {
$magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
$this->mInfo['mime'] = $magic->getMimeTypeFromExtensionOrNull( $this->getExtension() );
}
return $this->mInfo['mime'];
}
/**
* @return int|string
*/
public function getMediaType() {
if ( isset( $this->mInfo['mediatype'] ) ) {
return $this->mInfo['mediatype'];
}
$magic = MediaWikiServices::getInstance()->getMimeAnalyzer();
return $magic->getMediaType( null, $this->getMimeType() );
}
/**
* @return string|false
*/
public function getDescriptionUrl() {
return $this->mInfo['descriptionurl'] ?? false;
}
/**
* Only useful if we're locally caching thumbs anyway...
* @param string $suffix
* @return null|string
*/
public function getThumbPath( $suffix = '' ) {
if ( !$this->repo->canCacheThumbs() ) {
return null;
}
$path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath();
if ( $suffix ) {
$path .= $suffix . '/';
}
return $path;
}
/**
* @return string[]
*/
protected function getThumbnails() {
$dir = $this->getThumbPath( $this->getName() );
$iter = $this->repo->getBackend()->getFileList( [ 'dir' => $dir ] );
$files = [];
if ( $iter ) {
foreach ( $iter as $file ) {
$files[] = $file;
}
}
return $files;
}
public function purgeCache( $options = [] ) {
$this->purgeThumbnails( $options );
$this->purgeDescriptionPage();
}
private function purgeDescriptionPage() {
$services = MediaWikiServices::getInstance();
$langCode = $services->getContentLanguage()->getCode();
// Key must match File::getDescriptionText
$key = $this->repo->getLocalCacheKey( 'file-remote-description', $langCode, md5( $this->getName() ) );
$services->getMainWANObjectCache()->delete( $key );
}
/**
* @param array $options
*/
public function purgeThumbnails( $options = [] ) {
$key = $this->repo->getLocalCacheKey( 'file-thumb-url', sha1( $this->getName() ) );
MediaWikiServices::getInstance()->getMainWANObjectCache()->delete( $key );
$files = $this->getThumbnails();
// Give media handler a chance to filter the purge list
$handler = $this->getHandler();
if ( $handler ) {
$handler->filterThumbnailPurgeList( $files, $options );
}
$dir = $this->getThumbPath( $this->getName() );
$purgeList = [];
foreach ( $files as $file ) {
$purgeList[] = "{$dir}{$file}";
}
# Delete the thumbnails
$this->repo->quickPurgeBatch( $purgeList );
# Clear out the thumbnail directory if empty
$this->repo->quickCleanDir( $dir );
}
/**
* The thumbnail is created on the foreign server and fetched over internet
* @since 1.25
* @return bool
*/
public function isTransformedLocally() {
return false;
}
}
|