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