Add some $retval = '' before some wfShellExec
[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 $retval = '';
112 $err = wfShellExec( $cmd, &$retval );
113 wfProfileOut( 'ddjvu' );
114
115 $removed = $this->removeBadFile( $dstPath, $retval );
116 if ( $retval != 0 || $removed ) {
117 wfDebugLog( 'thumbnail',
118 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
119 wfHostname(), $retval, trim($err), $cmd ) );
120 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
121 } else {
122 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
123 }
124 }
125
126 /**
127 * Cache an instance of DjVuImage in an Image object, return that instance
128 */
129 function getDjVuImage( $image, $path ) {
130 if ( !$image ) {
131 $deja = new DjVuImage( $path );
132 } elseif ( !isset( $image->dejaImage ) ) {
133 $deja = $image->dejaImage = new DjVuImage( $path );
134 } else {
135 $deja = $image->dejaImage;
136 }
137 return $deja;
138 }
139
140 /**
141 * Cache a document tree for the DjVu XML metadata
142 */
143 function getMetaTree( $image , $gettext = false ) {
144 if ( isset( $image->dejaMetaTree ) ) {
145 return $image->dejaMetaTree;
146 }
147
148 $metadata = $image->getMetadata();
149 if ( !$this->isMetadataValid( $image, $metadata ) ) {
150 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
151 return false;
152 }
153 wfProfileIn( __METHOD__ );
154
155 wfSuppressWarnings();
156 try {
157 // Set to false rather than null to avoid further attempts
158 $image->dejaMetaTree = false;
159 $image->djvuTextTree = false;
160 $tree = new SimpleXMLElement( $metadata );
161 if( $tree->getName() == 'mw-djvu' ) {
162 foreach($tree->children() as $b){
163 if( $b->getName() == 'DjVuTxt' ) {
164 $image->djvuTextTree = $b;
165 }
166 else if ( $b->getName() == 'DjVuXML' ) {
167 $image->dejaMetaTree = $b;
168 }
169 }
170 } else {
171 $image->dejaMetaTree = $tree;
172 }
173 } catch( Exception $e ) {
174 wfDebug( "Bogus multipage XML metadata on '$image->name'\n" );
175 }
176 wfRestoreWarnings();
177 wfProfileOut( __METHOD__ );
178 if( $gettext ) {
179 return $image->djvuTextTree;
180 } else {
181 return $image->dejaMetaTree;
182 }
183 }
184
185 function getImageSize( $image, $path ) {
186 return $this->getDjVuImage( $image, $path )->getImageSize();
187 }
188
189 function getThumbType( $ext, $mime, $params = null ) {
190 global $wgDjvuOutputExtension;
191 static $mime;
192 if ( !isset( $mime ) ) {
193 $magic = MimeMagic::singleton();
194 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
195 }
196 return array( $wgDjvuOutputExtension, $mime );
197 }
198
199 function getMetadata( $image, $path ) {
200 wfDebug( "Getting DjVu metadata for $path\n" );
201 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
202 }
203
204 function getMetadataType( $image ) {
205 return 'djvuxml';
206 }
207
208 function isMetadataValid( $image, $metadata ) {
209 return !empty( $metadata ) && $metadata != serialize(array());
210 }
211
212 function pageCount( $image ) {
213 $tree = $this->getMetaTree( $image );
214 if ( !$tree ) {
215 return false;
216 }
217 return count( $tree->xpath( '//OBJECT' ) );
218 }
219
220 function getPageDimensions( $image, $page ) {
221 $tree = $this->getMetaTree( $image );
222 if ( !$tree ) {
223 return false;
224 }
225
226 $o = $tree->BODY[0]->OBJECT[$page-1];
227 if ( $o ) {
228 return array(
229 'width' => intval( $o['width'] ),
230 'height' => intval( $o['height'] )
231 );
232 } else {
233 return false;
234 }
235 }
236
237 function getPageText( $image, $page ){
238 $tree = $this->getMetaTree( $image, true );
239 if ( !$tree ) {
240 return false;
241 }
242
243 $o = $tree->BODY[0]->PAGE[$page-1];
244 if ( $o ) {
245 $txt = $o['value'];
246 return $txt;
247 } else {
248 return false;
249 }
250
251 }
252
253 }