wikiscript 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,59 @@
1
+ <div align="center">
2
+
3
+ # Autowiki
4
+
5
+ **A zero-dependency mediawiki API client for JavaScript.**
6
+
7
+ [![GitHub](https://img.shields.io/github/license/arzeh/wikiscript)](https://github.com/arzeh/wikiscript/blob/main/LICENSE.md)
8
+ [![npm](https://img.shields.io/npm/v/wikiscript?color=crimson&logo=npm&style=flat-square)](https://www.npmjs.com/package/wikiscript)
9
+
10
+ </div>
11
+
12
+ ## Description
13
+
14
+ This library allows to query and perform actions on MediaWiki-based
15
+ wikis. You can query page lists, edit pages, delete them, upload files,
16
+ and any action that MediaWiki supports through its API.
17
+
18
+ **This project is actively evolving.** Although the API is expected to
19
+ remain stable and focus mostly in finding edge-cases or supporting other
20
+ API actions, breaking changes may occur until 1.0. Some types haven't been
21
+ verified to match the actual API response; if you find any error, please
22
+ submit an issue. The current version does not handle errors, but it is intended
23
+ to do so in a future version.
24
+
25
+ ## Features
26
+
27
+ - Use all common query modules (like listing all pages or getting a list of page's categories).
28
+ - **Zero dependencies.**
29
+ - Written in TypeScript.
30
+
31
+ ## Installation
32
+
33
+ ``` sh
34
+ npm install wikiscript
35
+ yarn add wikiscript
36
+ ```
37
+
38
+ ## Usage
39
+
40
+ ``` ts
41
+ import { Wiki } from 'wikiscript';
42
+
43
+ function main() {
44
+ const wiki = new Wiki('https://mywiki.com/api.php');
45
+ const allpages = await wiki.allpages({
46
+ apfrom: 'A',
47
+ aplimit: 'max',
48
+ apto: 'B',
49
+ });
50
+ const titles = allpages.map(page => page.title);
51
+ console.log(titles);
52
+ }
53
+ ```
54
+
55
+ Calling the previous function will print in the console a list of page titles.
56
+
57
+ ## License
58
+
59
+ GNU General Public License version 3.
package/dist/main.cjs ADDED
@@ -0,0 +1,601 @@
1
+
2
+ //#region src/lib/client.ts
3
+ var Client = class Client {
4
+ api;
5
+ cookies = [];
6
+ options;
7
+ constructor(api, options) {
8
+ if (typeof api === "string") this.api = new URL(api);
9
+ else this.api = api;
10
+ this.options = options || {};
11
+ }
12
+ static formData(params) {
13
+ return Object.entries(params).reduce((form, [key, value]) => {
14
+ form.set(key, value);
15
+ return form;
16
+ }, new FormData());
17
+ }
18
+ static searchParams(params) {
19
+ return Object.entries(params).reduce((result, [key, value]) => {
20
+ if (Array.isArray(value)) value = value.join("|");
21
+ switch (typeof value) {
22
+ case "string":
23
+ result.set(key, value);
24
+ break;
25
+ case "boolean":
26
+ result.set(key, value ? "1" : "0");
27
+ break;
28
+ default: result.set(key, `${value}`);
29
+ }
30
+ return result;
31
+ }, new URLSearchParams());
32
+ }
33
+ async get(params, options) {
34
+ const searchParams = Client.searchParams(params);
35
+ return (await this.request(`${this.api}?${searchParams}`, options)).json();
36
+ }
37
+ async post(params, options = {}) {
38
+ let body;
39
+ switch (options.headers["content-type"]) {
40
+ case "application/x-www-form-urlencoded":
41
+ body = Client.searchParams(params);
42
+ break;
43
+ case "multipart/form-data":
44
+ body = Client.formData(params);
45
+ break;
46
+ default:
47
+ body = JSON.stringify(params);
48
+ break;
49
+ }
50
+ options = Object.assign({}, options, {
51
+ body,
52
+ method: "post"
53
+ });
54
+ return (await this.request(this.api, options)).json();
55
+ }
56
+ async request(url, options = {}) {
57
+ const headers = Object.assign({}, this.options.headers || {}, options.headers || {}, { cookie: this.cookies.join(",") });
58
+ options = Object.assign({}, options, { headers });
59
+ const req = await fetch(url, options);
60
+ this.cookies = this.cookies.concat(req.headers.getSetCookie());
61
+ return req;
62
+ }
63
+ };
64
+
65
+ //#endregion
66
+ //#region src/lib/wiki.ts
67
+ var Wiki = class {
68
+ client;
69
+ cachedTokens = {};
70
+ constructor(url, options) {
71
+ this.client = new Client(url, options);
72
+ }
73
+ async action(params, options = {}) {
74
+ const defaults = {
75
+ format: "json",
76
+ formatversion: 2
77
+ };
78
+ if (!options.token) defaults.token = await this.csrfToken();
79
+ params = Object.assign(options, defaults, params);
80
+ return await this.client.post(params);
81
+ }
82
+ /**
83
+ * Enumerate all categories.
84
+ * @see https://www.mediawiki.org/wiki/API:Allcategories
85
+ */
86
+ allcategories(params) {
87
+ return this.query(params, { list: "allcategories" });
88
+ }
89
+ /**
90
+ * List all deleted revisions by a user or in a namespace.
91
+ * @see https://www.mediawiki.org/wiki/API:Alldeletedrevisions
92
+ */
93
+ alldeletedrevisions(params) {
94
+ return this.query(params, { list: "alldeletedrevisions" });
95
+ }
96
+ /**
97
+ * List all file usages, including non-existing.
98
+ * @see https://www.mediawiki.org/wiki/API:Allfileusages
99
+ */
100
+ allfileusages(params) {
101
+ return this.query(params, { list: "allfileusages" });
102
+ }
103
+ /**
104
+ * Enumerate all images sequentially.
105
+ * @see https://www.mediawiki.org/wiki/API:Allimages
106
+ */
107
+ allimages(params) {
108
+ return this.query(params, { list: "allimages" });
109
+ }
110
+ /**
111
+ * Enumerate all links that point to a given namespace.
112
+ * @see https://www.mediawiki.org/wiki/API:Alllinks
113
+ */
114
+ alllinks(params) {
115
+ return this.query(params, { list: "alllinks" });
116
+ }
117
+ /**
118
+ * Returns messages from the site.
119
+ * @see https://www.mediawiki.org/wiki/API:Allmessages
120
+ */
121
+ async allmessages(params) {
122
+ return (await this.query(params, { meta: "allmessages" })).query.allmessages;
123
+ }
124
+ /**
125
+ * Enumerate all pages sequentially in a given namespace.
126
+ * @see https://www.mediawiki.org/wiki/API:Allpages
127
+ */
128
+ allpages(params) {
129
+ return this.query(params, { list: "allpages" });
130
+ }
131
+ /**
132
+ * List all redirects to a namespace.
133
+ * @see https://www.mediawiki.org/wiki/API:Allredirects
134
+ */
135
+ allredirects(params) {
136
+ return this.query(params, { list: "allredirects" });
137
+ }
138
+ /**
139
+ * List all revisions.
140
+ * @see https://www.mediawiki.org/wiki/API:Allrevisions
141
+ */
142
+ allrevisions(params) {
143
+ return this.query(params, { list: "allrevisions" });
144
+ }
145
+ /**
146
+ * List all transclusions (pages embedded using `{{x}}`), including non-existing.
147
+ * @see https://www.mediawiki.org/wiki/API:Alltransclusions
148
+ */
149
+ alltransclusions(params) {
150
+ return this.query(params, { list: "alltransclusions" });
151
+ }
152
+ /**
153
+ * Enumerate all registered users.
154
+ * @see https://www.mediawiki.org/wiki/API:Allusers
155
+ */
156
+ allusers(params) {
157
+ return this.query(params, { list: "allusers" });
158
+ }
159
+ /**
160
+ * Find all pages that link to the given page.
161
+ * @see https://www.mediawiki.org/wiki/API:Backlinks
162
+ */
163
+ backlinks(params) {
164
+ return this.query(params, { list: "backlinks" });
165
+ }
166
+ /**
167
+ * Block a user.
168
+ */
169
+ async block(params) {
170
+ return (await this.action(params, { action: "block" })).block;
171
+ }
172
+ /**
173
+ * List all blocked users and IP addresses.
174
+ * @see https://www.mediawiki.org/wiki/API:Blocks
175
+ */
176
+ blocks(params) {
177
+ return this.query(params, { list: "blocks" });
178
+ }
179
+ /**
180
+ * List all categories the pages belong to.
181
+ * @see https://www.mediawiki.org/wiki/API:Categories
182
+ */
183
+ categories(params) {
184
+ return this.query(params, { prop: "categories" });
185
+ }
186
+ /**
187
+ * Returns information about the given categories.
188
+ * @see https://www.mediawiki.org/wiki/API:Categoryinfo
189
+ */
190
+ categoryinfo(params) {
191
+ return this.query(params, { prop: "categoryinfo" });
192
+ }
193
+ /**
194
+ * List all pages in a given category.
195
+ * @see https://www.mediawiki.org/wiki/API:Categorymembers
196
+ */
197
+ categorymembers(params) {
198
+ return this.query(params, { list: "categorymembers" });
199
+ }
200
+ /**
201
+ * Get the list of logged-in contributors (including temporary users) and the count of
202
+ * logged-out contributors to a page.
203
+ * @see https://www.mediawiki.org/wiki/API:Contributors
204
+ */
205
+ contributors(params) {
206
+ return this.query(params, { prop: "contributors" });
207
+ }
208
+ async csrfToken(force = false) {
209
+ return (await this.tokens("csrf", force)).csrftoken;
210
+ }
211
+ /**
212
+ * Get deleted revision information.
213
+ * @see https://www.mediawiki.org/wiki/API:Deletedrevisions
214
+ */
215
+ deletedrevisions(params) {
216
+ return this.query(params, { prop: "deletedrevisions" });
217
+ }
218
+ /**
219
+ * List all files that are duplicates of the given files based on hash values.
220
+ * @see https://www.mediawiki.org/wiki/API:Duplicatefiles
221
+ */
222
+ duplicatefiles(params) {
223
+ return this.query(params, { prop: "duplicatefiles" });
224
+ }
225
+ /**
226
+ * Create and edit pages.
227
+ */
228
+ async edit(params) {
229
+ return (await this.action(params, {
230
+ action: "edit",
231
+ assert: params.bot ? "bot" : "user"
232
+ })).edit;
233
+ }
234
+ /**
235
+ * Find all pages that embed (transclude) the given title.
236
+ * @see https://www.mediawiki.org/wiki/API:Embeddedin
237
+ */
238
+ embeddedin(params) {
239
+ return this.query(params, { list: "embeddedin" });
240
+ }
241
+ /**
242
+ * Returns all external URLs (not interwikis) from the given pages.
243
+ * @see https://www.mediawiki.org/wiki/API:Extlinks
244
+ */
245
+ extlinks(params) {
246
+ return this.query(params, { prop: "extlinks" });
247
+ }
248
+ /**
249
+ * Enumerate pages that contain a given URL.
250
+ * @see https://www.mediawiki.org/wiki/API:Exturlusage
251
+ */
252
+ exturlusage(params) {
253
+ return this.query(params, { list: "exturlusage" });
254
+ }
255
+ /**
256
+ * Enumerate all deleted files sequentially.
257
+ * @see https://www.mediawiki.org/wiki/API:Filearchive
258
+ */
259
+ filearchive(params) {
260
+ return this.query(params, { list: "filearchive" });
261
+ }
262
+ /**
263
+ * Return meta information about image repositories configured on the wiki.
264
+ * @see https://www.mediawiki.org/wiki/API:Filerepoinfo
265
+ */
266
+ async filerepoinfo(params) {
267
+ return (await this.query(params, { meta: "filerepoinfo" })).query.repos;
268
+ }
269
+ /**
270
+ * Find all pages that use the given files.
271
+ * @see https://www.mediawiki.org/wiki/API:Fileusage
272
+ */
273
+ fileusage(params) {
274
+ return this.query(params, { prop: "fileusage" });
275
+ }
276
+ /**
277
+ * Get the list of pages to work on by executing the specified query module.
278
+ * @see https://www.mediawiki.org/wiki/API:Query#Generators
279
+ */
280
+ generate(params) {
281
+ return this.query(params);
282
+ }
283
+ /**
284
+ * Returns global image usage for a certain image.
285
+ * @see https://www.mediawiki.org/wiki/API:Globalusage
286
+ */
287
+ globalusage(params) {
288
+ return this.query(params, { prop: "globalusage" });
289
+ }
290
+ /**
291
+ * Returns file information and upload history.
292
+ * @see https://www.mediawiki.org/wiki/API:Imageinfo
293
+ */
294
+ imageinfo(params) {
295
+ return this.query(params, { prop: "imageinfo" });
296
+ }
297
+ /**
298
+ * Return all files contained on the given pages.
299
+ * @see https://www.mediawiki.org/wiki/API:Images
300
+ */
301
+ images(params) {
302
+ return this.query(params, { prop: "images" });
303
+ }
304
+ /**
305
+ * Find all pages that use the given image title.
306
+ * @see https://www.mediawiki.org/wiki/API:Imageusage
307
+ */
308
+ imageusage(params) {
309
+ return this.query(params, { list: "imageusage" });
310
+ }
311
+ /**
312
+ * Get basic page information.
313
+ * @see https://www.mediawiki.org/wiki/API:Info
314
+ */
315
+ info(params) {
316
+ return this.query(params, { prop: "info" });
317
+ }
318
+ /**
319
+ * Find all pages that link to the given interwiki link.
320
+ * Can be used to find all links with a prefix, or all links to a title (with a given prefix).
321
+ * Using neither parameter is effectively "all interwiki links".
322
+ * @see https://www.mediawiki.org/wiki/API:Iwbacklinks
323
+ */
324
+ iwbacklinks(params) {
325
+ return this.query(params, { list: "iwbacklinks" });
326
+ }
327
+ /**
328
+ * Returns all interwiki links from the given pages.
329
+ * @see https://www.mediawiki.org/wiki/API:Iwlinks
330
+ */
331
+ iwlinks(params) {
332
+ return this.query(params, { prop: "iwlinks" });
333
+ }
334
+ /**
335
+ * Find all pages thast link to the given language link.
336
+ * Can be used to find all links with a language code, or all links to a title (with a given language).
337
+ * Using neither parameter is effectively "all language links".
338
+ * @see https://www.mediawiki.org/wiki/API:Langbacklinks
339
+ */
340
+ langbacklinks(params) {
341
+ return this.query(params, { list: "langbacklinks" });
342
+ }
343
+ /**
344
+ * Returns all interlanguage links from the given pages.
345
+ * @see https://www.mediawiki.org/wiki/API:Langlinks
346
+ */
347
+ langlinks(params) {
348
+ return this.query(params, { prop: "langlinks" });
349
+ }
350
+ /**
351
+ * Returns all links from the given pages.
352
+ * @see https://www.mediawiki.org/wiki/API:Links
353
+ */
354
+ links(params) {
355
+ return this.query(params, { prop: "links" });
356
+ }
357
+ /**
358
+ * Find all pages that link to the given pages.
359
+ * @see https://www.mediawiki.org/wiki/API:Linkshere
360
+ */
361
+ linkshere(params) {
362
+ return this.query(params, { prop: "linkshere" });
363
+ }
364
+ /**
365
+ * Get events from logs.
366
+ * @see https://www.mediawiki.org/wiki/API:Logevents
367
+ */
368
+ logevents(params) {
369
+ return this.query(params, { list: "logevents" });
370
+ }
371
+ /**
372
+ * Log in and get authentication tokens.
373
+ *
374
+ * This action should only be used in combination with Special:BotPasswords.
375
+ *
376
+ * This will modify your "Wiki" instance, and all next requests will be authenticated.
377
+ * @see https://www.mediawiki.org/wiki/API:Login
378
+ */
379
+ async login(username, password) {
380
+ const tokens = await this.tokens("login");
381
+ const params = {
382
+ action: "login",
383
+ format: "json",
384
+ formatversion: "2",
385
+ lgname: username,
386
+ lgpassword: password,
387
+ lgtoken: tokens.logintoken
388
+ };
389
+ return (await this.client.post(params, { headers: { "content-type": "application/x-www-form-urlencoded" } })).login;
390
+ }
391
+ /**
392
+ * Log out and clear session data.
393
+ */
394
+ async logout() {
395
+ const params = {
396
+ action: "logout",
397
+ token: await this.csrfToken()
398
+ };
399
+ await this.client.post(params, { headers: { "content-type": "application/x-www-form-urlencoded" } });
400
+ }
401
+ /**
402
+ * List all page property names in use on the wiki.
403
+ * @see https://www.mediawiki.org/wiki/API:Pagepropnames
404
+ */
405
+ pagepropnames(params) {
406
+ return this.query(params, { list: "pagepropnames" });
407
+ }
408
+ /**
409
+ * Get various page properties defined in the page content.
410
+ * @see https://www.mediawiki.org/wiki/API:Pageprops
411
+ */
412
+ pageprops(params) {
413
+ return this.query(params, { prop: "pageprops" });
414
+ }
415
+ /**
416
+ * List all pages using a given page property.
417
+ * @see https://www.mediawiki.org/wiki/API:Pageswithprop
418
+ */
419
+ pageswithprop(params) {
420
+ return this.query(params, { list: "pageswithprop" });
421
+ }
422
+ /**
423
+ * Perform a prefix search for page titles.
424
+ * @see https://www.mediawiki.org/wiki/API:Prefixsearch
425
+ */
426
+ prefixsearch(params) {
427
+ return this.query(params, { list: "prefixsearch" });
428
+ }
429
+ /**
430
+ * List all titles protected from creation.
431
+ * @see https://www.mediawiki.org/wiki/API:Protectedtitles
432
+ */
433
+ protectedtitles(params) {
434
+ return this.query(params, { list: "protectedtitles" });
435
+ }
436
+ async query(params, options = {}) {
437
+ options = Object.assign(options, {
438
+ action: "query",
439
+ format: "json",
440
+ formatversion: 2
441
+ });
442
+ params = Object.assign(options, params);
443
+ return await this.client.get(params);
444
+ }
445
+ /**
446
+ * Get a list provided by a QueryPage-based special page.
447
+ * @see https://www.mediawiki.org/wiki/API:Querypage
448
+ */
449
+ querypage(params) {
450
+ return this.query(params, { list: "querypage" });
451
+ }
452
+ /**
453
+ * Get a set of random pages.
454
+ * @see https://www.mediawiki.org/wiki/API:Random
455
+ */
456
+ random(params) {
457
+ return this.query(params, { list: "random" });
458
+ }
459
+ /**
460
+ * Enumerate recent changes.
461
+ * @see https://www.mediawiki.org/wiki/API:Recentchanges
462
+ */
463
+ recentchanges(params) {
464
+ return this.query(params, { list: "recentchanges" });
465
+ }
466
+ /**
467
+ * Returns all redirects to the given pages.
468
+ * @see https://www.mediawiki.org/wiki/API:Redirects
469
+ */
470
+ redirects(params) {
471
+ return this.query(params, { prop: "redirects" });
472
+ }
473
+ /**
474
+ * Get revision information.
475
+ * @see https://www.mediawiki.org/wiki/API:Revisions
476
+ */
477
+ revisions(params) {
478
+ return this.query(params, { prop: "revisions" });
479
+ }
480
+ /**
481
+ * Perform a full text search.
482
+ * @see https://www.mediawiki.org/wiki/API:Search
483
+ */
484
+ search(params) {
485
+ return this.query(params, { list: "search" });
486
+ }
487
+ /**
488
+ * Return general information about the site.
489
+ * @see https://www.mediawiki.org/wiki/API:Siteinfo
490
+ */
491
+ async siteinfo(params) {
492
+ return (await this.query(params, { meta: "siteinfo" })).query;
493
+ }
494
+ /**
495
+ * List change tags.
496
+ * @see https://www.mediawiki.org/wiki/API:Tags
497
+ */
498
+ tags(params) {
499
+ return this.query(params, { list: "tags" });
500
+ }
501
+ /**
502
+ * Returns all pages transcluded on the given pages.
503
+ * @see https://www.mediawiki.org/wiki/API:Templates
504
+ */
505
+ templates(params) {
506
+ return this.query(params, { prop: "templates" });
507
+ }
508
+ /**
509
+ * Gets tokens for data-modifying actions.
510
+ * @see https://www.mediawiki.org/wiki/API:Tokens
511
+ */
512
+ async tokens(tokenType, force = false) {
513
+ if (!force && typeof tokenType === "string" && this.cachedTokens[tokenType]) return { [`${tokenType}token`]: this.cachedTokens[tokenType] };
514
+ let type;
515
+ if (Array.isArray(tokenType)) type = tokenType.join("|");
516
+ else type = tokenType;
517
+ return (await this.client.get({
518
+ action: "query",
519
+ format: "json",
520
+ formatversion: "2",
521
+ meta: "tokens",
522
+ type
523
+ })).query.tokens;
524
+ }
525
+ /**
526
+ * Find all pages that transclude the given pages.
527
+ * @see https://www.mediawiki.org/wiki/API:Transcludedin
528
+ */
529
+ transcludedin(params) {
530
+ return this.query(params, { prop: "transcludedin" });
531
+ }
532
+ /**
533
+ * Unblock a user.
534
+ */
535
+ async unblock(params) {
536
+ return (await this.action(params, { action: "unblock" })).unblock;
537
+ }
538
+ /**
539
+ * Upload a file, or get the status of pending uploads.
540
+ */
541
+ async upload(params) {
542
+ const defaults = {
543
+ action: "upload",
544
+ format: "json",
545
+ formatversion: 2,
546
+ token: await this.csrfToken()
547
+ };
548
+ params = Object.assign(defaults, params);
549
+ return await this.client.post(params, { headers: { "content-type": "multipart/form-data" } });
550
+ }
551
+ /**
552
+ * Get all edits by a user.
553
+ * @see https://www.mediawiki.org/wiki/API:Usercontribs
554
+ */
555
+ usercontribs(params) {
556
+ return this.query(params, { list: "usercontribs" });
557
+ }
558
+ /**
559
+ * Get information about the current user.
560
+ * @see https://www.mediawiki.org/wiki/API:Userinfo
561
+ */
562
+ async userinfo(params) {
563
+ return (await this.query(params, { meta: "userinfo" })).query.userinfo;
564
+ }
565
+ /**
566
+ * Change a user's group membership.
567
+ */
568
+ async userrights(params) {
569
+ const token = await this.tokens("userrights");
570
+ return (await this.action(params, {
571
+ action: "userrights",
572
+ token: token.userrightstoken
573
+ })).userrights;
574
+ }
575
+ /**
576
+ * Get information about a list of users.
577
+ * @see https://www.mediawiki.org/wiki/API:Users
578
+ */
579
+ users(params) {
580
+ return this.query(params, { list: "users" });
581
+ }
582
+ /**
583
+ * Get recent changes to pages in the current user's watchlist.
584
+ * @see https://www.mediawiki.org/wiki/API:Watchlist
585
+ */
586
+ watchlist(params) {
587
+ return this.query(params, { list: "watchlist" });
588
+ }
589
+ /**
590
+ * Get all pages on the current user's watchlist.
591
+ * @see https://www.mediawiki.org/wiki/API:Watchlistraw
592
+ */
593
+ watchlistraw(params) {
594
+ return this.query(params, { list: "watchlistraw" });
595
+ }
596
+ };
597
+
598
+ //#endregion
599
+ exports.Client = Client;
600
+ exports.Wiki = Wiki;
601
+ //# sourceMappingURL=main.cjs.map