Difference between revisions of "Module:Sandbox/Ahecht/flag/redirects/doc"
< Module:Sandbox | Ahecht | flag | redirects
Jump to navigation
Jump to search
blackwiki>Ahecht (cleanup) |
blackwiki>Ahecht (update) |
||
Line 2: | Line 2: | ||
<syntaxhighlight lang="js"> | <syntaxhighlight lang="js"> | ||
var api = new mw.Api(); | var api = new mw.Api(); | ||
− | var | + | var titlePrefix = "Module:Sandbox/Ahecht/flag/" |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
pageIDs = {}; | pageIDs = {}; | ||
redirects = {}; | redirects = {}; | ||
− | function apiGet( | + | function apiGet(cont) { |
+ | var query = { | ||
+ | action: "query", | ||
+ | generator: "allpages", | ||
+ | gapprefix: "Country data ", | ||
+ | gapnamespace: "10", | ||
+ | gapfilterredir: "nonredirects", | ||
+ | gaplimit: "max", | ||
+ | prop: "redirects", | ||
+ | rdlimit: "max" | ||
+ | }; | ||
if (cont) { | if (cont) { | ||
query = Object.assign(query, cont); | query = Object.assign(query, cont); | ||
Line 40: | Line 41: | ||
} | } | ||
if (data && data['continue']) { | if (data && data['continue']) { | ||
− | + | showStatus("Getting...") | |
− | return apiGet( | + | return apiGet(data['continue']); |
} else { | } else { | ||
apiDone(); | apiDone(); | ||
Line 50: | Line 51: | ||
function apiDone() { | function apiDone() { | ||
− | + | showStatus("Done!"); | |
− | + | showStatus(Object.keys(pageIDs).length + " pages, " + Object.keys(redirects).length + " redirects."); | |
output = 'return {\n'; | output = 'return {\n'; | ||
for (title in redirects) { | for (title in redirects) { | ||
Line 57: | Line 58: | ||
} | } | ||
output += '}'; | output += '}'; | ||
− | + | showStatus(output); | |
+ | apiPost("redirects", output); | ||
} | } | ||
− | apiGet( | + | function apiPost(alias, text) { |
+ | var params = { | ||
+ | action: "edit", | ||
+ | title: titlePrefix + alias.replace(/\s/, "_"), | ||
+ | summary: "Extracting data from [[Template:Country data " + alias + "]]", | ||
+ | text: text | ||
+ | }; | ||
+ | |||
+ | api.postWithEditToken( params ).done( function ( data ) { | ||
+ | if ( data && data.edit && data.edit.result && data.edit.result === "Success" ) { | ||
+ | showStatus( | ||
+ | "Edit saved successfully to https:" + | ||
+ | (mw.config.values.wgServer + mw.config.values.wgArticlePath).replace("$1", data.edit.title.replace(/\s/, "_")) | ||
+ | ); | ||
+ | } else { | ||
+ | showStatus( "Couldn't save due to error: " + JSON.stringify( data ) ); | ||
+ | } | ||
+ | } ).fail( function ( error ) { | ||
+ | showStatus( "Couldn't save due to error: " + JSON.stringify( error ) ); | ||
+ | } ); | ||
+ | } | ||
+ | |||
+ | function showStatus(status) { | ||
+ | console.log(status); | ||
+ | } | ||
+ | |||
+ | apiGet(); | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 15:54, 13 June 2020
Generated by the following code:
var api = new mw.Api();
var titlePrefix = "Module:Sandbox/Ahecht/flag/"
pageIDs = {};
redirects = {};
function apiGet(cont) {
var query = {
action: "query",
generator: "allpages",
gapprefix: "Country data ",
gapnamespace: "10",
gapfilterredir: "nonredirects",
gaplimit: "max",
prop: "redirects",
rdlimit: "max"
};
if (cont) {
query = Object.assign(query, cont);
}
api.get( query )
.done (function (data) {
if (data && data.query) {
if (data.query.pages) {
Object.entries(data.query.pages).forEach(function(item) {
if (!/\//.test(item[1].title) ) {
itemTitle = item[1].title.replace(/Template:(?:Flag|Country) data /,'');
pageIDs[itemTitle] = item[1].pageid;
if(item[1].redirects) {
for (var i = 0; i < item[1].redirects.length; i++) {
fromTitle = item[1].redirects[i].title.replace(/Template:(?:Flag|Country) data /,'');
if (fromTitle != itemTitle) { redirects[fromTitle] = itemTitle; }
}
}
}
} );
}
}
if (data && data['continue']) {
showStatus("Getting...")
return apiGet(data['continue']);
} else {
apiDone();
return;
}
} );
}
function apiDone() {
showStatus("Done!");
showStatus(Object.keys(pageIDs).length + " pages, " + Object.keys(redirects).length + " redirects.");
output = 'return {\n';
for (title in redirects) {
output += '\t["' + title + '"] = "' + redirects[title] + '",\n';
}
output += '}';
showStatus(output);
apiPost("redirects", output);
}
function apiPost(alias, text) {
var params = {
action: "edit",
title: titlePrefix + alias.replace(/\s/, "_"),
summary: "Extracting data from [[Template:Country data " + alias + "]]",
text: text
};
api.postWithEditToken( params ).done( function ( data ) {
if ( data && data.edit && data.edit.result && data.edit.result === "Success" ) {
showStatus(
"Edit saved successfully to https:" +
(mw.config.values.wgServer + mw.config.values.wgArticlePath).replace("$1", data.edit.title.replace(/\s/, "_"))
);
} else {
showStatus( "Couldn't save due to error: " + JSON.stringify( data ) );
}
} ).fail( function ( error ) {
showStatus( "Couldn't save due to error: " + JSON.stringify( error ) );
} );
}
function showStatus(status) {
console.log(status);
}
apiGet();