Jump to content

User:Quibik/EightRequestsHelper.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// To use this script, place "importScript( 'User:Quibik/EightRequestsHelper.js' ); // [[User:Quibik/EightRequestsHelper.js]]"
// to the bottom of the page opened by http://wiki.riteme.site/wiki/Special:MyPage/vector.js
// This will add a link "Update Eight Requests" to the menu next to the search box, if you are on the Eight Requests page.

function eightRequestsHelper() {

if (/erhelper=init/i.test(location.href)) {
	//window.open("/w/index.php?title=Wikipedia:Graphics_Lab/Photography_workshop/Eight_Requests/Users_log&action=edit&erhelper=process", "ER User Log");
	window.open("/w/index.php?title=Wikipedia:Graphics_Lab/Photography_workshop/Eight_Requests/Archive/" + (new Date()).getFullYear() + "&action=edit&erhelper=process", "ER Archive");
	window.open("http://commons.wikimedia.org/wiki/User:Quibik/Files_by_viewcounts", "Files by viewcounts");
}

if (/\/Eight_Requests/i.test(location.href) && mw.config.get( 'wgAction' ) === 'edit') {
	if (!/\/Eight_Requests\//i.test(location.href)) {
		// Replace the completed requests with empty fields
		var m = /<!--COPY.+\s*(<gallery[\s\S]+<\/gallery>)/i.exec(jQuery("#wpTextbox1").val());
		if (!m) {
			return;
		}
		var cleanGallery = m[1];
		jQuery("#wpTextbox1").val(jQuery("#wpTextbox1").val()
			.replace(/<center>\s*(<gallery[\s\S]+<\/gallery>)\s*<\/center>/i, "<center>\n" + cleanGallery + "\n</center>"));
		jQuery("#wpSummary").val("Added a new set of images. (using a [[User:Quibik/EightRequestsHelper.js|script]])");
	}
	else {
		jQuery.get("/w/index.php?title=Wikipedia:Graphics_Lab/Photography_workshop/Eight_Requests&action=raw&nocache=" + new Date().getTime(), "", function(data) {
			var m = /<center>\s*(<gallery[\s\S]+<\/gallery>)\s*<\/center>/i.exec(data);
			if (!m) {
				return;
			}
			var gal = m[1];
			
			var re = /^\s*(?:File|Image):.+taken by\s*=\s*([^|]+)\s*\|.+$/mig;
			var requests = "";
			var counts = new Object();
			while (true) {
				m = re.exec(gal);
				if (!m)
					break;
				requests += m[0] + "\n";
				m[1] = jQuery.trim(m[1]);
				if (m[1].length > 0)
					counts[m[1]] = (counts.hasOwnProperty(m[1]) ? counts[m[1]] : 0) + 1;
			}
			
			if (/\/Eight_Requests\/Archive/i.test(location.href)) {
				// Archive the most recent set of requests
				var monthnames = [ "January", "February", "March", "April", "May", "June",
					"July", "August", "September", "October", "November", "December" ];
				var d = new Date();
				jQuery("#wpTextbox1").val(jQuery("#wpTextbox1").val() + 
					"\n;" + d.getDate() + " " + monthnames[d.getMonth()] + "\n" +
					"<gallery widths=\"160px\" perrow=\"4\">\n" +
					requests +
					"</gallery>"
				);
				jQuery("#wpSummary").val("Archived the most recent set. (using a [[User:Quibik/EightRequestsHelper.js|script]])");
				jQuery("#wpDiff").click();
			}
			else if (/\/Eight_Requests\/Users_log/i.test(location.href)) {
				// Update the user log
				var re = /^\|\s*{{User\|\s*([^}]+)\s*}}\s*\|\|\s*(\d+)\s*$/mig;
				var m;
				while (true) {
					m = re.exec(jQuery("#wpTextbox1").val());
					if (!m)
						break;
					m[1] = jQuery.trim(m[1]);
					counts[m[1]] = (counts.hasOwnProperty(m[1]) ? counts[m[1]] : 0) + parseInt(m[2]);
				}
				var names = [];
				for(var name in counts){
					names.push(name);
				}
				names.sort();
				
				userList = ""
				for (x in names) {
					name = names[x];
					userList += "|-\n| {{User|" + name + "}} || " + counts[name] + "\n";
				}
				jQuery("#wpTextbox1").val(jQuery("#wpTextbox1").val()
					.replace(/\|-[\s\S]+\|}\n\n==/i, userList + "|}\n\n==")
					.replace(/as of \d.+/im, "as of ~~~~~. Updated by ~~~"));
				jQuery("#wpSummary").val("Updated. (using a [[User:Quibik/EightRequestsHelper.js|script]])");
				jQuery("#wpDiff").click();
			}
		});
	}
}
}

$( document ).ready( function () {
	if (/\/Eight_Requests/i.test(location.href))
		mw.util.addPortletLink ( "p-cactions", "/w/index.php?title=Wikipedia:Graphics_Lab/Photography_workshop/Eight_Requests&action=edit&erhelper=init", "Update Eight Requests", "ca-eightRequestsHelper");
	if (/erhelper=/i.test(location.href))
		eightRequestsHelper();
} );