Index: trunk/phase3/tests/phpunit/includes/media/XMPValidateTest.php |
— | — | @@ -0,0 +1,47 @@ |
| 2 | +<?php |
| 3 | +class XMPTest extends MediaWikiTestCase { |
| 4 | + |
| 5 | + /** |
| 6 | + * @dataProvider providerDate |
| 7 | + */ |
| 8 | + function testValidateDate( $value, $expected ) { |
| 9 | + // The method should modify $value. |
| 10 | + XMPValidate::validateDate( array(), $value, true ); |
| 11 | + $this->assertEquals( $expected, $value ); |
| 12 | + } |
| 13 | + |
| 14 | + function providerDate() { |
| 15 | + /* For reference valid date formats are: |
| 16 | + * YYYY |
| 17 | + * YYYY-MM |
| 18 | + * YYYY-MM-DD |
| 19 | + * YYYY-MM-DDThh:mmTZD |
| 20 | + * YYYY-MM-DDThh:mm:ssTZD |
| 21 | + * YYYY-MM-DDThh:mm:ss.sTZD |
| 22 | + * (Time zone is optional) |
| 23 | + */ |
| 24 | + return array( |
| 25 | + array( '1992', '1992' ), |
| 26 | + array( '1992-04', '1992:04' ), |
| 27 | + array( '1992-02-01', '1992:02:01' ), |
| 28 | + array( '2011-09-29', '2011:09:29' ), |
| 29 | + array( '1982-12-15T20:12', '1982:12:15 20:12' ), |
| 30 | + array( '1982-12-15T20:12Z', '1982:12:15 20:12' ), |
| 31 | + array( '1982-12-15T20:12+02:30', '1982:12:15 22:42' ), |
| 32 | + array( '1982-12-15T01:12-02:30', '1982:12:14 22:42' ), |
| 33 | + array( '1982-12-15T20:12:11', '1982:12:15 20:12:11' ), |
| 34 | + array( '1982-12-15T20:12:11Z', '1982:12:15 20:12:11' ), |
| 35 | + array( '1982-12-15T20:12:11+01:10', '1982:12:15 21:22:11' ), |
| 36 | + array( '2045-12-15T20:12:11', '2045:12:15 20:12:11' ), |
| 37 | + array( '1867-06-01T15:00:00', '1867:06:01 15:00:00' ), |
| 38 | + /* some invalid ones */ |
| 39 | + array( '2001--12', null ), |
| 40 | + array( '2001-5-12', null ), |
| 41 | + array( '2001-5-12TZ', null ), |
| 42 | + array( '2001-05-12T15', null ), |
| 43 | + array( '2001-12T15:13', null ), |
| 44 | + ); |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | +} |
Property changes on: trunk/phase3/tests/phpunit/includes/media/XMPValidateTest.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 49 | + native |
Index: trunk/phase3/includes/media/FormatMetadata.php |
— | — | @@ -233,10 +233,19 @@ |
234 | 234 | if ( $val == '0000:00:00 00:00:00' || $val == ' : : : : ' ) { |
235 | 235 | $val = wfMsg( 'exif-unknowndate' ); |
236 | 236 | } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D', $val ) ) { |
| 237 | + // Full date. |
237 | 238 | $time = wfTimestamp( TS_MW, $val ); |
238 | 239 | if ( $time && intval( $time ) > 0 ) { |
239 | 240 | $val = $wgLang->timeanddate( $time ); |
240 | 241 | } |
| 242 | + } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d)$/D', $val ) ) { |
| 243 | + // No second field. Still format the same |
| 244 | + // since timeanddate doesn't include seconds anyways, |
| 245 | + // but second still available in api |
| 246 | + $time = wfTimestamp( TS_MW, $val . ':00' ); |
| 247 | + if ( $time && intval( $time ) > 0 ) { |
| 248 | + $val = $wgLang->timeanddate( $time ); |
| 249 | + } |
241 | 250 | } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) { |
242 | 251 | // If only the date but not the time is filled in. |
243 | 252 | $time = wfTimestamp( TS_MW, substr( $val, 0, 4 ) |
Index: trunk/phase3/includes/media/XMPValidate.php |
— | — | @@ -201,10 +201,20 @@ |
202 | 202 | } |
203 | 203 | |
204 | 204 | /** |
205 | | - * function to validate date properties, and convert to Exif format. |
| 205 | + * function to validate date properties, and convert to (partial) Exif format. |
206 | 206 | * |
| 207 | + * Dates can be one of the following formats: |
| 208 | + * YYYY |
| 209 | + * YYYY-MM |
| 210 | + * YYYY-MM-DD |
| 211 | + * YYYY-MM-DDThh:mmTZD |
| 212 | + * YYYY-MM-DDThh:mm:ssTZD |
| 213 | + * YYYY-MM-DDThh:mm:ss.sTZD |
| 214 | + * |
207 | 215 | * @param $info Array information about current property |
208 | 216 | * @param &$val Mixed current value to validate. Converts to TS_EXIF as a side-effect. |
| 217 | + * in cases where there's only a partial date, it will give things like |
| 218 | + * 2011:04. |
209 | 219 | * @param $standalone Boolean if this is a simple property or array |
210 | 220 | */ |
211 | 221 | public static function validateDate( $info, &$val, $standalone ) { |
— | — | @@ -240,26 +250,42 @@ |
241 | 251 | $val = null; |
242 | 252 | return; |
243 | 253 | } |
244 | | - //if month, etc unspecified, full out as 01. |
245 | | - $res[2] = isset( $res[2] ) ? $res[2] : '01'; //month |
246 | | - $res[3] = isset( $res[3] ) ? $res[3] : '01'; //day |
| 254 | + |
247 | 255 | if ( !isset( $res[4] ) ) { //hour |
248 | | - //just have the year month day |
249 | | - $val = $res[1] . ':' . $res[2] . ':' . $res[3]; |
| 256 | + //just have the year month day (if that) |
| 257 | + $val = $res[1]; |
| 258 | + if ( isset( $res[2] ) ) { |
| 259 | + $val .= ':' . $res[2]; |
| 260 | + } |
| 261 | + if ( isset( $res[3] ) ) { |
| 262 | + $val .= ':' . $res[3]; |
| 263 | + } |
250 | 264 | return; |
251 | 265 | } |
252 | | - //if hour is set, so is minute or regex above will fail. |
253 | | - //Extra check for empty string necessary due to TZ but no second case. |
254 | | - $res[6] = isset( $res[6] ) && $res[6] != '' ? $res[6] : '00'; |
255 | 266 | |
256 | 267 | if ( !isset( $res[7] ) || $res[7] === 'Z' ) { |
| 268 | + //if hour is set, then minute must also be or regex above will fail. |
257 | 269 | $val = $res[1] . ':' . $res[2] . ':' . $res[3] |
258 | | - . ' ' . $res[4] . ':' . $res[5] . ':' . $res[6]; |
| 270 | + . ' ' . $res[4] . ':' . $res[5]; |
| 271 | + if ( isset( $res[6] ) && $res[6] !== '' ) { |
| 272 | + $val .= ':' . $res[6]; |
| 273 | + } |
259 | 274 | return; |
260 | 275 | } |
261 | 276 | |
262 | | - //do timezone processing. We've already done the case that tz = Z. |
263 | 277 | |
| 278 | + // Extra check for empty string necessary due to TZ but no second case. |
| 279 | + $stripSeconds = false; |
| 280 | + if ( !isset( $res[6] ) || $res[6] === '' ) { |
| 281 | + $res[6] = '00'; |
| 282 | + $stripSeconds = true; |
| 283 | + } |
| 284 | + |
| 285 | + // Do timezone processing. We've already done the case that tz = Z. |
| 286 | + |
| 287 | + // We know that if we got to this step, year, month day hour and min must be set |
| 288 | + // by virtue of regex not failing. |
| 289 | + |
264 | 290 | $unix = wfTimestamp( TS_UNIX, $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6] ); |
265 | 291 | $offset = intval( substr( $res[7], 1, 2 ) ) * 60 * 60; |
266 | 292 | $offset += intval( substr( $res[7], 4, 2 ) ) * 60; |
— | — | @@ -267,6 +293,11 @@ |
268 | 294 | $offset = -$offset; |
269 | 295 | } |
270 | 296 | $val = wfTimestamp( TS_EXIF, $unix + $offset ); |
| 297 | + |
| 298 | + if ( $stripSeconds ) { |
| 299 | + // If seconds weren't specified, remove the trailing ':00'. |
| 300 | + $val = substr( $val, 0, -3 ); |
| 301 | + } |
271 | 302 | } |
272 | 303 | |
273 | 304 | } |