Merge "Fetch image title from CommonsMetadata"

This commit is contained in:
jenkins-bot 2014-10-28 19:18:00 +00:00 committed by Gerrit Code Review
commit eb21f30631
5 changed files with 37 additions and 21 deletions

View file

@ -23,6 +23,7 @@
* @class mw.mmv.model.Image * @class mw.mmv.model.Image
* @constructor * @constructor
* @param {mw.Title} title * @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} size Filesize in bytes of the original image
* @param {number} width Width of the original image * @param {number} width Width of the original image
* @param {number} height Height of the original image * @param {number} height Height of the original image
@ -43,6 +44,7 @@
*/ */
function Image( function Image(
title, title,
name,
size, size,
width, width,
height, height,
@ -64,6 +66,9 @@
/** @property {mw.Title} title The title of the image file */ /** @property {mw.Title} title The title of the image file */
this.title = title; 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 */ /** @property {number} size The filesize, in bytes, of the original image */
this.size = size; this.size = size;
@ -133,23 +138,16 @@
* @returns {mw.mmv.model.Image} * @returns {mw.mmv.model.Image}
*/ */
Image.newFromImageInfo = function ( title, imageInfo ) { Image.newFromImageInfo = function ( title, imageInfo ) {
var uploadDateTime, creationDateTime, imageData, var name, uploadDateTime, creationDateTime, imageData,
description, source, author, license, permission, description, source, author, license, permission,
latitude, longitude, latitude, longitude,
innerInfo = imageInfo.imageinfo[0], innerInfo = imageInfo.imageinfo[0],
extmeta = innerInfo.extmetadata; extmeta = innerInfo.extmetadata;
if ( extmeta ) { if ( extmeta ) {
creationDateTime = extmeta.DateTimeOriginal; creationDateTime = this.parseExtmeta( extmeta.DateTimeOriginal, 'plaintext' );
uploadDateTime = extmeta.DateTime; uploadDateTime = this.parseExtmeta( extmeta.DateTime, 'plaintext' );
name = this.parseExtmeta( extmeta.ObjectName, 'plaintext' );
if ( uploadDateTime ) {
uploadDateTime = uploadDateTime.value.replace( /<.*?>/g, '' );
}
if ( creationDateTime ) {
creationDateTime = creationDateTime.value.replace( /<.*?>/g, '' );
}
description = this.parseExtmeta( extmeta.ImageDescription, 'string' ); description = this.parseExtmeta( extmeta.ImageDescription, 'string' );
source = this.parseExtmeta( extmeta.Credit, 'string' ); source = this.parseExtmeta( extmeta.Credit, 'string' );
@ -170,8 +168,13 @@
longitude = this.parseExtmeta( extmeta.GPSLongitude, 'float' ); longitude = this.parseExtmeta( extmeta.GPSLongitude, 'float' );
} }
if ( !name ) {
name = title.getNameText();
}
imageData = new Image( imageData = new Image(
title, title,
name,
innerInfo.size, innerInfo.size,
innerInfo.width, innerInfo.width,
innerInfo.height, innerInfo.height,
@ -204,13 +207,15 @@
/** /**
* Reads and parses a value from the imageinfo API extmetadata field. * Reads and parses a value from the imageinfo API extmetadata field.
* @param {Array} data * @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 * @return {string|number|Array} value or undefined if it is missing
*/ */
Image.parseExtmeta = function ( data, type ) { Image.parseExtmeta = function ( data, type ) {
var value = data && data.value; var value = data && data.value;
if ( value === null || value === undefined ) { if ( value === null || value === undefined ) {
return undefined; return undefined;
} else if ( type === 'plaintext' ) {
return value.toString().replace( /<.*?>/g, '' );
} else if ( type === 'string' ) { } else if ( type === 'string' ) {
return value.toString(); return value.toString();
} else if ( type === 'float' ) { } else if ( type === 'float' ) {

View file

@ -59,6 +59,7 @@
ImageInfo.prototype.iiextmetadatafilter = [ ImageInfo.prototype.iiextmetadatafilter = [
'DateTime', 'DateTime',
'DateTimeOriginal', 'DateTimeOriginal',
'ObjectName',
'ImageDescription', 'ImageDescription',
'License', 'License',
'LicenseShortName', 'LicenseShortName',

View file

@ -4,9 +4,9 @@
function createEmbedFileInfo( options ) { function createEmbedFileInfo( options ) {
var license = options.licenseShortName ? new mw.mmv.model.License( options.licenseShortName, var license = options.licenseShortName ? new mw.mmv.model.License( options.licenseShortName,
options.licenseInternalName, options.licenseLongName, options.licenseUrl ) : undefined, options.licenseInternalName, options.licenseLongName, options.licenseUrl ) : undefined,
imageInfo = new mw.mmv.model.Image( options.title, undefined, undefined, undefined, imageInfo = new mw.mmv.model.Image( options.title, options.title.getNameText(), undefined,
undefined, options.imgUrl, options.filePageUrl, 'repo', undefined, undefined, undefined, undefined, undefined, options.imgUrl, options.filePageUrl, 'repo', undefined,
undefined, undefined, options.source, options.author, license ), undefined, undefined, undefined, options.source, options.author, license ),
repoInfo = { displayName: options.siteName, getSiteLink: repoInfo = { displayName: options.siteName, getSiteLink:
function () { return options.siteUrl; } }; function () { return options.siteUrl; } };

View file

@ -18,9 +18,10 @@
( function( mw ) { ( function( mw ) {
QUnit.module( 'mmv.model.Image', QUnit.newMwEnvironment() ); 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 var
title = mw.Title.newFromText( 'File:Foobar.jpg' ), title = mw.Title.newFromText( 'File:Foobar.jpg' ),
name = 'Foo bar',
size = 100, size = 100,
width = 10, width = 10,
height = 15, height = 15,
@ -39,12 +40,13 @@
latitude = 39.12381283, latitude = 39.12381283,
longitude = 100.983829, longitude = 100.983829,
imageData = new mw.mmv.model.Image( imageData = new mw.mmv.model.Image(
title, size, width, height, mime, url, title, name, size, width, height, mime, url,
descurl, repo, user, datetime, origdatetime, descurl, repo, user, datetime, origdatetime,
description, source, author, license, permission, description, source, author, license, permission,
latitude, longitude ); latitude, longitude );
assert.strictEqual( imageData.title, title, 'Title is set correctly' ); 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.size, size, 'Size is set correctly' );
assert.strictEqual( imageData.width, width, 'Width is set correctly' ); assert.strictEqual( imageData.width, width, 'Width is set correctly' );
assert.strictEqual( imageData.height, height, 'Height is set correctly' ); assert.strictEqual( imageData.height, height, 'Height is set correctly' );
@ -68,13 +70,13 @@
QUnit.test( 'hasCoords()', 2, function ( assert ) { QUnit.test( 'hasCoords()', 2, function ( assert ) {
var var
firstImageData = new mw.mmv.model.Image( 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', 10, 10, 10, 'image/jpeg', 'http://example.org', 'http://example.com',
'example', 'tester', '2013-11-10', '2013-11-09', 'Blah blah blah', 'example', 'tester', '2013-11-10', '2013-11-09', 'Blah blah blah',
'A person', 'Another person', 'CC-BY-SA-3.0', 'Permitted' 'A person', 'Another person', 'CC-BY-SA-3.0', 'Permitted'
), ),
secondImageData = new mw.mmv.model.Image( 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', 10, 10, 10, 'image/jpeg', 'http://example.org', 'http://example.com',
'example', 'tester', '2013-11-10', '2013-11-09', 'Blah blah blah', 'example', 'tester', '2013-11-10', '2013-11-09', 'Blah blah blah',
'A person', 'Another person', 'CC-BY-SA-3.0', 'Permitted', '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.' ); 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, var Image = mw.mmv.model.Image,
stringData = { value: 'foo' }, stringData = { value: 'foo' },
plaintextData = { value: 'fo<b>o</b>' },
floatData = { value: 1.23 }, floatData = { value: 1.23 },
floatStringData = { value: '1.23' }, floatStringData = { value: '1.23' },
listDataEmpty = {value: '' }, listDataEmpty = {value: '' },
@ -97,6 +100,8 @@
assert.strictEqual( Image.parseExtmeta( stringData, 'string' ), 'foo', assert.strictEqual( Image.parseExtmeta( stringData, 'string' ), 'foo',
'Extmeta string parsed correctly.' ); 'Extmeta string parsed correctly.' );
assert.strictEqual( Image.parseExtmeta( plaintextData, 'plaintext' ), 'foo',
'Extmeta plaintext parsed correctly.' );
assert.strictEqual( Image.parseExtmeta( floatData, 'float' ), 1.23, assert.strictEqual( Image.parseExtmeta( floatData, 'float' ), 1.23,
'Extmeta float parsed correctly.' ); 'Extmeta float parsed correctly.' );
assert.strictEqual( Image.parseExtmeta( floatStringData, 'float' ), 1.23, assert.strictEqual( Image.parseExtmeta( floatStringData, 'float' ), 1.23,

View file

@ -25,7 +25,7 @@
assert.ok( imageInfoProvider ); assert.ok( imageInfoProvider );
} ); } );
QUnit.asyncTest( 'ImageInfo get test', 22, function ( assert ) { QUnit.asyncTest( 'ImageInfo get test', 23, function ( assert ) {
var apiCallCount = 0, var apiCallCount = 0,
api = { get: function() { api = { get: function() {
apiCallCount++; apiCallCount++;
@ -51,6 +51,10 @@
sha1: 'a1ba23d471f4dad208b71c143e2e105a0e3032db', sha1: 'a1ba23d471f4dad208b71c143e2e105a0e3032db',
metadata: [], metadata: [],
extmetadata: { extmetadata: {
ObjectName: {
value: 'Some stuff',
source: 'commons-templates'
},
License: { License: {
value: 'cc0', value: 'cc0',
source: 'commons-templates', source: 'commons-templates',
@ -116,6 +120,7 @@
imageInfoProvider.get( file ).then( function( image ) { imageInfoProvider.get( file ).then( function( image ) {
assert.strictEqual( image.title.getPrefixedDb(), 'File:Stuff.jpg', 'title is set correctly' ); 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.size, 346684, 'size is set correctly' );
assert.strictEqual( image.width, 720, 'width is set correctly' ); assert.strictEqual( image.width, 720, 'width is set correctly' );
assert.strictEqual( image.height, 1412, 'height is set correctly' ); assert.strictEqual( image.height, 1412, 'height is set correctly' );