Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 83 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
ThumbnailRenderJob | |
0.00% |
0 / 83 |
|
0.00% |
0 / 5 |
756 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
run | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
72 | |||
hitThumbUrl | |
0.00% |
0 / 43 |
|
0.00% |
0 / 1 |
210 | |||
maybeEnqueueNextPage | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
allowRetries | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | use MediaWiki\MainConfigNames; |
22 | use MediaWiki\MediaWikiServices; |
23 | use MediaWiki\Status\Status; |
24 | use MediaWiki\Title\Title; |
25 | use Wikimedia\Rdbms\IDBAccessObject; |
26 | |
27 | /** |
28 | * Job for asynchronous rendering of thumbnails, e.g. after new uploads. |
29 | * |
30 | * @ingroup JobQueue |
31 | */ |
32 | class ThumbnailRenderJob extends Job { |
33 | public function __construct( Title $title, array $params ) { |
34 | parent::__construct( 'ThumbnailRender', $title, $params ); |
35 | } |
36 | |
37 | public function run() { |
38 | $uploadThumbnailRenderMethod = MediaWikiServices::getInstance() |
39 | ->getMainConfig()->get( MainConfigNames::UploadThumbnailRenderMethod ); |
40 | |
41 | $transformParams = $this->params['transformParams']; |
42 | |
43 | $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo() |
44 | ->newFile( $this->title ); |
45 | $file->load( IDBAccessObject::READ_LATEST ); |
46 | |
47 | if ( $file && $file->exists() ) { |
48 | if ( $uploadThumbnailRenderMethod === 'jobqueue' ) { |
49 | $thumb = $file->transform( $transformParams, File::RENDER_NOW ); |
50 | |
51 | if ( !$thumb || $thumb->isError() ) { |
52 | if ( $thumb instanceof MediaTransformError ) { |
53 | $this->setLastError( __METHOD__ . ': thumbnail couldn\'t be generated:' . |
54 | $thumb->toText() ); |
55 | } else { |
56 | $this->setLastError( __METHOD__ . ': thumbnail couldn\'t be generated' ); |
57 | } |
58 | return false; |
59 | } |
60 | $this->maybeEnqueueNextPage( $transformParams ); |
61 | return true; |
62 | } elseif ( $uploadThumbnailRenderMethod === 'http' ) { |
63 | $res = $this->hitThumbUrl( $file, $transformParams ); |
64 | $this->maybeEnqueueNextPage( $transformParams ); |
65 | return $res; |
66 | } else { |
67 | $this->setLastError( __METHOD__ . ': unknown thumbnail render method ' . |
68 | $uploadThumbnailRenderMethod ); |
69 | return false; |
70 | } |
71 | } else { |
72 | $this->setLastError( __METHOD__ . ': file doesn\'t exist' ); |
73 | return false; |
74 | } |
75 | } |
76 | |
77 | /** |
78 | * @param LocalFile $file |
79 | * @param array $transformParams |
80 | * @return bool Success status (error will be set via setLastError() when false) |
81 | */ |
82 | protected function hitThumbUrl( LocalFile $file, $transformParams ) { |
83 | $config = MediaWikiServices::getInstance()->getMainConfig(); |
84 | $uploadThumbnailRenderHttpCustomHost = |
85 | $config->get( MainConfigNames::UploadThumbnailRenderHttpCustomHost ); |
86 | $uploadThumbnailRenderHttpCustomDomain = |
87 | $config->get( MainConfigNames::UploadThumbnailRenderHttpCustomDomain ); |
88 | $handler = $file->getHandler(); |
89 | if ( !$handler ) { |
90 | $this->setLastError( __METHOD__ . ': could not get handler' ); |
91 | return false; |
92 | } elseif ( !$handler->normaliseParams( $file, $transformParams ) ) { |
93 | $this->setLastError( __METHOD__ . ': failed to normalize' ); |
94 | return false; |
95 | } |
96 | $thumbName = $file->thumbName( $transformParams ); |
97 | $thumbUrl = $file->getThumbUrl( $thumbName ); |
98 | |
99 | if ( $thumbUrl === null ) { |
100 | $this->setLastError( __METHOD__ . ': could not get thumb URL' ); |
101 | return false; |
102 | } |
103 | |
104 | if ( $uploadThumbnailRenderHttpCustomDomain ) { |
105 | $parsedUrl = wfGetUrlUtils()->parse( $thumbUrl ); |
106 | |
107 | if ( !isset( $parsedUrl['path'] ) || $parsedUrl['path'] === '' ) { |
108 | $this->setLastError( __METHOD__ . ": invalid thumb URL: $thumbUrl" ); |
109 | return false; |
110 | } |
111 | |
112 | $thumbUrl = '//' . $uploadThumbnailRenderHttpCustomDomain . $parsedUrl['path']; |
113 | } |
114 | |
115 | wfDebug( __METHOD__ . ": hitting url {$thumbUrl}" ); |
116 | |
117 | // T203135 We don't wait for the request to complete, as this is mostly fire & forget. |
118 | // Looking at the HTTP status of requests that take less than 1s is a double check. |
119 | $request = MediaWikiServices::getInstance()->getHttpRequestFactory()->create( |
120 | $thumbUrl, |
121 | [ 'method' => 'HEAD', 'followRedirects' => true, 'timeout' => 1 ], |
122 | __METHOD__ |
123 | ); |
124 | |
125 | if ( $uploadThumbnailRenderHttpCustomHost ) { |
126 | $request->setHeader( 'Host', $uploadThumbnailRenderHttpCustomHost ); |
127 | } |
128 | |
129 | $status = $request->execute(); |
130 | $statusCode = $request->getStatus(); |
131 | wfDebug( __METHOD__ . ": received status {$statusCode}" ); |
132 | |
133 | // 400 happens when requesting a size greater or equal than the original |
134 | // TODO use proper error signaling. 400 could mean a number of other things. |
135 | if ( $statusCode === 200 || $statusCode === 301 || $statusCode === 302 || $statusCode === 400 ) { |
136 | return true; |
137 | } elseif ( $statusCode ) { |
138 | $this->setLastError( __METHOD__ . ": incorrect HTTP status $statusCode when hitting $thumbUrl" ); |
139 | } elseif ( $status->hasMessage( 'http-timed-out' ) ) { |
140 | // T203135 we ignore timeouts, as it would be inefficient for this job to wait for |
141 | // minutes for the slower thumbnails to complete. |
142 | return true; |
143 | } else { |
144 | $this->setLastError( __METHOD__ . ': HTTP request failure: ' |
145 | . Status::wrap( $status )->getWikiText( false, false, 'en' ) ); |
146 | } |
147 | return false; |
148 | } |
149 | |
150 | private function maybeEnqueueNextPage( $transformParams ) { |
151 | if ( |
152 | ( $this->params['enqueueNextPage'] ?? false ) && |
153 | ( $transformParams['page'] ?? 0 ) < ( $this->params['pageLimit'] ?? 0 ) |
154 | ) { |
155 | $transformParams['page'] += 1; |
156 | $job = new ThumbnailRenderJob( |
157 | $this->getTitle(), |
158 | [ |
159 | 'transformParams' => $transformParams, |
160 | 'enqueueNextPage' => true, |
161 | 'pageLimit' => $this->params['pageLimit'] |
162 | ] |
163 | ); |
164 | |
165 | MediaWikiServices::getInstance()->getJobQueueGroup()->lazyPush( [ $job ] ); |
166 | } |
167 | } |
168 | |
169 | /** |
170 | * Whether to retry the job. |
171 | * @return bool |
172 | */ |
173 | public function allowRetries() { |
174 | // ThumbnailRenderJob is a warmup for the thumbnails cache, |
175 | // so loosing it is not a problem. Most times the job fails |
176 | // for non-renderable or missing images which will not be fixed |
177 | // by a retry, but will create additional load on the renderer. |
178 | return false; |
179 | } |
180 | } |