MediaWiki master
ThumbnailRenderJob.php
Go to the documentation of this file.
1<?php
22
32
38class 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
88 protected function hitThumbUrl( LocalFile $file, $transformParams ) {
89 $config = MediaWikiServices::getInstance()->getMainConfig();
90 $uploadThumbnailRenderHttpCustomHost =
92 $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']++;
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
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
189class_alias( ThumbnailRenderJob::class, 'ThumbnailRenderJob' );
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfGetUrlUtils()
Basic media transform error class.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
thumbName( $params, $flags=0)
Return the file name of a thumbnail with the specified parameters.
Definition File.php:1112
getHandler()
Get a MediaHandler instance for this file.
Definition File.php:1636
getThumbUrl( $suffix=false)
Get the URL of the thumbnail directory, or a particular file if $suffix is specified.
Definition File.php:1980
Local file in the wiki's own database.
Definition LocalFile.php:93
Describe and execute a background job.
Definition Job.php:41
array $params
Array of job parameters.
Definition Job.php:46
setLastError( $error)
Definition Job.php:435
Job for asynchronous rendering of thumbnails, e.g.
hitThumbUrl(LocalFile $file, $transformParams)
A class containing constants representing the names of configuration variables.
const UploadThumbnailRenderMethod
Name constant for the UploadThumbnailRenderMethod setting, for use with Config::get()
const UploadThumbnailRenderHttpCustomDomain
Name constant for the UploadThumbnailRenderHttpCustomDomain setting, for use with Config::get()
const UploadThumbnailRenderHttpCustomHost
Name constant for the UploadThumbnailRenderHttpCustomHost setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Represents a title within MediaWiki.
Definition Title.php:78
Interface for database access objects.
if(count( $args)< 1) $job