MediaWiki  1.28.0
ThumbnailRenderJob.php
Go to the documentation of this file.
1 <?php
29 class ThumbnailRenderJob extends Job {
30  public function __construct( Title $title, array $params ) {
31  parent::__construct( 'ThumbnailRender', $title, $params );
32  }
33 
34  public function run() {
36 
37  $transformParams = $this->params['transformParams'];
38 
39  $file = wfLocalFile( $this->title );
40  $file->load( File::READ_LATEST );
41 
42  if ( $file && $file->exists() ) {
43  if ( $wgUploadThumbnailRenderMethod === 'jobqueue' ) {
44  $thumb = $file->transform( $transformParams, File::RENDER_NOW );
45 
46  if ( $thumb && !$thumb->isError() ) {
47  return true;
48  } else {
49  $this->setLastError( __METHOD__ . ': thumbnail couln\'t be generated' );
50  return false;
51  }
52  } elseif ( $wgUploadThumbnailRenderMethod === 'http' ) {
53  $thumbUrl = '';
54  $status = $this->hitThumbUrl( $file, $transformParams, $thumbUrl );
55 
56  wfDebug( __METHOD__ . ": received status {$status}\n" );
57 
58  // 400 happens when requesting a size greater or equal than the original
59  if ( $status === 200 || $status === 301 || $status === 302 || $status === 400 ) {
60  return true;
61  } elseif ( $status ) {
62  $this->setLastError( __METHOD__ . ': incorrect HTTP status ' .
63  $status . ' when hitting ' . $thumbUrl );
64  return false;
65  } else {
66  $this->setLastError( __METHOD__ . ': HTTP request failure' );
67  return false;
68  }
69  } else {
70  $this->setLastError( __METHOD__ . ': unknown thumbnail render method ' .
71  $wgUploadThumbnailRenderMethod );
72  return false;
73  }
74  } else {
75  $this->setLastError( __METHOD__ . ': file doesn\'t exist' );
76  return false;
77  }
78  }
79 
80  protected function hitThumbUrl( $file, $transformParams, &$thumbUrl ) {
82 
83  $thumbName = $file->thumbName( $transformParams );
84  $thumbUrl = $file->getThumbUrl( $thumbName );
85 
86  if ( $wgUploadThumbnailRenderHttpCustomDomain ) {
87  $parsedUrl = wfParseUrl( $thumbUrl );
88 
89  if ( !$parsedUrl || !isset( $parsedUrl['path'] ) || !strlen( $parsedUrl['path'] ) ) {
90  return false;
91  }
92 
93  $thumbUrl = '//' . $wgUploadThumbnailRenderHttpCustomDomain . $parsedUrl['path'];
94  }
95 
96  wfDebug( __METHOD__ . ": hitting url {$thumbUrl}\n" );
97 
98  $request = MWHttpRequest::factory( $thumbUrl,
99  [ 'method' => 'HEAD', 'followRedirects' => true ],
100  __METHOD__
101  );
102 
103  if ( $wgUploadThumbnailRenderHttpCustomHost ) {
104  $request->setHeader( 'Host', $wgUploadThumbnailRenderHttpCustomHost );
105  }
106 
107  $status = $request->execute();
108 
109  return $request->getStatus();
110  }
111 }
$wgUploadThumbnailRenderHttpCustomDomain
When using the "http" wgUploadThumbnailRenderMethod, lets one specify a custom domain to send the HTT...
the array() calling protocol came about after MediaWiki 1.4rc1.
Class to both describe a background job and handle jobs.
Definition: Job.php:31
$wgUploadThumbnailRenderMethod
The method through which the thumbnails will be prerendered for the entries in $wgUploadThumbnailRend...
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
title
wfLocalFile($title)
Get an object referring to a locally registered file.
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
const RENDER_NOW
Force rendering in the current process.
Definition: File.php:58
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2573
hitThumbUrl($file, $transformParams, &$thumbUrl)
Job for asynchronous rendering of thumbnails.
__construct(Title $title, array $params)
setLastError($error)
Definition: Job.php:393
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1046
array $params
Array of job parameters.
Definition: Job.php:36
static factory($url, $options=null, $caller=__METHOD__)
Generate a new request object.
$wgUploadThumbnailRenderHttpCustomHost
When using the "http" wgUploadThumbnailRenderMethod, lets one specify a custom Host HTTP header...
wfParseUrl($url)
parse_url() work-alike, but non-broken.
Title $title
Definition: Job.php:42