Jump to content

User:LinguistAtLarge/afdlog.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by LinguistAtLarge (talk | contribs) at 07:47, 21 April 2009 (tru). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// ############################### SAVE TO AFD LOG ##############################
importScript('User:LinguistAtLarge/jquery132.js');
var afdlog = function()
{
	if (!wgPageName || wgPageName.indexOf('Wikipedia:Articles_for_deletion') == -1)
	{
		return -1; // not on afd at all
	}

	var skiplist = [
		{'matchtype':'start', 'text':'Wikipedia:Articles_for_deletion/Log/'},
		{'matchtype':'exact', 'text':'Wikipedia:Articles_for_deletion'},
		{'matchtype':'exact', 'text':'Wikipedia:Articles_for_deletion/Old'},
		{'matchtype':'exact', 'text':'Wikipedia:Articles_for_deletion/Policy_consensus'},
		{'matchtype':'exact', 'text':'Wikipedia:Articles_for_deletion/Common_outcomes'},
	];

	var i;
	for (i in skiplist)
	{
		if (skiplist[i] && skiplist[i].matchtype && skiplist[i].text)
		{
			if (skiplist[i].matchtype == 'start')
			{
				if (wgPageName.indexOf(skiplist[i].text) == 0)
				{
					return -2;
				}
			}
			else if (skiplist[i].matchtype == 'exact')
			{
				if (wgPageName == skiplist[i].text)
				{
					return -3;
				}
			}
		}
	}

	if (document.referrer.indexOf('title=' + wgPageName + '&action=edit') == -1)
	{
//		return -4; // page has not just been saved
	}

	// see if the user has participated in this afd
	var has_participated_in_afd = function()
	{
		var anchors = document.getElementById('bodyContent').getElementsByTagName('a');
		var i;
		for (i = 0; i < anchors.length; ++i)
		{
			if (	
				anchors[i].href &&
				anchors[i].href.indexOf('User:' + wgUserName) != -1 &&
				anchors[i].parentNode &&
				anchors[i].parentNode.nodeName.toUpperCase() != 'SMALL' &&
				anchors[i].parentNode.parentNode &&
				anchors[i].parentNode.parentNode.nodeName.toUpperCase() != 'SMALL'
			)
			{
				return true;
			}
		}
		return false;
	};

	// helper function to create a link
	var construct_wikilink = function(articlename, linktext)
	{
		if (typeof(linktext) == 'undefined' || !linktext)
		{
			linktext = articlename;
		}
		return '<a href="/wiki/' + encodeURI(articlename) + '" title="' + linktext + '">' + linktext + '</a>';
	};
	
	if (has_participated_in_afd())
	{
		if (typeof(afdlog_config) == 'undefined' || typeof(afdlog_config.afdlog_page) == 'undefined')
		{
			jsMsg('You just participated in this deletion discussion, but you need to set up your AfD log page before you can log the discussions.');
			return -7;
		}
		var logpage = 'User:' + wgUserName + '/' + afdlog_config.afdlog_page;

		if (typeof(wfSupportsAjax) == 'function' && !wfSupportsAjax())
		{
			jsMsg('You just participated in this deletion discussion, but your browser does not support AJAX, so it cannot be saved to your ' + construct_wikilink(logpage, 'AfD log'));
			return -6;
		}

		jsMsg('Your participation in this deletion discussion will now be written to your ' + construct_wikilink(logpage, 'AfD log') + '.');

		// save it
		var edit_page = new LaL_SimplePageEditor();
		var pagename = wgPageName.replace(/_/g, ' ');
		edit_page.append(logpage, '\*\[\[' + pagename + '\]\] &mdash; \~\~\~\~\~', 'Loggin participation in AfD: \[\[' + pagename + '\]\]', function(){jsMsgAppend('<b>Log written.</b>');});

		// done
	}
	else
	{
		return -5;
	}
};

function LaL_SimplePageEditor()
{
	var that = this;

	// wrappers to append, prepend or overwrite a page
	this.append = function(pagename, pagetext, editsummary, callback)
	{
		this.edit_page_start('append', pagename, pagetext, editsummary, callback);
	};
	this.prepend = function(pagename, pagetext, editsummary, callback)
	{
		this.edit_page_start('prepend', pagename, pagetext, editsummary, callback);
	};
	this.overwrite = function(pagename, pagetext, editsummary, callback)
	{
		this.edit_page_start('overwrite', pagename, pagetext, editsummary, callback);
	};

	// start editing the page (get token)
	this.edit_page_start = function(edittype, pagename, pagetext, editsummary, callback)
	{
		this.edittype		= edittype;
		this.pagename		= pagename;
		this.pagetext		= pagetext;
		this.editsummary	= editsummary;
		this.callback		= callback;

		jsMsgAppend('Fetching edit token...');

		var url = wgServer + '/w/api.php?format=json&action=query&prop=info&indexpageids=1&intoken=edit&titles=' + encodeURIComponent(this.pagename);
		
		$.get(url, function(d, t){that.edit_page_continue(d, t);}, 'json');
	};

	// second stage of editing the page
	this.edit_page_continue = function(data, textstatus)
	{
		var response = eval('(' + data + ')');
		this.token = response['query']['pages'][response['query']['pageids'][0]]['edittoken'];

		alert('edit_page_continue');
		alert(this.token);
		alert(
			'edittype: ' + this.edittype + "\n" +
			'pagename: ' + this.pagename + "\n" +
			'pagetext: ' + this.pagetext + "\n" +
			'editsummary: ' + this.editsummary + "\n"
		);
		jsMsgAppend('Done.');

		alert(this.callback);
		this.callback();
	};	
}

// utility function to append to the jsMsg output
function jsMsgAppend(text)
{
	var currenttext = '';
	var el = document.getElementById('mw-js-message');
	if (el)
	{
		currenttext = el.innerHTML;
	}
	jsMsg(currenttext + ' ' + text);
}

addOnloadHook(afdlog);