r62701 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62700‎ | r62701 | r62702 >
Date:11:31, 19 February 2010
Author:mglaser
Status:deferred (Comments)
Tags:
Comment:
Check ImageMagick-Parameter %r in addition to %A
Modified paths:
  • /trunk/extensions/PagedTiffHandler/PagedTiffHandler.image.php (modified) (history)

Diff [purge]

Index: trunk/extensions/PagedTiffHandler/PagedTiffHandler.image.php
@@ -29,215 +29,230 @@
3030 */
3131
3232 class PagedTiffImage {
33 - protected $_meta = NULL;
34 - protected $mFilename;
35 -
36 - function __construct( $filename ) {
37 - $this->mFilename = $filename;
38 - }
39 -
40 - /**
41 - * Customize the exiv shell command.
42 - */
43 - static function exivCommand( &$cmd, $filename ) {
44 - return true;
45 - }
46 -
47 - /**
48 - * Called by MimeMagick functions.
49 - */
50 - public function isValid() {
51 - return count($this->retrieveMetaData());
52 - }
53 -
54 - /**
55 - * Returns an array that corresponds to the native PHP function getimagesize().
56 - */
57 - public function getImageSize() {
58 - $data = $this->retrieveMetaData();
59 - $size = $this->getPageSize( $data, 1 );
60 -
61 - if( $size ) {
62 - $width = $size['width'];
63 - $height = $size['height'];
64 - return array( $width, $height, 'Tiff',
65 - "width=\"$width\" height=\"$height\"" );
66 - }
67 - return false;
68 - }
69 -
70 - /**
71 - * Returns an array with width and height of the tiff page.
72 - */
73 - public static function getPageSize( $data, $page ) {
74 - if( isset( $data['pages'][$page] ) ) {
75 - return array('width' => $data['pages'][$page]['width'],
76 - 'height' => $data['pages'][$page]['height']);
77 - }
78 - return false;
79 - }
80 -
81 - /**
82 - * Reads metadata of the tiff file via shell command and returns an associative array.
83 - * layout:
84 - * meta['Pages'] = amount of pages
85 - * meta['pages'] = metadata per page
86 - * meta['exif'] = Exif, XMP and IPTC
87 - * meta['errors'] = identify-errors
88 - * meta['warnings'] = identify-warnings
89 - */
90 - public function retrieveMetaData() {
91 - global $wgImageMagickIdentifyCommand, $wgTiffExivCommand, $wgTiffUseExiv, $wgMemc, $wgTiffErrorCacheTTL;
92 -
93 - $imgKey = wfMemcKey('PagedTiffHandler-ThumbnailGeneration', $this->mFilename);
94 - $isCached = $wgMemc->get($imgKey);
95 - if($isCached) {
96 - return -1;
97 - }
98 - $wgMemc->add($imgKey, 1, $wgTiffErrorCacheTTL);
99 -
100 - if($this->_meta === NULL) {
101 - if ( $wgImageMagickIdentifyCommand ) {
102 -
103 - wfProfileIn( 'PagedTiffImage' );
104 - /**
105 - * ImageMagick is used in order to get the basic metadata of embedded files.
106 - * This is not reliable in exiv2m since it is not possible to name a set of required fields.
107 - */
108 - $cmd = wfEscapeShellArg( $wgImageMagickIdentifyCommand ) .
109 - ' -format "[BEGIN]page=%p\nalpha=%A\nheight=%h\nwidth=%w\ndepth=%z[END]" ' .
110 - wfEscapeShellArg( $this->mFilename ) . ' 2>&1';
111 -
112 - wfProfileIn( 'identify' );
113 - wfDebug( __METHOD__.": $cmd\n" );
114 - $dump = wfShellExec( $cmd, $retval );
115 - wfProfileOut( 'identify' );
116 - if($retval) {
117 - return false;
118 - }
119 - $this->_meta = $this->convertDumpToArray( $dump );
120 - $this->_meta['exif'] = array();
121 -
122 - if($wgTiffUseExiv) {
123 - $cmd = wfEscapeShellArg( $wgTiffExivCommand ) .
124 - ' -u -psix -Pnt ' . // read EXIF, XMP, IPTC as name-tag => interpreted data -ignore unknown fields
125 - // exiv2-doc @link http://www.exiv2.org/sample.html
126 - wfEscapeShellArg( $this->mFilename );
127 -
128 - wfRunHooks( "PagedTiffHandlerExivCommand", array( &$cmd, $this->mFilename ) );
129 -
130 - wfProfileIn( 'exiv2' );
131 - wfDebug( __METHOD__.": $cmd\n" );
132 - $dump = wfShellExec( $cmd, $retval );
133 - wfProfileOut( 'exiv2' );
134 - $result = array();
135 - preg_match_all('/(\w+)\s+(.+)/', $dump, $result, PREG_SET_ORDER);
136 -
137 - foreach($result as $data) {
138 - $this->_meta['exif'][$data[1]] = $data[2];
139 - }
140 - }
141 - else {
142 - $cmd = wfEscapeShellArg( $wgImageMagickIdentifyCommand ) .
143 - ' -verbose ' .
144 - wfEscapeShellArg( $this->mFilename )."[0]";
145 -
146 - wfProfileIn( 'identify -verbose' );
147 - wfDebug( __METHOD__.": $cmd\n" );
148 - $dump = wfShellExec( $cmd, $retval );
149 - wfProfileOut( 'identify -verbose' );
150 - $this->_meta['exif'] = $this->parseVerbose($dump);
151 - }
152 - wfProfileOut( 'PagedTiffImage' );
153 - }
154 - }
155 - unset($this->_meta['exif']['Image']);
156 - unset($this->_meta['exif']['filename']);
157 - unset($this->_meta['exif']['Base filename']);
158 - return $this->_meta;
159 - }
160 -
161 - /**
162 - * helper function of retrieveMetaData().
163 - * parses shell return from identify-command into an array.
164 - */
165 - protected function convertDumpToArray( $dump ) {
166 - global $wgTiffIdentifyRejectMessages, $wgTiffIdentifyBypassMessages;
167 - if ( strval( $dump ) == '' ) return false;
168 - $infos = NULL;
169 - preg_match_all('/\[BEGIN\](.+?)\[END\]/si', $dump, $infos, PREG_SET_ORDER);
170 - $data = array();
171 - $data['Pages'] = count($infos);
172 - $data['pages'] = array();
173 - foreach($infos as $info) {
174 - $entry = array();
175 - $lines = explode("\n", $info[1]);
176 - foreach($lines as $line) {
177 - if(trim($line) == '') {
178 - continue;
179 - }
180 - $parts = explode('=', $line);
181 - $entry[trim($parts[0])] = trim($parts[1]);
182 - }
183 - $entry['pixels'] = $entry['height'] * $entry['width'];
184 - $data['pages'][$entry['page']] = $entry;
185 - }
186 -
187 - $dump = preg_replace('/\[BEGIN\](.+?)\[END\]/si', '', $dump);
188 - if(strlen($dump)) {
189 - $errors = explode("\n", $dump);
190 - foreach($errors as $error) {
191 - $knownError = false;
192 - foreach($wgTiffIdentifyRejectMessages as $msg) {
193 - if (preg_match($msg, trim($error))) {
194 - $data['errors'][] = $error;
195 - $knownError = true;
196 - break;
197 - }
198 - }
199 - if(!$knownError) {
200 - foreach($wgTiffIdentifyBypassMessages as $msg) {
201 - if (preg_match($msg, trim($error))) {
202 - $data['warnings'][] = $error;
203 - $knownError = true;
204 - break;
205 - }
206 - }
207 - }
208 - if(!$knownError) {
209 - $data['unknownErrors'][] = $error;
210 - }
211 - }
212 - }
213 - return $data;
214 - }
215 -
216 - /**
217 - * helper function of retrieveMetaData().
218 - * parses shell return from identify-verbose-command into an array.
219 - */
220 - protected function parseVerbose($dump) {
221 - $data = array();
222 - $dump = explode("\n", $dump);
223 - $lastwhite = 0;
224 - $lastkey = false;
225 - foreach($dump as $line) {
226 - if(preg_match('/^(\s*?)(\w([\w\s]+?)?):(.*?)$/sim', $line, $res)) {
227 - if($lastwhite == 0 || strlen($res[1]) == $lastwhite) {
228 - if(strlen(trim($res[4]))) {
229 - $data[trim($res[2])] = trim($res[4]);
230 - }
231 - else {
232 - $data[trim($res[2])] = " Data:\n";
233 - }
234 - $lastkey = trim($res[2]);
235 - $lastwhite = strlen($res[1]);
236 - }
237 - else {
238 - $data[$lastkey] .= $line."\n";
239 - }
240 - }
241 - }
242 - return $data;
243 - }
 33+ protected $_meta = NULL;
 34+ protected $mFilename;
 35+
 36+ function __construct( $filename ) {
 37+ $this->mFilename = $filename;
 38+ }
 39+
 40+ /**
 41+ * Customize the exiv shell command.
 42+ */
 43+ static function exivCommand( &$cmd, $filename ) {
 44+ return true;
 45+ }
 46+
 47+ /**
 48+ * Called by MimeMagick functions.
 49+ */
 50+ public function isValid() {
 51+ return count($this->retrieveMetaData());
 52+ }
 53+
 54+ /**
 55+ * Returns an array that corresponds to the native PHP function getimagesize().
 56+ */
 57+ public function getImageSize() {
 58+ $data = $this->retrieveMetaData();
 59+ $size = $this->getPageSize( $data, 1 );
 60+
 61+ if( $size ) {
 62+ $width = $size['width'];
 63+ $height = $size['height'];
 64+ return array( $width, $height, 'Tiff',
 65+ "width=\"$width\" height=\"$height\"" );
 66+ }
 67+ return false;
 68+ }
 69+
 70+ /**
 71+ * Returns an array with width and height of the tiff page.
 72+ */
 73+ public static function getPageSize( $data, $page ) {
 74+ if( isset( $data['pages'][$page] ) ) {
 75+ return array('width' => $data['pages'][$page]['width'],
 76+ 'height' => $data['pages'][$page]['height']);
 77+ }
 78+ return false;
 79+ }
 80+
 81+ /**
 82+ * Reads metadata of the tiff file via shell command and returns an associative array.
 83+ * layout:
 84+ * meta['Pages'] = amount of pages
 85+ * meta['pages'] = metadata per page
 86+ * meta['exif'] = Exif, XMP and IPTC
 87+ * meta['errors'] = identify-errors
 88+ * meta['warnings'] = identify-warnings
 89+ */
 90+ public function retrieveMetaData() {
 91+ global $wgImageMagickIdentifyCommand, $wgTiffExivCommand, $wgTiffUseExiv, $wgMemc, $wgTiffErrorCacheTTL;
 92+
 93+ $imgKey = wfMemcKey('PagedTiffHandler-ThumbnailGeneration', $this->mFilename);
 94+ $isCached = $wgMemc->get($imgKey);
 95+ if($isCached) {
 96+ return -1;
 97+ }
 98+ $wgMemc->add($imgKey, 1, $wgTiffErrorCacheTTL);
 99+
 100+ if($this->_meta === NULL) {
 101+ if ( $wgImageMagickIdentifyCommand ) {
 102+
 103+ wfProfileIn( 'PagedTiffImage' );
 104+ /**
 105+ * ImageMagick is used in order to get the basic metadata of embedded files.
 106+ * This is not reliable in exiv2m since it is not possible to name a set of required fields.
 107+ */
 108+ $cmd = wfEscapeShellArg( $wgImageMagickIdentifyCommand ) .
 109+ ' -format "[BEGIN]page=%p\nalpha=%A\nalpha2=%r\nheight=%h\nwidth=%w\ndepth=%z[END]" ' .
 110+ wfEscapeShellArg( $this->mFilename ) . ' 2>&1';
 111+
 112+ wfProfileIn( 'identify' );
 113+ wfDebug( __METHOD__.": $cmd\n" );
 114+ $dump = wfShellExec( $cmd, $retval );
 115+ wfProfileOut( 'identify' );
 116+ if($retval) {
 117+ return false;
 118+ }
 119+ $this->_meta = $this->convertDumpToArray( $dump );
 120+ $this->_meta['exif'] = array();
 121+
 122+ if($wgTiffUseExiv) {
 123+ $cmd = wfEscapeShellArg( $wgTiffExivCommand ) .
 124+ ' -u -psix -Pnt ' . // read EXIF, XMP, IPTC as name-tag => interpreted data -ignore unknown fields
 125+ // exiv2-doc @link http://www.exiv2.org/sample.html
 126+ wfEscapeShellArg( $this->mFilename );
 127+
 128+ wfRunHooks( "PagedTiffHandlerExivCommand", array( &$cmd, $this->mFilename ) );
 129+
 130+ wfProfileIn( 'exiv2' );
 131+ wfDebug( __METHOD__.": $cmd\n" );
 132+ $dump = wfShellExec( $cmd, $retval );
 133+ wfProfileOut( 'exiv2' );
 134+ $result = array();
 135+ preg_match_all('/(\w+)\s+(.+)/', $dump, $result, PREG_SET_ORDER);
 136+
 137+ foreach($result as $data) {
 138+ $this->_meta['exif'][$data[1]] = $data[2];
 139+ }
 140+ }
 141+ else {
 142+ $cmd = wfEscapeShellArg( $wgImageMagickIdentifyCommand ) .
 143+ ' -verbose ' .
 144+ wfEscapeShellArg( $this->mFilename )."[0]";
 145+
 146+ wfProfileIn( 'identify -verbose' );
 147+ wfDebug( __METHOD__.": $cmd\n" );
 148+ $dump = wfShellExec( $cmd, $retval );
 149+ wfProfileOut( 'identify -verbose' );
 150+ $this->_meta['exif'] = $this->parseVerbose($dump);
 151+ }
 152+ wfProfileOut( 'PagedTiffImage' );
 153+ }
 154+ }
 155+ unset($this->_meta['exif']['Image']);
 156+ unset($this->_meta['exif']['filename']);
 157+ unset($this->_meta['exif']['Base filename']);
 158+ return $this->_meta;
 159+ }
 160+
 161+ /**
 162+ * helper function of retrieveMetaData().
 163+ * parses shell return from identify-command into an array.
 164+ */
 165+ protected function convertDumpToArray( $dump ) {
 166+ global $wgTiffIdentifyRejectMessages, $wgTiffIdentifyBypassMessages;
 167+ if ( strval( $dump ) == '' ) return false;
 168+ $infos = NULL;
 169+ preg_match_all('/\[BEGIN\](.+?)\[END\]/si', $dump, $infos, PREG_SET_ORDER);
 170+ $data = array();
 171+ $data['Pages'] = count($infos);
 172+ $data['pages'] = array();
 173+ foreach($infos as $info) {
 174+ $entry = array();
 175+ $lines = explode("\n", $info[1]);
 176+ foreach($lines as $line) {
 177+ if(trim($line) == '') {
 178+ continue;
 179+ }
 180+ $parts = explode('=', $line);
 181+ if(trim($parts[0]) == 'alpha' && trim($parts[1]) == '%A') {
 182+ continue;
 183+ }
 184+ if(trim($parts[0]) == 'alpha2' && !isset($entry['alpha'])) {
 185+ switch(trim($parts[1])) {
 186+ case 'DirectClassRGBMatte':
 187+ case 'DirectClassRGBA':
 188+ $entry['alpha'] = 'true';
 189+ break;
 190+ default:
 191+ $entry['alpha'] = 'false';
 192+ break;
 193+ }
 194+ continue;
 195+ }
 196+ $entry[trim($parts[0])] = trim($parts[1]);
 197+ }
 198+ $entry['pixels'] = $entry['height'] * $entry['width'];
 199+ $data['pages'][$entry['page']] = $entry;
 200+ }
 201+
 202+ $dump = preg_replace('/\[BEGIN\](.+?)\[END\]/si', '', $dump);
 203+ if(strlen($dump)) {
 204+ $errors = explode("\n", $dump);
 205+ foreach($errors as $error) {
 206+ $knownError = false;
 207+ foreach($wgTiffIdentifyRejectMessages as $msg) {
 208+ if (preg_match($msg, trim($error))) {
 209+ $data['errors'][] = $error;
 210+ $knownError = true;
 211+ break;
 212+ }
 213+ }
 214+ if(!$knownError) {
 215+ foreach($wgTiffIdentifyBypassMessages as $msg) {
 216+ if (preg_match($msg, trim($error))) {
 217+ $data['warnings'][] = $error;
 218+ $knownError = true;
 219+ break;
 220+ }
 221+ }
 222+ }
 223+ if(!$knownError) {
 224+ $data['unknownErrors'][] = $error;
 225+ }
 226+ }
 227+ }
 228+ return $data;
 229+ }
 230+
 231+ /**
 232+ * helper function of retrieveMetaData().
 233+ * parses shell return from identify-verbose-command into an array.
 234+ */
 235+ protected function parseVerbose($dump) {
 236+ $data = array();
 237+ $dump = explode("\n", $dump);
 238+ $lastwhite = 0;
 239+ $lastkey = false;
 240+ foreach($dump as $line) {
 241+ if(preg_match('/^(\s*?)(\w([\w\s]+?)?):(.*?)$/sim', $line, $res)) {
 242+ if($lastwhite == 0 || strlen($res[1]) == $lastwhite) {
 243+ if(strlen(trim($res[4]))) {
 244+ $data[trim($res[2])] = trim($res[4]);
 245+ }
 246+ else {
 247+ $data[trim($res[2])] = " Data:\n";
 248+ }
 249+ $lastkey = trim($res[2]);
 250+ $lastwhite = strlen($res[1]);
 251+ }
 252+ else {
 253+ $data[$lastkey] .= $line."\n";
 254+ }
 255+ }
 256+ }
 257+ return $data;
 258+ }
244259 }

Comments

#Comment by 😂 (talk | contribs)   11:41, 19 February 2010

Please keep whitespace fixes and functional changes in separate commits. Because you decided to fix the spaces/tabs in the entire file, the diff is absolutely useless.

#Comment by Tim Starling (talk | contribs)   10:12, 22 February 2010

Using svn diff -x-w gives you a nice short diff, but I'll agree that that is inconvenient.

Index: PagedTiffHandler.image.php
===================================================================
--- PagedTiffHandler.image.php	(revision 62700)
+++ PagedTiffHandler.image.php	(revision 62701)
@@ -105,7 +105,7 @@
                  * This is not reliable in exiv2m since it is not possible to name a set of required fields.
                  */
                 $cmd = wfEscapeShellArg( $wgImageMagickIdentifyCommand ) .
-                    ' -format "[BEGIN]page=%p\nalpha=%A\nheight=%h\nwidth=%w\ndepth=%z[END]" ' .
+					' -format "[BEGIN]page=%p\nalpha=%A\nalpha2=%r\nheight=%h\nwidth=%w\ndepth=%z[END]" ' .
                     wfEscapeShellArg( $this->mFilename ) . ' 2>&1';
                 
                 wfProfileIn( 'identify' );
@@ -177,6 +177,21 @@
                     continue;
                 }
                 $parts = explode('=', $line);
+				if(trim($parts[0]) == 'alpha' && trim($parts[1]) == '%A') {
+					continue;
+				}
+				if(trim($parts[0]) == 'alpha2' && !isset($entry['alpha'])) {
+					switch(trim($parts[1])) {
+						case 'DirectClassRGBMatte':
+						case 'DirectClassRGBA':
+							$entry['alpha'] = 'true';
+							break;
+						default:
+							$entry['alpha'] = 'false';
+							break;
+					}
+					continue;
+				}
                 $entry[trim($parts[0])] = trim($parts[1]);
             }
             $entry['pixels'] = $entry['height'] * $entry['width'];

Status & tagging log