MediaWiki:Skin/Commons:Kugel.js

//importJS("MediaWiki:Skin/Commons:Kugel/simple.js"); /* Parameter: * - cmd: Such-String * - known_modifs: Liste der Bekannten Modifier * - modif_aliases: Modifier-Übersetzungs-Tabelle * - minus: Soll "-Suchwort" in einen Extra-Eintrag "-"? * Return: [Suchwort-Tree, Fehler] */ function analyzeSearchString(cmd, known_modifs, modif_aliases, minus) {	//Interne Escapings: // \0l…\0t Literal // \0o Oder // \0u Und // \0t Terminalzeichen für einige Escapings var errors = []; // Schritt 1: Escaping Sequences entfernen und Strip cmd = cmd.replace(/\u0000/g,""); cmd = cmd.replace(/^\s+/, ""); cmd = cmd.replace(/\s+$/, ""); // Schritt 1: Literale entfernen var literals = []; cmd = cmd.replace(/('[^']*'|"[^"]*")/g, function (x) { var lnum = literals.length; literals.push(x.substr(1,x.length-2)); return "\0l"+lnum+"\0t"; });	// Schritt 2: Leerzeichen nach Doppelpunkten entfernen	cmd = cmd.replace(/:\s+/g,":");	// Schritt 3: ODER-Blöcke zusammenfassen	cmd = cmd.replace(/\s+OR\s+/g, "\0o");	// Schritt 4: UND-Blöcke zusammenfassen (Leerzeichen == AND)	cmd = cmd.replace(/\s+AND\s+/g, " ");	cmd = cmd.replace(/\s+/g, "\0u");	// Schritt 5: Umwandeln in eine Baumstruktur	function raiseError(x)	{		errors.push(x);		if (window.console && window.console.log)			console.log(x);	}	// Debug-Funktionen	function Block(modifier,objects) { this.modifier = modifier; this.objects = objects };	Block.prototype.toString = function { return "("+this.modifier+":"+this.objects+")" };	function processLiterals(x)	{		return x.replace(/\u0000l[^\u0000]+\u0000t/g, function (x) { return literals[parseInt(x.substr(2,x.length-4))]; }); }	function processOrBlocks(x) {		// Modif Tuple: (Modifier, String) function getModifTuple(x) {			var idx = x.indexOf(":"); var a = x.substr(0,idx); var b = x.substr(idx+1); // Schlüsselwort "-" if(a == "" && minus && b.length>0 && b[0] == "-") {                           a = "-"; b = b.substr(1); }

// Schlüsselwort-Ersetzung if(modif_aliases[a] != null) {                           var alias = modif_aliases[a];

if(alias instanceof Function) {                               // komplizierte Ersetzung mittels Funktion var tuple = alias(a,b); a = tuple[0]; b = tuple[1]; }                           else // String-Ersetzung a = alias; }			return [a,b]; }		var obj_arr = x.split("\0o"); var modif = getModifTuple(obj_arr[0])[0]; for (var i=0; i<obj_arr.length; i++) {			var el = getModifTuple(obj_arr[i]); if( el[0] != modif ) raiseError('"'+modif+'" und "'+el[0]+'" können nicht untereinander mit OR verknüpft werden.'); obj_arr[i] = processLiterals(el[1]); }		return new Block(modif,obj_arr); }	function processAndBlocks(x) {		var arr = x.split("\0u"); for (var i=0; i<arr.length; i++) {			arr[i] = processOrBlocks(arr[i]); }		return arr; }	var tree = processAndBlocks(cmd); return [tree, errors]; }

//importJS("MediaWiki:Skin/Commons:Kugel/api.js"); var bs_DPL = { defaultSearch: function (qs) {

var redir_prefix = ""; var redir_suffix = ""; if( qs.redir ) {                      redir_prefix = ""; }

var dplstring = "";

seestring = seeitems.length ? " " + seeitems.join(", ") + " " : ""; seenotstring = seenotitems.length ? " " + seenotitems.join(", ") + " " : "";

jQuery.ajax({			url: "/api.php",			data: {				action: "parse",				format: "xml",				text: dplstring + seestring + seenotstring			},			dataType: "xml",			success: bs_DPL.display,			error: bs_DPL.ajaxError		}); },	display: function (xml) { var data = jQuery(xml).find("api parse text").text; jQuery("#bs_results")[0].innerHTML = data; jQuery("#bs_results").fadeIn; },	ajaxError: function (request,error) { var ediv = jQuery("#bs_errors"); ediv.text("Es sind Fehler aufgetreten: "+error); ediv.fadeIn('slow'); }

}

/*bs_DPL.defaultSearch({ search:"Kamel", page: 0, limit: 10 });*/

//importJS("MediaWiki:Skin/Commons:Kugel/dpl.js"); var bs_API = { qry: "",

defaultSearch: function (p) {		bs_API.qry = p.search;

if (p.limit == null) p.limit = 16; jQuery.ajax({			url: "/api.php",			async: false,			data: {				sroffset:p.limit * p.page,				action:"query",				list:"search",				srsearch:p.search,				srwhat:"text",				srnamespace:"6",				format:"xml",				srlimit: p.limit,				srprop:"",				srinfo:"" // eigentlich "totalhits|suggestion" aber funktioniert nicht			},			success: bs_API.prepareRender,                       error: bs_API.ajaxError		}); },	prepareRender: function (transport) {		var results = []; jQuery(transport).find("api query search p").each(function(num,obj) {results.push(jQuery(obj).attr("title")) }); var source = "";

if(bs_API.qry) {			var items = bs_API.qry.split(/\s+/); for (var i = 0; i < items.length; i++) {				items[i] = "Kategorie:Commons: " + items[i].substr(0,1).toUpperCase + items[i].substr(1) + ""; }			source += " " + items.join(", "); }

if (results.length == 0) source = "-- keine Ergebnisse --";

jQuery.ajax({			url: "/api.php",			data: {				action:"parse",				text:source,				format:"xml",				prop: "text"			},			success: bs_API.display,                       error: bs_API.ajaxError		}); },	display: function (transport) { var text = jQuery(transport).find("api parse text").text; var o = jQuery("#bs_results"); o.empty; o.append(jQuery(text)); o.fadeIn('slow'); },       ajaxError: function (request,error) { var ediv = jQuery("#bs_errors"); ediv.text("Es sind Fehler aufgetreten: "+error); ediv.fadeIn('slow'); } };

var form = null; jQuery(document).ready( function {	jQuery("#bs_form").empty;	form = jQuery(" ");	form.appendTo("#bs_form");	form.append("");	form.append(" ");	form.append('');	form.append('API ');	form.append('');	form.append('DPL &emsp;');	form.append("Tipps &amp; Tricks");	form.append('Tag-Redirects aufl&ouml;sen ');        jQuery('input[name=q]').focus;

});

var currentQuery = {}; var currentEngine = null; /* * Analysiert den Suchstring qs und wählt eine passende Suchmaschine aus. * Rückgabewert ist ein Object {engine:…, render:…, params:…, error:…} *  - engine: Pointer auf die Suchfunktion *  - params: Parameter-Objekt *        - page: Seitennummer, beginnend mit 0 *        - optional weitere Parameter *  - error: im Erfolgsfall null, sonst Fehlerstring *  - render: Pointer auf die Renderfunktion */ function parseQuery (qs) { var mode = jQuery(form).find("input:radio:checked").val; if (mode == "API") currentEngine = bs_API.defaultSearch; if (mode == "DPL") currentEngine = bs_DPL.defaultSearch;

currentQuery = { redir: !! (jQuery("#bs_redir:checked")[0]), search:qs, limit: 12, page: 0 };

currentEngine(currentQuery); }

/* transform control spans into "real" control links */ function createControls(name) { jQuery(name+" span").attr("class","pseudolink"); jQuery(name+" span").each(function (num,obj) {	   jQuery(obj).bind("click", function  { currentQuery.page = num - 1; currentEngine(currentQuery); });	});	jQuery(name+" span:first").each(function (num,obj) {	   jQuery(obj).unbind("click")	    jQuery(obj).bind("click", function  { currentQuery.page -= 1; if (currentQuery.page < 0) currentQuery.page = 0 currentEngine(currentQuery); });	});	jQuery(name+" span:last").each(function (num,obj) {	   jQuery(obj).unbind("click")	    jQuery(obj).bind("click", function  { currentQuery.page += 1; currentEngine(currentQuery); });	}); }

jQuery(document).ready( function {

jQuery("#bs_mode_api").bind("click",function { jQuery("#bs_redir_div").fadeOut }); jQuery("#bs_mode_dpl").bind("click",function { jQuery("#bs_redir_div").fadeIn });

form.bind('submit', function {	    try {		jQuery("#bs_logo, #bs_controls, #bs_controls2, #bs_results, #bs_errors").fadeOut('slow');		parseQuery(this.q.value);		jQuery("#bs_controls, #bs_controls2, #bs_results").fadeIn('slow');	    }	    catch(e)	    {		if (console && console.log)		    console.log(e)	    }	    finally	    {		return false;	    }	});

createControls("#bs_controls"); createControls("#bs_controls2");

//create an argument dict var argstring = location.search.substr(1).split("&"); argdict = {} for(var i=0; i<argstring.length; i++) {		var kv = argstring[i].split("="); argdict[kv[0]] = kv[1]; }

// run search directly? if(argdict["bs_q"] != null) //bugfix: +++ decodeURIComponent, damit Sönderzeichün richtig bei Kugel ankommen ... 		form.find("input:text").val(decodeURIComponent(argdict["bs_q"])); if(argdict["bs_mode"] == "DPL" ) form.find("#bs_mode_dpl")[0].checked = true; if(argdict["bs"] == "submit") form.submit });