Content deleted Content added
Implement caching of template list to speed up load times |
read external includelist and blocklist |
||
Line 1:
//jshint maxerr:
//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 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";
▲ "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
var
var listData = JSON.parse($.ajax({
url: mw.util.wikiScript('api'), async:false,
error: function (jsondata) {
console.log("Unable to fetch contents of " +
}, data: {action:'parse', format:'json', prop:'wikitext', page:
}).responseText).parse;
if (
try {
} catch (jsonerror) {
console.log("Error parsing JSON
wikiprojects = {};▼
}
console.log("Error parsing " + listName + " data:");
if (wikiprojects && wikiprojects._timestamp) {▼
var cacheTimestamp = new Date(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...");▼
▲ }
console.log("Cache timestamp \"" + cacheTimestamp + "\" OK.");
} else {
console.log("
wikiprojects = {};
}
} else {
console.log("
▲ console.log(cacheData);
wikiprojects = {};
}
if (Object.entries(wikiprojects).length == 0) {
getTemplateCategories();
} else {
constructForm();
}
Line 324 ⟶ 339:
action: "query",
list: "categorymembers",
cmtitle:
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) {
} );
}
Line 341 ⟶ 356:
getTemplateCategories(data.continue);
} else {
//console.log(
getTemplatesFromCategories();
}
Line 351 ⟶ 366:
function getTemplatesFromCategories(catTitle, cont) { // Recursively call API
if (typeof catTitle === 'undefined') { // Grab next item in wikiProjectCategories
catTitle =
}
if (typeof catTitle === 'undefined') { // No remaining entries in wikiProjectCategories
// Manually add Wikiprojects with /s in their title
jQuery.extend( wikiprojects, fetchJSONList(templateIncludelistName) );
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
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);
|