* Standardised file description headers
[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 function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
72 global $wgDjvuRenderer, $wgDjvuPostProcessor;
73
74 // Fetch XML and check it, to give a more informative error message than the one which
75 // normaliseParams will inevitably give.
76 $xml = $image->getMetadata();
77 if ( !$xml ) {
78 return new MediaTransformError( 'thumbnail_error', @$params['width'], @$params['height'],
79 wfMsg( 'djvu_no_xml' ) );
80 }
81
82 if ( !$this->normaliseParams( $image, $params ) ) {
83 return new TransformParameterError( $params );
84 }
85 $width = $params['width'];
86 $height = $params['height'];
87 $srcPath = $image->getPath();
88 $page = $params['page'];
89 if ( $page > $this->pageCount( $image ) ) {
90 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
91 }
92
93 if ( $flags & self::TRANSFORM_LATER ) {
94 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
95 }
96
97 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
98 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
99 }
100
101 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
102 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
103 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page} -size={$width}x{$height} " .
104 wfEscapeShellArg( $srcPath );
105 if ( $wgDjvuPostProcessor ) {
106 $cmd .= " | {$wgDjvuPostProcessor}";
107 }
108 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
109 wfProfileIn( 'ddjvu' );
110 wfDebug( __METHOD__.": $cmd\n" );
111 $err = wfShellExec( $cmd, $retval );
112 wfProfileOut( 'ddjvu' );
113
114 $removed = $this->removeBadFile( $dstPath, $retval );
115 if ( $retval != 0 || $removed ) {
116 wfDebugLog( 'thumbnail',
117 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
118 wfHostname(), $retval, trim($err), $cmd ) );
119 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
120 } else {
121 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
122 }
123 }
124
125 /**
126 * Cache an instance of DjVuImage in an Image object, return that instance
127 */
128 function getDjVuImage( $image, $path ) {
129 if ( !$image ) {
130 $deja = new DjVuImage( $path );
131 } elseif ( !isset( $image->dejaImage ) ) {
132 $deja = $image->dejaImage = new DjVuImage( $path );
133 } else {
134 $deja = $image->dejaImage;
135 }
136 return $deja;
137 }
138
139 /**
140 * Cache a document tree for the DjVu XML metadata
141 */
142 function getMetaTree( $image , $gettext = false ) {
143 if ( isset( $image->dejaMetaTree ) ) {
144 return $image->dejaMetaTree;
145 }
146
147 $metadata = $image->getMetadata();
148 if ( !$this->isMetadataValid( $image, $metadata ) ) {
149 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
150 return false;
151 }
152 wfProfileIn( __METHOD__ );
153
154 wfSuppressWarnings();
155 try {
156 // Set to false rather than null to avoid further attempts
157 $image->dejaMetaTree = false;
158 $image->djvuTextTree = false;
159 $tree = new SimpleXMLElement( $metadata );
160 if( $tree->getName() == 'mw-djvu' ) {
161 foreach($tree->children() as $b){
162 if( $b->getName() == 'DjVuTxt' ) {
163 $image->djvuTextTree = $b;
164 }
165 else if ( $b->getName() == 'DjVuXML' ) {
166 $image->dejaMetaTree = $b;
167 }
168 }
169 } else {
170 $image->dejaMetaTree = $tree;
171 }
172 } catch( Exception $e ) {
173 wfDebug( "Bogus multipage XML metadata on '$image->name'\n" );
174 }
175 wfRestoreWarnings();
176 wfProfileOut( __METHOD__ );
177 if( $gettext ) {
178 return $image->djvuTextTree;
179 } else {
180 return $image->dejaMetaTree;
181 }
182 }
183
184 function getImageSize( $image, $path ) {
185 return $this->getDjVuImage( $image, $path )->getImageSize();
186 }
187
188 function getThumbType( $ext, $mime, $params = null ) {
189 global $wgDjvuOutputExtension;
190 static $mime;
191 if ( !isset( $mime ) ) {
192 $magic = MimeMagic::singleton();
193 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
194 }
195 return array( $wgDjvuOutputExtension, $mime );
196 }
197
198 function getMetadata( $image, $path ) {
199 wfDebug( "Getting DjVu metadata for $path\n" );
200 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
201 }
202
203 function getMetadataType( $image ) {
204 return 'djvuxml';
205 }
206
207 function isMetadataValid( $image, $metadata ) {
208 return !empty( $metadata ) && $metadata != serialize(array());
209 }
210
211 function pageCount( $image ) {
212 $tree = $this->getMetaTree( $image );
213 if ( !$tree ) {
214 return false;
215 }
216 return count( $tree->xpath( '//OBJECT' ) );
217 }
218
219 function getPageDimensions( $image, $page ) {
220 $tree = $this->getMetaTree( $image );
221 if ( !$tree ) {
222 return false;
223 }
224
225 $o = $tree->BODY[0]->OBJECT[$page-1];
226 if ( $o ) {
227 return array(
228 'width' => intval( $o['width'] ),
229 'height' => intval( $o['height'] )
230 );
231 } else {
232 return false;
233 }
234 }
235
236 function getPageText( $image, $page ){
237 $tree = $this->getMetaTree( $image, true );
238 if ( !$tree ) {
239 return false;
240 }
241
242 $o = $tree->BODY[0]->PAGE[$page-1];
243 if ( $o ) {
244 $txt = $o['value'];
245 return $txt;
246 } else {
247 return false;
248 }
249
250 }
251
252 }