Hoppa till innehållet

MediaWiki:Gadget-HittaDubbleradeMallargument.js

Från Wikipedia

OBS: Efter du har publicerat sidan kan du behöva tömma din webbläsares cache för att se ändringarna.

  • Firefox / Safari: Håll ned Skift och klicka på Uppdatera sidan eller tryck Ctrl-F5 eller Ctrl-R (⌘-R på Mac)
  • Google Chrome: Tryck Ctrl-Skift-R (⌘-Skift-R på Mac)
  • Edge Håll ned Ctrl och klicka på Uppdatera eller tryck Ctrl-F5.
  • Opera: Tryck Ctrl-F5.
$( function () {
	'use strict';
	function createDialog( content ) {
		// Creating and opening a simple dialog window.

		// Subclass Dialog class. Note that the OOjs inheritClass() method extends the parent constructor's prototype and static methods and properties to the child constructor.

		function MyDialog( config ) {
			MyDialog.super.call( this, config );
		}
		var myDialog;
		OO.inheritClass( MyDialog, OO.ui.Dialog );

		// Specify a title statically (or, alternatively, with data passed to the opening() method).
		MyDialog.static.name = 'gadgethittadubblerademallargumentdialog';
		MyDialog.static.title = 'Simple dialog';

		// Customize the initialize() function: This is where to add content to the dialog body and set up event handlers.
		MyDialog.prototype.initialize = function () {
			// Call the parent method
			MyDialog.super.prototype.initialize.call( this );
			// Create and append a layout and some content.
			this.content = new OO.ui.PanelLayout( { padded: true, expanded: false } );
			this.content.$element.append( content );
			this.$body.append( this.content.$element );
		};

		// Make the window.
		myDialog = new MyDialog();

		// Create and append a window manager, which will open and close the window.
		var windowManager = new OO.ui.WindowManager();
		$( 'body' ).append( windowManager.$element );

		// Add the window to the window manager using the addWindows() method.
		windowManager.addWindows( [ myDialog ] );

		// Open the window!
		windowManager.openWindow( myDialog );
	}
	var myContent = $( '#wpTextbox1' );
	if ( mw.config.get( 'wgNamespaceNumber' ) != -1 && myContent.length ) {
		mw.util.addPortletLink('p-cactions', '#', 'Hitta dubbletter', 'ca-fdup');
		$( '#ca-fdup' ).click( function( e ) {
			e.preventDefault();
			// Internal for and while loop variables
			var i = 0;
			var j = 0;
			var loopcount = 0;
			// Array used to hold the list of unnested templates
			var tlist = [];
			// Regular expression which matches a template arg
			var argexp = new RegExp("\\|[\\s]*([^\\s=\\|\\[\\]\\{\\}][^=\\|\\[\\]\\{\\}]*[^\\s=\\|\\[\\]\\{\\}]|[^\\s=\\|\\[\\]\\{\\}])[\\s]*=", "gm");
			// Copy the contents of the text window so we can modify it without problems
			var mytxt = myContent.val();
			var strlist;
			var unp;
			var p;
			// Remove PAGENAME, BASEPAGENAME, ... nested inside of triple braces
			mytxt = mytxt.replace(/\{\{\{[^\{\}]*\|[ ]*\{\{[A-Z]+\}\}\}\}\}/g, '');
			// Remove some triple braces
			loopcount = 0;
			while((mytxt.search(/\{\{\{[^\{\}]*\}\}\}/g) >= 0) && (loopcount < 5) ) {
				mytxt = mytxt.replace(/\{\{\{[^\{\}]*\}\}\}/g, '');
				loopcount++;
			}
			// Replace some bare braces with HTML equivalent
			mytxt = mytxt.replace(/([^\{])\{([^\{])/g, '$1&#123;$2');
			mytxt = mytxt.replace(/([^\}])\}([^\}])/g, '$1&#125;$2');
			// Remove newlines and tabs which confuse the regexp search
			mytxt = mytxt.replace(/[\s]/gm, ' ');
			// Compress whitespace
			mytxt = mytxt.replace(/[\s][\s]+/gm, ' ');
			// Remove some HTML comments
			mytxt = mytxt.replace(/<!--[^<>]*-->/gm, '');
			// Modify some = inside of file/image/wikilinks which cause false positives
			loopcount = 0;
			while((mytxt.search(/\[\[[^\[\]\{\}]*=/gi) >= 0) && (loopcount < 5) ) {
				mytxt = mytxt.replace(/(\[\[[^\[\]\{\}]*)=/gi, '$1&#61;');
				loopcount++;
			}
			// Split the text into chunks to reduce processing overhead
			strlist = mytxt.split(/(\{\{|\}\})/);
			// Now start unnesting the templates
			loopcount = 0;
			while( (strlist.length > 0) && (loopcount < 10) ) {
				for (i = strlist.length - 1; i >= 0; i--) {
					if((i+2) < strlist.length && strlist[i] == '{{' && strlist[i+2] == '}}') {
						tlist.push(strlist[i] + strlist[i+1] + strlist[i+2]);
						strlist.splice(i,3);
					} else if((i+3) < strlist.length && strlist[i] == '{{' && strlist[i+3] == '}}') {
						if( strlist[i+1].search(/[\{\}]/g) < 0 && strlist[i+2].search(/[\{\}]/g) < 0 ) {
							tlist.push(strlist[i] + strlist[i+1] + strlist[i+2] + strlist[i+3]);
							strlist.splice(i,4);
						}
					} else if( strlist[i].search(/^[\s]*$/g) >= 0 ) {
						strlist.splice(i,1);
					} else if((i+1) < strlist.length && strlist[i].search(/[\{\}]/g) < 0 && strlist[i+1].search(/[\{\}]/g) < 0) {
						strlist[i] = strlist[i] + strlist[i+1];
						strlist.splice(i+1,1);
					}
				}
				loopcount++;
			}
			// Now find duplicates in the list of unnested templates
			for(i=0; i < tlist.length; ++i) {
				// Add numbers for unnamed parameters
				unp=0;
				while((tlist[i].search(/(\{\{(?:[^{}\[\]]|\[\[[^\[\]]*\]\])*?\|)((?:[^{}\[\]=\|]|\[\[[^\[\]]*\]\])*(?:\||\}\}))/) >= 0)
				&& (unp < 25)) {
					unp++;
					tlist[i] = tlist[i].replace(/(\{\{(?:[^{}\[\]]|\[\[[^\[\]]*\]\])*?\|)((?:[^{}\[\]=\|]|\[\[[^\[\]]*\]\])*(?:\||\}\}))/, '$1' + unp + '=$2');
				}
				// Split the template into an array of | arg = ... strings
				p = tlist[i].match(argexp);
				if( p ) {
					for(j=0; j < p.length; ++j) {
						p[j] = p[j].replace(argexp, '$1');
					}
					p = p.sort();
					for(j=0; j < p.length - 1; ++j) {
						if( p[j] == p[j+1]) {
							createDialog( p[j] + ' i ' + tlist[i] );
						}
					}
				}
			}
		} );
	}
} );