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