Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / jobqueue / jobs / ThumbnailRenderJob.php
blobe0a23d3a0bdb3ace6fc817421f980ccbaaf4bd05
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use MediaWiki\MainConfigNames;
22 use MediaWiki\MediaWikiServices;
23 use MediaWiki\Status\Status;
24 use MediaWiki\Title\Title;
25 use Wikimedia\Rdbms\IDBAccessObject;
27 /**
28 * Job for asynchronous rendering of thumbnails, e.g. after new uploads.
30 * @ingroup JobQueue
32 class ThumbnailRenderJob extends Job {
33 public function __construct( Title $title, array $params ) {
34 parent::__construct( 'ThumbnailRender', $title, $params );
37 public function run() {
38 $uploadThumbnailRenderMethod = MediaWikiServices::getInstance()
39 ->getMainConfig()->get( MainConfigNames::UploadThumbnailRenderMethod );
41 $transformParams = $this->params['transformParams'];
43 $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
44 ->newFile( $this->title );
45 $file->load( IDBAccessObject::READ_LATEST );
47 if ( $file && $file->exists() ) {
48 if ( $uploadThumbnailRenderMethod === 'jobqueue' ) {
49 $thumb = $file->transform( $transformParams, File::RENDER_NOW );
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' );
58 return false;
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;
71 } else {
72 $this->setLastError( __METHOD__ . ': file doesn\'t exist' );
73 return false;
77 /**
78 * @param LocalFile $file
79 * @param array $transformParams
80 * @return bool Success status (error will be set via setLastError() when false)
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;
96 $thumbName = $file->thumbName( $transformParams );
97 $thumbUrl = $file->getThumbUrl( $thumbName );
99 if ( $thumbUrl === null ) {
100 $this->setLastError( __METHOD__ . ': could not get thumb URL' );
101 return false;
104 if ( $uploadThumbnailRenderHttpCustomDomain ) {
105 $parsedUrl = wfGetUrlUtils()->parse( $thumbUrl );
107 if ( !isset( $parsedUrl['path'] ) || $parsedUrl['path'] === '' ) {
108 $this->setLastError( __METHOD__ . ": invalid thumb URL: $thumbUrl" );
109 return false;
112 $thumbUrl = '//' . $uploadThumbnailRenderHttpCustomDomain . $parsedUrl['path'];
115 wfDebug( __METHOD__ . ": hitting url {$thumbUrl}" );
117 // T203135 We don't wait for the request to complete, as this is mostly fire & forget.
118 // Looking at the HTTP status of requests that take less than 1s is a double check.
119 $request = MediaWikiServices::getInstance()->getHttpRequestFactory()->create(
120 $thumbUrl,
121 [ 'method' => 'HEAD', 'followRedirects' => true, 'timeout' => 1 ],
122 __METHOD__
125 if ( $uploadThumbnailRenderHttpCustomHost ) {
126 $request->setHeader( 'Host', $uploadThumbnailRenderHttpCustomHost );
129 $status = $request->execute();
130 $statusCode = $request->getStatus();
131 wfDebug( __METHOD__ . ": received status {$statusCode}" );
133 // 400 happens when requesting a size greater or equal than the original
134 // TODO use proper error signaling. 400 could mean a number of other things.
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 // T203135 we ignore timeouts, as it would be inefficient for this job to wait for
141 // minutes for the slower thumbnails to complete.
142 return true;
143 } else {
144 $this->setLastError( __METHOD__ . ': HTTP request failure: '
145 . Status::wrap( $status )->getWikiText( false, false, 'en' ) );
147 return false;
150 private function maybeEnqueueNextPage( $transformParams ) {
151 if (
152 ( $this->params['enqueueNextPage'] ?? false ) &&
153 ( $transformParams['page'] ?? 0 ) < ( $this->params['pageLimit'] ?? 0 )
155 $transformParams['page']++;
156 $job = new ThumbnailRenderJob(
157 $this->getTitle(),
159 'transformParams' => $transformParams,
160 'enqueueNextPage' => true,
161 'pageLimit' => $this->params['pageLimit']
165 MediaWikiServices::getInstance()->getJobQueueGroup()->lazyPush( [ $job ] );
170 * Whether to retry the job.
171 * @return bool
173 public function allowRetries() {
174 // ThumbnailRenderJob is a warmup for the thumbnails cache,
175 // so loosing it is not a problem. Most times the job fails
176 // for non-renderable or missing images which will not be fixed
177 // by a retry, but will create additional load on the renderer.
178 return false;