00001 <?php
00010 class DjVuHandler extends ImageHandler {
00011 function isEnabled() {
00012 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
00013 if ( !$wgDjvuRenderer || ( !$wgDjvuDump && !$wgDjvuToXML ) ) {
00014 wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
00015 return false;
00016 } else {
00017 return true;
00018 }
00019 }
00020
00021 function mustRender() { return true; }
00022 function isMultiPage() { return true; }
00023
00024 function getParamMap() {
00025 return array(
00026 'img_width' => 'width',
00027 'img_page' => 'page',
00028 );
00029 }
00030
00031 function validateParam( $name, $value ) {
00032 if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
00033 if ( $value <= 0 ) {
00034 return false;
00035 } else {
00036 return true;
00037 }
00038 } else {
00039 return false;
00040 }
00041 }
00042
00043 function makeParamString( $params ) {
00044 $page = isset( $params['page'] ) ? $params['page'] : 1;
00045 if ( !isset( $params['width'] ) ) {
00046 return false;
00047 }
00048 return "page{$page}-{$params['width']}px";
00049 }
00050
00051 function parseParamString( $str ) {
00052 $m = false;
00053 if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
00054 return array( 'width' => $m[2], 'page' => $m[1] );
00055 } else {
00056 return false;
00057 }
00058 }
00059
00060 function getScriptParams( $params ) {
00061 return array(
00062 'width' => $params['width'],
00063 'page' => $params['page'],
00064 );
00065 }
00066
00067 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
00068 global $wgDjvuRenderer, $wgDjvuPostProcessor;
00069
00070
00071
00072 $xml = $image->getMetadata();
00073 if ( !$xml ) {
00074 return new MediaTransformError( 'thumbnail_error', @$params['width'], @$params['height'],
00075 wfMsg( 'djvu_no_xml' ) );
00076 }
00077
00078 if ( !$this->normaliseParams( $image, $params ) ) {
00079 return new TransformParameterError( $params );
00080 }
00081 $width = $params['width'];
00082 $height = $params['height'];
00083 $srcPath = $image->getPath();
00084 $page = $params['page'];
00085 if ( $page > $this->pageCount( $image ) ) {
00086 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
00087 }
00088
00089 if ( $flags & self::TRANSFORM_LATER ) {
00090 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
00091 }
00092
00093 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
00094 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
00095 }
00096
00097 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
00098 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
00099 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page} -size={$width}x{$height} " .
00100 wfEscapeShellArg( $srcPath );
00101 if ( $wgDjvuPostProcessor ) {
00102 $cmd .= " | {$wgDjvuPostProcessor}";
00103 }
00104 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
00105 wfProfileIn( 'ddjvu' );
00106 wfDebug( __METHOD__.": $cmd\n" );
00107 $err = wfShellExec( $cmd, $retval );
00108 wfProfileOut( 'ddjvu' );
00109
00110 $removed = $this->removeBadFile( $dstPath, $retval );
00111 if ( $retval != 0 || $removed ) {
00112 wfDebugLog( 'thumbnail',
00113 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
00114 wfHostname(), $retval, trim($err), $cmd ) );
00115 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
00116 } else {
00117 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
00118 }
00119 }
00120
00124 function getDjVuImage( $image, $path ) {
00125 if ( !$image ) {
00126 $deja = new DjVuImage( $path );
00127 } elseif ( !isset( $image->dejaImage ) ) {
00128 $deja = $image->dejaImage = new DjVuImage( $path );
00129 } else {
00130 $deja = $image->dejaImage;
00131 }
00132 return $deja;
00133 }
00134
00138 function getMetaTree( $image ) {
00139 if ( isset( $image->dejaMetaTree ) ) {
00140 return $image->dejaMetaTree;
00141 }
00142
00143 $metadata = $image->getMetadata();
00144 if ( !$this->isMetadataValid( $image, $metadata ) ) {
00145 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
00146 return false;
00147 }
00148 wfProfileIn( __METHOD__ );
00149
00150 wfSuppressWarnings();
00151 try {
00152 $image->dejaMetaTree = new SimpleXMLElement( $metadata );
00153 } catch( Exception $e ) {
00154 wfDebug( "Bogus multipage XML metadata on '$image->name'\n" );
00155
00156 $image->dejaMetaTree = false;
00157 }
00158 wfRestoreWarnings();
00159 wfProfileOut( __METHOD__ );
00160 return $image->dejaMetaTree;
00161 }
00162
00163 function getImageSize( $image, $path ) {
00164 return $this->getDjVuImage( $image, $path )->getImageSize();
00165 }
00166
00167 function getThumbType( $ext, $mime ) {
00168 global $wgDjvuOutputExtension;
00169 static $mime;
00170 if ( !isset( $mime ) ) {
00171 $magic = MimeMagic::singleton();
00172 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
00173 }
00174 return array( $wgDjvuOutputExtension, $mime );
00175 }
00176
00177 function getMetadata( $image, $path ) {
00178 wfDebug( "Getting DjVu metadata for $path\n" );
00179 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
00180 }
00181
00182 function getMetadataType( $image ) {
00183 return 'djvuxml';
00184 }
00185
00186 function isMetadataValid( $image, $metadata ) {
00187 return !empty( $metadata ) && $metadata != serialize(array());
00188 }
00189
00190 function pageCount( $image ) {
00191 $tree = $this->getMetaTree( $image );
00192 if ( !$tree ) {
00193 return false;
00194 }
00195 return count( $tree->xpath( '//OBJECT' ) );
00196 }
00197
00198 function getPageDimensions( $image, $page ) {
00199 $tree = $this->getMetaTree( $image );
00200 if ( !$tree ) {
00201 return false;
00202 }
00203
00204 $o = $tree->BODY[0]->OBJECT[$page-1];
00205 if ( $o ) {
00206 return array(
00207 'width' => intval( $o['width'] ),
00208 'height' => intval( $o['height'] )
00209 );
00210 } else {
00211 return false;
00212 }
00213 }
00214 }