MediaWiki REL1_34
ThumbnailRenderJob.php
Go to the documentation of this file.
1<?php
25
31class ThumbnailRenderJob extends Job {
32 public function __construct( Title $title, array $params ) {
33 parent::__construct( 'ThumbnailRender', $title, $params );
34 }
35
36 public function run() {
38
39 $transformParams = $this->params['transformParams'];
40
41 $file = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
42 ->newFile( $this->title );
43 $file->load( File::READ_LATEST );
44
45 if ( $file && $file->exists() ) {
46 if ( $wgUploadThumbnailRenderMethod === 'jobqueue' ) {
47 $thumb = $file->transform( $transformParams, File::RENDER_NOW );
48
49 if ( !$thumb || $thumb->isError() ) {
50 if ( $thumb instanceof MediaTransformError ) {
51 $this->setLastError( __METHOD__ . ': thumbnail couln\'t be generated:' .
52 $thumb->toText() );
53 } else {
54 $this->setLastError( __METHOD__ . ': thumbnail couln\'t be generated' );
55 }
56 return false;
57 }
58 return true;
59 } elseif ( $wgUploadThumbnailRenderMethod === 'http' ) {
60 return $this->hitThumbUrl( $file, $transformParams );
61 } else {
62 $this->setLastError( __METHOD__ . ': unknown thumbnail render method ' .
64 return false;
65 }
66 } else {
67 $this->setLastError( __METHOD__ . ': file doesn\'t exist' );
68 return false;
69 }
70 }
71
77 protected function hitThumbUrl( LocalFile $file, $transformParams ) {
79
80 $handler = $file->getHandler();
81 if ( !$handler ) {
82 $this->setLastError( __METHOD__ . ': could not get handler' );
83 return false;
84 } elseif ( !$handler->normaliseParams( $file, $transformParams ) ) {
85 $this->setLastError( __METHOD__ . ': failed to normalize' );
86 return false;
87 }
88 $thumbName = $file->thumbName( $transformParams );
89 $thumbUrl = $file->getThumbUrl( $thumbName );
90
91 if ( $thumbUrl === null ) {
92 $this->setLastError( __METHOD__ . ': could not get thumb URL' );
93 return false;
94 }
95
97 $parsedUrl = wfParseUrl( $thumbUrl );
98
99 if ( !isset( $parsedUrl['path'] ) || $parsedUrl['path'] === '' ) {
100 $this->setLastError( __METHOD__ . ": invalid thumb URL: $thumbUrl" );
101 return false;
102 }
103
104 $thumbUrl = '//' . $wgUploadThumbnailRenderHttpCustomDomain . $parsedUrl['path'];
105 }
106
107 wfDebug( __METHOD__ . ": hitting url {$thumbUrl}\n" );
108
109 // T203135 We don't wait for the request to complete, as this is mostly fire & forget.
110 // Looking at the HTTP status of requests that take less than 1s is a sanity check.
111 $request = MediaWikiServices::getInstance()->getHttpRequestFactory()->create(
112 $thumbUrl,
113 [ 'method' => 'HEAD', 'followRedirects' => true, 'timeout' => 1 ],
114 __METHOD__
115 );
116
118 $request->setHeader( 'Host', $wgUploadThumbnailRenderHttpCustomHost );
119 }
120
121 $status = $request->execute();
122 $statusCode = $request->getStatus();
123 wfDebug( __METHOD__ . ": received status {$statusCode}\n" );
124
125 // 400 happens when requesting a size greater or equal than the original
126 // TODO use proper error signaling. 400 could mean a number of other things.
127 if ( $statusCode === 200 || $statusCode === 301 || $statusCode === 302 || $statusCode === 400 ) {
128 return true;
129 } elseif ( $statusCode ) {
130 $this->setLastError( __METHOD__ . ": incorrect HTTP status $statusCode when hitting $thumbUrl" );
131 } elseif ( $status->hasMessage( 'http-timed-out' ) ) {
132 // T203135 we ignore timeouts, as it would be inefficient for this job to wait for
133 // minutes for the slower thumbnails to complete.
134 return true;
135 } else {
136 $this->setLastError( __METHOD__ . ': HTTP request failure: '
137 . Status::wrap( $status )->getWikiText( null, null, 'en' ) );
138 }
139 return false;
140 }
141
146 public function allowRetries() {
147 // ThumbnailRenderJob is a warmup for the thumbnails cache,
148 // so loosing it is not a problem. Most times the job fails
149 // for non-renderable or missing images which will not be fixed
150 // by a retry, but will create additional load on the renderer.
151 return false;
152 }
153}
$wgUploadThumbnailRenderHttpCustomHost
When using the "http" wgUploadThumbnailRenderMethod, lets one specify a custom Host HTTP header.
$wgUploadThumbnailRenderMethod
The method through which the thumbnails will be prerendered for the entries in $wgUploadThumbnailRend...
$wgUploadThumbnailRenderHttpCustomDomain
When using the "http" wgUploadThumbnailRenderMethod, lets one specify a custom domain to send the HTT...
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
const RENDER_NOW
Force rendering in the current process.
Definition File.php:69
Class to both describe a background job and handle jobs.
Definition Job.php:30
Title $title
Definition Job.php:41
setLastError( $error)
Definition Job.php:418
array $params
Array of job parameters.
Definition Job.php:35
Class to represent a local file in the wiki's own database.
Definition LocalFile.php:56
Basic media transform error class.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Job for asynchronous rendering of thumbnails.
__construct(Title $title, array $params)
hitThumbUrl(LocalFile $file, $transformParams)
allowRetries()
Whether to retry the job.
Represents a title within MediaWiki.
Definition Title.php:42
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42