3 * Job for asynchronous rendering of thumbnails.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Job for asynchronous rendering of thumbnails.
29 class ThumbnailRenderJob
extends Job
{
30 public function __construct( Title
$title, array $params ) {
31 parent
::__construct( 'ThumbnailRender', $title, $params );
34 public function run() {
35 global $wgUploadThumbnailRenderMethod;
37 $transformParams = $this->params
['transformParams'];
39 $file = wfLocalFile( $this->title
);
40 $file->load( File
::READ_LATEST
);
42 if ( $file && $file->exists() ) {
43 if ( $wgUploadThumbnailRenderMethod === 'jobqueue' ) {
44 $thumb = $file->transform( $transformParams, File
::RENDER_NOW
);
46 if ( !$thumb ||
$thumb->isError() ) {
47 if ( $thumb instanceof MediaTransformError
) {
48 $this->setLastError( __METHOD__
. ': thumbnail couln\'t be generated:' .
51 $this->setLastError( __METHOD__
. ': thumbnail couln\'t be generated' );
56 } elseif ( $wgUploadThumbnailRenderMethod === 'http' ) {
57 return $this->hitThumbUrl( $file, $transformParams );
59 $this->setLastError( __METHOD__
. ': unknown thumbnail render method ' .
60 $wgUploadThumbnailRenderMethod );
64 $this->setLastError( __METHOD__
. ': file doesn\'t exist' );
70 * @param LocalFile $file
71 * @param array $transformParams
72 * @return bool Success status (error will be set via setLastError() when false)
74 protected function hitThumbUrl( LocalFile
$file, $transformParams ) {
75 global $wgUploadThumbnailRenderHttpCustomHost, $wgUploadThumbnailRenderHttpCustomDomain;
77 $handler = $file->getHandler();
79 $this->setLastError( __METHOD__
. ': could not get handler' );
81 } elseif ( !$handler->normaliseParams( $file, $transformParams ) ) {
82 $this->setLastError( __METHOD__
. ': failed to normalize' );
85 $thumbName = $file->thumbName( $transformParams );
86 $thumbUrl = $file->getThumbUrl( $thumbName );
88 if ( $thumbUrl === null ) {
89 $this->setLastError( __METHOD__
. ': could not get thumb URL' );
93 if ( $wgUploadThumbnailRenderHttpCustomDomain ) {
94 $parsedUrl = wfParseUrl( $thumbUrl );
96 if ( !isset( $parsedUrl['path'] ) ||
$parsedUrl['path'] === '' ) {
97 $this->setLastError( __METHOD__
. ": invalid thumb URL: $thumbUrl" );
101 $thumbUrl = '//' . $wgUploadThumbnailRenderHttpCustomDomain . $parsedUrl['path'];
104 wfDebug( __METHOD__
. ": hitting url {$thumbUrl}\n" );
106 // T203135 We don't wait for the request to complete, as this is mostly fire & forget.
107 // Looking at the HTTP status of requests that take less than 1s is a sanity check.
108 $request = MWHttpRequest
::factory( $thumbUrl,
109 [ 'method' => 'HEAD', 'followRedirects' => true, 'timeout' => 1 ],
113 if ( $wgUploadThumbnailRenderHttpCustomHost ) {
114 $request->setHeader( 'Host', $wgUploadThumbnailRenderHttpCustomHost );
117 $status = $request->execute();
118 $statusCode = $request->getStatus();
119 wfDebug( __METHOD__
. ": received status {$statusCode}\n" );
121 // 400 happens when requesting a size greater or equal than the original
122 // TODO use proper error signaling. 400 could mean a number of other things.
123 if ( $statusCode === 200 ||
$statusCode === 301 ||
$statusCode === 302 ||
$statusCode === 400 ) {
125 } elseif ( $statusCode ) {
126 $this->setLastError( __METHOD__
. ": incorrect HTTP status $statusCode when hitting $thumbUrl" );
127 } elseif ( $status->hasMessage( 'http-timed-out' ) ) {
128 // T203135 we ignore timeouts, as it would be inefficient for this job to wait for
129 // minutes for the slower thumbnails to complete.
132 $this->setLastError( __METHOD__
. ': HTTP request failure: '
133 . Status
::wrap( $status )->getWikiText( null, null, 'en' ) );
139 * Whether to retry the job.
142 public function allowRetries() {
143 // ThumbnailRenderJob is a warmup for the thumbnails cache,
144 // so loosing it is not a problem. Most times the job fails
145 // for non-renderable or missing images which will not be fixed
146 // by a retry, but will create additional load on the renderer.