2015-08-10 19:39:05 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Implementation of search in SQlite FTS index
|
|
|
|
*
|
|
|
|
* @category DMS
|
|
|
|
* @package SeedDMS_Lucene
|
|
|
|
* @license GPL 2
|
|
|
|
* @version @version@
|
|
|
|
* @author Uwe Steinmann <uwe@steinmann.cx>
|
|
|
|
* @copyright Copyright (C) 2010, Uwe Steinmann
|
|
|
|
* @version Release: @package_version@
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for searching in a SQlite FTS index.
|
|
|
|
*
|
|
|
|
* @category DMS
|
|
|
|
* @package SeedDMS_Lucene
|
|
|
|
* @version @version@
|
|
|
|
* @author Uwe Steinmann <uwe@steinmann.cx>
|
|
|
|
* @copyright Copyright (C) 2011, Uwe Steinmann
|
|
|
|
* @version Release: @package_version@
|
|
|
|
*/
|
|
|
|
class SeedDMS_SQliteFTS_Search {
|
|
|
|
/**
|
|
|
|
* @var object $index SQlite FTS index
|
|
|
|
* @access protected
|
|
|
|
*/
|
|
|
|
protected $index;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new instance of the search
|
|
|
|
*
|
|
|
|
* @param object $index SQlite FTS index
|
|
|
|
* @return object instance of SeedDMS_SQliteFTS_Search
|
|
|
|
*/
|
|
|
|
function __construct($index) { /* {{{ */
|
|
|
|
$this->index = $index;
|
|
|
|
$this->version = '@package_version@';
|
|
|
|
if($this->version[0] == '@')
|
|
|
|
$this->version = '3.0.0';
|
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
/**
|
2020-12-12 15:27:53 +00:00
|
|
|
* Get document from index
|
2015-08-10 19:39:05 +00:00
|
|
|
*
|
2020-12-12 15:27:53 +00:00
|
|
|
* @param int $id real document id
|
|
|
|
* @return object instance of SeedDMS_SQliteFTS_QueryHit or false
|
2015-08-10 19:39:05 +00:00
|
|
|
*/
|
|
|
|
function getDocument($id) { /* {{{ */
|
2020-12-12 15:27:53 +00:00
|
|
|
$hits = $this->index->find('D'.$id);
|
|
|
|
return $hits['hits'] ? $hits['hits'][0] : false;
|
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get folder from index
|
|
|
|
*
|
|
|
|
* @param int $id real folder id
|
|
|
|
* @return object instance of SeedDMS_SQliteFTS_QueryHit or false
|
|
|
|
*/
|
|
|
|
function getFolder($id) { /* {{{ */
|
|
|
|
$hits = $this->index->find('F'.$id);
|
|
|
|
return $hits['hits'] ? $hits['hits'][0] : false;
|
2015-08-10 19:39:05 +00:00
|
|
|
} /* }}} */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search in index
|
|
|
|
*
|
|
|
|
* @param object $index SQlite FTS index
|
|
|
|
* @return object instance of SeedDMS_Lucene_Search
|
|
|
|
*/
|
2020-09-14 14:33:36 +00:00
|
|
|
function search($term, $fields=array(), $limit=array()) { /* {{{ */
|
2015-08-10 19:39:05 +00:00
|
|
|
$querystr = '';
|
2020-12-14 09:44:32 +00:00
|
|
|
$term = trim($term);
|
|
|
|
if($term) {
|
|
|
|
$querystr = substr($term, -1) != '*' ? $term.'*' : $term;
|
|
|
|
}
|
2020-09-14 07:33:01 +00:00
|
|
|
if(!empty($fields['owner'])) {
|
2020-09-12 11:40:53 +00:00
|
|
|
if(is_string($fields['owner'])) {
|
|
|
|
if($querystr)
|
2021-05-05 11:44:02 +00:00
|
|
|
$querystr .= ' AND ';
|
2020-09-12 11:40:53 +00:00
|
|
|
$querystr .= 'owner:'.$fields['owner'];
|
|
|
|
} elseif(is_array($fields['owner'])) {
|
|
|
|
if($querystr)
|
2021-05-05 11:44:02 +00:00
|
|
|
$querystr .= ' AND ';
|
2020-09-12 11:40:53 +00:00
|
|
|
$querystr .= '(owner:';
|
|
|
|
$querystr .= implode(' OR owner:', $fields['owner']);
|
|
|
|
$querystr .= ')';
|
|
|
|
}
|
2015-08-10 19:39:05 +00:00
|
|
|
}
|
2020-09-14 07:33:01 +00:00
|
|
|
if(!empty($fields['category'])) {
|
2015-08-10 19:39:05 +00:00
|
|
|
if($querystr)
|
2021-05-05 11:44:02 +00:00
|
|
|
$querystr .= ' AND ';
|
2020-09-12 11:40:53 +00:00
|
|
|
$querystr .= '(category:';
|
|
|
|
$querystr .= implode(' OR category:', $fields['category']);
|
|
|
|
$querystr .= ')';
|
2015-08-10 19:39:05 +00:00
|
|
|
}
|
2020-09-14 07:33:01 +00:00
|
|
|
if(!empty($fields['status'])) {
|
2015-08-10 19:39:05 +00:00
|
|
|
if($querystr)
|
2021-05-05 11:44:02 +00:00
|
|
|
$querystr .= ' AND ';
|
2020-09-12 11:40:53 +00:00
|
|
|
$status = array_map(function($v){return $v+10;}, $fields['status']);
|
|
|
|
$querystr .= '(status:';
|
|
|
|
$querystr .= implode(' OR status:', $status);
|
|
|
|
$querystr .= ')';
|
2015-08-10 19:39:05 +00:00
|
|
|
}
|
2020-09-14 07:33:01 +00:00
|
|
|
if(!empty($fields['user'])) {
|
2020-09-02 06:59:24 +00:00
|
|
|
if($querystr)
|
2021-05-05 11:44:02 +00:00
|
|
|
$querystr .= ' AND ';
|
2020-09-12 11:40:53 +00:00
|
|
|
$querystr .= '(users:';
|
|
|
|
$querystr .= implode(' OR users:', $fields['user']);
|
|
|
|
$querystr .= ')';
|
2020-09-02 06:59:24 +00:00
|
|
|
}
|
2020-12-17 17:33:16 +00:00
|
|
|
if(!empty($fields['rootFolder']) && $fields['rootFolder']->getFolderList()) {
|
|
|
|
if($querystr)
|
2021-05-05 11:44:02 +00:00
|
|
|
$querystr .= ' AND ';
|
2020-12-17 17:33:16 +00:00
|
|
|
$querystr .= '(path:';
|
2020-12-21 07:14:54 +00:00
|
|
|
$querystr .= str_replace(':', 'x', $fields['rootFolder']->getFolderList().$fields['rootFolder']->getID().':');
|
2020-12-17 17:33:16 +00:00
|
|
|
$querystr .= ')';
|
|
|
|
}
|
|
|
|
if(!empty($fields['startFolder']) && $fields['startFolder']->getFolderList()) {
|
|
|
|
if($querystr)
|
2021-05-05 11:44:02 +00:00
|
|
|
$querystr .= ' AND ';
|
2020-12-17 17:33:16 +00:00
|
|
|
$querystr .= '(path:';
|
2020-12-21 07:14:54 +00:00
|
|
|
$querystr .= str_replace(':', 'x', $fields['startFolder']->getFolderList().$fields['startFolder']->getID().':');
|
2020-12-17 17:33:16 +00:00
|
|
|
$querystr .= ')';
|
|
|
|
}
|
2015-08-10 19:39:05 +00:00
|
|
|
try {
|
2020-09-14 14:33:36 +00:00
|
|
|
$result = $this->index->find($querystr, $limit);
|
2015-08-10 19:39:05 +00:00
|
|
|
$recs = array();
|
2020-09-10 13:26:40 +00:00
|
|
|
foreach($result["hits"] as $hit) {
|
2020-12-12 15:27:53 +00:00
|
|
|
$recs[] = array('id'=>$hit->id, 'document_id'=>$hit->documentid);
|
2015-08-10 19:39:05 +00:00
|
|
|
}
|
2020-09-12 11:40:53 +00:00
|
|
|
return array('count'=>$result['count'], 'hits'=>$recs, 'facets'=>array());
|
2015-08-10 19:39:05 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} /* }}} */
|
|
|
|
}
|
|
|
|
?>
|