r65157 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65156‎ | r65157 | r65158 >
Date:13:23, 17 April 2010
Author:catrope
Status:deferred
Tags:
Comment:
PagedTiffHandler: Initial cleanup. Latge-scale whitespace fixes upcoming. Adding FIXME comments and translating some comments from German and loading TiffReader using AutoLoader
Modified paths:
  • /trunk/extensions/PagedTiffHandler/PagedTiffHandler.php (modified) (history)
  • /trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler_body.php
@@ -1,6 +1,6 @@
22 <?php
33 /**
4 - * Copyright (C) Wikimedia Deuschland, 2009
 4+ * Copyright (C) Wikimedia Deutschland, 2009
55 * Authors Hallo Welt! Medienwerkstatt GmbH
66 * Authors Sebastian Ulbricht, Daniel Lynge, Marc Reymann, Markus Glaser
77 *
@@ -26,7 +26,8 @@
2727 * Sets $wgShowEXIF to true when file is a tiff file.
2828 * This does not influence other ImageHandlers, which are possibly dependent on read-exif-data.
2929 */
30 - function PagedTiffHandler() {
 30+ function __construct() {
 31+ // FIXME: Manipulating globals in a constructor this way is evil
3132 global $wgShowEXIF;
3233 $wgShowEXIF = true;
3334 }
@@ -48,23 +49,23 @@
4950 * Customize the thumbnail shell command.
5051 */
5152 static function renderCommand( &$cmd, $srcPath, $dstPath, $page, $width, $height ) { ### why? ^DK
 53+ // FIXME: This function seems useless
5254 return true;
5355 }
5456
5557 function isEnabled() { return true; }
5658 function mustRender() { return true; }
57 - function isMultiPage($img = false) {
 59+ function isMultiPage( $img = false ) {
5860 if (!$img) return true;
5961 $meta = unserialize($img->metadata);
60 - return ($meta['page_amount'] > 1);
 62+ return $meta['page_amount'] > 1;
6163 }
6264
6365 /**
64 - * various checks against the uploaded file
65 - * - maximum upload-size
 66+ * Various checks against the uploaded file
 67+ * - maximum upload size
6668 * - maximum number of embedded files
6769 * - maximum size of metadata
68 - * - maximum size of metadata
6970 * - identify-errors
7071 * - identify-warnings
7172 * - check for running-identify-service
@@ -74,90 +75,87 @@
7576 $wgTiffUseTiffReader, $wgTiffReaderPath, $wgTiffReaderCheckEofForJS;
7677 wfLoadExtensionMessages( 'PagedTiffHandler' );
7778 if($wgTiffUseTiffReader) {
78 - require_once($wgTiffReaderPath.'/TiffReader.php');
7979 $tr = new TiffReader($tempName);
8080 $tr->check();
8181 if(!$tr->isValidTiff()) {
8282 $error = 'tiff_bad_file';
83 - wfDebug( __METHOD__.": tiff_bad_file ($saveName)\n" );
 83+ wfDebug( __METHOD__.": $error ($saveName)\n" );
8484 return false;
8585 }
8686 if($tr->checkScriptAtEnd($wgTiffReaderCheckEofForJS)) {
8787 $error = 'tiff_script_detected';
88 - wfDebug( __METHOD__.": tiff_script_detected ($saveName)\n" );
 88+ wfDebug( __METHOD__.": $error ($saveName)\n" );
8989 return false;
9090 }
9191 if(!$tr->checkSize()) {
9292 $error = 'tiff_size_error';
93 - wfDebug( __METHOD__.": tiff_size_error ($saveName)\n" );
 93+ wfDebug( __METHOD__.": $error ($saveName)\n" );
9494 return false;
9595 }
9696 }
9797 $meta = self::getTiffImage(false, $tempName)->retrieveMetaData();
9898 if(!$meta && $meta != -1) {
9999 $error = 'tiff_out_of_service';
100 - wfDebug( __METHOD__.": tiff_out_of_service ($saveName)\n" );
 100+ wfDebug( __METHOD__.": $error ($saveName)\n" );
101101 return false;
102102 }
103103 if($meta == -1) {
104104 $error = 'tiff_error_cached';
105 - wfDebug( __METHOD__.": tiff_error_cached ($saveName)\n" );
 105+ wfDebug( __METHOD__.": $error ($saveName)\n" );
106106 }
107107 return self::extCheck($meta, $error, $saveName);
108108 }
109109
110110 function extCheck($meta, &$error, $saveName = '') {
111 - global $wgTiffMaxEmbedFiles, $wgTiffMaxMetaSize;
 111+ global $wgTiffMaxEmbedFiles, $wgTiffMaxMetaSize;
112112 if(isset($meta['errors'])) {
113113 $error = 'tiff_bad_file';
114114
115 - //NOTE: in future, it will become possible to pass parameters
 115+ // NOTE: in future, it will become possible to pass parameters
116116 //$error = array( 'tiff_bad_file' , join('<br />', $meta['errors']) );
117 - ### does that work now? ^DK
 117+ // does that work now? ^DK
118118
119 - wfDebug( __METHOD__.": tiff_bad_file ($saveName) " . join('; ', $meta['errors']) . "\n" );
 119+ wfDebug( __METHOD__.": $error ($saveName) " . join('; ', $meta['errors']) . "\n" );
120120 return false;
121121 }
122122 if((strlen(serialize($meta))+1) > $wgTiffMaxMetaSize) {
123123 $error = 'tiff_too_much_meta';
124 - wfDebug( __METHOD__.": tiff_too_much_meta ($saveName)\n" );
 124+ wfDebug( __METHOD__.": $error ($saveName)\n" );
125125 return false;
126126 }
127127 if($wgTiffMaxEmbedFiles && $meta['page_amount'] > $wgTiffMaxEmbedFiles) {
128128 $error = 'tiff_too_much_embed_files';
129 - wfDebug( __METHOD__.": tiff_too_much_embed_files ($saveName)\n" );
 129+ wfDebug( __METHOD__.": $error ($saveName)\n" );
130130 return false;
131131 }
132132 return true;
133133 }
134134
135135 /**
136 - * maps MagicWord-IDs to parameters.
137 - * parameter 'lossy' was added.
 136+ * Maps MagicWord-IDs to parameters.
 137+ * Parameter 'lossy' was added.
138138 */
139139 function getParamMap() {
 140+ // FIXME: This function is unused and should probably be a static member anyway
140141 return array(
141 - 'img_width' => 'width',
142 - // @todo check height ## MediaWiki bietet keine MagicWord-ID für height. Dies scheint ein Bug zu sein. ^SU
143 - 'img_page' => 'page',
144 - 'img_lossy' => 'lossy',
 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',
145146 );
146147 }
147148
148149
149 - /*
150 - * checks whether parameters are valid and have valid values.
151 - * check for lossy was added.
 150+ /**
 151+ * Checks whether parameters are valid and have valid values.
 152+ * Check for lossy was added.
152153 */
153154 function validateParam( $name, $value ) {
154155 if ( in_array( $name, array( 'width', 'height', 'page', 'lossy' ) ) ) {
155156 if($name == 'lossy') {
156 - if(in_array($value, array(1, 0, '1', '0', 'true', 'false', 'lossy', 'lossless'))) {
157 - return true;
158 - }
159 - return false;
 157+ return in_array( $value, array( 1, 0, '1', '0', 'true', 'false', 'lossy', 'lossless' ) );
160158 }
161 - if ( $value <= 0 || $value > 65535 ) { ## ImageMagick läuft bei einer Seitenangabe höher als 65535 in einen Overflow. ^SU
 159+ else if ( $value <= 0 || $value > 65535 ) { // ImageMagick hits an overflow for values over 65536
162160 return false;
163161 } else {
164162 return true;
@@ -168,20 +166,20 @@
169167 }
170168
171169 /**
172 - * creates parameter string for file name.
173 - * page number was added.
 170+ * Creates parameter string for file name.
 171+ * Page number was added.
174172 */
175173 function makeParamString( $params ) {
176174 $page = isset( $params['page'] ) ? $params['page'] : 1;
177175 if ( !isset( $params['width'] ) ) {
178176 return false;
179177 }
180 - $lossy = isset( $params['lossy'] ) && $params['lossy'] ? 'lossy' : 'lossless';
 178+ $lossy = isset( $params['lossy'] ) && $params['lossy'] ? 'lossy' : 'lossless';
181179 return "{$lossy}-page{$page}-{$params['width']}px";
182180 }
183181
184182 /**
185 - * parses parameter string into an array.
 183+ * Parses parameter string into an array.
186184 */
187185 function parseParamString( $str ) {
188186 $m = false;
@@ -193,24 +191,24 @@
194192 }
195193
196194 /**
197 - * lossy-paramter added
 195+ * Lossy parameter added
198196 * TODO: The purpose of this function is not yet fully clear.
199197 */
200198 function getScriptParams( $params ) { ## wtf?? ^DK
 199+ // FIXME: This function is unused, seems to be useless,
 200+ // and could be replaced with an array_intersect() call
201201 return array(
202 - 'width' => $params['width'],
203 - 'page' => $params['page'],
204 - 'lossy' => $params['lossy'],
 202+ 'width' => $params['width'],
 203+ 'page' => $params['page'],
 204+ 'lossy' => $params['lossy'],
205205 );
206206 }
207207
208208 /**
209 - * prepares param array and sets standard values
210 - * standard values for page and lossy are added
 209+ * Prepares param array and sets standard values.
 210+ * Standard values for page and lossy are added.
211211 */
212212 function normaliseParams( $image, &$params ) {
213 - global $wgImageMagickIdentifyCommand;
214 -
215213 $mimeType = $image->getMimeType();
216214
217215 if ( !isset( $params['width'] ) ) {
@@ -223,17 +221,16 @@
224222 $params['page'] = $this->pageCount( $image );
225223 }
226224 if ( !isset( $params['lossy'] ) ) {
227 - $params['lossy'] = NULL;
 225+ $params['lossy'] = null;
228226 }
229 - $size = PagedTiffImage::getPageSize($this->getMetaArray($image), $params['page']);
 227+ $size = PagedTiffImage::getPageSize($this->getMetaArray($image), $params['page']);
230228 $srcWidth = $size['width'];
231229 $srcHeight = $size['height'];
232230
233231 if ( isset( $params['height'] ) && $params['height'] != -1 ) {
234 - ## Wenn das Bild im Hochformat ist und der Height-Parameter angegeben ist,
235 - ## wird der Width-Parameter angepasst, sofern width x height die Original-Ratio
236 - ## zerstören würden. Dies dient der Paging-Vorschau auf der ImagePage,
237 - ## damit die Seiten-Vorschauen nicht das Seiten-Layout zerstören. ^SU
 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
238235 if ( $params['width'] * $srcHeight > $params['height'] * $srcWidth ) {
239236 $params['width'] = wfFitBoxWidth( $srcWidth, $srcHeight, $params['height'] );
240237 }
@@ -259,22 +256,22 @@
260257 if ( !$metadata ) {
261258 if($metadata == -1) {
262259 return $this->doThumbError( @$params['width'], @$params['height'], 'tiff_error_cached' ); ##test this ^DK
263 - // $wgCacheType = CACHE_DB
 260+ // $wgCacheType = CACHE_DB
264261 }
265262 return $this->doThumbError( @$params['width'], @$params['height'], 'tiff_no_metadata' );
266263 }
267264 if ( !$this->normaliseParams( $image, $params ) )
268265 return new TransformParameterError( $params );
269266
270 - ## get params an forces width, height and page to be integer
271 - $width = $params['width'] - 0;
272 - $height = $params['height'] - 0;
 267+ // Get params and force width, height and page to be integers
 268+ $width = intval( $params['width'] );
 269+ $height = intval( $params['height'] );
273270 $srcPath = $image->getPath();
274 - $page = $params['page'] - 0;
 271+ $page = intval( $params['page'] );
275272
276273 $extension = $this->getThumbExtension($image, $page, $params['lossy']);
277 - $dstPath .= $extension;
278 - $dstUrl .= $extension;
 274+ $dstPath .= $extension;
 275+ $dstUrl .= $extension;
279276
280277 $meta = unserialize($metadata);
281278
@@ -284,7 +281,7 @@
285282
286283 if ( is_file($dstPath) )
287284 return new ThumbnailImage( $image, $dstUrl, $width,
288 - $height, $dstPath, $page );
 285+ $height, $dstPath, $page );
289286
290287 if(isset($meta['page_data'][$page]['pixels']) && $meta['page_data'][$page]['pixels'] > $wgTiffMaxEmbedFileResolution)
291288 return $this->doThumbError( $width, $height, 'tiff_sourcefile_too_large' );
@@ -319,9 +316,8 @@
320317 $removed = $this->removeBadFile( $dstPath, $retval );
321318
322319 if ( $retval != 0 || $removed ) {
323 - wfDebugLog( 'thumbnail',
324 - sprintf( 'thumbnail failed on %s: error %d "%s" from "%s"',
325 - wfHostname(), $retval, trim($err), $cmd ) );
 320+ wfDebugLog( 'thumbnail', "thumbnail failed on " . wfHostname() .
 321+ "; error $retval \"$err\" from \"$cmd\"" );
326322 return new MediaTransformError( 'thumbnail_error', $width, $height, $err );
327323 } else {
328324 return new ThumbnailImage( $image, $dstUrl, $width, $height, $dstPath, $page );
@@ -330,12 +326,12 @@
331327
332328 /**
333329 * Decides (taking lossy parameter into account) the filetype of the thumbnail.
334 - * If there is no lossy-Parameter (NULL = not set), the decision is being made
 330+ * If there is no lossy parameter (null = not set), the decision is made
335331 * according to the presence of an alpha value.
336332 * (alpha == true = png, alpha == false = jpg)
337333 */
338334 function getThumbExtension( $image, $page, $lossy ) {
339 - if($lossy === NULL) {
 335+ if($lossy === null) {
340336 $data = $this->getMetaArray($image);
341337 if((strtolower($data['page_data'][$page]['alpha']) == 'true')) {
342338 return '.png';
@@ -369,7 +365,7 @@
370366 protected function doThumbError( $width, $height, $msg ) {
371367 wfLoadExtensionMessages( 'PagedTiffHandler' );
372368 return new MediaTransformError( 'thumbnail_error',
373 - $width, $height, wfMsg($msg) );
 369+ $width, $height, wfMsg($msg) );
374370 }
375371
376372 /**
@@ -519,12 +515,14 @@
520516 }
521517
522518 /**
523 - * Returns a PagedTiffImage or create a new one if don´t exist.
 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
524522 */
525523 static function getTiffImage( $image, $path ) {
526 - ## Wenn kein ImageObject übergeben wurde, wird ein TiffImage-Object anhand des Pfades erzeugt.
527 - ## Ist ein ImageObject vorhanden, wird geprüft, ob darin schon eine TiffImage-Instanz gespeichert wurde.
528 - ## Ist dies nicht der Fall, wird eine Instanz erstellt und im ImageObject gespeichert. ^SU
 524+ // If no Image object is passed, a TiffImage is created based on $path .
 525+ // If there is an Image object, we check whether there's already a TiffImage instance in there;
 526+ // if not, a new instance is created and stored in the Image object
529527 if ( !$image )
530528 $tiffimg = new PagedTiffImage( $path );
531529 elseif ( !isset( $image->tiffImage ) )
Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler.php
@@ -1,6 +1,6 @@
22 <?php
33 /**
4 - * Copyright (C) Wikimedia Deuschland, 2009
 4+ * Copyright (C) Wikimedia Deutschland, 2009
55 * Authors Hallo Welt! Medienwerkstatt GmbH
66 * Authors Sebastian Ulbricht, Daniel Lynge, Marc Reymann, Markus Glaser
77 *
@@ -21,14 +21,13 @@
2222 *
2323 */
2424
25 -//error_reporting(E_ALL);
2625 # Not a valid entry point, skip unless MEDIAWIKI is defined
2726 if (!defined('MEDIAWIKI')) {
2827 echo "PagedTiffHandler extension";
2928 exit(1);
3029 }
3130
32 -/** Add to LocalSettings.php
 31+/* Add to LocalSettings.php
3332 require_once("$IP/extensions/PagedTiffHandler/PagedTiffHandler.php");
3433
3534 $wgUseImageMagick = true;
@@ -36,10 +35,8 @@
3736 $wgImageMagickIdentifyCommand = "C:\Program Files\ImageMagick-6.5.6-Q8\identify";
3837 $wgTiffExivCommand = "C:\Program Files\Exiv2\exiv2";
3938 $wgMaxUploadSize = 1073741824;
 39+*/
4040
41 -
42 - */
43 -
4441 $wgExtensionCredits['media'][] = array(
4542 'path' => __FILE__,
4643 'name' => 'PagedTiffHandler',
@@ -92,9 +89,9 @@
9390 $wgTiffMaxEmbedFiles = 10000;
9491 // Maximum resolution of embedded images (product of width x height pixels)
9592 $wgTiffMaxEmbedFileResolution = 25600000; // max. Resolution 1600 x 1600 pixels
96 -// Maximum size of meta data
 93+// Maximum size of metadata
9794 $wgTiffMaxMetaSize = 67108864; // 64kB
98 -// TTL of Cacheentries for Errors
 95+// TTL of cache entries for errors
9996 $wgTiffErrorCacheTTL = 84600;
10097
10198 $wgFileExtensions[] = 'tiff';
@@ -102,11 +99,11 @@
103100
104101 $dir = dirname(__FILE__) . '/';
105102 $wgExtensionMessagesFiles['PagedTiffHandler'] = $dir . 'PagedTiffHandler.i18n.php';
106 -$wgExtensionMessagesFiles['PagedTiffImage'] = $dir . 'PagedTiffHandler.i18n.php';
107103 $wgAutoloadClasses['PagedTiffImage'] = $dir . 'PagedTiffHandler.image.php';
108104 $wgAutoloadClasses['PagedTiffHandler'] = $dir . 'PagedTiffHandler_body.php';
 105+$wgAutoloadClasses['TiffReader'] = $dir . 'TiffReader.php';
109106 $wgMediaHandlers['image/tiff'] = 'PagedTiffHandler';
110107 $wgHooks['UploadVerification'][] = 'PagedTiffHandler::check';
111108 $wgHooks['LanguageGetMagic'][] = 'PagedTiffHandler::addTiffLossyMagicWordLang';
112109 $wgHooks['PagedTiffHandlerRenderCommand'][] = 'PagedTiffHandler::renderCommand';
113 -$wgHooks['PagedTiffHandlerExivCommand'][] = 'PagedTiffImage::exivCommand';
\ No newline at end of file
 110+$wgHooks['PagedTiffHandlerExivCommand'][] = 'PagedTiffImage::exivCommand';

Status & tagging log