User:Aeroid/CatComplete.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
// [[Category:User scripts|CatComplete]] <nowiki>
// Tests: https://jsfiddle.net/d1p748xh/
// build Category Tree or {{Taken on |1= |cat= |location=Hamburg |source= }} 

function CatComplete() {
	var messages = {
		'en': {
			'catcomplete': 'CatComplete',
			'button': 'Insert missing template generated from category name.',
			'getting-image-info': "Inserting template.",
		}
	};
	var lang = mw.config.get( 'wgUserLanguage' );
	
	var m = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
	var state = ["Saxony", "Baden-Württemberg", "Bavaria", "Berlin", "Brandenburg", "Bremen", "Hamburg", "Hesse", "Lower Saxony", "Mecklenburg-Vorpommern", "North Rhine-Westphalia", "Rhineland-Palatinate", "Saarland", "Saxony-Anhalt", "Schleswig-Holstein", "Thuringia", "Germany", "Europe"];

	function getTemplateFromCat(catname) {
	  var result = "";
	  var date = -1;
	  var year = -1;
	  var yearN = -1;
	  var yearNN = -1;
	  var month = -1;
	  var monthName = "";
	  var decade = -1;
	  var century = -1;
	  var centuryS = ""; //2nd, 21st, 15th ...
	  var place = "";

	  var regexmatch = catname.match(/\d\d\d\d-\d\d-\d\d/);
	  if (regexmatch) {
	    var d = new Date(regexmatch);
	    year = d.getFullYear();
	    month = d.getMonth() + 1;
	    monthName = m[d.getMonth()];
	    date += d.getDate() + 1;
	  } else {
	    var i = 0;
	    do {
	      var rx = new RegExp(m[i] + ' \\d\\d\\d\\d');
	      regexmatch = rx.exec(catname);
	      if (regexmatch) {
	        month = (i + 1);
	        monthName = m[i];
	      }
	      i++;
	    }
	    while (i < m.length);

	    year = catname.match(/\d\d\d\d/);
	  }

	  var i = 0;
	  do {
	    if (catname.search(state[i]) != -1)
	      place = state[i];
	    i++;
	  }
	  while (i < state.length);

	  if (place === "") place = "Hamburg";
	  if (year > -1) {
	    decade = Math.floor(year / 10);
	    yearN = year - (decade * 10);
	    century = Math.floor(year / 100) + 1;
	    yearNN = year - ((century-1) * 100);
	    centuryS = century + "th";
	    if (yearN == 1 && yearNN != 11) centuryS = century + "st";
	    else if (yearN == 2 && yearNN != 12) centuryS = century + "nd";
	    else if (yearN == 3 && yearNN != 13) centuryS = century + "rd";
	  }

	  if (/ photographs of Germany/.test(catname)) {
	    result = "{{Germanyphotoyear|" + decade + "|" + yearN + "}}";
	  } else if ((new RegExp(monthName + " " + year + " in Germany by state")).test(catname)) {
	    result = "{{Germanymonthyearbystate|" + year + "|" +  monthName + "}}";
	  } else if ((new RegExp(monthName + " " + year + " in the German Democratic Republic")).test(catname)) {
	    result = "{{GDR-monthyear|" + year + "|" +  monthName + "}}";
	  } else if ((new RegExp(monthName + " " + year + " in West Germany")).test(catname)) {
	    result = "{{West Germanymonthyear|" + year + "|" +  monthName + "}}";
	  } else if ((new RegExp(monthName + " " + year + " in Germany")).test(catname)) {
	    result = "{{monthbyyeargermany|" + decade + "|" + yearN + "|" + month + "}}";
	  } else if ((new RegExp("^" + place + " photographs taken on ")).test(catname)) {
	    result = "{{" + place + " photographs taken on navbox|" + year + "|" + month + "|" + date + "}}";
	  } else if ((new RegExp(" " + place + " photographs")).test(catname)) {
	    result = "{{" + place + "photomonthyear|" + year + "|" + monthName + "}}";
	  } else if ((new RegExp(" photographs of " + place + "")).test(catname)) {
	    result = "{{" + place + "Photographs|" + decade + "|" + yearN + "}}";
	  } else if ((new RegExp(" in " + place + " by month")).test(catname)) {
	    result = "{{" + place + "bymonthyear|" + decade + "|" + yearN + "}}";
	  } else if ((new RegExp(monthName + " " + year + " in " + place + "")).test(catname)) {
	    result = "{{" + place + "monthyear|" + year + "|" + monthName + "}}";
	  } else if ((new RegExp(year + " in " + place + "")).test(catname)) {
	    result = "{{" + place + "year|" + decade + "|" + yearN + "}}";
	  } else if ((new RegExp("0s maps of " + place)).test(catname)) {
	    result = "{{" + place + "MapsDecade|" + (century - 1) + "|" + ("00"+yearNN).slice(-2) + "|" + centuryS + "}}";
	  } else if ((new RegExp(" maps of " + place)).test(catname)) {
	    result = "{{Maps" + place + "|" + decade + "|" + yearN + "}}";
	  } else if (/^Germany photographs taken on /.test(catname)) {
	    result = "{{Germany photographs taken on navbox|" + year + "|" + month + "|" + date + "}}";
	  } else if (/ Germany photographs/.test(catname)) {
	    result = "{{Germanyphotomonthyear|" + year + "|" + monthName + "}}";
	  } else if (/^Photographs taken on /.test(catname)) {
	    result = "{{Photographs taken on navbox|" + year + "|" + month + "|" + date + "}}";
	  } else if (/ by month by city/.test(catname)) {
	    result = "{{metacat|month|city}}\n\n[[Category:"+ year +" by city]]\n[[Category:"+ year +" by month]]\n";
	  } else if (/ by city/.test(catname)) {
	    result = "{{Monthbycitymonth|" + decade + "|" + yearN + "|" + month + "}}";
	  } else if (/^\d\d\d\d-\d\d-\d\d/.test(catname)) {
	    result = "{{Date navbox|" + year + "|" + month + "|" + date + "}}";
	  }
	  
	  return result;
	}
	
	function getMsg( msg ) {
		if ( messages[ lang ] !== undefined && messages[ lang ][ msg ] !== undefined ) {
			return messages[ lang ][ msg ];
		} else if ( messages.en[ msg ] !== undefined ) {
			return messages.en[ msg ];
		} else {
			return '⟨' + msg + '⟩';
		}
	}	

	function main() {
		$( '#wpTextbox1' ).on( 'wikiEditor-toolbar-doneInitialSections', function () {
			var catCompleteButtonDetails = {
				type: 'button',
				icon: 'https://upload.wikimedia.org/wikipedia/commons/e/e2/Legendary.png',
				label: getMsg( 'catcomplete' ),
				action: {
					type: 'callback',
					execute: function() {
						var textSelectionOpts = {
							peri: getTemplateFromCat(mw.config.get( 'wgTitle' )),
							replace: true,
							selectPeri: false
						};
						$( '#wpTextbox1' ).textSelection( 'encapsulateSelection', textSelectionOpts );
					}
				}
			};
			var catCompleteButton = {
				section: 'main',
				group: 'insert',
				tools: { 'CatComplete': catCompleteButtonDetails }
			};
			$( "#wpTextbox1" ).wikiEditor( 'addToToolbar', catCompleteButton );

		} );
	}

	main();	
}

var isEditing = $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1,
	isFile = mw.config.get( 'wgCanonicalNamespace' ) === 'Category';

if ( isEditing && isFile ) {
	var dependencies = [
		'oojs-ui-core',
		'oojs-ui-windows'
	];
	mw.loader.using( dependencies, $.ready ).then( CatComplete );
}
// EOF </nowiki>