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

13
tag.php
View file

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

10
util.js
View file

@ -258,7 +258,7 @@ updatePage = function (data) {
} else { } else {
$("#sortForm").hide (); $("#sortForm").hide ();
} }
$('input[name=query]').typeahead([ $('input[name=query]').typeahead([
{ {
name: 'search', name: 'search',
@ -268,9 +268,13 @@ updatePage = function (data) {
limit: 24, limit: 24,
template: templateSuggestion, template: templateSuggestion,
remote: { 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) { 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);
} }
} }
} }