Doc tweaks:
[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 "{$params['width']}px-page{$page}";
38 }
39
40 function parseParamString( $str ) {
41 $m = false;
42 if ( preg_match( '/^(\d+)px-page(\d+)$/', $str, $m ) ) {
43 return array( 'width' => $m[1], 'page' => $m[2] );
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->getImagePath();
73 $page = $params['page'];
74 $pageCount = $this->pageCount( $image );
75 if ( $page > $this->pageCount( $image ) ) {
76 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'djvu_page_error' ) );
77 }
78
79 if ( $flags & self::TRANSFORM_LATER ) {
80 return new ThumbnailImage( $dstUrl, $width, $height, $dstPath );
81 }
82
83 if ( !wfMkdirParents( dirname( $dstPath ) ) ) {
84 return new MediaTransformError( 'thumbnail_error', $width, $height, wfMsg( 'thumbnail_dest_directory' ) );
85 }
86
87 # Use a subshell (brackets) to aggregate stderr from both pipeline commands
88 # before redirecting it to the overall stdout. This works in both Linux and Windows XP.
89 $cmd = '(' . wfEscapeShellArg( $wgDjvuRenderer ) . " -format=ppm -page={$page} -size={$width}x{$height} " .
90 wfEscapeShellArg( $srcPath );
91 if ( $wgDjvuPostProcessor ) {
92 $cmd .= " | {$wgDjvuPostProcessor}";
93 }
94 $cmd .= ' > ' . wfEscapeShellArg($dstPath) . ') 2>&1';
95 wfProfileIn( 'ddjvu' );
96 wfDebug( __METHOD__.": $cmd\n" );
97 $err = wfShellExec( $cmd, $retval );
98 wfProfileOut( 'ddjvu' );
99
100 $removed = $this->removeBadFile( $dstPath, $retval );
101 if ( $retval != 0 || $removed ) {
102 wfDebugLog( 'thumbnail',
103 sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
104 wfHostname(), $retval, trim($err), $cmd ) );
105 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
106 } else {
107 return new ThumbnailImage( $dstUrl, $width, $height, $dstPath );
108 }
109 }
110
111 /**
112 * Cache an instance of DjVuImage in an Image object, return that instance
113 */
114 function getDjVuImage( $image, $path ) {
115 if ( !$image ) {
116 $deja = new DjVuImage( $path );
117 } elseif ( !isset( $image->dejaImage ) ) {
118 $deja = $image->dejaImage = new DjVuImage( $path );
119 } else {
120 $deja = $image->dejaImage;
121 }
122 return $deja;
123 }
124
125 /**
126 * Cache a document tree for the DjVu XML metadata
127 */
128 function getMetaTree( $image ) {
129 if ( isset( $image->dejaMetaTree ) ) {
130 return $image->dejaMetaTree;
131 }
132
133 $metadata = $image->getMetadata();
134 if ( !$this->isMetadataValid( $image, $metadata ) ) {
135 wfDebug( "DjVu XML metadata is invalid or missing, should have been fixed in upgradeRow\n" );
136 return false;
137 }
138 wfProfileIn( __METHOD__ );
139
140 wfSuppressWarnings();
141 try {
142 $image->dejaMetaTree = new SimpleXMLElement( $metadata );
143 } catch( Exception $e ) {
144 wfDebug( "Bogus multipage XML metadata on '$image->name'\n" );
145 // Set to false rather than null to avoid further attempts
146 $image->dejaMetaTree = false;
147 }
148 wfRestoreWarnings();
149 wfProfileOut( __METHOD__ );
150 return $image->dejaMetaTree;
151 }
152
153 function getImageSize( $image, $path ) {
154 return $this->getDjVuImage( $image, $path )->getImageSize();
155 }
156
157 function getThumbType( $ext, $mime ) {
158 global $wgDjvuOutputExtension;
159 static $mime;
160 if ( !isset( $mime ) ) {
161 $magic = MimeMagic::singleton();
162 $mime = $magic->guessTypesForExtension( $wgDjvuOutputExtension );
163 }
164 return array( $wgDjvuOutputExtension, $mime );
165 }
166
167 function getMetadata( $image, $path ) {
168 wfDebug( "Getting DjVu metadata for $path\n" );
169 return $this->getDjVuImage( $image, $path )->retrieveMetaData();
170 }
171
172 function getMetadataType( $image ) {
173 return 'djvuxml';
174 }
175
176 function isMetadataValid( $image, $metadata ) {
177 return !empty( $metadata ) && $metadata != serialize(array());
178 }
179
180 function pageCount( $image ) {
181 $tree = $this->getMetaTree( $image );
182 if ( !$tree ) {
183 return false;
184 }
185 return count( $tree->xpath( '//OBJECT' ) );
186 }
187
188 function getPageDimensions( $image, $page ) {
189 $tree = $this->getMetaTree( $image );
190 if ( !$tree ) {
191 return false;
192 }
193
194 $o = $tree->BODY[0]->OBJECT[$page-1];
195 if ( $o ) {
196 return array(
197 'width' => intval( $o['width'] ),
198 'height' => intval( $o['height'] )
199 );
200 } else {
201 return false;
202 }
203 }
204 }
205
206 ?>