Merge "Cleanup a bunch of tests and add todos"
[lhc/web/wiklou.git] / includes / media / DjVuImage.php
index f201c28..e1e7992 100644 (file)
@@ -3,7 +3,7 @@
  * DjVu image handler.
  *
  * Copyright © 2006 Brion Vibber <brion@pobox.com>
- * http://www.mediawiki.org/
+ * https://www.mediawiki.org/
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * @ingroup Media
  */
 class DjVuImage {
+       /**
+        * @const DJVUTXT_MEMORY_LIMIT Memory limit for the DjVu description software
+        */
+       const DJVUTXT_MEMORY_LIMIT = 300000;
+
        /**
         * Constructor
         *
@@ -43,11 +48,6 @@ class DjVuImage {
                $this->mFilename = $filename;
        }
 
-       /**
-        * @const DJVUTXT_MEMORY_LIMIT Memory limit for the DjVu description software
-        */
-       const DJVUTXT_MEMORY_LIMIT = 300000;
-
        /**
         * Check if the given file is indeed a valid DjVu image file
         * @return bool
@@ -84,8 +84,11 @@ class DjVuImage {
        function dump() {
                $file = fopen( $this->mFilename, 'rb' );
                $header = fread( $file, 12 );
-               // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
+               // @todo FIXME: Would be good to replace this extract() call with
+               // something that explicitly initializes local variables.
                extract( unpack( 'a4magic/a4chunk/NchunkLength', $header ) );
+               /** @var string $chunk
+                *  @var string $chunkLength */
                echo "$chunk $chunkLength\n";
                $this->dumpForm( $file, $chunkLength, 1 );
                fclose( $file );
@@ -100,8 +103,11 @@ class DjVuImage {
                        if ( $chunkHeader == '' ) {
                                break;
                        }
-                       // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
+                       // @todo FIXME: Would be good to replace this extract() call with
+                       // something that explicitly initializes local variables.
                        extract( unpack( 'a4chunk/NchunkLength', $chunkHeader ) );
+                       /** @var string $chunk
+                        *  @var string $chunkLength */
                        echo str_repeat( ' ', $indent * 4 ) . "$chunk $chunkLength\n";
 
                        if ( $chunk == 'FORM' ) {
@@ -132,9 +138,14 @@ class DjVuImage {
                if ( strlen( $header ) < 16 ) {
                        wfDebug( __METHOD__ . ": too short file header\n" );
                } else {
-                       // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
+                       // @todo FIXME: Would be good to replace this extract() call with
+                       // something that explicitly initializes local variables.
                        extract( unpack( 'a4magic/a4form/NformLength/a4subtype', $header ) );
 
+                       /** @var string $magic
+                        *  @var string $subtype
+                        *  @var string $formLength
+                        *  @var string $formType */
                        if ( $magic != 'AT&T' ) {
                                wfDebug( __METHOD__ . ": not a DjVu file\n" );
                        } elseif ( $subtype == 'DJVU' ) {
@@ -157,9 +168,12 @@ class DjVuImage {
                if ( strlen( $header ) < 8 ) {
                        return array( false, 0 );
                } else {
-                       // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
+                       // @todo FIXME: Would be good to replace this extract() call with
+                       // something that explicitly initializes local variables.
                        extract( unpack( 'a4chunk/Nlength', $header ) );
 
+                       /** @var string $chunk
+                        *  @var string $length */
                        return array( $chunk, $length );
                }
        }
@@ -222,7 +236,8 @@ class DjVuImage {
                        return false;
                }
 
-               // @todo FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
+               // @todo FIXME: Would be good to replace this extract() call with
+               // something that explicitly initializes local variables.
                extract( unpack(
                        'nwidth/' .
                        'nheight/' .
@@ -233,6 +248,13 @@ class DjVuImage {
 
                # Newer files have rotation info in byte 10, but we don't use it yet.
 
+               /** @var string $width
+                *  @var string $height
+                *  @var string $major
+                *  @var string $minor
+                *  @var string $resolution
+                *  @var string $length
+                *  @var string $gamma */
                return array(
                        'width' => $width,
                        'height' => $height,
@@ -301,12 +323,15 @@ EOR;
 
        function pageTextCallback( $matches ) {
                # Get rid of invalid UTF-8, strip control characters
-               return '<PAGE value="' . htmlspecialchars( UtfNormal::cleanUp( $matches[1] ) ) . '" />';
+               $val = htmlspecialchars( UtfNormal::cleanUp( stripcslashes( $matches[1] ) ) );
+               $val = str_replace( array( "\n", '�' ), array( '&#10;', '' ), $val );
+               return '<PAGE value="' . $val . '" />';
        }
 
        /**
         * Hack to temporarily work around djvutoxml bug
-        * @return bool|string
+        * @param $dump
+        * @return string
         */
        function convertDumpToXML( $dump ) {
                if ( strval( $dump ) == '' ) {
@@ -381,7 +406,11 @@ EOT;
                                break;
                        }
 
-                       if ( preg_match( '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/', $line, $m ) ) {
+                       if ( preg_match(
+                               '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/',
+                               $line,
+                               $m
+                       ) ) {
                                $xml .= Xml::tags(
                                        'OBJECT',
                                        array(