-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathmw.lua
More file actions
1430 lines (1214 loc) · 50.4 KB
/
mw.lua
File metadata and controls
1430 lines (1214 loc) · 50.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---@meta mw
-- luacheck: ignore
---This file contains definitions and simulations of the MediaWiki enviroment
mw = {}
---Adds a warning which is displayed above the preview when previewing an edit. `text` is parsed as wikitext.
---@param text string
function mw.addWarning(text) end
---Calls tostring() on all arguments, then concatenates them with tabs as separators.
---@param ... any
---@return string
---@nodiscard
function mw.allToString(...) end
---Creates a deep copy of a value. All tables (and their metatables) are reconstructed from scratch. Functions are still shared, however.
---@generic T
---@param value T
---@return T
---@nodiscard
function mw.clone(value) end
---Adds one to the "expensive parser function" count, and throws an exception if it exceeds the limit (see $wgExpensiveParserFunctionLimit).
function mw.incrementExpensiveFunctionCount() end
---Returns true if the current #invoke is being `substed`, false otherwise.
---@return boolean
function mw.isSubsting() end
---See www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#mw.loadData
---@param module string
---@return table
function mw.loadData(module)
--TODO: add __index that errors
return require(module)
end
---This is the same as mw.loadData(), except it loads data from JSON pages rather than Lua tables. The JSON content must be an array or object. See also mw.text.jsonDecode().
---@param page string
---@return table
function mw.loadJsonData(page) end
---Serializes object to a human-readable representation, then returns the resulting string.
---@param object any
---@return string
function mw.dumpObject(object)
local str = ''
for k, v in pairs(object) do
if type(v) == 'table' then
str = str .. k .. ': ' .. mw.dumpObject(v) .. '\n'
else
str = str .. k .. ': ' .. tostring(v) .. '\n'
end
end
return str
end
---Passes the arguments to mw.allToString(), then appends the resulting string to the log buffer.
---@param ... any
function mw.log(...) end
---Calls mw.dumpObject() and appends the resulting string to the log buffer. If prefix is given, it will be added to the log buffer followed by an equals sign before the serialized string is appended (i.e. the logged text will be "prefix = object-string").
---@param object any
---@param prefix any?
function mw.logObject(object, prefix) end
---@class Frame
---@field args table?
mw.frame = {}
---Returns the current frame object, typically the frame object from the most recent #invoke.
---@return Frame
---@nodiscard
function mw.getCurrentFrame()
return setmetatable(mw.frame, {})
end
---Call a parser function, returning an appropriate string. This is preferable to frame:preprocess, but whenever possible, native Lua functions or Scribunto library functions should be preferred to this interface.
---@param name string
---@param args table|string
---@return string
---@overload fun(self, params: {name: string, args: table|string}): string
---@overload fun(self, name: string, ...: string): string
function mw.frame:callParserFunction(name, args) end
---This is transclusion. As in transclusion, if the passed title does not contain a namespace prefix it will be assumed to be in the Template: namespace.
---@param params {title: string, args: table?}
---@return string
function mw.frame:expandTemplate(params)
error('Cannot expand template in fake')
end
---This is equivalent to a call to frame:callParserFunction() with function name '#tag:' .. name and with content prepended to args.
---@param name string
---@param content string
---@param args table|string
---@return string
---@overload fun(self, params: {name: string, content: string, args: table|string}): string
function mw.frame:extensionTag(name, content, args) end
---Called on the frame created by {{#invoke:}}, returns the frame for the page that called {{#invoke:}}. Called on that frame, returns nil.
---@return Frame
function mw.frame:getParent() end
---Returns the title associated with the frame as a string. For the frame created by {{#invoke:}}, this is the title of the module invoked.
---@return string
function mw.frame:getTitle() return '' end
---Create a new Frame object that is a child of the current frame, with optional arguments and title.
--This is mainly intended for use in the debug console for testing functions that would normally be called by {{#invoke:}}. The number of frames that may be created at any one time is limited.
---@param params {title: string, args: table}
---@return string
function mw.frame:newChild(params) end
---This expands wikitext in the context of the frame, i.e. templates, parser functions, and parameters such as {{{1}}} are expanded.
---Not recommended. Use frame:expandTemplate or frame:callParserFunction depending on usecase.
---@param params {text: string}
---@return string
---@overload fun(self, text: string): string
function mw.frame:preprocess(params) end
---Gets an object for the specified argument, or nil if the argument is not provided.
---The returned object has one method, object:expand(), that returns the expanded wikitext for the argument.
---@param params {arg: string}
---@return {expand: fun(self):string}
---@overload fun(self, arg: string): {expand: fun(self):string}
function mw.frame:getArgument(params) end
---Returns an object with one method, object:expand(), that returns the result of frame:preprocess( text ).
---@param params {text: string}
---@return {expand: fun(self):string}
---@overload fun(self, text: string): {expand: fun(self):string}
function mw.frame:newParserValue(params) end
---Returns an object with one method, object:expand(), that returns the result of frame:expandTemplate called with the given arguments.
---@param params {title: string, args: table}
---@return {expand: fun(self):string}
function mw.frame:newTemplateParserValue(params) end
mw.hash = {}
---Hashes a string value with the specified algorithm. Valid algorithms may be fetched using mw.hash.listAlgorithms().
---@param algo string
---@param value any
---@return string
function mw.hash.hashValue(algo, value) end
---Returns a list of supported hashing algorithms, for use in mw.hash.hashValue().
---@return string[]
function mw.hash.listAlgorithms() end
---@class Html
mw.html = {}
---Creates a new mw.html object containing a tagName html element. You can also pass an empty string or nil as tagName in order to create an empty mw.html object.
---@param tagName? string
---@param args? {selfClosing: boolean, parent: Html}
---@return Html
function mw.html.create(tagName, args) end
---Appends a child mw.html (builder) node to the current mw.html instance. If a nil parameter is passed, this is a no-op. A (builder) node is a string representation of an html element.
---@param builder? Html|string|number|Widget
---@return self
function mw.html:node(builder) end
---Appends an undetermined number of wikitext strings to the mw.html object. Note that this stops at the first nil item.
---@param ... string|number|nil
---@return self
function mw.html:wikitext(...) end
---Appends a newline to the mw.html object.
---@return self
function mw.html:newline() end
---Appends a new child node with the given tagName to the builder, and returns a mw.html instance representing that new node. The args parameter is identical to that of mw.html.create
---Note that contrarily to other methods such as html:node(), this method doesn't return the current mw.html instance, but the mw.html instance of the newly inserted tag. Make sure to use html:done() to go up to the parent mw.html instance, or html:allDone() if you have nested tags on several levels.
---@param tagName string
---@param args? {selfClosing: boolean, parent: Html}
---@return Html
function mw.html:tag(tagName, args) end
---Set an HTML attribute with the given name and value on the node. Alternatively a table holding name->value pairs of attributes to set can be passed. In the first form, a value of nil causes any attribute with the given name to be unset if it was previously set.
---@param name string
---@param value string|number|nil
---@return self
---@overload fun(self, param: {[string]: string})
function mw.html:attr(name, value) end
---Get the value of a html attribute previously set using html:attr() with the given name.
---@param name string
---@return string
function mw.html:getAttr(name) end
---Adds a class name to the node's class attribute. If a nil parameter is passed, this is a no-op.
---@param class string?
---@return self
function mw.html:addClass(class) end
---Set a CSS property with the given name and value on the node. Alternatively a table holding name->value pairs of attributes to set can be passed. In the first form, a value of nil causes any attribute with the given name to be unset if it was previously set.
---@param name string
---@param value string|number|nil
---@return self
---@overload fun(self, param: {[string]: string|number|nil})
function mw.html:css(name, value) end
---Add some raw css to the node's style attribute. If a nil parameter is passed, this is a no-op.
---@param css string?
---@return self
function mw.html:cssText(css) end
---Returns the parent node under which the current node was created.
---@return Html
function mw.html:done() end
---Like html:done(), but traverses all the way to the root node of the tree and returns it.
---@return Html
function mw.html:allDone() end
--- Include the full functionality for faking
mw.html = require('3rd.mw.html')
---@class Language
mw.language = {}
---The full name of the language for the given language code: native name (language autonym) by default, name translated in target language if a value is given for inLanguage.
---@param code string
---@param inLanguage? string
---@return string
function mw.language.fetchLanguageName(code, inLanguage) end
---Fetch the list of languages known to MediaWiki, returning a table mapping language code to language name.
---@param inLanguage? string
---@param include? 'all'|'mwfile'|'mw'
---@return table
function mw.language.fetchLanguageNames(inLanguage, include) end
---Returns a new language object for the wiki's default content language.
---@return Language
function mw.language.getContentLanguage()
return setmetatable(mw.language, {})
end
mw.getContentLanguage = mw.language.getContentLanguage
---Returns a list of MediaWiki's fallback language codes for the specified code.
---@param code string
---@return string[]
function mw.language.getFallbacksFor(code) end
---Returns true if a language code is known to MediaWiki.
---@param code string
---@return boolean
function mw.language.isKnownLanguageTag(code) end
---Checks whether any localisation is available for that language code in MediaWiki.
---@param code string
---@return boolean
function mw.language.isSupportedLanguage(code) end
---Returns true if a language code is of a valid form for the purposes of internal customisation of MediaWiki.
---@param code string
---@return boolean
function mw.language.isValidBuiltInCode(code) end
---Returns true if a language code string is of a valid form, whether or not it exists. This includes codes which are used solely for customisation via the MediaWiki namespace.
---@param code string
---@return boolean
function mw.language.isValidCode(code) end
---Creates a new language object. Language objects do not have any publicly accessible properties, but they do have several methods, which are documented below.
---@param code string
---@return Language
function mw.language.new(code)
return mw.language.getContentLanguage()
end
mw.getLanguage = mw.language.new
---Returns the language code for this language object.
---@return string
function mw.language:getCode() end
---Returns a list of MediaWiki's fallback language codes for this language object. Equivalent to mw.language.getFallbacksFor( lang:getCode() ).
---@return string[]
function mw.language:getFallbackLanguages() end
---Returns true if the language is written right-to-left, false if it is written left-to-right.
---@return boolean
function mw.language:isRTL() end
---Converts the string to lowercase, honoring any special rules for the given language.
---@param str string
---@return string
function mw.language:lc(str) end
---Converts the first character of the string to lowercase.
---@param str string
---@return string
function mw.language:lcfirst(str) end
---Converts the string to uppercase, honoring any special rules for the given language.
---@param str string
---@return string
function mw.language:uc(str) end
---Converts the first character of the string to uppercase.
---@param str string
---@return string
function mw.language:ucfirst(str)
-- TODO: UTF8 support in fake
if str == 'übung' then
return 'Übung'
end
return (str:gsub("^%l", string.upper))
end
---Converts the string to a representation appropriate for case-insensitive comparison. Note that the result may not make any sense when displayed.
---@param str string
---@return string
function mw.language:caseFold(str) end
---Formats a number with grouping and decimal separators appropriate for the given language. Given 123456.78, this may produce "123,456.78", "123.456,78", or even something like "١٢٣٬٤٥٦٫٧٨" depending on the language and wiki configuration.
---@param num number
---@param options? {noCommafy: boolean}
---@return string
function mw.language:formatNum(num, options)
local k
local formatted = tostring(num)
while true do
formatted, k = string.gsub(formatted, '^(-?%d+)(%d%d%d)', '%1,%2')
if (k == 0) then
break
end
end
return formatted
end
---Formats a date according to the given format string. If timestamp is omitted, the default is the current time. The value for local must be a boolean or nil; if true, the time is formatted in the wiki's local time rather than in UTC.
---@param format string
---@param timestamp string|osdateparam?
---@param localTime boolean?
---@return string
function mw.language:formatDate(format, timestamp, localTime)
local ostimeWrapper = function(date)
local time = os.time(date)
-- Fix for running on MacOS. Year = 0000 returns nil on mac, let's assume it's 0000-01-01
if time == nil then
return -62167219200
end
return time
end
local function localTimezoneOffset(ts)
local utcDt = os.date("!*t", ts)
local localDt = os.date("*t", ts)
localDt.isdst = false
return os.difftime(ostimeWrapper(localDt --[[@as osdateparam]]), ostimeWrapper(utcDt --[[@as osdateparam]]))
end
local function parseDateString(timeString)
local year, month, day = timeString:match('(%d%d%d%d)-?(%d%d)-?(%d%d)')
local hour = timeString:match('%d%d%d%d%-?%d%d%-?%d%d[ T]?(%d%d)')
local minute = timeString:match('%d%d%d%d%-?%d%d%-?%d%d[ T]?%d%d:?(%d%d)')
local second = timeString:match('%d%d%d%d%-?%d%d%-?%d%d[ T]?%d%d:?%d%d:?(%d%d)')
return year, month, day, hour, minute, second
end
local function makeOsdateParam(year, month, day, hour, minute, second)
return { year = year, month = month or 1, day = day or 1, hour = hour or 0, min = minute, sec = second }
end
if format == 'U' then
if not timestamp then
return tostring(os.time(os.date("!*t") --[[@as osdateparam]]))
end
if type(timestamp) ~= 'string' then
return tostring(os.time(timestamp))
end
local tzHour, tzMinutes = timestamp:match('([%-%+]%d?%d):(%d%d)$')
local offset = 0
if tzHour then
offset = tonumber(tzHour) * 3600 + tonumber(tzMinutes) * 60
end
local year, month, day, hour, minute, second = parseDateString(timestamp)
if not year then
return ''
end
local timestampBeforeOffset = ostimeWrapper(makeOsdateParam(year, month, day, hour, minute, second))
local ts = timestampBeforeOffset - offset
return tostring(ts + localTimezoneOffset(ts))
elseif format == 'c' then
local outFormat = '%Y-%m-%dT%H:%M:%S'
if not timestamp then
return os.date(outFormat) --[[@as string]]
end
if type(timestamp) == 'string' and string.sub(timestamp, 1, 1) == '@' then
return os.date(outFormat, tonumber(string.sub(timestamp, 2))) --[[@as string]]
end
if type(timestamp) == 'string' then
local year, month, day, hour, minute, second = parseDateString(timestamp)
if not year then
return ''
end
return os.date(outFormat, ostimeWrapper(makeOsdateParam(year, month, day, hour, minute, second))) --[[@as string]]
end
return os.date(outFormat, ostimeWrapper(timestamp)) --[[@as string]]
elseif format == 'Y' then
local outFormat = '%Y'
if not timestamp then
return os.date(outFormat) --[[@as string]]
end
if type(timestamp) == 'string' and string.sub(timestamp, 1, 1) == '@' then
return os.date(outFormat, tonumber(string.sub(timestamp, 2))) --[[@as string]]
end
if type(timestamp) == 'string' then
local year = parseDateString(timestamp)
return year or ''
end
return os.date(outFormat, ostimeWrapper(timestamp)) --[[@as string]]
elseif format == 'n' then
local outFormat = '%m'
if not timestamp then
return os.date(outFormat) --[[@as string]]
end
if type(timestamp) == 'string' and string.sub(timestamp, 1, 1) == '@' then
return os.date(outFormat, tonumber(string.sub(timestamp, 2))) --[[@as string]]
end
if type(timestamp) == 'string' then
local _, month = parseDateString(timestamp)
return month or ''
end
return os.date(outFormat, ostimeWrapper(timestamp)) --[[@as string]]
elseif format == 'd' then
local outFormat = '%d'
if not timestamp then
return os.date(outFormat) --[[@as string]]
end
if type(timestamp) == 'string' and string.sub(timestamp, 1, 1) == '@' then
return os.date(outFormat, tonumber(string.sub(timestamp, 2))) --[[@as string]]
end
if type(timestamp) == 'string' then
local _, _, day = parseDateString(timestamp)
return day or ''
end
return os.date(outFormat, ostimeWrapper(timestamp)) --[[@as string]]
end
return ''
end
---Breaks a duration in seconds into more human-readable units, e.g. 12345 to 3 hours, 25 minutes and 45 seconds, returning the result as a string.
---@param seconds number
---@param chosenIntervals table
---@return string
function mw.language:formatDuration(seconds, chosenIntervals) end
---This takes a number as formatted by lang:formatNum() and returns the actual number. In other words, this is basically a language-aware version of tonumber().
---@param str string
---@return number
---@overload fun(str: string?): nil
function mw.language:parseFormattedNumber(str) end
---This chooses the appropriate grammatical form from forms (which must be a sequence table) or ... based on the number n.
---@param n number
---@param ... string
---@return string
---@overload fun(n: number, forms: table):string
function mw.language:convertPlural(n, ...) end
mw.language.plural = mw.language.convertPlural
---This chooses the appropriate inflected form of word for the given inflection code case.
---@param word string
---@param case string
---@return string
function mw.language:convertGrammar(word, case) end
---This chooses the appropriate inflected form of word for the given inflection code case.
---@param case string
---@param word string
---@return string
function mw.language:grammar(case, word) end
---Returns a Unicode arrow character corresponding to direction:
---@param direction 'forwards'|'backwards'|'left'|'right'|'up'|'down'
---@return '→'|'←'|'↑'|'↓'
function mw.language:getArrow(direction) end
---Returns "ltr" or "rtl", depending on the directionality of the language.
---@return 'ltr'|'rtl'
function mw.language:getDir() end
---@class Message
mw.message = {}
---Creates a new message object for the given message key. The remaining parameters are passed to the new object's params() method.
---@param key string
---@param ... any
---@return Message
function mw.message.new(key, ...) end
---Creates a new message object for the given messages (the first one that exists will be used).
---@param ... string
---@return Message
function mw.message.newFallbackSequence(...) end
---Creates a new message object, using the given text directly rather than looking up an internationalized message.
---@param msg string
---@param ... string
---@return Message
function mw.message.newRawMessage(msg, ...) end
---Wraps the value so that it will not be parsed as wikitext by msg:parse().
---@param value string
---@return string
function mw.message.rawParam(value) end
---Wraps the value so that it will automatically be formatted as by lang:formatNum(). Note this does not depend on the Language library actually being available.
---@param value string
---@return string
function mw.message.numParam(value) end
---Returns a Language object for the default language.
---@return Language
function mw.message.getDefaultLanguage() end
---Add parameters to the message, which may be passed as individual arguments or as a sequence table. Parameters must be numbers, strings, or the special values returned by mw.message.numParam() or mw.message.rawParam(). If a sequence table is used, parameters must be directly present in the table; references using the __index metamethod will not work.
---@param ... string|number
---@return self
---@overload fun(self, params: table):self
---@overload fun(self, param: string|number):self
function mw.message:params(...) end
---Like :params(), but has the effect of passing all the parameters through mw.message.rawParam() first.
---@param ... string
---@return self
---@overload fun(self, params: table):self
function mw.message:rawParams(...) end
---Like :params(), but has the effect of passing all the parameters through mw.message.numParam() first.
---@param ... number
---@return self
---@overload fun(self, params: table):self
function mw.message:numParams(...) end
---Specifies the language to use when processing the message. lang may be a string or a table with a getCode() method (i.e. a Language object).
---@param lang string
---@return self
function mw.message:inLanguage(lang) end
---Specifies whether to look up messages in the MediaWiki: namespace (i.e. look in the database), or just use the default messages distributed with MediaWiki.
---@param bool boolean
---@return self
function mw.message:useDatabase(bool) end
---Substitutes the parameters and returns the message wikitext as-is. Template calls and parser functions are intact.
---@return string
function mw.message:plain() end
---Returns a boolean indicating whether the message key exists.
---@return boolean
function mw.message:exists() end
---Returns a boolean indicating whether the message key has content. Returns true if the message key does not exist or the message is the empty string.
---@return boolean
function mw.message:isBlank() end
---Returns a boolean indicating whether the message key is disabled. Returns true if the message key does not exist or if the message is the empty string or the string "-".
---@return boolean
function mw.message:isDisabled() end
---@alias namespaceInfo {id: integer, name: string, canonicalName: string, displayName: string, hasSubpages: boolean, hasGenderDistinction: boolean, isCapitalized: boolean, isContent: boolean, isIncludable: boolean, isMovable:boolean, isSubject: boolean, isTalk: boolean, defaultContentModel: string, aliases: string[], subject: namespaceInfo, talk: namespaceInfo, associated: namespaceInfo}
---@class Site
---@field currentVersion string
---@field scriptPath string
---@field server string
---@field siteName string
---@field namespaces table<integer, namespaceInfo>
---@field contentNamespaces table<integer, namespaceInfo>
---@field subjectNamespaces table<integer, namespaceInfo>
---@field talkNamespaces table<integer, namespaceInfo>
---@field stats SiteStats
mw.site = { server = 'https://liquipedia.net/wiki/' }
---@class SiteStats
---@field pages integer
---@field articles integer
---@field files integer
---@field edits integer
---@field users integer
---@field activeUsers integer
---@field admins integer
mw.site.stats = {}
---Gets statistics about the category. Each new category queried will increment the expensive function count.
---@param category string
---@param which 'all'|'subcats'|'files'|'pages'
---@return integer
---@overload fun(category: string, which: '*'): {all: integer, subcats: integer, files: integer, pages: integer}
function mw.site.stats.pagesInCategory(category, which) end
---Returns the number of pages in the given namespace.
---@param namespace integer
---@return integer
function mw.site.stats.pagesInNamespace(namespace) end
---Returns the number of users in the given group.
---@param group string
---@return integer
function mw.site.stats.usersInGroup(group) end
---Returns a table holding data about available interwiki prefixes. If filter is the string "local", then only data for local interwiki prefixes is returned. If filter is the string "!local", then only data for non-local prefixes is returned. If no filter is specified, data for all prefixes is returned. A "local" prefix in this context is one that is for the same project.
---@param filter nil|'local'|'!local'
---@return {prefix: string, url: string, isProtocolRelative: boolean, isLocal: boolean, isCurrentWiki: boolean, isTranscludable: boolean, isExtraLanguageLink: boolean, displayText:string, tooltip: string?}
function mw.site.interwikiMap(filter) end
mw.text = {}
---Replaces HTML entities in the string with the corresponding characters.
---@param s string
---@param decodeNamedEntities boolean?
---@return string
function mw.text.decode(s, decodeNamedEntities) end
---Replaces characters in a string with HTML entities. Characters '<', '>', '&', '"', and the non-breaking space are replaced with the appropriate named entities; all others are replaced with numeric entities.
---@param s string
---@param charset string?
---@return string
function mw.text.encode(s, charset) end
---Decodes a JSON string. flags is 0 or a combination (use +) of the flags mw.text.JSON_PRESERVE_KEYS and mw.text.JSON_TRY_FIXING.
---@param s string
---@param flags number?
---@return table
function mw.text.jsonDecode(s, flags)
return require('3rd.jsonlua.mock_json'):decode(s)
end
---Encode a JSON string. Errors are raised if the passed value cannot be encoded in JSON. flags is 0 or a combination (use +) of the flags mw.text.JSON_PRESERVE_KEYS and mw.text.JSON_PRETTY.
---@param s any
---@param flags number?
---@return string
function mw.text.jsonEncode(s, flags)
return require('3rd.jsonlua.mock_json'):encode(s)
end
---Removes all MediaWiki strip markers from a string.
---@param s string
---@return string
function mw.text.killMarkers(s)
return s
end
---Joins a list, prose-style. In other words, it's like table.concat() but with a different separator before the final item.
---@param list table
---@param separator string?
---@param conjunction string?
---@return string
function mw.text.listToText(list, separator, conjunction) end
---Replaces various characters in the string with HTML entities to prevent their interpretation as wikitext.
---@param s string
---@return string
function mw.text.nowiki(s)
-- TODO: This only covers some
return (string.gsub(s, '["&\'<=>%[%]{|}]', {
['"'] = '"',
['&'] = '&',
["'"] = ''',
['<'] = '<',
['='] = '=',
['>'] = '>',
['['] = '[',
[']'] = ']',
['{'] = '{',
['|'] = '|',
['}'] = '}',
}))
end
---Splits the string into substrings at boundaries matching the Ustring pattern pattern. If plain is specified and true, pattern will be interpreted as a literal string rather than as a Lua pattern.
---@param s string|number
---@param pattern string?
---@param plain boolean?
---@return string[]
function mw.text.split(s, pattern, plain)
pattern = pattern or "%s"
local results = {}
local start = 1
local splitStart, splitEnd = string.find(s, pattern, start, plain)
while splitStart do
table.insert(results, string.sub(s, start, splitStart - 1))
start = splitEnd + 1
splitStart, splitEnd = string.find(s, pattern, start, plain)
end
table.insert(results, string.sub(s, start))
return results
end
---Returns an iterator function that will iterate over the substrings that would be returned by the equivalent call to mw.text.split().
---@param s string
---@param pattern string?
---@param plain boolean?
---@return function
function mw.text.gsplit(s, pattern, plain) end
---Generates an HTML-style tag for name.
---@param name string
---@param attrs table?
---@param content nil|string|boolean
---@return string
---@overload fun(params: {name: string, attrs: table, content: nil|string|boolean})
function mw.text.tag(name, attrs, content) end
---Remove whitespace or other characters from the beginning and end of a string.
---@param s string
---@param charset string?
---@return string
function mw.text.trim(s, charset)
-- TODO: UTF8 support in fake
return string.match(s, '^()%s*$') and '' or string.match(s, '^%s*(.*%S)')
end
---Truncates text to the specified length in code points, adding ellipsis if truncation was performed. If length is positive, the end of the string will be truncated; if negative, the beginning will be removed
---@param text string
---@param length number
---@param ellipsis string?
---@param adjustLength boolean
---@return string
function mw.text.truncate(text, length, ellipsis, adjustLength) end
---Replaces MediaWiki <nowiki> strip markers with the corresponding text. Other types of strip markers are not changed.
---@param s string
---@return string
function mw.text.unstripNoWiki(s) end
---Equivalent to mw.text.killMarkers( mw.text.unstripNoWiki( s ) ).
---@param s string
---@return string
function mw.text.unstrip(s) end
---@class Title
---@field id integer
---@field interwiki string
---@field namespace number
---@field nsText string
---@field subjectNsText string
---@field text string
---@field prefixedText string
---@field fullText string
---@field rootText string
---@field baseText string
---@field subpageText string
---@field canTalk string
---@field fragment string?
---@field exists boolean
---@field file File
---@field fileExists boolean
---@field isContentPage boolean
---@field isExternal boolean
---@field isLocal boolean
---@field isRedirect boolean
---@field isSpecialPage boolean
---@field isSubpage boolean
---@field isTalkPage boolean
---@field contentModel string
---@field basePageTitle Title
---@field rootPageTitle Title
---@field talkPageTitle Title?
---@field subjectPageTitle Title
---@field redirectTarget Title|false
---@field protectionLevels table
---@field cascadingProtection table
mw.title = {
namespace = 0,
id = 123,
nsText = '',
text = 'FakePage',
prefixedText = 'FakePage',
fullText = 'FakePage',
baseText = 'FakePage',
}
---@class File
---@field exists boolean
---@field width number
---@field height number
---@field pages {width: number, height: number}[]?
---@field size number
---@field mimeType string
---@field length number
---Test for whether two titles are equal. Note that fragments are ignored in the comparison.
---@param a Title
---@param b Title
---@return boolean
function mw.title.equals(a, b) end
---Returns -1, 0, or 1 to indicate whether the title a is less than, equal to, or greater than title b.
---@param a Title
---@param b Title
---@return -1|0|1
function mw.title.compare(a, b) end
---Returns the title object for the current page.
---@return Title
function mw.title.getCurrentTitle()
return setmetatable(mw.title, {})
end
---Creates a new title object. This function is expensive when called with an ID.
---If the text string does not specify a namespace, namespace (which may be any key found in mw.site.namespaces) will be used.
---If the text is not a valid title, nil is returned.
---@param text string
---@param namespace string|integer?
---@return Title?
---@overload fun(id: number):Title?
function mw.title.new(text, namespace)
return setmetatable(mw.title, {})
end
---Creates a title object with title title in namespace namespace, optionally with the specified fragment and interwiki prefix. namespace may be any key found in mw.site.namespaces. If the resulting title is not valid, returns nil.
---Note that, unlike mw.title.new(), this method will always apply the specified namespace.
---If the text is not a valid title, nil is returned.
---@param namespace string|integer
---@param title string
---@param fragment string?
---@param interwiki string?
---@return Title?
function mw.title.makeTitle(namespace, title, fragment, interwiki) end
---Whether this title is a subpage of the given title.
---@param title2 Title
---@return boolean
function mw.title:isSubpageOf(title2) end
---Whether this title is in the given namespace.
---@param ns string|number
---@return boolean
function mw.title:inNamespace(ns)
-- Currently only supports mocking main namespace as all busted tests are written expecting it
return ns == 0
end
---Whether this title is in any of the given namespaces.
---@param ... string|number
---@return boolean
function mw.title:inNamespaces(...)
-- Currently only supports mocking main namespace as all busted tests are written expecting it
for _, ns in ipairs(arg) do
if ns == 0 then
return true
end
end
return false
end
---Whether this title's subject namespace is in the given namespace.
---@param ns string|number
---@return boolean
function mw.title:hasSubjectNamespace(ns)
-- Currently only supports mocking main and talk namespace as all busted tests are written expecting it
return ns == 0 or ns == 1
end
---The same as mw.title.makeTitle( title.namespace, title.text .. '/' .. text ).
---@param text string
---@return Title
function mw.title:subPageTitle(text) end
---Returns title.text encoded as it would be in a URL.
---@return string
function mw.title:partialUrl() end
---Returns the full URL (with optional query table/string) for this title.
---@param query? table|string
---@param proto? 'http'|'https'|'relative'|'canonical'
---@return string
function mw.title:fullUrl(query, proto) end
---Returns the local URL (with optional query table/string) for this title.
---@param query? table|string
---@return string
function mw.title:localUrl(query) end
---Returns the canonical URL (with optional query table/string) for this title.
---@param query? table|string
---@return string
function mw.title:canonicalUrl(query) end
---Returns the (unparsed) content of the page, or nil if there is no page. The page will be recorded as a transclusion.
---@return string?
function mw.title:getContent() end
---@class ustring
---@field maxPatternLength number The maximum allowed length of a pattern, in bytes.
---@field maxStringLength number The maximum allowed length of a string, in bytes.
mw.ustring = {}
---Returns individual bytes; identical to string.byte().
---@see string.byte
---@param s string|number
---@param i? integer
---@param j? integer
---@return integer ...
function mw.ustring.byte(s, i, j) end
---Returns the byte offset of a character in the string. The default for both l and i is 1. i may be negative, in which case it counts from the end of the string.
---@param s string|number
---@param l? integer
---@param i? integer
---@return integer ...
function mw.ustring.byteoffset(s, l, i) end
---Much like string.char(), except that the integers are Unicode codepoints rather than byte values.
---@see string.char
---@param ... integer
---@return string
function mw.ustring.char(...) end
---Much like string.byte(), except that the return values are codepoints and the offsets are characters rather than bytes.
---@see string.byte
---@param s string|number
---@param i? integer
---@param j? integer
---@return integer ...
function mw.ustring.codepoint(s, i, j) end
---Much like string.find(), except that the pattern is extended as described in Ustring patterns and the init offset is in characters rather than bytes.
---@see string.find
---@param s string|number
---@param pattern string|number
---@param init? integer
---@param plain? boolean
---@return integer|nil start
---@return integer|nil end
---@return any|nil ... captured
function mw.ustring.find(s, pattern, init, plain) end
---Identical to string.format(). Widths and precisions for strings are expressed in bytes, not codepoints.
---@see string.format
---@param format string|number
---@param ... any
---@return string
function mw.ustring.format(format, ...) end
---Returns three values for iterating over the codepoints in the string. i defaults to 1, and j to -1. This is intended for use in the iterator form of for:
---@param s string|number
---@param i? integer
---@param j? integer
---@return string
function mw.ustring.gcodepoint(s, i, j) end
---Much like string.gmatch(), except that the pattern is extended as described in Ustring patterns.
---@see string.gmatch
---@param s string|number
---@param pattern string|number
---@return fun():string, ...
function mw.ustring.gmatch(s, pattern) end
---Much like string.gmatch(), except that the pattern is extended as described in Ustring patterns.
---@see string.gsub
---@param s string|number
---@param pattern string|number
---@param repl string|number|table|function
---@param n? integer
---@return string
---@return integer count
function mw.ustring.gsub(s, pattern, repl, n) end
---Returns true if the string is valid UTF-8, false if not.
---@param s string|number
---@return boolean
function mw.ustring.isutf8(s) end
---Returns the length of the string in codepoints, or nil if the string is not valid UTF-8.
---@see string.len
---@param s string|number
---@return integer
function mw.ustring.len(s) end
---Much like string.lower(), except that all characters with lowercase to uppercase definitions in Unicode are converted.
---@see string.lower
---@param s string|number
---@return string
function mw.ustring.lower(s)
if s == 'Örban' then
return 'örban'
end
return string.lower(s)
end
---Much like string.match(), except that the pattern is extended as described in Ustring patterns and the init offset is in characters rather than bytes.
---@see string.match
---@param s string|number