Factorize a little more the DB handling. re #127

This commit is contained in:
Sébastien Lucas 2013-12-14 18:25:32 +01:00
parent 3df11c6ef7
commit cd626b25ef
3 changed files with 24 additions and 13 deletions

View file

@ -455,7 +455,7 @@ class Page
$this->subtitle = $config['cops_subtitle_default'];
if (Base::noDatabaseSelected ()) {
$i = 0;
foreach (array_keys ($config['calibre_directory']) as $key) {
foreach (Base::getDbNameList () as $key) {
$nBooks = Book::getBookCount ($i);
array_push ($this->entryArray, new Entry ($key, "cops:{$i}:catalog",
str_format (localize ("bookword", $nBooks), $nBooks), "text",
@ -740,7 +740,7 @@ class PageQueryResult extends Page
// Special case when we are doing a search and no database is selected
if (Base::noDatabaseSelected ()) {
$i = 0;
foreach ($config['calibre_directory'] as $key => $value) {
foreach (Base::getDbNameList () as $key) {
Base::clearDb ();
list ($array, $totalNumber) = Book::getBooksByQuery (array ("all" => $crit), 1, $i, 1);
array_push ($this->entryArray, new Entry ($key, DB . ":query:{$i}",
@ -905,6 +905,15 @@ abstract class Base
}
}
public static function getDbNameList () {
global $config;
if (self::isMultipleDatabaseEnabled ()) {
return array_keys ($config['calibre_directory']);
} else {
return array ("");
}
}
public static function getDbName ($database = NULL) {
global $config;
if (self::isMultipleDatabaseEnabled ()) {
@ -950,6 +959,17 @@ abstract class Base
return self::$db;
}
public static function checkDatabaseAvailability () {
if (self::noDatabaseSelected ()) {
for ($i = 0; $i < count (self::getDbList ()); $i++) {
self::getDb ($i);
self::clearDb ();
}
} else {
self::getDb ();
}
}
public static function clearDb () {
self::$db = NULL;
}

View file

@ -642,7 +642,7 @@ function getJson ($complete = false) {
// Special case when no databases were chosen, we search on all databases
if (Base::noDatabaseSelected ()) {
$i = 0;
foreach (array_keys ($config['calibre_directory']) as $key) {
foreach (Base::getDbNameList () as $key) {
Base::clearDb ();
array_push ($out, array ("title" => $key,
"class" => "tt-header",

View file

@ -33,16 +33,7 @@
// Access the database ASAP to be sure it's readable, redirect if that's not the case.
// It has to be done before any header is sent.
if (Base::noDatabaseSelected ()) {
$i = 0;
foreach (array_keys ($config['calibre_directory']) as $key) {
$test = Base::getDb ($i);
Base::clearDb ();
$i++;
}
} else {
$test = Base::getDb ();
}
Base::checkDatabaseAvailability ();
if ($config ['cops_fetch_protect'] == "1") {
session_start();