From dcc60c18faaa36e32d4fa0b07a9c79d3f68026a7 Mon Sep 17 00:00:00 2001 From: Siddharth VP Date: Thu, 25 Mar 2021 00:04:49 +0530 Subject: [PATCH] Add getMessages() --- src/bot.ts | 24 +++++++++++++++++++++++- tests/bot.test.js | 12 ++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/bot.ts b/src/bot.ts index cff88a4..a4772d0 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -67,7 +67,7 @@ import {ispromise, merge, mergeDeep1, arrayChunk, sleep, makeTitle, makeTitles} import type { ApiDeleteParams, ApiEditPageParams, ApiMoveParams, ApiParseParams, - ApiPurgeParams, ApiQueryAllPagesParams, ApiQueryCategoryMembersParams, + ApiPurgeParams, ApiQueryAllMessagesParams, ApiQueryAllPagesParams, ApiQueryCategoryMembersParams, ApiQuerySearchParams, ApiQueryUserInfoParams, ApiRollbackParams, ApiUndeleteParams, ApiUploadParams } from "./api_params"; @@ -775,6 +775,28 @@ export class mwn { }); } + /** + * Fetch MediaWiki messages + * @param messages + * @param options + */ + getMessages(messages: string | string[], options: ApiQueryAllMessagesParams = {}) { + return this.request({ + "action": "query", + "meta": "allmessages", + "ammessages": messages, + ...options + }).then((data) => { + let result: Record = {}; + data.query.allmessages.forEach((obj) => { + if (!obj.missing) { + result[obj.name] = obj.content; + } + }); + return result; + }); + } + /** * Enable bot emergency shutoff */ diff --git a/tests/bot.test.js b/tests/bot.test.js index a459779..e57718d 100644 --- a/tests/bot.test.js +++ b/tests/bot.test.js @@ -167,6 +167,18 @@ describe('mwn', async function() { expect(pages[0]).to.be.a('string'); }); + it('getMessages', async function () { + let messages = await bot.getMessages(['and', 'word-separator']); + expect(messages).to.deep.equal({ + 'and': ' and', + 'word-separator': ' ' + }); + let singleMessage = await bot.getMessages('colon-separator'); + expect(singleMessage).to.deep.equal({ + 'colon-separator': ': ' + }); + }); + let fileTitle = 'File:Example demo image.png'; it('downloads an image from title without local name specified', function() {