User:Mike Dillon/Scripts/topicCats.js
< User:Mike Dillon | Scripts
Note – after saving, you may have to bypass your browser’s cache to see the changes.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- This script lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • redirects • your own
var topicCatPageType, topicCatName;
(function () {
var m = wgPageName.match(/^Template:topic_cat_(parents|description)\//);
if (!m) return;
topicCatPageType = m[1];
topicCatName = wgTitle.split(/\//, 2)[1];
})();
if (wgCanonicalNamespace == 'Category') {
topicCatName = wgTitle.match(/:/) ? wgTitle.split(/:/, 2)[1] : wgTitle;
addOnloadHook(function () {
var tabs = document.getElementById('p-cactions');
if (!tabs) return;
var tabList = tabs.getElementsByTagName('ul')[0];
if (!tabList) return;
var parentConfigTemplate = 'Template:topic_cat_parents/' + topicCatName;
var x = sajax_init_object();
x.open('GET', '/w/api.php?action=query&&format=json&titles=' + encodeURIComponent(parentConfigTemplate), true);
x.onreadystatechange = function () {
if (x.readyState != 4 || x.status != 200) return;
eval('processParentsChecked(' + x.responseText + ')');
};
x.send(null);
function processParentsChecked(json) {
if (!json.query) return;
if (!json.query.pages) return;
var missing = !!(json.query.pages['-1']);
var url = wgScript + '?action=edit&title=' + encodeURIComponent(parentConfigTemplate);
if (missing) url += '&preload=Template:topic_cat_parents/preload';
with (easydom) {
var tab = li(a({ 'href': url }, 'Parents'));
if (missing) {
tab.className = 'new';
}
tabList.appendChild(tab);
}
return false;
}
});
} else if (topicCatPageType == 'parents') {
addOnloadHook(function () {
var tb = document.getElementById('wpTextbox1');
var diff = document.getElementById('wpDiff');
var summary = document.getElementById('wpSummary');
if (!(tb && diff && summary)) return;
var m = tb.value.match(/\{\{topic cat parents\/helper\|(.*)\|lang=/);
if (!m) return;
var declaredParents = m[1].split(/\|/);
with (easydom) {
var updateParents = input({ 'type': 'button', 'value': 'Update parents' });
updateParents.onclick = doUpdateParents;
diff.parentNode.insertBefore(updateParents, diff.nextSibling);
}
function doUpdateParents() {
var x = sajax_init_object();
x.open('GET', '/w/api.php?action=query&prop=categories&format=json&titles=' + encodeURIComponent('Category:' + topicCatName), true);
x.onreadystatechange = function () {
if (x.readyState != 4 || x.status != 200) return;
eval('processCategories(' + x.responseText + ')');
};
x.send(null);
}
function processCategories(json) {
if (!json.query) return;
if (!json.query.pages) return;
var page;
for (var id in json.query.pages) {
page = json.query.pages[id];
}
if (!page.categories) return;
var existingParents = page.categories.map(function (e) {
return e.title.replace(/^Category:/, '');
});
if (!existingParents) return;
var newValue = tb.value.replace(/(\{\{topic cat parents\/helper\|).*(\|lang=)/,
'$1' + existingParents.join('|') + '$2');
if (tb.value != newValue) {
tb.value = newValue;
summary.value = 'Updated parent categories to: ' + existingParents.join(', ');
diff.click();
}
return false;
}
});
}