store djvu text layer in img_metadata. fetch it in proofreadpage
[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 , $gettext = false ) {
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 // Set to false rather than null to avoid further attempts
153 $image->dejaMetaTree = false;
154 $image->djvuTextTree = false;
155 $tree = new SimpleXMLElement( $metadata );
156 if( $tree->getName() == 'mw-djvu' ) {
157 foreach($tree->children() as $b){
158 if( $b->getName() == 'DjVuTxt' ) {
159 $image->djvuTextTree = $b;
160 }
161 else if ( $b->getName() == 'DjVuXML' ) {
162 $image->dejaMetaTree = $b;
163 }
164 }
165 } else {
166 $image->dejaMetaTree = $tree;
167 }
168 } catch( Exception $e ) {
169 wfDebug( "Bogus multipage XML metadata on '$image->name'\n" );
170 }
171 wfRestoreWarnings();
172 wfProfileOut( __METHOD__ );
173 if( $gettext ) {
174 return $image->djvuTextTree;
175 } else {
176 return $image->dejaMetaTree;
177 }
178 }
179
180 function getImageSize( $image, $path ) {
181 return $this->getDjVuImage( $image, $path )->getImageSize();
182 }
183
184 function getThumbType( $ext, $mime ) {
185 global $wgDjvuOutputExtension;
186 static $mime;
187 if ( !isset( $mime ) ) {
188 $magic = MimeMagic::singleton();
189 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
190 }
191 return array( $wgDjvuOutputExtension, $mime );
192 }
193
194 function getMetadata( $image, $path ) {
195 wfDebug( "Getting DjVu metadata for $path\n" );
196 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
197 }
198
199 function getMetadataType( $image ) {
200 return 'djvuxml';
201 }
202
203 function isMetadataValid( $image, $metadata ) {
204 return !empty( $metadata ) && $metadata != serialize(array());
205 }
206
207 function pageCount( $image ) {
208 $tree = $this->getMetaTree( $image );
209 if ( !$tree ) {
210 return false;
211 }
212 return count( $tree->xpath( '//OBJECT' ) );
213 }
214
215 function getPageDimensions( $image, $page ) {
216 $tree = $this->getMetaTree( $image );
217 if ( !$tree ) {
218 return false;
219 }
220
221 $o = $tree->BODY[0]->OBJECT[$page-1];
222 if ( $o ) {
223 return array(
224 'width' => intval( $o['width'] ),
225 'height' => intval( $o['height'] )
226 );
227 } else {
228 return false;
229 }
230 }
231
232 function getPageText( $image, $page ){
233 $tree = $this->getMetaTree( $image, true );
234 if ( !$tree ) {
235 return false;
236 }
237
238 $o = $tree->BODY[0]->PAGE[$page-1];
239 if ( $o ) {
240 $txt = $o['value'];
241 return $txt;
242 } else {
243 return false;
244 }
245
246 }
247
248 }