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