MediaWiki  1.33.0
CreatePdfThumbnailsJob.php
Go to the documentation of this file.
1 <?php
2 
3 class CreatePdfThumbnailsJob extends Job {
7  const BIG_THUMB = 1;
8  const SMALL_THUMB = 2;
9 
20  public function __construct( $title, $params ) {
21  parent::__construct( 'createPdfThumbnailsJob', $title, $params );
22  }
23 
28  public function run() {
29  if ( !isset( $this->params['page'] ) ) {
30  wfDebugLog( 'thumbnails', 'A page for thumbnails job of ' . $this->title->getText() .
31  ' was not specified! That should never happen!' );
32  return true; // no page set? that should never happen
33  }
34 
35  $file = wfLocalFile( $this->title ); // we just want a local file
36  if ( !$file ) {
37  return true; // Just silently fail, perhaps the file was already deleted, don't bother
38  }
39 
40  switch ( $this->params['jobtype'] ) {
41  case self::BIG_THUMB:
42  global $wgImageLimits;
43  // Ignore user preferences, do default thumbnails
44  // everything here shamelessy copied and reused from includes/ImagePage.php
45  $sizeSel = User::getDefaultOption( 'imagesize' );
46 
47  // The user offset might still be incorrect, specially if
48  // $wgImageLimits got changed (see bug #8858).
49  if ( !isset( $wgImageLimits[$sizeSel] ) ) {
50  // Default to the first offset in $wgImageLimits
51  $sizeSel = 0;
52  }
53  $max = $wgImageLimits[$sizeSel];
54  $maxWidth = $max[0];
55  $maxHeight = $max[1];
56 
57  $width_orig = $file->getWidth( $this->params['page'] );
58  $width = $width_orig;
59  $height_orig = $file->getHeight( $this->params['page'] );
60  $height = $height_orig;
61  if ( $width > $maxWidth || $height > $maxHeight ) {
62  // Calculate the thumbnail size.
63  // First case, the limiting factor is the width, not the height.
64  if ( $width / $height >= $maxWidth / $maxHeight ) {
65  // $height = round( $height * $maxWidth / $width );
66  $width = $maxWidth;
67  // Note that $height <= $maxHeight now.
68  } else {
69  $newwidth = floor( $width * $maxHeight / $height );
70  // $height = round( $height * $newwidth / $width );
71  $width = $newwidth;
72  // Note that $height <= $maxHeight now, but might not be identical
73  // because of rounding.
74  }
75  $transformParams = [ 'page' => $this->params['page'], 'width' => $width ];
76  $file->transform( $transformParams );
77  }
78  break;
79 
80  case self::SMALL_THUMB:
81  Linker::makeThumbLinkObj( $this->title, $file, '', '', 'none',
82  [ 'page' => $this->params['page'] ] );
83  break;
84  }
85 
86  return true;
87  }
88 
95  public static function insertJobs( $upload, $mime, &$error ) {
96  global $wgPdfCreateThumbnailsInJobQueue;
97  if ( !$wgPdfCreateThumbnailsInJobQueue ) {
98  return true;
99  }
100  $magic = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
101  if ( !$magic->isMatchingExtension( 'pdf', $mime ) ) {
102  return true; // not a PDF, abort
103  }
104 
105  $title = $upload->getTitle();
106  $uploadFile = $upload->getLocalFile();
107  if ( is_null( $uploadFile ) ) {
108  wfDebugLog( 'thumbnails', '$uploadFile seems to be null, should never happen...' );
109  return true; // should never happen, but it's better to be secure
110  }
111 
112  $metadata = $uploadFile->getMetadata();
113  $unserialized = unserialize( $metadata );
114  $pages = intval( $unserialized['Pages'] );
115 
116  $jobs = [];
117  for ( $i = 1; $i <= $pages; $i++ ) {
118  $jobs[] = new CreatePdfThumbnailsJob(
119  $title,
120  [ 'page' => $i, 'jobtype' => self::BIG_THUMB ]
121  );
122  $jobs[] = new CreatePdfThumbnailsJob(
123  $title,
124  [ 'page' => $i, 'jobtype' => self::SMALL_THUMB ]
125  );
126  }
127  JobQueueGroup::singleton()->push( $jobs );
128  return true;
129  }
130 }
User\getDefaultOption
static getDefaultOption( $opt)
Get a given default option value.
Definition: User.php:1818
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
Job\$title
Title $title
Definition: Job.php:41
CreatePdfThumbnailsJob\SMALL_THUMB
const SMALL_THUMB
Definition: CreatePdfThumbnailsJob.php:8
CreatePdfThumbnailsJob\run
run()
Run a thumbnail job on a given PDF file.
Definition: CreatePdfThumbnailsJob.php:28
CreatePdfThumbnailsJob\insertJobs
static insertJobs( $upload, $mime, &$error)
Definition: CreatePdfThumbnailsJob.php:95
Job\$params
array $params
Array of job parameters.
Definition: Job.php:35
CreatePdfThumbnailsJob\BIG_THUMB
const BIG_THUMB
Flags for thumbnail jobs.
Definition: CreatePdfThumbnailsJob.php:7
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:1043
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
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:30
Job\$error
string $error
Text for error that occurred last.
Definition: Job.php:47
Linker\makeThumbLinkObj
static makeThumbLinkObj(Title $title, $file, $label='', $alt='', $align='right', $params=[], $framed=false, $manualthumb="")
Make HTML for a thumbnail including image, border and caption.
Definition: Linker.php:496
Job\$metadata
array $metadata
Additional queue metadata.
Definition: Job.php:38
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:124
title
title
Definition: parserTests.txt:245
CreatePdfThumbnailsJob
Definition: CreatePdfThumbnailsJob.php:3
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:142
$wgImageLimits
$wgImageLimits
Limit images on image description pages to a user-selectable limit.
Definition: DefaultSettings.php:1446
CreatePdfThumbnailsJob\__construct
__construct( $title, $params)
Construct a thumbnail job.
Definition: CreatePdfThumbnailsJob.php:20
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:70
wfLocalFile
wfLocalFile( $title)
Get an object referring to a locally registered file.
Definition: GlobalFunctions.php:2688