Debian Codegraph @

extensions/MultimediaViewer/resources/mmv/provider/mmv.provider.ImageInfo.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 Api = require( './mmv.provider.Api.js' );
19 const ImageModel = require( '../model/mmv.model.Image.js' );
20
21 /**
22 * Gets file information.
23 *
24 * See https://www.mediawiki.org/wiki/API:Properties#imageinfo_.2F_ii
25 */
26 class ImageInfo extends Api {
27 /**
28 * @param {mw.Api} api
29 * @param {Object} [options]
30 * @param {string} [options.language=null] image metadata language
31 * @param {number} [options.maxage] cache expiration time, in seconds
32 * Will be used for both client-side cache (maxage) and reverse proxies (s-maxage)
33 */
34 constructor( api, options ) {
35 options = Object.assign( {
36 language: null
37 }, options );
38
39 super( api, options );
40 }
41
42 /**
43 * Array of imageinfo API properties which are needed to construct an Image model.
44 *
45 * @return {string[]}
46 */
47 get iiprop() {
48 return [
49 'timestamp',
50 'url',
51 'size',
52 'mime',
53 'mediatype',
54 'extmetadata'
55 ];
56 }
57
58 /**
59 * Array of imageinfo extmetadata fields which are needed to construct an Image model.
60 *
61 * @return {string[]}
62 */
63 get iiextmetadatafilter() {
64 return [
65 'DateTime',
66 'DateTimeOriginal',
67 'ObjectName',
68 'ImageDescription',
69 'License',
70 'LicenseShortName',
71 'UsageTerms',
72 'LicenseUrl',
73 'Credit',
74 'Artist',
75 'AuthorCount',
76 'GPSLatitude',
77 'GPSLongitude',
78 'Permission',
79 'Attribution',
80 'AttributionRequired',
81 'NonFree',
82 'Restrictions',
83 'DeletionReason'
84 ];
85 }
86
87 /**
88 * Runs an API GET request to get the image info.
89 *
90 * @param {mw.Title} file
91 * @return {jQuery.Promise} a promise which resolves to an Image object.
92 */
93 get( file ) {
94 return this.getCachedPromise( file.getPrefixedDb(), () => this.apiGetWithMaxAge( {
95 formatversion: 2,
96 action: 'query',
97 prop: 'imageinfo',
98 titles: file.getPrefixedDb(),
99 iiprop: this.iiprop,
100 iiextmetadatafilter: this.iiextmetadatafilter,
101 iiextmetadatalanguage: this.options.language,
102 uselang: 'content'
103 } ).then( ( data ) => this.getQueryPage( data ) ).then( ( page ) => {
104 if ( page.imageinfo && page.imageinfo.length ) {
105 return ImageModel.newFromImageInfo( file, page );
106 } else if ( page.missing === true && page.imagerepository === '' ) {
107 return $.Deferred().reject( `file does not exist: ${ file.getPrefixedDb() }` );
108 } else {
109 return $.Deferred().reject( 'unknown error' );
110 }
111 } ) );
112 }
113 }
114
115 module.exports = ImageInfo;