http://www.mediawiki.org/wiki/Special:Code/MediaWiki/72988

Revision: 72988
Author:   daniel
Date:     2010-09-14 16:17:54 +0000 (Tue, 14 Sep 2010)

Log Message:
-----------
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
    trunk/extensions/PagedTiffHandler/PagedTiffHandler.php
    trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php
    trunk/extensions/PagedTiffHandler/tests/PagedTiffHandlerTest.php

Modified: trunk/extensions/PagedTiffHandler/PagedTiffHandler.image.php
===================================================================
--- trunk/extensions/PagedTiffHandler/PagedTiffHandler.image.php        
2010-09-14 16:16:01 UTC (rev 72987)
+++ trunk/extensions/PagedTiffHandler/PagedTiffHandler.image.php        
2010-09-14 16:17:54 UTC (rev 72988)
@@ -77,7 +77,9 @@
        /**
         * Reads metadata of the tiff file via shell command and returns an 
associative array.
         * layout:
-        * meta['page_amount'] = amount of pages
+        * meta['page_count'] = number of pages
+        * meta['first_page'] = number of first page
+        * meta['last_page'] = number of last page
         * meta['page_data'] = metadata per page
         * meta['exif']  = Exif, XMP and IPTC
         * meta['errors'] = identify-errors
@@ -262,7 +264,6 @@
                                $value = $m[2];
 
                                if ( $key == 'Page Number' && 
preg_match('/(\d+)-(\d+)/', $value, $m) ) {
-                                       $state->setFileProperty('page_amount', 
(int)$m[2]);
                                        $state->setPageProperty('page', 
(int)$m[1] +1);
                                } else if ( $key == 'Samples/Pixel' ) {
                                        if ($value == '4') 
$state->setPageProperty('alpha', 'true');
@@ -408,13 +409,22 @@
                        $this->finishPage( );
                }
 
-               if ( !isset( $this->metadata['page_amount'] ) ) {
-                       $this->metadata['page_amount'] = count( 
$this->metadata['page_data'] );
-               }
-
                if ( ! $this->metadata['page_data'] ) {
                        $this->metadata['errors'][] = 'no page data found in 
tiff directory!';
+                       return;
                }
+
+               if ( !isset( $this->metadata['page_count'] ) ) {
+                       $this->metadata['page_count'] = count( 
$this->metadata['page_data'] );
+               }
+
+               if ( !isset( $this->metadata['first_page'] ) ) {
+                       $this->metadata['first_page'] = min( array_keys( 
$this->metadata['page_data'] ) );
+               }
+
+               if ( !isset( $this->metadata['last_page'] ) ) {
+                       $this->metadata['last_page'] = max( array_keys( 
$this->metadata['page_data'] ) );
+               }
        }
 
        function resetPage( ) {

Modified: trunk/extensions/PagedTiffHandler/PagedTiffHandler.php
===================================================================
--- trunk/extensions/PagedTiffHandler/PagedTiffHandler.php      2010-09-14 
16:16:01 UTC (rev 72987)
+++ trunk/extensions/PagedTiffHandler/PagedTiffHandler.php      2010-09-14 
16:17:54 UTC (rev 72988)
@@ -123,7 +123,7 @@
 $wgMediaHandlers['image/tiff'] = 'PagedTiffHandler';
 $wgHooks['LanguageGetMagic'][] = 'PagedTiffHandler::addTiffLossyMagicWordLang';
 
-define('TIFF_METADATA_VERSION', '1.3');
+define('TIFF_METADATA_VERSION', '1.4');
 # 1.0: initial
 # 1.1: fixed bugs in imageinfo parser
 # 1.2: photoshop quirks (reverted)

Modified: trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php
===================================================================
--- trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php 2010-09-14 
16:16:01 UTC (rev 72987)
+++ trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php 2010-09-14 
16:17:54 UTC (rev 72988)
@@ -47,7 +47,7 @@
                        return true;
                }
                $meta = unserialize( $img->metadata );
-               return $meta['page_amount'] > 1;
+               return $meta['page_count'] > 1;
        }
 
        /**
@@ -110,13 +110,13 @@
                        return false;
                }
 
-               if ( $meta['page_amount'] <= 0 || empty( $meta['page_data'] ) ) 
{
-                       $error = array( 'tiff_page_error', $meta['page_amount'] 
);
+               if ( $meta['page_count'] <= 0 || empty( $meta['page_data'] ) ) {
+                       $error = array( 'tiff_page_error', $meta['page_count'] 
);
                        wfDebug( __METHOD__ . ": {$error[0]}\n" );
                        return false;
                }
-               if ( $wgTiffMaxEmbedFiles && $meta['page_amount'] > 
$wgTiffMaxEmbedFiles ) {
-                       $error = array( 'tiff_too_much_embed_files', 
$meta['page_amount'], $wgTiffMaxEmbedFiles );
+               if ( $wgTiffMaxEmbedFiles && $meta['page_count'] > 
$wgTiffMaxEmbedFiles ) {
+                       $error = array( 'tiff_too_much_embed_files', 
$meta['page_count'], $wgTiffMaxEmbedFiles );
                        wfDebug( __METHOD__ . ": {$error[0]}\n" );
                        return false;
                }
@@ -231,7 +231,7 @@
                                $params['lossy'] = 'lossless';
                        }
                } else {
-                       $page = $params['page'];
+                       $page = $this->adjustPage( $image, $params['page'] );
 
                        if ( ( strtolower( $data['page_data'][$page]['alpha'] ) 
== 'true' ) ) {
                                $params['lossy'] = 'lossless';
@@ -319,6 +319,7 @@
                $height = intval( $params['height'] );
                $srcPath = $image->getPath();
                $page = intval( $params['page'] );
+               $page = $this->adjustPage( $image, $page );
 
                if ( $flags & self::TRANSFORM_LATER ) { 
                        // pretend the thumbnail exists, let it be created by a 
404-handler
@@ -415,10 +416,49 @@
                if ( !$data ) {
                        return false;
                }
-               return intval( $data['page_amount'] );
+               return intval( $data['page_count'] );
        }
 
        /**
+        * Returns the number of the first page in the file
+        */
+       function firstPage( $image ) {
+               $data = $this->getMetaArray( $image );
+               if ( !$data ) {
+                       return false;
+               }
+               return intval( $data['first_page'] );
+       }
+
+       /**
+        * Returns the number of the last page in the file
+        */
+       function lastPage( $image ) {
+               $data = $this->getMetaArray( $image );
+               if ( !$data ) {
+                       return false;
+               }
+               return intval( $data['last_page'] );
+       }
+
+       /**
+        * Returns a page number within range. 
+        */
+       function adjustPage( $image, $page ) {
+               $page = intval( $page );
+
+               if ( !$page || $page < $this->firstPage( $image ) ) {
+                       $page = $this->firstPage( $image );
+               }
+
+               if ( $page > $this->lastPage( $image ) ) {
+                       $page = $this->lastPage( $image );
+               }
+
+               return $page;
+       }
+
+       /**
         * Returns a new error message.
         */
        protected function doThumbError( $params, $msg ) {
@@ -473,12 +513,8 @@
        function getLongDesc( $image ) {
                global $wgLang, $wgRequest;
                $page = $wgRequest->getText( 'page', 1 );
-               if ( !isset( $page ) || $page < 1 ) {
-                       $page = 1;
-               }
-               if ( $page > $this->pageCount( $image ) ) {
-                       $page = $this->pageCount( $image );
-               }
+               $page = $this->adjustPage( $image, $page );
+
                $metadata = $this->getMetaArray( $image );
                if ( $metadata ) {
                        wfLoadExtensionMessages( 'PagedTiffHandler' );
@@ -661,12 +697,7 @@
        function getPageDimensions( $image, $page ) {
                // makeImageLink2 (Linker.php) sets $page to false if no page 
parameter  
                // is set in wiki code 
-               if ( !$page ) {
-                       $page = 1;
-               }
-               if ( $page > $this->pageCount( $image ) ) {
-                       $page = $this->pageCount( $image );
-               }
+               $page = $this->adjustPage( $image, $page );
                $data = $this->getMetaArray( $image );
                return PagedTiffImage::getPageSize( $data, $page );
        }

Modified: trunk/extensions/PagedTiffHandler/tests/PagedTiffHandlerTest.php
===================================================================
--- trunk/extensions/PagedTiffHandler/tests/PagedTiffHandlerTest.php    
2010-09-14 16:16:01 UTC (rev 72987)
+++ trunk/extensions/PagedTiffHandler/tests/PagedTiffHandlerTest.php    
2010-09-14 16:17:54 UTC (rev 72988)
@@ -145,23 +145,23 @@
                // ---- Metadata handling
                // getMetadata
                $metadata =  $this->handler->getMetadata( false, 
$this->multipage_path );
-               $this->assertTrue( strpos( $metadata, '"page_amount";i:7' ) !== 
false );
+               $this->assertTrue( strpos( $metadata, '"page_count";i:7' ) !== 
false );
                $this->assertTrue( $this->handler->isMetadataValid( 
$this->multipage_image, $metadata ) );
 
                $metadata =  $this->handler->getMetadata( false, 
$this->mhz_path );
-               $this->assertTrue( strpos( $metadata, '"page_amount";i:1' ) !== 
false );
+               $this->assertTrue( strpos( $metadata, '"page_count";i:1' ) !== 
false );
                $this->assertTrue( $this->handler->isMetadataValid( 
$this->mhz_image, $metadata ) );
 
                // getMetaArray
                $metaArray = $this->handler->getMetaArray( $this->mhz_image );
                if ( !empty( $metaArray['errors'] ) ) $this->fail( join('; ', 
$metaArray['error']) );
-               $this->assertEquals( $metaArray['page_amount'], 1 );
+               $this->assertEquals( $metaArray['page_count'], 1 );
 
                $this->assertEquals( strtolower( 
$metaArray['page_data'][1]['alpha'] ), 'true' );
 
                $metaArray = $this->handler->getMetaArray( 
$this->multipage_image );
                if ( !empty( $metaArray['errors'] ) ) $this->fail( join('; ', 
$metaArray['error']) );
-               $this->assertEquals( $metaArray['page_amount'], 7 );
+               $this->assertEquals( $metaArray['page_count'], 7 );
 
                $this->assertEquals( strtolower( 
$metaArray['page_data'][1]['alpha'] ), 'false' );
                $this->assertEquals( strtolower( 
$metaArray['page_data'][2]['alpha'] ), 'true' );



_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to