| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\MainConfigNames; |
| 22 |
use MediaWiki\MediaWikiServices; |
| 23 |
use MediaWiki\Status\Status; |
| 24 |
use MediaWiki\Title\Title; |
| 25 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class ThumbnailRenderJob extends Job { |
| 33 |
public function __construct( Title $title, array $params ) { |
| 34 |
parent::__construct( 'ThumbnailRender', $title, $params ); |
| 35 |
} |
| 36 |
|
| 37 |
public function run() { |
| 38 |
$uploadThumbnailRenderMethod = MediaWikiServices::getInstance() |
| 39 |
->getMainConfig()->get( MainConfigNames::UploadThumbnailRenderMethod ); |
| 40 |
|
| 41 |
$transformParams = $this->params['transformParams']; |
| 42 |
|
| 43 |
$file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo() |
| 44 |
->newFile( $this->title ); |
| 45 |
$file->load( IDBAccessObject::READ_LATEST ); |
| 46 |
|
| 47 |
if ( $file && $file->exists() ) { |
| 48 |
if ( $uploadThumbnailRenderMethod === 'jobqueue' ) { |
| 49 |
$thumb = $file->transform( $transformParams, File::RENDER_NOW ); |
| 50 |
|
| 51 |
if ( !$thumb || $thumb->isError() ) { |
| 52 |
if ( $thumb instanceof MediaTransformError ) { |
| 53 |
$this->setLastError( __METHOD__ . ': thumbnail couldn\'t be generated:' . |
| 54 |
$thumb->toText() ); |
| 55 |
} else { |
| 56 |
$this->setLastError( __METHOD__ . ': thumbnail couldn\'t be generated' ); |
| 57 |
} |
| 58 |
return false; |
| 59 |
} |
| 60 |
$this->maybeEnqueueNextPage( $transformParams ); |
| 61 |
return true; |
| 62 |
} elseif ( $uploadThumbnailRenderMethod === 'http' ) { |
| 63 |
$res = $this->hitThumbUrl( $file, $transformParams ); |
| 64 |
$this->maybeEnqueueNextPage( $transformParams ); |
| 65 |
return $res; |
| 66 |
} else { |
| 67 |
$this->setLastError( __METHOD__ . ': unknown thumbnail render method ' . |
| 68 |
$uploadThumbnailRenderMethod ); |
| 69 |
return false; |
| 70 |
} |
| 71 |
} else { |
| 72 |
$this->setLastError( __METHOD__ . ': file doesn\'t exist' ); |
| 73 |
return false; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
protected function hitThumbUrl( LocalFile $file, $transformParams ) { |
| 83 |
$config = MediaWikiServices::getInstance()->getMainConfig(); |
| 84 |
$uploadThumbnailRenderHttpCustomHost = |
| 85 |
$config->get( MainConfigNames::UploadThumbnailRenderHttpCustomHost ); |
| 86 |
$uploadThumbnailRenderHttpCustomDomain = |
| 87 |
$config->get( MainConfigNames::UploadThumbnailRenderHttpCustomDomain ); |
| 88 |
$handler = $file->getHandler(); |
| 89 |
if ( !$handler ) { |
| 90 |
$this->setLastError( __METHOD__ . ': could not get handler' ); |
| 91 |
return false; |
| 92 |
} elseif ( !$handler->normaliseParams( $file, $transformParams ) ) { |
| 93 |
$this->setLastError( __METHOD__ . ': failed to normalize' ); |
| 94 |
return false; |
| 95 |
} |
| 96 |
$thumbName = $file->thumbName( $transformParams ); |
| 97 |
$thumbUrl = $file->getThumbUrl( $thumbName ); |
| 98 |
|
| 99 |
if ( $thumbUrl === null ) { |
| 100 |
$this->setLastError( __METHOD__ . ': could not get thumb URL' ); |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
if ( $uploadThumbnailRenderHttpCustomDomain ) { |
| 105 |
$parsedUrl = wfGetUrlUtils()->parse( $thumbUrl ); |
| 106 |
|
| 107 |
if ( !isset( $parsedUrl['path'] ) || $parsedUrl['path'] === '' ) { |
| 108 |
$this->setLastError( __METHOD__ . ": invalid thumb URL: $thumbUrl" ); |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
$thumbUrl = '//' . $uploadThumbnailRenderHttpCustomDomain . $parsedUrl['path']; |
| 113 |
} |
| 114 |
|
| 115 |
wfDebug( __METHOD__ . ": hitting url {$thumbUrl}" ); |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
$request = MediaWikiServices::getInstance()->getHttpRequestFactory()->create( |
| 120 |
$thumbUrl, |
| 121 |
[ 'method' => 'HEAD', 'followRedirects' => true, 'timeout' => 1 ], |
| 122 |
__METHOD__ |
| 123 |
); |
| 124 |
|
| 125 |
if ( $uploadThumbnailRenderHttpCustomHost ) { |
| 126 |
$request->setHeader( 'Host', $uploadThumbnailRenderHttpCustomHost ); |
| 127 |
} |
| 128 |
|
| 129 |
$status = $request->execute(); |
| 130 |
$statusCode = $request->getStatus(); |
| 131 |
wfDebug( __METHOD__ . ": received status {$statusCode}" ); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
if ( $statusCode === 200 || $statusCode === 301 || $statusCode === 302 || $statusCode === 400 ) { |
| 136 |
return true; |
| 137 |
} elseif ( $statusCode ) { |
| 138 |
$this->setLastError( __METHOD__ . ": incorrect HTTP status $statusCode when hitting $thumbUrl" ); |
| 139 |
} elseif ( $status->hasMessage( 'http-timed-out' ) ) { |
| 140 |
|
| 141 |
|
| 142 |
return true; |
| 143 |
} else { |
| 144 |
$this->setLastError( __METHOD__ . ': HTTP request failure: ' |
| 145 |
. Status::wrap( $status )->getWikiText( false, false, 'en' ) ); |
| 146 |
} |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
private function maybeEnqueueNextPage( $transformParams ) { |
| 151 |
if ( |
| 152 |
( $this->params['enqueueNextPage'] ?? false ) && |
| 153 |
( $transformParams['page'] ?? 0 ) < ( $this->params['pageLimit'] ?? 0 ) |
| 154 |
) { |
| 155 |
$transformParams['page'] += 1; |
| 156 |
$job = new ThumbnailRenderJob( |
| 157 |
$this->getTitle(), |
| 158 |
[ |
| 159 |
'transformParams' => $transformParams, |
| 160 |
'enqueueNextPage' => true, |
| 161 |
'pageLimit' => $this->params['pageLimit'] |
| 162 |
] |
| 163 |
); |
| 164 |
|
| 165 |
MediaWikiServices::getInstance()->getJobQueueGroup()->lazyPush( [ $job ] ); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function allowRetries() { |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
return false; |
| 179 |
} |
| 180 |
} |