User:L10nM4st3r/ReportUser/Beta.js

Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac);
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5;
  • Konqueror: simply click the Reload button, or press F5;
  • Opera users may need to completely clear their cache in Tools→Preferences.
//<nowiki>
/*
  To use, simply add "mw.loader.load('//en.wikibooks.org/w/index.php?title=User:L10nM4st3r/ReportUser.js&action=raw&ctype=text/javascript');" to you /common.js subpage.
  After doing that, go to RC, look for a user making really bad edits, and go to their userpage, talkpage, or contributions page. Press the "report" tab and give a reason to report them. Then they will automatically be reported to the admins.
  
  
  On TODO list:
	* Use custom UI, rather than being lazy and using a very limited prompt() command.
	* Allow using preset reasons, maybe. Would be quite difficult though.
	* Check if user is already blocked.
*/

// Will be used to make sure the user uses the newest version. Currently no idea how to implement this though.
const build=1;
const version="0.0.4";

/////////// Settings the user can change //////
var force_enabled_for_admins=false;// If true, even admins can use the script.
var can_report_self=false;// If true, the user can report themselves. Useful for testing the script.

function success(){
	if(same){
		same=false;
		mw.notify("User already reported",{type:'error'});
	}
	else mw.notify("Successfuly reported user!");
}
function failed(){
	if(same){
		same=false;
		mw.notify("User already reported",{type:'error'});
	}
	else mw.notify("Unable to report user. Perhaps you don't have permission to edit your local vandalism reporting page?",{type:'error'});
}
// Used in getting the correct error message. Do not modify deafult value.
var same=false;

$.when($.ready,mw.loader.using(['mediawiki.util','mediawiki.api'])).then( function(){

// Must have a user to accociate the page with
if (mw.config.get('wgRelevantUserName') && ( mw.config.get('wgRelevantUserName')!=mw.config.get('wgUserName') || can_report_self )

// Accepted wikis below ///////////////////////////////////////////////////////////
&& ['enwikibooks','metawiki','uncy_en'].includes(mw.config.get('wgDBname'))

// Sysops do not need this, disabled automatically
&& ( !mw.config.get('wgUserGroups').includes('sysop') || force_enabled_for_admins )

// User must be autoconfirmed to use this script in the first place.
&& mw.config.get('wgUserGroups').includes('autoconfirmed')){
	var node=mw.util.addPortletLink('p-cactions','','Report','utreportuser',"Report this user to the local admins.");
	$(node).on('click',function(e){
		var user=mw.config.get('wgRelevantUserName');
		var reason=prompt("Give a reason for reporting "+user+".\n\nExamples: spam, vandalism, blanking pages, LTA, long-term abuse\n\nLeave blank to cancel.");
		// There MUST be a reason filled out for the edit to take place
		if(reason) {
                var ad=" (using [[wikibooks:en:User:L10nM4st3r/ReportUser|ReportUser "+version+"]])";
			var summary="Reporting user [[User:"+user+"|"+user+"]] ([[User talk:"+user+"|talk]] · [[Special:Contribs/"+user+"|contribs]])";
			var tag='<!--USERREPORTED:/'+user+'/-->';
			var full_reason=reason+' '+tag;
			// Report user, depending on the wiki

			///////// en wikibooks /////////
			if(mw.config.get('wgDBname')=='enwikibooks')
				new mw.Api().edit('Wikibooks:Reading_room/Administrative_Assistance',function(revision){
					ad=" (using [[User:L10nM4st3r/ReportUser|ReportUser "+version+"]])";
					if (revision.content.includes('<!--USERREPORTED:/'+user+'/-->')){same=true;return{text:revision.content}}
					return {
						text: revision.content+"\n\n==Please block [[User:"+user+"|"+user+']] ([[User talk:'+user+'|talk]] · [[Special:Contribs/'+user+'|contribs]])==\nReason: '+full_reason+' ~~~~',
						summary: summary+ad,
					};
				}).then(function(){success()},function(){failed()});

			///////// meta /////////
			else if(mw.config.get('wgDBname')=='metawiki')
				new mw.Api().edit('Meta:Requests_for_help_from_a_sysop_or_bureaucrat',function(revision){
					if (revision.content.includes('<!--USERREPORTED:/'+user+'/-->')){same=true;return{text:revision.content}}
					return {
						text: revision.content+"\n\n{{subst:VandalReport|"+user+'|'+full_reason+'}}',
						summary:summary+ad,
					};
				}).then(function(){success()},function(){failed()});


			///////// uncyclopedia /////////
			else if(mw.config.get('wgDBname')=='uncy_en')
				new mw.Api().edit('Uncyclopedia:Ban_Patrol',function(revision){
					if (revision.content.includes('<!--USERREPORTED:/'+user+'/-->')){same=true;return{text:revision.content}}
					section="== Manual Ban Patrol (new stuff below this line) ==";
					message="\n{{user|"+user+"}} "+full_reason+" ~~~~";
					// Something went wrong, and the section was not found. Instead show at exact bottem with the other scripted reports.
					// Why we don't just report this with the other scripted reports is because it is easier for the admins this way. Plus, it might lead to those scripts getting improved.
					if(!revision.content.includes(section)) return {
						text: revision.content+message,summary:summary+ad+" - ERROR: edit section not found, unable to correctly format report (please [[wikibooks:en:User talk:L10nM4st3r|report this bug!]])"};
					return {
						text: revision.content.replace(section,section+message),
						summary: summary+ad,
					};
				}).then(function(){success()},function(){failed()});

		}
		else 
			// No reason given. Aborted the report
			mw.notify('You must give a reason to request to have this user blocked.',{type:'error'});
		e.preventDefault();
	});
	
	
	new mw.Api().get({
		action: "query",
		list: "users",
		ususers: user,
		usprop: "blockinfo",
		format: "json"
	}).then(function(response) {
			var userdata = response.query.users[0];
			if (userdata.blockid) {
				print('user '+user+' already blocked');
				node.css('display:none');
			}
		});

}});
//</nowiki>