1 ( function ( mw
, $, OO
) {
3 * @class mw.ForeignStructuredUpload
4 * @extends mw.ForeignUpload
6 * Used to represent an upload in progress on the frontend.
8 * This subclass will upload to a wiki using a structured metadata
9 * system similar to (or identical to) the one on Wikimedia Commons.
11 * See <https://commons.wikimedia.org/wiki/Commons:Structured_data> for
12 * a more detailed description of how that system works.
14 * **TODO: This currently only supports uploads under CC-BY-SA 4.0,
15 * and should really have support for more licenses.**
19 function ForeignStructuredUpload( target
, apiconfig
) {
20 this.date
= undefined;
21 this.descriptions
= [];
24 // Config for uploads to local wiki.
25 // Can be overridden with foreign wiki config when #loadConfig is called.
26 this.config
= mw
.config
.get( 'wgUploadDialog' );
28 mw
.ForeignUpload
.call( this, target
, apiconfig
);
31 OO
.inheritClass( ForeignStructuredUpload
, mw
.ForeignUpload
);
34 * Get the configuration for the form and filepage from the foreign wiki, if any, and use it for
37 * @return {jQuery.Promise} Promise returning config object
39 ForeignStructuredUpload
.prototype.loadConfig = function () {
43 if ( this.configPromise
) {
44 return this.configPromise
;
47 if ( this.target
=== 'local' ) {
48 deferred
= $.Deferred();
49 setTimeout( function () {
50 // Resolve asynchronously, so that it's harder to accidentally write synchronous code that
51 // will break for cross-wiki uploads
52 deferred
.resolve( upload
.config
);
54 this.configPromise
= deferred
.promise();
56 this.configPromise
= this.apiPromise
.then( function ( api
) {
57 // Get the config from the foreign wiki
61 siprop
: 'uploaddialog',
62 // For convenient true/false booleans
64 } ).then( function ( resp
) {
65 // Foreign wiki might be running a pre-1.27 MediaWiki, without support for this
66 if ( resp
.query
&& resp
.query
.uploaddialog
) {
67 upload
.config
= resp
.query
.uploaddialog
;
70 return $.Deferred().reject( 'upload-foreign-cant-load-config' );
73 return $.Deferred().reject( 'upload-foreign-cant-load-config' );
78 return this.configPromise
;
82 * Add categories to the upload.
84 * @param {string[]} categories Array of categories to which this upload will be added.
86 ForeignStructuredUpload
.prototype.addCategories = function ( categories
) {
89 for ( i
= 0; i
< categories
.length
; i
++ ) {
90 category
= categories
[ i
];
91 this.categories
.push( category
);
96 * Empty the list of categories for the upload.
98 ForeignStructuredUpload
.prototype.clearCategories = function () {
103 * Add a description to the upload.
105 * @param {string} language The language code for the description's language. Must have a template on the target wiki to work properly.
106 * @param {string} description The description of the file.
108 ForeignStructuredUpload
.prototype.addDescription = function ( language
, description
) {
109 this.descriptions
.push( {
116 * Empty the list of descriptions for the upload.
118 ForeignStructuredUpload
.prototype.clearDescriptions = function () {
119 this.descriptions
= [];
123 * Set the date of creation for the upload.
127 ForeignStructuredUpload
.prototype.setDate = function ( date
) {
132 * Get the text of the file page, to be created on upload. Brings together
133 * several different pieces of information to create useful text.
137 ForeignStructuredUpload
.prototype.getText = function () {
138 return this.config
.format
.filepage
139 // Replace "named parameters" with the given information
140 .replace( '$DESCRIPTION', this.getDescriptions() )
141 .replace( '$DATE', this.getDate() )
142 .replace( '$SOURCE', this.getSource() )
143 .replace( '$AUTHOR', this.getUser() )
144 .replace( '$LICENSE', this.getLicense() )
145 .replace( '$CATEGORIES', this.getCategories() );
151 ForeignStructuredUpload
.prototype.getComment = function () {
153 isLocal
= this.target
=== 'local',
154 comment
= typeof this.config
.comment
=== 'string' ?
155 this.config
.comment
:
156 this.config
.comment
[ isLocal
? 'local' : 'foreign' ];
158 .replace( '$PAGENAME', mw
.config
.get( 'wgPageName' ) )
159 .replace( '$HOST', location
.host
);
163 * Gets the wikitext for the creation date of this upload.
168 ForeignStructuredUpload
.prototype.getDate = function () {
173 return this.date
.toString();
177 * Fetches the wikitext for any descriptions that have been added
183 ForeignStructuredUpload
.prototype.getDescriptions = function () {
184 var i
, desc
, templateCalls
= [];
186 for ( i
= 0; i
< this.descriptions
.length
; i
++ ) {
187 desc
= this.descriptions
[ i
];
189 this.config
.format
.description
190 .replace( '$LANGUAGE', desc
.language
)
191 .replace( '$TEXT', desc
.text
)
195 return templateCalls
.join( '\n' );
199 * Fetches the wikitext for the categories to which the upload will
205 ForeignStructuredUpload
.prototype.getCategories = function () {
206 var i
, cat
, categoryLinks
= [];
208 if ( this.categories
.length
=== 0 ) {
209 return this.config
.format
.uncategorized
;
212 for ( i
= 0; i
< this.categories
.length
; i
++ ) {
213 cat
= this.categories
[ i
];
214 categoryLinks
.push( '[[Category:' + cat
+ ']]' );
217 return categoryLinks
.join( '\n' );
221 * Gets the wikitext for the license of the upload.
226 ForeignStructuredUpload
.prototype.getLicense = function () {
227 return this.config
.format
.license
;
231 * Get the source. This should be some sort of localised text for "Own work".
236 ForeignStructuredUpload
.prototype.getSource = function () {
237 return this.config
.format
.ownwork
;
246 ForeignStructuredUpload
.prototype.getUser = function () {
247 var username
, namespace;
248 // Do not localise, we don't know the language of target wiki
250 username
= mw
.config
.get( 'wgUserName' );
252 // The user is not logged in locally. However, they might be logged in on the foreign wiki.
253 // We should record their username there. (If they're not logged in there either, this will
254 // record the IP address.) It's also possible that the user opened this dialog, got an error
255 // about not being logged in, logged in in another browser tab, then continued uploading.
256 username
= '{{subst:REVISIONUSER}}';
258 return '[[' + namespace + ':' + username
+ '|' + username
+ ']]';
261 mw
.ForeignStructuredUpload
= ForeignStructuredUpload
;
262 }( mediaWiki
, jQuery
, OO
) );