Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[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 use MediaWiki\MediaWikiServices;
25
26 /**
27 * Job for asynchronous rendering of thumbnails.
28 *
29 * @ingroup JobQueue
30 */
31 class ThumbnailRenderJob extends Job {
32 public function __construct( Title $title, array $params ) {
33 parent::__construct( 'ThumbnailRender', $title, $params );
34 }
35
36 public function run() {
37 global $wgUploadThumbnailRenderMethod;
38
39 $transformParams = $this->params['transformParams'];
40
41 $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
42 ->newFile( $this->title );
43 $file->load( File::READ_LATEST );
44
45 if ( $file && $file->exists() ) {
46 if ( $wgUploadThumbnailRenderMethod === 'jobqueue' ) {
47 $thumb = $file->transform( $transformParams, File::RENDER_NOW );
48
49 if ( !$thumb || $thumb->isError() ) {
50 if ( $thumb instanceof MediaTransformError ) {
51 $this->setLastError( __METHOD__ . ': thumbnail couln\'t be generated:' .
52 $thumb->toText() );
53 } else {
54 $this->setLastError( __METHOD__ . ': thumbnail couln\'t be generated' );
55 }
56 return false;
57 }
58 return true;
59 } elseif ( $wgUploadThumbnailRenderMethod === 'http' ) {
60 return $this->hitThumbUrl( $file, $transformParams );
61 } else {
62 $this->setLastError( __METHOD__ . ': unknown thumbnail render method ' .
63 $wgUploadThumbnailRenderMethod );
64 return false;
65 }
66 } else {
67 $this->setLastError( __METHOD__ . ': file doesn\'t exist' );
68 return false;
69 }
70 }
71
72 /**
73 * @param LocalFile $file
74 * @param array $transformParams
75 * @return bool Success status (error will be set via setLastError() when false)
76 */
77 protected function hitThumbUrl( LocalFile $file, $transformParams ) {
78 global $wgUploadThumbnailRenderHttpCustomHost, $wgUploadThumbnailRenderHttpCustomDomain;
79
80 $handler = $file->getHandler();
81 if ( !$handler ) {
82 $this->setLastError( __METHOD__ . ': could not get handler' );
83 return false;
84 } elseif ( !$handler->normaliseParams( $file, $transformParams ) ) {
85 $this->setLastError( __METHOD__ . ': failed to normalize' );
86 return false;
87 }
88 $thumbName = $file->thumbName( $transformParams );
89 $thumbUrl = $file->getThumbUrl( $thumbName );
90
91 if ( $thumbUrl === null ) {
92 $this->setLastError( __METHOD__ . ': could not get thumb URL' );
93 return false;
94 }
95
96 if ( $wgUploadThumbnailRenderHttpCustomDomain ) {
97 $parsedUrl = wfParseUrl( $thumbUrl );
98
99 if ( !isset( $parsedUrl['path'] ) || $parsedUrl['path'] === '' ) {
100 $this->setLastError( __METHOD__ . ": invalid thumb URL: $thumbUrl" );
101 return false;
102 }
103
104 $thumbUrl = '//' . $wgUploadThumbnailRenderHttpCustomDomain . $parsedUrl['path'];
105 }
106
107 wfDebug( __METHOD__ . ": hitting url {$thumbUrl}\n" );
108
109 // T203135 We don't wait for the request to complete, as this is mostly fire & forget.
110 // Looking at the HTTP status of requests that take less than 1s is a sanity check.
111 $request = MWHttpRequest::factory( $thumbUrl,
112 [ 'method' => 'HEAD', 'followRedirects' => true, 'timeout' => 1 ],
113 __METHOD__
114 );
115
116 if ( $wgUploadThumbnailRenderHttpCustomHost ) {
117 $request->setHeader( 'Host', $wgUploadThumbnailRenderHttpCustomHost );
118 }
119
120 $status = $request->execute();
121 $statusCode = $request->getStatus();
122 wfDebug( __METHOD__ . ": received status {$statusCode}\n" );
123
124 // 400 happens when requesting a size greater or equal than the original
125 // TODO use proper error signaling. 400 could mean a number of other things.
126 if ( $statusCode === 200 || $statusCode === 301 || $statusCode === 302 || $statusCode === 400 ) {
127 return true;
128 } elseif ( $statusCode ) {
129 $this->setLastError( __METHOD__ . ": incorrect HTTP status $statusCode when hitting $thumbUrl" );
130 } elseif ( $status->hasMessage( 'http-timed-out' ) ) {
131 // T203135 we ignore timeouts, as it would be inefficient for this job to wait for
132 // minutes for the slower thumbnails to complete.
133 return true;
134 } else {
135 $this->setLastError( __METHOD__ . ': HTTP request failure: '
136 . Status::wrap( $status )->getWikiText( null, null, 'en' ) );
137 }
138 return false;
139 }
140
141 /**
142 * Whether to retry the job.
143 * @return bool
144 */
145 public function allowRetries() {
146 // ThumbnailRenderJob is a warmup for the thumbnails cache,
147 // so loosing it is not a problem. Most times the job fails
148 // for non-renderable or missing images which will not be fixed
149 // by a retry, but will create additional load on the renderer.
150 return false;
151 }
152 }