2014-01-10 21:11:21 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MediaWiki extension MultimediaViewer.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MultimediaViewer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-03-20 01:19:54 +00:00
|
|
|
var IP;
|
|
|
|
|
2014-01-10 21:11:21 +00:00
|
|
|
/**
|
|
|
|
* Represents information about a single image
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-19 02:27:30 +00:00
|
|
|
* @class mw.mmv.model.Image
|
2014-01-10 21:11:21 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {mw.Title} title
|
2014-10-28 10:40:23 +00:00
|
|
|
* @param {string} name Image name (e.g. title of the artwork) or human-readable file if there is no better title
|
2014-01-10 21:11:21 +00:00
|
|
|
* @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
|
|
|
|
* @param {string} mimeType
|
|
|
|
* @param {string} url URL to the image itself (original version)
|
|
|
|
* @param {string} descriptionUrl URL to the image description page
|
2016-07-18 13:49:27 +00:00
|
|
|
* @param {string} descriptionShortUrl A short URL to the description page for the image, using curid=...
|
|
|
|
* @param {string} pageID pageId of the description page for the image
|
2014-01-10 21:11:21 +00:00
|
|
|
* @param {string} repo The repository this image belongs to
|
2014-01-31 02:03:08 +00:00
|
|
|
* @param {string} uploadDateTime The time and date the last upload occurred
|
2014-12-02 10:05:12 +00:00
|
|
|
* @param {string} anonymizedUploadDateTime Anonymized and EL-friendly version of uploadDateTime
|
2014-01-31 02:03:08 +00:00
|
|
|
* @param {string} creationDateTime The time and date the original upload occurred
|
2014-01-10 21:11:21 +00:00
|
|
|
* @param {string} description
|
|
|
|
* @param {string} source
|
|
|
|
* @param {string} author
|
2014-11-27 07:19:24 +00:00
|
|
|
* @param {number} authorCount
|
2014-03-20 01:19:54 +00:00
|
|
|
* @param {mw.mmv.model.License} license
|
2014-02-14 00:36:10 +00:00
|
|
|
* @param {string} permission
|
2014-12-28 21:04:22 +00:00
|
|
|
* @param {string} attribution Custom attribution string that replaces credit line when set
|
2014-12-07 10:09:25 +00:00
|
|
|
* @param {string} deletionReason
|
2014-01-13 21:40:22 +00:00
|
|
|
* @param {number} latitude
|
|
|
|
* @param {number} longitude
|
2015-01-15 03:14:20 +00:00
|
|
|
* @param {string[]} restrictions
|
2014-01-10 21:11:21 +00:00
|
|
|
*/
|
|
|
|
function Image(
|
2017-10-31 17:49:29 +00:00
|
|
|
title,
|
|
|
|
name,
|
|
|
|
size,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
mimeType,
|
|
|
|
url,
|
|
|
|
descriptionUrl,
|
|
|
|
descriptionShortUrl,
|
|
|
|
pageID,
|
|
|
|
repo,
|
|
|
|
uploadDateTime,
|
|
|
|
anonymizedUploadDateTime,
|
|
|
|
creationDateTime,
|
|
|
|
description,
|
|
|
|
source,
|
|
|
|
author,
|
|
|
|
authorCount,
|
|
|
|
license,
|
|
|
|
permission,
|
|
|
|
attribution,
|
|
|
|
deletionReason,
|
|
|
|
latitude,
|
|
|
|
longitude,
|
|
|
|
restrictions
|
2014-01-10 21:11:21 +00:00
|
|
|
) {
|
2014-01-22 00:22:43 +00:00
|
|
|
/** @property {mw.Title} title The title of the image file */
|
2014-01-10 21:11:21 +00:00
|
|
|
this.title = title;
|
|
|
|
|
2014-10-28 10:40:23 +00:00
|
|
|
/** @property {string} name Image name (e.g. title of the artwork) or human-readable file if there is no better title */
|
|
|
|
this.name = name;
|
|
|
|
|
2014-01-22 00:22:43 +00:00
|
|
|
/** @property {number} size The filesize, in bytes, of the original image */
|
2014-01-10 21:11:21 +00:00
|
|
|
this.size = size;
|
|
|
|
|
2014-01-22 00:22:43 +00:00
|
|
|
/** @property {number} width The width, in pixels, of the original image */
|
2014-01-10 21:11:21 +00:00
|
|
|
this.width = width;
|
|
|
|
|
2014-01-22 00:22:43 +00:00
|
|
|
/** @property {number} height The height, in pixels, of the original image */
|
2014-01-10 21:11:21 +00:00
|
|
|
this.height = height;
|
|
|
|
|
2014-01-22 00:22:43 +00:00
|
|
|
/** @property {string} mimeType The MIME type of the original image */
|
2014-01-10 21:11:21 +00:00
|
|
|
this.mimeType = mimeType;
|
|
|
|
|
2014-01-22 00:22:43 +00:00
|
|
|
/** @property {string} url The URL to the original image */
|
2014-01-10 21:11:21 +00:00
|
|
|
this.url = url;
|
|
|
|
|
2014-01-22 00:22:43 +00:00
|
|
|
/** @property {string} descriptionUrl The URL to the description page for the image */
|
2014-01-10 21:11:21 +00:00
|
|
|
this.descriptionUrl = descriptionUrl;
|
|
|
|
|
2015-12-24 20:35:52 +00:00
|
|
|
/** @property {string} descriptionShortUrl A short URL to the description page for the image, using curid=... */
|
|
|
|
this.descriptionShortUrl = descriptionShortUrl;
|
|
|
|
|
|
|
|
/** @property {number} pageId of the description page for the image */
|
|
|
|
this.pageID = pageID;
|
|
|
|
|
2014-01-22 00:22:43 +00:00
|
|
|
/** @property {string} repo The name of the repository where this image is stored */
|
2014-01-10 21:11:21 +00:00
|
|
|
this.repo = repo;
|
|
|
|
|
2014-01-22 00:22:43 +00:00
|
|
|
/** @property {string} uploadDateTime The date and time of the last upload */
|
2014-01-10 21:11:21 +00:00
|
|
|
this.uploadDateTime = uploadDateTime;
|
|
|
|
|
2014-12-02 10:05:12 +00:00
|
|
|
/** @property {string} anonymizedUploadDateTime The anonymized date and time of the last upload */
|
|
|
|
this.anonymizedUploadDateTime = anonymizedUploadDateTime;
|
|
|
|
|
2014-01-22 00:22:43 +00:00
|
|
|
/** @property {string} creationDateTime The date and time that the image was created */
|
2014-01-10 21:11:21 +00:00
|
|
|
this.creationDateTime = creationDateTime;
|
|
|
|
|
2014-01-22 00:22:43 +00:00
|
|
|
/** @property {string} description The description from the file page - unsafe HTML sometimes goes here */
|
2014-01-10 21:11:21 +00:00
|
|
|
this.description = description;
|
|
|
|
|
2014-01-22 00:22:43 +00:00
|
|
|
/** @property {string} source The source for the image (could be an organization, e.g.) - unsafe HTML sometimes goes here */
|
2014-01-10 21:11:21 +00:00
|
|
|
this.source = source;
|
|
|
|
|
2014-01-22 00:22:43 +00:00
|
|
|
/** @property {string} author The author of the image - unsafe HTML sometimes goes here */
|
2014-01-10 21:11:21 +00:00
|
|
|
this.author = author;
|
|
|
|
|
2014-11-27 07:19:24 +00:00
|
|
|
/** @property {number} authorCount The number of different authors of the image. This is guessed by the
|
|
|
|
* number of templates with author fields, so might be less than the number of actual authors. */
|
|
|
|
this.authorCount = authorCount;
|
|
|
|
|
2014-03-20 01:19:54 +00:00
|
|
|
/** @property {mw.mmv.model.License} license The license under which the image is distributed */
|
2014-01-10 21:11:21 +00:00
|
|
|
this.license = license;
|
|
|
|
|
2014-02-14 00:36:10 +00:00
|
|
|
/** @property {string} additional license conditions by the author (note that this is usually a big ugly HTML blob) */
|
|
|
|
this.permission = permission;
|
|
|
|
|
2014-12-28 21:04:22 +00:00
|
|
|
/** @property {string} attribution custom attribution string set by uploader that replaces credit line */
|
|
|
|
this.attribution = attribution;
|
|
|
|
|
2014-12-07 10:09:25 +00:00
|
|
|
/** @property {string|null} reason for pending deletion, null if image is not about to be deleted */
|
|
|
|
this.deletionReason = deletionReason;
|
|
|
|
|
2014-01-22 00:22:43 +00:00
|
|
|
/** @property {number} latitude The latitude of the place where the image was created */
|
2014-01-13 21:40:22 +00:00
|
|
|
this.latitude = latitude;
|
|
|
|
|
2014-01-22 00:22:43 +00:00
|
|
|
/** @property {number} longitude The longitude of the place where the image was created */
|
2014-01-13 21:40:22 +00:00
|
|
|
this.longitude = longitude;
|
|
|
|
|
2015-01-15 03:14:20 +00:00
|
|
|
/** @property {string[]} restrictions Any re-use restrictions for the image, eg trademarked */
|
|
|
|
this.restrictions = restrictions;
|
|
|
|
|
2014-01-10 21:11:21 +00:00
|
|
|
/**
|
2014-01-11 00:05:03 +00:00
|
|
|
* @property {Object} thumbUrls
|
2014-01-10 21:11:21 +00:00
|
|
|
* An object indexed by image widths
|
|
|
|
* with URLs to appropriately sized thumbnails
|
|
|
|
*/
|
|
|
|
this.thumbUrls = {};
|
|
|
|
}
|
2014-03-20 01:19:54 +00:00
|
|
|
IP = Image.prototype;
|
2014-01-10 21:11:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a new Image object out of an object containing
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-01-10 21:11:21 +00:00
|
|
|
* imageinfo data from an API response.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-02-19 02:27:30 +00:00
|
|
|
* @static
|
2014-01-10 21:11:21 +00:00
|
|
|
* @param {mw.Title} title
|
2014-01-11 00:05:03 +00:00
|
|
|
* @param {Object} imageInfo
|
2016-07-18 13:49:27 +00:00
|
|
|
* @return {mw.mmv.model.Image}
|
2014-01-10 21:11:21 +00:00
|
|
|
*/
|
|
|
|
Image.newFromImageInfo = function ( title, imageInfo ) {
|
2014-12-02 10:05:12 +00:00
|
|
|
var name, uploadDateTime, anonymizedUploadDateTime, creationDateTime, imageData,
|
2014-12-28 21:04:22 +00:00
|
|
|
description, source, author, authorCount, license, permission, attribution,
|
2014-12-07 10:09:25 +00:00
|
|
|
deletionReason, latitude, longitude, restrictions,
|
2016-07-18 13:49:27 +00:00
|
|
|
innerInfo = imageInfo.imageinfo[ 0 ],
|
2014-01-10 21:11:21 +00:00
|
|
|
extmeta = innerInfo.extmetadata;
|
|
|
|
|
|
|
|
if ( extmeta ) {
|
2014-10-28 10:40:23 +00:00
|
|
|
creationDateTime = this.parseExtmeta( extmeta.DateTimeOriginal, 'plaintext' );
|
2014-12-02 10:05:12 +00:00
|
|
|
uploadDateTime = this.parseExtmeta( extmeta.DateTime, 'plaintext' ).toString();
|
|
|
|
|
|
|
|
// Convert to "timestamp" format commonly used in EventLogging
|
|
|
|
anonymizedUploadDateTime = uploadDateTime.replace( /[^\d]/g, '' );
|
|
|
|
|
|
|
|
// Anonymise the timestamp to avoid making the file identifiable
|
|
|
|
// We only need to know the day
|
|
|
|
anonymizedUploadDateTime = anonymizedUploadDateTime.substr( 0, anonymizedUploadDateTime.length - 6 ) + '000000';
|
|
|
|
|
2014-10-28 10:40:23 +00:00
|
|
|
name = this.parseExtmeta( extmeta.ObjectName, 'plaintext' );
|
2014-01-10 21:11:21 +00:00
|
|
|
|
2014-03-20 01:19:54 +00:00
|
|
|
description = this.parseExtmeta( extmeta.ImageDescription, 'string' );
|
|
|
|
source = this.parseExtmeta( extmeta.Credit, 'string' );
|
|
|
|
author = this.parseExtmeta( extmeta.Artist, 'string' );
|
2014-11-27 07:19:24 +00:00
|
|
|
authorCount = this.parseExtmeta( extmeta.AuthorCount, 'integer' );
|
|
|
|
|
2014-11-24 22:09:03 +00:00
|
|
|
license = this.newLicenseFromImageInfo( extmeta );
|
2014-03-20 01:19:54 +00:00
|
|
|
permission = this.parseExtmeta( extmeta.Permission, 'string' );
|
2014-12-28 21:04:22 +00:00
|
|
|
attribution = this.parseExtmeta( extmeta.Attribution, 'string' );
|
2014-12-07 10:09:25 +00:00
|
|
|
deletionReason = this.parseExtmeta( extmeta.DeletionReason, 'string' );
|
2015-01-15 03:14:20 +00:00
|
|
|
restrictions = this.parseExtmeta( extmeta.Restrictions, 'list' );
|
2014-01-13 21:40:22 +00:00
|
|
|
|
2014-03-20 01:19:54 +00:00
|
|
|
latitude = this.parseExtmeta( extmeta.GPSLatitude, 'float' );
|
|
|
|
longitude = this.parseExtmeta( extmeta.GPSLongitude, 'float' );
|
2014-01-10 21:11:21 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 10:40:23 +00:00
|
|
|
if ( !name ) {
|
|
|
|
name = title.getNameText();
|
|
|
|
}
|
|
|
|
|
2014-01-10 21:11:21 +00:00
|
|
|
imageData = new Image(
|
|
|
|
title,
|
2014-10-28 10:40:23 +00:00
|
|
|
name,
|
2014-01-10 21:11:21 +00:00
|
|
|
innerInfo.size,
|
|
|
|
innerInfo.width,
|
|
|
|
innerInfo.height,
|
|
|
|
innerInfo.mime,
|
|
|
|
innerInfo.url,
|
|
|
|
innerInfo.descriptionurl,
|
2015-12-24 20:35:52 +00:00
|
|
|
innerInfo.descriptionshorturl,
|
|
|
|
imageInfo.pageid,
|
2014-01-10 21:11:21 +00:00
|
|
|
imageInfo.imagerepository,
|
|
|
|
uploadDateTime,
|
2014-12-02 10:05:12 +00:00
|
|
|
anonymizedUploadDateTime,
|
2014-01-10 21:11:21 +00:00
|
|
|
creationDateTime,
|
|
|
|
description,
|
|
|
|
source,
|
|
|
|
author,
|
2014-11-27 07:19:24 +00:00
|
|
|
authorCount,
|
2014-01-13 21:40:22 +00:00
|
|
|
license,
|
2014-02-14 00:36:10 +00:00
|
|
|
permission,
|
2014-12-28 21:04:22 +00:00
|
|
|
attribution,
|
2014-12-07 10:09:25 +00:00
|
|
|
deletionReason,
|
2014-01-13 21:40:22 +00:00
|
|
|
latitude,
|
2015-01-15 03:14:20 +00:00
|
|
|
longitude,
|
|
|
|
restrictions
|
2014-01-10 21:11:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ( innerInfo.thumburl ) {
|
|
|
|
imageData.addThumbUrl(
|
|
|
|
innerInfo.thumbwidth,
|
|
|
|
innerInfo.thumburl
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageData;
|
|
|
|
};
|
|
|
|
|
2014-11-24 22:09:03 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new License object out of an object containing
|
|
|
|
* imageinfo data from an API response.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-11-24 22:09:03 +00:00
|
|
|
* @static
|
|
|
|
* @param {Object} extmeta the extmeta array of the imageinfo data
|
2016-07-18 13:49:27 +00:00
|
|
|
* @return {mw.mmv.model.License|undefined}
|
2014-11-24 22:09:03 +00:00
|
|
|
*/
|
|
|
|
Image.newLicenseFromImageInfo = function ( extmeta ) {
|
|
|
|
var license;
|
|
|
|
|
|
|
|
if ( extmeta.LicenseShortName ) {
|
|
|
|
license = new mw.mmv.model.License(
|
|
|
|
this.parseExtmeta( extmeta.LicenseShortName, 'string' ),
|
|
|
|
this.parseExtmeta( extmeta.License, 'string' ),
|
|
|
|
this.parseExtmeta( extmeta.UsageTerms, 'string' ),
|
|
|
|
this.parseExtmeta( extmeta.LicenseUrl, 'string' ),
|
|
|
|
this.parseExtmeta( extmeta.AttributionRequired, 'boolean' ),
|
|
|
|
this.parseExtmeta( extmeta.NonFree, 'boolean' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return license;
|
|
|
|
};
|
|
|
|
|
2014-03-20 01:19:54 +00:00
|
|
|
/**
|
|
|
|
* Reads and parses a value from the imageinfo API extmetadata field.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-03-20 01:19:54 +00:00
|
|
|
* @param {Array} data
|
2014-11-24 22:09:03 +00:00
|
|
|
* @param {string} type one of 'plaintext', 'string', 'float', 'boolean', 'list'
|
|
|
|
* @return {string|number|boolean|Array} value or undefined if it is missing
|
2014-03-20 01:19:54 +00:00
|
|
|
*/
|
|
|
|
Image.parseExtmeta = function ( data, type ) {
|
|
|
|
var value = data && data.value;
|
2014-03-21 20:29:55 +00:00
|
|
|
if ( value === null || value === undefined ) {
|
2014-03-20 01:19:54 +00:00
|
|
|
return undefined;
|
2014-10-28 10:40:23 +00:00
|
|
|
} else if ( type === 'plaintext' ) {
|
|
|
|
return value.toString().replace( /<.*?>/g, '' );
|
2014-03-20 01:19:54 +00:00
|
|
|
} else if ( type === 'string' ) {
|
|
|
|
return value.toString();
|
2014-11-27 07:19:24 +00:00
|
|
|
} else if ( type === 'integer' ) {
|
|
|
|
return parseInt( value, 10 );
|
2014-03-20 01:19:54 +00:00
|
|
|
} else if ( type === 'float' ) {
|
|
|
|
return parseFloat( value );
|
2014-11-24 22:09:03 +00:00
|
|
|
} else if ( type === 'boolean' ) {
|
|
|
|
value = value.toString().toLowerCase().replace( /^\s+|\s+$/g, '' );
|
2016-07-18 13:49:27 +00:00
|
|
|
if ( value in { 1: null, yes: null, 'true': null } ) {
|
2014-11-24 22:09:03 +00:00
|
|
|
return true;
|
2016-07-18 13:49:27 +00:00
|
|
|
} else if ( value in { 0: null, no: null, 'false': null } ) {
|
2014-11-24 22:09:03 +00:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return undefined;
|
|
|
|
}
|
2014-03-20 01:19:54 +00:00
|
|
|
} else if ( type === 'list' ) {
|
2014-03-21 20:29:55 +00:00
|
|
|
return value === '' ? [] : value.split( '|' );
|
2014-03-20 01:19:54 +00:00
|
|
|
} else {
|
2017-05-03 09:15:05 +00:00
|
|
|
throw new Error( 'mw.mmv.model.Image.parseExtmeta: unknown type' );
|
2014-03-20 01:19:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-10 21:11:21 +00:00
|
|
|
/**
|
|
|
|
* Add a thumb URL
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-01-10 21:11:21 +00:00
|
|
|
* @param {number} width
|
|
|
|
* @param {string} url
|
|
|
|
*/
|
2014-03-20 01:19:54 +00:00
|
|
|
IP.addThumbUrl = function ( width, url ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
this.thumbUrls[ width ] = url;
|
2014-01-10 21:11:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a thumb URL if we have it.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-01-10 21:11:21 +00:00
|
|
|
* @param {number} width
|
2016-07-18 13:49:27 +00:00
|
|
|
* @return {string|undefined}
|
2014-01-10 21:11:21 +00:00
|
|
|
*/
|
2014-03-20 01:19:54 +00:00
|
|
|
IP.getThumbUrl = function ( width ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
return this.thumbUrls[ width ];
|
2014-01-10 21:11:21 +00:00
|
|
|
};
|
|
|
|
|
2014-01-13 21:40:22 +00:00
|
|
|
/**
|
|
|
|
* Check whether the image has geolocation data.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
|
|
|
* @return {boolean}
|
2014-01-13 21:40:22 +00:00
|
|
|
*/
|
2014-03-20 01:19:54 +00:00
|
|
|
IP.hasCoords = function () {
|
2018-09-13 21:13:56 +00:00
|
|
|
return this.latitude !== undefined && this.latitude !== null &&
|
2014-01-13 21:40:22 +00:00
|
|
|
this.longitude !== undefined && this.longitude !== null;
|
|
|
|
};
|
|
|
|
|
2014-01-10 21:11:21 +00:00
|
|
|
mw.mmv.model.Image = Image;
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|