User:Epicgenius/sortcategories.js: Difference between revisions

Content deleted Content added
update
fix
 
(2 intermediate revisions by the same user not shown)
Line 4:
The main script page is [[User:Epicgenius/sortcategories.js]].
*/
$mw.loader.using("mediawiki.util", function ($) {
// Only run script if user is editing an article
/*
if (!document.forms.editform || (mw.config.get("wgAction") !== "edit" && mw.config.get("wgAction") !== "submit")) {
if (
config.mw.wgNamespaceNumber == 1 || //talk page
config.mw.wgArticleId === 0 || // Page doesn't exist
) {
return;
} else (
$(function($) {
)
* /*
if (
mw.loader.using( ['mediawiki.util'] ).then( function () {
config.mw.wgNamespaceNumber == 1 || //talk page
var portletlink = mw.util.addPortletLink('p-tb', '#', 'Sort categories');
config.mw.wgArticleId === 0 || // Page doesn't exist
$(portletlink).click( function(e) {
) {
e.preventDefault();
return;
// Determine if we need to go to the editing page.
} else (
var loc = window.___location.href;
)
if (loc.indexOf('action=edit') < 0 && loc.indexOf('action=submit') < 0) {
*/
alert("Go to the edit page to use this script.");
mw.loader.using( ['mediawiki.util'] ).then( function () {
} else {
var portletlink = mw.util.addPortletLink('p-tb', '#', 'Sort categories');
// Get textbox value
$(portletlink).click( function(e) {
var wpTextbox1 = document.getElementById('wpTextbox1');
e.preventDefault();
var wpTextbox1_V = wpTextbox1.value;
// CategoriesGet totextbox checkvalue
var wpTextbox1 = document.getElementById('wpTextbox1');
var category = "[[Category:";
var categoryAwpTextbox1_V = category+"A "wpTextbox1.value;
var categoryAn = category+"An ";
var categoryThe = category+"The ";
var categoryEnd = "]]"; // This might match to both categories and links
var categoryEponymous = category+mw.config.get("wgTitle");
var defaultSort = "{{DEFAULTSORT:";
var stubTag = "-stub}}";
var someTemplateEnd = "}}";
// Get the text up to the start of the categories, and then all of the text with the categories.
var categoriesStart = wpTextbox1_V.indexOf(category);
var categoriesEnd = wpTextbox1_V.lastIndexOf(categoryEnd);
var textBeforeCategories = wpTextbox1_V.substr(0, categoriesStart).trim();
var textWithCategories = wpTextbox1_V.substr(categoriesStart,categoriesEnd-categoriesStart+2).trim();
var textAfterCategories = wpTextbox1_V.substr(categoriesEnd+2).trim();
// Categories should be on new lines, so split by new line, sort alphabetically with a few checks, then join again with new lines.
// Checks: eponymous categories are listed first; categories are sorted without preceding "The"
var splitCategories = textWithCategories.split("\n");
splitCategories.sort(function(a, b) {
// Categories to check
if (a.substr(0, categoryEponymous.length) == categoryEponymous) return -1e8;
var category = "[[Category:";
if (b.substr(0, categoryEponymous.length) == categoryEponymous) return 1e8;
var categoryA = category+"A ";
var categoryAn = category+"An ";
var categoryThe = category+"The ";
var categoryEnd = "]]"; // This might match to both categories and links
var categoryEponymous = category+mw.config.get("wgTitle");
var defaultSort = "{{DEFAULTSORT:";
var stubTag = "-stub}}";
var someTemplateEnd = "}}";
// Get the text up to the start of the categories, and then all of the text with the categories.
if (a.substr(0, categoryThe.length) == categoryThe) a = a.replace(categoryThe, category);
ifvar (b.substr(0, categoryThe.length)categoriesStart == categoryThe) b = bwpTextbox1_V.replaceindexOf(categoryThe, category);
var categoriesEnd = wpTextbox1_V.lastIndexOf(categoryEnd);
var textBeforeCategories = wpTextbox1_V.substr(0, categoriesStart).trim();
var textWithCategories = wpTextbox1_V.substr(categoriesStart,categoriesEnd-categoriesStart+2).trim();
var textAfterCategories = wpTextbox1_V.substr(categoriesEnd+2).trim();
// Categories should be on new lines, so split by new line, sort alphabetically with a few checks, then join again with new lines.
if (a.substr(0, categoryA.length) == categoryA) a = a.replace(categoryA, category);
// Checks: eponymous categories are listed first; categories are sorted without preceding "The"
if (b.substr(0, categoryA.length) == categoryA) b = b.replace(categoryA, category);
var splitCategories = textWithCategories.split("\n");
splitCategories.sort(function(a, b) {
if (a.substr(0, categoryEponymous.length) == categoryEponymous) return -1e8;
if (b.substr(0, categoryEponymous.length) == categoryEponymous) return 1e8;
if (a.substr(0, categoryThe.length) == categoryThe) a = a.replace(categoryThe, category);
if (b.substr(0, categoryThe.length) == categoryThe) b = b.replace(categoryThe, category);
if (a.substr(0, categoryA.length) == categoryA) a = a.replace(categoryA, category);
if (b.substr(0, categoryA.length) == categoryA) b = b.replace(categoryA, category);
if (a.substr(0, categoryAn.length) == categoryAn) a = a.replace(categoryAn, category);
if (b.substr(0, categoryAn.length) == categoryAn) b = b.replace(categoryAn, category);
return a.localeCompare(b);
});
textWithCategories = splitCategories.join("\n");
if (a.substr(0, categoryAn.length) == categoryAn) a = a.replace(categoryAn, category);
if (b.substr(0, categoryAn.length) == categoryAn) b = b.replace(categoryAn, category);
if (wpTextbox1_V.indexOf(defaultSort) < 0) {
return a.localeCompare(b);
textWithCategories = "\n"+textWithCategories;
});
} else {
textWithCategories = splitCategories.join("\n")textWithCategories;
}
if (wpTextbox1_V.indexOf(defaultSort) < 0) {
if (wpTextbox1_V.indexOf(stubTag) < 0) {
textWithCategories = "\n"+textWithCategories;
textWithCategories = textWithCategories;
} else {
} else {
textWithCategories = textWithCategories+"\n\n";
}
// Merge pre-category text back with the sorted and joined category text, place back in textbok and add summary.
if (wpTextbox1_V.indexOf(stubTag) < 0) {
textWithCategorieswpTextbox1.value = textBeforeCategories+"\n"+textWithCategories+textAfterCategories;
} else {
setoptions(minor = 'true');
textWithCategories = textWithCategories+"\n\n\n";
setreason('sorted categories alphabetically via [[User:Epicgenius/sortcategories|script]]', 'append');
}
doaction('diff');
// Merge pre-category text back with the sorted and joined category text, place back in textbok and add summary.
wpTextbox1.value = textBeforeCategories+"\n"+textWithCategories+textAfterCategories;
});
setoptions(minor = 'true');
setreason('sorted categories alphabetically via [[User:Epicgenius/sortcategories.js|script]]', 'append');
doaction('diff');
}
});
});