User:LinguistAtLarge/afdlog.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:LinguistAtLarge/afdlog. |
// ############################### SAVE TO AFD LOG ##############################
importScript('User:LinguistAtLarge/jquery132.js');
var afdlogdebug = false;
var afdlog = function()
{
if (!wgPageName || wgPageName.indexOf('Wikipedia:Articles_for_deletion') == -1)
{
return -1; // not on afd at all
}
if (afdlogdebug) { alert('On AfD. wgPageName: ' + wgPageName); }
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)
{
if (afdlogdebug) { alert('Skiplist match (Start). wgPageName: ' + wgPageName); }
return -2;
}
}
else if (skiplist[i].matchtype == 'exact')
{
if (wgPageName == skiplist[i].text)
{
if (afdlogdebug) { alert('Skiplist match (Exact). wgPageName: ' + wgPageName); }
return -3;
}
}
}
}
if (afdlog_config.autolog)
{
if (afdlogdebug) { alert('Autolog (Yes)'); }
if (document.referrer.indexOf('title=' + wgPageName + '&action=edit') == -1 && document.referrer.indexOf('title=' + wgPageName + '&action=submit') == -1)
{
if (afdlogdebug) { alert('Autolog (Yes). Page has not "just been saved"'); }
return -4; // page has not just been saved
}
if (document.location.href.indexOf('&action=submit') != -1)
{
if (afdlogdebug) { alert('Autolog (Yes). Page is being previewed'); }
return -8; // page is being previewed
}
// 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'
)
{
if (afdlogdebug) { alert('has_participated_in_afd() returning true'); }
return true;
}
}
if (afdlogdebug) { alert('has_participated_in_afd() returning false'); }
return false;
};
if (has_participated_in_afd())
{
afdlogexec();
}
else
{
return -5;
}
}
else
{
// not autolog
if (afdlogdebug) { alert('Not autolog. Adding portlet link'); }
mw.util.addPortletLink('p-cactions', 'javascript:afdlogexec();', 'Log AfD', 'ca-logafdlink', 'Log this discussion to your AfD log.');
if (afdlogdebug) { alert('Not autolog. Portlet link added'); }
}
};
var afdlogexec = function()
{
if (afdlogdebug) { alert('Start afdlogexec()'); }
// 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 (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') + '.');
if (afdlogdebug) { alert('afdlogexec() Starting AJAX.'); }
// save it
var edit_page = new LaL_SimplePageEditor();
var afdpagename = wgPageName.replace(/_/g, ' ');
var afdpagenamenice = afdpagename.substr(32); // remove "Wikipedia:Articles for deletion/" (32 chars long)
edit_page.prepend(logpage, "\n*[[" + afdpagename + '|' + afdpagenamenice + ']] <small>— \~\~\~\~\~</small>', 'Logging participation in AfD: [[' + afdpagename + "]]");
// done
};
function LaL_SimplePageEditor()
{
var that = this;
// wrappers to append, prepend or overwrite a page
this.append = function(pagename, pagetext, editsummary)
{
this.edit_page_start('appendtext', pagename, pagetext, editsummary);
};
this.prepend = function(pagename, pagetext, editsummary)
{
this.edit_page_start('prependtext', pagename, pagetext, editsummary);
};
this.overwrite = function(pagename, pagetext, editsummary)
{
this.edit_page_start('text', pagename, pagetext, editsummary);
};
// start editing the page (get token)
this.edit_page_start = function(edittype, pagename, pagetext, editsummary)
{
this.edittype = edittype;
this.pagename = pagename;
this.pagetext = pagetext;
this.editsummary = editsummary;
//this.callback = callback;
jsMsgAppend('Fetching edit token...');
// get the edit token
var url = wgServer + '/w/api.php?format=json&action=query&prop=info&intoken=edit&indexpageids=1&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'];
if (this.token)
{
jsMsgAppend('Success.');
// edit the page this.edittype encodeURIComponent
jsMsgAppend('Writing to log...');
var postdata = {
'action' : 'edit',
'format' : 'json',
'title' : this.pagename,
'summary' : this.editsummary,
'token' : this.token,
'minor' : '1',
'watch' : '1',
};
postdata[this.edittype] = this.pagetext;
$.ajax({
'url' : wgServer + '/w/api.php',
'type' : 'POST',
'data' : postdata,
'dataType' : 'json',
'success' : function(data, textstatus)
{
jsMsgAppend(data['edit']['result'] + '. <b>Done.</b>');
},
'complete' : function(){/*alert('COMPLETE');*/},
'error' : function(data, textstatus)
{
jsMsgAppend(data['edit']['result'] + '. <b>Done.</b>');
},
});
// function(d, t){that.callback(d, t);}
// this.callback();
}
else
{
jsMsgAppend('Error, aborting.');
}
};
}
// 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);
}
$(afdlog);