Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/init/ve.init.mw.Platform.js

raw symbols SCIP
1 /*!
2 * VisualEditor MediaWiki Initialization Platform class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Initialization MediaWiki platform.
10 *
11 * @class
12 * @extends ve.init.Platform
13 *
14 * @constructor
15 */
16 ve.init.mw.Platform = function VeInitMwPlatform() {
17 // Parent constructor
18 ve.init.mw.Platform.super.call( this );
19
20 // Properties
21 this.externalLinkUrlProtocolsRegExp = new RegExp(
22 '^(' + mw.config.get( 'wgUrlProtocols' ) + ')',
23 'i'
24 );
25 this.unanchoredExternalLinkUrlProtocolsRegExp = new RegExp(
26 '(' + mw.config.get( 'wgUrlProtocols' ) + ')',
27 'i'
28 );
29 this.parsedMessages = {};
30 this.linkCache = new ve.init.mw.LinkCache();
31 this.imageInfoCache = new ve.init.mw.ImageInfoCache();
32 this.galleryImageInfoCache = new ve.init.mw.GalleryImageInfoCache();
33 };
34
35 /* Inheritance */
36
37 OO.inheritClass( ve.init.mw.Platform, ve.init.Platform );
38
39 /* Methods */
40
41 /** @inheritdoc */
42 ve.init.mw.Platform.prototype.getExternalLinkUrlProtocolsRegExp = function () {
43 return this.externalLinkUrlProtocolsRegExp;
44 };
45
46 /** @inheritdoc */
47 ve.init.mw.Platform.prototype.getUnanchoredExternalLinkUrlProtocolsRegExp = function () {
48 return this.unanchoredExternalLinkUrlProtocolsRegExp;
49 };
50
51 /** @inheritdoc */
52 ve.init.mw.Platform.prototype.notify = function ( message, title, options ) {
53 return mw.notify( message, ve.extendObject( { title: title }, options ) );
54 };
55
56 /**
57 * Regular expression matching RESTBase IDs
58 *
59 * This isn't perfect, see T147607
60 *
61 * @inheritdoc
62 */
63 ve.init.mw.Platform.prototype.getMetadataIdRegExp = function () {
64 return mw.libs.ve.restbaseIdRegExp;
65 };
66
67 /** @inheritdoc */
68 ve.init.mw.Platform.prototype.addMessages = function ( messages ) {
69 return mw.messages.set( messages );
70 };
71
72 /**
73 * @method
74 * @inheritdoc
75 */
76 ve.init.mw.Platform.prototype.getMessage = mw.msg.bind( mw );
77
78 /**
79 * @method
80 * @inheritdoc
81 */
82 ve.init.mw.Platform.prototype.getUserName = function () {
83 return mw.user.getName();
84 };
85
86 /**
87 * @method
88 * @inheritdoc
89 */
90 ve.init.mw.Platform.prototype.parseNumber = function ( value ) {
91 const number = $.tablesorter.getParser( 'number' ).format( value );
92 // formatDigit returns -Infinity when parsing fails, change this to NaN
93 return number !== -Infinity ? number : NaN;
94 };
95
96 /**
97 * @method
98 * @inheritdoc
99 */
100 ve.init.mw.Platform.prototype.formatNumber = function ( number ) {
101 return mw.language.convertNumber( number );
102 };
103
104 /**
105 * @inheritdoc
106 */
107 ve.init.mw.Platform.prototype.getHtmlMessage = function () {
108 return mw.message.apply( mw.message, arguments ).parseDom().toArray();
109 };
110
111 /**
112 * @method
113 * @inheritdoc
114 */
115 ve.init.mw.Platform.prototype.getConfig = mw.config.get.bind( mw.config );
116
117 /**
118 * All values are JSON-parsed. To get raw values, use mw.user.options.get directly.
119 *
120 * @inheritdoc
121 */
122 ve.init.mw.Platform.prototype.getUserConfig = function ( keys ) {
123 if ( Array.isArray( keys ) ) {
124 const values = mw.user.options.get( keys );
125 const parsedValues = {};
126 Object.keys( values ).forEach( ( value ) => {
127 try {
128 parsedValues[ value ] = JSON.parse( values[ value ] );
129 } catch ( e ) {
130 // We might encounter corrupted values in the store
131 parsedValues[ value ] = null;
132 }
133 } );
134 return parsedValues;
135 } else {
136 try {
137 return JSON.parse( mw.user.options.get( keys ) );
138 } catch ( e ) {
139 // We might encounter corrupted values in the store
140 return null;
141 }
142 }
143 };
144
145 /**
146 * Options must be registered in onGetPreferences
147 *
148 * All values are JSON encoded. To set raw values, use mw.user.options.set directly.
149 *
150 * @inheritdoc
151 */
152 ve.init.mw.Platform.prototype.setUserConfig = function ( keyOrValueMap, value ) {
153 // T214963: Don't try to set user preferences for logged-out users, it doesn't work
154 if ( !mw.user.isNamed() ) {
155 return false;
156 }
157
158 if ( typeof keyOrValueMap === 'object' ) {
159 if ( OO.compare( keyOrValueMap, this.getUserConfig( Object.keys( keyOrValueMap ) ) ) ) {
160 return false;
161 }
162 // JSON encode all the values for API storage
163 const jsonValues = {};
164 Object.keys( keyOrValueMap ).forEach( ( key ) => {
165 jsonValues[ key ] = JSON.stringify( keyOrValueMap[ key ] );
166 } );
167 ve.init.target.getLocalApi().saveOptions( jsonValues );
168 return mw.user.options.set( jsonValues );
169 } else {
170 if ( value === this.getUserConfig( keyOrValueMap ) ) {
171 return false;
172 }
173 // JSON encode the value for API storage
174 const jsonValue = JSON.stringify( value );
175 ve.init.target.getLocalApi().saveOption( keyOrValueMap, jsonValue );
176 return mw.user.options.set( keyOrValueMap, jsonValue );
177 }
178 };
179
180 ve.init.mw.Platform.prototype.createLocalStorage = function () {
181 return this.createConflictableStorage( mw.storage );
182 };
183
184 ve.init.mw.Platform.prototype.createSessionStorage = function () {
185 return this.createConflictableStorage( mw.storage.session );
186 };
187
188 /**
189 * @inheritdoc
190 */
191 ve.init.mw.Platform.prototype.addParsedMessages = function ( messages ) {
192 for ( const key in messages ) {
193 this.parsedMessages[ key ] = messages[ key ];
194 }
195 };
196
197 /**
198 * @inheritdoc
199 */
200 ve.init.mw.Platform.prototype.getParsedMessage = function ( key ) {
201 if ( Object.prototype.hasOwnProperty.call( this.parsedMessages, key ) ) {
202 // Prefer parsed results from VisualEditorDataModule if available.
203 return this.parsedMessages[ key ];
204 }
205 // Fallback to regular messages, with mw.message html escaping applied.
206 // eslint-disable-next-line mediawiki/msg-doc
207 return mw.message( key ).escaped();
208 };
209
210 /**
211 * @inheritdoc
212 */
213 ve.init.mw.Platform.prototype.getLanguageCodes = function () {
214 return Object.keys(
215 mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'languageNames' ) ||
216 $.uls.data.getAutonyms()
217 );
218 };
219
220 /**
221 * @inheritdoc
222 */
223 ve.init.mw.Platform.prototype.getLanguageName = function ( code ) {
224 const languageNames = mw.language.getData( mw.config.get( 'wgUserLanguage' ), 'languageNames' ) ||
225 $.uls.data.getAutonyms();
226 return languageNames[ code ] || code;
227 };
228
229 /**
230 * @method
231 * @inheritdoc
232 */
233 ve.init.mw.Platform.prototype.getLanguageAutonym = $.uls.data.getAutonym;
234
235 /**
236 * @method
237 * @inheritdoc
238 */
239 ve.init.mw.Platform.prototype.getLanguageDirection = $.uls.data.getDir;
240
241 /**
242 * @method
243 * @inheritdoc
244 */
245 ve.init.mw.Platform.prototype.getUserLanguages = mw.language.getFallbackLanguageChain;
246
247 /**
248 * @inheritdoc
249 */
250 ve.init.mw.Platform.prototype.fetchSpecialCharList = function () {
251 return mw.loader.using( 'mediawiki.language.specialCharacters' ).then( () => {
252 const specialCharacterGroups = require( 'mediawiki.language.specialCharacters' ),
253 characters = {},
254 otherGroupName = mw.msg( 'visualeditor-special-characters-group-other' ),
255 otherMsg = mw.message( 'visualeditor-quick-access-characters.json' ).plain(),
256 // TODO: This information should be available upstream in mw.language.specialCharacters
257 rtlGroups = [ 'arabic', 'arabicextended', 'hebrew' ];
258
259 try {
260 const other = JSON.parse( otherMsg );
261 if ( other ) {
262 characters.other = {
263 label: otherGroupName,
264 symbols: this.processSpecialCharSymbols( other ),
265 attributes: { dir: mw.config.get( 'wgVisualEditorConfig' ).pageLanguageDir }
266 };
267 }
268 } catch ( err ) {
269 ve.log( 've.init.mw.Platform: Could not parse the Special Character list.' );
270 ve.log( err );
271 }
272
273 // eslint-disable-next-line no-jquery/no-each-util
274 $.each( specialCharacterGroups, ( groupName, groupCharacters ) => {
275 const groupObject = {}; // button label => character data to insert
276 // eslint-disable-next-line no-jquery/no-each-util
277 $.each( groupCharacters, ( charKey, charVal ) => {
278 let key, val;
279 // VE has a different format and it would be a pain to change it now
280 if ( typeof charVal === 'string' ) {
281 key = charVal;
282 val = charVal;
283 } else if ( typeof charVal === 'object' && 0 in charVal && 1 in charVal ) {
284 key = charVal[ 0 ];
285 val = charVal[ 1 ];
286 } else {
287 key = charVal.label;
288 val = charVal;
289 }
290 groupObject[ key ] = val;
291 } );
292 // The following messages are used here:
293 // * special-characters-group-arabic
294 // * special-characters-group-arabicextended
295 // * special-characters-group-bangla
296 // * special-characters-group-canadianaboriginal
297 // * special-characters-group-cyrillic
298 // * special-characters-group-devanagari
299 // * special-characters-group-greek
300 // * special-characters-group-greekextended
301 // * special-characters-group-gujarati
302 // * special-characters-group-hebrew
303 // * special-characters-group-ipa
304 // * special-characters-group-khmer
305 // * special-characters-group-lao
306 // * special-characters-group-latin
307 // * special-characters-group-latinextended
308 // * special-characters-group-persian
309 // * special-characters-group-sinhala
310 // * special-characters-group-symbols
311 // * special-characters-group-tamil
312 // * special-characters-group-telugu
313 // * special-characters-group-thai
314 characters[ groupName ] = {
315 label: mw.msg( 'special-characters-group-' + groupName ),
316 symbols: this.processSpecialCharSymbols( groupObject ),
317 attributes: { dir: rtlGroups.indexOf( groupName ) !== -1 ? 'rtl' : 'ltr' }
318 };
319 } );
320
321 return characters;
322 } );
323 };
324
325 /**
326 * @inheritdoc
327 */
328 ve.init.mw.Platform.prototype.decodeEntities = function ( html ) {
329 const character = ve.safeDecodeEntities( html );
330 return [
331 {
332 type: 'mwEntity',
333 attributes: { character: character }
334 },
335 {
336 type: '/mwEntity'
337 }
338 ];
339 };