MediaWiki  1.29.2
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 ' .
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 
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 
104  $request->setHeader( 'Host', $wgUploadThumbnailRenderHttpCustomHost );
105  }
106 
107  $status = $request->execute();
108 
109  return $request->getStatus();
110  }
111 }
MWHttpRequest\factory
static factory( $url, $options=null, $caller=__METHOD__)
Generate a new request object.
Definition: MWHttpRequest.php:180
$request
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2612
Job\$title
Title $title
Definition: Job.php:42
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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:1049
Job\$params
array $params
Array of job parameters.
Definition: Job.php:36
Job\setLastError
setLastError( $error)
Definition: Job.php:393
php
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
ThumbnailRenderJob\hitThumbUrl
hitThumbUrl( $file, $transformParams, &$thumbUrl)
Definition: ThumbnailRenderJob.php:80
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:31
wfParseUrl
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Definition: GlobalFunctions.php:818
ThumbnailRenderJob\run
run()
Run the job.
Definition: ThumbnailRenderJob.php:34
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
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:999
ThumbnailRenderJob
Job for asynchronous rendering of thumbnails.
Definition: ThumbnailRenderJob.php:29
title
title
Definition: parserTests.txt:211
ThumbnailRenderJob\__construct
__construct(Title $title, array $params)
Definition: ThumbnailRenderJob.php:30
$wgUploadThumbnailRenderHttpCustomHost
$wgUploadThumbnailRenderHttpCustomHost
When using the "http" wgUploadThumbnailRenderMethod, lets one specify a custom Host HTTP header.
Definition: DefaultSettings.php:1423
File\RENDER_NOW
const RENDER_NOW
Force rendering in the current process.
Definition: File.php:59
Title
Represents a title within MediaWiki.
Definition: Title.php:39
wfLocalFile
wfLocalFile( $title)
Get an object referring to a locally registered file.
Definition: GlobalFunctions.php:3112
$wgUploadThumbnailRenderMethod
$wgUploadThumbnailRenderMethod
The method through which the thumbnails will be prerendered for the entries in $wgUploadThumbnailRend...
Definition: DefaultSettings.php:1416
$wgUploadThumbnailRenderHttpCustomDomain
$wgUploadThumbnailRenderHttpCustomDomain
When using the "http" wgUploadThumbnailRenderMethod, lets one specify a custom domain to send the HTT...
Definition: DefaultSettings.php:1431
array
the array() calling protocol came about after MediaWiki 1.4rc1.