<?
 
 
require "demo.inc";
 
require "KwIndex.lib";
 
 
$limit_contentlength = 10*1024;
 
#$limit_numdocs = 10000;
 
 
class MyKwIndex extends KwIndex {
 
    function &document_sub($doc_ids) {
 
        $docs = array();
 
 
        # let's select the documents in a single query
 
        $res = mysql_query("select id, title, content from document ".
 
                              "where id in (".join(',', $doc_ids).")");
 
        while($row = mysql_fetch_row($res)) {
 
            $docs[ $row[0] ] = $row[1]." ".$row[2];
 
        }
 
        return $docs;
 
    }
 
}
 
 
$kw = new MyKwIndex(array(
 
    "index_name" => "kwindex",
 
    "hostname" => "localhost",
 
 
    // from "demo.inc"
 
    "db_name" => "kwindex_test",
 
    "username" => $username,
 
    "password" => $password
 
));
 
 
?>
 
 
<title>KwIndex demo</title>
 
<h1>KwIndex demo</h1>
 
 
 
 
 
 
<? if(!$action) { ?>
 
 
<h2>What do you want to do?</h2>
 
<ul>
 
<li><a href=demo.php?action=add>Add and index a document</li>
 
<li><a href=demo.php?action=browse>Browse documents</li>
 
<li><a href=demo.php?action=search>Search</a>
 
<li><a href=demo.phps>See the source code of this script</a>
 
</ul>
 
 
<h2>Document and index statistics</h2>
 
<ul>
 
<li>Total number of documents: <? $res = mysql_query("select count(*) from document"); $row = mysql_fetch_row($res); echo $row[0]; ?>
 
<li>Total size of documents: <? $res = mysql_query("select sum(contentlength) from document"); $row = mysql_fetch_row($res); echo $row[0]; ?> bytes
 
</ul>
 
 
 
 
 
 
 
 
 
<? } elseif($action == 'add') { ?>
 
 
<h2>Add and index a document</h2>
 
 
<?   if (!$title && !$content) { ?>
 
<form method=POST>
 
<input type=hidden name=action value=add>
 
Title<br><input name=title size=70><br>
 
Content<br><textarea name=content cols=70 rows=20></textarea><br>
 
<input type=submit>
 
</form>
 
 
<?   } else { ?>
 
 
<?
 
 
mysql_query("insert into document (title,content,contentlength) values ('".addslashes($title)."','".addslashes($content)."',".strlen($content).")")
 
    or die("can't insert: ".mysql_error());
 
$id = mysql_insert_id();
 
$kw->add_document(array($id))
 
    or die("can't index: ".$kw->ERROR);
 
?>
 
 
Document indexed. Document id = <? echo $id ?>. Content length = <? echo strlen($content) ?>.
 
 
<?   } ?>
 
 
 
 
 
 
 
 
 
 
<? } elseif($action == 'browse' || $action == 'search') { ?>
 
 
<?   if ($action == 'search') { ?>
 
<h2>Search</h2>
 
<?   } else { ?>
 
<h2>Browse documents</h2>
 
<?   } ?>
 
 
<form method=GET>
 
<input type=hidden name=action value=search>
 
Search: <input name=words value="<? echo htmlentities($words) ?>">
 
Boolean: <select name=boolean><option<? echo $boolean == 'AND' ? " selected":"" ?>>AND<option<? echo $boolean == 'OR' ? " selected":""?>>OR</select>
 
Show: <select name=num>
 
    <option<? echo $num == 10 ? " selected":""?>>10
 
    <option<? echo $num == 20 ? " selected":""?>>20
 
    <option<? echo $num == 50 ? " selected":""?>>50
 
    <option<? echo $num == 100 ? " selected":""?>>100
 
</select>
 
<input type=submit value=Search>
 
</form>
 
 
<?   if($action == 'browse' || $action == "search" && $words) { ?>
 
<?
 
 
if(!isset($start) || preg_match("/\D/", $start) || $start < 0) $start=1;
 
if(!isset($num) || preg_match("/\D/", $num) || $num > 100 || $num < 0) $num=20;
 
if(!isset($boolean) || $boolean != 'OR') $boolean='AND';
 
if(!preg_match("/\S/", $words)) {
 
    $docs = array();
 
    if(!($res = mysql_query("select id,mtime,title,content from document limit ".($start-1).",".($num+1))))
 
        die("can't select: ".mysql_error());
 
    while($row = mysql_fetch_row($res)) 
 
        array_push($docs, $row);
 
} else {
 
    $doc_ids = $kw->search(array('words'=>$words,'num'=>$num+1,'start'=>$start,'boolean'=>$boolean));
 
    if (!isset($doc_ids))
 
        die("can't search: ".$kw->ERROR);
 
    $docs = array();
 
    if (sizeof($doc_ids)) {
 
        if(!($res = mysql_query("select id,mtime,title,content from document where id in (".join(',',$doc_ids).")")))
 
        die("can't select: ".mysql_error());
 
        while($row = mysql_fetch_row($res)) 
 
            array_push($docs, $row);
 
    }
 
}
 
 
?>
 
<table border>
 
<?   if($start>1) { ?>
 
<tr><td colspan=5 align=right><a href=demo.php?action=browse&words=<? echo urlencode($words) ?>&start=<? echo urlencode($start-$num) ?>&num=<? echo urlencode($num) ?>&boolean=<? echo $boolean ?>>Previous</a></td></tr>
 
<?   } ?>
 
<tr><td>id</td><td>title</td><td>content length</td><td>date</td><td>options</td></tr>
 
<?   for($i=0; $i<min($num,sizeof(&$docs)); ++$i) { ?>
 
<tr><td><? echo $docs[$i][0] ?></td><td><a href=demo.php?action=view&id=<? echo $docs[$i][0] ?>><? echo htmlentities($docs[$i][2]) ?></td><td><? echo strlen($docs[$i][3]) ?></td><td><? echo $docs[$i][1] ?></a></td><td><a href=demo.php?action=delete&id=<? echo $docs[$i][0] ?>>delete</a></tr>
 
<?   } ?>
 
<?   if(sizeof($docs) > $num) { ?>
 
<tr><td colspan=5 align=right><a href=demo.php?action=browse&words=<? echo urlencode($words) ?>&start=<? echo urlencode($start+$num) ?>&num=<? echo urlencode($num) ?>&boolean=<? echo $boolean ?>>Next</a></td></tr>
 
<?   } ?>
 
 
 
 
 
 
 
 
 
<?   } ?>
 
<? } elseif($action == 'view') { ?>
 
 
<h2>View document</h2>
 
 
<?   if($id) { ?>
 
<?
 
 
$res = mysql_query("select title,content from document where id='".addslashes($id)."'");
 
$row = mysql_fetch_row($res);
 
 
     if ($row) {
 
         echo "<h3>Title</h3>\n\n",htmlentities($row[0]),"\n\n<h3>Content</h3>\n\n",htmlentities($row[1]),"\n\n";
 
     }
 
?>
 
<?   } ?>
 
 
 
 
 
 
 
 
 
 
<? } elseif($action == 'delete') { ?>
 
 
<h2>Delete document</h2>
 
 
<? 
 
 
if(mysql_query("delete from document where id='".addslashes($id)."'")) {
 
    echo "Article id=".htmlentities($id)." has been deleted.";
 
} else {
 
    echo "Can't delete: ".mysql_error();
 
}
 
 
if(!$kw->remove_document($id)) {
 
    echo "Index for article id=".htmlentities($id)." has been removed.";
 
} else {
 
    echo "Can't remove article index: ".$kw->ERROR;
 
}
 
 
?>
 
 
 
 
 
 
 
 
 
<? } ?>
 
<hr>
 
<a href=http://steven.haryan.to/php/KwIndex.html>KwIndex page</a>
 
 
 |