Index: trunk/extensions/PagedTiffHandler/tests/PagedTiffHandlerTest.php |
— | — | @@ -145,23 +145,23 @@ |
146 | 146 | // ---- Metadata handling |
147 | 147 | // getMetadata |
148 | 148 | $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 ); |
150 | 150 | $this->assertTrue( $this->handler->isMetadataValid( $this->multipage_image, $metadata ) ); |
151 | 151 | |
152 | 152 | $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 ); |
154 | 154 | $this->assertTrue( $this->handler->isMetadataValid( $this->mhz_image, $metadata ) ); |
155 | 155 | |
156 | 156 | // getMetaArray |
157 | 157 | $metaArray = $this->handler->getMetaArray( $this->mhz_image ); |
158 | 158 | if ( !empty( $metaArray['errors'] ) ) $this->fail( join('; ', $metaArray['error']) ); |
159 | | - $this->assertEquals( $metaArray['page_amount'], 1 ); |
| 159 | + $this->assertEquals( $metaArray['page_count'], 1 ); |
160 | 160 | |
161 | 161 | $this->assertEquals( strtolower( $metaArray['page_data'][1]['alpha'] ), 'true' ); |
162 | 162 | |
163 | 163 | $metaArray = $this->handler->getMetaArray( $this->multipage_image ); |
164 | 164 | if ( !empty( $metaArray['errors'] ) ) $this->fail( join('; ', $metaArray['error']) ); |
165 | | - $this->assertEquals( $metaArray['page_amount'], 7 ); |
| 165 | + $this->assertEquals( $metaArray['page_count'], 7 ); |
166 | 166 | |
167 | 167 | $this->assertEquals( strtolower( $metaArray['page_data'][1]['alpha'] ), 'false' ); |
168 | 168 | $this->assertEquals( strtolower( $metaArray['page_data'][2]['alpha'] ), 'true' ); |
Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php |
— | — | @@ -47,7 +47,7 @@ |
48 | 48 | return true; |
49 | 49 | } |
50 | 50 | $meta = unserialize( $img->metadata ); |
51 | | - return $meta['page_amount'] > 1; |
| 51 | + return $meta['page_count'] > 1; |
52 | 52 | } |
53 | 53 | |
54 | 54 | /** |
— | — | @@ -110,13 +110,13 @@ |
111 | 111 | return false; |
112 | 112 | } |
113 | 113 | |
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'] ); |
116 | 116 | wfDebug( __METHOD__ . ": {$error[0]}\n" ); |
117 | 117 | return false; |
118 | 118 | } |
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 ); |
121 | 121 | wfDebug( __METHOD__ . ": {$error[0]}\n" ); |
122 | 122 | return false; |
123 | 123 | } |
— | — | @@ -231,7 +231,7 @@ |
232 | 232 | $params['lossy'] = 'lossless'; |
233 | 233 | } |
234 | 234 | } else { |
235 | | - $page = $params['page']; |
| 235 | + $page = $this->adjustPage( $image, $params['page'] ); |
236 | 236 | |
237 | 237 | if ( ( strtolower( $data['page_data'][$page]['alpha'] ) == 'true' ) ) { |
238 | 238 | $params['lossy'] = 'lossless'; |
— | — | @@ -319,6 +319,7 @@ |
320 | 320 | $height = intval( $params['height'] ); |
321 | 321 | $srcPath = $image->getPath(); |
322 | 322 | $page = intval( $params['page'] ); |
| 323 | + $page = $this->adjustPage( $image, $page ); |
323 | 324 | |
324 | 325 | if ( $flags & self::TRANSFORM_LATER ) { |
325 | 326 | // pretend the thumbnail exists, let it be created by a 404-handler |
— | — | @@ -415,10 +416,49 @@ |
416 | 417 | if ( !$data ) { |
417 | 418 | return false; |
418 | 419 | } |
419 | | - return intval( $data['page_amount'] ); |
| 420 | + return intval( $data['page_count'] ); |
420 | 421 | } |
421 | 422 | |
422 | 423 | /** |
| 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 | + /** |
423 | 463 | * Returns a new error message. |
424 | 464 | */ |
425 | 465 | protected function doThumbError( $params, $msg ) { |
— | — | @@ -473,12 +513,8 @@ |
474 | 514 | function getLongDesc( $image ) { |
475 | 515 | global $wgLang, $wgRequest; |
476 | 516 | $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 | + |
483 | 519 | $metadata = $this->getMetaArray( $image ); |
484 | 520 | if ( $metadata ) { |
485 | 521 | wfLoadExtensionMessages( 'PagedTiffHandler' ); |
— | — | @@ -661,12 +697,7 @@ |
662 | 698 | function getPageDimensions( $image, $page ) { |
663 | 699 | // makeImageLink2 (Linker.php) sets $page to false if no page parameter |
664 | 700 | // 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 ); |
671 | 702 | $data = $this->getMetaArray( $image ); |
672 | 703 | return PagedTiffImage::getPageSize( $data, $page ); |
673 | 704 | } |
Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler.php |
— | — | @@ -123,7 +123,7 @@ |
124 | 124 | $wgMediaHandlers['image/tiff'] = 'PagedTiffHandler'; |
125 | 125 | $wgHooks['LanguageGetMagic'][] = 'PagedTiffHandler::addTiffLossyMagicWordLang'; |
126 | 126 | |
127 | | -define('TIFF_METADATA_VERSION', '1.3'); |
| 127 | +define('TIFF_METADATA_VERSION', '1.4'); |
128 | 128 | # 1.0: initial |
129 | 129 | # 1.1: fixed bugs in imageinfo parser |
130 | 130 | # 1.2: photoshop quirks (reverted) |
Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler.image.php |
— | — | @@ -77,7 +77,9 @@ |
78 | 78 | /** |
79 | 79 | * Reads metadata of the tiff file via shell command and returns an associative array. |
80 | 80 | * 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 |
82 | 84 | * meta['page_data'] = metadata per page |
83 | 85 | * meta['exif'] = Exif, XMP and IPTC |
84 | 86 | * meta['errors'] = identify-errors |
— | — | @@ -262,7 +264,6 @@ |
263 | 265 | $value = $m[2]; |
264 | 266 | |
265 | 267 | if ( $key == 'Page Number' && preg_match('/(\d+)-(\d+)/', $value, $m) ) { |
266 | | - $state->setFileProperty('page_amount', (int)$m[2]); |
267 | 268 | $state->setPageProperty('page', (int)$m[1] +1); |
268 | 269 | } else if ( $key == 'Samples/Pixel' ) { |
269 | 270 | if ($value == '4') $state->setPageProperty('alpha', 'true'); |
— | — | @@ -408,13 +409,22 @@ |
409 | 410 | $this->finishPage( ); |
410 | 411 | } |
411 | 412 | |
412 | | - if ( !isset( $this->metadata['page_amount'] ) ) { |
413 | | - $this->metadata['page_amount'] = count( $this->metadata['page_data'] ); |
414 | | - } |
415 | | - |
416 | 413 | if ( ! $this->metadata['page_data'] ) { |
417 | 414 | $this->metadata['errors'][] = 'no page data found in tiff directory!'; |
| 415 | + return; |
418 | 416 | } |
| 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 | + } |
419 | 429 | } |
420 | 430 | |
421 | 431 | function resetPage( ) { |