MediaWiki  1.30.2
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 
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:
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  if ( !MimeMagic::singleton()->isMatchingExtension( 'pdf', $mime ) ) {
101  return true; // not a PDF, abort
102  }
103 
104  $title = $upload->getTitle();
105  $uploadFile = $upload->getLocalFile();
106  if ( is_null( $uploadFile ) ) {
107  wfDebugLog( 'thumbnails', '$uploadFile seems to be null, should never happen...' );
108  return true; // should never happen, but it's better to be secure
109  }
110 
111  $metadata = $uploadFile->getMetadata();
112  $unserialized = unserialize( $metadata );
113  $pages = intval( $unserialized['Pages'] );
114 
115  $jobs = [];
116  for ( $i = 1; $i <= $pages; $i++ ) {
117  $jobs[] = new CreatePdfThumbnailsJob(
118  $title,
119  [ 'page' => $i, 'jobtype' => self::BIG_THUMB ]
120  );
121  $jobs[] = new CreatePdfThumbnailsJob(
122  $title,
123  [ 'page' => $i, 'jobtype' => self::SMALL_THUMB ]
124  );
125  }
126  JobQueueGroup::singleton()->push( $jobs );
127  return true;
128  }
129 }
User\getDefaultOption
static getDefaultOption( $opt)
Get a given default option value.
Definition: User.php:1619
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:28
CreatePdfThumbnailsJob::insertJobs
static insertJobs( $upload, $mime, &$error)
Definition: CreatePdfThumbnailsJob.class.php:95
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:1140
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
title
to move a page</td >< td > &*You are moving the page across *A non empty talk page already exists under the new or *You uncheck the box below In those you will have to move or merge the page manually if desired</td >< td > be sure to &You are responsible for making sure that links continue to point where they are supposed to go Note that the page will &a page at the new title
Definition: All_system_messages.txt:2696
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:59
CreatePdfThumbnailsJob
Definition: CreatePdfThumbnailsJob.class.php:3
$wgImageLimits
$wgImageLimits
Limit images on image description pages to a user-selectable limit.
Definition: DefaultSettings.php:1362
CreatePdfThumbnailsJob::__construct
__construct( $title, $params)
Construct a thumbnail job.
Definition: CreatePdfThumbnailsJob.class.php:20
JobQueueGroup\singleton
static singleton( $wiki=false)
Definition: JobQueueGroup.php:72
wfLocalFile
wfLocalFile( $title)
Get an object referring to a locally registered file.
Definition: GlobalFunctions.php:2908
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:507