MagicWord: Fix an indent
[lhc/web/wiklou.git] / includes / media / DjVu.php
index 3bc5636..566efb2 100644 (file)
@@ -73,6 +73,11 @@ class DjVuHandler extends ImageHandler {
         * @return bool
         */
        function validateParam( $name, $value ) {
+               if ( $name === 'page' && trim( $value ) !== (string) intval( $value ) ) {
+                       // Extra junk on the end of page, probably actually a caption
+                       // e.g. [[File:Foo.djvu|thumb|Page 3 of the document shows foo]]
+                       return false;
+               }
                if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
                        if ( $value <= 0 ) {
                                return false;
@@ -177,7 +182,33 @@ class DjVuHandler extends ImageHandler {
                        );
                }
 
-               $srcPath = $image->getLocalRefPath();
+               // Get local copy source for shell scripts
+               // Thumbnail extraction is very inefficient for large files.
+               // Provide a way to pool count limit the number of downloaders.
+               if ( $image->getSize() >= 1e7 ) { // 10MB
+                       $work = new PoolCounterWorkViaCallback( 'GetLocalFileCopy', sha1( $image->getName() ),
+                               array(
+                                       'doWork' => function() use ( $image ) {
+                                               return $image->getLocalRefPath();
+                                       }
+                               )
+                       );
+                       $srcPath = $work->execute();
+               } else {
+                       $srcPath = $image->getLocalRefPath();
+               }
+
+               if ( $srcPath === false ) { // Failed to get local copy
+                       wfDebugLog( 'thumbnail',
+                               sprintf( 'Thumbnail failed on %s: could not get local copy of "%s"',
+                                       wfHostname(), $image->getName() ) );
+
+                       return new MediaTransformError( 'thumbnail_error',
+                               $params['width'], $params['height'],
+                               wfMessage( 'filemissing' )->text()
+                       );
+               }
+
                # Use a subshell (brackets) to aggregate stderr from both pipeline commands
                # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
                $cmd = '(' . wfEscapeShellArg(
@@ -234,7 +265,7 @@ class DjVuHandler extends ImageHandler {
         * Get metadata, unserializing it if neccessary.
         *
         * @param File $file The DjVu file in question
-        * @return String XML metadata as a string.
+        * @return string XML metadata as a string.
         */
        private function getUnserializedMetadata( File $file ) {
                $metadata = $file->getMetadata();