MediaWiki master
ThumbnailRenderJob.php
Go to the documentation of this file.
1<?php
26
32class 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
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']++;
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
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}
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfGetUrlUtils()
array $params
The job parameters.
setLastError( $error)
This is actually implemented in the Job class.
getThumbUrl( $suffix=false)
Get the URL of the thumbnail directory, or a particular file if $suffix is specified.
Definition File.php:1912
thumbName( $params, $flags=0)
Return the file name of a thumbnail with the specified parameters.
Definition File.php:1098
getHandler()
Get a MediaHandler instance for this file.
Definition File.php:1568
Describe and execute a background job.
Definition Job.php:38
Local file in the wiki's own database.
Definition LocalFile.php:75
Basic media transform error class.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
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
Job for asynchronous rendering of thumbnails, e.g.
__construct(Title $title, array $params)
hitThumbUrl(LocalFile $file, $transformParams)
allowRetries()
Whether to retry the job.
Interface for database access objects.
if(count( $args)< 1) $job