1
0

Fixing error in search library

This commit is contained in:
Stefan Frech
2008-06-28 09:51:39 +00:00
committed by Jonas Kattendick
parent c25d9cbc46
commit 6b4bacaddb

View File

@@ -39,12 +39,12 @@ function tokenize($criteria) {
$tokens = array( $tokens = array(
TOKEN_STRING => '', TOKEN_STRING => '',
TOKEN_AND => 'and', TOKEN_AND => 'and',
TOKEN_OR => 'or', TOKEN_OR => 'or',
TOKEN_NOT => 'not', TOKEN_NOT => 'not',
TOKEN_LEFTPAREN => '(', TOKEN_LEFTPAREN => '(',
TOKEN_RIGHTPAREN => ')', TOKEN_RIGHTPAREN => ')',
TOKEN_PLUS => '+', TOKEN_PLUS => '+',
TOKEN_MINUS => '-'); TOKEN_MINUS => '-');
// automaton [states][chartypes] => actions // automaton [states][chartypes] => actions
// states: // states:
@@ -101,7 +101,7 @@ function tokenize($criteria) {
if (!$tokentype) $tokentype = TOKEN_STRING; if (!$tokentype) $tokentype = TOKEN_STRING;
} }
$out[] = array($tokentype, $word); $out[] = array($tokentype, $word);
$word = ''; $word = '';
} }
} }
} //for j } //for j
@@ -115,7 +115,7 @@ function tokenize($criteria) {
if (!$tokentype) $tokentype = TOKEN_STRING; if (!$tokentype) $tokentype = TOKEN_STRING;
} }
$out[] = array($tokentype, $word); $out[] = array($tokentype, $word);
$word = ''; $word = '';
} }
return $out; return $out;
} //tokenize } //tokenize
@@ -137,46 +137,46 @@ function parsecriteria($criteria) {
$results = array(); $results = array();
$tokens = array(); $tokens = array();
$thisresult = array('','',TRUE,0); $thisresult = array('','',TRUE,0);
$nesting = 0; $nesting = 0;
//var_dump($criteria); //@@@ //var_dump($criteria); //@@@
//replace html quoting put there by some browsers, then tokenize //replace html quoting put there by some browsers, then tokenize
$tokens = tokenize(str_replace ('"', '"', $criteria)); $tokens = tokenize(str_replace ('"', '"', $criteria));
//var_dump($tokens); //@@@ //var_dump($tokens); //@@@
foreach ($tokens as $token) { foreach ($tokens as $token) {
switch ($token[0]) { switch ($token[0]) {
case TOKEN_AND: case TOKEN_AND:
$thisresult[0] = ' AND '; $thisresult[0] = ' AND ';
$thisresult[2] = TRUE; //reset wildcard in case of bad syntax $thisresult[2] = TRUE; //reset wildcard in case of bad syntax
break; break;
case TOKEN_OR: case TOKEN_OR:
$thisresult[0] = ' OR '; $thisresult[0] = ' OR ';
$thisresult[2] = TRUE; //reset wildcard in case of bad syntax $thisresult[2] = TRUE; //reset wildcard in case of bad syntax
break; break;
case TOKEN_PLUS: case TOKEN_PLUS:
$thisresult[2] = FALSE; $thisresult[2] = FALSE;
break; break;
case TOKEN_NOT: case TOKEN_NOT:
case TOKEN_MINUS: case TOKEN_MINUS:
$thisresult[0] .= ' NOT '; //NOT or AND NOT $thisresult[0] .= ' NOT '; //NOT or AND NOT
break; break;
case TOKEN_LEFTPAREN: case TOKEN_LEFTPAREN:
$nesting += 1; $nesting += 1;
$thisresult[2] = TRUE; //reset just in case of bad syntax $thisresult[2] = TRUE; //reset just in case of bad syntax
break; break;
case TOKEN_RIGHTPAREN: case TOKEN_RIGHTPAREN:
$nesting -= 1; $nesting -= 1;
$thisresult[2] = TRUE; //reset just in case of bad syntax $thisresult[2] = TRUE; //reset just in case of bad syntax
break; break;
default: default:
// anything else -> output "as is" // anything else -> output "as is"
$thisresult[1] .= $token[1]; $thisresult[1] .= $token[1];
@@ -193,18 +193,18 @@ function parsecriteria($criteria) {
function assemble_query ($criteria, $searchfields) { function assemble_query ($criteria, $searchfields) {
global $mysql, $username, $search; global $mysql, $username, $search;
$whereCriterias = parsecriteria ($criteria); $whereCriterias = parsecriteria ($criteria);
//var_dump($whereCriterias); //@@@ //var_dump($whereCriterias); //@@@
$whereData = array(); $whereData = array();
$columnNumber = 0; $columnNumber = 0;
$whereClause = ""; $whereClause = "";
$nesting = 0; $nesting = 0;
foreach ($whereCriterias as $mycriteria) { foreach ($whereCriterias as $mycriteria) {
$whereClause .= $mycriteria[0]; $whereClause .= $mycriteria[0];
$thisnesting = $mycriteria[3]; $thisnesting = $mycriteria[3];
if ($thisnesting >= $nesting) { if ($thisnesting >= $nesting) {
$whereClause .= str_repeat('(', $thisnesting - $nesting); $whereClause .= str_repeat('(', $thisnesting - $nesting);
@@ -212,7 +212,7 @@ function assemble_query ($criteria, $searchfields) {
$whereClause .= str_repeat(')', $nesting - $thisnesting); $whereClause .= str_repeat(')', $nesting - $thisnesting);
} }
$nesting = $thisnesting; $nesting = $thisnesting;
$firstcolumn = TRUE; $firstcolumn = TRUE;
$whereClause .= ' ('; $whereClause .= ' (';
foreach ($searchfields as $column) { foreach ($searchfields as $column) {
@@ -221,7 +221,7 @@ function assemble_query ($criteria, $searchfields) {
} else { } else {
$whereClause .= ' OR '; $whereClause .= ' OR ';
} }
if ($mycriteria[2]) { if ($mycriteria[2]) {
$whereClause .= "$column LIKE " . '\'' .'%' . $mysql->escape ($mycriteria[1]) . '%' . '\'' ; $whereClause .= "$column LIKE " . '\'' .'%' . $mysql->escape ($mycriteria[1]) . '%' . '\'' ;
} else { } else {
@@ -233,27 +233,33 @@ function assemble_query ($criteria, $searchfields) {
} //foreach $whereCriterias } //foreach $whereCriterias
$whereClause .= str_repeat(')', $nesting); $whereClause .= str_repeat(')', $nesting);
$whereClause = trim ($whereClause);
$query = sprintf ("SELECT bookmark.title, if ($whereClause != '') {
bookmark.url, $query = sprintf ("SELECT bookmark.title,
bookmark.description, bookmark.url,
UNIX_TIMESTAMP(bookmark.date) AS timestamp, bookmark.description,
bookmark.childof, UNIX_TIMESTAMP(bookmark.date) AS timestamp,
bookmark.id, bookmark.childof,
bookmark.favicon, bookmark.id,
bookmark.public, bookmark.favicon,
folder.name, bookmark.public,
folder.id AS fid, folder.name,
folder.public AS fpublic folder.id AS fid,
FROM bookmark LEFT JOIN folder ON bookmark.childof=folder.id folder.public AS fpublic
FROM bookmark LEFT JOIN folder ON bookmark.childof=folder.id
WHERE bookmark.user='%s'
AND bookmark.deleted!='1' WHERE bookmark.user='%s'
AND ( %s ) AND bookmark.deleted!='1'
ORDER BY title", AND ( %s )
$mysql->escape ($username), ORDER BY title",
$whereClause); $mysql->escape ($username),
$whereClause);
}
else {
$query = false;
}
return $query; return $query;
} }