summaryrefslogtreecommitdiff
path: root/modules/search/helpers/search.php
diff options
context:
space:
mode:
Diffstat (limited to 'modules/search/helpers/search.php')
-rw-r--r--modules/search/helpers/search.php49
1 files changed, 38 insertions, 11 deletions
diff --git a/modules/search/helpers/search.php b/modules/search/helpers/search.php
index bbde8feb..b7fa21c4 100644
--- a/modules/search/helpers/search.php
+++ b/modules/search/helpers/search.php
@@ -1,7 +1,7 @@
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
- * Copyright (C) 2000-2011 Bharat Mediratta
+ * Copyright (C) 2000-2012 Bharat Mediratta
*
* 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
@@ -36,29 +36,35 @@ class search_Core {
static function search($q, $limit, $offset) {
$db = Database::instance();
- $q = $db->escape($q);
+ $query = self::_build_query_base($q) .
+ "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));
+ }
+
+ private static function _build_query_base($q, $where=array()) {
+ $q = Database::instance()->escape($q);
if (!identity::active_user()->admin) {
foreach (identity::group_ids_for_active_user() as $id) {
$fields[] = "`view_$id` = TRUE"; // access::ALLOW
}
- $access_sql = "AND (" . join(" OR ", $fields) . ")";
+ $access_sql = " AND (" . join(" OR ", $fields) . ")";
} else {
$access_sql = "";
}
- $query =
+ return
"SELECT SQL_CALC_FOUND_ROWS {items}.*, " .
" MATCH({search_records}.`data`) AGAINST ('$q') AS `score` " .
"FROM {items} JOIN {search_records} ON ({items}.`id` = {search_records}.`item_id`) " .
"WHERE MATCH({search_records}.`data`) AGAINST ('$q' IN BOOLEAN MODE) " .
- $access_sql .
- "ORDER BY `score` DESC " .
- "LIMIT $limit OFFSET $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));
+ (empty($where) ? "" : " AND " . join(" AND ", $where)) .
+ $access_sql;
}
/**
@@ -103,4 +109,25 @@ class search_Core {
return array($remaining, $total, $percent);
}
+
+ static function get_position($item, $q) {
+ $page_size = module::get_var("gallery", "page_size", 9);
+ $query = self::_build_query_base($q, array("{items}.id = " . $item->id));
+ $db = Database::instance();
+
+ // Truncate the score by two decimal places as this resolves the issues
+ // that arise due to in exact numeric conversions.
+ $score = $db->query($query)->current()->score;
+ if (strlen($score) > 7) {
+ $score = substr($score, 0, strlen($score) - 2);
+ }
+
+ $data = $db->query(self::_build_query_base($q) . " HAVING `score` >= " . $score);
+ $data->seek($data->count() - 1);
+
+ while ($data->get("id") != $item->id && $data->prev()->valid()) {
+ }
+
+ return $data->key() + 1;
+ }
}