r76941 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76940‎ | r76941 | r76942 >
Date:09:06, 18 November 2010
Author:holek
Status:ok (Comments)
Tags:
Comment:
adding a job for PdfHandler to create PDF thumbnails in the background; disabled by default to not change current behaviour
Modified paths:
  • /trunk/extensions/PdfHandler/CreatePdfThumbnailsJob.class.php (added) (history)
  • /trunk/extensions/PdfHandler/PdfHandler.php (modified) (history)

Diff [purge]

Index: trunk/extensions/PdfHandler/PdfHandler.php
@@ -32,7 +32,7 @@
3333 $wgExtensionCredits['media'][] = array(
3434 'path' => __FILE__,
3535 'name' => 'PDF Handler',
36 - 'author' => 'Martin Seidel',
 36+ 'author' => array( 'Martin Seidel', 'Mike Połtyn'),
3737 'descriptionmsg' => 'pdf-desc',
3838 'url' => 'http://www.mediawiki.org/wiki/Extension:PdfHandler',
3939 );
@@ -46,6 +46,11 @@
4747 $wgPdfOutputExtension = 'jpg';
4848 $wgPdfHandlerDpi = 150;
4949
 50+// This setting, if enabled, will put creating thumbnails into a job queue,
 51+// so they do not have to be created on-the-fly,
 52+// but rather inconspicuously during normal wiki browsing
 53+$wgPdfCreateThumbnailsInJobQueue = false;
 54+
5055 // To upload new PDF files you'll need to do this too:
5156 // $wgFileExtensions[] = 'pdf';
5257
@@ -53,4 +58,7 @@
5459 $wgExtensionMessagesFiles['PdfHandler'] = $dir . 'PdfHandler.i18n.php';
5560 $wgAutoloadClasses['PdfImage'] = $dir . 'PdfHandler.image.php';
5661 $wgAutoloadClasses['PdfHandler'] = $dir . 'PdfHandler_body.php';
 62+$wgAutoloadClasses['CreatePdfThumbnailsJob'] = $dir . 'CreatePdfThumbnailsJob.class.php';
5763 $wgMediaHandlers['application/pdf'] = 'PdfHandler';
 64+$wgJobClasses['createPdfThumbnailsJob'] = 'CreatePdfThumbnailsJob';
 65+$wgHooks['UploadVerifyFile'][] = 'CreatePdfThumbnailsJob::insertJobs';
Index: trunk/extensions/PdfHandler/CreatePdfThumbnailsJob.class.php
@@ -0,0 +1,124 @@
 2+<?php
 3+
 4+class CreatePdfThumbnailsJob extends Job {
 5+ /**
 6+ * Flags for thumbnail jobs
 7+ */
 8+ const BIG_THUMB = 1;
 9+ const SMALL_THUMB = 2;
 10+
 11+ /**
 12+ * Construct a thumbnail job
 13+ *
 14+ * @param $title Title: Title object
 15+ * @param $params Associative array of options:
 16+ * page: page number for which the thumbnail will be created
 17+ * jobtype: CreatePDFThumbnailsJob::BIG_THUMB or CreatePDFThumbnailsJob::SMALL_THUMB
 18+ * BIG_THUMB will create a thumbnail visible for full thumbnail view,
 19+ * SMALL_THUMB will create a thumbnail shown in "previous page"/"next page" boxes
 20+ *
 21+ */
 22+ public function __construct( $title, $params ) {
 23+ parent::__construct( 'createPdfThumbnailsJob', $title, $params );
 24+ }
 25+
 26+ /**
 27+ * Run a thumbnail job on a given PDF file.
 28+ * @return bool true
 29+ */
 30+ public function run() {
 31+ if ( !isset( $this->params['page'] ) ) {
 32+ wfDebugLog('thumbnails', 'A page for thumbnails job of ' . $this->title->getText() . ' was not specified! That should never happen!');
 33+ return true; // no page set? that should never happen
 34+ }
 35+
 36+ $file = wfLocalFile( $this->title ); // we just want a local file
 37+ if ( !$file ) {
 38+ return true; // Just silently fail, perhaps the file was already deleted, don't bother
 39+ }
 40+
 41+ switch ($this->params['jobtype']) {
 42+ case self::BIG_THUMB:
 43+ global $wgImageLimits;
 44+ // Ignore user preferences, do default thumbnails
 45+ // everything here shamelessy copied and reused from includes/ImagePage.php
 46+ $sizeSel = User::getDefaultOption( 'imagesize' );
 47+
 48+ // The user offset might still be incorrect, specially if
 49+ // $wgImageLimits got changed (see bug #8858).
 50+ if ( !isset( $wgImageLimits[$sizeSel] ) ) {
 51+ // Default to the first offset in $wgImageLimits
 52+ $sizeSel = 0;
 53+ }
 54+ $max = $wgImageLimits[$sizeSel];
 55+ $maxWidth = $max[0];
 56+ $maxHeight = $max[1];
 57+
 58+ $width_orig = $file->getWidth( $this->params['page'] );
 59+ $width = $width_orig;
 60+ $height_orig = $file->getHeight( $this->params['page'] );
 61+ $height = $height_orig;
 62+ if ( $width > $maxWidth || $height > $maxHeight ) {
 63+ # Calculate the thumbnail size.
 64+ # First case, the limiting factor is the width, not the height.
 65+ if ( $width / $height >= $maxWidth / $maxHeight ) {
 66+ $height = round( $height * $maxWidth / $width );
 67+ $width = $maxWidth;
 68+ # Note that $height <= $maxHeight now.
 69+ } else {
 70+ $newwidth = floor( $width * $maxHeight / $height );
 71+ $height = round( $height * $newwidth / $width );
 72+ $width = $newwidth;
 73+ # Note that $height <= $maxHeight now, but might not be identical
 74+ # because of rounding.
 75+ }
 76+ $transformParams = array( 'page' => $this->params['page'], 'width' => $width );
 77+ $file->transform( $transformParams );
 78+ }
 79+ break;
 80+
 81+ case self::SMALL_THUMB:
 82+ // @TODO: Minor, get rid of thumbnail preferences ($wgUser dependance)
 83+ global $wgUser;
 84+ $sk = $wgUser->getSkin();
 85+ $sk->makeThumbLinkObj( $this->title, $file, '', '', 'none', array( 'page' => $this->params['page'] ) );
 86+ break;
 87+ }
 88+
 89+ return true;
 90+ }
 91+
 92+
 93+ public static function insertJobs( $upload, $mime, &$error ) {
 94+ global $wgPdfCreateThumbnailsInJobQueue;
 95+ if ( !$wgPdfCreateThumbnailsInJobQueue ) {
 96+ return true;
 97+ }
 98+ if (!MimeMagic::singleton()->isMatchingExtension('pdf', $mime)) {
 99+ return true; // not a PDF, abort
 100+ }
 101+
 102+ $title = $upload->getTitle();
 103+ $uploadFile = $upload->getLocalFile();
 104+ if ( is_null($uploadFile) ) {
 105+ wfDebugLog('thumbnails', '$uploadFile seems to be null, should never happen...');
 106+ return true; // should never happen, but it's better to be secure
 107+ }
 108+
 109+ $metadata = $uploadFile->getMetadata();
 110+ $unserialized = unserialize( $metadata );
 111+ $pages = intval( $unserialized['Pages'] );
 112+
 113+ $jobs = array();
 114+ for ($i = 1; $i <= $pages; $i++) {
 115+ $jobs[] = new CreatePdfThumbnailsJob( $title,
 116+ array( 'page' => $i, 'jobtype' => self::BIG_THUMB )
 117+ );
 118+ $jobs[] = new CreatePdfThumbnailsJob( $title,
 119+ array( 'page' => $i, 'jobtype' => self::SMALL_THUMB )
 120+ );
 121+ }
 122+ Job::batchInsert( $jobs );
 123+ return true;
 124+ }
 125+}
Property changes on: trunk/extensions/PdfHandler/CreatePdfThumbnailsJob.class.php
___________________________________________________________________
Added: svn:eol-style
1126 + native

Comments

#Comment by Holek (talk | contribs)   10:08, 18 November 2010

solves bug 22902 (forgot to mention that in commit summary)

#Comment by Bryan (talk | contribs)   12:38, 5 January 2011

This really belongs in core to support pre-caching thumbnails for any file format rather than just PDFs.

#Comment by P858snake (talk | contribs)   08:14, 29 October 2011

Do you know if there a bug lying around requesting this?

Status & tagging log