r72988 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72987‎ | r72988 | r72989 >
Date:16:17, 14 September 2010
Author:daniel
Status:ok
Tags:
Comment:
allow for page numbering to start at a number different from 1. doesn't quite work with the image page, but no longer fails horribly.
Modified paths:
  • /trunk/extensions/PagedTiffHandler/PagedTiffHandler.image.php (modified) (history)
  • /trunk/extensions/PagedTiffHandler/PagedTiffHandler.php (modified) (history)
  • /trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php (modified) (history)
  • /trunk/extensions/PagedTiffHandler/tests/PagedTiffHandlerTest.php (modified) (history)

Diff [purge]

Index: trunk/extensions/PagedTiffHandler/tests/PagedTiffHandlerTest.php
@@ -145,23 +145,23 @@
146146 // ---- Metadata handling
147147 // getMetadata
148148 $metadata = $this->handler->getMetadata( false, $this->multipage_path );
149 - $this->assertTrue( strpos( $metadata, '"page_amount";i:7' ) !== false );
 149+ $this->assertTrue( strpos( $metadata, '"page_count";i:7' ) !== false );
150150 $this->assertTrue( $this->handler->isMetadataValid( $this->multipage_image, $metadata ) );
151151
152152 $metadata = $this->handler->getMetadata( false, $this->mhz_path );
153 - $this->assertTrue( strpos( $metadata, '"page_amount";i:1' ) !== false );
 153+ $this->assertTrue( strpos( $metadata, '"page_count";i:1' ) !== false );
154154 $this->assertTrue( $this->handler->isMetadataValid( $this->mhz_image, $metadata ) );
155155
156156 // getMetaArray
157157 $metaArray = $this->handler->getMetaArray( $this->mhz_image );
158158 if ( !empty( $metaArray['errors'] ) ) $this->fail( join('; ', $metaArray['error']) );
159 - $this->assertEquals( $metaArray['page_amount'], 1 );
 159+ $this->assertEquals( $metaArray['page_count'], 1 );
160160
161161 $this->assertEquals( strtolower( $metaArray['page_data'][1]['alpha'] ), 'true' );
162162
163163 $metaArray = $this->handler->getMetaArray( $this->multipage_image );
164164 if ( !empty( $metaArray['errors'] ) ) $this->fail( join('; ', $metaArray['error']) );
165 - $this->assertEquals( $metaArray['page_amount'], 7 );
 165+ $this->assertEquals( $metaArray['page_count'], 7 );
166166
167167 $this->assertEquals( strtolower( $metaArray['page_data'][1]['alpha'] ), 'false' );
168168 $this->assertEquals( strtolower( $metaArray['page_data'][2]['alpha'] ), 'true' );
Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php
@@ -47,7 +47,7 @@
4848 return true;
4949 }
5050 $meta = unserialize( $img->metadata );
51 - return $meta['page_amount'] > 1;
 51+ return $meta['page_count'] > 1;
5252 }
5353
5454 /**
@@ -110,13 +110,13 @@
111111 return false;
112112 }
113113
114 - if ( $meta['page_amount'] <= 0 || empty( $meta['page_data'] ) ) {
115 - $error = array( 'tiff_page_error', $meta['page_amount'] );
 114+ if ( $meta['page_count'] <= 0 || empty( $meta['page_data'] ) ) {
 115+ $error = array( 'tiff_page_error', $meta['page_count'] );
116116 wfDebug( __METHOD__ . ": {$error[0]}\n" );
117117 return false;
118118 }
119 - if ( $wgTiffMaxEmbedFiles && $meta['page_amount'] > $wgTiffMaxEmbedFiles ) {
120 - $error = array( 'tiff_too_much_embed_files', $meta['page_amount'], $wgTiffMaxEmbedFiles );
 119+ if ( $wgTiffMaxEmbedFiles && $meta['page_count'] > $wgTiffMaxEmbedFiles ) {
 120+ $error = array( 'tiff_too_much_embed_files', $meta['page_count'], $wgTiffMaxEmbedFiles );
121121 wfDebug( __METHOD__ . ": {$error[0]}\n" );
122122 return false;
123123 }
@@ -231,7 +231,7 @@
232232 $params['lossy'] = 'lossless';
233233 }
234234 } else {
235 - $page = $params['page'];
 235+ $page = $this->adjustPage( $image, $params['page'] );
236236
237237 if ( ( strtolower( $data['page_data'][$page]['alpha'] ) == 'true' ) ) {
238238 $params['lossy'] = 'lossless';
@@ -319,6 +319,7 @@
320320 $height = intval( $params['height'] );
321321 $srcPath = $image->getPath();
322322 $page = intval( $params['page'] );
 323+ $page = $this->adjustPage( $image, $page );
323324
324325 if ( $flags & self::TRANSFORM_LATER ) {
325326 // pretend the thumbnail exists, let it be created by a 404-handler
@@ -415,10 +416,49 @@
416417 if ( !$data ) {
417418 return false;
418419 }
419 - return intval( $data['page_amount'] );
 420+ return intval( $data['page_count'] );
420421 }
421422
422423 /**
 424+ * Returns the number of the first page in the file
 425+ */
 426+ function firstPage( $image ) {
 427+ $data = $this->getMetaArray( $image );
 428+ if ( !$data ) {
 429+ return false;
 430+ }
 431+ return intval( $data['first_page'] );
 432+ }
 433+
 434+ /**
 435+ * Returns the number of the last page in the file
 436+ */
 437+ function lastPage( $image ) {
 438+ $data = $this->getMetaArray( $image );
 439+ if ( !$data ) {
 440+ return false;
 441+ }
 442+ return intval( $data['last_page'] );
 443+ }
 444+
 445+ /**
 446+ * Returns a page number within range.
 447+ */
 448+ function adjustPage( $image, $page ) {
 449+ $page = intval( $page );
 450+
 451+ if ( !$page || $page < $this->firstPage( $image ) ) {
 452+ $page = $this->firstPage( $image );
 453+ }
 454+
 455+ if ( $page > $this->lastPage( $image ) ) {
 456+ $page = $this->lastPage( $image );
 457+ }
 458+
 459+ return $page;
 460+ }
 461+
 462+ /**
423463 * Returns a new error message.
424464 */
425465 protected function doThumbError( $params, $msg ) {
@@ -473,12 +513,8 @@
474514 function getLongDesc( $image ) {
475515 global $wgLang, $wgRequest;
476516 $page = $wgRequest->getText( 'page', 1 );
477 - if ( !isset( $page ) || $page < 1 ) {
478 - $page = 1;
479 - }
480 - if ( $page > $this->pageCount( $image ) ) {
481 - $page = $this->pageCount( $image );
482 - }
 517+ $page = $this->adjustPage( $image, $page );
 518+
483519 $metadata = $this->getMetaArray( $image );
484520 if ( $metadata ) {
485521 wfLoadExtensionMessages( 'PagedTiffHandler' );
@@ -661,12 +697,7 @@
662698 function getPageDimensions( $image, $page ) {
663699 // makeImageLink2 (Linker.php) sets $page to false if no page parameter
664700 // is set in wiki code
665 - if ( !$page ) {
666 - $page = 1;
667 - }
668 - if ( $page > $this->pageCount( $image ) ) {
669 - $page = $this->pageCount( $image );
670 - }
 701+ $page = $this->adjustPage( $image, $page );
671702 $data = $this->getMetaArray( $image );
672703 return PagedTiffImage::getPageSize( $data, $page );
673704 }
Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler.php
@@ -123,7 +123,7 @@
124124 $wgMediaHandlers['image/tiff'] = 'PagedTiffHandler';
125125 $wgHooks['LanguageGetMagic'][] = 'PagedTiffHandler::addTiffLossyMagicWordLang';
126126
127 -define('TIFF_METADATA_VERSION', '1.3');
 127+define('TIFF_METADATA_VERSION', '1.4');
128128 # 1.0: initial
129129 # 1.1: fixed bugs in imageinfo parser
130130 # 1.2: photoshop quirks (reverted)
Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler.image.php
@@ -77,7 +77,9 @@
7878 /**
7979 * Reads metadata of the tiff file via shell command and returns an associative array.
8080 * layout:
81 - * meta['page_amount'] = amount of pages
 81+ * meta['page_count'] = number of pages
 82+ * meta['first_page'] = number of first page
 83+ * meta['last_page'] = number of last page
8284 * meta['page_data'] = metadata per page
8385 * meta['exif'] = Exif, XMP and IPTC
8486 * meta['errors'] = identify-errors
@@ -262,7 +264,6 @@
263265 $value = $m[2];
264266
265267 if ( $key == 'Page Number' && preg_match('/(\d+)-(\d+)/', $value, $m) ) {
266 - $state->setFileProperty('page_amount', (int)$m[2]);
267268 $state->setPageProperty('page', (int)$m[1] +1);
268269 } else if ( $key == 'Samples/Pixel' ) {
269270 if ($value == '4') $state->setPageProperty('alpha', 'true');
@@ -408,13 +409,22 @@
409410 $this->finishPage( );
410411 }
411412
412 - if ( !isset( $this->metadata['page_amount'] ) ) {
413 - $this->metadata['page_amount'] = count( $this->metadata['page_data'] );
414 - }
415 -
416413 if ( ! $this->metadata['page_data'] ) {
417414 $this->metadata['errors'][] = 'no page data found in tiff directory!';
 415+ return;
418416 }
 417+
 418+ if ( !isset( $this->metadata['page_count'] ) ) {
 419+ $this->metadata['page_count'] = count( $this->metadata['page_data'] );
 420+ }
 421+
 422+ if ( !isset( $this->metadata['first_page'] ) ) {
 423+ $this->metadata['first_page'] = min( array_keys( $this->metadata['page_data'] ) );
 424+ }
 425+
 426+ if ( !isset( $this->metadata['last_page'] ) ) {
 427+ $this->metadata['last_page'] = max( array_keys( $this->metadata['page_data'] ) );
 428+ }
419429 }
420430
421431 function resetPage( ) {

Status & tagging log