JavaScript MediaWiki API
A simple way to access MediaWiki API via JavaScript with wikitext parser.
This is basically a modern syntax version of CeJS MediaWiki module. For example, using async functions.
Features
- Read / edit pages.
- Get list of categorymembers, pages transclude specified template, and more...
- Auto-limited editing rate.
- Parse wikitext / pages. You may modify parts of the wikitext, then regenerate the page just using .toString(). See wikitext parser examples.
Installation
This is a nodejs module. Please install node.js first.
npm install wikiapi
Usage
Here lists some examples of this module.
As node.js module
const Wikiapi = require('wikiapi');
(async () => {
const wiki = new Wikiapi('zh');
await wiki.login('user', 'password');
let page_data = await wiki.page('Universe', {});
console.log('page_data: ', page_data);
console.log('page_data.text: ', page_data.wikitext);
let page_data = await wiki.page('Universe', { revisions: 2 });
console.log('page_data: ', page_data);
console.log('page_data.text: ', page_data.wikitext);
let page_data = await wiki.page('Universe'),
newContent = page_data.wikitext + '\nTest edit using wikiapi.';
await enwiki.edit(
function (page_data) { return newContent; },
{ bot: 1, summary: 'Test edit.' }
);
let page_data = await wiki.page('Universe'),
newContent = page_data.wikitext.replace(/Test edit using wikiapi/g, 'Test: replace content was successful!');
await enwiki.edit(
function (page_data) { return newContent; },
{ bot: 1, minor: 1, nocreate: 1, summary: 'Test: replace content.' }
);
let page_data = await wiki.page('Universe');
await wiki.edit(
'{{Speedy|reason=Vandalism}}',
{ bot: 1, minor: 0, nocreate: 1, summary: 'Test: wipe clean, please delete.' }
);
await wiki.edit_page('Wikipedia:Sandbox', function (page_data) {
return page_data.wikitext + '\nTest edit using {{GitHub|kanasimi/wikiapi}}.';
}, { bot: 1, nocreate: 1, minor: 1, summary: 'Test: edit page via .edit_page().' });
let list = ['Wikipedia:Sandbox', 'Wikipedia:Sandbox2', 'Wikipedia:Sandbox/wikiapi'];
let listMembers = await wiki.categorymembers('Chemical elements');
let listLinks = await wiki.redirects_here('ABC');
let listTranscluded = await wiki.embeddedin('Template:Periodic table');
let listSearch = await wiki.search(' dragon');
await wiki.for_each_page(
listMembers,
page_data => { return `{{stub}}\n` + page_data.wikitext; },
{ summary: 'Test: multi-edits', minor: 1 }
);
await wiki.for_each_page(
listLinks,
page_data => {
console.log(page_data.title);
return Wikiapi.skip_edit;
},
);
result = await wiki.move_page('Wikipedia:Sanbox/Wikiapi', 'Wikipedia:Sanbox/NewWikiapi',
{ reason: 'Test: move page (1).', noredirect: true, movetalk: true }
);
result = await wiki.move_page('Wikipedia:Sanbox/NewWikiapi', 'Wikipedia:Sanbox/Wikiapi',
{ reason: 'Test: move page (2).', noredirect: true, movetalk: true }
);
const page_data = await wiki.page('JavaScript');
const parsed = page_data.parse();
let infobox;
parsed.each('template', template_token => {
if (template_token.name.startsWith('Infobox')) {
infobox = template_token.parameters;
return parsed.each.exit;
}
});
for (const [key, value] of Object.entries(infobox))
infobox[key] = value.toString();
console.log(infobox);
const parsed = await wiki.page('Wikipedia:Sandbox').parse();
parsed.each('template', template_token => { });
await wiki.edit(parsed.toString(), { bot: 1, minor: 1, nocreate: 1 });
let page_data = await wiki.page('Universe');
page_data.parse().each('template',
token => console.log(token.name));
wiki.listen(function for_each_row() {
}, {
delay: '2m',
filter: function filter_row(row) {
},
with_diff: { LCS: true, line: true },
namespace: '0|talk',
});
let options = {
description: 'Photo of Osaka',
date: new Date() || '2021-01-01',
source_url: 'https://github.com/kanasimi/wikiapi',
author: '[[User:user]]',
permission: '{{cc-by-sa-2.5}}',
other_versions: '',
other_fields: '',
license: ['{{cc-by-sa-2.5}}'],
categories: ['[[Category:test images]]'],
bot: 1,
tags: "tag1|tag2",
};
let result = await wiki.upload({
file_path: '/local/file/path',
filename: 'New_Osaka_Photograph.jpg',
comment: '',
ignorewarnings: 1,
...options
});
result = await wiki.upload({
media_url: 'https://media.url/Thunder-Dragon.ogg',
text: "Her eis wikicode to replave the page's content instead of various other parameters.",
comment: 'Thunder Dragon audio from vacation in Philipines. Page uses custom template.',
ignorewarnings: 1,
...options
});
const wiki = new Wikiapi('https://wikidata.org/w/api.php')
let page_data = await wiki.data('Q1');
console.log(page_data.labels.zh)
let data = await wiki.data('Universe', 'P1419');
console.assert(data.includes('shape of the universe'));
let entity = await wiki.data('Q7');
await entity.modify({ claims: [{ P17: 'Q213280' }] });
await entity.modify({ claims: [{ language: 'en', country: [, 'Test Country 1'] }] });
await entity.modify({ claims: [{ language: 'en', country: [, 'Test Country 1'], remove: true }] });
await entity.modify({ labels: [{ language: 'zh-tw', value: '地球' }] });
})();
More examples: Please see test.js.
OS support
Platform | support |
---|
Windows | ✔️ |
macOS | ✔️ |
UNIX, Linux | ✔️ |
See also
For old style JavaScript, or general environment usage, please see wikibot.
Contact
Contact us at GitHub.