Jump to content

User:Suffusion of Yellow/effp-helper-dev.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Suffusion of Yellow (talk | contribs) at 23:11, 28 January 2019 (Development version of User:Suffusion of Yellow/effp-helper.js). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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_dev_aflid=" + abuselog_id[1];

			mw.util.addPortletLink('p-cactions', link , 'make edit (dev)', '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_dev_aflid=([0-9]+)$/)) !== null) {
		mw.loader.using( ['mediawiki.notify', '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 || le.details.article_articleid;
		var le_page_name = le.details.page_prefixedtitle || le.details.article_prefixedtext;
		var final_wikitext = le.details.new_wikitext;
		
		// 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;
			}

			mw.notify("Page has changed. Attempting to merge " + (i == rv.length ? "at least " : "") + i + " edit(s). Your browser may freeze or crash now.");
			
			merged = merge_wikitext(cur_wikitext, le.details.old_wikitext, le.details.new_wikitext);
			
			if (merged !== null) {				
				mw.notify("Merge succeeded!");
				final_wikitext = merged;		
			} else {			
				if (i == rv.length) {
					// FIXME: Get more revisions?
					mw.notify("Too many edits to merge. Nothing done here.");
					
					return;
				}
				
				if (!only_me) 
					mw.notify("Merge failed, and will be attempted on save. Edit conflict likely.",
							  { autoHide : false });
				else
					mw.notify("Merge failed, at it looks you were the only editor to the page. 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;
		}

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

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

	function merge_wikitext(A_text, O_text, B_text) {
		var A = A_text.split("\n");
		var O = O_text.split("\n");
		var B = B_text.split("\n");

		var result = Diff3.diff3_merge(A, O, B, true);

		console.log("Three-way merge result:\n", result);
		
		if (result.length == 1 && result[0].ok !== undefined)
			return result[0].ok.join("\n");
		else
			return null;
	}
});
// </nowiki>