* Kill E_NOTICE
[lhc/web/wiklou.git] / includes / media / DjVu.php
1 <?php
2 /**
3 * @file
4 * @ingroup Media
5 */
6
7 /**
8 * @ingroup Media
9 */
10 class DjVuHandler extends ImageHandler {
11 function isEnabled() {
12 global $wgDjvuRenderer, $wgDjvuDump, $wgDjvuToXML;
13 if ( !$wgDjvuRenderer || ( !$wgDjvuDump && !$wgDjvuToXML ) ) {
14 wfDebug( "DjVu is disabled, please set \$wgDjvuRenderer and \$wgDjvuDump\n" );
15 return false;
16 } else {
17 return true;
18 }
19 }
20
21 function mustRender( $file ) { return true; }
22 function isMultiPage( $file ) { return true; }
23
24 function getParamMap() {
25 return array(
26 'img_width' => 'width',
27 'img_page' => 'page',
28 );
29 }
30
31 function validateParam( $name, $value ) {
32 if ( in_array( $name, array( 'width', 'height', 'page' ) ) ) {
33 if ( $value <= 0 ) {
34 return false;
35 } else {
36 return true;
37 }
38 } else {
39 return false;
40 }
41 }
42
43 function makeParamString( $params ) {
44 $page = isset( $params['page'] ) ? $params['page'] : 1;
45 if ( !isset( $params['width'] ) ) {
46 return false;
47 }
48 return "page{$page}-{$params['width']}px";
49 }
50
51 function parseParamString( $str ) {
52 $m = false;
53 if ( preg_match( '/^page(\d+)-(\d+)px$/', $str, $m ) ) {
54 return array( 'width' => $m[2], 'page' => $m[1] );
55 } else if ( preg_match( '/^page(\d+)-djvutxt$/', $str, $m ) ) {
56 return array( 'djvutxt' => 1, 'page' => $m[1] );
57 } else {
58 return false;
59 }
60 }
61
62 function getScriptParams( $params ) {
63 return array(
64 'width' => $params['width'],
65 'page' => $params['page'],
66 );
67 }
68
69
70 function normaliseParams( $image, &$params ) {
71 global $wgDjvuTxt;
72 if( $wgDjvuTxt && isset($params['djvutxt']) && $params['djvutxt']) {
73 if ( !isset( $params['page'] ) ) {
74 $params['page'] = 1;
75 }
76 $params['width'] = 0;
77 $params['height'] = 0;
78 return true;
79 }
80 else return parent::normaliseParams( $image, $params );
81 }
82
83 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
84 global $wgDjvuRenderer, $wgDjvuPostProcessor, $wgDjvuTxt, $wgSed;
85
86 // Fetch XML and check it, to give a more informative error message than the one which
87 // normaliseParams will inevitably give.
88 $xml = $image->getMetadata();
89 if ( !$xml ) {
90 return new MediaTransformError( 'thumbnail_error', @$params['width'], @$params['height'],
91 wfMsg( 'djvu_no_xml' ) );
92 }
93
94 if ( !$this->normaliseParams( $image, $params ) ) {
95 return new TransformParameterError( $params );
96 }
97 $width = $params['width'];
98 $height = $params['height'];
99 $srcPath = $image->getPath();
100 $page = $params['page'];
101 if ( $page > $this->pageCount( $image ) ) {
102 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
103 }
104
105 if ( $flags & self::TRANSFORM_LATER ) {
106 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
107 }
108
109 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
110 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
111 }
112
113 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
114 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
115
116 if( $params['djvutxt'] && $wgDjvuTxt && $wgSed ) {
117 #Read text from djvu
118 $cmd = '(' . wfEscapeShellArg( $wgDjvuTxt ) . " --page={$page} " . wfEscapeShellArg( $srcPath );
119 #Escape < > & characters
120 $cmd .= ' | ' . wfEscapeShellArg( $wgSed ) . ' "s/\&/\&amp;/g ; s/</\&lt;/g ; s/>/\&gt;/g ; s/\"/\&quot;/g "';
121 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
122 }
123 else {
124 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page} -size={$width}x{$height} " .
125 wfEscapeShellArg( $srcPath );
126 if ( $wgDjvuPostProcessor ) {
127 $cmd .= " | {$wgDjvuPostProcessor}";
128 }
129 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
130 }
131 wfProfileIn( 'ddjvu' );
132 wfDebug( __METHOD__.": $cmd\n" );
133 $err = wfShellExec( $cmd, $retval );
134 wfProfileOut( 'ddjvu' );
135
136 $removed = $this->removeBadFile( $dstPath, $retval );
137 if ( $retval != 0 || $removed ) {
138 wfDebugLog( 'thumbnail',
139 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
140 wfHostname(), $retval, trim($err), $cmd ) );
141 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
142 } else {
143 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
144 }
145 }
146
147 /**
148 * Cache an instance of DjVuImage in an Image object, return that instance
149 */
150 function getDjVuImage( $image, $path ) {
151 if ( !$image ) {
152 $deja = new DjVuImage( $path );
153 } elseif ( !isset( $image->dejaImage ) ) {
154 $deja = $image->dejaImage = new DjVuImage( $path );
155 } else {
156 $deja = $image->dejaImage;
157 }
158 return $deja;
159 }
160
161 /**
162 * Cache a document tree for the DjVu XML metadata
163 */
164 function getMetaTree( $image ) {
165 if ( isset( $image->dejaMetaTree ) ) {
166 return $image->dejaMetaTree;
167 }
168
169 $metadata = $image->getMetadata();
170 if ( !$this->isMetadataValid( $image, $metadata ) ) {
171 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
172 return false;
173 }
174 wfProfileIn( __METHOD__ );
175
176 wfSuppressWarnings();
177 try {
178 $image->dejaMetaTree = new SimpleXMLElement( $metadata );
179 } catch( Exception $e ) {
180 wfDebug( "Bogus multipage XML metadata on '$image->name'\n" );
181 // Set to false rather than null to avoid further attempts
182 $image->dejaMetaTree = false;
183 }
184 wfRestoreWarnings();
185 wfProfileOut( __METHOD__ );
186 return $image->dejaMetaTree;
187 }
188
189 function getImageSize( $image, $path ) {
190 return $this->getDjVuImage( $image, $path )->getImageSize();
191 }
192
193 function getThumbType( $ext, $mime ) {
194 global $wgDjvuOutputExtension;
195 static $mime;
196 if ( !isset( $mime ) ) {
197 $magic = MimeMagic::singleton();
198 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
199 }
200 return array( $wgDjvuOutputExtension, $mime );
201 }
202
203 function getMetadata( $image, $path ) {
204 wfDebug( "Getting DjVu metadata for $path\n" );
205 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
206 }
207
208 function getMetadataType( $image ) {
209 return 'djvuxml';
210 }
211
212 function isMetadataValid( $image, $metadata ) {
213 return !empty( $metadata ) && $metadata != serialize(array());
214 }
215
216 function pageCount( $image ) {
217 $tree = $this->getMetaTree( $image );
218 if ( !$tree ) {
219 return false;
220 }
221 return count( $tree->xpath( '//OBJECT' ) );
222 }
223
224 function getPageDimensions( $image, $page ) {
225 $tree = $this->getMetaTree( $image );
226 if ( !$tree ) {
227 return false;
228 }
229
230 $o = $tree->BODY[0]->OBJECT[$page-1];
231 if ( $o ) {
232 return array(
233 'width' => intval( $o['width'] ),
234 'height' => intval( $o['height'] )
235 );
236 } else {
237 return false;
238 }
239 }
240 }