Merge "resourceloader: Fix broken getRequest/getDirection in derived context"
[lhc/web/wiklou.git] / includes / media / DjVu.php
index 1b0eb49..b422bfa 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
@@ -137,31 +148,12 @@ class DjVuHandler extends ImageHandler {
        function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
                global $wgDjvuRenderer, $wgDjvuPostProcessor;
 
-               // Fetch XML and check it, to give a more informative error message than the one which
-               // normaliseParams will inevitably give.
-               $xml = $image->getMetadata();
-               if ( !$xml ) {
-                       $width = isset( $params['width'] ) ? $params['width'] : 0;
-                       $height = isset( $params['height'] ) ? $params['height'] : 0;
-
-                       return new MediaTransformError( 'thumbnail_error', $width, $height,
-                               wfMessage( 'djvu_no_xml' )->text() );
-               }
-
                if ( !$this->normaliseParams( $image, $params ) ) {
                        return new TransformParameterError( $params );
                }
                $width = $params['width'];
                $height = $params['height'];
                $page = $params['page'];
-               if ( $page > $this->pageCount( $image ) ) {
-                       return new MediaTransformError(
-                               'thumbnail_error',
-                               $width,
-                               $height,
-                               wfMessage( 'djvu_page_error' )->text()
-                       );
-               }
 
                if ( $flags & self::TRANSFORM_LATER ) {
                        $params = array(
@@ -273,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;
@@ -312,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;
@@ -335,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 {
@@ -384,29 +376,55 @@ class DjVuHandler extends ImageHandler {
        }
 
        function pageCount( $image ) {
-               $tree = $this->getMetaTree( $image );
-               if ( !$tree ) {
-                       return false;
+               global $wgMemc;
+
+               $key = wfMemcKey( 'file-djvu', 'pageCount', $image->getSha1() );
+
+               $count = $wgMemc->get( $key );
+               if ( $count === false ) {
+                       $tree = $this->getMetaTree( $image );
+                       if ( !$tree ) {
+                               return false;
+                       }
+                       $count = count( $tree->xpath( '//OBJECT' ) );
+                       $wgMemc->set( $key, $count );
                }
 
-               return count( $tree->xpath( '//OBJECT' ) );
+               return $count;
        }
 
        function getPageDimensions( $image, $page ) {
-               $tree = $this->getMetaTree( $image );
-               if ( !$tree ) {
-                       return false;
-               }
+               global $wgMemc;
 
-               $o = $tree->BODY[0]->OBJECT[$page - 1];
-               if ( $o ) {
-                       return array(
-                               'width' => intval( $o['width'] ),
-                               'height' => intval( $o['height'] )
-                       );
-               } else {
-                       return false;
+               $key = wfMemcKey( 'file-djvu', 'dimensions', $image->getSha1() );
+
+               $dimsByPage = $wgMemc->get( $key );
+               if ( !is_array( $dimsByPage ) ) {
+                       $tree = $this->getMetaTree( $image );
+                       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;
+                               }
+                       }
+
+                       $wgMemc->set( $key, $dimsByPage );
                }
+
+               $index = $page - 1; // MW starts pages at 1
+
+               return isset( $dimsByPage[$index] ) ? $dimsByPage[$index] : false;
        }
 
        /**