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']; $this->subtitle = $config['cops_subtitle_default'];
if (Base::noDatabaseSelected ()) { if (Base::noDatabaseSelected ()) {
$i = 0; $i = 0;
foreach (array_keys ($config['calibre_directory']) as $key) { foreach (Base::getDbNameList () as $key) {
$nBooks = Book::getBookCount ($i); $nBooks = Book::getBookCount ($i);
array_push ($this->entryArray, new Entry ($key, "cops:{$i}:catalog", array_push ($this->entryArray, new Entry ($key, "cops:{$i}:catalog",
str_format (localize ("bookword", $nBooks), $nBooks), "text", 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 // Special case when we are doing a search and no database is selected
if (Base::noDatabaseSelected ()) { if (Base::noDatabaseSelected ()) {
$i = 0; $i = 0;
foreach ($config['calibre_directory'] as $key => $value) { foreach (Base::getDbNameList () as $key) {
Base::clearDb (); Base::clearDb ();
list ($array, $totalNumber) = Book::getBooksByQuery (array ("all" => $crit), 1, $i, 1); list ($array, $totalNumber) = Book::getBooksByQuery (array ("all" => $crit), 1, $i, 1);
array_push ($this->entryArray, new Entry ($key, DB . ":query:{$i}", array_push ($this->entryArray, new Entry ($key, DB . ":query:{$i}",
@ -904,6 +904,15 @@ abstract class Base
return array ("" => $config['calibre_directory']); return array ("" => $config['calibre_directory']);
} }
} }
public static function getDbNameList () {
global $config;
if (self::isMultipleDatabaseEnabled ()) {
return array_keys ($config['calibre_directory']);
} else {
return array ("");
}
}
public static function getDbName ($database = NULL) { public static function getDbName ($database = NULL) {
global $config; global $config;
@ -949,6 +958,17 @@ abstract class Base
} }
return self::$db; 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 () { public static function clearDb () {
self::$db = NULL; 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 // Special case when no databases were chosen, we search on all databases
if (Base::noDatabaseSelected ()) { if (Base::noDatabaseSelected ()) {
$i = 0; $i = 0;
foreach (array_keys ($config['calibre_directory']) as $key) { foreach (Base::getDbNameList () as $key) {
Base::clearDb (); Base::clearDb ();
array_push ($out, array ("title" => $key, array_push ($out, array ("title" => $key,
"class" => "tt-header", "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. // 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. // It has to be done before any header is sent.
if (Base::noDatabaseSelected ()) { Base::checkDatabaseAvailability ();
$i = 0;
foreach (array_keys ($config['calibre_directory']) as $key) {
$test = Base::getDb ($i);
Base::clearDb ();
$i++;
}
} else {
$test = Base::getDb ();
}
if ($config ['cops_fetch_protect'] == "1") { if ($config ['cops_fetch_protect'] == "1") {
session_start(); session_start();