MediaWiki:Gadget-twinklewarn.js

//

(function($){

//* **************************************** *** twinklewarn.js: Warn module **************************************** * Mode of invocation:    Tab ("Warn") * Active on:             User talk pages * Config directives in:  TwinkleConfig */

Twinkle.warn = function twinklewarn { if( mw.config.get('wgNamespaceNumber') === 3 ) { twAddPortletLink( Twinkle.warn.callback, "Warn", "tw-warn", "Warn/notify user" ); }

// modify URL of talk page on rollback success pages if( mw.config.get('wgAction') === 'rollback' ) { var $vandalTalkLink = $("#mw-rollback-success").find(".mw-usertoollinks a").first; $vandalTalkLink.css("font-weight", "bold"); $vandalTalkLink.wrapInner($(" ").attr("title", "If appropriate, you can use Twinkle to warn the user about their edits to this page."));

var extraParam = "vanarticle=" + mw.util.rawurlencode(mw.config.get("wgPageName").replace(/_/g, " ")); var href = $vandalTalkLink.attr("href"); if (href.indexOf("?") === -1) { $vandalTalkLink.attr("href", href + "?" + extraParam); } else { $vandalTalkLink.attr("href", href + "&" + extraParam); }	} };

Twinkle.warn.callback = function twinklewarnCallback { if ( !twinkleUserAuthorized ) { alert("Your account is too new to use Twinkle."); return; }	if( mw.config.get('wgTitle').split( '/' )[0] === mw.config.get('wgUserName') &&			!confirm( 'You are about to warn yourself! Are you sure you want to do that?' ) ) { return; }	var Window = new Morebits.simpleWindow( 600, 440 ); Window.setTitle( "Warn/notify user" ); Window.setScriptName( "Twinkle" ); Window.addFooterLink( "User talk page warnings", "Template:User_talk_page_warnings#Warnings_and_notices" ); Window.addFooterLink( "Twinkle help", "M:TW/DOC#warn" );

var form = new Morebits.quickForm( Twinkle.warn.callback.evaluate ); var main_select = form.append( {			type:'field',			label:'Choose type of warning/notice to issue',			tooltip:'First choose a main warning group, then the specific warning to issue.'		} );

var main_group = main_select.append( {			type:'select',			name:'main_group',			event:Twinkle.warn.callback.change_category		} );

var defaultGroup = parseInt(Twinkle.getPref('defaultWarningGroup'), 10); main_group.append( { type:'option', label:'General note (1)', value:'level1', selected: ( defaultGroup === 1 || defaultGroup < 1 || ( Morebits.userIsInGroup( 'sysop' ) ? defaultGroup > 8 : defaultGroup > 7 ) ) } ); main_group.append( { type:'option', label:'Caution (2)', value:'level2', selected: ( defaultGroup === 2 ) } ); main_group.append( { type:'option', label:'Warning (3)', value:'level3', selected: ( defaultGroup === 3 ) } ); main_group.append( { type:'option', label:'Final warning (4)', value:'level4', selected: ( defaultGroup === 4 ) } ); main_group.append( { type:'option', label:'Only warning (4im)', value:'level4im', selected: ( defaultGroup === 5 ) } ); main_group.append( { type:'option', label:'Single issue notices', value:'singlenotice', selected: ( defaultGroup === 6 ) } ); main_group.append( { type:'option', label:'Single issue warnings', value:'singlewarn', selected: ( defaultGroup === 7 ) } ); if( Morebits.userIsInGroup( 'sysop' ) ) { main_group.append( { type:'option', label:'Blocking', value:'block', selected: ( defaultGroup === 8 ) } ); }

main_select.append( { type:'select', name:'sub_group', event:Twinkle.warn.callback.change_subcategory } ); //Will be empty to begin with.

form.append( {			type:'input',			name:'article',			label:'Linked article',			value:( Morebits.queryString.exists( 'vanarticle' ) ? Morebits.queryString.get( 'vanarticle' ) : '' ),			tooltip:'An article can be linked within the notice, perhaps because it was a revert to said article that dispatched this notice. Leave empty for no article to be linked.'		} );

var more = form.append( { type: 'field', name: 'reasonGroup', label: 'Warning information' } ); more.append( { type:'textarea', label:'Optional message:', name:'reason', tooltip:'Perhaps a reason, or that a more detailed notice must be appended' } );

var previewlink = document.createElement( 'a' ); $(previewlink).click(function{		Twinkle.warn.callbacks.preview(result); // |result| is defined below	}); previewlink.style.cursor = "pointer"; previewlink.textContent = 'Preview'; more.append( { type: 'div', id: 'warningpreview', label: [ previewlink ] } ); more.append( { type: 'div', id: 'twinklewarn-previewbox', style: 'display: none' } );

more.append( { type:'submit', label:'Submit' } );

var result = form.render; Window.setContent( result ); Window.display; result.main_group.root = result; result.previewer = new Morebits.wiki.preview($(result).find('div#twinklewarn-previewbox').last[0]);

// We must init the first choice (General Note); var evt = document.createEvent( "Event" ); evt.initEvent( 'change', true, true ); result.main_group.dispatchEvent( evt ); };

// This is all the messages that might be dispatched by the code // Each of the individual templates require the following information: //  label (required): A short description displayed in the dialog //  summary (required): The edit summary used. If an article name is entered, the summary is postfixed with "on article", and it is always postfixed with ". $summaryAd" //  suppressArticleInSummary (optional): Set to true to suppress showing the article name in the edit summary. Useful if the warning relates to attack pages, or some such. Twinkle.warn.messages = { level1: { "uw-vandalism1": { label:"Vandalism", summary:"General note: Vandalism" },		"uw-test1": { label:"Editing tests", summary:"General note: Editing tests		}		"uw-create1": {			label:"Creating inappropriate pages",			summary:"General note: Creating inappropriate pages"		}		"uw-advert1": {				label: "Using Meta for advertising or promotion",				summary: "General note: Using Meta for advertising or promotion"		},	},	level2: {		"uw-vandalism2": { 			label:"Vandalism", 			summary:"Caution: Vandalism" 		},		"uw-test2": { 			label:"Editing tests", 			summary:"Caution: Editing tests" 		},		"uw-create2": { 			label:"Creating inappropriate pages", 			summary:"Caution: Creating inappropriate pages" 		},		"uw-advert2": {				label: "Using Meta for advertising or promotion",				summary: "Caution: Using Meta for advertising or promotion"		},	},	level3: {		"uw-vandalism3": { 			label:"Vandalism", 			summary:"Warning: Vandalism" 		},		"uw-test3": { 			label:"Editing tests", 			summary:"Warning: Editing tests" 		},		"uw-create3": { label:"Creating inappropriate pages", summary:"Warning: Creating inappropriate pages" },		"uw-advert3": { label:"Using Meta for advertising or promotion", summary:"Warning: Using Meta for advertising or promotion" },	},	level4: { "uw-vandalism4": { label:"Vandalism", summary:"Final warning: Vandalism" },		"uw-test4": { label:"Editing tests", summary:"Final warning: Editing tests" },		"uw-create4": { label:"Creating inappropriate pages", summary:"Final warning: Creating inappropriate pages" },		"uw-advert4": { label:"Using Meta for advertising or promotion", summary:"Final warning: Using Meta for advertising or promotion" },	},	level4im: { "uw-vandalism4im": { label:"Vandalism", summary:"Only warning: Vandalism" },		"uw-create4im": { label:"Creating inappropriate pages", summary:"Only warning: Creating inappropriate pages" },	},	singlenotice: { "uw-invalidwikireq": { label:"Invalid wiki request", summary:"Notice: Invalid wiki request" },		"uw-dupewikireq": { label:"Duplicate wiki requests", summary:"Notice:Duplicate wiki requests" },	},	singlewarn: { "uw-npa": { label:"Making personal attacks", summary:"Warning: Personal attack directed at another user" },		"uw-harass": { label:"Harassment of other users", summary:"Warning:Harassment of other users" },		"uw-sock": { label:"Sockpuppetry", summary:"Warning: Sockpuppetry" },		"uw-username": { label:"Username policy violation", summary:"Warning:Username policy violation" },	},	block: { "uw-block1": { label: "Block level 1", summary: "You have been temporarily blocked", reasonParam: true },		"uw-block2": { label: "Block level 2", summary: "You have been blocked", reasonParam: true },		"uw-block3": { label: "Block level 3", summary: "You have been indefinitely blocked", reasonParam: true },		"UsernameBlocked": { label: "Username block", summary: "You have been blocked for violation of the username policy", reasonParam: true },		"UsernameHardBlocked": { label: "Username hard block", summary: "You have been blocked for a blatant violation of the username policy", reasonParam: true },       "Blocked proxy": { label: "Blocked proxy", summary: "You have been blocked because this IP is an open proxy" },       "Uw-spamblock": { label: "Spam block", summary: "You have been blocked for advertising or promotion" },       "Talkpage-revoked": { label: "Talk-page access removed", summary: "Your ability to change this talk page has been removed" }	} };

Twinkle.warn.prev_block_timer = null; Twinkle.warn.prev_block_reason = null; Twinkle.warn.prev_article = null; Twinkle.warn.prev_reason = null;

Twinkle.warn.callback.change_category = function twinklewarnCallbackChangeCategory(e) { var value = e.target.value; var sub_group = e.target.root.sub_group; sub_group.main_group = value; var old_subvalue = sub_group.value; var old_subvalue_re; if( old_subvalue ) { old_subvalue = old_subvalue.replace(/\d*(im)?$/, '' ); old_subvalue_re = new RegExp( $.escapeRE( old_subvalue ) + "(\\d*(?:im)?)$" ); }

while( sub_group.hasChildNodes ){ sub_group.removeChild( sub_group.firstChild ); }

// worker function to create the combo box entries var createEntries = function( contents, container, wrapInOptgroup ) { // due to an apparent iOS bug, we have to add an option-group to prevent truncation of text // (search WT:TW archives for "Problem selecting warnings on an iPhone") if ( wrapInOptgroup && $.client.profile.platform === "iphone" ) { var wrapperOptgroup = new Morebits.quickForm.element( {				type: 'optgroup',				label: 'Available templates'			} ); wrapperOptgroup = wrapperOptgroup.render; container.appendChild( wrapperOptgroup ); container = wrapperOptgroup; }

$.each( contents, function( itemKey, itemProperties ) {			var key = (typeof itemKey === "string") ? itemKey : itemProperties.value;

var selected = false; if( old_subvalue && old_subvalue_re.test( key ) ) { selected = true; }

var elem = new Morebits.quickForm.element( {				type: 'option',				label: ": " + itemProperties.label,				value: key,				selected: selected			} ); var elemRendered = container.appendChild( elem.render ); $(elemRendered).data("messageData", itemProperties); } );	};

if( value === "singlenotice" || value === "singlewarn" || value === "block" ) { // no categories, just create the options right away createEntries( Twinkle.warn.messages[ value ], sub_group, true ); } else if( value === "custom" ) { createEntries( Twinkle.getPref("customWarningList"), sub_group, true ); } else { // create the option-groups $.each( Twinkle.warn.messages[ value ], function( groupLabel, groupContents ) {			var optgroup = new Morebits.quickForm.element( { type: 'optgroup', label: groupLabel } );			optgroup = optgroup.render;			sub_group.appendChild( optgroup );			// create the options			createEntries( groupContents, optgroup, false );		} ); }

if( value === 'block' ) { // create the block-related fields var more = new Morebits.quickForm.element( { type: 'div', id: 'block_fields' } ); more.append( {			type: 'input',			name: 'block_timer',			label: 'Period of blocking: ',			tooltip: 'The period the blocking is due for, for example 24 hours, 2 weeks, indefinite etc...'		} ); more.append( {			type: 'input',			name: 'block_reason',			label: '"You have been blocked for ..." ',			tooltip: 'An optional reason, to replace the default generic reason. Only available for the generic block templates.'		} ); e.target.root.insertBefore( more.render, e.target.root.lastChild );

// restore saved values of fields if(Twinkle.warn.prev_block_timer !== null) { e.target.root.block_timer.value = Twinkle.warn.prev_block_timer; Twinkle.warn.prev_block_timer = null; }		if(Twinkle.warn.prev_block_reason !== null) { e.target.root.block_reason.value = Twinkle.warn.prev_block_reason; Twinkle.warn.prev_block_reason = null; }		if(Twinkle.warn.prev_article === null) { Twinkle.warn.prev_article = e.target.root.article.value; }		e.target.root.article.disabled = false;

$(e.target.root.reason).parent.hide; e.target.root.previewer.closePreview; } else if( e.target.root.block_timer ) { // hide the block-related fields if(!e.target.root.block_timer.disabled && Twinkle.warn.prev_block_timer === null) { Twinkle.warn.prev_block_timer = e.target.root.block_timer.value; }		if(!e.target.root.block_reason.disabled && Twinkle.warn.prev_block_reason === null) { Twinkle.warn.prev_block_reason = e.target.root.block_reason.value; }

// hack to fix something really weird - removed elements seem to somehow keep an association with the form e.target.root.block_reason = null;

$(e.target.root).find("#block_fields").remove;

if(e.target.root.article.disabled && Twinkle.warn.prev_article !== null) { e.target.root.article.value = Twinkle.warn.prev_article; Twinkle.warn.prev_article = null; }		e.target.root.article.disabled = false;

$(e.target.root.reason).parent.show; e.target.root.previewer.closePreview; }

// clear overridden label on article textbox Morebits.quickForm.setElementTooltipVisibility(e.target.root.article, true); Morebits.quickForm.resetElementLabel(e.target.root.article);

// hide the big red notice $("#tw-warn-red-notice").remove; };

Twinkle.warn.callback.change_subcategory = function twinklewarnCallbackChangeSubcategory(e) { var main_group = e.target.form.main_group.value; var value = e.target.form.sub_group.value;

if( main_group === 'singlenotice' || main_group === 'singlewarn' ) { if( value === 'uw-bite' || value === 'uw-username' || value === 'uw-socksuspect' ) { if(Twinkle.warn.prev_article === null) { Twinkle.warn.prev_article = e.target.form.article.value; }			e.target.form.article.notArticle = true; e.target.form.article.value = ''; } else if( e.target.form.article.notArticle ) { if(Twinkle.warn.prev_article !== null) { e.target.form.article.value = Twinkle.warn.prev_article; Twinkle.warn.prev_article = null; }			e.target.form.article.notArticle = false; }	} else if( main_group === 'block' ) { if( Twinkle.warn.messages.block[value].indefinite ) { if(Twinkle.warn.prev_block_timer === null) { Twinkle.warn.prev_block_timer = e.target.form.block_timer.value; }			e.target.form.block_timer.disabled = true; e.target.form.block_timer.value = 'indefinite'; } else if( e.target.form.block_timer.disabled ) { if(Twinkle.warn.prev_block_timer !== null) { e.target.form.block_timer.value = Twinkle.warn.prev_block_timer; Twinkle.warn.prev_block_timer = null; }			e.target.form.block_timer.disabled = false; }

if( Twinkle.warn.messages.block[value].pageParam ) { if(Twinkle.warn.prev_article !== null) { e.target.form.article.value = Twinkle.warn.prev_article; Twinkle.warn.prev_article = null; }			e.target.form.article.disabled = false; } else if( !e.target.form.article.disabled ) { if(Twinkle.warn.prev_article === null) { Twinkle.warn.prev_article = e.target.form.article.value; }			e.target.form.article.disabled = true; e.target.form.article.value = ''; }

if( Twinkle.warn.messages.block[value].reasonParam ) { if(Twinkle.warn.prev_block_reason !== null) { e.target.form.block_reason.value = Twinkle.warn.prev_block_reason; Twinkle.warn.prev_block_reason = null; }			e.target.form.block_reason.disabled = false; } else if( !e.target.form.block_reason.disabled ) { if(Twinkle.warn.prev_block_reason === null) { Twinkle.warn.prev_block_reason = e.target.form.block_reason.value; }			e.target.form.block_reason.disabled = true; e.target.form.block_reason.value = ''; }	}

// change form labels according to the warning selected if (value === "uw-socksuspect") { Morebits.quickForm.setElementTooltipVisibility(e.target.form.article, false); Morebits.quickForm.overrideElementLabel(e.target.form.article, "Username of sock master, if known (without User:) "); } else if (value === "uw-username") { Morebits.quickForm.setElementTooltipVisibility(e.target.form.article, false); Morebits.quickForm.overrideElementLabel(e.target.form.article, "Username violates policy because... "); } else if (value === "uw-bite") { Morebits.quickForm.setElementTooltipVisibility(e.target.form.article, false); Morebits.quickForm.overrideElementLabel(e.target.form.article, "Username of 'bitten' user (without User:) "); } else { Morebits.quickForm.setElementTooltipVisibility(e.target.form.article, true); Morebits.quickForm.resetElementLabel(e.target.form.article); }

// add big red notice, warning users about how to use appropriately $("#tw-warn-red-notice").remove;

var $redWarning; if (value === "uw-username") { $redWarning = $(" should not be used for blatant username policy violations. " +			"Blatant violations should be reported directly to UAA (via Twinkle's ARV tab). " +			" should only be used in edge cases in order to engage in discussion with the user. "); $redWarning.insertAfter(Morebits.quickForm.getElementLabelObject(e.target.form.reasonGroup)); } else if (value === "uw-coi-username") { $redWarning = $(" should not be used for blatant username policy violations. " +			"Blatant violations should be reported directly to UAA (via Twinkle's ARV tab). " +			" should only be used in edge cases in order to engage in discussion with the user. "); $redWarning.insertAfter(Morebits.quickForm.getElementLabelObject(e.target.form.reasonGroup)); } };

Twinkle.warn.callbacks = { getWarningWikitext: function(templateName, article, reason, isCustom) { var text = "';

if (reason && isCustom) { // we assume that custom warnings lack a parameter text += " " + reason + ""; }

return text; },	getBlockNoticeWikitext: function(templateName, article, blockTime, blockReason, isIndefTemplate) { var text = "^\s*$"; return text; },	preview: function(form) { var templatename = form.sub_group.value; var linkedarticle = form.article.value; var templatetext;

if (templatename in Twinkle.warn.messages.block) { templatetext = Twinkle.warn.callbacks.getBlockNoticeWikitext(templatename, linkedarticle, form.block_timer.value,				form.block_reason.value, Twinkle.warn.messages.block[templatename].indefinite); } else { templatetext = Twinkle.warn.callbacks.getWarningWikitext(templatename, linkedarticle, 				form.reason.value, form.main_group.value === 'custom'); }

form.previewer.beginRender(templatetext); },	main: function( pageobj ) { var text = pageobj.getPageText; var params = pageobj.getCallbackParameters; var messageData = params.messageData;

var history_re = /.*?(\d{1,2}:\d{1,2}, \d{1,2} \w+ \d{4}) \(UTC\)/g; var history = {}; var latest = { date: new Date( 0 ), type: '' }; var current;

while( ( current = history_re.exec( text ) ) ) { var current_date = new Date( current[2] + ' UTC' ); if( !( current[1] in history ) || history[ current[1] ] < current_date ) { history[ current[1] ] = current_date; }			if( current_date > latest.date ) { latest.date = current_date; latest.type = current[1]; }		}

var date = new Date;

if( params.sub_group in history ) { var temp_time = new Date( history[ params.sub_group ] ); temp_time.setUTCHours( temp_time.getUTCHours + 24 );

if( temp_time > date ) { if( !confirm( "An identical " + params.sub_group + " has been issued in the last 24 hours. \nWould you still like to add this warning/notice?" ) ) {					pageobj.statelem.info( 'aborted per user request' ); return; }			}		}

latest.date.setUTCMinutes( latest.date.getUTCMinutes + 1 ); // after long debate, one minute is max

if( latest.date > date ) { if( !confirm( "A " + latest.type + " has been issued in the last minute. \nWould you still like to add this warning/notice?" ) ) {				pageobj.statelem.info( 'aborted per user request' ); return; }		}

var dateHeaderRegex = new RegExp( "^==+\\s*(?:" + date.getUTCMonthName + '|' + date.getUTCMonthNameAbbrev + ")\\s+" + date.getUTCFullYear + "\\s*==+", 'mg' ); var dateHeaderRegexLast, dateHeaderRegexResult; while ((dateHeaderRegexLast = dateHeaderRegex.exec( text )) !== null) { dateHeaderRegexResult = dateHeaderRegexLast; }		// If dateHeaderRegexResult is null then lastHeaderIndex is never checked. If it is not null but // \n== is not found, then the date header must be at the very start of the page. lastIndexOf // returns -1 in this case, so lastHeaderIndex gets set to 0 as desired. var lastHeaderIndex = text.lastIndexOf( "\n==" ) + 1;

if( text.length > 0 ) { text += "\n\n"; }

if( params.main_group === 'block' ) { if( Twinkle.getPref('blankTalkpageOnIndefBlock') && params.sub_group !== 'uw-lblock' && ( messageData.indefinite || (/indef|\*|max/).exec( params.block_timer ) ) ) { Morebits.status.info( 'Info', 'Blanking talk page per preferences and creating a new level 2 heading for the date' ); text = "== " + date.getUTCMonthName + " " + date.getUTCFullYear + " ==\n"; } else if( !dateHeaderRegexResult || dateHeaderRegexResult.index !== lastHeaderIndex ) { Morebits.status.info( 'Info', 'Will create a new level 2 heading for the date, as none was found for this month' ); text += "== " + date.getUTCMonthName + " " + date.getUTCFullYear + " ==\n"; }

text += Twinkle.warn.callbacks.getBlockNoticeWikitext(params.sub_group, params.article, params.block_timer, params.reason, messageData.indefinite); } else { if( messageData.heading ) { text += "== " + messageData.heading + " ==\n"; } else if( !dateHeaderRegexResult || dateHeaderRegexResult.index !== lastHeaderIndex ) { Morebits.status.info( 'Info', 'Will create a new level 2 heading for the date, as none was found for this month' ); text += "== " + date.getUTCMonthName + " " + date.getUTCFullYear + " ==\n"; }			text += Twinkle.warn.callbacks.getWarningWikitext(params.sub_group, params.article, 				params.reason, params.main_group === 'custom') + " ~"; }

if ( Twinkle.getPref('showSharedIPNotice') && Morebits.isIPAddress( mw.config.get('wgTitle') ) ) { Morebits.status.info( 'Info', 'Adding a shared IP notice' ); text += "\n"; }

// build the edit summary var summary; if( params.main_group === 'custom' ) { switch( params.sub_group.substr( -1 ) ) { case "1": summary = "General note"; break; case "2": summary = "Caution"; break; case "3": summary = "Warning"; break; case "4": summary = "Final warning"; break; case "m": if( params.sub_group.substr( -3 ) === "4im" ) { summary = "Only warning"; break; }					summary = "Notice"; break; default: summary = "Notice"; break; }			summary += ": " + Morebits.string.toUpperCaseFirstChar(messageData.label); } else { summary = messageData.summary; if ( messageData.suppressArticleInSummary !== true && params.article ) { if ( params.sub_group === "uw-socksuspect" ) { // this template requires a username summary += " of User:" + params.article + ""; } else { summary += " on " + params.article + ""; }			}		}		summary += "." + Twinkle.getPref("summaryAd");

pageobj.setPageText( text ); pageobj.setEditSummary( summary ); pageobj.setWatchlist( Twinkle.getPref('watchWarnings') ); pageobj.save; } };

Twinkle.warn.callback.evaluate = function twinklewarnCallbackEvaluate(e) {

// First, check to make sure a reason was filled in if uw-username was selected

if(e.target.sub_group.value === 'uw-username' && e.target.article.value.trim === '') { alert("You must supply a reason for the template."); return; }

// Find the selected element so we can fetch the data structure var selectedEl = $(e.target.sub_group).find('option[value="' + $(e.target.sub_group).val + '"]');

// Then, grab all the values provided by the form var params = { reason: e.target.block_reason ? e.target.block_reason.value : e.target.reason.value, main_group: e.target.main_group.value, sub_group: e.target.sub_group.value, article: e.target.article.value, // .replace( /^(Image|Category):/i, ':$1:' ),  -- apparently no longer needed... block_timer: e.target.block_timer ? e.target.block_timer.value : null, messageData: selectedEl.data("messageData") };

Morebits.simpleWindow.setButtonsEnabled( false ); Morebits.status.init( e.target );

Morebits.wiki.actionCompleted.redirect = mw.config.get('wgPageName'); Morebits.wiki.actionCompleted.notice = "Warning complete, reloading talk page in a few seconds";

var Meta_page = new Morebits.wiki.page( mw.config.get('wgPageName'), 'User talk page modification' ); Meta_page.setCallbackParameters( params ); Meta_page.setFollowRedirect( true ); Meta_page.load( Twinkle.warn.callbacks.main ); }; })(jQuery);

//