Handle better search of multiple databases at once. Optimize a little typeahead search. re #34

This commit is contained in:
Sébastien Lucas 2013-11-22 14:41:56 +01:00
parent caa46c50e3
commit 58c48a71a8
3 changed files with 53 additions and 22 deletions

View File

@ -553,12 +553,12 @@ order by substr (upper (sort), 1, 1)");
return $entryArray;
}
public static function getBooksByStartingLetter($letter, $n) {
return self::getEntryArray (self::SQL_BOOKS_BY_FIRST_LETTER, array ($letter . "%"), $n);
public static function getBooksByStartingLetter($letter, $n, $database = NULL, $numberPerPage = NULL) {
return self::getEntryArray (self::SQL_BOOKS_BY_FIRST_LETTER, array ($letter . "%"), $n, $database, $numberPerPage);
}
public static function getEntryArray ($query, $params, $n, $database = NULL) {
list ($totalNumber, $result) = parent::executeQuery ($query, self::BOOK_COLUMNS, self::getFilterString (), $params, $n, $database);
public static function getEntryArray ($query, $params, $n, $database = NULL, $numberPerPage = NULL) {
list ($totalNumber, $result) = parent::executeQuery ($query, self::BOOK_COLUMNS, self::getFilterString (), $params, $n, $database, $numberPerPage);
$entryArray = array();
while ($post = $result->fetchObject ())
{
@ -582,30 +582,59 @@ function getJson ($complete = false) {
$page = getURLParam ("page", Base::PAGE_INDEX);
$query = getURLParam ("query");
$search = getURLParam ("search");
$multi = getURLParam ("multi");
$qid = getURLParam ("id");
$n = getURLParam ("n", "1");
$database = GetUrlParam (DB);
if ($search) {
$out = array ();
$arrayTag = Tag::getAllTagsByQuery ($query);
$arraySeries = Serie::getAllSeriesByQuery ($query);
$arrayAuthor = Author::getAuthorsByStartingLetter ('%' . $query);
list ($arrayBook, $totalNumber) = Book::getBooksByStartingLetter ('%' . $query, -1);
$pagequery = Base::PAGE_OPENSEARCH_QUERY;
if (is_array ($config['calibre_directory']) && $multi === "1") {
$i = 0;
foreach ($config['calibre_directory'] as $key => $value) {
Base::clearDb ();
array_push ($out, array ("title" => $key,
"class" => "tt-header",
"navlink" => "index.php?db={$i}"));
list ($array, $total) = Book::getBooksByStartingLetter ('%' . $query, 1, $i, 5);
array_push ($out, array ("title" => str_format (localize("bookword", $total), $total),
"class" => "",
"navlink" => "index.php?page={$pagequery}&query={$query}&db={$i}&scope=book"));
$i++;
}
return $out;
}
$arrayTag = Tag::getAllTagsByQuery ($query, 1, NULL, 5);
$arraySeries = Serie::getAllSeriesByQuery ($query);
$arrayAuthor = Author::getAuthorsByStartingLetter ('%' . $query);
$arrayBook = Book::getBooksByStartingLetter ('%' . $query, 1, NULL, 5);
foreach (array ("book" => $arrayBook,
"author" => $arrayAuthor,
"series" => $arraySeries,
"tag" => $arrayTag) as $key => $array) {
$i = 0;
$pagequery = Base::PAGE_OPENSEARCH_QUERY;
if (count($array) > 0) {
if (count ($array) == 2 && is_array ($array [0])) {
$total = $array [1];
$array = $array [0];
} else {
$total = count($array);
}
if ($total > 0) {
// Comment to help the perl i18n script
// str_format (localize("bookword", count($array))
// str_format (localize("authorword", count($array)
// str_format (localize("seriesword", count($array)
// str_format (localize("tagword", count($array)
array_push ($out, array ("title" => str_format (localize("{$key}word", count($array)), count($array)),
array_push ($out, array ("title" => str_format (localize("{$key}word", $total), $total),
"class" => "tt-header",
"navlink" => "index.php?page={$pagequery}&query={$query}&db={$database}&scope={$key}"));
}
@ -636,6 +665,7 @@ function getJson ($complete = false) {
$out ["databaseId"] = GetUrlParam (DB, "");
$out ["databaseName"] = Base::getDbName ();
$out ["page"] = $page;
$out ["multipleDatabase"] = Base::isMultipleDatabaseEnabled () ? 1 : 0;
$out ["entries"] = $entries;
$out ["isPaginated"] = 0;
if ($currentPage->isPaginated ()) {

13
tag.php
View File

@ -62,14 +62,11 @@ order by tags.name');
return $entryArray;
}
public static function getAllTagsByQuery($query) {
$result = parent::getDb ()->prepare('select tags.id as id, tags.name as name, count(*) as count
from tags, books_tags_link
where tags.id = tag and tags.name like ?
group by tags.id, tags.name
order by tags.name');
public static function getAllTagsByQuery($query, $n, $database = NULL, $numberPerPage = NULL) {
$columns = "tags.id as id, tags.name as name, (select count(*) from books_tags_link where tags.id = tag) as count";
$sql = 'select {0} from tags where tags.name like ? {1} order by tags.name';
list ($totalNumber, $result) = parent::executeQuery ($sql, $columns, "", array ('%' . $query . '%'), $n, $database, $numberPerPage);
$entryArray = array();
$result->execute (array ('%' . $query . '%'));
while ($post = $result->fetchObject ())
{
$tag = new Tag ($post->id, $post->name);
@ -77,6 +74,6 @@ order by tags.name');
str_format (localize("bookword", $post->count), $post->count), "text",
array ( new LinkNavigation ($tag->getUri ()))));
}
return $entryArray;
return array ($entryArray, $totalNumber);
}
}

10
util.js
View File

@ -258,7 +258,7 @@ updatePage = function (data) {
} else {
$("#sortForm").hide ();
}
$('input[name=query]').typeahead([
{
name: 'search',
@ -268,9 +268,13 @@ updatePage = function (data) {
limit: 24,
template: templateSuggestion,
remote: {
url: 'getJSON.php?search=1&db=%DB&query=%QUERY',
url: 'getJSON.php?search=1&db=%DB&query=%QUERY&multi=%MULTI',
replace: function (url, query) {
return url.replace('%QUERY', query).replace('%DB', currentData.databaseId);
var multi = 0;
if (currentData.multipleDatabase === 1 && currentData.databaseId === "") {
multi = 1;
}
return url.replace('%QUERY', query).replace('%DB', currentData.databaseId).replace ("%MULTI", multi);
}
}
}