User:Ahecht/Scripts/draft-sorter.js: Difference between revisions

Content deleted Content added
Implement caching of template list to speed up load times
read external includelist and blocklist
Line 1:
//jshint maxerr:256512
//Based on [[User:Enterprisey/draft-sorter.js]] <nowiki>
( function ( $, mw ) { mw.loader.using( ["mediawiki.api", "jquery.chosen", "oojs-ui-core"], function () {
Line 20:
// If it's already there, don't duplicate
if ( $( "#draft-sorter-wrapper" ).length ) { return; }
];
 
// Configure defaults
var templateCache = mw.config.get("wgFormattedNamespaces")[2]+":"+mw.config.get("wgUserName")+"/Scripts/draft-sorter.json";
//var templateCache = "Wikipedia:WikiProject Articles for creation/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>" )
Line 67 ⟶ 81:
var existingProjects = [];
var wikiprojects = {};
var templateBlockList = fetchJSONList(templateBlocklistName);
var templateCache = mw.config.get("wgFormattedNamespaces")[2]+":"+mw.config.get("wgUserName")+"/Scripts/draft-sorter.json";
var wikiprojectCategories = [
"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",
];
 
api.get( {
Line 101 ⟶ 108:
predicts = [];
function checkTemplateCachefetchJSONList(listName) {
var cacheDataparsedList = JSON.parse($.ajax({};
}
var listData = JSON.parse($.ajax({
url: mw.util.wikiScript('api'), async:false,
error: function (jsondata) {
console.log("Unable to fetch contents of " + templateCachelistName + ": " + jasondata);
getTemplateCategories()return {};
}, data: {action:'parse', format:'json', prop:'wikitext', page: templateCachelistName, formatversion: '2' }
}).responseText).parse;
if (cacheDatalistData && cacheDatalistData.wikitext) {
try {
wikiprojectsparsedList = JSON.parse(cacheDatalistData.wikitext);
} catch (jsonerror) {
console.log("Error parsing JSON cachelist " + listName + ": " + jsonerror);
wikiprojects = {};
}
} else {
console.log("Error parsing " + listName + " data:");
if (wikiprojects && wikiprojects._timestamp) {
console.log(cacheDatalistData);
var cacheTimestamp = new Date(wikiprojects._timestamp);
}
delete wikiprojects._timestamp;
 
//Check if cache timestamp is more than 24 hours old
return parsedList;
if ( isFinite(cacheTimestamp) && (Date.now() - cacheTimestamp < 86400000) ) {
}
console.log("Using cached values.");
} else {
function checkTemplateCache() {
console.log("Cache too old, refreshing...");
wikiprojects = {}fetchJSONList(templateCache);
 
}
wikiprojectsif =(wikiprojects._timestamp) {};
var cacheTimestamp = new Date(wikiprojects._timestamp);
if (wikiprojects &&delete wikiprojects._timestamp) {;
//Check if cache timestamp is more than 24 hours old
if ( isFinite(cacheTimestamp) && (Date.now() - cacheTimestamp < 86400000) ) {
console.log("Cache timestamp \"" + cacheTimestamp + "\" OK.");
} else {
console.log("Cannot findCache timestamp in\"" + cacheTimestamp + "\" too cacheold. dataDiscarding.");
wikiprojects = {};
}
} else {
console.log("ErrorCannot parsingfind timestamp in project cache data:list. Discarding.");
console.log(cacheData);
wikiprojects = {};
}
 
if (Object.entries(wikiprojects).length == 0) {
console.log("Cache tooempty. Fetching new old,project refreshinglist...");
getTemplateCategories();
} else {
console.log("Using cached valuesproject list.");
constructForm();
}
Line 324 ⟶ 339:
action: "query",
list: "categorymembers",
cmtitle: "Category:WikiProject banner wrapper templates"wikiProjectMainCategory,
cmtype: "subcat",
cmlimit: "max"
Line 335 ⟶ 350:
if (data && data.query && data.query.categorymembers) { //API query returned members
Object.entries(data.query.categorymembers).forEach( function(item) {
wikiprojectCategorieswikiProjectCategories.push(item[1].title);
} );
}
Line 341 ⟶ 356:
getTemplateCategories(data.continue);
} else {
//console.log(wikiprojectCategorieswikiProjectCategories);
getTemplatesFromCategories();
}
Line 351 ⟶ 366:
function getTemplatesFromCategories(catTitle, cont) { // Recursively call API
if (typeof catTitle === 'undefined') { // Grab next item in wikiProjectCategories
catTitle = wikiprojectCategorieswikiProjectCategories.pop();
}
if (typeof catTitle === 'undefined') { // No remaining entries in wikiProjectCategories
// Manually add Wikiprojects with /s in their title
jQuery.extend( wikiprojects, fetchJSONList(templateIncludelistName) );
wikiprojects["C/C++"] = "WikiProject C/C++";
wikiprojects["Hinduism/Shaktism"] = "WikiProject Hinduism/Shaktism";
wikiprojects["Indigenous peoples of North America/Anishinaabe"] = "WikiProject Indigenous peoples of North America/Anishinaabe";
wikiprojects["MIT/Media Lab"] = "WikiProject MIT/Media Lab";
wikiprojects["Nigeria/Lagos"] = "WikiProject Nigeria/Lagos";
writeTemplateCache();
} else {
Line 383 ⟶ 394:
if (title && title[1] && title[1] !== "") { //Valid page name format
if (title[1].indexOf("/") == -1 || title[1].match(/ task ?force$/i) ) { //No subpages
wikiprojectstitle[1] = title[1].replace(/ task ?force$/i,"")] = item[1].title.replace("Template:", "");
if(templateBlockList[ title[1] ]) {
//console.log("Entry on blocklist:" + title[1]);
} else {
wikiprojects[ title[1] ] = item[1].title.replace("Template:", "");
}
} else {
//console.log("Subpage rejected:" + item[1].title);