MediaWiki master
ThumbnailRenderJob.php
Go to the documentation of this file.
1<?php
8
18
24class ThumbnailRenderJob extends Job {
25 public function __construct( Title $title, array $params ) {
26 parent::__construct( 'ThumbnailRender', $title, $params );
27 }
28
30 public function run() {
31 $uploadThumbnailRenderMethod = MediaWikiServices::getInstance()
32 ->getMainConfig()->get( MainConfigNames::UploadThumbnailRenderMethod );
33
34 $transformParams = $this->params['transformParams'];
35
36 $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
37 ->newFile( $this->title );
38 $file->load( IDBAccessObject::READ_LATEST );
39
40 if ( $file && $file->exists() ) {
41 if ( $uploadThumbnailRenderMethod === 'jobqueue' ) {
42 $thumb = $file->transform( $transformParams, File::RENDER_NOW );
43
44 if ( !$thumb || $thumb->isError() ) {
45 if ( $thumb instanceof MediaTransformError ) {
46 $this->setLastError( __METHOD__ . ': thumbnail couldn\'t be generated:' .
47 $thumb->toText() );
48 } else {
49 $this->setLastError( __METHOD__ . ': thumbnail couldn\'t be generated' );
50 }
51 return false;
52 }
53 $this->maybeEnqueueNextPage( $transformParams );
54 return true;
55 } elseif ( $uploadThumbnailRenderMethod === 'http' ) {
56 $res = $this->hitThumbUrl( $file, $transformParams );
57 $this->maybeEnqueueNextPage( $transformParams );
58 return $res;
59 } else {
60 $this->setLastError( __METHOD__ . ': unknown thumbnail render method ' .
61 $uploadThumbnailRenderMethod );
62 return false;
63 }
64 } else {
65 $this->setLastError( __METHOD__ . ': file doesn\'t exist' );
66 return false;
67 }
68 }
69
75 protected function hitThumbUrl( LocalFile $file, $transformParams ) {
76 $config = MediaWikiServices::getInstance()->getMainConfig();
77 $urlUtils = MediaWikiServices::getInstance()->getUrlUtils();
78 $uploadThumbnailRenderHttpCustomHost =
80 $uploadThumbnailRenderHttpCustomDomain =
82 $handler = $file->getHandler();
83 if ( !$handler ) {
84 $this->setLastError( __METHOD__ . ': could not get handler' );
85 return false;
86 } elseif ( !$handler->normaliseParams( $file, $transformParams ) ) {
87 $this->setLastError( __METHOD__ . ': failed to normalize' );
88 return false;
89 }
90 $thumbName = $file->thumbName( $transformParams );
91 $thumbUrl = $file->getThumbUrl( $thumbName );
92
93 if ( $thumbUrl === null ) {
94 $this->setLastError( __METHOD__ . ': could not get thumb URL' );
95 return false;
96 }
97
98 if ( $uploadThumbnailRenderHttpCustomDomain ) {
99 $parsedUrl = $urlUtils->parse( $thumbUrl );
100
101 if ( !isset( $parsedUrl['path'] ) || $parsedUrl['path'] === '' ) {
102 $this->setLastError( __METHOD__ . ": invalid thumb URL: $thumbUrl" );
103 return false;
104 }
105
106 $thumbUrl = '//' . $uploadThumbnailRenderHttpCustomDomain . $parsedUrl['path'];
107 }
108
109 wfDebug( __METHOD__ . ": hitting url {$thumbUrl}" );
110
111 // T203135 We don't wait for the request to complete, as this is mostly fire & forget.
112 // Looking at the HTTP status of requests that take less than 1s is a double check.
113 $request = MediaWikiServices::getInstance()->getHttpRequestFactory()->create(
114 $thumbUrl,
115 [ 'method' => 'HEAD', 'followRedirects' => true, 'timeout' => 1 ],
116 __METHOD__
117 );
118
119 if ( $uploadThumbnailRenderHttpCustomHost ) {
120 $request->setHeader( 'Host', $uploadThumbnailRenderHttpCustomHost );
121 }
122
123 $status = $request->execute();
124 $statusCode = $request->getStatus();
125 wfDebug( __METHOD__ . ": received status {$statusCode}" );
126
127 // 400 happens when requesting a size greater or equal than the original
128 // TODO use proper error signaling. 400 could mean a number of other things.
129 if ( $statusCode === 200 || $statusCode === 301 || $statusCode === 302 || $statusCode === 400 ) {
130 return true;
131 } elseif ( $statusCode ) {
132 $this->setLastError( __METHOD__ . ": incorrect HTTP status $statusCode when hitting $thumbUrl" );
133 } elseif ( $status->hasMessage( 'http-timed-out' ) ) {
134 // T203135 we ignore timeouts, as it would be inefficient for this job to wait for
135 // minutes for the slower thumbnails to complete.
136 return true;
137 } else {
138 $this->setLastError( __METHOD__ . ': HTTP request failure: '
139 . Status::wrap( $status )->getWikiText( false, false, 'en' ) );
140 }
141 return false;
142 }
143
144 private function maybeEnqueueNextPage( array $transformParams ) {
145 if (
146 ( $this->params['enqueueNextPage'] ?? false ) &&
147 ( $transformParams['page'] ?? 0 ) < ( $this->params['pageLimit'] ?? 0 )
148 ) {
149 $transformParams['page']++;
151 $this->getTitle(),
152 [
153 'transformParams' => $transformParams,
154 'enqueueNextPage' => true,
155 'pageLimit' => $this->params['pageLimit']
156 ]
157 );
158
159 MediaWikiServices::getInstance()->getJobQueueGroup()->lazyPush( [ $job ] );
160 }
161 }
162
167 public function allowRetries() {
168 // ThumbnailRenderJob is a warmup for the thumbnails cache,
169 // so loosing it is not a problem. Most times the job fails
170 // for non-renderable or missing images which will not be fixed
171 // by a retry, but will create additional load on the renderer.
172 return false;
173 }
174}
175
177class_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.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:80
getHandler(?Language $lang=null)
Get a MediaHandler instance for this file.
Definition File.php:1613
thumbName( $params, $flags=0)
Return the file name of a thumbnail with the specified parameters.
Definition File.php:1155
getThumbUrl( $suffix=false)
Get the URL of the thumbnail directory, or a particular file if $suffix is specified.
Definition File.php:1960
Local file in the wiki's own database.
Definition LocalFile.php:81
Describe and execute a background job.
Definition Job.php:28
array $params
Array of job parameters.
Definition Job.php:33
setLastError( $error)
Definition Job.php:424
Job for asynchronous rendering of thumbnails, e.g.
hitThumbUrl(LocalFile $file, $transformParams)
run()
Run the job.If this method returns false or completes exceptionally, the job runner will retry execut...
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.
Basic media transform error class.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Represents a title within MediaWiki.
Definition Title.php:69
Interface for database access objects.
if(count( $args)< 1) $job