MediaWiki:Common.js/SigCheck.js

/* Any JavaScript here will be loaded for all users on every page load. */

if (!RegExp.escape) { RegExp.escape = function(str) { 'use strict'; return (str + '').replace(/[\.\?\*\+\[\]\(\)\{\}\\\-\|\^\$]/g, '\\$&'); }; }

if (window.SignatureCheckJS || window.SignatureCheckJS === void 0) { (function(window, $, mw, config) {	"use strict";	var haveForum = false,	   sectionEdit,	    sectionNum;	if (({edit:1, submit:1})[mw.config.get('wgAction')] !== 1) {		return;	}	var namespace = mw.config.get('wgNamespaceNumber');	if ((namespace & 1) !== 1) {		if (namespace !== mw.config.get('wgNamespaceIds').forum) {			return;		}		haveForum = true;	}	if (namespace >= 200 || namespace < 0) {		return;	}	if ((/(?:^\?|&)undo=/).test(window.location.search)) {		return;	}	sectionEdit = (/(?:^\?|&)section=([^&]+)(?:&|$)/).exec(window.location.search);	sectionNum = sectionEdit && +sectionEdit[1];	if (sectionEdit) {		if (sectionNum !== sectionNum) {			sectionNum = ((/^(?:\d|new$)/).test(sectionEdit[1]) ? 'new' : 0);		}	}	var log = (window.console && function { var args = Array.prototype.slice.call(arguments); args.unshift('SIGNATURECHECK:'); return window.console.log.apply(window.console, args); }) || $.noop;	config = $.extend({ preamble: 'There are a few potential problems with your edit:\n\n', epilogue: '\nAre you sure you want to post anyway?\n, noForumheader: '!! There is no forum header on this forum page. You should not create forum pages without the header since they will not actually show up in the forum list.\n', noSignature: 'You forgot your signature. Use 02:56, July 14, 2013 (UTC) to sign.\n', // Other stuff forumheader: 'Forumheader', checkSignature: true }, config);	if (typeof(config.forumheader) !== 'string' && config.forumheader) {		config.forumheader = false;		log('Forumheader is not a string');	}	function escapeWikiTitleForRegex(str) {		str += ;		var u = str.charAt(0).toUpperCase, l = u.toLowerCase;		if (u !== l) {			u = '[' + u + l + ']';			str = str.substr(1);		} else {			u = ;		}		return u + RegExp.escape(str);	}	// We need to wait for the onDOMReady before proceeding now (Monobook)	// Or for the WikiaEditor to fully initialise (Oasis)	var start;	if (({wikia:1, oasis:1})[mw.config.get('skin')] === 1) {		start = function(f) {			function callback {				window.GlobalTriggers.unbind('WikiaEditorReady', callback);				var editor = window.WikiaEditor && window.WikiaEditor.getInstance('wpTextbox1')				, getState				  ;				if (editor && editor.ck) {					getState = function {						var $VE = null, text;						if (editor.ck.document) { // Visual Editor IFRAME (iframe.contentDocument) $VE = $(editor.ck.document.$.body);

if ($VE.find('pre').length) { text = $VE.clone; text.find('pre').remove; text = text.text; } else { text = $VE.text; }						} else if (editor.ck.textarea) { text = $(editor.ck.textarea.$).val; }

if (text === void 0) { log('Failsafe tripped. WikiaEditor is not behaving correctly? This may not work properly.'); text = $('#wpTextbox1').val; }						return { $VE: $VE, text: text }; };				} else { log('Cannot connect to WikiaEditor. Will try standard MediaWiki instead.'); getState = function { return { $VE: null, text: $('#wpTextbox1').val }; }; }				try { f(getState); } catch(e) { log('Internal Crash:', e, e.stack); }			}			window.GlobalTriggers.bind('WikiaEditorReady', callback); };	} else { start = function(f) { $(function($) {				try {					return f(function { return { $VE: null, text: $('#wpTextbox1').val }; });				} catch(e) {					log('Internal Crash:', e, e.stack);				}			}); };	}	start(function(getState) {		var $form = $('#wpSave').closest('form');		$form.on('submit.CheckSignature', function(evt) { var problems = config.preamble , haveProblems = false ;

var $VE = getState, text = $VE.text; $VE = $VE.$VE; if (!$VE) { text = text.replace(/(?:.|\n)*?<\/nowiki\s*>|/g, ''); text = text.replace(/^ .*$/gm, ''); // m is multiline mode for ^/$ }			if (haveForum && (!sectionEdit || sectionNum === 0) && typeof(config.forumheader) === 'string' && config.forumheader) { var headerMissing = false; if (!$VE) { headerMissing = !(new RegExp( '\\{\\{\\s*' + escapeWikiTitleForRegex(config.forumheader) + '\\s*\\|(?:.|\\n)*?\\}\\}' ).test(text)); } else { var scannerRegex = new RegExp('^' + escapeWikiTitleForRegex(config.forumheader) + '$'); headerMissing = $VE.find('.placeholder.placeholder-double-brackets').filter(function {

return scannerRegex.test(							JSON.parse( window.decodeURIComponent(									this.getAttribute('data-rte-meta')								) ).title						); }).length === 0;				}				if (headerMissing) {					problems += config.noForumheader;					haveProblems = true;				}			}			if (config.checkSignature && !$form.find('#wpMinoredit').prop('checked') && text.indexOf('') === -1) {				problems += config.noSignature;				haveProblems = true;			}			if (haveProblems) {				problems += config.epilogue;				if (!confirm(problems)) {					evt.preventDefault;					evt.stopImmediatePropagation;				}			}		}); var events = $._data($form[0], 'events').submit; events.unshift(events.pop); events = null; }); })(window, jQuery, mediaWiki, window.SignatureCheckJS); } //