Merge "Cleanup a bunch of tests and add todos"
[lhc/web/wiklou.git] / includes / media / DjVu.php
index fe3313a..1202c9a 100644 (file)
@@ -42,7 +42,7 @@ class DjVuHandler extends ImageHandler {
        }
 
        /**
-        * @param $file
+        * @param File $file
         * @return bool
         */
        function mustRender( $file ) {
@@ -50,7 +50,7 @@ class DjVuHandler extends ImageHandler {
        }
 
        /**
-        * @param $file
+        * @param File $file
         * @return bool
         */
        function isMultiPage( $file ) {
@@ -68,8 +68,8 @@ class DjVuHandler extends ImageHandler {
        }
 
        /**
-        * @param $name
-        * @param $value
+        * @param string $name
+        * @param mixed $value
         * @return bool
         */
        function validateParam( $name, $value ) {
@@ -85,7 +85,7 @@ class DjVuHandler extends ImageHandler {
        }
 
        /**
-        * @param $params
+        * @param array $params
         * @return bool|string
         */
        function makeParamString( $params ) {
@@ -98,7 +98,7 @@ class DjVuHandler extends ImageHandler {
        }
 
        /**
-        * @param $str
+        * @param string $str
         * @return array|bool
         */
        function parseParamString( $str ) {
@@ -111,7 +111,7 @@ class DjVuHandler extends ImageHandler {
        }
 
        /**
-        * @param $params
+        * @param array $params
         * @return array
         */
        function getScriptParams( $params ) {
@@ -122,10 +122,10 @@ class DjVuHandler extends ImageHandler {
        }
 
        /**
-        * @param $image File
-        * @param  $dstPath
-        * @param  $dstUrl
-        * @param  $params
+        * @param File $image
+        * @param string $dstPath
+        * @param string $dstUrl
+        * @param array $params
         * @param int $flags
         * @return MediaTransformError|ThumbnailImage|TransformParameterError
         */
@@ -180,9 +180,12 @@ class DjVuHandler extends ImageHandler {
                $srcPath = $image->getLocalRefPath();
                # 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( $wgDjvuRenderer ) . " -format=ppm -page={$page}" .
-                       " -size={$params['physicalWidth']}x{$params['physicalHeight']} " .
-                       wfEscapeShellArg( $srcPath );
+               $cmd = '(' . wfEscapeShellArg(
+                       $wgDjvuRenderer,
+                       "-format=ppm",
+                       "-page={$page}",
+                       "-size={$params['physicalWidth']}x{$params['physicalHeight']}",
+                       $srcPath );
                if ( $wgDjvuPostProcessor ) {
                        $cmd .= " | {$wgDjvuPostProcessor}";
                }
@@ -195,10 +198,7 @@ class DjVuHandler extends ImageHandler {
 
                $removed = $this->removeBadFile( $dstPath, $retval );
                if ( $retval != 0 || $removed ) {
-                       wfDebugLog( 'thumbnail',
-                               sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
-                                       wfHostname(), $retval, trim( $err ), $cmd ) );
-
+                       $this->logErrorForExternalProcess( $retval, $err, $cmd );
                        return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
                } else {
                        $params = array(
@@ -214,6 +214,8 @@ class DjVuHandler extends ImageHandler {
        /**
         * Cache an instance of DjVuImage in an Image object, return that instance
         *
+        * @param File $image
+        * @param string $path
         * @return DjVuImage
         */
        function getDjVuImage( $image, $path ) {
@@ -230,12 +232,15 @@ class DjVuHandler extends ImageHandler {
 
        /**
         * Cache a document tree for the DjVu XML metadata
-        * @param $image File
-        * @param $gettext Boolean: DOCUMENT (Default: false)
-        * @return bool
+        * @param File $image
+        * @param bool $gettext DOCUMENT (Default: false)
+        * @return bool|SimpleXMLElement
         */
        function getMetaTree( $image, $gettext = false ) {
-               if ( isset( $image->dejaMetaTree ) ) {
+               if ( $gettext && isset( $image->djvuTextTree ) ) {
+                       return $image->djvuTextTree;
+               }
+               if ( !$gettext && isset( $image->dejaMetaTree ) ) {
                        return $image->dejaMetaTree;
                }
 
@@ -254,8 +259,11 @@ class DjVuHandler extends ImageHandler {
                        $image->djvuTextTree = false;
                        $tree = new SimpleXMLElement( $metadata );
                        if ( $tree->getName() == 'mw-djvu' ) {
+                               /** @var SimpleXMLElement $b */
                                foreach ( $tree->children() as $b ) {
                                        if ( $b->getName() == 'DjVuTxt' ) {
+                                               // @todo File::djvuTextTree and File::dejaMetaTree are declared
+                                               // dynamically. Add a public File::$data to facilitate this?
                                                $image->djvuTextTree = $b;
                                        } elseif ( $b->getName() == 'DjVuXML' ) {
                                                $image->dejaMetaTree = $b;
@@ -276,6 +284,11 @@ class DjVuHandler extends ImageHandler {
                }
        }
 
+       /**
+        * @param File $image
+        * @param string $path
+        * @return bool|array False on failure
+        */
        function getImageSize( $image, $path ) {
                return $this->getDjVuImage( $image, $path )->getImageSize();
        }
@@ -331,6 +344,11 @@ class DjVuHandler extends ImageHandler {
                }
        }
 
+       /**
+        * @param File $image
+        * @param int $page Page number to get information for
+        * @return bool|string Page text or false when no text found.
+        */
        function getPageText( $image, $page ) {
                $tree = $this->getMetaTree( $image, true );
                if ( !$tree ) {