Implement extension registration from an extension.json file
[mediawiki.git] / includes / jobqueue / jobs / ThumbnailRenderJob.php
blobdbc4f23ff87705ab11db5a6f73fe9af1be36be0d
1 <?php
2 /**
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
20 * @file
21 * @ingroup JobQueue
24 /**
25 * Job for asynchronous rendering of thumbnails.
27 * @ingroup JobQueue
29 class ThumbnailRenderJob extends Job {
30 public function __construct( $title, $params ) {
31 parent::__construct( 'ThumbnailRender', $title, $params );
34 public function run() {
35 global $wgUploadThumbnailRenderMethod;
37 $transformParams = $this->params['transformParams'];
39 $file = wfLocalFile( $this->title );
41 if ( $file && $file->exists() ) {
42 if ( $wgUploadThumbnailRenderMethod === 'jobqueue' ) {
43 $thumb = $file->transform( $transformParams, File::RENDER_NOW );
45 if ( $thumb && !$thumb->isError() ) {
46 return true;
47 } else {
48 $this->setLastError( __METHOD__ . ': thumbnail couln\'t be generated' );
49 return false;
51 } elseif ( $wgUploadThumbnailRenderMethod === 'http' ) {
52 $status = $this->hitThumbUrl( $file, $transformParams );
54 wfDebug( __METHOD__ . ": received status {$status}\n" );
56 if ( $status === 200 || $status === 301 || $status === 302 ) {
57 return true;
58 } elseif ( $status ) {
59 // Note that this currently happens (500) when requesting sizes larger then or
60 // equal to the original, which is harmless.
61 $this->setLastError( __METHOD__ . ': incorrect HTTP status ' . $status );
62 return false;
63 } else {
64 $this->setLastError( __METHOD__ . ': HTTP request failure' );
65 return false;
67 } else {
68 $this->setLastError( __METHOD__ . ': unknown thumbnail render method ' . $wgUploadThumbnailRenderMethod );
69 return false;
71 } else {
72 $this->setLastError( __METHOD__ . ': file doesn\'t exist' );
73 return false;
77 protected function hitThumbUrl( $file, $transformParams ) {
78 global $wgUploadThumbnailRenderHttpCustomHost, $wgUploadThumbnailRenderHttpCustomDomain;
80 $thumbName = $file->thumbName( $transformParams );
81 $thumbUrl = $file->getThumbUrl( $thumbName );
83 if ( $wgUploadThumbnailRenderHttpCustomDomain ) {
84 $parsedUrl = wfParseUrl( $thumbUrl );
86 if ( !$parsedUrl || !isset( $parsedUrl['path'] ) || !strlen( $parsedUrl['path'] ) ) {
87 return false;
90 $thumbUrl = '//' . $wgUploadThumbnailRenderHttpCustomDomain . $parsedUrl['path'];
93 wfDebug( __METHOD__ . ": hitting url {$thumbUrl}\n" );
95 $request = MWHttpRequest::factory( $thumbUrl, array(
96 'method' => 'HEAD',
97 'followRedirects' => true ) );
99 if ( $wgUploadThumbnailRenderHttpCustomHost ) {
100 $request->setHeader( 'Host', $wgUploadThumbnailRenderHttpCustomHost );
103 $status = $request->execute();
105 return $request->getStatus();