1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| <?php require_once("config.php"); class DBManager { var $table; var $dbh; var $words; var $cond; public function DBManager() { $this->table = TABLE_NAME; $this->dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB\_NAME, DB\_USER, DB_PASSWORD); } public function insertFullTextIndex($uri, $title, $content) { $stmt = $this->dbh->prepare("INSERT INTO ".$this->table ." (uri, title, content) VALUES (:uri, :title, :content)"); $stmt->bindParam(':uri', $uri); $stmt->bindParam(':title', $title); $stmt->bindParam(':content', $content); return $stmt->execute(); } public function setWords($sw) { $sw = mb\_ereg\_replace("(\\s| )+"," ", $sw); $sw = mb\_ereg\_replace("^\\s+", "", $sw); $sw = mb\_ereg\_replace("\\s+$", "", $sw); $sw = mb\_ereg\_replace("'", "\\'", $sw); $this->words = preg_split("/\\s/", $sw); $this->cond = ""; foreach ($this->words as $str){ if ('-' != substr($str, 0, 1)) { $this->cond .= " +".$str; } else { $this->cond .= " ".$str; } } } public function hitcount(){ $stmt = $this->dbh->prepare("SELECT COUNT(*) FROM ".$this->table ." WHERE MATCH(title, content) AGAINST ('" .$this->cond."' IN BOOLEAN MODE)"); $stmt->execute(); return $stmt->fetchColumn(); } public function find($page=-1, $page_unit=50){ $kcount = 0; $kwic = "kwic(content, 320, 1, 0, \\"\\", \\"...\\" "; foreach ($this->words as $str){ if ('-' != substr($str, 0, 1)) { $kwic .= ", \\"".$str."\\", \\"<em class='kw".$kcount."'>\\", \\"</em>\\" "; if (++$kcount == 5) $kcount = 0; } } $kwic .= ") "; if ($page == -1) { $pagesql = ""; } else { $pagesql = " LIMIT ". ($page * $page_unit) .",".$page_unit; } $stmt = $this->dbh->prepare("SELECT uri, title, ".$kwic." as summary FROM " .$this->table." WHERE MATCH(title, content) AGAINST ('" .$this->cond."' IN BOOLEAN MODE)".$pagesql); return $stmt; } } ?>
|