Merge "Remove register_globals and magic_quotes_* checks"
[lhc/web/wiklou.git] / resources / src / mediawiki / mediawiki.ForeignStructuredUpload.js
1 ( function ( mw, OO ) {
2 /**
3 * @class mw.ForeignStructuredUpload
4 * @extends mw.ForeignUpload
5 *
6 * Used to represent an upload in progress on the frontend.
7 *
8 * This subclass will upload to a wiki using a structured metadata
9 * system similar to (or identical to) the one on Wikimedia Commons.
10 *
11 * See <https://commons.wikimedia.org/wiki/Commons:Structured_data> for
12 * a more detailed description of how that system works.
13 *
14 * **TODO: This currently only supports uploads under CC-BY-SA 4.0,
15 * and should really have support for more licenses.**
16 *
17 * @inheritdoc
18 */
19 function ForeignStructuredUpload( target, apiconfig ) {
20 this.date = undefined;
21 this.descriptions = [];
22 this.categories = [];
23
24 mw.ForeignUpload.call( this, target, apiconfig );
25 }
26
27 OO.inheritClass( ForeignStructuredUpload, mw.ForeignUpload );
28
29 /**
30 * Add categories to the upload.
31 *
32 * @param {string[]} categories Array of categories to which this upload will be added.
33 */
34 ForeignStructuredUpload.prototype.addCategories = function ( categories ) {
35 var i, category;
36
37 for ( i = 0; i < categories.length; i++ ) {
38 category = categories[ i ];
39 this.categories.push( category );
40 }
41 };
42
43 /**
44 * Empty the list of categories for the upload.
45 */
46 ForeignStructuredUpload.prototype.clearCategories = function () {
47 this.categories = [];
48 };
49
50 /**
51 * Add a description to the upload.
52 *
53 * @param {string} language The language code for the description's language. Must have a template on the target wiki to work properly.
54 * @param {string} description The description of the file.
55 */
56 ForeignStructuredUpload.prototype.addDescription = function ( language, description ) {
57 this.descriptions.push( {
58 language: language,
59 text: description
60 } );
61 };
62
63 /**
64 * Empty the list of descriptions for the upload.
65 */
66 ForeignStructuredUpload.prototype.clearDescriptions = function () {
67 this.descriptions = [];
68 };
69
70 /**
71 * Set the date of creation for the upload.
72 *
73 * @param {Date} date
74 */
75 ForeignStructuredUpload.prototype.setDate = function ( date ) {
76 this.date = date;
77 };
78
79 /**
80 * Get the text of the file page, to be created on upload. Brings together
81 * several different pieces of information to create useful text.
82 *
83 * @return {string}
84 */
85 ForeignStructuredUpload.prototype.getText = function () {
86 return (
87 '== {{int:filedesc}} ==\n' +
88 '{{' +
89 this.getTemplateName() +
90 '\n|description=' +
91 this.getDescriptions() +
92 '\n|date=' +
93 this.getDate() +
94 '\n|source=' +
95 this.getSource() +
96 '\n|author=' +
97 this.getUser() +
98 '\n}}\n\n' +
99 '== {{int:license-header}} ==\n' +
100 this.getLicense() +
101 '\n\n' +
102 this.getCategories()
103 );
104 };
105
106 /**
107 * Gets the wikitext for the creation date of this upload.
108 *
109 * @private
110 * @return {string}
111 */
112 ForeignStructuredUpload.prototype.getDate = function () {
113 if ( !this.date ) {
114 return '';
115 }
116
117 return this.date.toString();
118 };
119
120 /**
121 * Gets the name of the template to use for creating the file metadata.
122 * Override in subclasses for other templates.
123 *
124 * @private
125 * @return {string}
126 */
127 ForeignStructuredUpload.prototype.getTemplateName = function () {
128 return 'Information';
129 };
130
131 /**
132 * Fetches the wikitext for any descriptions that have been added
133 * to the upload.
134 *
135 * @private
136 * @return {string}
137 */
138 ForeignStructuredUpload.prototype.getDescriptions = function () {
139 var i, desc, templateCalls = [];
140
141 for ( i = 0; i < this.descriptions.length; i++ ) {
142 desc = this.descriptions[ i ];
143 templateCalls.push( '{{' + desc.language + '|1=' + desc.text + '}}' );
144 }
145
146 return templateCalls.join( '\n' );
147 };
148
149 /**
150 * Fetches the wikitext for the categories to which the upload will
151 * be added.
152 *
153 * @private
154 * @return {string}
155 */
156 ForeignStructuredUpload.prototype.getCategories = function () {
157 var i, cat, categoryLinks = [];
158
159 if ( this.categories.length === 0 ) {
160 return '{{subst:unc}}';
161 }
162
163 for ( i = 0; i < this.categories.length; i++ ) {
164 cat = this.categories[ i ];
165 categoryLinks.push( '[[Category:' + cat + ']]' );
166 }
167
168 return categoryLinks.join( '\n' );
169 };
170
171 /**
172 * Gets the wikitext for the license of the upload.
173 *
174 * @private
175 * @return {string}
176 */
177 ForeignStructuredUpload.prototype.getLicense = function () {
178 // Make sure this matches the messages for different targets in
179 // mw.ForeignStructuredUpload.BookletLayout.prototype.renderUploadForm
180 return this.target === 'shared' ? '{{self|cc-by-sa-4.0}}' : '';
181 };
182
183 /**
184 * Get the source. This should be some sort of localised text for "Own work".
185 *
186 * @private
187 * @return {string}
188 */
189 ForeignStructuredUpload.prototype.getSource = function () {
190 return '{{own}}';
191 };
192
193 /**
194 * Get the username.
195 *
196 * @private
197 * @return {string}
198 */
199 ForeignStructuredUpload.prototype.getUser = function () {
200 var username, namespace;
201 // Do not localise, we don't know the language of target wiki
202 namespace = 'User';
203 username = mw.config.get( 'wgUserName' );
204 if ( !username ) {
205 // The user is not logged in locally. However, they might be logged in on the foreign wiki.
206 // We should record their username there. (If they're not logged in there either, this will
207 // record the IP address.) It's also possible that the user opened this dialog, got an error
208 // about not being logged in, logged in in another browser tab, then continued uploading.
209 username = '{{subst:REVISIONUSER}}';
210 }
211 return '[[' + namespace + ':' + username + '|' + username + ']]';
212 };
213
214 mw.ForeignStructuredUpload = ForeignStructuredUpload;
215 }( mediaWiki, OO ) );