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
* @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' ) {

View file

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

View file

@ -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; } };

View file

@ -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: 'fo<b>o</b>' },
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,

View file

@ -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' );