| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\MediaWikiServices; |
| 22 |
use MediaWiki\Permissions\Authority; |
| 23 |
use MediaWiki\Title\Title; |
| 24 |
use MediaWiki\User\UserIdentity; |
| 25 |
use MediaWiki\User\UserIdentityValue; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class ForeignAPIFile extends File { |
| 33 |
|
| 34 |
private $mExists; |
| 35 |
|
| 36 |
private $mInfo; |
| 37 |
|
| 38 |
|
| 39 |
protected $repoClass = ForeignAPIRepo::class; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function __construct( $title, $repo, $info, $exists = false ) { |
| 48 |
parent::__construct( $title, $repo ); |
| 49 |
|
| 50 |
$this->mInfo = $info; |
| 51 |
$this->mExists = $exists; |
| 52 |
|
| 53 |
$this->assertRepoDefined(); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static function newFromTitle( Title $title, $repo ) { |
| 62 |
$data = $repo->fetchImageQuery( [ |
| 63 |
'titles' => 'File:' . $title->getDBkey(), |
| 64 |
'iiprop' => self::getProps(), |
| 65 |
'prop' => 'imageinfo', |
| 66 |
'iimetadataversion' => MediaHandler::getMetadataVersion(), |
| 67 |
|
| 68 |
|
| 69 |
'iiextmetadatamultilang' => 1, |
| 70 |
] ); |
| 71 |
|
| 72 |
$info = $repo->getImageInfo( $data ); |
| 73 |
|
| 74 |
if ( $info ) { |
| 75 |
$lastRedirect = count( $data['query']['redirects'] ?? [] ) - 1; |
| 76 |
if ( $lastRedirect >= 0 ) { |
| 77 |
|
| 78 |
$newtitle = Title::newFromText( $data['query']['redirects'][$lastRedirect]['to'] ); |
| 79 |
$img = new self( $newtitle, $repo, $info, true ); |
| 80 |
$img->redirectedFrom( $title->getDBkey() ); |
| 81 |
} else { |
| 82 |
$img = new self( $title, $repo, $info, true ); |
| 83 |
} |
| 84 |
|
| 85 |
return $img; |
| 86 |
} else { |
| 87 |
return null; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static function getProps() { |
| 96 |
return 'timestamp|user|comment|url|size|sha1|metadata|mime|mediatype|extmetadata'; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function getRepo() { |
| 103 |
return $this->repo; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function exists() { |
| 112 |
return $this->mExists; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public function getPath() { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public function transform( $params, $flags = 0 ) { |
| 128 |
if ( !$this->canRender() ) { |
| 129 |
|
| 130 |
return parent::transform( $params, $flags ); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
$otherParams = $this->handler->makeParamString( $params ); |
| 136 |
$width = $params['width'] ?? -1; |
| 137 |
$height = $params['height'] ?? -1; |
| 138 |
$thumbUrl = false; |
| 139 |
|
| 140 |
if ( $width > 0 || $height > 0 ) { |
| 141 |
|
| 142 |
$thumbUrl = $this->repo->getThumbUrlFromCache( |
| 143 |
$this->getName(), |
| 144 |
$width, |
| 145 |
$height, |
| 146 |
$otherParams |
| 147 |
); |
| 148 |
} elseif ( $this->getMediaType() === MEDIATYPE_AUDIO ) { |
| 149 |
|
| 150 |
$thumbUrl = '/'; |
| 151 |
} |
| 152 |
if ( $thumbUrl === false ) { |
| 153 |
global $wgLang; |
| 154 |
|
| 155 |
return $this->repo->getThumbError( |
| 156 |
$this->getName(), |
| 157 |
$width, |
| 158 |
$height, |
| 159 |
$otherParams, |
| 160 |
$wgLang->getCode() |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
return $this->handler->getTransform( $this, 'bogus', $thumbUrl, $params ); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function getWidth( $page = 1 ) { |
| 174 |
return (int)( $this->mInfo['width'] ?? 0 ); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public function getHeight( $page = 1 ) { |
| 182 |
return (int)( $this->mInfo['height'] ?? 0 ); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public function getMetadata() { |
| 189 |
if ( isset( $this->mInfo['metadata'] ) ) { |
| 190 |
return serialize( self::parseMetadata( $this->mInfo['metadata'] ) ); |
| 191 |
} |
| 192 |
|
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public function getMetadataArray(): array { |
| 200 |
if ( isset( $this->mInfo['metadata'] ) ) { |
| 201 |
return self::parseMetadata( $this->mInfo['metadata'] ); |
| 202 |
} |
| 203 |
|
| 204 |
return []; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
public function getExtendedMetadata() { |
| 212 |
return $this->mInfo['extmetadata'] ?? null; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public static function parseMetadata( $metadata ) { |
| 220 |
if ( !is_array( $metadata ) ) { |
| 221 |
return [ '_error' => $metadata ]; |
| 222 |
} |
| 223 |
'@phan-var array[] $metadata'; |
| 224 |
$ret = []; |
| 225 |
foreach ( $metadata as $meta ) { |
| 226 |
$ret[$meta['name']] = self::parseMetadataValue( $meta['value'] ); |
| 227 |
} |
| 228 |
|
| 229 |
return $ret; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
private static function parseMetadataValue( $metadata ) { |
| 237 |
if ( !is_array( $metadata ) ) { |
| 238 |
return $metadata; |
| 239 |
} |
| 240 |
'@phan-var array[] $metadata'; |
| 241 |
$ret = []; |
| 242 |
foreach ( $metadata as $meta ) { |
| 243 |
$ret[$meta['name']] = self::parseMetadataValue( $meta['value'] ); |
| 244 |
} |
| 245 |
|
| 246 |
return $ret; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public function getSize() { |
| 253 |
return isset( $this->mInfo['size'] ) ? intval( $this->mInfo['size'] ) : null; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public function getUrl() { |
| 260 |
return isset( $this->mInfo['url'] ) ? strval( $this->mInfo['url'] ) : null; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public function getDescriptionShortUrl() { |
| 271 |
if ( isset( $this->mInfo['descriptionshorturl'] ) ) { |
| 272 |
return $this->mInfo['descriptionshorturl']; |
| 273 |
} elseif ( isset( $this->mInfo['pageid'] ) ) { |
| 274 |
$url = $this->repo->makeUrl( [ 'curid' => $this->mInfo['pageid'] ] ); |
| 275 |
if ( $url !== false ) { |
| 276 |
return $url; |
| 277 |
} |
| 278 |
} |
| 279 |
return null; |
| 280 |
} |
| 281 |
|
| 282 |
public function getUploader( int $audience = self::FOR_PUBLIC, ?Authority $performer = null ): ?UserIdentity { |
| 283 |
if ( isset( $this->mInfo['user'] ) ) { |
| 284 |
return UserIdentityValue::newExternal( $this->getRepoName(), $this->mInfo['user'] ); |
| 285 |
} |
| 286 |
return null; |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public function getDescription( $audience = self::FOR_PUBLIC, ?Authority $performer = null ) { |
| 295 |
return isset( $this->mInfo['comment'] ) ? strval( $this->mInfo['comment'] ) : null; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
public function getSha1() { |
| 302 |
return isset( $this->mInfo['sha1'] ) |
| 303 |
? Wikimedia\base_convert( strval( $this->mInfo['sha1'] ), 16, 36, 31 ) |
| 304 |
: null; |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
public function getTimestamp() { |
| 311 |
return wfTimestamp( TS_MW, |
| 312 |
isset( $this->mInfo['timestamp'] ) |
| 313 |
? strval( $this->mInfo['timestamp'] ) |
| 314 |
: null |
| 315 |
); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public function getMimeType() { |
| 322 |
if ( !isset( $this->mInfo['mime'] ) ) { |
| 323 |
$magic = MediaWikiServices::getInstance()->getMimeAnalyzer(); |
| 324 |
$this->mInfo['mime'] = $magic->getMimeTypeFromExtensionOrNull( $this->getExtension() ); |
| 325 |
} |
| 326 |
|
| 327 |
return $this->mInfo['mime']; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public function getMediaType() { |
| 334 |
if ( isset( $this->mInfo['mediatype'] ) ) { |
| 335 |
return $this->mInfo['mediatype']; |
| 336 |
} |
| 337 |
$magic = MediaWikiServices::getInstance()->getMimeAnalyzer(); |
| 338 |
|
| 339 |
return $magic->getMediaType( null, $this->getMimeType() ); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public function getDescriptionUrl() { |
| 346 |
return $this->mInfo['descriptionurl'] ?? false; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
public function getThumbPath( $suffix = '' ) { |
| 355 |
if ( !$this->repo->canCacheThumbs() ) { |
| 356 |
return null; |
| 357 |
} |
| 358 |
|
| 359 |
$path = $this->repo->getZonePath( 'thumb' ) . '/' . $this->getHashPath(); |
| 360 |
if ( $suffix ) { |
| 361 |
$path .= $suffix . '/'; |
| 362 |
} |
| 363 |
return $path; |
| 364 |
} |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
protected function getThumbnails() { |
| 370 |
$dir = $this->getThumbPath( $this->getName() ); |
| 371 |
$iter = $this->repo->getBackend()->getFileList( [ 'dir' => $dir ] ); |
| 372 |
|
| 373 |
$files = []; |
| 374 |
if ( $iter ) { |
| 375 |
foreach ( $iter as $file ) { |
| 376 |
$files[] = $file; |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
return $files; |
| 381 |
} |
| 382 |
|
| 383 |
public function purgeCache( $options = [] ) { |
| 384 |
$this->purgeThumbnails( $options ); |
| 385 |
$this->purgeDescriptionPage(); |
| 386 |
} |
| 387 |
|
| 388 |
private function purgeDescriptionPage() { |
| 389 |
$services = MediaWikiServices::getInstance(); |
| 390 |
$langCode = $services->getContentLanguage()->getCode(); |
| 391 |
|
| 392 |
|
| 393 |
$key = $this->repo->getLocalCacheKey( 'file-remote-description', $langCode, md5( $this->getName() ) ); |
| 394 |
$services->getMainWANObjectCache()->delete( $key ); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
public function purgeThumbnails( $options = [] ) { |
| 401 |
$key = $this->repo->getLocalCacheKey( 'file-thumb-url', sha1( $this->getName() ) ); |
| 402 |
MediaWikiServices::getInstance()->getMainWANObjectCache()->delete( $key ); |
| 403 |
|
| 404 |
$files = $this->getThumbnails(); |
| 405 |
|
| 406 |
$handler = $this->getHandler(); |
| 407 |
if ( $handler ) { |
| 408 |
$handler->filterThumbnailPurgeList( $files, $options ); |
| 409 |
} |
| 410 |
|
| 411 |
$dir = $this->getThumbPath( $this->getName() ); |
| 412 |
$purgeList = []; |
| 413 |
foreach ( $files as $file ) { |
| 414 |
$purgeList[] = "{$dir}{$file}"; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
$this->repo->quickPurgeBatch( $purgeList ); |
| 419 |
|
| 420 |
$this->repo->quickCleanDir( $dir ); |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
public function isTransformedLocally() { |
| 429 |
return false; |
| 430 |
} |
| 431 |
} |