User:ZI Jony/Abuse filter helper.js

From Meta, a Wikimedia project coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
// This is a forked version of [[en:User:Suffusion of Yellow/effp-helper.js]].
// Copied to User:ZI Jony/Abuse filter helper.js after FWTH retired, who took over as maintainer.
// Experimental script to copy text from [[Special:AbuseLog]] entries to the edit form, and merge with subsequent revisions if possible.

// Installation
// Add the following code to your local wiki [[Special:MyPage/common.js]] page or meta's [[Special:MyPage/global.js]].
// mw.loader.load('//meta.wikimedia.org/w/index.php?title=User:ZI_Jony/Abuse_filter_helper.js&action=raw&ctype=text/javascript');

// <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 af_vars = mw.config.get('wgAbuseFilterVariables');

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

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

			mw.util.addPortletLink('p-cactions', link , 'Perform 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 = " ([[m:User:ZI Jony/Abuse filter helper.js|AF 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;
		}

		$('#wpTextbox1').val(le.details.new_wikitext);
		$('#wpSummary').val(sum_begin + sum_end);

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