Fix variable name in doc comment added in r78192
[lhc/web/wiklou.git] / includes / DjVuImage.php
index 5086f99..a55017e 100644 (file)
@@ -1,8 +1,8 @@
 <?php
-
 /**
+ * DjVu image handler
  *
- * Copyright (C) 2006 Brion Vibber <brion@pobox.com>
+ * Copyright © 2006 Brion Vibber <brion@pobox.com>
  * http://www.mediawiki.org/
  *
  * This program is free software; you can redistribute it and/or modify
@@ -20,6 +20,7 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
  *
+ * @file
  */
 
 /**
@@ -29,7 +30,7 @@
  * File format docs are available in source package for DjVuLibre:
  * http://djvulibre.djvuzone.org/
  *
- * @addtogroup Media
+ * @ingroup Media
  */
 class DjVuImage {
        function __construct( $filename ) {
@@ -224,7 +225,7 @@ class DjVuImage {
         * @return string
         */
        function retrieveMetaData() {
-               global $wgDjvuToXML, $wgDjvuDump;
+               global $wgDjvuToXML, $wgDjvuDump, $wgDjvuTxt;
                if ( isset( $wgDjvuDump ) ) {
                        # djvudump is faster as of version 3.5
                        # http://sourceforge.net/tracker/index.php?func=detail&aid=1704049&group_id=32953&atid=406583
@@ -242,9 +243,42 @@ class DjVuImage {
                } else {
                        $xml = null;
                }
+               # Text layer
+               if ( isset( $wgDjvuTxt ) ) { 
+                       wfProfileIn( 'djvutxt' );
+                       $cmd = wfEscapeShellArg( $wgDjvuTxt ) . ' --detail=page ' . wfEscapeShellArg( $this->mFilename ) ;
+                       wfDebug( __METHOD__.": $cmd\n" );
+                       $retval = '';
+                       $txt = wfShellExec( $cmd, $retval );
+                       wfProfileOut( 'djvutxt' );
+                       if( $retval == 0) {
+                               # Get rid of invalid UTF-8, strip control characters
+                               $txt = UtfNormal::cleanUp( $txt );
+                               $txt = preg_replace( "/[\013\035\037]/", "", $txt );
+                               $reg = <<<EOR
+                                       /\(page\s[\d-]*\s[\d-]*\s[\d-]*\s[\d-]*\s*"
+                                       ((?>    # Text to match is composed of atoms of either:
+                                         \\\\. # - any escaped character 
+                                         |     # - any character different from " and \
+                                         [^"\\\\]+
+                                       )*?)
+                                       "\s*\)
+                                       | # Or page can be empty ; in this case, djvutxt dumps ()
+                                       \(\s*()\)/sx
+EOR;
+                               $txt = preg_replace_callback( $reg, array( $this, 'pageTextCallback' ), $txt );
+                               $txt = "<DjVuTxt>\n<HEAD></HEAD>\n<BODY>\n" . $txt . "</BODY>\n</DjVuTxt>\n";
+                               $xml = preg_replace( "/<DjVuXML>/", "<mw-djvu><DjVuXML>", $xml );
+                               $xml = $xml . $txt. '</mw-djvu>' ;
+                       }
+               }
                return $xml;
        }
 
+       function pageTextCallback( $matches ) {
+               return '<PAGE value="' . htmlspecialchars( $matches[1] ) . '" />';
+       }
+
        /**
         * Hack to temporarily work around djvutoxml bug
         */