1 ( function ( mw
, $, OO
) {
3 * Used to represent an upload in progress on the frontend.
5 * This subclass will upload to a wiki using a structured metadata
6 * system similar to (or identical to) the one on Wikimedia Commons.
8 * See <https://commons.wikimedia.org/wiki/Commons:Structured_data> for
9 * a more detailed description of how that system works.
11 * **TODO: This currently only supports uploads under CC-BY-SA 4.0,
12 * and should really have support for more licenses.**
14 * @class mw.ForeignStructuredUpload
15 * @extends mw.ForeignUpload
18 * @param {string} [target]
19 * @param {Object} [apiconfig]
21 function ForeignStructuredUpload( target
, apiconfig
) {
22 this.date
= undefined;
23 this.descriptions
= [];
26 // Config for uploads to local wiki.
27 // Can be overridden with foreign wiki config when #loadConfig is called.
28 this.config
= mw
.config
.get( 'wgUploadDialog' );
30 mw
.ForeignUpload
.call( this, target
, apiconfig
);
33 OO
.inheritClass( ForeignStructuredUpload
, mw
.ForeignUpload
);
36 * Get the configuration for the form and filepage from the foreign wiki, if any, and use it for
39 * @return {jQuery.Promise} Promise returning config object
41 ForeignStructuredUpload
.prototype.loadConfig = function () {
45 if ( this.configPromise
) {
46 return this.configPromise
;
49 if ( this.target
=== 'local' ) {
50 deferred
= $.Deferred();
51 setTimeout( function () {
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
);
56 this.configPromise
= deferred
.promise();
58 this.configPromise
= this.apiPromise
.then( function ( api
) {
59 // Get the config from the foreign wiki
63 siprop
: 'uploaddialog',
64 // For convenient true/false booleans
66 } ).then( function ( 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
;
72 return $.Deferred().reject( 'upload-foreign-cant-load-config' );
75 return $.Deferred().reject( 'upload-foreign-cant-load-config' );
80 return this.configPromise
;
84 * Add categories to the upload.
86 * @param {string[]} categories Array of categories to which this upload will be added.
88 ForeignStructuredUpload
.prototype.addCategories = function ( categories
) {
89 // The length of the array must be less than 10000.
90 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push#Merging_two_arrays
91 Array
.prototype.push
.apply( this.categories
, categories
);
95 * Empty the list of categories for the upload.
97 ForeignStructuredUpload
.prototype.clearCategories = function () {
102 * Add a description to the upload.
104 * @param {string} language The language code for the description's language. Must have a template on the target wiki to work properly.
105 * @param {string} description The description of the file.
107 ForeignStructuredUpload
.prototype.addDescription = function ( language
, description
) {
108 this.descriptions
.push( {
115 * Empty the list of descriptions for the upload.
117 ForeignStructuredUpload
.prototype.clearDescriptions = function () {
118 this.descriptions
= [];
122 * Set the date of creation for the upload.
126 ForeignStructuredUpload
.prototype.setDate = function ( date
) {
131 * Get the text of the file page, to be created on upload. Brings together
132 * several different pieces of information to create useful text.
136 ForeignStructuredUpload
.prototype.getText = function () {
137 return this.config
.format
.filepage
138 // Replace "named parameters" with the given information
139 .replace( '$DESCRIPTION', this.getDescriptions() )
140 .replace( '$DATE', this.getDate() )
141 .replace( '$SOURCE', this.getSource() )
142 .replace( '$AUTHOR', this.getUser() )
143 .replace( '$LICENSE', this.getLicense() )
144 .replace( '$CATEGORIES', this.getCategories() );
150 ForeignStructuredUpload
.prototype.getComment = function () {
152 isLocal
= this.target
=== 'local',
153 comment
= typeof this.config
.comment
=== 'string' ?
154 this.config
.comment
:
155 this.config
.comment
[ isLocal
? 'local' : 'foreign' ];
157 .replace( '$PAGENAME', mw
.config
.get( 'wgPageName' ) )
158 .replace( '$HOST', location
.host
);
162 * Gets the wikitext for the creation date of this upload.
167 ForeignStructuredUpload
.prototype.getDate = function () {
172 return this.date
.toString();
176 * Fetches the wikitext for any descriptions that have been added
182 ForeignStructuredUpload
.prototype.getDescriptions = function () {
184 return this.descriptions
.map( function ( desc
) {
185 return upload
.config
.format
.description
186 .replace( '$LANGUAGE', desc
.language
)
187 .replace( '$TEXT', desc
.text
);
192 * Fetches the wikitext for the categories to which the upload will
198 ForeignStructuredUpload
.prototype.getCategories = function () {
199 if ( this.categories
.length
=== 0 ) {
200 return this.config
.format
.uncategorized
;
203 return this.categories
.map( function ( cat
) {
204 return '[[Category:' + cat
+ ']]';
209 * Gets the wikitext for the license of the upload.
214 ForeignStructuredUpload
.prototype.getLicense = function () {
215 return this.config
.format
.license
;
219 * Get the source. This should be some sort of localised text for "Own work".
224 ForeignStructuredUpload
.prototype.getSource = function () {
225 return this.config
.format
.ownwork
;
234 ForeignStructuredUpload
.prototype.getUser = function () {
235 var username
, namespace;
236 // Do not localise, we don't know the language of target wiki
238 username
= mw
.config
.get( 'wgUserName' );
240 // The user is not logged in locally. However, they might be logged in on the foreign wiki.
241 // We should record their username there. (If they're not logged in there either, this will
242 // record the IP address.) It's also possible that the user opened this dialog, got an error
243 // about not being logged in, logged in in another browser tab, then continued uploading.
244 username
= '{{subst:REVISIONUSER}}';
246 return '[[' + namespace + ':' + username
+ '|' + username
+ ']]';
249 mw
.ForeignStructuredUpload
= ForeignStructuredUpload
;
250 }( mediaWiki
, jQuery
, OO
) );