Swap else if for elseif
[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 return new MediaTransformError( 'thumbnail_error', @$params['width'], @$params['height'],
124 wfMsg( 'djvu_no_xml' ) );
125 }
126
127 if ( !$this->normaliseParams( $image, $params ) ) {
128 return new TransformParameterError( $params );
129 }
130 $width = $params['width'];
131 $height = $params['height'];
132 $srcPath = $image->getPath();
133 $page = $params['page'];
134 if ( $page > $this->pageCount( $image ) ) {
135 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
136 }
137
138 if ( $flags & self::TRANSFORM_LATER ) {
139 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
140 }
141
142 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
143 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
144 }
145
146 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
147 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
148 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page} -size={$width}x{$height} " .
149 wfEscapeShellArg( $srcPath );
150 if ( $wgDjvuPostProcessor ) {
151 $cmd .= " | {$wgDjvuPostProcessor}";
152 }
153 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
154 wfProfileIn( 'ddjvu' );
155 wfDebug( __METHOD__.": $cmd\n" );
156 $retval = '';
157 $err = wfShellExec( $cmd, $retval );
158 wfProfileOut( 'ddjvu' );
159
160 $removed = $this->removeBadFile( $dstPath, $retval );
161 if ( $retval != 0 || $removed ) {
162 wfDebugLog( 'thumbnail',
163 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
164 wfHostname(), $retval, trim($err), $cmd ) );
165 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
166 } else {
167 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
168 }
169 }
170
171 /**
172 * Cache an instance of DjVuImage in an Image object, return that instance
173 *
174 * @return DjVuImage
175 */
176 function getDjVuImage( $image, $path ) {
177 if ( !$image ) {
178 $deja = new DjVuImage( $path );
179 } elseif ( !isset( $image->dejaImage ) ) {
180 $deja = $image->dejaImage = new DjVuImage( $path );
181 } else {
182 $deja = $image->dejaImage;
183 }
184 return $deja;
185 }
186
187 /**
188 * Cache a document tree for the DjVu XML metadata
189 * @param $image File
190 */
191 function getMetaTree( $image , $gettext = false ) {
192 if ( isset( $image->dejaMetaTree ) ) {
193 return $image->dejaMetaTree;
194 }
195
196 $metadata = $image->getMetadata();
197 if ( !$this->isMetadataValid( $image, $metadata ) ) {
198 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
199 return false;
200 }
201 wfProfileIn( __METHOD__ );
202
203 wfSuppressWarnings();
204 try {
205 // Set to false rather than null to avoid further attempts
206 $image->dejaMetaTree = false;
207 $image->djvuTextTree = false;
208 $tree = new SimpleXMLElement( $metadata );
209 if( $tree->getName() == 'mw-djvu' ) {
210 foreach($tree->children() as $b){
211 if( $b->getName() == 'DjVuTxt' ) {
212 $image->djvuTextTree = $b;
213 }
214 elseif ( $b->getName() == 'DjVuXML' ) {
215 $image->dejaMetaTree = $b;
216 }
217 }
218 } else {
219 $image->dejaMetaTree = $tree;
220 }
221 } catch( Exception $e ) {
222 wfDebug( "Bogus multipage XML metadata on '$image->name'\n" );
223 }
224 wfRestoreWarnings();
225 wfProfileOut( __METHOD__ );
226 if( $gettext ) {
227 return $image->djvuTextTree;
228 } else {
229 return $image->dejaMetaTree;
230 }
231 }
232
233 function getImageSize( $image, $path ) {
234 return $this->getDjVuImage( $image, $path )->getImageSize();
235 }
236
237 function getThumbType( $ext, $mime, $params = null ) {
238 global $wgDjvuOutputExtension;
239 static $mime;
240 if ( !isset( $mime ) ) {
241 $magic = MimeMagic::singleton();
242 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
243 }
244 return array( $wgDjvuOutputExtension, $mime );
245 }
246
247 function getMetadata( $image, $path ) {
248 wfDebug( "Getting DjVu metadata for $path\n" );
249 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
250 }
251
252 function getMetadataType( $image ) {
253 return 'djvuxml';
254 }
255
256 function isMetadataValid( $image, $metadata ) {
257 return !empty( $metadata ) && $metadata != serialize(array());
258 }
259
260 function pageCount( $image ) {
261 $tree = $this->getMetaTree( $image );
262 if ( !$tree ) {
263 return false;
264 }
265 return count( $tree->xpath( '//OBJECT' ) );
266 }
267
268 function getPageDimensions( $image, $page ) {
269 $tree = $this->getMetaTree( $image );
270 if ( !$tree ) {
271 return false;
272 }
273
274 $o = $tree->BODY[0]->OBJECT[$page-1];
275 if ( $o ) {
276 return array(
277 'width' => intval( $o['width'] ),
278 'height' => intval( $o['height'] )
279 );
280 } else {
281 return false;
282 }
283 }
284
285 function getPageText( $image, $page ){
286 $tree = $this->getMetaTree( $image, true );
287 if ( !$tree ) {
288 return false;
289 }
290
291 $o = $tree->BODY[0]->PAGE[$page-1];
292 if ( $o ) {
293 $txt = $o['value'];
294 return $txt;
295 } else {
296 return false;
297 }
298
299 }
300
301 }