MediaWiki  1.34.4
ThumbnailRenderJob.php
Go to the documentation of this file.
1 <?php
25 
31 class 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 }
MediaTransformError
Basic media transform error class.
Definition: MediaTransformError.php:29
ThumbnailRenderJob\hitThumbUrl
hitThumbUrl(LocalFile $file, $transformParams)
Definition: ThumbnailRenderJob.php:77
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
Job\$title
Title $title
Definition: Job.php:41
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
Job\$params
array $params
Array of job parameters.
Definition: Job.php:35
Job\setLastError
setLastError( $error)
Definition: Job.php:418
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:30
wfParseUrl
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Definition: GlobalFunctions.php:793
ThumbnailRenderJob\run
run()
Run the job.
Definition: ThumbnailRenderJob.php:36
Status\wrap
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:55
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:913
LocalFile
Class to represent a local file in the wiki's own database.
Definition: LocalFile.php:56
ThumbnailRenderJob
Job for asynchronous rendering of thumbnails.
Definition: ThumbnailRenderJob.php:31
ThumbnailRenderJob\__construct
__construct(Title $title, array $params)
Definition: ThumbnailRenderJob.php:32
$wgUploadThumbnailRenderHttpCustomHost
$wgUploadThumbnailRenderHttpCustomHost
When using the "http" wgUploadThumbnailRenderMethod, lets one specify a custom Host HTTP header.
Definition: DefaultSettings.php:1562
File\RENDER_NOW
const RENDER_NOW
Force rendering in the current process.
Definition: File.php:69
Title
Represents a title within MediaWiki.
Definition: Title.php:42
ThumbnailRenderJob\allowRetries
allowRetries()
Whether to retry the job.
Definition: ThumbnailRenderJob.php:146
$wgUploadThumbnailRenderMethod
$wgUploadThumbnailRenderMethod
The method through which the thumbnails will be prerendered for the entries in $wgUploadThumbnailRend...
Definition: DefaultSettings.php:1555
$wgUploadThumbnailRenderHttpCustomDomain
$wgUploadThumbnailRenderHttpCustomDomain
When using the "http" wgUploadThumbnailRenderMethod, lets one specify a custom domain to send the HTT...
Definition: DefaultSettings.php:1570