Debian Codegraph @

resources/src/mediawiki.ForeignStructuredUpload.BookletLayout/ForeignStructuredUpload.js

raw symbols SCIP
1 ( function () {
2 /**
3 * @classdesc Upload to another MediaWiki site using structured metadata.
4 *
5 * This subclass uses a structured metadata system similar to
6 * (or identical to) the one on Wikimedia Commons.
7 * See <https://commons.wikimedia.org/wiki/Commons:Structured_data> for
8 * a more detailed description of how that system works.
9 *
10 * TODO: This currently only supports uploads under CC-BY-SA 4.0,
11 * and should really have support for more licenses.
12 *
13 * @class ForeignStructuredUpload
14 * @memberof mw
15 * @extends mw.ForeignUpload
16 *
17 * @constructor
18 * @description Used to represent an upload in progress on the frontend.
19 * @param {string} [target]
20 * @param {Object} [apiconfig]
21 */
22 function ForeignStructuredUpload( target, apiconfig ) {
23 this.date = undefined;
24 this.descriptions = [];
25 this.categories = [];
26
27 // Config for uploads to local wiki.
28 // Can be overridden with foreign wiki config when #loadConfig is called.
29 this.config = require( './config.json' ).UploadDialog;
30
31 mw.ForeignUpload.call( this, target, apiconfig );
32 }
33
34 OO.inheritClass( ForeignStructuredUpload, mw.ForeignUpload );
35
36 /**
37 * Get the configuration for the form and filepage from the foreign wiki, if any, and use it for
38 * this upload.
39 *
40 * @return {jQuery.Promise} Promise returning config object
41 */
42 ForeignStructuredUpload.prototype.loadConfig = function () {
43 const upload = this;
44
45 if ( this.configPromise ) {
46 return this.configPromise;
47 }
48
49 if ( this.target === 'local' ) {
50 const deferred = $.Deferred();
51 setTimeout( () => {
52 // Resolve asynchronously, so that it's harder to accidentally write synchronous code that
53 // will break for cross-wiki uploads
54 deferred.resolve( upload.config );
55 } );
56 this.configPromise = deferred.promise();
57 } else {
58 this.configPromise = this.apiPromise.then(
59 // Get the config from the foreign wiki
60 ( api ) => api.get( {
61 action: 'query',
62 meta: 'siteinfo',
63 siprop: 'uploaddialog',
64 // For convenient true/false booleans
65 formatversion: 2
66 } ).then( ( resp ) => {
67 // Foreign wiki might be running a pre-1.27 MediaWiki, without support for this
68 if ( resp.query && resp.query.uploaddialog ) {
69 upload.config = resp.query.uploaddialog;
70 return upload.config;
71 } else {
72 return $.Deferred().reject( 'upload-foreign-cant-load-config' );
73 }
74 }, () => $.Deferred().reject( 'upload-foreign-cant-load-config' ) )
75 );
76 }
77
78 return this.configPromise;
79 };
80
81 /**
82 * Add categories to the upload.
83 *
84 * @param {string[]} categories Array of categories to which this upload will be added.
85 */
86 ForeignStructuredUpload.prototype.addCategories = function ( categories ) {
87 this.categories.push( ...categories );
88 };
89
90 /**
91 * Empty the list of categories for the upload.
92 */
93 ForeignStructuredUpload.prototype.clearCategories = function () {
94 this.categories = [];
95 };
96
97 /**
98 * Add a description to the upload.
99 *
100 * @param {string} language The language code for the description's language. Must have a template on the target wiki to work properly.
101 * @param {string} description The description of the file.
102 */
103 ForeignStructuredUpload.prototype.addDescription = function ( language, description ) {
104 this.descriptions.push( {
105 language: language,
106 text: description
107 } );
108 };
109
110 /**
111 * Empty the list of descriptions for the upload.
112 */
113 ForeignStructuredUpload.prototype.clearDescriptions = function () {
114 this.descriptions = [];
115 };
116
117 /**
118 * Set the date of creation for the upload.
119 *
120 * @param {Date} date
121 */
122 ForeignStructuredUpload.prototype.setDate = function ( date ) {
123 this.date = date;
124 };
125
126 /**
127 * Get the text of the file page, to be created on upload. Brings together
128 * several different pieces of information to create useful text.
129 *
130 * @return {string}
131 */
132 ForeignStructuredUpload.prototype.getText = function () {
133 return this.config.format.filepage
134 // Replace "named parameters" with the given information
135 .replace( '$DESCRIPTION', this.getDescriptions() )
136 .replace( '$DATE', this.getDate() )
137 .replace( '$SOURCE', this.getSource() )
138 .replace( '$AUTHOR', this.getUser() )
139 .replace( '$LICENSE', this.getLicense() )
140 .replace( '$CATEGORIES', this.getCategories() );
141 };
142
143 /**
144 * @inheritdoc
145 */
146 ForeignStructuredUpload.prototype.getComment = function () {
147 const
148 isLocal = this.target === 'local',
149 comment = typeof this.config.comment === 'string' ?
150 this.config.comment :
151 this.config.comment[ isLocal ? 'local' : 'foreign' ],
152 pagename = mw.config.get( 'wgPageName' );
153 return comment
154 .replace( '$PAGENAME', pagename.replace( /_/g, ' ' ) )
155 .replace( '$HOST', location.host );
156 };
157
158 /**
159 * Gets the wikitext for the creation date of this upload.
160 *
161 * @private
162 * @return {string}
163 */
164 ForeignStructuredUpload.prototype.getDate = function () {
165 if ( !this.date ) {
166 return '';
167 }
168
169 return this.date.toString();
170 };
171
172 /**
173 * Fetches the wikitext for any descriptions that have been added
174 * to the upload.
175 *
176 * @private
177 * @return {string}
178 */
179 ForeignStructuredUpload.prototype.getDescriptions = function () {
180 const upload = this;
181 return this.descriptions.map( ( desc ) => upload.config.format.description
182 .replace( '$LANGUAGE', desc.language )
183 .replace( '$TEXT', desc.text ) ).join( '\n' );
184 };
185
186 /**
187 * Fetches the wikitext for the categories to which the upload will
188 * be added.
189 *
190 * @private
191 * @return {string}
192 */
193 ForeignStructuredUpload.prototype.getCategories = function () {
194 if ( this.categories.length === 0 ) {
195 return this.config.format.uncategorized;
196 }
197
198 return this.categories.map( ( cat ) => '[[Category:' + cat + ']]' ).join( '\n' );
199 };
200
201 /**
202 * Gets the wikitext for the license of the upload.
203 *
204 * @private
205 * @return {string}
206 */
207 ForeignStructuredUpload.prototype.getLicense = function () {
208 return this.config.format.license;
209 };
210
211 /**
212 * Get the source. This should be some sort of localised text for "Own work".
213 *
214 * @private
215 * @return {string}
216 */
217 ForeignStructuredUpload.prototype.getSource = function () {
218 return this.config.format.ownwork;
219 };
220
221 /**
222 * Get the username.
223 *
224 * @private
225 * @return {string}
226 */
227 ForeignStructuredUpload.prototype.getUser = function () {
228 // Do not localise, we don't know the language of target wiki
229 const namespace = 'User';
230 let username = mw.config.get( 'wgUserName' );
231 if ( !username ) {
232 // The user is not logged in locally. However, they might be logged in on the foreign wiki.
233 // We should record their username there. (If they're not logged in there either, this will
234 // record the IP address.) It's also possible that the user opened this dialog, got an error
235 // about not being logged in, logged in in another browser tab, then continued uploading.
236 username = '{{subst:REVISIONUSER}}';
237 }
238 return '[[' + namespace + ':' + username + '|' + username + ']]';
239 };
240
241 mw.ForeignStructuredUpload = ForeignStructuredUpload;
242 }() );