Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 84 |
|
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 | namespace MediaWiki\JobQueue\Jobs; |
22 | |
23 | use MediaTransformError; |
24 | use MediaWiki\FileRepo\File\File; |
25 | use MediaWiki\FileRepo\File\LocalFile; |
26 | use MediaWiki\JobQueue\Job; |
27 | use MediaWiki\MainConfigNames; |
28 | use MediaWiki\MediaWikiServices; |
29 | use MediaWiki\Status\Status; |
30 | use MediaWiki\Title\Title; |
31 | use Wikimedia\Rdbms\IDBAccessObject; |
32 | |
33 | /** |
34 | * Job for asynchronous rendering of thumbnails, e.g. after new uploads. |
35 | * |
36 | * @ingroup JobQueue |
37 | */ |
38 | class ThumbnailRenderJob extends Job { |
39 | public function __construct( Title $title, array $params ) { |
40 | parent::__construct( 'ThumbnailRender', $title, $params ); |
41 | } |
42 | |
43 | public function run() { |
44 | $uploadThumbnailRenderMethod = MediaWikiServices::getInstance() |
45 | ->getMainConfig()->get( MainConfigNames::UploadThumbnailRenderMethod ); |
46 | |
47 | $transformParams = $this->params['transformParams']; |
48 | |
49 | $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo() |
50 | ->newFile( $this->title ); |
51 | $file->load( IDBAccessObject::READ_LATEST ); |
52 | |
53 | if ( $file && $file->exists() ) { |
54 | if ( $uploadThumbnailRenderMethod === 'jobqueue' ) { |
55 | $thumb = $file->transform( $transformParams, File::RENDER_NOW ); |
56 | |
57 | if ( !$thumb || $thumb->isError() ) { |
58 | if ( $thumb instanceof MediaTransformError ) { |
59 | $this->setLastError( __METHOD__ . ': thumbnail couldn\'t be generated:' . |
60 | $thumb->toText() ); |
61 | } else { |
62 | $this->setLastError( __METHOD__ . ': thumbnail couldn\'t be generated' ); |
63 | } |
64 | return false; |
65 | } |
66 | $this->maybeEnqueueNextPage( $transformParams ); |
67 | return true; |
68 | } elseif ( $uploadThumbnailRenderMethod === 'http' ) { |
69 | $res = $this->hitThumbUrl( $file, $transformParams ); |
70 | $this->maybeEnqueueNextPage( $transformParams ); |
71 | return $res; |
72 | } else { |
73 | $this->setLastError( __METHOD__ . ': unknown thumbnail render method ' . |
74 | $uploadThumbnailRenderMethod ); |
75 | return false; |
76 | } |
77 | } else { |
78 | $this->setLastError( __METHOD__ . ': file doesn\'t exist' ); |
79 | return false; |
80 | } |
81 | } |
82 | |
83 | /** |
84 | * @param LocalFile $file |
85 | * @param array $transformParams |
86 | * @return bool Success status (error will be set via setLastError() when false) |
87 | */ |
88 | protected function hitThumbUrl( LocalFile $file, $transformParams ) { |
89 | $config = MediaWikiServices::getInstance()->getMainConfig(); |
90 | $uploadThumbnailRenderHttpCustomHost = |
91 | $config->get( MainConfigNames::UploadThumbnailRenderHttpCustomHost ); |
92 | $uploadThumbnailRenderHttpCustomDomain = |
93 | $config->get( MainConfigNames::UploadThumbnailRenderHttpCustomDomain ); |
94 | $handler = $file->getHandler(); |
95 | if ( !$handler ) { |
96 | $this->setLastError( __METHOD__ . ': could not get handler' ); |
97 | return false; |
98 | } elseif ( !$handler->normaliseParams( $file, $transformParams ) ) { |
99 | $this->setLastError( __METHOD__ . ': failed to normalize' ); |
100 | return false; |
101 | } |
102 | $thumbName = $file->thumbName( $transformParams ); |
103 | $thumbUrl = $file->getThumbUrl( $thumbName ); |
104 | |
105 | if ( $thumbUrl === null ) { |
106 | $this->setLastError( __METHOD__ . ': could not get thumb URL' ); |
107 | return false; |
108 | } |
109 | |
110 | if ( $uploadThumbnailRenderHttpCustomDomain ) { |
111 | $parsedUrl = wfGetUrlUtils()->parse( $thumbUrl ); |
112 | |
113 | if ( !isset( $parsedUrl['path'] ) || $parsedUrl['path'] === '' ) { |
114 | $this->setLastError( __METHOD__ . ": invalid thumb URL: $thumbUrl" ); |
115 | return false; |
116 | } |
117 | |
118 | $thumbUrl = '//' . $uploadThumbnailRenderHttpCustomDomain . $parsedUrl['path']; |
119 | } |
120 | |
121 | wfDebug( __METHOD__ . ": hitting url {$thumbUrl}" ); |
122 | |
123 | // T203135 We don't wait for the request to complete, as this is mostly fire & forget. |
124 | // Looking at the HTTP status of requests that take less than 1s is a double check. |
125 | $request = MediaWikiServices::getInstance()->getHttpRequestFactory()->create( |
126 | $thumbUrl, |
127 | [ 'method' => 'HEAD', 'followRedirects' => true, 'timeout' => 1 ], |
128 | __METHOD__ |
129 | ); |
130 | |
131 | if ( $uploadThumbnailRenderHttpCustomHost ) { |
132 | $request->setHeader( 'Host', $uploadThumbnailRenderHttpCustomHost ); |
133 | } |
134 | |
135 | $status = $request->execute(); |
136 | $statusCode = $request->getStatus(); |
137 | wfDebug( __METHOD__ . ": received status {$statusCode}" ); |
138 | |
139 | // 400 happens when requesting a size greater or equal than the original |
140 | // TODO use proper error signaling. 400 could mean a number of other things. |
141 | if ( $statusCode === 200 || $statusCode === 301 || $statusCode === 302 || $statusCode === 400 ) { |
142 | return true; |
143 | } elseif ( $statusCode ) { |
144 | $this->setLastError( __METHOD__ . ": incorrect HTTP status $statusCode when hitting $thumbUrl" ); |
145 | } elseif ( $status->hasMessage( 'http-timed-out' ) ) { |
146 | // T203135 we ignore timeouts, as it would be inefficient for this job to wait for |
147 | // minutes for the slower thumbnails to complete. |
148 | return true; |
149 | } else { |
150 | $this->setLastError( __METHOD__ . ': HTTP request failure: ' |
151 | . Status::wrap( $status )->getWikiText( false, false, 'en' ) ); |
152 | } |
153 | return false; |
154 | } |
155 | |
156 | private function maybeEnqueueNextPage( array $transformParams ) { |
157 | if ( |
158 | ( $this->params['enqueueNextPage'] ?? false ) && |
159 | ( $transformParams['page'] ?? 0 ) < ( $this->params['pageLimit'] ?? 0 ) |
160 | ) { |
161 | $transformParams['page']++; |
162 | $job = new ThumbnailRenderJob( |
163 | $this->getTitle(), |
164 | [ |
165 | 'transformParams' => $transformParams, |
166 | 'enqueueNextPage' => true, |
167 | 'pageLimit' => $this->params['pageLimit'] |
168 | ] |
169 | ); |
170 | |
171 | MediaWikiServices::getInstance()->getJobQueueGroup()->lazyPush( [ $job ] ); |
172 | } |
173 | } |
174 | |
175 | /** |
176 | * Whether to retry the job. |
177 | * @return bool |
178 | */ |
179 | public function allowRetries() { |
180 | // ThumbnailRenderJob is a warmup for the thumbnails cache, |
181 | // so loosing it is not a problem. Most times the job fails |
182 | // for non-renderable or missing images which will not be fixed |
183 | // by a retry, but will create additional load on the renderer. |
184 | return false; |
185 | } |
186 | } |
187 | |
188 | /** @deprecated class alias since 1.44 */ |
189 | class_alias( ThumbnailRenderJob::class, 'ThumbnailRenderJob' ); |