require "include.php";
require "functions.php";
require "display.php";
require "searchform.php";
require "Token.php";
require "KeywordToSQL.php";
require "BooleanToSQL.php";
/* get options:
a# - areas to search in (0 - all, 1-## - catid for top level cat)
c - compact results
f# - fields to search in (keys from $fieldnames array in include.php)
o - order by: t - title, d - date, e - date of entry, f - rev. date of entry
q - number in result block
r - list results starting at result #r
s - end result list at result #s
v - verbose: show advanced options
*/
# if there's a search string, perform the search; else print the search form
$f_set = false;
$r = isset($_GET['r']) ? intval($_GET['r']) : 0;
$s = isset($_GET['s']) ? intval($_GET['s']) : 0;
$sstr = isset($_GET['sstr']) ? strip_tags($_GET['sstr']) : '';
$c = isset($_GET['c']) ? $_GET['c'] : 0;
$t = isset($_GET['t']) ? $_GET['t'] : 'k';
$q = isset($_GET['q']) ? $_GET['q'] : 'all';
$o = isset($_GET['o']) ? $_GET['o'] : 'd';
$f0 = isset($_GET['f0']) ? $_GET['f0'] : 1;
$a0 = isset($_GET['a0']) ? $_GET['a0'] : 1;
$v = isset($_GET['v']) ? $_GET['v'] : 1;
$am = isset($_GET['am']) ? $_GET['am'] : 0;
$ar = isset($_GET['ar']) ? $_GET['ar'] : 0;
for($j = 1; $j < count($fieldnames); $j++)
{
if(isset($_GET['f'.$j]))
{
${"f" . $j} = 1;
$f_set = true;
} else {
${"f" . $j} = 0;
}
}
if($f_set) {
$f0 = 0;
}
if($am || $ar) {
$a0 = 0;
}
$query0 = "select catid from category where parentid < 0";
$result0 = mysqli_query($dblink,$query0);
while($crow = mysqli_fetch_row($result0))
{
$id = $crow[0];
if(isset($_GET["a" . $id]))
{
${"a" . $id} = 1;
} else {
${"a" . $id} = 0;
}
}
if(!empty($sstr))
{
printSearchResults($sstr);
}
else
{
printtop("Search Form");
print "
| \n";
printSearchForm($v);
print " |
\n";
printfoot();
}
function printSearchResults($sstr)
{
extract($GLOBALS);
$v = (isset($_REQUEST['v'])) ? $_REQUEST['v'] : $v;
$t = (isset($_REQUEST['t'])) ? $_REQUEST['t'] : $t;
$o = (isset($_REQUEST['o'])) ? $_REQUEST['o'] : $o;
$am = (isset($_REQUEST['am'])) ? $_REQUEST['am'] : $am;
$ar = (isset($_REQUEST['ar'])) ? $_REQUEST['ar'] : $ar;
$q = (isset($_REQUEST['q'])) ? $_REQUEST['q'] : $q;
$r = (isset($_REQUEST['r'])) ? $_REQUEST['r'] : $r;
printtop("Search Results");
#print "| Browse the OBL |
\n";
print "| \n";
printSearchForm($v);
print " |
\n";
if($sstr == "")
{
print "| ";
print "No query was entered. Please try again.";
print " |
\n";
printfoot();
exit();
}
$sstr = stripslashes($sstr);
if($t == "b")
{
$stosql = new BooleanToSQL($sstr);
}
else
{
$stosql = new KeywordToSQL($sstr);
}
if($stosql->getCount() < 1)
{
print "| ";
print "$sstr includes only common words or punctuation which aren't included in the search. Try using more specific words. \n";
print " |
\n";
printfoot();
exit();
}
if($o == "t")
{
$orderby = "title";
}
else if($o == "e")
{
$orderby = "dateofentry";
}
else if($o == "f")
{
$orderby = "dateofentry desc";
}
else
{
$o = 'd';
$orderby = "date desc";
}
$query = "select * from link where ";
$join = "or";
$rel = "regexp";
$pre = "[[:<:]]";
$post = "[[:>:]]";
$varnames = array();
$fieldvar = "f0";
# add the chosen fields (index # of $fieldnames is key)
if(!$f0)
{
$f = 0;
for($i = 1; $i < count($fieldnames); $i++)
{
$fieldvar = "f" . $i;
if(${$fieldvar})
{
$varnames[$f] = $fieldnames[$i];
$dispvarnames[$f++] = $dispfieldnames[$i];
# if url field is added, search alturls, too
if($fieldnames[$i] == "url")
{
$varnames[$f++] = "alturls";
}
}
}
}
# if all fields were chosen or no fields were chosen,
# search all fields in array $fieldnames
else
{
$varnames = $fieldnames;
# take the "all" off
array_shift($varnames);
array_push($varnames, "alturls");
}
$wc = $stosql->getSQL($varnames, $join, $rel, $pre, $post);
if($wc == "0")
{
print "| \n";
print "The search engine wasn't able to understand your query $sstr. \n";
if($t == "k")
{
print "Check that quotes around exact phrases are paired correctly.";
}
else if($t == "b")
{
print "Make sure that the logic in your query is correct:\n";
print " the parentheses are nested correctly and match\n";
print "any phrases are in quotes\n";
print "the allowed operators are 'and', 'or', and 'not'\n";
print "* is the wildcard character\n";
print "Some properly formed queries: \n";
print "cats and dogs \n";
print "cat* or (dog* and horse) \n";
print "(cat or dog) and (black not white) \n";
print "'house cat' not tiger* \n";
}
print " |
\n";
printfoot();
exit();
}
# if no areas were chosen and a0 wasn't set, set $a0 to 1
$query2 = "select catid from category where parentid < 0";
$result2 = mysqli_query($dblink,$query2);
$aflag = 0;
while($crow = mysqli_fetch_row($result2))
{
$id = $crow[0];
if(${"a" . $id})
{
$aflag = 1;
}
}
if($aflag == 0 && (!$am) && (!$ar))
{
$a0 = 1;
}
$query .= "( $wc ) && available = 1 order by $orderby";
$result = mysqli_query($dblink,$query);
# put the category matches into $coutput; only display them on the
# first page
$catquery = "select catid from category where ";
$catvarnames = array("catname");
$catwc = $stosql->getSQL($catvarnames, $join, $rel, $pre, $post);
$catquery .= "$catwc order by catname";
$catresult = mysqli_query($dblink,$catquery);
$rcount = 0;
$routput = array();
# filter out results so that only results in the specified categories
# are shown; add them to $routput, which is printed later
while($dbrow = mysqli_fetch_object($result))
{
$show = 0;
# for cats, key is catid for top level categories
$topcat = getTopCat($dbrow->linkid, 0);
$topvar = "a" . $topcat;
$topparent = getTopCat($dbrow->linkid, 1);
if($a0 == 1)
{
$show = 1;
}
else if($topparent == -1 && $am == 1)
{
$show = 1;
}
else if($topparent == -2 && $ar == 1)
{
$show = 1;
}
else if(${$topvar} == 1)
{
$show = 1;
}
if($show)
{
$routput[$rcount] = "";
$routput[$rcount] .= formatLink($dbrow, 1, $c);
$routput[$rcount] .= "\n";
$rcount++;
}
}
print "
| \n";
$rintroline = "matches were found for $sstr in ";
$catintroline = $rintroline;
$fields = "";
$areas = "";
if($f0)
{
$rintroline .= "all fields";
}
else
{
for($i = 0; $i < count($dispvarnames); $i++)
{
if($varnames[$i] != "alturls")
{
$fields .= "$dispvarnames[$i], ";
}
}
$fields = preg_replace("/,\s*$/", "", $fields);
$rintroline .= "field(s) $fields";
}
$rintroline .= " and in ";
if($a0)
{
$rintroline .= " all sections";
$catintroline .= " all sections";
}
else
{
$query2 = "select catid, catname from category where parentid = '-1' order by catname";
$result2 = mysqli_query($dblink,$query2);
$j = 0;
while($dbrow = mysqli_fetch_object($result2))
{
$catids[$j++] = $dbrow;
}
if($am == 1)
{
$areas .= "Main Library, ";
}
if($ar == 1)
{
$areas .= "Reading Room, ";
}
for($i = 0; $i < count($catids); $i++)
{
$topvar = "a" . $catids[$i]->catid;
if(${$topvar})
{
$areas .= $catids[$i]->catname . ", ";
}
}
$areas = preg_replace("/,\s*$/", "", $areas);
$rintroline .= "section(s) $areas";
$catintroline .= "section(s) $areas";
}
$rintroline .= ". \n";
$catintroline .= ". \n";
if($rcount < 1)
{
print "Try searching for more general terms or using 'or' instead of 'and' in a boolean query.\n";
}
else
{
if($q)
{
$rblock = $q;
}
if(!$r)
{
$rstart = 0;
}
else
{
$rstart = $r;
}
if(!$s)
{
$rstop = $rstart + $rblock - 1;
}
else if($s > count($routput))
{
$rstop = count($routput) - 1;
}
else
{
$rstop = $s;
}
$stop = $rstop;
if($stop > count($routput) - 1)
{
$stop = count($routput) - 1;
}
if($q == "all")
{
$rstart = 0;
$rstop = count($routput);
$stop = $rstop - 1;
}
if($rstart == 0)
{
$coutput = getCatMatchOutput($catresult);
if(count($coutput) > 0)
{
print " \n" . count($coutput) . " category $catintroline \n";
for($i = 0; $i < count($coutput); $i++)
{
print "$coutput[$i] \n";
}
print " \n \n\n";
}
}
print "$rcount link matches $rintroline\n";
print " Link matches " . ($rstart + 1) . " - " . ($stop + 1) . "\n \n";
for($j = $rstart; $j <= $rstop && $j < count($routput); $j++)
{
print $routput[$j];
}
print " ";
$qsarray = modifyQS($QUERY_STRING, -1, $rstart, $rstop, count($routput), $rblock);
$querystring = $qsarray[0];
$nextnum = $qsarray[1];
if($r > 0 && $q != "all")
{
$nextnum++;
print "| ";
print "<- previous $nextnum results\n";
print " | \n";
}
$qsarray = modifyQS($QUERY_STRING, 1, $rstart, $rstop, count($routput), $rblock);
$querystring = $qsarray[0];
$nextnum = $qsarray[1];
if($nextnum >= 0 && $stop != count($routput) - 1 && $q != "all")
{
$nextnum++;
print "";
print "next $nextnum results ->\n";
print " | \n";
}
print " \n";
}
print " |
\n";
printfoot();
exit();
}
function modifyQS($querystring, $unit, $rstart, $rstop, $rnum, $rblock)
{
if($unit > 0)
{
$rstop += $rblock;
}
else if($unit < 0)
{
$rstop = $rstart - 1;
}
$rstart += $rblock * $unit;
if($rstart < 0)
{
$rstart = 0;
}
else if($rstart > $rnum - 1)
{
$rstart = $rnum - 1;
}
if($rstop < 0)
{
$rstop = 0;
}
else if($rstop > $rnum - 1)
{
$rstop = $rnum - 1;
}
if(preg_match("/\&r=\d+/", $querystring))
{
$querystring = preg_replace("/\&r=\d+/", "\&r=$rstart", $querystring);
}
else if($querystring == "")
{
$querystring = "r=$rstart";
}
else
{
$querystring .= "&r=$rstart";
}
if(preg_match("/s=\d+/", $querystring))
{
$querystring = preg_replace("/s=\d+/", "s=$rstop", $querystring);
}
else if($querystring == "")
{
$querystring = "s=$rstop";
}
else
{
$querystring .= "&s=$rstop";
}
return array($querystring, $rstop-$rstart);
}
function getCatMatchOutput($catresult)
{
extract($GLOBALS);
$a0 = (isset($_REQUEST['a0'])) ? $_REQUEST['a0'] : $a0;
$am = (isset($_REQUEST['am'])) ? $_REQUEST['am'] : $am;
$ar = (isset($_REQUEST['ar'])) ? $_REQUEST['ar'] : $ar;
$lo = (isset($_REQUEST['lo'])) ? $_REQUEST['lo'] : $lo;
$sl = (isset($_REQUEST['sl'])) ? $_REQUEST['sl'] : $sl;
$coutput = array();
while($catrow = mysqli_fetch_object($catresult))
{
$catid = $catrow->catid;
$categories = parentCats($catid);
$catline = "";
$show = 0;
# for cats, key is catid for top level categories
$topcat = $categories[0]->catid;
$topvar = "a" . $topcat;
$topparent = $categories[0]->parentid;
if($a0 == 1)
{
$show = 1;
}
else if($topparent == -1 && $am == 1)
{
$show = 1;
}
else if($topparent == -2 && $ar == 1)
{
$show = 1;
}
else if(${$topvar} == 1)
{
$show = 1;
}
if($show)
{
if($categories[0]->parentid == -1)
{
$catline .= "Main Library\n";
}
else if($categories[0]->parentid == -2)
{
$catline .= "Reading Room\n";
}
for($i = 0; $i < count($categories); $i++)
{
$catline .= "> " . $categories[$i]->catname . " ";
}
$catline .= "\n";
array_push($coutput, $catline);
}
}
return $coutput;
}
?>