Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php |
— | — | @@ -97,14 +97,16 @@ |
98 | 98 | |
99 | 99 | static function extCheck( $meta, &$error, $saveName = '' ) { |
100 | 100 | global $wgTiffMaxEmbedFiles, $wgTiffMaxMetaSize; |
101 | | - if ( isset( $meta['errors'] ) ) { |
| 101 | + |
| 102 | + $errors = pagedTiffHandler::getMetadataErrors( $meta ); |
| 103 | + if ( $errors ) { |
102 | 104 | $error = 'tiff_bad_file'; |
103 | 105 | |
104 | 106 | // NOTE: in future, it will become possible to pass parameters |
105 | | - // $error = array( 'tiff_bad_file' , join('<br />', $meta['errors']) ); |
| 107 | + // $error = array( 'tiff_bad_file' , pagedTiffHandler::joinMessages( $errors ) ); |
106 | 108 | // does that work now? ^DK |
107 | 109 | |
108 | | - wfDebug( __METHOD__ . ": $error ($saveName) " . join( '; ', $meta['errors'] ) . "\n" ); |
| 110 | + wfDebug( __METHOD__ . ": $error ($saveName) " . pagedTiffHandler::joinMessages( $errors, false ) . "\n" ); |
109 | 111 | return false; |
110 | 112 | } |
111 | 113 | if ( ( strlen( serialize( $meta ) ) + 1 ) > $wgTiffMaxMetaSize ) { |
— | — | @@ -228,6 +230,41 @@ |
229 | 231 | return true; |
230 | 232 | } |
231 | 233 | |
| 234 | + static function getMetadataErrors( $metadata ) { |
| 235 | + if ( is_string( $metadata ) ) { |
| 236 | + $metadata = unserialize( $metadata ); |
| 237 | + } |
| 238 | + |
| 239 | + if ( !$metadata ) { |
| 240 | + return true; |
| 241 | + } else if ( isset( $metadata[ 'errors' ] ) ) { |
| 242 | + return $metadata[ 'errors' ]; |
| 243 | + } else { |
| 244 | + return false; |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + static function joinMessages( $errors_raw, $to_html = true ) { |
| 249 | + if ( is_array( $errors_raw ) ) { |
| 250 | + if ( !$errors_raw ) { |
| 251 | + return false; |
| 252 | + } |
| 253 | + |
| 254 | + if ( $to_html ) { |
| 255 | + $errors = array(); |
| 256 | + foreach ( $errors_raw as $error ) { |
| 257 | + $errors[] = htmlspecialchars( $error ); |
| 258 | + } |
| 259 | + |
| 260 | + return join( '<br />', $errors ); |
| 261 | + } else { |
| 262 | + return join( ";\n", $errors_raw ); |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + return $errors_raw; |
| 267 | + } |
| 268 | + |
232 | 269 | /** |
233 | 270 | * Checks whether a thumbnail with the requested file type and resolution exists, |
234 | 271 | * creates it if necessary, unless self::TRANSFORM_LATER is set in $flags. |
— | — | @@ -235,15 +272,19 @@ |
236 | 273 | */ |
237 | 274 | function doTransform( $image, $dstPath, $dstUrl, $params, $flags = 0 ) { |
238 | 275 | global $wgImageMagickConvertCommand, $wgTiffMaxEmbedFileResolution, $wgTiffUseVips, $wgTiffVipsCommand; |
239 | | - $metadata = $image->getMetadata(); |
240 | 276 | |
241 | | - if ( !$metadata ) { |
242 | | - if ( $metadata == - 1 ) { |
243 | | - return $this->doThumbError( $params['width'], $params['height'], 'tiff_error_cached' ); # #test this ^DK |
244 | | - // $wgCacheType = CACHE_DB |
| 277 | + $meta = $this->getMetaArray( $image ); |
| 278 | + $errors = PagedTiffHandler::getMetadataErrors( $meta ); |
| 279 | + |
| 280 | + if ( $errors ) { |
| 281 | + $errors = PagedTiffHandler::joinMessages( $errors ); |
| 282 | + if ( is_string( $errors ) ) { |
| 283 | + return $this->doThumbError( $params, 'tiff_bad_file' ); #TODO: original error as param # #test this ^DK |
| 284 | + } else { |
| 285 | + return $this->doThumbError( $params, 'tiff_no_metadata' ); |
245 | 286 | } |
246 | | - return $this->doThumbError( $params['width'], $params['height'], 'tiff_no_metadata' ); |
247 | 287 | } |
| 288 | + |
248 | 289 | if ( !$this->normaliseParams( $image, $params ) ) |
249 | 290 | return new TransformParameterError( $params ); |
250 | 291 | |
— | — | @@ -257,24 +298,23 @@ |
258 | 299 | return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page ); |
259 | 300 | } |
260 | 301 | |
261 | | - $meta = unserialize( $metadata ); |
262 | | - |
263 | 302 | if ( !$this->extCheck( $meta, $error, $dstPath ) ) { |
264 | | - return $this->doThumbError( $params['width'], $params['height'], $error ); |
| 303 | + return $this->doThumbError( $params, $error ); |
265 | 304 | } |
266 | 305 | |
267 | | - if ( is_file( $dstPath ) ) |
| 306 | + if ( is_file( $dstPath ) ) { |
268 | 307 | return new ThumbnailImage( $image, $dstUrl, $width, |
269 | 308 | $height, $dstPath, $page ); |
| 309 | + } |
270 | 310 | |
271 | 311 | if ( isset( $meta['page_data'][$page]['pixels'] ) && $meta['page_data'][$page]['pixels'] > $wgTiffMaxEmbedFileResolution ) |
272 | | - return $this->doThumbError( $width, $height, 'tiff_sourcefile_too_large' ); |
| 312 | + return $this->doThumbError( $params, 'tiff_sourcefile_too_large' ); |
273 | 313 | |
274 | 314 | if ( ( $width * $height ) > $wgTiffMaxEmbedFileResolution ) |
275 | | - return $this->doThumbError( $width, $height, 'tiff_targetfile_too_large' ); |
| 315 | + return $this->doThumbError( $params, 'tiff_targetfile_too_large' ); |
276 | 316 | |
277 | 317 | if ( !wfMkdirParents( dirname( $dstPath ) ) ) |
278 | | - return $this->doThumbError( $width, $height, 'thumbnail_dest_directory' ); |
| 318 | + return $this->doThumbError( $params, 'thumbnail_dest_directory' ); |
279 | 319 | |
280 | 320 | if ( $wgTiffUseVips ) { |
281 | 321 | // tested in Linux |
— | — | @@ -333,7 +373,28 @@ |
334 | 374 | /** |
335 | 375 | * Returns a new error message. |
336 | 376 | */ |
337 | | - protected function doThumbError( $width, $height, $msg ) { |
| 377 | + protected function doThumbError( $params, $msg ) { |
| 378 | + global $wgUser, $wgThumbLimits; |
| 379 | + |
| 380 | + if ( empty( $params['width'] ) ) { |
| 381 | + // no width/height in the parameter array |
| 382 | + // only happens if we don't have image meta-data, and no |
| 383 | + // size was specified by the user. |
| 384 | + // we need to pick *some* size, and the preferred |
| 385 | + // thumbnail size seems sane. |
| 386 | + $sz = $wgUser->getOption( 'thumbsize' ); |
| 387 | + $width = $wgThumbLimits[ $sz ]; |
| 388 | + $height = $width; // we don't have a hight, and no aspect ratio. make it square. |
| 389 | + } else { |
| 390 | + $width = intval( $params['width'] ); |
| 391 | + |
| 392 | + if ( !empty( $params['height'] ) ) { |
| 393 | + $height = intval( $params['height'] ); |
| 394 | + } else { |
| 395 | + $height = $width; // we don't have a hight, and no aspect ratio. make it square. |
| 396 | + } |
| 397 | + } |
| 398 | + |
338 | 399 | wfLoadExtensionMessages( 'PagedTiffHandler' ); |
339 | 400 | return new MediaTransformError( 'thumbnail_error', |
340 | 401 | $width, $height, wfMsg( $msg ) ); |
— | — | @@ -383,8 +444,14 @@ |
384 | 445 | * If it returns false, Image will reload the metadata from the file and update the database |
385 | 446 | */ |
386 | 447 | function isMetadataValid( $image, $metadata ) { |
| 448 | + |
387 | 449 | if ( !empty( $metadata ) && $metadata != serialize( array() ) ) { |
388 | 450 | $meta = unserialize( $metadata ); |
| 451 | + if ( isset( $meta['errors'] ) ) { |
| 452 | + //metadata contains an error message, but it's valid. |
| 453 | + //don't try to re-render until the error is resolved! |
| 454 | + return true; |
| 455 | + } |
389 | 456 | if ( isset( $meta['page_amount'] ) && isset( $meta['page_data'] ) ) { |
390 | 457 | return true; |
391 | 458 | } |
— | — | @@ -461,28 +528,23 @@ |
462 | 529 | ); |
463 | 530 | } |
464 | 531 | $meta = unserialize( $metadata ); |
465 | | - if ( isset( $meta['errors'] ) ) { |
466 | | - $errors = array(); |
467 | | - foreach ( $meta['errors'] as $error ) { |
468 | | - $errors[] = htmlspecialchars( $error ); |
469 | | - } |
| 532 | + $errors_raw = PagedTiffHandler::getMetadataErrors( $meta ); |
| 533 | + if ( $errors_raw ) { |
| 534 | + $errors = PagedTiffHandler::joinMessages( $errors_raw ); |
470 | 535 | self::addMeta( $result, |
471 | 536 | 'collapsed', |
472 | 537 | 'identify', |
473 | 538 | 'error', |
474 | | - join( '<br />', $errors ) |
| 539 | + $errors |
475 | 540 | ); |
476 | 541 | } |
477 | 542 | if ( isset( $meta['warnings'] ) ) { |
478 | | - $warnings = array(); |
479 | | - foreach ( $meta['warnings'] as $warning ) { |
480 | | - $warnings[] = htmlspecialchars( $warning ); |
481 | | - } |
| 543 | + $warnings = PagedTiffHandler::joinMessages( $meta['warnings'] ); |
482 | 544 | self::addMeta( $result, |
483 | 545 | 'collapsed', |
484 | 546 | 'identify', |
485 | 547 | 'warning', |
486 | | - join( '<br />', $warnings ) |
| 548 | + $warnings |
487 | 549 | ); |
488 | 550 | } |
489 | 551 | return $result; |
Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler.image.php |
— | — | @@ -80,15 +80,8 @@ |
81 | 81 | * meta['warnings'] = identify-warnings |
82 | 82 | */ |
83 | 83 | public function retrieveMetaData() { |
84 | | - global $wgImageMagickIdentifyCommand, $wgTiffExivCommand, $wgTiffUseExiv, $wgMemc, $wgTiffErrorCacheTTL; |
| 84 | + global $wgImageMagickIdentifyCommand, $wgTiffExivCommand, $wgTiffUseExiv; |
85 | 85 | |
86 | | - $imgKey = wfMemcKey( 'PagedTiffHandler-ThumbnailGeneration', $this->mFilename ); |
87 | | - $isCached = $wgMemc->get( $imgKey ); |
88 | | - if ( $isCached ) { |
89 | | - return - 1; |
90 | | - } |
91 | | - $wgMemc->add( $imgKey, 1, $wgTiffErrorCacheTTL ); |
92 | | - |
93 | 86 | if ( $this->_meta === null ) { |
94 | 87 | if ( $wgImageMagickIdentifyCommand ) { |
95 | 88 | |
— | — | @@ -106,7 +99,9 @@ |
107 | 100 | $dump = wfShellExec( $cmd, $retval ); |
108 | 101 | wfProfileOut( 'identify' ); |
109 | 102 | if ( $retval ) { |
110 | | - return false; |
| 103 | + $data['errors'][] = "identify command failed: $cmd"; |
| 104 | + wfDebug( __METHOD__ . ": identify command failed: $cmd\n" ); |
| 105 | + return $data; //fail. we *need* that info |
111 | 106 | } |
112 | 107 | $this->_meta = $this->convertDumpToArray( $dump ); |
113 | 108 | $this->_meta['exif'] = array(); |
— | — | @@ -124,6 +119,13 @@ |
125 | 120 | wfDebug( __METHOD__ . ": $cmd\n" ); |
126 | 121 | $dump = wfShellExec( $cmd, $retval ); |
127 | 122 | wfProfileOut( 'exiv2' ); |
| 123 | + |
| 124 | + if ( $retval ) { |
| 125 | + $data['errors'][] = "exiv command failed: $cmd"; |
| 126 | + wfDebug( __METHOD__ . ": exiv command failed: $cmd\n" ); |
| 127 | + //don't fail - we are missing info, but that's no reason to abort yet. |
| 128 | + } |
| 129 | + |
128 | 130 | $result = array(); |
129 | 131 | preg_match_all( '/(\w+)\s+(.+)/', $dump, $result, PREG_SET_ORDER ); |
130 | 132 | |
— | — | @@ -139,6 +141,13 @@ |
140 | 142 | wfDebug( __METHOD__ . ": $cmd\n" ); |
141 | 143 | $dump = wfShellExec( $cmd, $retval ); |
142 | 144 | wfProfileOut( 'identify -verbose' ); |
| 145 | + |
| 146 | + if ( $retval ) { |
| 147 | + $data['errors'][] = "identify command failed: $cmd"; |
| 148 | + wfDebug( __METHOD__ . ": identify command failed: $cmd\n" ); |
| 149 | + //don't fail - we are missing info, but that's no reason to abort yet. |
| 150 | + } |
| 151 | + |
143 | 152 | $this->_meta['exif'] = $this->parseVerbose( $dump ); |
144 | 153 | } |
145 | 154 | wfProfileOut( 'PagedTiffImage::retrieveMetaData' ); |
— | — | @@ -156,12 +165,15 @@ |
157 | 166 | */ |
158 | 167 | protected function convertDumpToArray( $dump ) { |
159 | 168 | global $wgTiffIdentifyRejectMessages, $wgTiffIdentifyBypassMessages; |
| 169 | + |
| 170 | + $data = array(); |
160 | 171 | if ( strval( $dump ) == '' ) { |
161 | | - return false; |
| 172 | + $data['errors'][] = "no metadata"; |
| 173 | + return $data; |
162 | 174 | } |
| 175 | + |
163 | 176 | $infos = null; |
164 | 177 | preg_match_all( '/\[BEGIN\](.+?)\[END\]/si', $dump, $infos, PREG_SET_ORDER ); |
165 | | - $data = array(); |
166 | 178 | $data['page_amount'] = count( $infos ); |
167 | 179 | $data['page_data'] = array(); |
168 | 180 | foreach ( $infos as $info ) { |
— | — | @@ -216,7 +228,7 @@ |
217 | 229 | } |
218 | 230 | } |
219 | 231 | if ( !$knownError ) { |
220 | | - $data['warning'][] = $error; |
| 232 | + $data['warnings'][] = $error; |
221 | 233 | } |
222 | 234 | } |
223 | 235 | } |