123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- <?php
- require_once 'xapian.php';
- require_once dirname(__FILE__) . '/../IndexableChunk.class.php';
- abstract class XapianIndexer
- {
-
- protected $db;
-
- protected $chunks;
-
- public $indexer;
-
- public $stemmer;
-
- function __construct()
- {
- $this->db = null;
- $this->stemmer = null;
- }
-
- public final function xapian_languages()
- {
-
- return array(
- 'none' => 'none',
- 'da' => 'danish',
- 'nl' => 'dutch',
-
- 'en' => 'english',
-
- 'lovins' => 'english_lovins',
-
- 'porter' => 'english_porter',
- 'fi' => 'finnish',
- 'fr' => 'french',
- 'de' => 'german',
- 'it' => 'italian',
- 'no' => 'norwegian',
- 'pt' => 'portuguese',
- 'ru' => 'russian',
- 'es' => 'spanish',
- 'sv' => 'swedish',
- );
- }
-
- function connectDb($path = null, $dbMode = null, $lang = 'english')
- {
- if ($this->db != null)
- return $this->db;
- if ($dbMode == null)
- $dbMode = Xapian::DB_CREATE_OR_OPEN;
- if ($path == null)
- $path = api_get_path(SYS_UPLOAD_PATH).'plugins/xapian/searchdb/';
- try {
- $this->db = new XapianWritableDatabase($path, $dbMode);
- $this->indexer = new XapianTermGenerator();
- if (!in_array($lang, $this->xapian_languages())) {
- $lang = 'english';
- }
- $this->stemmer = new XapianStem($lang);
- $this->indexer->set_stemmer($this->stemmer);
- return $this->db;
- } catch (Exception $e) {
- Display::display_error_message($e->getMessage());
- return 1;
- }
- }
-
- function getDb()
- {
- return $this->db;
- }
-
- function addChunk($chunk)
- {
- $this->chunks[] = $chunk;
- }
-
- function index()
- {
- try {
- if (!empty($this->chunks)) {
- foreach ($this->chunks as $chunk) {
- $doc = new XapianDocument();
- $this->indexer->set_document($doc);
- if (!empty($chunk->terms)) {
- foreach ($chunk->terms as $term) {
-
- $doc->add_term($term['flag'] . $term['name'], 1);
- }
- }
-
- if (!empty($chunk->data)) {
- foreach ($chunk->data as $key => $value) {
- $this->indexer->index_text($value, 1);
- }
- }
- $doc->set_data($chunk->xapian_data, 1);
- $did = $this->db->add_document($doc);
-
- $this->db->flush();
- return $did;
- }
- }
- } catch (Exception $e) {
- Display::display_error_message($e->getMessage());
- exit(1);
- }
- }
-
- function get_document($did)
- {
- if ($this->db == null) {
- $this->connectDb();
- }
- try {
- $docid = $this->db->get_document($did);
- } catch (Exception $e) {
-
- return false;
- }
- return $docid;
- }
-
- function get_document_data($doc)
- {
- if ($this->db == null) {
- $this->connectDb();
- }
- try {
- if (!is_a($doc, 'XapianDocument')) {
- return FALSE;
- }
- $doc_data = $doc->get_data();
- return $doc_data;
- } catch (Exception $e) {
-
- return false;
- }
- }
-
- function update_terms($did, $terms, $prefix)
- {
- $doc = $this->get_document($did);
- if ($doc === false) {
- return false;
- }
- $doc->clear_terms();
- foreach ($terms as $term) {
-
- $doc->add_term($prefix . $term, 1);
- }
- $this->db->replace_document($did, $doc);
- $this->db->flush();
- return true;
- }
-
- function remove_document($did)
- {
- if ($this->db == null) {
- $this->connectDb();
- }
- if (is_numeric($did) && $did > 0) {
- $doc = $this->get_document($did);
- if ($doc !== FALSE) {
- $this->db->delete_document($did);
- $this->db->flush();
- }
- }
- }
-
- function add_term_to_doc($term, $doc)
- {
- if (!is_a($doc, 'XapianDocument')) {
- return FALSE;
- }
- try {
- $doc->add_term($term);
- } catch (Exception $e) {
- Display::display_error_message($e->getMessage());
- return 1;
- }
- }
-
- function remove_term_from_doc($term, $doc)
- {
- if (!is_a($doc, 'XapianDocument')) {
- return FALSE;
- }
- try {
- $doc->remove_term($term);
- } catch (Exception $e) {
- Display::display_error_message($e->getMessage());
- return 1;
- }
- }
-
- function replace_document($doc, $did)
- {
- if (!is_a($doc, 'XapianDocument')) {
- return FALSE;
- }
- if ($this->db == null) {
- $this->connectDb();
- }
- try {
- $this->getDb()->replace_document((int) $did, $doc);
- $this->getDb()->flush();
- } catch (Exception $e) {
- Display::display_error_message($e->getMessage());
- return 1;
- }
- }
-
- function __destruct()
- {
- unset($this->db);
- unset($this->stemmer);
- }
- }
|