User:FlightTime/SortCats.js

/* This is a custom fork of [[User:Epicgenius/sortcategories.js]] See original script by [[User:Alex 21]], [[User:Alex 21/script-categoriessort.js]] ********…

/*
This is a custom fork of  [[User:Epicgenius/sortcategories.js]]

See original script by [[User:Alex 21]], [[User:Alex 21/script-categoriessort.js]]

********************************************************************************************
To install this version copy below and add it to your favorite .js page

  importScript('User:FlightTime/SortCats.js'); // Backlink: [[User:FlightTime/SortCats.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")) {
		return;
	}
	$(function($) {
		/*
		if (
				config.mw.wgNamespaceNumber == 1 || //talk page
				config.mw.wgArticleId === 0 || // Page doesn't exist
		) {
			return;
		} else (
		)
		*/
		mw.loader.using( ['mediawiki.util'] ).then( function () {
			var portletlink = mw.util.addPortletLink('p-tb', '#', 'SORT-CATS');
			$(portletlink).click( function(e) {
				e.preventDefault();
	
					// Get textbox value
					var wpTextbox1 = document.getElementById('wpTextbox1');
					var wpTextbox1_V = wpTextbox1.value;
					
					// Categories to check
					var category = "[[Category:";
					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.
					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) {
						
						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 (wpTextbox1_V.indexOf(defaultSort) < 0) {
						textWithCategories = "\n"+textWithCategories;
					} else {
						textWithCategories = textWithCategories;
					}
					
					if (wpTextbox1_V.indexOf(stubTag) < 0) {
                        textWithCategories = textWithCategories;
                    } else {
                        textWithCategories = textWithCategories + "\n\n";
                    }
					
					// 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('Categories sorted alphabetically by [[User:FlightTime/SortCats|script]]', 'append');
					doaction('diff');
				document.getElementById('wpMinoredit').checked = true;
			});
		});
	});
});

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.