-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit-Commons-wikitext.js
39 lines (32 loc) · 1.28 KB
/
edit-Commons-wikitext.js
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
// PURPOSE: For a given a category, gets the list of pages, edits existing content.
const Wikiapi= require('wikiapi');
const logins = require('./logins-ShufaBot.js');
// Login credentials from .login*.js
var USER = logins.commons.user,
PASS = logins.commons.pass,
API = logins.commons.api;
(async () => {
// Connect
const targetWiki = new Wikiapi;
await targetWiki.login(USER, PASS, API);
console.log(`Username ${USER} is connected !`);
// List of targets
const list = await targetWiki.categorymembers('ShufaBot test: upload');
// Loop on targets
for(i=0;i<list.length;i++){
console.log(list[i]);
var currPage = list[i], // target page's page_data
title= currPage.title,
text= currPage.text;
// Page exist ?
let pageData = await targetWiki.page(title, {});
if(pageData.wikitext!=='') {
//Edit page, replace string:
await targetWiki.edit_page(title, function(page_data) {
return page_data.wikitext.replace(/toto/g,'') // <------- regex match and replacement
+ `\n[[Category:ShufaBot test: edit]]`;
}, {bot: 1, nocreate: 1, minor: 1, summary: 'ShufaBot test: edit'});
console.log('Done.');
}
}
})();