MediaWiki  1.29.1
CreatePdfThumbnailsJob.class.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 
21  public function __construct( $title, $params ) {
22  parent::__construct( 'createPdfThumbnailsJob', $title, $params );
23  }
24 
29  public function run() {
30  if ( !isset( $this->params['page'] ) ) {
31  wfDebugLog('thumbnails', 'A page for thumbnails job of ' . $this->title->getText() . ' 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:
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 = array( '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', array( 'page' => $this->params['page'] ) );
82  break;
83  }
84 
85  return true;
86  }
87 
94  public static function insertJobs( $upload, $mime, &$error ) {
95  global $wgPdfCreateThumbnailsInJobQueue;
96  if ( !$wgPdfCreateThumbnailsInJobQueue ) {
97  return true;
98  }
99  if (!MimeMagic::singleton()->isMatchingExtension('pdf', $mime)) {
100  return true; // not a PDF, abort
101  }
102 
103  $title = $upload->getTitle();
104  $uploadFile = $upload->getLocalFile();
105  if ( is_null( $uploadFile ) ) {
106  wfDebugLog('thumbnails', '$uploadFile seems to be null, should never happen...');
107  return true; // should never happen, but it's better to be secure
108  }
109 
110  $metadata = $uploadFile->getMetadata();
111  $unserialized = unserialize( $metadata );
112  $pages = intval( $unserialized['Pages'] );
113 
114  $jobs = array();
115  for ( $i = 1; $i <= $pages; $i++ ) {
116  $jobs[] = new CreatePdfThumbnailsJob(
117  $title,
118  array( 'page' => $i, 'jobtype' => self::BIG_THUMB )
119  );
120  $jobs[] = new CreatePdfThumbnailsJob(
121  $title,
122  array( 'page' => $i, 'jobtype' => self::SMALL_THUMB )
123  );
124  }
125  JobQueueGroup::singleton()->push( $jobs );
126  return true;
127  }
128 }
User\getDefaultOption
static getDefaultOption( $opt)
Get a given default option value.
Definition: User.php:1603
Job\$title
Title $title
Definition: Job.php:42
CreatePdfThumbnailsJob::SMALL_THUMB
const SMALL_THUMB
Definition: CreatePdfThumbnailsJob.class.php:8
unserialize
unserialize( $serialized)
Definition: ApiMessage.php:185
CreatePdfThumbnailsJob::run
run()
Run a thumbnail job on a given PDF file.
Definition: CreatePdfThumbnailsJob.class.php:29
CreatePdfThumbnailsJob::insertJobs
static insertJobs( $upload, $mime, &$error)
Definition: CreatePdfThumbnailsJob.class.php:94
Job\$params
array $params
Array of job parameters.
Definition: Job.php:36
CreatePdfThumbnailsJob::BIG_THUMB
const BIG_THUMB
Flags for thumbnail jobs.
Definition: CreatePdfThumbnailsJob.class.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:1092
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:31
Job\$error
string $error
Text for error that occurred last.
Definition: Job.php:48
Job\$metadata
array $metadata
Additional queue metadata.
Definition: Job.php:39
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
MimeMagic\singleton
static singleton()
Get an instance of this class.
Definition: MimeMagic.php:33
$mime
if( $ext=='php'|| $ext=='php5') $mime
Definition: router.php:65
title
title
Definition: parserTests.txt:211
CreatePdfThumbnailsJob
Definition: CreatePdfThumbnailsJob.class.php:3
$wgImageLimits
$wgImageLimits
Limit images on image description pages to a user-selectable limit.
Definition: DefaultSettings.php:1343
CreatePdfThumbnailsJob::__construct
__construct( $title, $params)
Construct a thumbnail job.
Definition: CreatePdfThumbnailsJob.class.php:21
JobQueueGroup\singleton
static singleton( $wiki=false)
Definition: JobQueueGroup.php:71
wfLocalFile
wfLocalFile( $title)
Get an object referring to a locally registered file.
Definition: GlobalFunctions.php:3112
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:502
array
the array() calling protocol came about after MediaWiki 1.4rc1.