2010-11-18 09:06:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class CreatePdfThumbnailsJob extends Job {
|
|
|
|
/**
|
|
|
|
* Flags for thumbnail jobs
|
|
|
|
*/
|
|
|
|
const BIG_THUMB = 1;
|
|
|
|
const SMALL_THUMB = 2;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct a thumbnail job
|
|
|
|
*
|
2012-07-26 22:29:49 +00:00
|
|
|
* @param $title Title Title object
|
|
|
|
* @param $params array Associative array of options:
|
2017-05-20 20:58:19 +00:00
|
|
|
* page: page number for which the thumbnail will be created
|
|
|
|
* jobtype: CreatePDFThumbnailsJob::BIG_THUMB or CreatePDFThumbnailsJob::SMALL_THUMB
|
|
|
|
* BIG_THUMB will create a thumbnail visible for full thumbnail view,
|
|
|
|
* SMALL_THUMB will create a thumbnail shown in "previous page"/"next page" boxes
|
2010-11-18 09:06:01 +00:00
|
|
|
*/
|
|
|
|
public function __construct( $title, $params ) {
|
|
|
|
parent::__construct( 'createPdfThumbnailsJob', $title, $params );
|
|
|
|
}
|
2017-05-20 20:58:19 +00:00
|
|
|
|
2010-11-18 09:06:01 +00:00
|
|
|
/**
|
|
|
|
* Run a thumbnail job on a given PDF file.
|
|
|
|
* @return bool true
|
|
|
|
*/
|
|
|
|
public function run() {
|
|
|
|
if ( !isset( $this->params['page'] ) ) {
|
2017-05-20 20:58:19 +00:00
|
|
|
wfDebugLog( 'thumbnails', 'A page for thumbnails job of ' . $this->title->getText() .
|
|
|
|
' was not specified! That should never happen!' );
|
2010-11-18 09:06:01 +00:00
|
|
|
return true; // no page set? that should never happen
|
|
|
|
}
|
|
|
|
|
|
|
|
$file = wfLocalFile( $this->title ); // we just want a local file
|
|
|
|
if ( !$file ) {
|
|
|
|
return true; // Just silently fail, perhaps the file was already deleted, don't bother
|
|
|
|
}
|
|
|
|
|
2017-05-20 20:58:19 +00:00
|
|
|
switch ( $this->params['jobtype'] ) {
|
2010-11-18 09:06:01 +00:00
|
|
|
case self::BIG_THUMB:
|
|
|
|
global $wgImageLimits;
|
|
|
|
// Ignore user preferences, do default thumbnails
|
|
|
|
// everything here shamelessy copied and reused from includes/ImagePage.php
|
|
|
|
$sizeSel = User::getDefaultOption( 'imagesize' );
|
|
|
|
|
|
|
|
// The user offset might still be incorrect, specially if
|
|
|
|
// $wgImageLimits got changed (see bug #8858).
|
|
|
|
if ( !isset( $wgImageLimits[$sizeSel] ) ) {
|
|
|
|
// Default to the first offset in $wgImageLimits
|
|
|
|
$sizeSel = 0;
|
|
|
|
}
|
|
|
|
$max = $wgImageLimits[$sizeSel];
|
|
|
|
$maxWidth = $max[0];
|
|
|
|
$maxHeight = $max[1];
|
2012-01-28 17:05:20 +00:00
|
|
|
|
2010-11-18 09:06:01 +00:00
|
|
|
$width_orig = $file->getWidth( $this->params['page'] );
|
|
|
|
$width = $width_orig;
|
|
|
|
$height_orig = $file->getHeight( $this->params['page'] );
|
|
|
|
$height = $height_orig;
|
|
|
|
if ( $width > $maxWidth || $height > $maxHeight ) {
|
2017-05-20 20:58:19 +00:00
|
|
|
// Calculate the thumbnail size.
|
|
|
|
// First case, the limiting factor is the width, not the height.
|
2010-11-18 09:06:01 +00:00
|
|
|
if ( $width / $height >= $maxWidth / $maxHeight ) {
|
2017-05-20 20:58:19 +00:00
|
|
|
// $height = round( $height * $maxWidth / $width );
|
2010-11-18 09:06:01 +00:00
|
|
|
$width = $maxWidth;
|
2017-05-20 20:58:19 +00:00
|
|
|
// Note that $height <= $maxHeight now.
|
2010-11-18 09:06:01 +00:00
|
|
|
} else {
|
|
|
|
$newwidth = floor( $width * $maxHeight / $height );
|
2017-05-20 20:58:19 +00:00
|
|
|
// $height = round( $height * $newwidth / $width );
|
2010-11-18 09:06:01 +00:00
|
|
|
$width = $newwidth;
|
2017-05-20 20:58:19 +00:00
|
|
|
// Note that $height <= $maxHeight now, but might not be identical
|
|
|
|
// because of rounding.
|
2010-11-18 09:06:01 +00:00
|
|
|
}
|
2017-05-20 20:58:19 +00:00
|
|
|
$transformParams = [ 'page' => $this->params['page'], 'width' => $width ];
|
2010-11-18 09:06:01 +00:00
|
|
|
$file->transform( $transformParams );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case self::SMALL_THUMB:
|
2017-05-20 20:58:19 +00:00
|
|
|
Linker::makeThumbLinkObj( $this->title, $file, '', '', 'none',
|
|
|
|
[ 'page' => $this->params['page'] ] );
|
2010-11-18 09:06:01 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-28 17:05:20 +00:00
|
|
|
/**
|
2012-07-26 22:29:49 +00:00
|
|
|
* @param $upload UploadBase
|
2012-01-28 17:05:20 +00:00
|
|
|
* @param $mime
|
|
|
|
* @param $error
|
|
|
|
* @return bool
|
|
|
|
*/
|
2010-11-18 09:06:01 +00:00
|
|
|
public static function insertJobs( $upload, $mime, &$error ) {
|
|
|
|
global $wgPdfCreateThumbnailsInJobQueue;
|
|
|
|
if ( !$wgPdfCreateThumbnailsInJobQueue ) {
|
|
|
|
return true;
|
|
|
|
}
|
2017-05-20 20:58:19 +00:00
|
|
|
if ( !MimeMagic::singleton()->isMatchingExtension( 'pdf', $mime ) ) {
|
2010-11-18 09:06:01 +00:00
|
|
|
return true; // not a PDF, abort
|
|
|
|
}
|
|
|
|
|
|
|
|
$title = $upload->getTitle();
|
|
|
|
$uploadFile = $upload->getLocalFile();
|
2012-07-26 22:29:49 +00:00
|
|
|
if ( is_null( $uploadFile ) ) {
|
2017-05-20 20:58:19 +00:00
|
|
|
wfDebugLog( 'thumbnails', '$uploadFile seems to be null, should never happen...' );
|
2010-11-18 09:06:01 +00:00
|
|
|
return true; // should never happen, but it's better to be secure
|
|
|
|
}
|
|
|
|
|
|
|
|
$metadata = $uploadFile->getMetadata();
|
|
|
|
$unserialized = unserialize( $metadata );
|
|
|
|
$pages = intval( $unserialized['Pages'] );
|
|
|
|
|
2017-05-20 20:58:19 +00:00
|
|
|
$jobs = [];
|
2012-07-26 22:29:49 +00:00
|
|
|
for ( $i = 1; $i <= $pages; $i++ ) {
|
2016-11-30 00:10:58 +00:00
|
|
|
$jobs[] = new CreatePdfThumbnailsJob(
|
|
|
|
$title,
|
2017-05-20 20:58:19 +00:00
|
|
|
[ 'page' => $i, 'jobtype' => self::BIG_THUMB ]
|
2016-11-30 00:10:58 +00:00
|
|
|
);
|
|
|
|
$jobs[] = new CreatePdfThumbnailsJob(
|
|
|
|
$title,
|
2017-05-20 20:58:19 +00:00
|
|
|
[ 'page' => $i, 'jobtype' => self::SMALL_THUMB ]
|
2016-11-30 00:10:58 +00:00
|
|
|
);
|
2010-11-18 09:06:01 +00:00
|
|
|
}
|
2016-11-30 00:10:58 +00:00
|
|
|
JobQueueGroup::singleton()->push( $jobs );
|
2010-11-18 09:06:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|