Merge "libs/Message: Improve documentation"
[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 = MediaWikiServices::getInstance()->getHttpRequestFactory()->create(
112 $thumbUrl,
113 [ 'method' => 'HEAD', 'followRedirects' => true, 'timeout' => 1 ],
114 __METHOD__
115 );
116
117 if ( $wgUploadThumbnailRenderHttpCustomHost ) {
118 $request->setHeader( 'Host', $wgUploadThumbnailRenderHttpCustomHost );
119 }
120
121 $status = $request->execute();
122 $statusCode = $request->getStatus();
123 wfDebug( __METHOD__ . ": received status {$statusCode}\n" );
124
125 // 400 happens when requesting a size greater or equal than the original
126 // TODO use proper error signaling. 400 could mean a number of other things.
127 if ( $statusCode === 200 || $statusCode === 301 || $statusCode === 302 || $statusCode === 400 ) {
128 return true;
129 } elseif ( $statusCode ) {
130 $this->setLastError( __METHOD__ . ": incorrect HTTP status $statusCode when hitting $thumbUrl" );
131 } elseif ( $status->hasMessage( 'http-timed-out' ) ) {
132 // T203135 we ignore timeouts, as it would be inefficient for this job to wait for
133 // minutes for the slower thumbnails to complete.
134 return true;
135 } else {
136 $this->setLastError( __METHOD__ . ': HTTP request failure: '
137 . Status::wrap( $status )->getWikiText( null, null, 'en' ) );
138 }
139 return false;
140 }
141
142 /**
143 * Whether to retry the job.
144 * @return bool
145 */
146 public function allowRetries() {
147 // ThumbnailRenderJob is a warmup for the thumbnails cache,
148 // so loosing it is not a problem. Most times the job fails
149 // for non-renderable or missing images which will not be fixed
150 // by a retry, but will create additional load on the renderer.
151 return false;
152 }
153 }