Jump to content

User:Suffusion of Yellow/effp-helper-test.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.
/* 
 * WP:EF/FP helper
 *
 * Experimental script to copy text from Special:AbuseLog entries to the edit form, and merge
 * with subsequent revisions if possible.
 */

// <nowiki>
$(function() {
	var abuselog_id;
	
	// First see if we are on a details/examine page, and add a handy link if so (under "More" menu in Vector)
	if ((abuselog_id = mw.config.get('wgPageName').match(/^Special:Abuse(?:Filter\/examine\/log|Log)\/([0-9]+)$/)) !== null) {
		mw.loader.using( 'mediawiki.util', function() {
			var page_title = $('.mw-abuselog-details-page_prefixedtitle div.mw-abuselog-var-value').text();

			// Private log entry and no permissions, no need to irritate with an error message every time, so fail silently
			if (!page_title.length)
				return;

			// Use action=submit instead of action=edit to prevent 2017 editor from loading
			var link = mw.config.get('wgScript') + "?title=" + mw.util.wikiUrlencode(page_title)
						+ "&action=submit&effp_aflid=" + abuselog_id[1];

			mw.util.addPortletLink('p-cactions', link , 'make edit', 'ca-makeedit', 'Perform edit on behalf of user');
		});
	} // Otherwise, did we follow the link added above?
	else if (mw.config.get('wgAction') == "submit" && (abuselog_id = window.location.href.match(/&effp_aflid=([0-9]+)/)) !== null) {
		mw.loader.using( [ 'mediawiki.api'], function() {
			mw.notify("Fetching data from filter log...");

			var api = new mw.Api();
			api.get( {
				action: 'query',
				prop: 'revisions',
				pageids: mw.config.get('wgArticleId'),
				rvprop: "ids|timestamp|user",
				rvlimit: 50,
				list: 'abuselog',
				afllogid: abuselog_id[1],
				aflprop: 'ids|user|timestamp|details'
			}).done(preload_edit) .fail(function() {
				mw.notify("Failed to copy data from filter log" );
			});
		});
	}
	
	function preload_edit(api_response) {
		var cur_wikitext = $('#wpTextbox1').val();
		var page_id = mw.config.get('wgArticleId');
		var page_name = mw.config.get('wgPageName').replace(/_/g, ' ');
		
		if (!api_response.query || !api_response.query.abuselog[0] || !api_response.query.abuselog[0].details) {
			mw.notify("Incomplete response from API");
			
			return;
		}
		
		var le = api_response.query.abuselog[0];
		var le_page_id = le.details.page_id;
		var le_page_name = le.details.page_prefixedtitle;

		// Text scraped from form (always?) has a trailing \n, text from API does not
		cur_wikitext = cur_wikitext.replace(/\n$/, '');

		// Sanity check
		if (le_page_id !== '0' && le_page_id != page_id) {
			mw.notify("Log entry is from page id " + le_page_id + " but this page has id " + page_id);

			return;
		}

		// Sanity check for page creations
		if (le_page_name != page_name) {
			mw.notify("Log entry is from page name \"" + le_page_name + "\" but this page has name \"" + page_name + "\"");

			return;
		}

		// The current state of the page is the same as what the editor was trying to do
		if (le.details.new_wikitext === cur_wikitext) {
			mw.notify("Edit already made. Nothing to do here.");

			return;
		}

		// The current state of the page is different from what it was when the editor made the attempt
		if (le.details.old_wikitext !== cur_wikitext) {
			var i, rv = api_response.query.pages[page_id].revisions;
			var only_me = true;
			
			if (le_page_id === '0') {
				mw.notify("Page already created and has different content. Not done");
				
				return;
			}

			// Find the newest revision made before the attempt
			for(i = 0; i < rv.length; i++) {
				if (rv[i].timestamp < le.timestamp)
					break;

				if (rv[i].user != mw.config.get('wgUserName'))
					only_me = false;
			}

			if (i == rv.length) {
				// We could fetch more revisions, but the chance of not getting an edit conflict is slim at this point
				mw.notify("Page has changed, and at least " + i + " edits have been made since the attempt. Not done.");

				return;
			}

			if (!only_me) 
				mw.notify("Page has changed, and " + i + " edit(s) have made been since the attempt. Merge will be attempted on save.",
						  { autoHide : false });
			else
				mw.notify("You have made " + i + " edit(s) to this page since the attempt. Your changes may be overwritten.",
						  { autoHide : false });

			// Apparently MW uses inconsistent date formats...
			var starttime = le.timestamp.replace(/[^0-9]/g, '');
			var edittime = rv[i].timestamp.replace(/[^0-9]/g, '');

			// Now it should appear to the server that we clicked "edit" whenever the abuse log entry was made
			$('input[name=wpStarttime]').val(starttime);
			$('input[name=wpEdittime]').val(edittime);
			$('input[name=editRevId]').val(rv[i].revid);
		}

		var sum_begin = "Edit made on behalf of [[Special:Contributions/" + le.user + "|" +
			le.user + "]] ([[User talk:" + le.user + "|talk]])  because it was [[Special:AbuseLog/" + le.id +
			"|disallowed]] by an edit filter";

		var sum_end = " ([[User:Suffusion of Yellow/effp-helper|effp-helper]])";

		if (le.details.summary.length) {
			sum_begin += ". Original summary was \"";
			sum_end = "\"" + sum_end;

			var remaining = 500 - sum_begin.length - sum_end.length;

			if (le.details.summary.length > remaining)
				sum_begin += le.details.summary.substring(0, remaining - 3) + "...";
			else
				sum_begin += le.details.summary;
		}

		var text = le.details.new_wikitext;

		// Sign with user's name, not our own!
		if (text.indexOf("~~~") !== -1 && confirm("Substitue signature(s)?")) {
			var user_prefix =
				(mw.util.isIPv4Address(le.user) ||
				 mw.util.isIPv6Address(le.user)) ?
				"Special:Contributions/" : "User:";
			var sig = "[[" + user_prefix + le.user + "|" + le.user +
					"]] ([[User talk:" + le.user + "|talk]])";

			var ts = new Date(le.timestamp);
			var ts_str = ("0" + ts.getUTCHours()).slice(-2)
                + ":" + ("0" + ts.getUTCMinutes()).slice(-2)
                + ", " + ts.getUTCDate()
                + " " + mw.config.get('wgMonthNames')[ts.getUTCMonth() + 1]
                + " " + ts.getUTCFullYear() + " (UTC)";

			text = text.replace("~~~~~", ts_str);
			text = text.replace("~~~~", sig + " " + ts_str);
			text = text.replace("~~~", sig);
		}

		$('#wpTextbox1').val(text);
		$('#wpSummary').val(sum_begin + sum_end);

		mw.notify("Edit copied from filter log. Please verify before saving.");
	}
});
// </nowiki>