// Configure defaults
//var templateCache = mw.config.get("wgFormattedNamespaces")[2]+":"+mw.config.get("wgUserName")+"/Scripts/draft-sorter.json";
//var templateCache = "WikipediaUser:WikiProject Articles for creationAhechtbot/WikiProject templates.json";
var templateIncludelistName = "Wikipedia:WikiProject Articles for creation/WikiProject templates.json/includelist.json";
var templateBlocklistName = "Wikipedia:WikiProject Articles for creation/WikiProject templates.json/blocklist.json";
var wikiProjectMainCategory = "Category:WikiProject banner wrapper templates";
var wikiProjectCategories = [ // other WikiProject categories not included in wikiProjectMainCategory
"Category:WikiProject banners with quality assessment",
"Category:WikiProject banners without quality assessment",
"Category:WikiProject banner templates not based on WPBannerMeta",
"Category:Inactive WikiProject banners",
"Category:WikiProject banner wrapper templates",
];
// Define the form
var form = $( "<div>" )
console.log( existingProjects );
fetchJSONList(templateCache).then( (cachedList) => {
checkTemplateCache(wikiprojects = cachedList);
constructForm();
} );
} ).fail (function() {
console.log("Retrieving project templates from talk page failed.");
fetchJSONList(templateCache).then( (cachedList) => {
checkTemplateCache(wikiprojects = cachedList);
constructForm();
} );
});
return parsedList;
}
function checkTemplateCache(cachedList) {
console.log("Retrieved cache from "+templateCache+".");
if (cachedList._timestamp) {
var cacheTimestamp = new Date(cachedList._timestamp);
delete cachedList._timestamp;
//Check if cache timestamp is more than 24 hours old
if ( isFinite(cacheTimestamp) && (Date.now() - cacheTimestamp < 86400000) ) {
console.log("Cache timestamp \"" + cacheTimestamp + "\" OK.");
wikiprojects = cachedList;
constructForm();
} else {
console.warn("Cache timestamp \"" + cacheTimestamp + "\" too old. Rebuilding...");
getTemplateCategories();
}
} else {
console.warn(wikiprojects);
console.warn("Cannot find timestamp in cache. Rebuilding...");
getTemplateCategories();
}
}
function writeTemplateCache() {
wikiprojects = Object.keys(wikiprojects).sort().reduce(
(obj, key) => {
obj[key] = wikiprojects[key];
return obj;
},
{}
);
wikiprojects._timestamp = new Date().toJSON();
var params = { action: 'edit',
title: templateCache,
text: JSON.stringify(wikiprojects),
summary: "Update WikiProject cache ([[User:Ahecht/Scripts/draft-sorter|draft-sorter]])",
watchlist:"unwatch"
};
new mw.Api().postWithToken("csrf", params ).done( function(reslt) {
console.log(templateCache + " updated:");
console.log(reslt);
constructForm();
} ).fail( function(reslt) {
console.error("Error updating " + templateCache + ":");
console.error(reslt);
constructForm();
} );
}
nextButton.setLabel( "Loading..." ).setDisabled( true );
}
window.___location.href = "https://randomincategory.toolforge.org/Pending_AfC_submissions?draftsorttrigger=y&server=" + mw.config.get("wgServerName");
getDrafts();
function getDrafts(cont) { // Recursively call API
var query = {
action: "query",
list: "categorymembers",
cmtitle: "Category:Pending_AfC_submissions",
cmprop: "title",
cmnamespace: "118",
cmtype: "page",
cmlimit: "max"
};
if (cont) {
query = Object.assign(query, cont);
}
new mw.Api().get( query )
.done (function (data) {
if (data && data.query && data.query.categorymembers) { //API query returned pages
data.query.categorymembers.forEach(function(item) {
draftList.push( item.title );
} );
}
if (data && data.continue) { //More results are available
getDrafts(data.continue);
} else { // Redirect to random page
console.log("Done fetching drafts!");
window.___location.href = mw.config.get( "wgServer" )
+ "/wiki/"
+ draftList[Math.random() * draftList.length | 0]
+ "?draftsorttrigger=y";
}
} ).fail (function(error) { // Use Special:RandomInCategory
console.error("Error getting list of drafts:");
console.error(error);
window.___location.href = mw.config.get( "wgServer" )
+ "/wiki/Special:RandomInCategory/Pending_AfC_submissions?draftsorttrigger=y";
} );
return;
}
}
} );
return;
}
function getTemplateCategories(cont) { // Recursively call API
var query = {
action: "query",
list: "categorymembers",
cmtitle: wikiProjectMainCategory,
cmtype: "subcat",
cmlimit: "max"
};
if (cont) {
query = Object.assign(query, cont);
}
new mw.Api().get( query )
.done (function (data) {
if (data && data.query && data.query.categorymembers) { //API query returned members
Object.entries(data.query.categorymembers).forEach( function(item) {
wikiProjectCategories.push(item[1].title);
} );
}
if (data && data.continue) { //More results are available
getTemplateCategories(data.continue);
} else {
fetchJSONList(templateBlocklistName).then( (templateBlocklist) => {
console.log("Template blocklist:");
console.log(templateBlocklist);
getTemplatesFromCategories(templateBlocklist);
} );
}
} ).fail (function(error) {
console.error("Error getting list of categories:");
console.error(error);
} );
return;
}
function getTemplatesFromCategories(templateBlockList, catTitle, cont) { // Recursively call API
if (typeof catTitle === 'undefined') { // Grab next item in wikiProjectCategories
catTitle = wikiProjectCategories.pop();
}
if (typeof catTitle === 'undefined') { // No remaining entries in wikiProjectCategories
// Manually add Wikiprojects with /s in their title
fetchJSONList(templateIncludelistName).then( (templateIncludelist) => {
console.log("Template includelist:");
console.log(templateIncludelist);
jQuery.extend( wikiprojects, templateIncludelist );
writeTemplateCache();
} );
} else {
var query = {
action: "query",
list: "categorymembers",
cmtitle: catTitle,
cmtype: "page",
cmnamespace: "10",
cmlimit: "max"
};
if (cont) {
query = Object.assign(query, cont);
}
new mw.Api().get( query )
.done (function (data) {
if (data && data.query && data.query.categorymembers) { //API query returned members
Object.entries(data.query.categorymembers).forEach( function(item) {
var title = item[1].title.match(/^Template:WikiProject\s(.*)$/);
if (title && title[1] && title[1] !== "") { //Valid page name format
if (title[1].indexOf("/") == -1 || title[1].match(/ task ?force$/i) ) { //No subpages
title[1] = title[1].replace(/ task ?force$/i,"");
if( !(templateBlockList[ title[1] ]) ) { //Not on blocklist
wikiprojects[ title[1] ] = item[1].title.replace("Template:", "");
}
}
}
} );
}
if (data && data.continue) { //More results are available
getTemplatesFromCategories(templateBlockList, catTitle, data.continue);
} else {
getTemplatesFromCategories(templateBlockList);
}
} ).fail (function(error) {
console.error("Error getting list of templates:");
console.error(error);
constructForm();
} );
return;
}
}
|