MediaWiki  1.34.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:1693
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
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
CreatePdfThumbnailsJob\insertJobs
static insertJobs( $upload, $mime, &$error)
Definition: CreatePdfThumbnailsJob.php:95
Linker\makeThumbLinkObj
static makeThumbLinkObj(LinkTarget $title, $file, $label='', $alt='', $align='right', $params=[], $framed=false, $manualthumb="")
Make HTML for a thumbnail including image, border and caption.
Definition: Linker.php:493
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:1007
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:138
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
Job\$metadata
array $metadata
Additional queue metadata.
Definition: Job.php:38
CreatePdfThumbnailsJob
Definition: CreatePdfThumbnailsJob.php:3
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:146
$wgImageLimits
$wgImageLimits
Limit images on image description pages to a user-selectable limit.
Definition: DefaultSettings.php:1461
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:2616