Debian Codegraph @

extensions/MultimediaViewer/resources/mmv/model/mmv.model.Image.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MultimediaViewer.
3 *
4 * MultimediaViewer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MultimediaViewer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const License = require( './mmv.model.License.js' );
19
20 /**
21 * Represents information about a single image
22 */
23 class ImageModel {
24 /**
25 * @param {mw.Title} title
26 * @param {string} name Image name (e.g. title of the artwork) or human-readable file if there is no better title
27 * @param {number} size Filesize in bytes of the original image
28 * @param {number} width Width of the original image
29 * @param {number} height Height of the original image
30 * @param {string} mimeType
31 * @param {string} url URL to the image itself (original version)
32 * @param {string} descriptionUrl URL to the image description page
33 * @param {string} descriptionShortUrl A short URL to the description page for the image, using curid=...
34 * @param {string} pageID pageId of the description page for the image
35 * @param {string} repo The repository this image belongs to
36 * @param {string} uploadDateTime The time and date the last upload occurred
37 * @param {string} anonymizedUploadDateTime Anonymized and EL-friendly version of uploadDateTime
38 * @param {string} creationDateTime The time and date the original upload occurred
39 * @param {string} description
40 * @param {string} source
41 * @param {string} author
42 * @param {number} authorCount
43 * @param {License} license
44 * @param {string} permission
45 * @param {string} attribution Custom attribution string that replaces credit line when set
46 * @param {string} deletionReason
47 * @param {number} latitude
48 * @param {number} longitude
49 * @param {string[]} restrictions
50 */
51 constructor(
52 title,
53 name,
54 size,
55 width,
56 height,
57 mimeType,
58 url,
59 descriptionUrl,
60 descriptionShortUrl,
61 pageID,
62 repo,
63 uploadDateTime,
64 anonymizedUploadDateTime,
65 creationDateTime,
66 description,
67 source,
68 author,
69 authorCount,
70 license,
71 permission,
72 attribution,
73 deletionReason,
74 latitude,
75 longitude,
76 restrictions
77 ) {
78 /** @property {mw.Title} title The title of the image file */
79 this.title = title;
80
81 /** @property {string} name Image name (e.g. title of the artwork) or human-readable file if there is no better title */
82 this.name = name;
83
84 /** @property {number} size The filesize, in bytes, of the original image */
85 this.size = size;
86
87 /** @property {number} width The width, in pixels, of the original image */
88 this.width = width;
89
90 /** @property {number} height The height, in pixels, of the original image */
91 this.height = height;
92
93 /** @property {string} mimeType The MIME type of the original image */
94 this.mimeType = mimeType;
95
96 /** @property {string} url The URL to the original image */
97 this.url = url;
98
99 /** @property {string} descriptionUrl The URL to the description page for the image */
100 this.descriptionUrl = descriptionUrl;
101
102 /** @property {string} descriptionShortUrl A short URL to the description page for the image, using curid=... */
103 this.descriptionShortUrl = descriptionShortUrl;
104
105 /** @property {number} pageId of the description page for the image */
106 this.pageID = pageID;
107
108 /** @property {string} repo The name of the repository where this image is stored */
109 this.repo = repo;
110
111 /** @property {string} uploadDateTime The date and time of the last upload */
112 this.uploadDateTime = uploadDateTime;
113
114 /** @property {string} anonymizedUploadDateTime The anonymized date and time of the last upload */
115 this.anonymizedUploadDateTime = anonymizedUploadDateTime;
116
117 /** @property {string} creationDateTime The date and time that the image was created */
118 this.creationDateTime = creationDateTime;
119
120 /** @property {string} description The description from the file page - unsafe HTML sometimes goes here */
121 this.description = description;
122
123 /** @property {string} source The source for the image (could be an organization, e.g.) - unsafe HTML sometimes goes here */
124 this.source = source;
125
126 /** @property {string} author The author of the image - unsafe HTML sometimes goes here */
127 this.author = author;
128
129 /**
130 * @property {number} authorCount The number of different authors of the image. This is guessed by the
131 * number of templates with author fields, so might be less than the number of actual authors.
132 */
133 this.authorCount = authorCount;
134
135 /** @property {License} license The license under which the image is distributed */
136 this.license = license;
137
138 /** @property {string} additional license conditions by the author (note that this is usually a big ugly HTML blob) */
139 this.permission = permission;
140
141 /** @property {string} attribution custom attribution string set by uploader that replaces credit line */
142 this.attribution = attribution;
143
144 /** @property {string|null} reason for pending deletion, null if image is not about to be deleted */
145 this.deletionReason = deletionReason;
146
147 /** @property {number} latitude The latitude of the place where the image was created */
148 this.latitude = latitude;
149
150 /** @property {number} longitude The longitude of the place where the image was created */
151 this.longitude = longitude;
152
153 /** @property {string[]} restrictions Any re-use restrictions for the image, eg trademarked */
154 this.restrictions = restrictions;
155
156 /**
157 * @property {Object} thumbUrls
158 * An object indexed by image widths
159 * with URLs to appropriately sized thumbnails
160 */
161 this.thumbUrls = {};
162 }
163
164 /**
165 * Constructs a new Image object out of an object containing
166 *
167 * imageinfo data from an API response.
168 *
169 * @static
170 * @param {mw.Title} title
171 * @param {Object} imageInfo
172 * @return {ImageModel}
173 */
174 static newFromImageInfo( title, imageInfo ) {
175 let name;
176 let uploadDateTime;
177 let anonymizedUploadDateTime;
178 let creationDateTime;
179 let description;
180 let source;
181 let author;
182 let authorCount;
183 let license;
184 let permission;
185 let attribution;
186 let deletionReason;
187 let latitude;
188 let longitude;
189 let restrictions;
190 const innerInfo = imageInfo.imageinfo[ 0 ];
191 const extmeta = innerInfo.extmetadata;
192
193 if ( extmeta ) {
194 creationDateTime = this.parseExtmeta( extmeta.DateTimeOriginal, 'datetime' );
195 uploadDateTime = this.parseExtmeta( extmeta.DateTime, 'datetime' );
196
197 // Convert to "timestamp" format commonly used in EventLogging
198 anonymizedUploadDateTime = uploadDateTime.replace( /[^\d]/g, '' );
199
200 // Anonymise the timestamp to avoid making the file identifiable
201 // We only need to know the day
202 anonymizedUploadDateTime = `${ anonymizedUploadDateTime.slice( 0, anonymizedUploadDateTime.length - 6 ) }000000`;
203
204 name = this.parseExtmeta( extmeta.ObjectName, 'plaintext' );
205
206 description = this.parseExtmeta( extmeta.ImageDescription, 'string' );
207 source = this.parseExtmeta( extmeta.Credit, 'string' );
208 author = this.parseExtmeta( extmeta.Artist, 'string' );
209 authorCount = this.parseExtmeta( extmeta.AuthorCount, 'integer' );
210
211 license = this.newLicenseFromImageInfo( extmeta );
212 permission = this.parseExtmeta( extmeta.Permission, 'string' );
213 attribution = this.parseExtmeta( extmeta.Attribution, 'string' );
214 deletionReason = this.parseExtmeta( extmeta.DeletionReason, 'string' );
215 restrictions = this.parseExtmeta( extmeta.Restrictions, 'list' );
216
217 latitude = this.parseExtmeta( extmeta.GPSLatitude, 'float' );
218 longitude = this.parseExtmeta( extmeta.GPSLongitude, 'float' );
219 }
220
221 if ( !name ) {
222 name = title.getNameText();
223 }
224
225 const imageData = new ImageModel(
226 title,
227 name,
228 innerInfo.size,
229 innerInfo.width,
230 innerInfo.height,
231 innerInfo.mime,
232 innerInfo.url,
233 innerInfo.descriptionurl,
234 innerInfo.descriptionshorturl,
235 imageInfo.pageid,
236 imageInfo.imagerepository,
237 uploadDateTime,
238 anonymizedUploadDateTime,
239 creationDateTime,
240 description,
241 source,
242 author,
243 authorCount,
244 license,
245 permission,
246 attribution,
247 deletionReason,
248 latitude,
249 longitude,
250 restrictions
251 );
252
253 if ( innerInfo.thumburl ) {
254 imageData.addThumbUrl(
255 innerInfo.thumbwidth,
256 innerInfo.thumburl
257 );
258 }
259
260 return imageData;
261 }
262
263 /**
264 * Constructs a new License object out of an object containing
265 * imageinfo data from an API response.
266 *
267 * @static
268 * @param {Object} extmeta the extmeta array of the imageinfo data
269 * @return {License|undefined}
270 */
271 static newLicenseFromImageInfo( extmeta ) {
272 let license;
273
274 if ( extmeta.LicenseShortName ) {
275 license = new License(
276 this.parseExtmeta( extmeta.LicenseShortName, 'string' ),
277 this.parseExtmeta( extmeta.License, 'string' ),
278 this.parseExtmeta( extmeta.UsageTerms, 'string' ),
279 this.parseExtmeta( extmeta.LicenseUrl, 'string' ),
280 this.parseExtmeta( extmeta.AttributionRequired, 'boolean' ),
281 this.parseExtmeta( extmeta.NonFree, 'boolean' )
282 );
283 }
284
285 return license;
286 }
287
288 /**
289 * Reads and parses a value from the imageinfo API extmetadata field.
290 *
291 * @param {Array} data
292 * @param {string} type one of 'plaintext', 'string', 'float', 'boolean', 'list', 'datetime'
293 * @return {string|number|boolean|Array} value or undefined if it is missing
294 */
295 static parseExtmeta( data, type ) {
296 let value = data && data.value;
297 if ( value === null || value === undefined ) {
298 return undefined;
299 } else if ( type === 'plaintext' ) {
300 return value.toString().replace( /<.*?>/g, '' );
301 } else if ( type === 'string' ) {
302 return value.toString();
303 } else if ( type === 'integer' ) {
304 return parseInt( value, 10 );
305 } else if ( type === 'float' ) {
306 return parseFloat( value );
307 } else if ( type === 'boolean' ) {
308 value = value.toString().toLowerCase().replace( /^\s+|\s+$/g, '' );
309 if ( value in { 1: null, yes: null, true: null } ) {
310 return true;
311 } else if ( value in { 0: null, no: null, false: null } ) {
312 return false;
313 } else {
314 return undefined;
315 }
316 } else if ( type === 'datetime' ) {
317 value = value.toString();
318 // https://datatracker.ietf.org/doc/html/rfc3339
319 // adapted from https://stackoverflow.com/questions/3143070/regex-to-match-an-iso-8601-datetime-string
320 const rfc3339 = /\d{4}-[01]\d-[0-3]\d(T[0-2]\d:[0-5]\d:[0-5]\d(\.\d+)?Z?)?/;
321 const match = value.match( rfc3339 );
322 if ( !match ) {
323 return value.replace( /<.*?>/g, '' );
324 }
325 value = match[ 0 ];
326 if ( value.match( /^\d{4}-00-00/ ) ) {
327 // assume yyyy
328 return value.slice( 0, 4 );
329 }
330 return value;
331 } else if ( type === 'list' ) {
332 return value === '' ? [] : value.split( '|' );
333 } else {
334 throw new Error( 'Image.parseExtmeta: unknown type' );
335 }
336 }
337
338 /**
339 * Add a thumb URL
340 *
341 * @param {number} width
342 * @param {string} url
343 */
344 addThumbUrl( width, url ) {
345 this.thumbUrls[ width ] = url;
346 }
347
348 /**
349 * Get a thumb URL if we have it.
350 *
351 * @param {number} width
352 * @return {string|undefined}
353 */
354 getThumbUrl( width ) {
355 return this.thumbUrls[ width ];
356 }
357
358 /**
359 * Check whether the image has geolocation data.
360 *
361 * @return {boolean}
362 */
363 hasCoords() {
364 return this.latitude !== undefined && this.latitude !== null &&
365 this.longitude !== undefined && this.longitude !== null;
366 }
367 }
368
369 module.exports = ImageModel;