Merge "Add MessagesBi.php"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / ThumbnailRenderJob.php
1 <?php
2 /**
3 * Job for asynchronous rendering of thumbnails.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup JobQueue
22 */
23
24 /**
25 * Job for asynchronous rendering of thumbnails.
26 *
27 * @ingroup JobQueue
28 */
29 class ThumbnailRenderJob extends Job {
30 public function __construct( Title $title, array $params ) {
31 parent::__construct( 'ThumbnailRender', $title, $params );
32 }
33
34 public function run() {
35 global $wgUploadThumbnailRenderMethod;
36
37 $transformParams = $this->params['transformParams'];
38
39 $file = wfLocalFile( $this->title );
40 $file->load( File::READ_LATEST );
41
42 if ( $file && $file->exists() ) {
43 if ( $wgUploadThumbnailRenderMethod === 'jobqueue' ) {
44 $thumb = $file->transform( $transformParams, File::RENDER_NOW );
45
46 if ( !$thumb || $thumb->isError() ) {
47 if ( $thumb instanceof MediaTransformError ) {
48 $this->setLastError( __METHOD__ . ': thumbnail couln\'t be generated:' .
49 $thumb->toText() );
50 } else {
51 $this->setLastError( __METHOD__ . ': thumbnail couln\'t be generated' );
52 }
53 return false;
54 }
55 return true;
56 } elseif ( $wgUploadThumbnailRenderMethod === 'http' ) {
57 return $this->hitThumbUrl( $file, $transformParams );
58 } else {
59 $this->setLastError( __METHOD__ . ': unknown thumbnail render method ' .
60 $wgUploadThumbnailRenderMethod );
61 return false;
62 }
63 } else {
64 $this->setLastError( __METHOD__ . ': file doesn\'t exist' );
65 return false;
66 }
67 }
68
69 /**
70 * @param LocalFile $file
71 * @param array $transformParams
72 * @return bool Success status (error will be set via setLastError() when false)
73 */
74 protected function hitThumbUrl( LocalFile $file, $transformParams ) {
75 global $wgUploadThumbnailRenderHttpCustomHost, $wgUploadThumbnailRenderHttpCustomDomain;
76
77 $handler = $file->getHandler();
78 if ( !$handler ) {
79 $this->setLastError( __METHOD__ . ': could not get handler' );
80 return false;
81 } elseif ( !$handler->normaliseParams( $file, $transformParams ) ) {
82 $this->setLastError( __METHOD__ . ': failed to normalize' );
83 return false;
84 }
85 $thumbName = $file->thumbName( $transformParams );
86 $thumbUrl = $file->getThumbUrl( $thumbName );
87
88 if ( $thumbUrl === null ) {
89 $this->setLastError( __METHOD__ . ': could not get thumb URL' );
90 return false;
91 }
92
93 if ( $wgUploadThumbnailRenderHttpCustomDomain ) {
94 $parsedUrl = wfParseUrl( $thumbUrl );
95
96 if ( !$parsedUrl || !isset( $parsedUrl['path'] ) || !strlen( $parsedUrl['path'] ) ) {
97 $this->setLastError( __METHOD__ . ": invalid thumb URL: $thumbUrl" );
98 return false;
99 }
100
101 $thumbUrl = '//' . $wgUploadThumbnailRenderHttpCustomDomain . $parsedUrl['path'];
102 }
103
104 wfDebug( __METHOD__ . ": hitting url {$thumbUrl}\n" );
105
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 ],
110 __METHOD__
111 );
112
113 if ( $wgUploadThumbnailRenderHttpCustomHost ) {
114 $request->setHeader( 'Host', $wgUploadThumbnailRenderHttpCustomHost );
115 }
116
117 $status = $request->execute();
118 $statusCode = $request->getStatus();
119 wfDebug( __METHOD__ . ": received status {$statusCode}\n" );
120
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 ) {
124 return true;
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.
130 return true;
131 } else {
132 $this->setLastError( __METHOD__ . ': HTTP request failure: '
133 . Status::wrap( $status )->getWikiText( null, null, 'en' ) );
134 }
135 return false;
136 }
137 }