MediaWiki 1.40.4
ThumbnailRenderJob.php
Go to the documentation of this file.
1<?php
24
30class ThumbnailRenderJob extends Job {
31 public function __construct( Title $title, array $params ) {
32 parent::__construct( 'ThumbnailRender', $title, $params );
33 }
34
35 public function run() {
36 $uploadThumbnailRenderMethod = MediaWikiServices::getInstance()
37 ->getMainConfig()->get( MainConfigNames::UploadThumbnailRenderMethod );
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 ( $uploadThumbnailRenderMethod === '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 ( $uploadThumbnailRenderMethod === 'http' ) {
60 return $this->hitThumbUrl( $file, $transformParams );
61 } else {
62 $this->setLastError( __METHOD__ . ': unknown thumbnail render method ' .
63 $uploadThumbnailRenderMethod );
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 ) {
78 $config = MediaWikiServices::getInstance()->getMainConfig();
79 $uploadThumbnailRenderHttpCustomHost =
80 $config->get( MainConfigNames::UploadThumbnailRenderHttpCustomHost );
81 $uploadThumbnailRenderHttpCustomDomain =
82 $config->get( MainConfigNames::UploadThumbnailRenderHttpCustomDomain );
83 $handler = $file->getHandler();
84 if ( !$handler ) {
85 $this->setLastError( __METHOD__ . ': could not get handler' );
86 return false;
87 } elseif ( !$handler->normaliseParams( $file, $transformParams ) ) {
88 $this->setLastError( __METHOD__ . ': failed to normalize' );
89 return false;
90 }
91 $thumbName = $file->thumbName( $transformParams );
92 $thumbUrl = $file->getThumbUrl( $thumbName );
93
94 if ( $thumbUrl === null ) {
95 $this->setLastError( __METHOD__ . ': could not get thumb URL' );
96 return false;
97 }
98
99 if ( $uploadThumbnailRenderHttpCustomDomain ) {
100 $parsedUrl = wfParseUrl( $thumbUrl );
101
102 if ( !isset( $parsedUrl['path'] ) || $parsedUrl['path'] === '' ) {
103 $this->setLastError( __METHOD__ . ": invalid thumb URL: $thumbUrl" );
104 return false;
105 }
106
107 $thumbUrl = '//' . $uploadThumbnailRenderHttpCustomDomain . $parsedUrl['path'];
108 }
109
110 wfDebug( __METHOD__ . ": hitting url {$thumbUrl}" );
111
112 // T203135 We don't wait for the request to complete, as this is mostly fire & forget.
113 // Looking at the HTTP status of requests that take less than 1s is a double check.
114 $request = MediaWikiServices::getInstance()->getHttpRequestFactory()->create(
115 $thumbUrl,
116 [ 'method' => 'HEAD', 'followRedirects' => true, 'timeout' => 1 ],
117 __METHOD__
118 );
119
120 if ( $uploadThumbnailRenderHttpCustomHost ) {
121 $request->setHeader( 'Host', $uploadThumbnailRenderHttpCustomHost );
122 }
123
124 $status = $request->execute();
125 $statusCode = $request->getStatus();
126 wfDebug( __METHOD__ . ": received status {$statusCode}" );
127
128 // 400 happens when requesting a size greater or equal than the original
129 // TODO use proper error signaling. 400 could mean a number of other things.
130 if ( $statusCode === 200 || $statusCode === 301 || $statusCode === 302 || $statusCode === 400 ) {
131 return true;
132 } elseif ( $statusCode ) {
133 $this->setLastError( __METHOD__ . ": incorrect HTTP status $statusCode when hitting $thumbUrl" );
134 } elseif ( $status->hasMessage( 'http-timed-out' ) ) {
135 // T203135 we ignore timeouts, as it would be inefficient for this job to wait for
136 // minutes for the slower thumbnails to complete.
137 return true;
138 } else {
139 $this->setLastError( __METHOD__ . ': HTTP request failure: '
140 . Status::wrap( $status )->getWikiText( false, false, 'en' ) );
141 }
142 return false;
143 }
144
149 public function allowRetries() {
150 // ThumbnailRenderJob is a warmup for the thumbnails cache,
151 // so loosing it is not a problem. Most times the job fails
152 // for non-renderable or missing images which will not be fixed
153 // by a retry, but will create additional load on the renderer.
154 return false;
155 }
156}
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.
Class to both describe a background job and handle jobs.
Definition Job.php:39
setLastError( $error)
Definition Job.php:429
Local file in the wiki's own database.
Definition LocalFile.php:61
Basic media transform error class.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:82
Job for asynchronous rendering of thumbnails, e.g.
__construct(Title $title, array $params)
hitThumbUrl(LocalFile $file, $transformParams)
allowRetries()
Whether to retry the job.
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42