r99316 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99315‎ | r99316 | r99317 >
Date:18:40, 8 October 2011
Author:bawolff
Status:resolved (Comments)
Tags:
Comment:
Make partial dates in XMP not have the ommitted fields fulled out to 1's (reported by AVRS on irc).

Basically, in XMP you can specify partial date (for example 2011-04 for april 2011 with no day).
We were extracting that, and fulling out the day to 01 if it wasn't present (My original resoning when writing
the code was that the nice localized date functions need a full date, but that's a pretty poor rationale for displaying
wrong date). This changes it to just display such year-month things as YYYY:MM.
Modified paths:
  • /trunk/phase3/includes/media/FormatMetadata.php (modified) (history)
  • /trunk/phase3/includes/media/XMPValidate.php (modified) (history)
  • /trunk/phase3/tests/phpunit/includes/media/XMPValidateTest.php (added) (history)

Diff [purge]

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
149 + native
Index: trunk/phase3/includes/media/FormatMetadata.php
@@ -233,10 +233,19 @@
234234 if ( $val == '0000:00:00 00:00:00' || $val == ' : : : : ' ) {
235235 $val = wfMsg( 'exif-unknowndate' );
236236 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d) (?:\d\d):(?:\d\d):(?:\d\d)$/D', $val ) ) {
 237+ // Full date.
237238 $time = wfTimestamp( TS_MW, $val );
238239 if ( $time && intval( $time ) > 0 ) {
239240 $val = $wgLang->timeanddate( $time );
240241 }
 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+ }
241250 } elseif ( preg_match( '/^(?:\d{4}):(?:\d\d):(?:\d\d)$/D', $val ) ) {
242251 // If only the date but not the time is filled in.
243252 $time = wfTimestamp( TS_MW, substr( $val, 0, 4 )
Index: trunk/phase3/includes/media/XMPValidate.php
@@ -201,10 +201,20 @@
202202 }
203203
204204 /**
205 - * function to validate date properties, and convert to Exif format.
 205+ * function to validate date properties, and convert to (partial) Exif format.
206206 *
 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+ *
207215 * @param $info Array information about current property
208216 * @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.
209219 * @param $standalone Boolean if this is a simple property or array
210220 */
211221 public static function validateDate( $info, &$val, $standalone ) {
@@ -240,26 +250,42 @@
241251 $val = null;
242252 return;
243253 }
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+
247255 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+ }
250264 return;
251265 }
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';
255266
256267 if ( !isset( $res[7] ) || $res[7] === 'Z' ) {
 268+ //if hour is set, then minute must also be or regex above will fail.
257269 $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+ }
259274 return;
260275 }
261276
262 - //do timezone processing. We've already done the case that tz = Z.
263277
 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+
264290 $unix = wfTimestamp( TS_UNIX, $res[1] . $res[2] . $res[3] . $res[4] . $res[5] . $res[6] );
265291 $offset = intval( substr( $res[7], 1, 2 ) ) * 60 * 60;
266292 $offset += intval( substr( $res[7], 4, 2 ) ) * 60;
@@ -267,6 +293,11 @@
268294 $offset = -$offset;
269295 }
270296 $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+ }
271302 }
272303
273304 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r99318follow-up r99316 - Fix class name in unit test class.bawolff19:00, 8 October 2011

Comments

#Comment by RobLa-WMF (talk | contribs)   21:18, 8 November 2011

Per triage, this can wait until 1.19

Status & tagging log