r65158 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65157‎ | r65158 | r65159 >
Date:14:01, 17 April 2010
Author:catrope
Status:deferred
Tags:
Comment:
PagedTiffHandler: spaces -> tabs
Modified paths:
  • /trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php
@@ -22,552 +22,552 @@
2323 */
2424
2525 class PagedTiffHandler extends ImageHandler {
26 - /**
27 - * Sets $wgShowEXIF to true when file is a tiff file.
28 - * This does not influence other ImageHandlers, which are possibly dependent on read-exif-data.
29 - */
30 - function __construct() {
31 - // FIXME: Manipulating globals in a constructor this way is evil
32 - global $wgShowEXIF;
33 - $wgShowEXIF = true;
34 - }
 26+ /**
 27+ * Sets $wgShowEXIF to true when file is a tiff file.
 28+ * This does not influence other ImageHandlers, which are possibly dependent on read-exif-data.
 29+ */
 30+ function __construct() {
 31+ // FIXME: Manipulating globals in a constructor this way is evil
 32+ global $wgShowEXIF;
 33+ $wgShowEXIF = true;
 34+ }
3535
36 - /**
37 - * Add the "lossy"-parameter to image link.
38 - * Usage:
39 - * lossy=true|false
40 - * lossy=1|0
41 - * lossy=lossy|lossless
42 - * E.g. [[Image:Test.tif|lossy=1]]
43 - */
44 - static function addTiffLossyMagicWordLang( &$magicWords, $langCode ) {
45 - $magicWords['img_lossy'] = array( 0, "lossy=$1" );
46 - return true;
47 - }
48 -
49 - /**
50 - * Customize the thumbnail shell command.
51 - */
52 - static function renderCommand( &$cmd, $srcPath, $dstPath, $page, $width, $height ) { ### why? ^DK
53 - // FIXME: This function seems useless
54 - return true;
55 - }
 36+ /**
 37+ * Add the "lossy"-parameter to image link.
 38+ * Usage:
 39+ * lossy=true|false
 40+ * lossy=1|0
 41+ * lossy=lossy|lossless
 42+ * E.g. [[Image:Test.tif|lossy=1]]
 43+ */
 44+ static function addTiffLossyMagicWordLang( &$magicWords, $langCode ) {
 45+ $magicWords['img_lossy'] = array( 0, "lossy=$1" );
 46+ return true;
 47+ }
 48+
 49+ /**
 50+ * Customize the thumbnail shell command.
 51+ */
 52+ static function renderCommand( &$cmd, $srcPath, $dstPath, $page, $width, $height ) { # ## why? ^DK
 53+ // FIXME: This function seems useless
 54+ return true;
 55+ }
5656
57 - function isEnabled() { return true; }
58 - function mustRender() { return true; }
59 - function isMultiPage( $img = false ) {
60 - if (!$img) return true;
61 - $meta = unserialize($img->metadata);
62 - return $meta['page_amount'] > 1;
63 - }
 57+ function isEnabled() { return true; }
 58+ function mustRender() { return true; }
 59+ function isMultiPage( $img = false ) {
 60+ if ( !$img ) return true;
 61+ $meta = unserialize( $img->metadata );
 62+ return $meta['page_amount'] > 1;
 63+ }
6464
65 - /**
66 - * Various checks against the uploaded file
67 - * - maximum upload size
68 - * - maximum number of embedded files
69 - * - maximum size of metadata
70 - * - identify-errors
71 - * - identify-warnings
72 - * - check for running-identify-service
73 - */
74 - function check( $saveName, $tempName, &$error ) {
75 - global $wgTiffMaxEmbedFiles, $wgTiffMaxMetaSize, $wgMaxUploadSize, $wgTiffRejectOnError, $wgTiffRejectOnWarning,
76 - $wgTiffUseTiffReader, $wgTiffReaderPath, $wgTiffReaderCheckEofForJS;
77 - wfLoadExtensionMessages( 'PagedTiffHandler' );
78 - if($wgTiffUseTiffReader) {
79 - $tr = new TiffReader($tempName);
80 - $tr->check();
81 - if(!$tr->isValidTiff()) {
82 - $error = 'tiff_bad_file';
83 - wfDebug( __METHOD__.": $error ($saveName)\n" );
84 - return false;
85 - }
86 - if($tr->checkScriptAtEnd($wgTiffReaderCheckEofForJS)) {
87 - $error = 'tiff_script_detected';
88 - wfDebug( __METHOD__.": $error ($saveName)\n" );
89 - return false;
90 - }
91 - if(!$tr->checkSize()) {
92 - $error = 'tiff_size_error';
93 - wfDebug( __METHOD__.": $error ($saveName)\n" );
94 - return false;
95 - }
96 - }
97 - $meta = self::getTiffImage(false, $tempName)->retrieveMetaData();
98 - if(!$meta && $meta != -1) {
99 - $error = 'tiff_out_of_service';
100 - wfDebug( __METHOD__.": $error ($saveName)\n" );
101 - return false;
102 - }
103 - if($meta == -1) {
104 - $error = 'tiff_error_cached';
105 - wfDebug( __METHOD__.": $error ($saveName)\n" );
106 - }
107 - return self::extCheck($meta, $error, $saveName);
108 - }
 65+ /**
 66+ * Various checks against the uploaded file
 67+ * - maximum upload size
 68+ * - maximum number of embedded files
 69+ * - maximum size of metadata
 70+ * - identify-errors
 71+ * - identify-warnings
 72+ * - check for running-identify-service
 73+ */
 74+ function check( $saveName, $tempName, &$error ) {
 75+ global $wgTiffMaxEmbedFiles, $wgTiffMaxMetaSize, $wgMaxUploadSize, $wgTiffRejectOnError, $wgTiffRejectOnWarning,
 76+ $wgTiffUseTiffReader, $wgTiffReaderPath, $wgTiffReaderCheckEofForJS;
 77+ wfLoadExtensionMessages( 'PagedTiffHandler' );
 78+ if ( $wgTiffUseTiffReader ) {
 79+ $tr = new TiffReader( $tempName );
 80+ $tr->check();
 81+ if ( !$tr->isValidTiff() ) {
 82+ $error = 'tiff_bad_file';
 83+ wfDebug( __METHOD__ . ": $error ($saveName)\n" );
 84+ return false;
 85+ }
 86+ if ( $tr->checkScriptAtEnd( $wgTiffReaderCheckEofForJS ) ) {
 87+ $error = 'tiff_script_detected';
 88+ wfDebug( __METHOD__ . ": $error ($saveName)\n" );
 89+ return false;
 90+ }
 91+ if ( !$tr->checkSize() ) {
 92+ $error = 'tiff_size_error';
 93+ wfDebug( __METHOD__ . ": $error ($saveName)\n" );
 94+ return false;
 95+ }
 96+ }
 97+ $meta = self::getTiffImage( false, $tempName )->retrieveMetaData();
 98+ if ( !$meta && $meta != - 1 ) {
 99+ $error = 'tiff_out_of_service';
 100+ wfDebug( __METHOD__ . ": $error ($saveName)\n" );
 101+ return false;
 102+ }
 103+ if ( $meta == - 1 ) {
 104+ $error = 'tiff_error_cached';
 105+ wfDebug( __METHOD__ . ": $error ($saveName)\n" );
 106+ }
 107+ return self::extCheck( $meta, $error, $saveName );
 108+ }
109109
110 - function extCheck($meta, &$error, $saveName = '') {
111 - global $wgTiffMaxEmbedFiles, $wgTiffMaxMetaSize;
112 - if(isset($meta['errors'])) {
113 - $error = 'tiff_bad_file';
 110+ function extCheck( $meta, &$error, $saveName = '' ) {
 111+ global $wgTiffMaxEmbedFiles, $wgTiffMaxMetaSize;
 112+ if ( isset( $meta['errors'] ) ) {
 113+ $error = 'tiff_bad_file';
114114
115 - // NOTE: in future, it will become possible to pass parameters
116 - //$error = array( 'tiff_bad_file' , join('<br />', $meta['errors']) );
117 - // does that work now? ^DK
 115+ // NOTE: in future, it will become possible to pass parameters
 116+ // $error = array( 'tiff_bad_file' , join('<br />', $meta['errors']) );
 117+ // does that work now? ^DK
118118
119 - wfDebug( __METHOD__.": $error ($saveName) " . join('; ', $meta['errors']) . "\n" );
120 - return false;
121 - }
122 - if((strlen(serialize($meta))+1) > $wgTiffMaxMetaSize) {
123 - $error = 'tiff_too_much_meta';
124 - wfDebug( __METHOD__.": $error ($saveName)\n" );
125 - return false;
126 - }
127 - if($wgTiffMaxEmbedFiles && $meta['page_amount'] > $wgTiffMaxEmbedFiles) {
128 - $error = 'tiff_too_much_embed_files';
129 - wfDebug( __METHOD__.": $error ($saveName)\n" );
130 - return false;
131 - }
132 - return true;
133 - }
 119+ wfDebug( __METHOD__ . ": $error ($saveName) " . join( '; ', $meta['errors'] ) . "\n" );
 120+ return false;
 121+ }
 122+ if ( ( strlen( serialize( $meta ) ) + 1 ) > $wgTiffMaxMetaSize ) {
 123+ $error = 'tiff_too_much_meta';
 124+ wfDebug( __METHOD__ . ": $error ($saveName)\n" );
 125+ return false;
 126+ }
 127+ if ( $wgTiffMaxEmbedFiles && $meta['page_amount'] > $wgTiffMaxEmbedFiles ) {
 128+ $error = 'tiff_too_much_embed_files';
 129+ wfDebug( __METHOD__ . ": $error ($saveName)\n" );
 130+ return false;
 131+ }
 132+ return true;
 133+ }
134134
135 - /**
136 - * Maps MagicWord-IDs to parameters.
137 - * Parameter 'lossy' was added.
138 - */
139 - function getParamMap() {
140 - // FIXME: This function is unused and should probably be a static member anyway
141 - return array(
142 - 'img_width' => 'width',
143 - // @todo check height ## MediaWiki doesn't have a MagicWord-ID for height, appears to be a bug. ^SU
144 - 'img_page' => 'page',
145 - 'img_lossy' => 'lossy',
146 - );
147 - }
 135+ /**
 136+ * Maps MagicWord-IDs to parameters.
 137+ * Parameter 'lossy' was added.
 138+ */
 139+ function getParamMap() {
 140+ // FIXME: This function is unused and should probably be a static member anyway
 141+ return array(
 142+ 'img_width' => 'width',
 143+ // @todo check height ## MediaWiki doesn't have a MagicWord-ID for height, appears to be a bug. ^SU
 144+ 'img_page' => 'page',
 145+ 'img_lossy' => 'lossy',
 146+ );
 147+ }
148148
149149
150 - /**
151 - * Checks whether parameters are valid and have valid values.
152 - * Check for lossy was added.
153 - */
154 - function validateParam( $name, $value ) {
155 - if ( in_array( $name, array( 'width', 'height', 'page', 'lossy' ) ) ) {
156 - if($name == 'lossy') {
157 - return in_array( $value, array( 1, 0, '1', '0', 'true', 'false', 'lossy', 'lossless' ) );
158 - }
159 - else if ( $value <= 0 || $value > 65535 ) { // ImageMagick hits an overflow for values over 65536
160 - return false;
161 - } else {
162 - return true;
163 - }
164 - } else {
165 - return false;
166 - }
167 - }
 150+ /**
 151+ * Checks whether parameters are valid and have valid values.
 152+ * Check for lossy was added.
 153+ */
 154+ function validateParam( $name, $value ) {
 155+ if ( in_array( $name, array( 'width', 'height', 'page', 'lossy' ) ) ) {
 156+ if ( $name == 'lossy' ) {
 157+ return in_array( $value, array( 1, 0, '1', '0', 'true', 'false', 'lossy', 'lossless' ) );
 158+ }
 159+ else if ( $value <= 0 || $value > 65535 ) { // ImageMagick hits an overflow for values over 65536
 160+ return false;
 161+ } else {
 162+ return true;
 163+ }
 164+ } else {
 165+ return false;
 166+ }
 167+ }
168168
169 - /**
170 - * Creates parameter string for file name.
171 - * Page number was added.
172 - */
173 - function makeParamString( $params ) {
174 - $page = isset( $params['page'] ) ? $params['page'] : 1;
175 - if ( !isset( $params['width'] ) ) {
176 - return false;
177 - }
178 - $lossy = isset( $params['lossy'] ) && $params['lossy'] ? 'lossy' : 'lossless';
179 - return "{$lossy}-page{$page}-{$params['width']}px";
180 - }
 169+ /**
 170+ * Creates parameter string for file name.
 171+ * Page number was added.
 172+ */
 173+ function makeParamString( $params ) {
 174+ $page = isset( $params['page'] ) ? $params['page'] : 1;
 175+ if ( !isset( $params['width'] ) ) {
 176+ return false;
 177+ }
 178+ $lossy = isset( $params['lossy'] ) && $params['lossy'] ? 'lossy' : 'lossless';
 179+ return "{$lossy}-page{$page}-{$params['width']}px";
 180+ }
181181
182 - /**
183 - * Parses parameter string into an array.
184 - */
185 - function parseParamString( $str ) {
186 - $m = false;
187 - if ( preg_match( '/^(\w+)-page(\d+)-(\d+)px$/', $str, $m ) ) {
188 - return array( 'width' => $m[3], 'page' => $m[2], 'lossy' => $m[1] );
189 - } else {
190 - return false;
191 - }
192 - }
 182+ /**
 183+ * Parses parameter string into an array.
 184+ */
 185+ function parseParamString( $str ) {
 186+ $m = false;
 187+ if ( preg_match( '/^(\w+)-page(\d+)-(\d+)px$/', $str, $m ) ) {
 188+ return array( 'width' => $m[3], 'page' => $m[2], 'lossy' => $m[1] );
 189+ } else {
 190+ return false;
 191+ }
 192+ }
193193
194 - /**
195 - * Lossy parameter added
196 - * TODO: The purpose of this function is not yet fully clear.
197 - */
198 - function getScriptParams( $params ) { ## wtf?? ^DK
199 - // FIXME: This function is unused, seems to be useless,
 194+ /**
 195+ * Lossy parameter added
 196+ * TODO: The purpose of this function is not yet fully clear.
 197+ */
 198+ function getScriptParams( $params ) { # # wtf?? ^DK
 199+ // FIXME: This function is unused, seems to be useless,
200200 // and could be replaced with an array_intersect() call
201 - return array(
202 - 'width' => $params['width'],
203 - 'page' => $params['page'],
204 - 'lossy' => $params['lossy'],
205 - );
206 - }
 201+ return array(
 202+ 'width' => $params['width'],
 203+ 'page' => $params['page'],
 204+ 'lossy' => $params['lossy'],
 205+ );
 206+ }
207207
208 - /**
209 - * Prepares param array and sets standard values.
210 - * Standard values for page and lossy are added.
211 - */
212 - function normaliseParams( $image, &$params ) {
213 - $mimeType = $image->getMimeType();
 208+ /**
 209+ * Prepares param array and sets standard values.
 210+ * Standard values for page and lossy are added.
 211+ */
 212+ function normaliseParams( $image, &$params ) {
 213+ $mimeType = $image->getMimeType();
214214
215 - if ( !isset( $params['width'] ) ) {
216 - return false;
217 - }
218 - if ( !isset( $params['page'] ) || $params['page'] < 1 ) {
219 - $params['page'] = 1;
220 - }
221 - if ( $params['page'] > $this->pageCount( $image ) ) {
222 - $params['page'] = $this->pageCount( $image );
223 - }
224 - if ( !isset( $params['lossy'] ) ) {
225 - $params['lossy'] = null;
226 - }
227 - $size = PagedTiffImage::getPageSize($this->getMetaArray($image), $params['page']);
228 - $srcWidth = $size['width'];
229 - $srcHeight = $size['height'];
 215+ if ( !isset( $params['width'] ) ) {
 216+ return false;
 217+ }
 218+ if ( !isset( $params['page'] ) || $params['page'] < 1 ) {
 219+ $params['page'] = 1;
 220+ }
 221+ if ( $params['page'] > $this->pageCount( $image ) ) {
 222+ $params['page'] = $this->pageCount( $image );
 223+ }
 224+ if ( !isset( $params['lossy'] ) ) {
 225+ $params['lossy'] = null;
 226+ }
 227+ $size = PagedTiffImage::getPageSize( $this->getMetaArray( $image ), $params['page'] );
 228+ $srcWidth = $size['width'];
 229+ $srcHeight = $size['height'];
230230
231 - if ( isset( $params['height'] ) && $params['height'] != -1 ) {
232 - // If the image is in letter format and the height parameter is set, the width
233 - // parameter is adjusted so the original ratio doesn't get messed up. This is
234 - // so the thumbnails on an ImagePage don't mess up the layout. ^SU
235 - if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
236 - $params['width'] = wfFitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
237 - }
238 - }
239 - $params['height'] = File::scaleHeight( $srcWidth, $srcHeight, $params['width'] );
240 - if ( !$this->validateThumbParams( $params['width'], $params['height'], $srcWidth, $srcHeight, $mimeType ) ) {
241 - return false;
242 - }
243 - return true;
244 - }
 231+ if ( isset( $params['height'] ) && $params['height'] != - 1 ) {
 232+ // If the image is in letter format and the height parameter is set, the width
 233+ // parameter is adjusted so the original ratio doesn't get messed up. This is
 234+ // so the thumbnails on an ImagePage don't mess up the layout. ^SU
 235+ if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
 236+ $params['width'] = wfFitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
 237+ }
 238+ }
 239+ $params['height'] = File::scaleHeight( $srcWidth, $srcHeight, $params['width'] );
 240+ if ( !$this->validateThumbParams( $params['width'], $params['height'], $srcWidth, $srcHeight, $mimeType ) ) {
 241+ return false;
 242+ }
 243+ return true;
 244+ }
245245
246 - /**
247 - * doTransform was changed for multipage and lossy support.
248 - * self::TRANSFORM_LATER is ignored. Instead, the function checks whether a
249 - * thumbnail with the requested file type and resolution exists. It will be
250 - * created if necessary.
251 - */
252 - function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
253 - global $wgImageMagickConvertCommand, $wgTiffMaxEmbedFileResolution, $wgTiffUseVips, $wgTiffVipsCommand;
 246+ /**
 247+ * doTransform was changed for multipage and lossy support.
 248+ * self::TRANSFORM_LATER is ignored. Instead, the function checks whether a
 249+ * thumbnail with the requested file type and resolution exists. It will be
 250+ * created if necessary.
 251+ */
 252+ function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) {
 253+ global $wgImageMagickConvertCommand, $wgTiffMaxEmbedFileResolution, $wgTiffUseVips, $wgTiffVipsCommand;
254254
255 - $metadata = $image->getMetadata();
 255+ $metadata = $image->getMetadata();
256256
257 - if ( !$metadata ) {
258 - if($metadata == -1) {
259 - return $this->doThumbError( @$params['width'], @$params['height'], 'tiff_error_cached' ); ##test this ^DK
260 - // $wgCacheType = CACHE_DB
261 - }
262 - return $this->doThumbError( @$params['width'], @$params['height'], 'tiff_no_metadata' );
263 - }
264 - if ( !$this->normaliseParams( $image, $params ) )
265 - return new TransformParameterError( $params );
 257+ if ( !$metadata ) {
 258+ if ( $metadata == - 1 ) {
 259+ return $this->doThumbError( @$params['width'], @$params['height'], 'tiff_error_cached' ); # #test this ^DK
 260+ // $wgCacheType = CACHE_DB
 261+ }
 262+ return $this->doThumbError( @$params['width'], @$params['height'], 'tiff_no_metadata' );
 263+ }
 264+ if ( !$this->normaliseParams( $image, $params ) )
 265+ return new TransformParameterError( $params );
266266
267 - // Get params and force width, height and page to be integers
268 - $width = intval( $params['width'] );
269 - $height = intval( $params['height'] );
270 - $srcPath = $image->getPath();
271 - $page = intval( $params['page'] );
 267+ // Get params and force width, height and page to be integers
 268+ $width = intval( $params['width'] );
 269+ $height = intval( $params['height'] );
 270+ $srcPath = $image->getPath();
 271+ $page = intval( $params['page'] );
272272
273 - $extension = $this->getThumbExtension($image, $page, $params['lossy']);
274 - $dstPath .= $extension;
275 - $dstUrl .= $extension;
 273+ $extension = $this->getThumbExtension( $image, $page, $params['lossy'] );
 274+ $dstPath .= $extension;
 275+ $dstUrl .= $extension;
276276
277 - $meta = unserialize($metadata);
 277+ $meta = unserialize( $metadata );
278278
279 - if(!$this->extCheck($meta, $error, $dstPath)) {
280 - return $this->doThumbError( @$params['width'], @$params['height'], $error );
281 - }
 279+ if ( !$this->extCheck( $meta, $error, $dstPath ) ) {
 280+ return $this->doThumbError( @$params['width'], @$params['height'], $error );
 281+ }
282282
283 - if ( is_file($dstPath) )
284 - return new ThumbnailImage( $image, $dstUrl, $width,
285 - $height, $dstPath, $page );
 283+ if ( is_file( $dstPath ) )
 284+ return new ThumbnailImage( $image, $dstUrl, $width,
 285+ $height, $dstPath, $page );
286286
287 - if(isset($meta['page_data'][$page]['pixels']) && $meta['page_data'][$page]['pixels'] > $wgTiffMaxEmbedFileResolution)
288 - return $this->doThumbError( $width, $height, 'tiff_sourcefile_too_large' );
 287+ if ( isset( $meta['page_data'][$page]['pixels'] ) && $meta['page_data'][$page]['pixels'] > $wgTiffMaxEmbedFileResolution )
 288+ return $this->doThumbError( $width, $height, 'tiff_sourcefile_too_large' );
289289
290 - if(($width * $height) > $wgTiffMaxEmbedFileResolution)
 290+ if ( ( $width * $height ) > $wgTiffMaxEmbedFileResolution )
291291 return $this->doThumbError( $width, $height, 'tiff_targetfile_too_large' );
292292
293 - if ( !wfMkdirParents( dirname( $dstPath ) ) )
294 - return $this->doThumbError( $width, $height, 'thumbnail_dest_directory' );
 293+ if ( !wfMkdirParents( dirname( $dstPath ) ) )
 294+ return $this->doThumbError( $width, $height, 'thumbnail_dest_directory' );
295295
296 - if($wgTiffUseVips) {
297 - // tested in Linux
298 - $cmd = wfEscapeShellArg( $wgTiffVipsCommand );
299 - $cmd .= ' im_resize_linear "'.wfEscapeShellArg( $srcPath ).':'.($page-1).'" ';
300 - $cmd .= wfEscapeShellArg( $dstPath );
301 - $cmd .= " {$width} {$height} 2>&1";
302 - }
303 - else {
304 - $cmd = wfEscapeShellArg( $wgImageMagickConvertCommand );
305 - $cmd .= " ". wfEscapeShellArg( $srcPath )."[".($page-1)."]";
306 - $cmd .= " -depth 8 -resize {$width} ";
307 - $cmd .= wfEscapeShellArg( $dstPath );
308 - }
 296+ if ( $wgTiffUseVips ) {
 297+ // tested in Linux
 298+ $cmd = wfEscapeShellArg( $wgTiffVipsCommand );
 299+ $cmd .= ' im_resize_linear "' . wfEscapeShellArg( $srcPath ) . ':' . ( $page - 1 ) . '" ';
 300+ $cmd .= wfEscapeShellArg( $dstPath );
 301+ $cmd .= " {$width} {$height} 2>&1";
 302+ }
 303+ else {
 304+ $cmd = wfEscapeShellArg( $wgImageMagickConvertCommand );
 305+ $cmd .= " " . wfEscapeShellArg( $srcPath ) . "[" . ( $page - 1 ) . "]";
 306+ $cmd .= " -depth 8 -resize {$width} ";
 307+ $cmd .= wfEscapeShellArg( $dstPath );
 308+ }
309309
310 - wfRunHooks( "PagedTiffHandlerRenderCommand", array( &$cmd, $srcPath, $dstPath, $page, $width, $height ) );
 310+ wfRunHooks( "PagedTiffHandlerRenderCommand", array( &$cmd, $srcPath, $dstPath, $page, $width, $height ) );
311311
312 - wfProfileIn( 'PagedTiffHandler' );
313 - wfDebug( __METHOD__.": $cmd\n" );
314 - $err = wfShellExec( $cmd, $retval );
315 - wfProfileOut( 'PagedTiffHandler' );
 312+ wfProfileIn( 'PagedTiffHandler' );
 313+ wfDebug( __METHOD__ . ": $cmd\n" );
 314+ $err = wfShellExec( $cmd, $retval );
 315+ wfProfileOut( 'PagedTiffHandler' );
316316
317 - $removed = $this->removeBadFile( $dstPath, $retval );
 317+ $removed = $this->removeBadFile( $dstPath, $retval );
318318
319 - if ( $retval != 0 || $removed ) {
320 - wfDebugLog( 'thumbnail', "thumbnail failed on " . wfHostname() .
321 - "; error $retval \"$err\" from \"$cmd\"" );
322 - return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
323 - } else {
324 - return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
325 - }
326 - }
 319+ if ( $retval != 0 || $removed ) {
 320+ wfDebugLog( 'thumbnail', "thumbnail failed on " . wfHostname() .
 321+ "; error $retval \"$err\" from \"$cmd\"" );
 322+ return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
 323+ } else {
 324+ return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
 325+ }
 326+ }
327327
328 - /**
329 - * Decides (taking lossy parameter into account) the filetype of the thumbnail.
330 - * If there is no lossy parameter (null = not set), the decision is made
331 - * according to the presence of an alpha value.
332 - * (alpha == true = png, alpha == false = jpg)
333 - */
334 - function getThumbExtension( $image, $page, $lossy ) {
335 - if($lossy === null) {
336 - $data = $this->getMetaArray($image);
337 - if((strtolower($data['page_data'][$page]['alpha']) == 'true')) {
338 - return '.png';
339 - }
340 - else {
341 - return '.jpg';
342 - }
343 - }
344 - else {
345 - if(in_array($lossy, array(1, '1', 'true', 'lossy'))) {
346 - return '.jpg';
347 - }
348 - else {
349 - return '.png';
350 - }
351 - }
352 - }
 328+ /**
 329+ * Decides (taking lossy parameter into account) the filetype of the thumbnail.
 330+ * If there is no lossy parameter (null = not set), the decision is made
 331+ * according to the presence of an alpha value.
 332+ * (alpha == true = png, alpha == false = jpg)
 333+ */
 334+ function getThumbExtension( $image, $page, $lossy ) {
 335+ if ( $lossy === null ) {
 336+ $data = $this->getMetaArray( $image );
 337+ if ( ( strtolower( $data['page_data'][$page]['alpha'] ) == 'true' ) ) {
 338+ return '.png';
 339+ }
 340+ else {
 341+ return '.jpg';
 342+ }
 343+ }
 344+ else {
 345+ if ( in_array( $lossy, array( 1, '1', 'true', 'lossy' ) ) ) {
 346+ return '.jpg';
 347+ }
 348+ else {
 349+ return '.png';
 350+ }
 351+ }
 352+ }
353353
354 - /**
355 - * Returns the number of available pages/embedded files
356 - */
357 - function pageCount( $image ) {
358 - $data = $this->getMetaArray( $image );
359 - if ( !$data ) return false;
360 - return intval( $data['page_amount'] );
361 - }
 354+ /**
 355+ * Returns the number of available pages/embedded files
 356+ */
 357+ function pageCount( $image ) {
 358+ $data = $this->getMetaArray( $image );
 359+ if ( !$data ) return false;
 360+ return intval( $data['page_amount'] );
 361+ }
362362
363 - /**
364 - * Returns a new Error-Message.
365 - */
366 - protected function doThumbError( $width, $height, $msg ) {
367 - wfLoadExtensionMessages( 'PagedTiffHandler' );
368 - return new MediaTransformError( 'thumbnail_error',
369 - $width, $height, wfMsg($msg) );
370 - }
 363+ /**
 364+ * Returns a new Error-Message.
 365+ */
 366+ protected function doThumbError( $width, $height, $msg ) {
 367+ wfLoadExtensionMessages( 'PagedTiffHandler' );
 368+ return new MediaTransformError( 'thumbnail_error',
 369+ $width, $height, wfMsg( $msg ) );
 370+ }
371371
372 - /**
373 - * Get handler-specific metadata which will be saved in the img_metadata field.
374 - *
375 - * @param Image $image The image object, or false if there isn't one
376 - * @param string $fileName The filename
377 - * @return string
378 - */
379 - function getMetadata( $image, $path ) {
380 - return serialize( $this->getTiffImage( $image, $path )->retrieveMetaData() );
381 - }
 372+ /**
 373+ * Get handler-specific metadata which will be saved in the img_metadata field.
 374+ *
 375+ * @param Image $image The image object, or false if there isn't one
 376+ * @param string $fileName The filename
 377+ * @return string
 378+ */
 379+ function getMetadata( $image, $path ) {
 380+ return serialize( $this->getTiffImage( $image, $path )->retrieveMetaData() );
 381+ }
382382
383 - /**
384 - * Creates detail information that is being displayed on image page.
385 - */
386 - function getLongDesc( $image ) {
387 - global $wgLang;
388 - $page = isset($_GET['page']) ? $_GET['page'] : 1;
389 - if ( !isset( $page ) || $page < 1 ) {
390 - $page = 1;
391 - }
392 - if ( $page > $this->pageCount( $image ) ) {
393 - $page = $this->pageCount( $image );
394 - }
395 - $metadata = $this->getMetaArray($image);
396 - if( $metadata ) {
397 - wfLoadExtensionMessages( 'PagedTiffHandler' );
398 - return wfMsgExt('tiff-file-info-size', 'parseinline',
399 - $wgLang->formatNum( $metadata['page_data'][$page]['width'] ),
400 - $wgLang->formatNum( $metadata['page_data'][$page]['height'] ),
401 - $wgLang->formatSize( $image->getSize() ),
402 - $image->getMimeType(),
403 - $page );
404 - }
405 - return true;
406 - }
 383+ /**
 384+ * Creates detail information that is being displayed on image page.
 385+ */
 386+ function getLongDesc( $image ) {
 387+ global $wgLang;
 388+ $page = isset( $_GET['page'] ) ? $_GET['page'] : 1;
 389+ if ( !isset( $page ) || $page < 1 ) {
 390+ $page = 1;
 391+ }
 392+ if ( $page > $this->pageCount( $image ) ) {
 393+ $page = $this->pageCount( $image );
 394+ }
 395+ $metadata = $this->getMetaArray( $image );
 396+ if ( $metadata ) {
 397+ wfLoadExtensionMessages( 'PagedTiffHandler' );
 398+ return wfMsgExt( 'tiff-file-info-size', 'parseinline',
 399+ $wgLang->formatNum( $metadata['page_data'][$page]['width'] ),
 400+ $wgLang->formatNum( $metadata['page_data'][$page]['height'] ),
 401+ $wgLang->formatSize( $image->getSize() ),
 402+ $image->getMimeType(),
 403+ $page );
 404+ }
 405+ return true;
 406+ }
407407
408 - /**
409 - * Check if the metadata string is valid for this handler.
410 - * If it returns false, Image will reload the metadata from the file and update the database
411 - */
412 - function isMetadataValid( $image, $metadata ) {
413 - if(!empty( $metadata ) && $metadata != serialize(array())) {
414 - $meta = unserialize($metadata);
415 - if(isset($meta['page_amount']) && isset($meta['page_data'])) {
416 - return true;
417 - }
418 - }
419 - return false;
420 - }
 408+ /**
 409+ * Check if the metadata string is valid for this handler.
 410+ * If it returns false, Image will reload the metadata from the file and update the database
 411+ */
 412+ function isMetadataValid( $image, $metadata ) {
 413+ if ( !empty( $metadata ) && $metadata != serialize( array() ) ) {
 414+ $meta = unserialize( $metadata );
 415+ if ( isset( $meta['page_amount'] ) && isset( $meta['page_data'] ) ) {
 416+ return true;
 417+ }
 418+ }
 419+ return false;
 420+ }
421421
422 - /**
423 - * Get a list of EXIF metadata items which should be displayed when
424 - * the metadata table is collapsed.
425 - *
426 - * @return array of strings
427 - * @access private
428 - */
429 - function visibleMetadataFields() {
430 - $fields = array();
431 - $lines = explode( "\n", wfMsg( 'metadata-fields' ) );
432 - foreach( $lines as $line ) {
433 - $matches = array();
434 - if( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
435 - $fields[] = $matches[1];
436 - }
437 - }
438 - $fields = array_map( 'strtolower', $fields );
439 - return $fields;
440 - }
 422+ /**
 423+ * Get a list of EXIF metadata items which should be displayed when
 424+ * the metadata table is collapsed.
 425+ *
 426+ * @return array of strings
 427+ * @access private
 428+ */
 429+ function visibleMetadataFields() {
 430+ $fields = array();
 431+ $lines = explode( "\n", wfMsg( 'metadata-fields' ) );
 432+ foreach ( $lines as $line ) {
 433+ $matches = array();
 434+ if ( preg_match( '/^\\*\s*(.*?)\s*$/', $line, $matches ) ) {
 435+ $fields[] = $matches[1];
 436+ }
 437+ }
 438+ $fields = array_map( 'strtolower', $fields );
 439+ return $fields;
 440+ }
441441
442 - /**
443 - * Get an array structure that looks like this:
444 - *
445 - * array(
446 - * 'visible' => array(
447 - * 'Human-readable name' => 'Human readable value',
448 - * ...
449 - * ),
450 - * 'collapsed' => array(
451 - * 'Human-readable name' => 'Human readable value',
452 - * ...
453 - * )
454 - * )
455 - * The UI will format this into a table where the visible fields are always
456 - * visible, and the collapsed fields are optionally visible.
457 - *
458 - * The function should return false if there is no metadata to display.
459 - */
 442+ /**
 443+ * Get an array structure that looks like this:
 444+ *
 445+ * array(
 446+ * 'visible' => array(
 447+ * 'Human-readable name' => 'Human readable value',
 448+ * ...
 449+ * ),
 450+ * 'collapsed' => array(
 451+ * 'Human-readable name' => 'Human readable value',
 452+ * ...
 453+ * )
 454+ * )
 455+ * The UI will format this into a table where the visible fields are always
 456+ * visible, and the collapsed fields are optionally visible.
 457+ *
 458+ * The function should return false if there is no metadata to display.
 459+ */
460460
461 - function formatMetadata( $image ) {
462 - $result = array(
463 - 'visible' => array(),
464 - 'collapsed' => array()
465 - );
466 - $metadata = $image->getMetadata();
467 - if ( !$metadata ) {
468 - return false;
469 - }
470 - $exif = unserialize( $metadata );
471 - $exif = $exif['exif'];
472 - if ( !$exif ) {
473 - return false;
474 - }
475 - unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
476 - $format = new FormatExif( $exif );
 461+ function formatMetadata( $image ) {
 462+ $result = array(
 463+ 'visible' => array(),
 464+ 'collapsed' => array()
 465+ );
 466+ $metadata = $image->getMetadata();
 467+ if ( !$metadata ) {
 468+ return false;
 469+ }
 470+ $exif = unserialize( $metadata );
 471+ $exif = $exif['exif'];
 472+ if ( !$exif ) {
 473+ return false;
 474+ }
 475+ unset( $exif['MEDIAWIKI_EXIF_VERSION'] );
 476+ $format = new FormatExif( $exif );
477477
478 - $formatted = $format->getFormattedData();
479 - // Sort fields into visible and collapsed
480 - $visibleFields = $this->visibleMetadataFields();
481 - foreach ( $formatted as $name => $value ) {
482 - $tag = strtolower( $name );
483 - self::addMeta( $result,
484 - in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
485 - 'exif',
486 - $tag,
487 - htmlspecialchars($value)
488 - );
489 - }
490 - $meta = unserialize($metadata);
491 - if(isset($meta['errors'])) {
492 - $errors = array();
493 - foreach($meta['errors'] as $error) {
494 - $errors[] = htmlspecialchars($error);
495 - }
496 - self::addMeta( $result,
497 - 'collapsed',
498 - 'identify',
499 - 'error',
500 - join('<br />', $errors)
501 - );
502 - }
503 - if(isset($meta['warnings'])) {
504 - $warnings = array();
505 - foreach($meta['warnings'] as $warning) {
506 - $warnings[] = htmlspecialchars($warning);
507 - }
508 - self::addMeta( $result,
509 - 'collapsed',
510 - 'identify',
511 - 'warning',
512 - join('<br />', $warnings)
513 - );
514 - }
515 - return $result;
516 - }
 478+ $formatted = $format->getFormattedData();
 479+ // Sort fields into visible and collapsed
 480+ $visibleFields = $this->visibleMetadataFields();
 481+ foreach ( $formatted as $name => $value ) {
 482+ $tag = strtolower( $name );
 483+ self::addMeta( $result,
 484+ in_array( $tag, $visibleFields ) ? 'visible' : 'collapsed',
 485+ 'exif',
 486+ $tag,
 487+ htmlspecialchars( $value )
 488+ );
 489+ }
 490+ $meta = unserialize( $metadata );
 491+ if ( isset( $meta['errors'] ) ) {
 492+ $errors = array();
 493+ foreach ( $meta['errors'] as $error ) {
 494+ $errors[] = htmlspecialchars( $error );
 495+ }
 496+ self::addMeta( $result,
 497+ 'collapsed',
 498+ 'identify',
 499+ 'error',
 500+ join( '<br />', $errors )
 501+ );
 502+ }
 503+ if ( isset( $meta['warnings'] ) ) {
 504+ $warnings = array();
 505+ foreach ( $meta['warnings'] as $warning ) {
 506+ $warnings[] = htmlspecialchars( $warning );
 507+ }
 508+ self::addMeta( $result,
 509+ 'collapsed',
 510+ 'identify',
 511+ 'warning',
 512+ join( '<br />', $warnings )
 513+ );
 514+ }
 515+ return $result;
 516+ }
517517
518 - /**
519 - * Returns a PagedTiffImage or creates a new one if it doesn't exist.
520 - * @param Image $image The image object, or false if there isn't one
521 - * @param string $fileName The filename
522 - */
523 - static function getTiffImage( $image, $path ) {
524 - // If no Image object is passed, a TiffImage is created based on $path .
 518+ /**
 519+ * Returns a PagedTiffImage or creates a new one if it doesn't exist.
 520+ * @param Image $image The image object, or false if there isn't one
 521+ * @param string $fileName The filename
 522+ */
 523+ static function getTiffImage( $image, $path ) {
 524+ // If no Image object is passed, a TiffImage is created based on $path .
525525 // If there is an Image object, we check whether there's already a TiffImage instance in there;
526526 // if not, a new instance is created and stored in the Image object
527 - if ( !$image )
528 - $tiffimg = new PagedTiffImage( $path );
529 - elseif ( !isset( $image->tiffImage ) )
530 - $tiffimg = $image->tiffImage = new PagedTiffImage( $path );
531 - else
532 - $tiffimg = $image->tiffImage;
 527+ if ( !$image )
 528+ $tiffimg = new PagedTiffImage( $path );
 529+ elseif ( !isset( $image->tiffImage ) )
 530+ $tiffimg = $image->tiffImage = new PagedTiffImage( $path );
 531+ else
 532+ $tiffimg = $image->tiffImage;
533533
534 - return $tiffimg;
535 - }
 534+ return $tiffimg;
 535+ }
536536
537 - /**
538 - * Returns an Array with the Image-Metadata.
539 - */
540 - function getMetaArray( $image ) {
541 - if ( isset( $image->tiffMetaArray ) )
542 - return $image->tiffMetaArray;
 537+ /**
 538+ * Returns an Array with the Image-Metadata.
 539+ */
 540+ function getMetaArray( $image ) {
 541+ if ( isset( $image->tiffMetaArray ) )
 542+ return $image->tiffMetaArray;
543543
544 - $metadata = $image->getMetadata();
 544+ $metadata = $image->getMetadata();
545545
546 - if ( !$this->isMetadataValid( $image, $metadata ) ) {
547 - wfDebug( "Tiff metadata is invalid or missing, should have been fixed in upgradeRow\n" );
548 - return false;
549 - }
 546+ if ( !$this->isMetadataValid( $image, $metadata ) ) {
 547+ wfDebug( "Tiff metadata is invalid or missing, should have been fixed in upgradeRow\n" );
 548+ return false;
 549+ }
550550
551 - wfProfileIn( __METHOD__ );
552 - wfSuppressWarnings();
553 - $image->tiffMetaArray = unserialize( $metadata );
554 - wfRestoreWarnings();
555 - wfProfileOut( __METHOD__ );
 551+ wfProfileIn( __METHOD__ );
 552+ wfSuppressWarnings();
 553+ $image->tiffMetaArray = unserialize( $metadata );
 554+ wfRestoreWarnings();
 555+ wfProfileOut( __METHOD__ );
556556
557 - return $image->tiffMetaArray;
558 - }
 557+ return $image->tiffMetaArray;
 558+ }
559559
560 - /**
561 - * Get an associative array of page dimensions
562 - * Currently "width" and "height" are understood, but this might be
563 - * expanded in the future.
564 - * Returns false if unknown or if the document is not multi-page.
565 - */
566 - function getPageDimensions( $image, $page ) {
567 - if(!$page) { $page=1; } // makeImageLink2 (Linker.php) sets $page to false if no page parameter in wiki code is set
568 - if ( $page > $this->pageCount( $image ) ) {
569 - $page = $this->pageCount( $image );
570 - }
571 - $data = $this->getMetaArray( $image );
572 - return PagedTiffImage::getPageSize( $data, $page );
573 - }
 560+ /**
 561+ * Get an associative array of page dimensions
 562+ * Currently "width" and "height" are understood, but this might be
 563+ * expanded in the future.
 564+ * Returns false if unknown or if the document is not multi-page.
 565+ */
 566+ function getPageDimensions( $image, $page ) {
 567+ if ( !$page ) { $page = 1; } // makeImageLink2 (Linker.php) sets $page to false if no page parameter in wiki code is set
 568+ if ( $page > $this->pageCount( $image ) ) {
 569+ $page = $this->pageCount( $image );
 570+ }
 571+ $data = $this->getMetaArray( $image );
 572+ return PagedTiffImage::getPageSize( $data, $page );
 573+ }
574574 }

Status & tagging log