r32045 committed from wrong working branch. Revert and commit the one I wanted.
[mediawiki.git] / includes / media / DjVu.php
blob20e59d18b80287faadcd5e3f3b3668ab534c1149
1 <?php
3 /**
4 * @addtogroup Media
5 */
6 class DjVuHandler extends ImageHandler {
7 function isEnabled() {
8 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
9 if ( !$wgDjvuRenderer || ( !$wgDjvuDump && !$wgDjvuToXML ) ) {
10 wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
11 return false;
12 } else {
13 return true;
17 function mustRender() { return true; }
18 function isMultiPage() { return true; }
20 function getParamMap() {
21 return array(
22 'img_width' => 'width',
23 'img_page' => 'page',
27 function validateParam( $name, $value ) {
28 if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
29 if ( $value <= 0 ) {
30 return false;
31 } else {
32 return true;
34 } else {
35 return false;
39 function makeParamString( $params ) {
40 $page = isset( $params['page'] ) ? $params['page'] : 1;
41 if ( !isset( $params['width'] ) ) {
42 return false;
44 return "page{$page}-{$params['width']}px";
47 function parseParamString( $str ) {
48 $m = false;
49 if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
50 return array( 'width' => $m[2], 'page' => $m[1] );
51 } else {
52 return false;
56 function getScriptParams( $params ) {
57 return array(
58 'width' => $params['width'],
59 'page' => $params['page'],
63 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
64 global $wgDjvuRenderer, $wgDjvuPostProcessor;
66 // Fetch XML and check it, to give a more informative error message than the one which
67 // normaliseParams will inevitably give.
68 $xml = $image->getMetadata();
69 if ( !$xml ) {
70 return new MediaTransformError( 'thumbnail_error', @$params['width'], @$params['height'],
71 wfMsg( 'djvu_no_xml' ) );
74 if ( !$this->normaliseParams( $image, $params ) ) {
75 return new TransformParameterError( $params );
77 $width = $params['width'];
78 $height = $params['height'];
79 $srcPath = $image->getPath();
80 $page = $params['page'];
81 if ( $page > $this->pageCount( $image ) ) {
82 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
85 if ( $flags & self::TRANSFORM_LATER ) {
86 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
89 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
90 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
93 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
94 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
95 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page} -size={$width}x{$height} " .
96 wfEscapeShellArg( $srcPath );
97 if ( $wgDjvuPostProcessor ) {
98 $cmd .= " | {$wgDjvuPostProcessor}";
100 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
101 wfProfileIn( 'ddjvu' );
102 wfDebug( __METHOD__.": $cmd\n" );
103 $err = wfShellExec( $cmd, $retval );
104 wfProfileOut( 'ddjvu' );
106 $removed = $this->removeBadFile( $dstPath, $retval );
107 if ( $retval != 0 || $removed ) {
108 wfDebugLog( 'thumbnail',
109 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
110 wfHostname(), $retval, trim($err), $cmd ) );
111 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
112 } else {
113 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
118 * Cache an instance of DjVuImage in an Image object, return that instance
120 function getDjVuImage( $image, $path ) {
121 if ( !$image ) {
122 $deja = new DjVuImage( $path );
123 } elseif ( !isset( $image->dejaImage ) ) {
124 $deja = $image->dejaImage = new DjVuImage( $path );
125 } else {
126 $deja = $image->dejaImage;
128 return $deja;
132 * Cache a document tree for the DjVu XML metadata
134 function getMetaTree( $image ) {
135 if ( isset( $image->dejaMetaTree ) ) {
136 return $image->dejaMetaTree;
139 $metadata = $image->getMetadata();
140 if ( !$this->isMetadataValid( $image, $metadata ) ) {
141 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
142 return false;
144 wfProfileIn( __METHOD__ );
146 wfSuppressWarnings();
147 try {
148 $image->dejaMetaTree = new SimpleXMLElement( $metadata );
149 } catch( Exception $e ) {
150 wfDebug( "Bogus multipage XML metadata on '$image->name'\n" );
151 // Set to false rather than null to avoid further attempts
152 $image->dejaMetaTree = false;
154 wfRestoreWarnings();
155 wfProfileOut( __METHOD__ );
156 return $image->dejaMetaTree;
159 function getImageSize( $image, $path ) {
160 return $this->getDjVuImage( $image, $path )->getImageSize();
163 function getThumbType( $ext, $mime ) {
164 global $wgDjvuOutputExtension;
165 static $mime;
166 if ( !isset( $mime ) ) {
167 $magic = MimeMagic::singleton();
168 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
170 return array( $wgDjvuOutputExtension, $mime );
173 function getMetadata( $image, $path ) {
174 wfDebug( "Getting DjVu metadata for $path\n" );
175 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
178 function getMetadataType( $image ) {
179 return 'djvuxml';
182 function isMetadataValid( $image, $metadata ) {
183 return !empty( $metadata ) && $metadata != serialize(array());
186 function pageCount( $image ) {
187 $tree = $this->getMetaTree( $image );
188 if ( !$tree ) {
189 return false;
191 return count( $tree->xpath( '//OBJECT' ) );
194 function getPageDimensions( $image, $page ) {
195 $tree = $this->getMetaTree( $image );
196 if ( !$tree ) {
197 return false;
200 $o = $tree->BODY[0]->OBJECT[$page-1];
201 if ( $o ) {
202 return array(
203 'width' => intval( $o['width'] ),
204 'height' => intval( $o['height'] )
206 } else {
207 return false;