Merged FileBackend branch. Manually avoiding merging the many prop-only changes SVN...
[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 */
194 function getMetaTree( $image , $gettext = false ) {
195 if ( isset( $image->dejaMetaTree ) ) {
196 return $image->dejaMetaTree;
197 }
198
199 $metadata = $image->getMetadata();
200 if ( !$this->isMetadataValid( $image, $metadata ) ) {
201 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
202 return false;
203 }
204 wfProfileIn( __METHOD__ );
205
206 wfSuppressWarnings();
207 try {
208 // Set to false rather than null to avoid further attempts
209 $image->dejaMetaTree = false;
210 $image->djvuTextTree = false;
211 $tree = new SimpleXMLElement( $metadata );
212 if( $tree->getName() == 'mw-djvu' ) {
213 foreach($tree->children() as $b){
214 if( $b->getName() == 'DjVuTxt' ) {
215 $image->djvuTextTree = $b;
216 }
217 elseif ( $b->getName() == 'DjVuXML' ) {
218 $image->dejaMetaTree = $b;
219 }
220 }
221 } else {
222 $image->dejaMetaTree = $tree;
223 }
224 } catch( Exception $e ) {
225 wfDebug( "Bogus multipage XML metadata on '{$image->getName()}'\n" );
226 }
227 wfRestoreWarnings();
228 wfProfileOut( __METHOD__ );
229 if( $gettext ) {
230 return $image->djvuTextTree;
231 } else {
232 return $image->dejaMetaTree;
233 }
234 }
235
236 function getImageSize( $image, $path ) {
237 return $this->getDjVuImage( $image, $path )->getImageSize();
238 }
239
240 function getThumbType( $ext, $mime, $params = null ) {
241 global $wgDjvuOutputExtension;
242 static $mime;
243 if ( !isset( $mime ) ) {
244 $magic = MimeMagic::singleton();
245 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
246 }
247 return array( $wgDjvuOutputExtension, $mime );
248 }
249
250 function getMetadata( $image, $path ) {
251 wfDebug( "Getting DjVu metadata for $path\n" );
252 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
253 }
254
255 function getMetadataType( $image ) {
256 return 'djvuxml';
257 }
258
259 function isMetadataValid( $image, $metadata ) {
260 return !empty( $metadata ) && $metadata != serialize(array());
261 }
262
263 function pageCount( $image ) {
264 $tree = $this->getMetaTree( $image );
265 if ( !$tree ) {
266 return false;
267 }
268 return count( $tree->xpath( '//OBJECT' ) );
269 }
270
271 function getPageDimensions( $image, $page ) {
272 $tree = $this->getMetaTree( $image );
273 if ( !$tree ) {
274 return false;
275 }
276
277 $o = $tree->BODY[0]->OBJECT[$page-1];
278 if ( $o ) {
279 return array(
280 'width' => intval( $o['width'] ),
281 'height' => intval( $o['height'] )
282 );
283 } else {
284 return false;
285 }
286 }
287
288 function getPageText( $image, $page ){
289 $tree = $this->getMetaTree( $image, true );
290 if ( !$tree ) {
291 return false;
292 }
293
294 $o = $tree->BODY[0]->PAGE[$page-1];
295 if ( $o ) {
296 $txt = $o['value'];
297 return $txt;
298 } else {
299 return false;
300 }
301
302 }
303
304 }