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 $uploadThumbnailRenderHttpCustomHost =
79 $uploadThumbnailRenderHttpCustomDomain =
81 $handler = $file->getHandler();
82 if ( !$handler ) {
83 $this->setLastError( __METHOD__ . ': could not get handler' );
84 return false;
85 } elseif ( !$handler->normaliseParams( $file, $transformParams ) ) {
86 $this->setLastError( __METHOD__ . ': failed to normalize' );
87 return false;
88 }
89 $thumbName = $file->thumbName( $transformParams );
90 $thumbUrl = $file->getThumbUrl( $thumbName );
91
92 if ( $thumbUrl === null ) {
93 $this->setLastError( __METHOD__ . ': could not get thumb URL' );
94 return false;
95 }
96
97 if ( $uploadThumbnailRenderHttpCustomDomain ) {
98 $parsedUrl = wfGetUrlUtils()->parse( $thumbUrl );
99
100 if ( !isset( $parsedUrl['path'] ) || $parsedUrl['path'] === '' ) {
101 $this->setLastError( __METHOD__ . ": invalid thumb URL: $thumbUrl" );
102 return false;
103 }
104
105 $thumbUrl = '//' . $uploadThumbnailRenderHttpCustomDomain . $parsedUrl['path'];
106 }
107
108 wfDebug( __METHOD__ . ": hitting url {$thumbUrl}" );
109
110 // T203135 We don't wait for the request to complete, as this is mostly fire & forget.
111 // Looking at the HTTP status of requests that take less than 1s is a double check.
112 $request = MediaWikiServices::getInstance()->getHttpRequestFactory()->create(
113 $thumbUrl,
114 [ 'method' => 'HEAD', 'followRedirects' => true, 'timeout' => 1 ],
115 __METHOD__
116 );
117
118 if ( $uploadThumbnailRenderHttpCustomHost ) {
119 $request->setHeader( 'Host', $uploadThumbnailRenderHttpCustomHost );
120 }
121
122 $status = $request->execute();
123 $statusCode = $request->getStatus();
124 wfDebug( __METHOD__ . ": received status {$statusCode}" );
125
126 // 400 happens when requesting a size greater or equal than the original
127 // TODO use proper error signaling. 400 could mean a number of other things.
128 if ( $statusCode === 200 || $statusCode === 301 || $statusCode === 302 || $statusCode === 400 ) {
129 return true;
130 } elseif ( $statusCode ) {
131 $this->setLastError( __METHOD__ . ": incorrect HTTP status $statusCode when hitting $thumbUrl" );
132 } elseif ( $status->hasMessage( 'http-timed-out' ) ) {
133 // T203135 we ignore timeouts, as it would be inefficient for this job to wait for
134 // minutes for the slower thumbnails to complete.
135 return true;
136 } else {
137 $this->setLastError( __METHOD__ . ': HTTP request failure: '
138 . Status::wrap( $status )->getWikiText( false, false, 'en' ) );
139 }
140 return false;
141 }
142
143 private function maybeEnqueueNextPage( array $transformParams ) {
144 if (
145 ( $this->params['enqueueNextPage'] ?? false ) &&
146 ( $transformParams['page'] ?? 0 ) < ( $this->params['pageLimit'] ?? 0 )
147 ) {
148 $transformParams['page']++;
150 $this->getTitle(),
151 [
152 'transformParams' => $transformParams,
153 'enqueueNextPage' => true,
154 'pageLimit' => $this->params['pageLimit']
155 ]
156 );
157
158 MediaWikiServices::getInstance()->getJobQueueGroup()->lazyPush( [ $job ] );
159 }
160 }
161
166 public function allowRetries() {
167 // ThumbnailRenderJob is a warmup for the thumbnails cache,
168 // so loosing it is not a problem. Most times the job fails
169 // for non-renderable or missing images which will not be fixed
170 // by a retry, but will create additional load on the renderer.
171 return false;
172 }
173}
174
176class_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:79
thumbName( $params, $flags=0)
Return the file name of a thumbnail with the specified parameters.
Definition File.php:1100
getHandler()
Get a MediaHandler instance for this file.
Definition File.php:1613
getThumbUrl( $suffix=false)
Get the URL of the thumbnail directory, or a particular file if $suffix is specified.
Definition File.php:1957
Local file in the wiki's own database.
Definition LocalFile.php:79
Describe and execute a background job.
Definition Job.php:27
array $params
Array of job parameters.
Definition Job.php:32
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.
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