Merge "Add tests for WikiMap and WikiReference"
[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( targetHost, apiconfig ) {
20 this.date = undefined;
21 this.descriptions = [];
22 this.categories = [];
23
24 mw.ForeignUpload.call( this, targetHost, 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 * Add a description to the upload.
45 *
46 * @param {string} language The language code for the description's language. Must have a template on the target wiki to work properly.
47 * @param {string} description The description of the file.
48 */
49 ForeignStructuredUpload.prototype.addDescription = function ( language, description ) {
50 this.descriptions.push( {
51 language: language,
52 text: description
53 } );
54 };
55
56 /**
57 * Set the date of creation for the upload.
58 *
59 * @param {Date} date
60 */
61 ForeignStructuredUpload.prototype.setDate = function ( date ) {
62 this.date = date;
63 };
64
65 /**
66 * Get the text of the file page, to be created on upload. Brings together
67 * several different pieces of information to create useful text.
68 *
69 * @return {string}
70 */
71 ForeignStructuredUpload.prototype.getText = function () {
72 return (
73 '{{' +
74 this.getTemplateName() +
75 '\n|description=' +
76 this.getDescriptions() +
77 '\n|date=' +
78 this.getDate() +
79 '\n|source=' +
80 this.getUser() +
81 '\n|author=' +
82 this.getUser() +
83 '\n}}\n\n' +
84 this.getLicense() +
85 '\n\n' +
86 this.getCategories()
87 );
88 };
89
90 /**
91 * Gets the wikitext for the creation date of this upload.
92 *
93 * @private
94 * @return {string}
95 */
96 ForeignStructuredUpload.prototype.getDate = function () {
97 if ( !this.date ) {
98 return '';
99 }
100
101 return this.date.toString();
102 };
103
104 /**
105 * Gets the name of the template to use for creating the file metadata.
106 * Override in subclasses for other templates.
107 *
108 * @private
109 * @return {string}
110 */
111 ForeignStructuredUpload.prototype.getTemplateName = function () {
112 return 'Information';
113 };
114
115 /**
116 * Fetches the wikitext for any descriptions that have been added
117 * to the upload.
118 *
119 * @private
120 * @return {string}
121 */
122 ForeignStructuredUpload.prototype.getDescriptions = function () {
123 var i, desc, templateCalls = [];
124
125 for ( i = 0; i < this.descriptions.length; i++ ) {
126 desc = this.descriptions[ i ];
127 templateCalls.push( '{{' + desc.language + '|' + desc.text + '}}' );
128 }
129
130 return templateCalls.join( '\n' );
131 };
132
133 /**
134 * Fetches the wikitext for the categories to which the upload will
135 * be added.
136 *
137 * @private
138 * @return {string}
139 */
140 ForeignStructuredUpload.prototype.getCategories = function () {
141 var i, cat, categoryLinks = [];
142
143 for ( i = 0; i < this.categories.length; i++ ) {
144 cat = this.categories[ i ];
145 categoryLinks.push( '[[Category:' + cat + ']]' );
146 }
147
148 return categoryLinks.join( '\n' );
149 };
150
151 /**
152 * Gets the wikitext for the license of the upload. Abstract for now.
153 *
154 * @private
155 * @return {string}
156 */
157 ForeignStructuredUpload.prototype.getLicense = function () {
158 return '';
159 };
160
161 /**
162 * Get the username.
163 *
164 * @private
165 * @return {string}
166 */
167 ForeignStructuredUpload.prototype.getUser = function () {
168 return mw.config.get( 'wgUserName' );
169 };
170
171 mw.ForeignStructuredUpload = ForeignStructuredUpload;
172 }( mediaWiki, OO ) );