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