-
Notifications
You must be signed in to change notification settings - Fork 1
/
fixRecentArticles.js
101 lines (86 loc) · 2.68 KB
/
fixRecentArticles.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Skrypt poprawiający ostatnio edytowane artykuły
*/
var bot = require('nodemw'),
client = new bot('config.js'),
SUMMARY = 'Oczyszczanie wikitekstu',
REPLACEMENTS = [
// spany, paragrafy, podkreślenia
/**
{
regexp: /<(span|p) [^>]+>|<\/span>|<\/p>|<u>|<\/u>/g,
repl: ''
},
**/
// unifikacja szerokości obrazków - 300px
{
regexp: /\|thumb\|\d+px/g,
repl: '|thumb|300px'
},
// linki do wikipedii -> interwiki
// [http://pl.wikipedia.org/wiki/Czes%C5%82aw_Mi%C5%82osz Czesławem Miłoszem]
{
regexp: /\[http:\/\/pl.wikipedia.org\/wiki[^\]]+\]/g,
repl: function(link) {
var re = /\/wiki\/([^\s]+) (.*)$/,
matches,
isDigit;
link = link.substring(1, link.length - 1);
matches = link.match(re);
//console.log(link); console.log(matches);
// fallback
if (!matches) return link;
// 23 stycznia / 1910
isDigit = /^\d/.test(matches[1]);
return isDigit ? ('[[' + decodeURIComponent(matches[1]) + '|' + matches[2] + ']]')
: ('[[wikipedia:pl:' + decodeURIComponent(matches[1]) + '|' + matches[2] + ']]');
}
},
// galerie
// <gallery spacing="medium" columns="4">
// <gallery captionalign="left" orientation="none" widths="200" columns="3" bordercolor="#ffffff" bordersize="large" spacing="small">
{
regexp: /<gallery[^>]?>/g,
repl: function(tag) {
if (tag.indexOf('orientation') < 0) {
tag = '<gallery captionalign="left" orientation="none" widths="200" columns="3" bordercolor="#ffffff" bordersize="large" spacing="small">';
console.log('Gallery fixed');
}
return tag;
}
}
];
client.logIn(function() {
// @see http://poznan.wikia.com/api.php?action=query&list=recentchanges&rclimit=500&rctoponly=1&rcnamespace=0
var params = {
action: 'query',
list: 'recentchanges',
rclimit: 250,
rctoponly: 1, // pokazuj tylko ostatnią edycję artykułu
rcnamespace: 0
},
cnt = 0;
client.api.call(params, function(data) {
var pages = data.recentchanges;
pages.forEach(function(page) {
console.log((++cnt) + ') sprawdzam ' + page.title + ' (@' + page.timestamp + ')...');
client.getArticle(page.title, function(content) {
var origContent = content;
REPLACEMENTS.forEach(function(item) {
content = content.replace(item.regexp, item.repl);
});
if (origContent === content) return;
console.log("\n\n");
console.log(page.title + ':');
console.log(origContent.substr(0,1500) + '...\n');
console.log(content.substr(0,1500) + '...');
console.log('---');
if (process.env.DEBUG) return;
// zapisz zmianę
client.edit(page.title, content, SUMMARY, function() {
console.log(page.title + ' zmieniony!');
});
});
});
});
});