2009-01-17 00:52:50 +00:00
|
|
|
<?php defined("SYSPATH") or die("No direct script access.");
|
|
|
|
|
/**
|
|
|
|
|
* Gallery - a web based photo album viewer and editor
|
2013-01-21 01:22:01 -05:00
|
|
|
* Copyright (C) 2000-2013 Bharat Mediratta
|
2009-01-17 00:52:50 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or (at
|
|
|
|
|
* your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
class search_Core {
|
2011-03-27 13:16:41 -07:00
|
|
|
/**
|
|
|
|
|
* Add more terms to the query by wildcarding the stem value of the first
|
|
|
|
|
* few terms in the query.
|
|
|
|
|
*/
|
|
|
|
|
static function add_query_terms($q) {
|
|
|
|
|
$MAX_TERMS = 5;
|
|
|
|
|
$terms = explode(" ", $q, $MAX_TERMS);
|
|
|
|
|
for ($i = 0; $i < min(count($terms), $MAX_TERMS - 1); $i++) {
|
|
|
|
|
// Don't wildcard quoted or already wildcarded terms
|
|
|
|
|
if ((substr($terms[$i], 0, 1) != '"') && (substr($terms[$i], -1, 1) != "*")) {
|
|
|
|
|
$terms[] = rtrim($terms[$i], "s") . "*";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return implode(" ", $terms);
|
|
|
|
|
}
|
|
|
|
|
|
2009-01-17 00:52:50 +00:00
|
|
|
static function search($q, $limit, $offset) {
|
2012-12-14 17:34:26 +00:00
|
|
|
return search::search_within_album($q, item::root(), $limit, $offset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function search_within_album($q, $album, $limit, $offset) {
|
2009-01-17 00:52:50 +00:00
|
|
|
$db = Database::instance();
|
|
|
|
|
|
2012-12-14 17:34:26 +00:00
|
|
|
$query = self::_build_query_base($q, $album) .
|
2011-08-11 22:04:20 -07:00
|
|
|
"ORDER BY `score` DESC " .
|
|
|
|
|
"LIMIT $limit OFFSET " . (int)$offset;
|
|
|
|
|
|
|
|
|
|
$data = $db->query($query);
|
|
|
|
|
$count = $db->query("SELECT FOUND_ROWS() as c")->current()->c;
|
|
|
|
|
|
|
|
|
|
return array($count, new ORM_Iterator(ORM::factory("item"), $data));
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-14 17:34:26 +00:00
|
|
|
private static function _build_query_base($q, $album, $where=array()) {
|
|
|
|
|
$db = Database::instance();
|
|
|
|
|
$q = $db->escape($q);
|
|
|
|
|
|
2009-10-22 13:09:20 -07:00
|
|
|
if (!identity::active_user()->admin) {
|
|
|
|
|
foreach (identity::group_ids_for_active_user() as $id) {
|
2009-07-19 00:49:47 +02:00
|
|
|
$fields[] = "`view_$id` = TRUE"; // access::ALLOW
|
2009-01-17 00:52:50 +00:00
|
|
|
}
|
2011-10-26 12:49:45 -07:00
|
|
|
$access_sql = " AND (" . join(" OR ", $fields) . ")";
|
2009-01-17 00:52:50 +00:00
|
|
|
} else {
|
|
|
|
|
$access_sql = "";
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-14 17:34:26 +00:00
|
|
|
if ($album->id == item::root()->id) {
|
|
|
|
|
$album_sql = "";
|
|
|
|
|
} else {
|
|
|
|
|
$album_sql =
|
|
|
|
|
" AND {items}.left_ptr > " .$db->escape($album->left_ptr) .
|
|
|
|
|
" AND {items}.right_ptr <= " . $db->escape($album->right_ptr);
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-11 22:04:20 -07:00
|
|
|
return
|
2009-10-11 11:21:51 -07:00
|
|
|
"SELECT SQL_CALC_FOUND_ROWS {items}.*, " .
|
|
|
|
|
" MATCH({search_records}.`data`) AGAINST ('$q') AS `score` " .
|
2009-03-03 03:52:21 +00:00
|
|
|
"FROM {items} JOIN {search_records} ON ({items}.`id` = {search_records}.`item_id`) " .
|
|
|
|
|
"WHERE MATCH({search_records}.`data`) AGAINST ('$q' IN BOOLEAN MODE) " .
|
2012-12-14 17:34:26 +00:00
|
|
|
$album_sql .
|
2011-08-11 22:04:20 -07:00
|
|
|
(empty($where) ? "" : " AND " . join(" AND ", $where)) .
|
2012-12-14 17:34:26 +00:00
|
|
|
$access_sql .
|
|
|
|
|
" ";
|
2009-01-17 00:52:50 +00:00
|
|
|
}
|
|
|
|
|
|
2009-07-09 08:43:42 -07:00
|
|
|
/**
|
|
|
|
|
* @return string An error message suitable for inclusion in the task log
|
|
|
|
|
*/
|
2009-01-17 00:52:50 +00:00
|
|
|
static function check_index() {
|
2009-05-20 06:10:14 +00:00
|
|
|
list ($remaining) = search::stats();
|
|
|
|
|
if ($remaining) {
|
2009-01-17 00:52:50 +00:00
|
|
|
site_status::warning(
|
2009-10-04 00:27:22 -06:00
|
|
|
t('Your search index needs to be updated. <a href="%url" class="g-dialog-link">Fix this now</a>',
|
2009-08-31 00:42:18 -07:00
|
|
|
array("url" => html::mark_clean(url::site("admin/maintenance/start/search_task::update_index?csrf=__CSRF__")))),
|
2009-01-17 00:52:50 +00:00
|
|
|
"search_index_out_of_date");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-20 06:10:14 +00:00
|
|
|
static function update($item) {
|
2009-07-28 20:30:34 -07:00
|
|
|
$data = new ArrayObject();
|
2009-11-26 12:09:04 -08:00
|
|
|
$record = ORM::factory("search_record")->where("item_id", "=", $item->id)->find();
|
2009-11-25 13:22:24 -08:00
|
|
|
if (!$record->loaded()) {
|
2009-05-20 06:10:14 +00:00
|
|
|
$record->item_id = $item->id;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-31 16:07:41 -08:00
|
|
|
$item = $record->item();
|
|
|
|
|
module::event("item_index_data", $item, $data);
|
2009-07-28 20:30:34 -07:00
|
|
|
$record->data = join(" ", (array)$data);
|
2009-01-17 00:52:50 +00:00
|
|
|
$record->dirty = 0;
|
|
|
|
|
$record->save();
|
|
|
|
|
}
|
2009-05-20 06:10:14 +00:00
|
|
|
|
|
|
|
|
static function stats() {
|
2009-12-02 00:34:34 -08:00
|
|
|
$remaining = db::build()
|
2009-05-20 06:10:14 +00:00
|
|
|
->from("items")
|
|
|
|
|
->join("search_records", "items.id", "search_records.item_id", "left")
|
2009-11-26 12:09:04 -08:00
|
|
|
->and_open()
|
2009-12-02 10:02:08 -08:00
|
|
|
->where("search_records.item_id", "IS", null)
|
2009-11-26 20:25:32 -08:00
|
|
|
->or_where("search_records.dirty", "=", 1)
|
2009-11-26 12:09:04 -08:00
|
|
|
->close()
|
2009-12-02 10:02:08 -08:00
|
|
|
->count_records();
|
2009-05-26 05:28:59 +00:00
|
|
|
|
2009-05-20 06:10:14 +00:00
|
|
|
$total = ORM::factory("item")->count_all();
|
|
|
|
|
$percent = round(100 * ($total - $remaining) / $total);
|
2009-05-26 05:28:59 +00:00
|
|
|
|
2009-05-20 06:10:14 +00:00
|
|
|
return array($remaining, $total, $percent);
|
|
|
|
|
}
|
2011-08-11 22:04:20 -07:00
|
|
|
|
|
|
|
|
static function get_position($item, $q) {
|
2012-12-14 17:34:26 +00:00
|
|
|
return search::get_position_within_album($item, $q, item::root());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function get_position_within_album($item, $q, $album) {
|
2011-08-11 22:04:20 -07:00
|
|
|
$page_size = module::get_var("gallery", "page_size", 9);
|
2013-02-05 13:02:26 -05:00
|
|
|
$query = self::_build_query_base($q, $album, array("{items}.id = " . $item->id)) .
|
|
|
|
|
"ORDER BY `score` DESC ";
|
2011-08-11 22:04:20 -07:00
|
|
|
$db = Database::instance();
|
|
|
|
|
|
|
|
|
|
// Truncate the score by two decimal places as this resolves the issues
|
2013-02-05 13:02:26 -05:00
|
|
|
// that arise due to inexact numeric conversions.
|
2012-12-09 20:26:16 -08:00
|
|
|
$current = $db->query($query)->current();
|
|
|
|
|
if (!$current) {
|
|
|
|
|
// We can't find this result in our result set - perhaps we've fallen out of context? Clear
|
|
|
|
|
// the context and try again.
|
|
|
|
|
item::clear_display_context_callback();
|
|
|
|
|
url::redirect(url::current());
|
|
|
|
|
}
|
2012-12-14 17:28:13 +00:00
|
|
|
$score = $current->score;
|
2011-11-03 21:24:35 -07:00
|
|
|
if (strlen($score) > 7) {
|
|
|
|
|
$score = substr($score, 0, strlen($score) - 2);
|
|
|
|
|
}
|
2011-08-11 22:04:20 -07:00
|
|
|
|
2013-02-05 13:02:26 -05:00
|
|
|
// Redo the query but only look for results greater than or equal to our current location
|
|
|
|
|
// then seek backwards until we find our item.
|
|
|
|
|
$data = $db->query(self::_build_query_base($q, $album) . " HAVING `score` >= " . $score .
|
|
|
|
|
"ORDER BY `score` DESC ");
|
2011-08-11 22:04:20 -07:00
|
|
|
$data->seek($data->count() - 1);
|
|
|
|
|
|
2011-11-03 21:24:35 -07:00
|
|
|
while ($data->get("id") != $item->id && $data->prev()->valid()) {
|
|
|
|
|
}
|
2011-08-11 22:04:20 -07:00
|
|
|
|
2011-11-03 21:24:35 -07:00
|
|
|
return $data->key() + 1;
|
2011-08-11 22:04:20 -07:00
|
|
|
}
|
2009-01-17 00:52:50 +00:00
|
|
|
}
|