All pastes #244933 Raw Edit

Anonymous

public text v1 · immutable
#244933 ·published 2006-11-10 12:02 UTC
rendered paste body
/*
 *
 * File: basicQuery.js
 *
 * Javascript for Verity K2 Enterprise Web Sample Templates - Basic Query
 *
 * Depends upon:
 *      none
 */

var g_blnLastChecked = false;

// Should the search query be checked for VQL and single/double quotes?
// If yes and the query contains some, THESAURUS, TYPO and SOUNDEX are NOT applied.
var g_blnCheckDisableTST = true;

// Helper function to trim whitespace
String.prototype.trim = function()
{
  var strThis = this;
  strThis = strThis.replace(/^\s*(.*)/, "$1");
  strThis = strThis.replace(/(.*?)\s*$/, "$1");
  return strThis;
}
    
/*
 * =====================================================================
 *                 F O R M   S U B M I T   &   C L E A R
 * =====================================================================
 */
 
function generateQuery(form, g_strTypo, g_strSoundex, g_strThesaurus)
{
    var strQueryText = getValue(form, "QueryText");
    strQueryText = strQueryText.trim();
    
    var nonUtf8String = false;
    var encoding = getValue(form, "Encoding");
    
    // check if we are in a latin1/2 environment
    if (encoding.indexOf("iso-8859-1") != -1)
    {
        nonUtf8String = strQueryText;
    	strQueryText = encode_utf8(strQueryText, "latin1");
    	setValue(form, "QueryText", strQueryText);
    }
    else
    if (encoding.indexOf("iso-8859-2") != -1)
    {
        nonUtf8String = strQueryText;
    	strQueryText = encode_utf8(strQueryText, "latin2");
    	setValue(form, "QueryText", strQueryText);
    }
    
    //remember original query field for later
    setValue(form, "hiddenOriginalQueryText", strQueryText);
    setValue(form, "hiddenBasicQueryText", strQueryText);

	var strSourceQuery = "";
	// exclude pdf
	// -----------
    var strExcludePDF = getExcludePDF(form);
    if (strExcludePDF != "")
    {
        strSourceQuery = "(" + strExcludePDF + ")";
    }

    var TSTenabled = (g_strTypo || g_strSoundex || g_strThesaurus) &&
					 g_blnCheckDisableTST &&
   		             expandQueryWithTST(strQueryText);

	// typo, soundex, thesaurus
	// ------------------------
	var strTSTResult = "";

    if (strQueryText != "" && TSTenabled) {
	    var arrQueryTextSplitted = strQueryText.split(" ");
	    var boolMode = " <OR> ";
	    var sSkippedWords = "";

		for (var i=0; i < arrQueryTextSplitted.length; ++i) {
			var sWord = arrQueryTextSplitted[i];
			var sWordLower = sWord.toLowerCase();
		    var bIsBool = false;
		    
			if (sWordLower == "und" || sWordLower == "and" || sWordLower == "+") {
				boolMode = " <AND> ";
				bIsBool = true;
			}
			if (sWordLower == "oder" || sWordLower == "or") {
				boolMode = " <OR> ";
				bIsBool = true;
			}
			if (sWordLower == "nicht" || sWordLower == "not" || sWordLower == "-") {
				boolMode = " <NOT> ";
				bIsBool = true;
			}
			if (sWord != "" && !bIsBool) {
				if (sWord.indexOf("<") != 0) {
					if (sWord.indexOf("-") == 0) {
						sWord = sWord.substr(1);
						boolMode = " <NOT> ";
						bIsBool = true;
					}
					if (sWord.indexOf("+") == 0) {
						sWord = sWord.substr(1);
						boolMode = " <AND> ";
						bIsBool = true;
					}
					if (g_strTypo) {
					    if (strTSTResult != "") {
					        strTSTResult = strTSTResult + boolMode;
					    }
   					    // inkludiere das evtl. falsch geschriebene Wort ebenfalls
					    // da wenn es richtig ist, Verity weniger findet als wenn
					    // kein Typo angeschaltet wurde...
				        strTSTResult = strTSTResult + "(" + sWord + ",<TYPO/1>" + sWord + ")";
				    }
					if (g_strSoundex) {
					    if (strTSTResult != "") {
					        strTSTResult = strTSTResult + boolMode;
					    }
			        	strTSTResult = strTSTResult + "(<SOUNDEX>" + sWord + ")";
					}
					if (g_strThesaurus) {
					    if (strTSTResult != "") {
					        strTSTResult = strTSTResult + boolMode;
					    }
				        strTSTResult = strTSTResult + "(<THESAURUS>" + sWord + ")";
					}
				} else {
					sSkippedWords += sWord + " ";
				}
				
				// set mode for next word
				boolMode = " <OR> ";
			}
		}
		
		sSkippedWords.trim();
		
		if (strTSTResult != "") {
			if (strSourceQuery == "") {
				strSourceQuery = strTSTResult;
			}
			else {
				strSourceQuery += " <AND> (" +strTSTResult+ ")";
			}
		}
		
		// Delete IQP Query, since for e.g. <typo/n> queries any
		// _wrongly_ typed word delivers 0 results.
		// Set any skipped words instead there.
		setValue(form, "QueryText", sSkippedWords);
	}

    setValue(form, "SourceQueryText", strSourceQuery);

	// disable the submit button, so if the search takes a little longer the user does
	// not do click-click-click
	// the search field with the name "search" may not exist if the form is the header 
	// search form
	if (form.elements['search'])
	{
		form.elements['search'].disabled = true;
	}
	
    // finally: submit
    // -----------------------
    form.submit();
    
    // reset the original encoded QueryText
    if (nonUtf8String)
    {
        window.setTimeout(function() { setValue(form, "QueryText", nonUtf8String) }, 10);
    }
}


/*
 * =====================================================================
 *                 H E L P E R   F U N C T I O N S
 * =====================================================================
 */

// checks if a string contains Verity Search VQL
// or strings in single or double quotes. These
// things are pretty much screwy up the above code
function expandQueryWithTST(str)
{	
	// at first find any single or double quote
	if (str.match(/[\'\"]/)) return false;

	// now find any VQL inside by matching <something> tags
	if (str.match(/\<.+?\>/)) return false;
	
	// this seems to be a normal string...
	return true;
}

function getValue(myForm, name) {
    if (myForm == null) return "";
	myElem = myForm.elements[name];
	if (myElem == null) return "";
	return myElem.value;
}

function setValue(myForm, name, value) {
	if (myForm == null) return false;
	myElem = myForm.elements[name];
	if (myElem == null) return false;
	myElem.value = value;
	return true;
}

function getExcludePDF(form)
{
    var strResult = "";
    var elmExcludePDF = form.elements["ExcludePDF"];
    if (elmExcludePDF)
    {
        var strExcludePDF = elmExcludePDF.checked;
        if (strExcludePDF == true) {
            strResult = "<NOT> MIME-Type <MATCHES> application/pdf";
        }
    }

    return strResult;
}

//
// taken from http://aktuell.de.selfhtml.org/artikel/javascript/utf8b64/utf8.htm
//
function encode_utf8(source, charset)
{
     source = source.replace(/\r\n/g,"\n");
     var dest = "";
     
     if (charset == "latin1")
     {
	     for(var n=0; n<source.length; n++)
	     {
	         var c=source.charCodeAt(n);
	         if (c<128)
	             dest += String.fromCharCode(c);
	         else if((c>127) && (c<2048)) {
	             dest += String.fromCharCode((c>>6)|192);
	             dest += String.fromCharCode((c&63)|128);}
	         else {
	             dest += String.fromCharCode((c>>12)|224);
	             dest += String.fromCharCode(((c>>6)&63)|128);
	             dest += String.fromCharCode((c&63)|128);}
	      }
	 }
	 elseif (charset == "latin2")
	 {
	    latin2table = new Array();
	    latin2table[0xA0] = String.fromCharCode(U+00A0);
		latin2table[0xA1] = String.fromCharCode(U+0104);
		latin2table[0xA2] = String.fromCharCode(U+02D8);
		latin2table[0xA3] = String.fromCharCode(U+0141);
		latin2table[0xA4] = String.fromCharCode(U+00A4);
		latin2table[0xA5] = String.fromCharCode(U+013D);
		latin2table[0xA6] = String.fromCharCode(U+015A);
		latin2table[0xA7] = String.fromCharCode(U+00A7);
		latin2table[0xA8] = String.fromCharCode(U+00A8);
		latin2table[0xA9] = String.fromCharCode(U+0160);
		latin2table[0xAA] = String.fromCharCode(U+015E);
		latin2table[0xAB] = String.fromCharCode(U+0164);
		latin2table[0xAC] = String.fromCharCode(U+0179);
		latin2table[0xAD] = String.fromCharCode(U+00AD);
		latin2table[0xAE] = String.fromCharCode(U+017D);
		latin2table[0xAF] = String.fromCharCode(U+017B);
		latin2table[0xB0] = String.fromCharCode(U+00B0);
		latin2table[0xB1] = String.fromCharCode(U+0105);
		latin2table[0xB2] = String.fromCharCode(U+02DB);
		latin2table[0xB3] = String.fromCharCode(U+0142);
		latin2table[0xB4] = String.fromCharCode(U+00B4);
		latin2table[0xB5] = String.fromCharCode(U+013E);
		latin2table[0xB6] = String.fromCharCode(U+015B);
		latin2table[0xB7] = String.fromCharCode(U+02C7);
		latin2table[0xB8] = String.fromCharCode(U+00B8);
		latin2table[0xB9] = String.fromCharCode(U+0161);
		latin2table[0xBA] = String.fromCharCode(U+015F);
		latin2table[0xBB] = String.fromCharCode(U+0165);
		latin2table[0xBC] = String.fromCharCode(U+017A);
		latin2table[0xBD] = String.fromCharCode(U+02DD);
		latin2table[0xBE] = String.fromCharCode(U+017E);
		latin2table[0xBF] = String.fromCharCode(U+017C);
		latin2table[0xC0] = String.fromCharCode(U+0154);
		latin2table[0xC1] = String.fromCharCode(U+00C1);
		latin2table[0xC2] = String.fromCharCode(U+00C2);
		latin2table[0xC3] = String.fromCharCode(U+0102);
		latin2table[0xC4] = String.fromCharCode(U+00C4);
		latin2table[0xC5] = String.fromCharCode(U+0139);
		latin2table[0xC6] = String.fromCharCode(U+0106);
		latin2table[0xC7] = String.fromCharCode(U+00C7);
		latin2table[0xC8] = String.fromCharCode(U+010C);
		latin2table[0xC9] = String.fromCharCode(U+00C9);
		latin2table[0xCA] = String.fromCharCode(U+0118);
		latin2table[0xCB] = String.fromCharCode(U+00CB);
		latin2table[0xCC] = String.fromCharCode(U+011A);
		latin2table[0xCD] = String.fromCharCode(U+00CD);
		latin2table[0xCE] = String.fromCharCode(U+00CE);
		latin2table[0xCF] = String.fromCharCode(U+010E);
		latin2table[0xD0] = String.fromCharCode(U+0110);
		latin2table[0xD1] = String.fromCharCode(U+0143);
		latin2table[0xD2] = String.fromCharCode(U+0147);
		latin2table[0xD3] = String.fromCharCode(U+00D3);
		latin2table[0xD4] = String.fromCharCode(U+00D4);
		latin2table[0xD5] = String.fromCharCode(U+0150);
		latin2table[0xD6] = String.fromCharCode(U+00D6);
		latin2table[0xD7] = String.fromCharCode(U+00D7);
		latin2table[0xD8] = String.fromCharCode(U+0158);
		latin2table[0xD9] = String.fromCharCode(U+016E);
		latin2table[0xDA] = String.fromCharCode(U+00DA);
		latin2table[0xDB] = String.fromCharCode(U+0170);
		latin2table[0xDC] = String.fromCharCode(U+00DC);
		latin2table[0xDD] = String.fromCharCode(U+00DD);
		latin2table[0xDE] = String.fromCharCode(U+0162);
		latin2table[0xDF] = String.fromCharCode(U+00DF);
		latin2table[0xE0] = String.fromCharCode(U+0155);
		latin2table[0xE1] = String.fromCharCode(U+00E1);
		latin2table[0xE2] = String.fromCharCode(U+00E2);
		latin2table[0xE3] = String.fromCharCode(U+0103);
		latin2table[0xE4] = String.fromCharCode(U+00E4);
		latin2table[0xE5] = String.fromCharCode(U+013A);
		latin2table[0xE6] = String.fromCharCode(U+0107);
		latin2table[0xE7] = String.fromCharCode(U+00E7);
		latin2table[0xE8] = String.fromCharCode(U+010D);
		latin2table[0xE9] = String.fromCharCode(U+00E9);
		latin2table[0xEA] = String.fromCharCode(U+0119);
		latin2table[0xEB] = String.fromCharCode(U+00EB);
		latin2table[0xEC] = String.fromCharCode(U+011B);
		latin2table[0xED] = String.fromCharCode(U+00ED);
		latin2table[0xEE] = String.fromCharCode(U+00EE);
		latin2table[0xEF] = String.fromCharCode(U+010F);
		latin2table[0xF0] = String.fromCharCode(U+0111);
		latin2table[0xF1] = String.fromCharCode(U+0144);
		latin2table[0xF2] = String.fromCharCode(U+0148);
		latin2table[0xF3] = String.fromCharCode(U+00F3);
		latin2table[0xF4] = String.fromCharCode(U+00F4);
		latin2table[0xF5] = String.fromCharCode(U+0151);
		latin2table[0xF6] = String.fromCharCode(U+00F6);
		latin2table[0xF7] = String.fromCharCode(U+00F7);
		latin2table[0xF8] = String.fromCharCode(U+0159);
		latin2table[0xF9] = String.fromCharCode(U+016F);
		latin2table[0xFA] = String.fromCharCode(U+00FA);
		latin2table[0xFB] = String.fromCharCode(U+0171);
		latin2table[0xFC] = String.fromCharCode(U+00FC);
		latin2table[0xFD] = String.fromCharCode(U+00FD);
		latin2table[0xFE] = String.fromCharCode(U+0163);
		latin2table[0xFF] = String.fromCharCode(U+02D9);
			     
		for(var n=0; n<source.length; n++)
	    {
	        var c=source.charCodeAt(n);
	        if (c < 0xA0)
	        {
	       	    dest += String.fromCharCode(c);
	        	continue;
	        }
	        dest += latin2table[c];
	    }
	 }
	 else
	 {
	 	alert("Unsupported charset");
	 	dest = source;
	 }
     return dest;
}

//
// Calls generateQuery for the head search field.
//
function generateQueryHead() {
    
	var form = document.getElementById("formBasicQueryHead");
	if (!form || typeof form != "object")
	{
		alert("Cannot find form, please ensure that\nit has the unique ID \"formBasicQueryHead\".");
		return false;
	}
	
    generateQuery(form, false, false, false);   
}