Merge "Add help link to Special:Search"
[lhc/web/wiklou.git] / includes / media / DjVu.php
index 011fb2a..662c330 100644 (file)
@@ -27,6 +27,8 @@
  * @ingroup Media
  */
 class DjVuHandler extends ImageHandler {
+       const EXPENSIVE_SIZE_LIMIT = 10485760; // 10MiB
+
        /**
         * @return bool
         */
@@ -49,6 +51,15 @@ class DjVuHandler extends ImageHandler {
                return true;
        }
 
+       /**
+        * True if creating thumbnails from the file is large or otherwise resource-intensive.
+        * @param File $file
+        * @return bool
+        */
+       public function isExpensiveToThumbnail( $file ) {
+               return $file->getSize() > static::EXPENSIVE_SIZE_LIMIT;
+       }
+
        /**
         * @param File $file
         * @return bool
@@ -254,9 +265,9 @@ class DjVuHandler extends ImageHandler {
                        return $metadata;
                }
 
-               wfSuppressWarnings();
+               MediaWiki\suppressWarnings();
                $unser = unserialize( $metadata );
-               wfRestoreWarnings();
+               MediaWiki\restoreWarnings();
                if ( is_array( $unser ) ) {
                        if ( isset( $unser['error'] ) ) {
                                return false;
@@ -278,7 +289,7 @@ class DjVuHandler extends ImageHandler {
         * @param bool $gettext DOCUMENT (Default: false)
         * @return bool|SimpleXMLElement
         */
-       function getMetaTree( $image, $gettext = false ) {
+       public function getMetaTree( $image, $gettext = false ) {
                if ( $gettext && isset( $image->djvuTextTree ) ) {
                        return $image->djvuTextTree;
                }
@@ -293,7 +304,7 @@ class DjVuHandler extends ImageHandler {
                        return false;
                }
 
-               wfSuppressWarnings();
+               MediaWiki\suppressWarnings();
                try {
                        // Set to false rather than null to avoid further attempts
                        $image->dejaMetaTree = false;
@@ -316,7 +327,7 @@ class DjVuHandler extends ImageHandler {
                } catch ( Exception $e ) {
                        wfDebug( "Bogus multipage XML metadata on '{$image->getName()}'\n" );
                }
-               wfRestoreWarnings();
+               MediaWiki\restoreWarnings();
                if ( $gettext ) {
                        return $image->djvuTextTree;
                } else {
@@ -364,30 +375,52 @@ class DjVuHandler extends ImageHandler {
                return !empty( $metadata ) && $metadata != serialize( array() );
        }
 
-       function pageCount( $image ) {
-               $tree = $this->getMetaTree( $image );
-               if ( !$tree ) {
-                       return false;
-               }
+       function pageCount( File $image ) {
+               $info = $this->getDimensionInfo( $image );
 
-               return count( $tree->xpath( '//OBJECT' ) );
+               return $info ? $info['pageCount'] : false;
        }
 
-       function getPageDimensions( $image, $page ) {
-               $tree = $this->getMetaTree( $image );
-               if ( !$tree ) {
-                       return false;
-               }
+       function getPageDimensions( File $image, $page ) {
+               $index = $page - 1; // MW starts pages at 1
 
-               $o = $tree->BODY[0]->OBJECT[$page - 1];
-               if ( $o ) {
-                       return array(
-                               'width' => intval( $o['width'] ),
-                               'height' => intval( $o['height'] )
-                       );
-               } else {
-                       return false;
+               $info = $this->getDimensionInfo( $image );
+               if ( $info && isset( $info['dimensionsByPage'][$index] ) ) {
+                       return $info['dimensionsByPage'][$index];
                }
+
+               return false;
+       }
+
+       protected function getDimensionInfo( File $file ) {
+               $that = $this;
+
+               return ObjectCache::getMainWANInstance()->getWithSetCallback(
+                       wfMemcKey( 'file-djvu', 'dimensions', $file->getSha1() ),
+                       WANObjectCache::TTL_INDEFINITE,
+                       function () use ( $that, $file ) {
+                               $tree = $that->getMetaTree( $file );
+                               if ( !$tree ) {
+                                       return false;
+                               }
+
+                               $dimsByPage = array();
+                               $count = count( $tree->xpath( '//OBJECT' ) );
+                               for ( $i = 0; $i < $count; ++$i ) {
+                                       $o = $tree->BODY[0]->OBJECT[$i];
+                                       if ( $o ) {
+                                               $dimsByPage[$i] = array(
+                                                       'width' => (int)$o['width'],
+                                                       'height' => (int)$o['height']
+                                               );
+                                       } else {
+                                               $dimsByPage[$i] = false;
+                                       }
+                               }
+
+                               return array( 'pageCount' => $count, 'dimensionsByPage' => $dimsByPage );
+                       }
+               );
        }
 
        /**
@@ -395,7 +428,7 @@ class DjVuHandler extends ImageHandler {
         * @param int $page Page number to get information for
         * @return bool|string Page text or false when no text found.
         */
-       function getPageText( $image, $page ) {
+       function getPageText( File $image, $page ) {
                $tree = $this->getMetaTree( $image, true );
                if ( !$tree ) {
                        return false;