Merge "ApiQueryInfo: fix query limits for testactions"
[lhc/web/wiklou.git] / resources / src / mediawiki.widgets.datetime / DiscordianDateTimeFormatter.js
1 /* eslint-disable no-restricted-properties */
2 ( function ( $, mw ) {
3
4 /**
5 * Provides various methods needed for formatting dates and times. This
6 * implementation implments the [Discordian calendar][1], mainly for testing with
7 * something very different from the usual Gregorian calendar.
8 *
9 * Being intended mainly for testing, niceties like i18n and better
10 * configurability have been omitted.
11 *
12 * [1]: https://en.wikipedia.org/wiki/Discordian_calendar
13 *
14 * @class
15 * @extends mw.widgets.datetime.DateTimeFormatter
16 *
17 * @constructor
18 * @param {Object} [config] Configuration options
19 */
20 mw.widgets.datetime.DiscordianDateTimeFormatter = function MwWidgetsDatetimeDiscordianDateTimeFormatter( config ) {
21 config = $.extend( {}, config );
22
23 // Parent constructor
24 mw.widgets.datetime.DiscordianDateTimeFormatter[ 'super' ].call( this, config );
25 };
26
27 /* Setup */
28
29 OO.inheritClass( mw.widgets.datetime.DiscordianDateTimeFormatter, mw.widgets.datetime.DateTimeFormatter );
30
31 /* Static */
32
33 /**
34 * @inheritdoc
35 */
36 mw.widgets.datetime.DiscordianDateTimeFormatter.static.formats = {
37 '@time': '${hour|0}:${minute|0}:${second|0}',
38 '@date': '$!{dow|full}${not-intercalary|1|, }${season|full}${not-intercalary|1| }${day|#}, ${year|#}',
39 '@datetime': '$!{dow|full}${not-intercalary|1|, }${season|full}${not-intercalary|1| }${day|#}, ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}',
40 '@default': '$!{dow|full}${not-intercalary|1|, }${season|full}${not-intercalary|1| }${day|#}, ${year|#} ${hour|0}:${minute|0}:${second|0} $!{zone|short}'
41 };
42
43 /* Methods */
44
45 /**
46 * @inheritdoc
47 *
48 * Additional fields implemented here are:
49 * - ${year|#}: Year as a number
50 * - ${season|#}: Season as a number
51 * - ${season|full}: Season as a string
52 * - ${day|#}: Day of the month as a number
53 * - ${day|0}: Day of the month as a number with leading 0
54 * - ${dow|full}: Day of the week as a string
55 * - ${hour|#}: Hour as a number
56 * - ${hour|0}: Hour as a number with leading 0
57 * - ${minute|#}: Minute as a number
58 * - ${minute|0}: Minute as a number with leading 0
59 * - ${second|#}: Second as a number
60 * - ${second|0}: Second as a number with leading 0
61 * - ${millisecond|#}: Millisecond as a number
62 * - ${millisecond|0}: Millisecond as a number, zero-padded to 3 digits
63 */
64 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.getFieldForTag = function ( tag, params ) {
65 var spec = null;
66
67 switch ( tag + '|' + params[ 0 ] ) {
68 case 'year|#':
69 spec = {
70 component: 'Year',
71 calendarComponent: true,
72 type: 'number',
73 size: 4,
74 zeropad: false
75 };
76 break;
77
78 case 'season|#':
79 spec = {
80 component: 'Season',
81 calendarComponent: true,
82 type: 'number',
83 size: 1,
84 intercalarySize: { 1: 0 },
85 zeropad: false
86 };
87 break;
88
89 case 'season|full':
90 spec = {
91 component: 'Season',
92 calendarComponent: true,
93 type: 'string',
94 intercalarySize: { 1: 0 },
95 values: {
96 1: 'Chaos',
97 2: 'Discord',
98 3: 'Confusion',
99 4: 'Bureaucracy',
100 5: 'The Aftermath'
101 }
102 };
103 break;
104
105 case 'dow|full':
106 spec = {
107 component: 'DOW',
108 calendarComponent: true,
109 editable: false,
110 type: 'string',
111 intercalarySize: { 1: 0 },
112 values: {
113 '-1': 'N/A',
114 0: 'Sweetmorn',
115 1: 'Boomtime',
116 2: 'Pungenday',
117 3: 'Prickle-Prickle',
118 4: 'Setting Orange'
119 }
120 };
121 break;
122
123 case 'day|#':
124 case 'day|0':
125 spec = {
126 component: 'Day',
127 calendarComponent: true,
128 type: 'string',
129 size: 2,
130 intercalarySize: { 1: 13 },
131 zeropad: params[ 0 ] === '0',
132 formatValue: function ( v ) {
133 if ( v === 'tib' ) {
134 return 'St. Tib\'s Day';
135 }
136 return mw.widgets.datetime.DateTimeFormatter.prototype.formatSpecValue.call( this, v );
137 },
138 parseValue: function ( v ) {
139 if ( /^\s*(st.?\s*)?tib('?s)?(\s*day)?\s*$/i.test( v ) ) {
140 return 'tib';
141 }
142 return mw.widgets.datetime.DateTimeFormatter.prototype.parseSpecValue.call( this, v );
143 }
144 };
145 break;
146
147 case 'hour|#':
148 case 'hour|0':
149 case 'minute|#':
150 case 'minute|0':
151 case 'second|#':
152 case 'second|0':
153 spec = {
154 component: tag.charAt( 0 ).toUpperCase() + tag.slice( 1 ),
155 calendarComponent: false,
156 type: 'number',
157 size: 2,
158 zeropad: params[ 0 ] === '0'
159 };
160 break;
161
162 case 'millisecond|#':
163 case 'millisecond|0':
164 spec = {
165 component: 'Millisecond',
166 calendarComponent: false,
167 type: 'number',
168 size: 3,
169 zeropad: params[ 0 ] === '0'
170 };
171 break;
172
173 default:
174 return mw.widgets.datetime.DiscordianDateTimeFormatter[ 'super' ].prototype.getFieldForTag.call( this, tag, params );
175 }
176
177 if ( spec ) {
178 if ( spec.editable === undefined ) {
179 spec.editable = true;
180 }
181 if ( spec.component !== 'Day' ) {
182 spec.formatValue = this.formatSpecValue;
183 spec.parseValue = this.parseSpecValue;
184 }
185 if ( spec.values ) {
186 spec.size = Math.max.apply(
187 null, $.map( spec.values, function ( v ) { return v.length; } )
188 );
189 }
190 }
191
192 return spec;
193 };
194
195 /**
196 * Get components from a Date object
197 *
198 * Components are:
199 * - Year {number}
200 * - Season {number} 1-5
201 * - Day {number|string} 1-73 or 'tib'
202 * - DOW {number} 0-4, or -1 on St. Tib's Day
203 * - Hour {number} 0-23
204 * - Minute {number} 0-59
205 * - Second {number} 0-59
206 * - Millisecond {number} 0-999
207 * - intercalary {string} '1' on St. Tib's Day
208 *
209 * @param {Date|null} date
210 * @return {Object} Components
211 */
212 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.getComponentsFromDate = function ( date ) {
213 var ret, day, month,
214 monthDays = [ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 ];
215
216 if ( !( date instanceof Date ) ) {
217 date = this.defaultDate;
218 }
219
220 if ( this.local ) {
221 day = date.getDate();
222 month = date.getMonth();
223 ret = {
224 Year: date.getFullYear() + 1166,
225 Hour: date.getHours(),
226 Minute: date.getMinutes(),
227 Second: date.getSeconds(),
228 Millisecond: date.getMilliseconds(),
229 zone: date.getTimezoneOffset()
230 };
231 } else {
232 day = date.getUTCDate();
233 month = date.getUTCMonth();
234 ret = {
235 Year: date.getUTCFullYear() + 1166,
236 Hour: date.getUTCHours(),
237 Minute: date.getUTCMinutes(),
238 Second: date.getUTCSeconds(),
239 Millisecond: date.getUTCMilliseconds(),
240 zone: 0
241 };
242 }
243
244 if ( month === 1 && day === 29 ) {
245 ret.Season = 1;
246 ret.Day = 'tib';
247 ret.DOW = -1;
248 ret.intercalary = '1';
249 } else {
250 day = monthDays[ month ] + day - 1;
251 ret.Season = Math.floor( day / 73 ) + 1;
252 ret.Day = ( day % 73 ) + 1;
253 ret.DOW = day % 5;
254 ret.intercalary = '';
255 }
256
257 return ret;
258 };
259
260 /**
261 * @inheritdoc
262 */
263 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.adjustComponent = function ( date, component, delta, mode ) {
264 return this.getDateFromComponents(
265 this.adjustComponentInternal(
266 this.getComponentsFromDate( date ), component, delta, mode
267 )
268 );
269 };
270
271 /**
272 * Adjust the components directly
273 *
274 * @private
275 * @param {Object} components Modified in place
276 * @param {string} component
277 * @param {number} delta
278 * @param {string} mode
279 * @return {Object} components
280 */
281 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.adjustComponentInternal = function ( components, component, delta, mode ) {
282 var i, min, max, range, next, preTib, postTib, wasTib;
283
284 if ( delta === 0 ) {
285 return components;
286 }
287
288 switch ( component ) {
289 case 'Year':
290 min = 1166;
291 max = 11165;
292 next = null;
293 break;
294 case 'Season':
295 min = 1;
296 max = 5;
297 next = 'Year';
298 break;
299 case 'Week':
300 if ( components.Day === 'tib' ) {
301 components.Day = 59; // Could choose either one...
302 components.Season = 1;
303 }
304 min = 1;
305 max = 73;
306 next = 'Season';
307 break;
308 case 'Day':
309 min = 1;
310 max = 73;
311 next = 'Season';
312 break;
313 case 'Hour':
314 min = 0;
315 max = 23;
316 next = 'Day';
317 break;
318 case 'Minute':
319 min = 0;
320 max = 59;
321 next = 'Hour';
322 break;
323 case 'Second':
324 min = 0;
325 max = 59;
326 next = 'Minute';
327 break;
328 case 'Millisecond':
329 min = 0;
330 max = 999;
331 next = 'Second';
332 break;
333 default:
334 return components;
335 }
336
337 switch ( mode ) {
338 case 'overflow':
339 case 'clip':
340 case 'wrap':
341 }
342
343 if ( component === 'Day' ) {
344 i = Math.abs( delta );
345 delta = delta < 0 ? -1 : 1;
346 preTib = delta > 0 ? 59 : 60;
347 postTib = delta > 0 ? 60 : 59;
348 while ( i-- > 0 ) {
349 if ( components.Day === preTib && components.Season === 1 && this.isLeapYear( components.Year ) ) {
350 components.Day = 'tib';
351 } else if ( components.Day === 'tib' ) {
352 components.Day = postTib;
353 components.Season = 1;
354 } else {
355 components.Day += delta;
356 if ( components.Day < min ) {
357 switch ( mode ) {
358 case 'overflow':
359 components.Day = max;
360 this.adjustComponentInternal( components, 'Season', -1, mode );
361 break;
362 case 'wrap':
363 components.Day = max;
364 break;
365 case 'clip':
366 components.Day = min;
367 i = 0;
368 break;
369 }
370 }
371 if ( components.Day > max ) {
372 switch ( mode ) {
373 case 'overflow':
374 components.Day = min;
375 this.adjustComponentInternal( components, 'Season', 1, mode );
376 break;
377 case 'wrap':
378 components.Day = min;
379 break;
380 case 'clip':
381 components.Day = max;
382 i = 0;
383 break;
384 }
385 }
386 }
387 }
388 } else {
389 if ( component === 'Week' ) {
390 component = 'Day';
391 delta *= 5;
392 }
393 if ( components.Day === 'tib' ) {
394 // For sanity
395 components.Season = 1;
396 }
397 switch ( mode ) {
398 case 'overflow':
399 if ( components.Day === 'tib' && ( component === 'Season' || component === 'Year' ) ) {
400 components.Day = 59; // Could choose either one...
401 wasTib = true;
402 } else {
403 wasTib = false;
404 }
405 i = Math.abs( delta );
406 delta = delta < 0 ? -1 : 1;
407 while ( i-- > 0 ) {
408 components[ component ] += delta;
409 if ( components[ component ] < min ) {
410 components[ component ] = max;
411 components = this.adjustComponentInternal( components, next, -1, mode );
412 }
413 if ( components[ component ] > max ) {
414 components[ component ] = min;
415 components = this.adjustComponentInternal( components, next, 1, mode );
416 }
417 }
418 if ( wasTib && components.Season === 1 && this.isLeapYear( components.Year ) ) {
419 components.Day = 'tib';
420 }
421 break;
422 case 'wrap':
423 range = max - min + 1;
424 components[ component ] += delta;
425 while ( components[ component ] < min ) {
426 components[ component ] += range;
427 }
428 while ( components[ component ] > max ) {
429 components[ component ] -= range;
430 }
431 break;
432 case 'clip':
433 components[ component ] += delta;
434 if ( components[ component ] < min ) {
435 components[ component ] = min;
436 }
437 if ( components[ component ] > max ) {
438 components[ component ] = max;
439 }
440 break;
441 }
442 if ( components.Day === 'tib' &&
443 ( components.Season !== 1 || !this.isLeapYear( components.Year ) )
444 ) {
445 components.Day = 59; // Could choose either one...
446 }
447 }
448
449 return components;
450 };
451
452 /**
453 * @inheritdoc
454 */
455 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.getDateFromComponents = function ( components ) {
456 var month, day, days,
457 date = new Date(),
458 monthDays = [ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 ];
459
460 components = $.extend( {}, this.getComponentsFromDate( null ), components );
461 if ( components.Day === 'tib' ) {
462 month = 1;
463 day = 29;
464 } else {
465 days = components.Season * 73 + components.Day - 74;
466 month = 0;
467 while ( days >= monthDays[ month + 1 ] ) {
468 month++;
469 }
470 day = days - monthDays[ month ] + 1;
471 }
472
473 if ( components.zone ) {
474 // Can't just use the constructor because that's stupid about ancient years.
475 date.setFullYear( components.Year - 1166, month, day );
476 date.setHours( components.Hour, components.Minute, components.Second, components.Millisecond );
477 } else {
478 // Date.UTC() is stupid about ancient years too.
479 date.setUTCFullYear( components.Year - 1166, month, day );
480 date.setUTCHours( components.Hour, components.Minute, components.Second, components.Millisecond );
481 }
482
483 return date;
484 };
485
486 /**
487 * Get whether the year is a leap year
488 *
489 * @private
490 * @param {number} year
491 * @return {boolean}
492 */
493 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.isLeapYear = function ( year ) {
494 year -= 1166;
495 if ( year % 4 ) {
496 return false;
497 } else if ( year % 100 ) {
498 return true;
499 }
500 return ( year % 400 ) === 0;
501 };
502
503 /**
504 * @inheritdoc
505 */
506 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.getCalendarHeadings = function () {
507 return [ 'SM', 'BT', 'PD', 'PP', null, 'SO' ];
508 };
509
510 /**
511 * @inheritdoc
512 */
513 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.sameCalendarGrid = function ( date1, date2 ) {
514 var components1 = this.getComponentsFromDate( date1 ),
515 components2 = this.getComponentsFromDate( date2 );
516
517 return components1.Year === components2.Year && components1.Season === components2.Season;
518 };
519
520 /**
521 * @inheritdoc
522 */
523 mw.widgets.datetime.DiscordianDateTimeFormatter.prototype.getCalendarData = function ( date ) {
524 var dt, components, season, i, row,
525 ret = {
526 dayComponent: 'Day',
527 weekComponent: 'Week',
528 monthComponent: 'Season'
529 },
530 seasons = [ 'Chaos', 'Discord', 'Confusion', 'Bureaucracy', 'The Aftermath' ],
531 seasonStart = [ 0, -3, -1, -4, -2 ];
532
533 if ( !( date instanceof Date ) ) {
534 date = this.defaultDate;
535 }
536
537 components = this.getComponentsFromDate( date );
538 components.Day = 1;
539 season = components.Season;
540
541 ret.header = seasons[ season - 1 ] + ' ' + components.Year;
542
543 if ( seasonStart[ season - 1 ] ) {
544 this.adjustComponentInternal( components, 'Day', seasonStart[ season - 1 ], 'overflow' );
545 }
546
547 ret.rows = [];
548 do {
549 row = [];
550 for ( i = 0; i < 6; i++ ) {
551 dt = this.getDateFromComponents( components );
552 row[ i ] = {
553 display: components.Day === 'tib' ? 'Tib' : String( components.Day ),
554 date: dt,
555 extra: components.Season < season ? 'prev' : components.Season > season ? 'next' : null
556 };
557
558 this.adjustComponentInternal( components, 'Day', 1, 'overflow' );
559 if ( components.Day !== 'tib' && i === 3 ) {
560 row[ ++i ] = null;
561 }
562 }
563
564 ret.rows.push( row );
565 } while ( components.Season === season );
566
567 return ret;
568 };
569
570 }( jQuery, mediaWiki ) );