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