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