Skip to content

Commit

Permalink
Add getMessages()
Browse files Browse the repository at this point in the history
  • Loading branch information
siddharthvp committed Mar 24, 2021
1 parent 6e03515 commit dcc60c1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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<string, string> = {};
data.query.allmessages.forEach((obj) => {
if (!obj.missing) {
result[obj.name] = obj.content;
}
});
return result;
});
}

/**
* Enable bot emergency shutoff
*/
Expand Down
12 changes: 12 additions & 0 deletions tests/bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit dcc60c1

Please sign in to comment.