diff --git a/base.php b/base.php index 484416a..09d312f 100644 --- a/base.php +++ b/base.php @@ -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}", @@ -904,6 +904,15 @@ abstract class Base 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) { global $config; @@ -949,6 +958,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; diff --git a/book.php b/book.php index 7ab3955..2eb59dd 100644 --- a/book.php +++ b/book.php @@ -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", diff --git a/index.php b/index.php index c162d09..2b68608 100644 --- a/index.php +++ b/index.php @@ -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();