Module:Sandbox/Ahecht/flag/redirects/doc: Difference between revisions

Content deleted Content added
create
 
update
 
(2 intermediate revisions by the same user not shown)
Line 1:
{{tl|Module rating |<!-- Values: pre-alpha • alpha • beta • release • protected -- If a rating not needed/relevant, delete this template call -->}}
<!-- Please place categories where indicated at the bottom of this page and interwikis at Wikidata (see [[Wikipedia:Wikidata]]) -->
== Usage ==
 
Generated by the following code:
<syntaxhighlight lang="js">
var api = new mw.Api();
var titlePrefix = "Module:Sandbox/Ahecht/flag/"
var query = {
action: "query",
generator: "allpages",
gapprefix: "Country data ",
gapnamespace: "10",
gapfilterredir: "nonredirects",
gaplimit: "max",
prop: "redirects",
rdlimit: "max"
};
 
pageIDs = {};
redirects = {};
 
function apiGet(query, 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);
Line 44 ⟶ 41:
}
if (data && data['continue']) {
console.logshowStatus("Getting...")
return apiGet(query, data['continue']);
} else {
apiDone();
Line 54 ⟶ 51:
 
function apiDone() {
console.logshowStatus("Done!");
console.logshowStatus(Object.keys(pageIDs).length + " pages, " + Object.keys(redirects).length + " redirects.");
output = 'return {\n';
for (title in redirects) {
Line 61 ⟶ 58:
}
output += '}';
console.logshowStatus(output);
apiPost("redirects", output);
}
 
function apiPost(alias, text) {
apiGet(query);
var params = {
</syntaxhighlight>
action: "edit",
title: titlePrefix + alias.replace(/\s/, "_"),
summary: "Extracting data from templates with prefix [[Template:Country data]]",
text: text
};
 
api.postWithEditToken( params ).done( function ( data ) {
}}</includeonly>
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(query);
</syntaxhighlight>