formatDate: handle year, handle <div>

Bug: T58794
Change-Id: Ie05241c8db18773d0eb84421813db69b425e0784
This commit is contained in:
Simon Legner 2024-01-24 19:17:28 +01:00
parent cfcb8d4755
commit 254ec713cc
4 changed files with 30 additions and 4 deletions

View file

@ -192,8 +192,8 @@ const License = require( './mmv.model.License.js' );
const extmeta = innerInfo.extmetadata;
if ( extmeta ) {
creationDateTime = this.parseExtmeta( extmeta.DateTimeOriginal, 'plaintext' );
uploadDateTime = this.parseExtmeta( extmeta.DateTime, 'plaintext' ).toString();
creationDateTime = this.parseExtmeta( extmeta.DateTimeOriginal, 'datetime' );
uploadDateTime = this.parseExtmeta( extmeta.DateTime, 'datetime' );
// Convert to "timestamp" format commonly used in EventLogging
anonymizedUploadDateTime = uploadDateTime.replace( /[^\d]/g, '' );
@ -288,7 +288,7 @@ const License = require( './mmv.model.License.js' );
* Reads and parses a value from the imageinfo API extmetadata field.
*
* @param {Array} data
* @param {string} type one of 'plaintext', 'string', 'float', 'boolean', 'list'
* @param {string} type one of 'plaintext', 'string', 'float', 'boolean', 'list', 'datetime'
* @return {string|number|boolean|Array} value or undefined if it is missing
*/
static parseExtmeta( data, type ) {
@ -312,6 +312,21 @@ const License = require( './mmv.model.License.js' );
} else {
return undefined;
}
} else if ( type === 'datetime' ) {
value = value.toString();
// https://datatracker.ietf.org/doc/html/rfc3339
// adapted from https://stackoverflow.com/questions/3143070/regex-to-match-an-iso-8601-datetime-string
const rfc3339 = /\d{4}-[01]\d-[0-3]\d(T[0-2]\d:[0-5]\d:[0-5]\d(\.\d+)?Z?)?/;
const match = value.match( rfc3339 );
if ( !match ) {
return value.replace( /<.*?>/g, '' );
}
value = match[ 0 ];
if ( value.match( /^\d{4}-00-00/ ) ) {
// assume yyyy
return value.slice( 0, 4 );
}
return value;
} else if ( type === 'list' ) {
return value === '' ? [] : value.split( '|' );
} else {

View file

@ -801,6 +801,10 @@ const TruncatableTextField = require( './mmv.ui.truncatableTextField.js' );
// and thus sometimes cause tests to fail.
lang = 'en-GB';
}
if ( dateString.length === 4 ) {
// assume yyyy
return dateString;
}
const date = new Date( dateString );
try {
if ( date instanceof Date && !isNaN( date ) ) {

View file

@ -140,6 +140,13 @@ const { ImageModel, License } = require( 'mmv' );
assert.strictEqual( ImageModel.parseExtmeta( missingData, 'string' ), undefined,
'Extmeta missing data parsed correctly.' );
assert.strictEqual( ImageModel.parseExtmeta( { value: '1960-03-14' }, 'datetime' ), '1960-03-14',
'Extmeta date is parsed correctly.' );
assert.strictEqual( ImageModel.parseExtmeta( { value: '1960' }, 'datetime' ), '1960',
'Extmeta year is parsed correctly.' );
assert.strictEqual( ImageModel.parseExtmeta( { value: '1926<div style="display: none;">date QS:P571,+1926-00-00T00:00:00Z/9</div>' }, 'datetime' ), '1926',
'Extmeta year is extracted from hidden div.' );
assert.throws( function () {
ImageModel.parseExtmeta( stringData, 'strong' );
}, 'Exception is thrown on invalid argument' );

View file

@ -212,7 +212,7 @@ const { ImageInfo } = require( 'mmv' );
assert.strictEqual( image.repo, 'shared', 'repo is set correctly' );
assert.strictEqual( image.uploadDateTime, '2013-08-25T14:41:02Z', 'uploadDateTime is set correctly' );
assert.strictEqual( image.anonymizedUploadDateTime, '20130825000000', 'anonymizedUploadDateTime is set correctly' );
assert.strictEqual( image.creationDateTime, '18 February 2009\u00a0(according to EXIF data)', 'creationDateTime is set correctly' );
assert.strictEqual( image.creationDateTime, '2009-02-18', 'creationDateTime is set correctly' );
assert.strictEqual( image.description, 'Wikis stuff', 'description is set correctly' );
assert.strictEqual( image.source, 'Wikipedia', 'source is set correctly' );
assert.strictEqual( image.author, 'John Smith', 'author is set correctly' );