From a44be28c4ea2dd14f137adc6b6cee2392a4c717b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20Tisza?= Date: Tue, 28 Oct 2014 10:40:23 +0000 Subject: [PATCH] Fetch image title from CommonsMetadata Change-Id: Id7d256150aedb4351778f6ad60fd7d85399b58c5 Mingle: https://wikimedia.mingle.thoughtworks.com/projects/multimedia/cards/589 --- resources/mmv/model/mmv.model.Image.js | 29 +++++++++++-------- .../mmv/provider/mmv.provider.ImageInfo.js | 1 + .../qunit/mmv/mmv.EmbedFileFormatter.test.js | 6 ++-- tests/qunit/mmv/model/mmv.model.Image.test.js | 15 ++++++---- .../provider/mmv.provider.ImageInfo.test.js | 7 ++++- 5 files changed, 37 insertions(+), 21 deletions(-) diff --git a/resources/mmv/model/mmv.model.Image.js b/resources/mmv/model/mmv.model.Image.js index e8d769629..8781aa534 100644 --- a/resources/mmv/model/mmv.model.Image.js +++ b/resources/mmv/model/mmv.model.Image.js @@ -23,6 +23,7 @@ * @class mw.mmv.model.Image * @constructor * @param {mw.Title} title + * @param {string} name Image name (e.g. title of the artwork) or human-readable file if there is no better title * @param {number} size Filesize in bytes of the original image * @param {number} width Width of the original image * @param {number} height Height of the original image @@ -43,6 +44,7 @@ */ function Image( title, + name, size, width, height, @@ -64,6 +66,9 @@ /** @property {mw.Title} title The title of the image file */ this.title = title; + /** @property {string} name Image name (e.g. title of the artwork) or human-readable file if there is no better title */ + this.name = name; + /** @property {number} size The filesize, in bytes, of the original image */ this.size = size; @@ -133,23 +138,16 @@ * @returns {mw.mmv.model.Image} */ Image.newFromImageInfo = function ( title, imageInfo ) { - var uploadDateTime, creationDateTime, imageData, + var name, uploadDateTime, creationDateTime, imageData, description, source, author, license, permission, latitude, longitude, innerInfo = imageInfo.imageinfo[0], extmeta = innerInfo.extmetadata; if ( extmeta ) { - creationDateTime = extmeta.DateTimeOriginal; - uploadDateTime = extmeta.DateTime; - - if ( uploadDateTime ) { - uploadDateTime = uploadDateTime.value.replace( /<.*?>/g, '' ); - } - - if ( creationDateTime ) { - creationDateTime = creationDateTime.value.replace( /<.*?>/g, '' ); - } + creationDateTime = this.parseExtmeta( extmeta.DateTimeOriginal, 'plaintext' ); + uploadDateTime = this.parseExtmeta( extmeta.DateTime, 'plaintext' ); + name = this.parseExtmeta( extmeta.ObjectName, 'plaintext' ); description = this.parseExtmeta( extmeta.ImageDescription, 'string' ); source = this.parseExtmeta( extmeta.Credit, 'string' ); @@ -170,8 +168,13 @@ longitude = this.parseExtmeta( extmeta.GPSLongitude, 'float' ); } + if ( !name ) { + name = title.getNameText(); + } + imageData = new Image( title, + name, innerInfo.size, innerInfo.width, innerInfo.height, @@ -204,13 +207,15 @@ /** * Reads and parses a value from the imageinfo API extmetadata field. * @param {Array} data - * @param {string} type one of 'string', 'float', 'list' + * @param {string} type one of 'plaintext', 'string', 'float', 'list' * @return {string|number|Array} value or undefined if it is missing */ Image.parseExtmeta = function ( data, type ) { var value = data && data.value; if ( value === null || value === undefined ) { return undefined; + } else if ( type === 'plaintext' ) { + return value.toString().replace( /<.*?>/g, '' ); } else if ( type === 'string' ) { return value.toString(); } else if ( type === 'float' ) { diff --git a/resources/mmv/provider/mmv.provider.ImageInfo.js b/resources/mmv/provider/mmv.provider.ImageInfo.js index e24f26a35..294db0a61 100644 --- a/resources/mmv/provider/mmv.provider.ImageInfo.js +++ b/resources/mmv/provider/mmv.provider.ImageInfo.js @@ -59,6 +59,7 @@ ImageInfo.prototype.iiextmetadatafilter = [ 'DateTime', 'DateTimeOriginal', + 'ObjectName', 'ImageDescription', 'License', 'LicenseShortName', diff --git a/tests/qunit/mmv/mmv.EmbedFileFormatter.test.js b/tests/qunit/mmv/mmv.EmbedFileFormatter.test.js index 39589bef6..6ea9a4c91 100644 --- a/tests/qunit/mmv/mmv.EmbedFileFormatter.test.js +++ b/tests/qunit/mmv/mmv.EmbedFileFormatter.test.js @@ -4,9 +4,9 @@ function createEmbedFileInfo( options ) { var license = options.licenseShortName ? new mw.mmv.model.License( options.licenseShortName, options.licenseInternalName, options.licenseLongName, options.licenseUrl ) : undefined, - imageInfo = new mw.mmv.model.Image( options.title, undefined, undefined, undefined, - undefined, options.imgUrl, options.filePageUrl, 'repo', undefined, undefined, - undefined, undefined, options.source, options.author, license ), + imageInfo = new mw.mmv.model.Image( options.title, options.title.getNameText(), undefined, + undefined, undefined, undefined, options.imgUrl, options.filePageUrl, 'repo', undefined, + undefined, undefined, undefined, options.source, options.author, license ), repoInfo = { displayName: options.siteName, getSiteLink: function () { return options.siteUrl; } }; diff --git a/tests/qunit/mmv/model/mmv.model.Image.test.js b/tests/qunit/mmv/model/mmv.model.Image.test.js index f070c7b09..163c0b1d0 100644 --- a/tests/qunit/mmv/model/mmv.model.Image.test.js +++ b/tests/qunit/mmv/model/mmv.model.Image.test.js @@ -18,9 +18,10 @@ ( function( mw ) { QUnit.module( 'mmv.model.Image', QUnit.newMwEnvironment() ); - QUnit.test( 'Image model constructor sanity check', 19, function ( assert ) { + QUnit.test( 'Image model constructor sanity check', 20, function ( assert ) { var title = mw.Title.newFromText( 'File:Foobar.jpg' ), + name = 'Foo bar', size = 100, width = 10, height = 15, @@ -39,12 +40,13 @@ latitude = 39.12381283, longitude = 100.983829, imageData = new mw.mmv.model.Image( - title, size, width, height, mime, url, + title, name, size, width, height, mime, url, descurl, repo, user, datetime, origdatetime, description, source, author, license, permission, latitude, longitude ); assert.strictEqual( imageData.title, title, 'Title is set correctly' ); + assert.strictEqual( imageData.name, name, 'Name is set correctly' ); assert.strictEqual( imageData.size, size, 'Size is set correctly' ); assert.strictEqual( imageData.width, width, 'Width is set correctly' ); assert.strictEqual( imageData.height, height, 'Height is set correctly' ); @@ -68,13 +70,13 @@ QUnit.test( 'hasCoords()', 2, function ( assert ) { var firstImageData = new mw.mmv.model.Image( - mw.Title.newFromText( 'File:Foobar.pdf.jpg' ), + mw.Title.newFromText( 'File:Foobar.pdf.jpg' ), 'Foo bar', 10, 10, 10, 'image/jpeg', 'http://example.org', 'http://example.com', 'example', 'tester', '2013-11-10', '2013-11-09', 'Blah blah blah', 'A person', 'Another person', 'CC-BY-SA-3.0', 'Permitted' ), secondImageData = new mw.mmv.model.Image( - mw.Title.newFromText( 'File:Foobar.pdf.jpg' ), + mw.Title.newFromText( 'File:Foobar.pdf.jpg' ), 'Foo bar', 10, 10, 10, 'image/jpeg', 'http://example.org', 'http://example.com', 'example', 'tester', '2013-11-10', '2013-11-09', 'Blah blah blah', 'A person', 'Another person', 'CC-BY-SA-3.0', 'Permitted', @@ -85,9 +87,10 @@ assert.strictEqual( secondImageData.hasCoords(), true, 'Coordinates present means hasCoords returns true.' ); } ); - QUnit.test( 'parseExtmeta()', 8, function ( assert ) { + QUnit.test( 'parseExtmeta()', 9, function ( assert ) { var Image = mw.mmv.model.Image, stringData = { value: 'foo' }, + plaintextData = { value: 'foo' }, floatData = { value: 1.23 }, floatStringData = { value: '1.23' }, listDataEmpty = {value: '' }, @@ -97,6 +100,8 @@ assert.strictEqual( Image.parseExtmeta( stringData, 'string' ), 'foo', 'Extmeta string parsed correctly.' ); + assert.strictEqual( Image.parseExtmeta( plaintextData, 'plaintext' ), 'foo', + 'Extmeta plaintext parsed correctly.' ); assert.strictEqual( Image.parseExtmeta( floatData, 'float' ), 1.23, 'Extmeta float parsed correctly.' ); assert.strictEqual( Image.parseExtmeta( floatStringData, 'float' ), 1.23, diff --git a/tests/qunit/mmv/provider/mmv.provider.ImageInfo.test.js b/tests/qunit/mmv/provider/mmv.provider.ImageInfo.test.js index 2d6cb01a1..346b27744 100644 --- a/tests/qunit/mmv/provider/mmv.provider.ImageInfo.test.js +++ b/tests/qunit/mmv/provider/mmv.provider.ImageInfo.test.js @@ -25,7 +25,7 @@ assert.ok( imageInfoProvider ); } ); - QUnit.asyncTest( 'ImageInfo get test', 22, function ( assert ) { + QUnit.asyncTest( 'ImageInfo get test', 23, function ( assert ) { var apiCallCount = 0, api = { get: function() { apiCallCount++; @@ -51,6 +51,10 @@ sha1: 'a1ba23d471f4dad208b71c143e2e105a0e3032db', metadata: [], extmetadata: { + ObjectName: { + value: 'Some stuff', + source: 'commons-templates' + }, License: { value: 'cc0', source: 'commons-templates', @@ -116,6 +120,7 @@ imageInfoProvider.get( file ).then( function( image ) { assert.strictEqual( image.title.getPrefixedDb(), 'File:Stuff.jpg', 'title is set correctly' ); + assert.strictEqual( image.name, 'Some stuff', 'name is set correctly' ); assert.strictEqual( image.size, 346684, 'size is set correctly' ); assert.strictEqual( image.width, 720, 'width is set correctly' ); assert.strictEqual( image.height, 1412, 'height is set correctly' );