2006-06-24 07:11:17 +00:00
|
|
|
<?php
|
2007-04-04 05:22:37 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2006 Brion Vibber <brion@pobox.com>
|
|
|
|
|
* http://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
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2007-04-04 05:22:37 +00:00
|
|
|
/**
|
|
|
|
|
* Support for detecting/validating DjVu image files and getting
|
|
|
|
|
* some basic file metadata (resolution etc)
|
|
|
|
|
*
|
|
|
|
|
* File format docs are available in source package for DjVuLibre:
|
|
|
|
|
* http://djvulibre.djvuzone.org/
|
2007-04-24 06:53:31 +00:00
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Media
|
2007-04-04 05:22:37 +00:00
|
|
|
*/
|
2006-06-24 07:11:17 +00:00
|
|
|
class DjVuImage {
|
|
|
|
|
function __construct( $filename ) {
|
|
|
|
|
$this->mFilename = $filename;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
/**
|
|
|
|
|
* Check if the given file is indeed a valid DjVu image file
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isValid() {
|
|
|
|
|
$info = $this->getInfo();
|
|
|
|
|
return $info !== false;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
|
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
/**
|
|
|
|
|
* Return data in the style of getimagesize()
|
|
|
|
|
* @return array or false on failure
|
|
|
|
|
*/
|
|
|
|
|
public function getImageSize() {
|
|
|
|
|
$data = $this->getInfo();
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
if( $data !== false ) {
|
|
|
|
|
$width = $data['width'];
|
|
|
|
|
$height = $data['height'];
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
return array( $width, $height, 'DjVu',
|
|
|
|
|
"width=\"$width\" height=\"$height\"" );
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
// ---------
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
/**
|
|
|
|
|
* For debugging; dump the IFF chunk structure
|
|
|
|
|
*/
|
|
|
|
|
function dump() {
|
|
|
|
|
$file = fopen( $this->mFilename, 'rb' );
|
|
|
|
|
$header = fread( $file, 12 );
|
2007-01-12 10:03:51 +00:00
|
|
|
// FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
|
2006-06-24 07:11:17 +00:00
|
|
|
extract( unpack( 'a4magic/a4chunk/NchunkLength', $header ) );
|
|
|
|
|
echo "$chunk $chunkLength\n";
|
|
|
|
|
$this->dumpForm( $file, $chunkLength, 1 );
|
|
|
|
|
fclose( $file );
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
private function dumpForm( $file, $length, $indent ) {
|
|
|
|
|
$start = ftell( $file );
|
|
|
|
|
$secondary = fread( $file, 4 );
|
|
|
|
|
echo str_repeat( ' ', $indent * 4 ) . "($secondary)\n";
|
|
|
|
|
while( ftell( $file ) - $start < $length ) {
|
|
|
|
|
$chunkHeader = fread( $file, 8 );
|
|
|
|
|
if( $chunkHeader == '' ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-01-12 10:03:51 +00:00
|
|
|
// FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
|
2006-06-24 07:11:17 +00:00
|
|
|
extract( unpack( 'a4chunk/NchunkLength', $chunkHeader ) );
|
|
|
|
|
echo str_repeat( ' ', $indent * 4 ) . "$chunk $chunkLength\n";
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
if( $chunk == 'FORM' ) {
|
|
|
|
|
$this->dumpForm( $file, $chunkLength, $indent + 1 );
|
|
|
|
|
} else {
|
|
|
|
|
fseek( $file, $chunkLength, SEEK_CUR );
|
|
|
|
|
if( $chunkLength & 1 == 1 ) {
|
|
|
|
|
// Padding byte between chunks
|
|
|
|
|
fseek( $file, 1, SEEK_CUR );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
function getInfo() {
|
2007-08-15 10:50:59 +00:00
|
|
|
wfSuppressWarnings();
|
2006-06-24 07:11:17 +00:00
|
|
|
$file = fopen( $this->mFilename, 'rb' );
|
2007-08-15 10:50:59 +00:00
|
|
|
wfRestoreWarnings();
|
2006-06-24 07:11:17 +00:00
|
|
|
if( $file === false ) {
|
|
|
|
|
wfDebug( __METHOD__ . ": missing or failed file read\n" );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
$header = fread( $file, 16 );
|
|
|
|
|
$info = false;
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
if( strlen( $header ) < 16 ) {
|
|
|
|
|
wfDebug( __METHOD__ . ": too short file header\n" );
|
|
|
|
|
} else {
|
2007-01-12 10:03:51 +00:00
|
|
|
// FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
|
2006-06-24 07:11:17 +00:00
|
|
|
extract( unpack( 'a4magic/a4form/NformLength/a4subtype', $header ) );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
if( $magic != 'AT&T' ) {
|
|
|
|
|
wfDebug( __METHOD__ . ": not a DjVu file\n" );
|
|
|
|
|
} elseif( $subtype == 'DJVU' ) {
|
|
|
|
|
// Single-page document
|
|
|
|
|
$info = $this->getPageInfo( $file, $formLength );
|
|
|
|
|
} elseif( $subtype == 'DJVM' ) {
|
|
|
|
|
// Multi-page document
|
|
|
|
|
$info = $this->getMultiPageInfo( $file, $formLength );
|
|
|
|
|
} else {
|
|
|
|
|
wfDebug( __METHOD__ . ": unrecognized DJVU file type '$formType'\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fclose( $file );
|
|
|
|
|
return $info;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
private function readChunk( $file ) {
|
|
|
|
|
$header = fread( $file, 8 );
|
|
|
|
|
if( strlen( $header ) < 8 ) {
|
|
|
|
|
return array( false, 0 );
|
|
|
|
|
} else {
|
2007-01-12 10:03:51 +00:00
|
|
|
// FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
|
2006-06-24 07:11:17 +00:00
|
|
|
extract( unpack( 'a4chunk/Nlength', $header ) );
|
|
|
|
|
return array( $chunk, $length );
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
private function skipChunk( $file, $chunkLength ) {
|
|
|
|
|
fseek( $file, $chunkLength, SEEK_CUR );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
if( $chunkLength & 0x01 == 1 && !feof( $file ) ) {
|
|
|
|
|
// padding byte
|
|
|
|
|
fseek( $file, 1, SEEK_CUR );
|
|
|
|
|
}
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
private function getMultiPageInfo( $file, $formLength ) {
|
|
|
|
|
// For now, we'll just look for the first page in the file
|
|
|
|
|
// and report its information, hoping others are the same size.
|
|
|
|
|
$start = ftell( $file );
|
|
|
|
|
do {
|
|
|
|
|
list( $chunk, $length ) = $this->readChunk( $file );
|
|
|
|
|
if( !$chunk ) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
if( $chunk == 'FORM' ) {
|
|
|
|
|
$subtype = fread( $file, 4 );
|
|
|
|
|
if( $subtype == 'DJVU' ) {
|
|
|
|
|
wfDebug( __METHOD__ . ": found first subpage\n" );
|
|
|
|
|
return $this->getPageInfo( $file, $length );
|
|
|
|
|
}
|
|
|
|
|
$this->skipChunk( $file, $length - 4 );
|
|
|
|
|
} else {
|
|
|
|
|
wfDebug( __METHOD__ . ": skipping '$chunk' chunk\n" );
|
|
|
|
|
$this->skipChunk( $file, $length );
|
|
|
|
|
}
|
|
|
|
|
} while( $length != 0 && !feof( $file ) && ftell( $file ) - $start < $formLength );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
wfDebug( __METHOD__ . ": multi-page DJVU file contained no pages\n" );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
private function getPageInfo( $file, $formLength ) {
|
|
|
|
|
list( $chunk, $length ) = $this->readChunk( $file );
|
|
|
|
|
if( $chunk != 'INFO' ) {
|
|
|
|
|
wfDebug( __METHOD__ . ": expected INFO chunk, got '$chunk'\n" );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
if( $length < 9 ) {
|
|
|
|
|
wfDebug( __METHOD__ . ": INFO should be 9 or 10 bytes, found $length\n" );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
$data = fread( $file, $length );
|
|
|
|
|
if( strlen( $data ) < $length ) {
|
|
|
|
|
wfDebug( __METHOD__ . ": INFO chunk cut off\n" );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-01-12 10:03:51 +00:00
|
|
|
// FIXME: Would be good to replace this extract() call with something that explicitly initializes local variables.
|
2006-06-24 07:11:17 +00:00
|
|
|
extract( unpack(
|
|
|
|
|
'nwidth/' .
|
|
|
|
|
'nheight/' .
|
|
|
|
|
'Cminor/' .
|
|
|
|
|
'Cmajor/' .
|
|
|
|
|
'vresolution/' .
|
|
|
|
|
'Cgamma', $data ) );
|
|
|
|
|
# Newer files have rotation info in byte 10, but we don't use it yet.
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2006-06-24 07:11:17 +00:00
|
|
|
return array(
|
|
|
|
|
'width' => $width,
|
|
|
|
|
'height' => $height,
|
|
|
|
|
'version' => "$major.$minor",
|
|
|
|
|
'resolution' => $resolution,
|
|
|
|
|
'gamma' => $gamma / 10.0 );
|
|
|
|
|
}
|
2006-08-13 17:34:48 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return an XML string describing the DjVu image
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
function retrieveMetaData() {
|
2009-06-04 09:16:25 +00:00
|
|
|
global $wgDjvuToXML, $wgDjvuDump, $wgDjvuTxt;
|
2007-04-20 12:31:36 +00:00
|
|
|
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
|
|
|
|
|
wfProfileIn( 'djvudump' );
|
|
|
|
|
$cmd = wfEscapeShellArg( $wgDjvuDump ) . ' ' . wfEscapeShellArg( $this->mFilename );
|
|
|
|
|
$dump = wfShellExec( $cmd );
|
|
|
|
|
$xml = $this->convertDumpToXML( $dump );
|
|
|
|
|
wfProfileOut( 'djvudump' );
|
|
|
|
|
} elseif ( isset( $wgDjvuToXML ) ) {
|
|
|
|
|
wfProfileIn( 'djvutoxml' );
|
2007-03-31 16:52:34 +00:00
|
|
|
$cmd = wfEscapeShellArg( $wgDjvuToXML ) . ' --without-anno --without-text ' .
|
2007-01-17 09:12:11 +00:00
|
|
|
wfEscapeShellArg( $this->mFilename );
|
2006-11-29 11:43:58 +00:00
|
|
|
$xml = wfShellExec( $cmd );
|
2007-04-20 12:31:36 +00:00
|
|
|
wfProfileOut( 'djvutoxml' );
|
2006-08-13 17:34:48 +00:00
|
|
|
} else {
|
|
|
|
|
$xml = null;
|
|
|
|
|
}
|
2009-06-04 09:16:25 +00:00
|
|
|
# Text layer
|
|
|
|
|
if ( isset( $wgDjvuTxt ) ) {
|
|
|
|
|
wfProfileIn( 'djvutxt' );
|
|
|
|
|
$cmd = wfEscapeShellArg( $wgDjvuTxt ) . ' --detail=page ' . wfEscapeShellArg( $this->mFilename ) ;
|
|
|
|
|
wfDebug( __METHOD__.": $cmd\n" );
|
|
|
|
|
$txt = wfShellExec( $cmd, $retval );
|
|
|
|
|
wfProfileOut( 'djvutxt' );
|
|
|
|
|
if( $retval == 0) {
|
2009-09-03 14:19:24 +00:00
|
|
|
# Get rid of invalid UTF-8, strip control characters
|
2010-01-19 17:23:29 +00:00
|
|
|
if( is_callable( 'iconv' ) ) {
|
|
|
|
|
wfSuppressWarnings();
|
|
|
|
|
$txt = iconv( "UTF-8","UTF-8//IGNORE", $txt );
|
|
|
|
|
wfRestoreWarnings();
|
|
|
|
|
} else {
|
|
|
|
|
$txt = UtfNormal::cleanUp( $txt );
|
|
|
|
|
}
|
2009-09-03 14:19:24 +00:00
|
|
|
$txt = preg_replace( "/[\013\035\037]/", "", $txt );
|
2009-06-04 09:16:25 +00:00
|
|
|
$txt = htmlspecialchars($txt);
|
2009-06-17 17:02:39 +00:00
|
|
|
$txt = preg_replace( "/\((page\s[\d-]*\s[\d-]*\s[\d-]*\s[\d-]*\s*\"([^<]*?)\"\s*|)\)/s", "<PAGE value=\"$2\" />", $txt );
|
2009-06-04 09:16:25 +00:00
|
|
|
$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>' ;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-08-13 17:34:48 +00:00
|
|
|
return $xml;
|
|
|
|
|
}
|
2007-04-20 12:31:36 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Hack to temporarily work around djvutoxml bug
|
|
|
|
|
*/
|
|
|
|
|
function convertDumpToXML( $dump ) {
|
|
|
|
|
if ( strval( $dump ) == '' ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$xml = <<<EOT
|
|
|
|
|
<?xml version="1.0" ?>
|
|
|
|
|
<!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
|
|
|
|
|
<DjVuXML>
|
|
|
|
|
<HEAD></HEAD>
|
|
|
|
|
<BODY>
|
|
|
|
|
EOT;
|
|
|
|
|
|
|
|
|
|
$dump = str_replace( "\r", '', $dump );
|
|
|
|
|
$line = strtok( $dump, "\n" );
|
|
|
|
|
$m = false;
|
|
|
|
|
$good = false;
|
|
|
|
|
if ( preg_match( '/^( *)FORM:DJVU/', $line, $m ) ) {
|
|
|
|
|
# Single-page
|
|
|
|
|
if ( $this->parseFormDjvu( $line, $xml ) ) {
|
|
|
|
|
$good = true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} elseif ( preg_match( '/^( *)FORM:DJVM/', $line, $m ) ) {
|
|
|
|
|
# Multi-page
|
|
|
|
|
$parentLevel = strlen( $m[1] );
|
|
|
|
|
# Find DIRM
|
|
|
|
|
$line = strtok( "\n" );
|
|
|
|
|
while ( $line !== false ) {
|
|
|
|
|
$childLevel = strspn( $line, ' ' );
|
|
|
|
|
if ( $childLevel <= $parentLevel ) {
|
|
|
|
|
# End of chunk
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( preg_match( '/^ *DIRM.*indirect/', $line ) ) {
|
|
|
|
|
wfDebug( "Indirect multi-page DjVu document, bad for server!\n" );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if ( preg_match( '/^ *FORM:DJVU/', $line ) ) {
|
|
|
|
|
# Found page
|
|
|
|
|
if ( $this->parseFormDjvu( $line, $xml ) ) {
|
|
|
|
|
$good = true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$line = strtok( "\n" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( !$good ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$xml .= "</BODY>\n</DjVuXML>\n";
|
|
|
|
|
return $xml;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parseFormDjvu( $line, &$xml ) {
|
|
|
|
|
$parentLevel = strspn( $line, ' ' );
|
|
|
|
|
$line = strtok( "\n" );
|
|
|
|
|
|
|
|
|
|
# Find INFO
|
|
|
|
|
while ( $line !== false ) {
|
|
|
|
|
$childLevel = strspn( $line, ' ' );
|
|
|
|
|
if ( $childLevel <= $parentLevel ) {
|
|
|
|
|
# End of chunk
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( preg_match( '/^ *INFO *\[\d*\] *DjVu *(\d+)x(\d+), *\w*, *(\d+) *dpi, *gamma=([0-9.-]+)/', $line, $m ) ) {
|
2008-04-14 07:45:50 +00:00
|
|
|
$xml .= Xml::tags( 'OBJECT',
|
2007-04-20 12:31:36 +00:00
|
|
|
array(
|
|
|
|
|
#'data' => '',
|
|
|
|
|
#'type' => 'image/x.djvu',
|
|
|
|
|
'height' => $m[2],
|
|
|
|
|
'width' => $m[1],
|
|
|
|
|
#'usemap' => '',
|
2008-04-14 07:45:50 +00:00
|
|
|
),
|
2007-04-20 12:31:36 +00:00
|
|
|
"\n" .
|
|
|
|
|
Xml::element( 'PARAM', array( 'name' => 'DPI', 'value' => $m[3] ) ) . "\n" .
|
|
|
|
|
Xml::element( 'PARAM', array( 'name' => 'GAMMA', 'value' => $m[4] ) ) . "\n"
|
|
|
|
|
) . "\n";
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
$line = strtok( "\n" );
|
|
|
|
|
}
|
|
|
|
|
# Not found
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2006-06-24 07:11:17 +00:00
|
|
|
}
|