diff --git a/resources/epub-loader/BookExport.class.php b/resources/epub-loader/BookExport.class.php index 2fee410..19e754a 100644 --- a/resources/epub-loader/BookExport.class.php +++ b/resources/epub-loader/BookExport.class.php @@ -28,7 +28,7 @@ class BookExport { switch ($inExportType) { case self::eExportTypeCsv: - $this->mExport = new CsvExport($inFileName); + $this->mExport = new CsvExport($inFileName, $inCreate); break; default: $error = sprintf('Incorrect export type: %d', $inExportType); diff --git a/resources/epub-loader/CalibreDbLoader.class.php b/resources/epub-loader/CalibreDbLoader.class.php index c4bc2cb..3b30563 100644 --- a/resources/epub-loader/CalibreDbLoader.class.php +++ b/resources/epub-loader/CalibreDbLoader.class.php @@ -30,7 +30,7 @@ class CalibreDbLoader */ public function __construct($inDbFileName, $inCreate = false) { - if ($inCreate || !file_exists($inDbFileName)) { + if ($inCreate) { $this->CreateDatabase($inDbFileName); } else { @@ -51,7 +51,7 @@ class CalibreDbLoader // Read the sql file $content = file_get_contents(CalibreCreateDbSql); if ($content === false) { - $error = sprintf('Cannot read sql file: %s', $inDbFileName); + $error = sprintf('Cannot read sql file: %s', CalibreCreateDbSql); throw new Exception($error); } diff --git a/resources/epub-loader/CsvExport.class.php b/resources/epub-loader/CsvExport.class.php index be31b4e..595d699 100644 --- a/resources/epub-loader/CsvExport.class.php +++ b/resources/epub-loader/CsvExport.class.php @@ -20,7 +20,7 @@ class CsvExport extends BaseExport * @param string Export file name * @param boolean Force file creation */ - public function __construct($inFileName) + public function __construct($inFileName, $inCreate = false) { $this->mSearch = array("\r", "\n", self::CsvSeparator); $this->mReplace = array('', '
', ''); @@ -28,7 +28,7 @@ class CsvExport extends BaseExport // Init container $this->mLines = array(); - parent::__construct($inFileName); + parent::__construct($inFileName, $inCreate); } /** diff --git a/resources/epub-loader/app/action_cvs_export.php b/resources/epub-loader/app/action_cvs_export.php new file mode 100644 index 0000000..ac5c707 --- /dev/null +++ b/resources/epub-loader/app/action_cvs_export.php @@ -0,0 +1,30 @@ + + */ + +foreach ($gConfig['databases'] as $dbConfig) { + // Init csv file + $fileName = $dbConfig['db_path'] . DIRECTORY_SEPARATOR . basename($dbConfig['db_path']) . '_metadata.csv'; + try { + // Open or create the export file + $export = new BookExport($fileName, BookExport::eExportTypeCsv, true); + echo sprintf('Export ebooks to %s', $fileName) . '
'; + // Add the epub files into the export file + if (!empty($dbConfig['epub_path'])) { + $fileList = glob($dbConfig['epub_path'] . DIRECTORY_SEPARATOR . '*.epub'); + foreach ($fileList as $fileName) { + $export->AddEpub($fileName); + } + } + $export->SaveToFile(); + } + catch (Exception $e) { + $gErrorArray[$fileName] = $e->getMessage(); + } +} + +?> diff --git a/resources/epub-loader/app/action_db_load.php b/resources/epub-loader/app/action_db_load.php new file mode 100644 index 0000000..6f46b41 --- /dev/null +++ b/resources/epub-loader/app/action_db_load.php @@ -0,0 +1,29 @@ + + */ + +foreach ($gConfig['databases'] as $dbConfig) { + // Init database file + $fileName = $dbConfig['db_path'] . DIRECTORY_SEPARATOR . 'metadata.db'; + try { + // Open or create the database + $db = new CalibreDbLoader($fileName, $gConfig['create_db']); + echo sprintf('Load database %s', $fileName) . '
'; + // Add the epub files into the database + if (!empty($dbConfig['epub_path'])) { + $fileList = glob($dbConfig['epub_path'] . DIRECTORY_SEPARATOR . '*.epub'); + foreach ($fileList as $fileName) { + $db->AddEpub($fileName); + } + } + } + catch (Exception $e) { + $gErrorArray[$fileName] = $e->getMessage(); + } +} + +?> diff --git a/resources/epub-loader/app/config.php b/resources/epub-loader/app/config.php new file mode 100644 index 0000000..15d2cb0 --- /dev/null +++ b/resources/epub-loader/app/config.php @@ -0,0 +1,45 @@ + + */ + +$gConfig = array(); + +/** + * Cops directory + * + * This is the base path of Cops library + */ +$gConfig['cops_directory'] = dirname(dirname(dirname(__DIR__))); + +/** + * Create Calibre databases ? + * + * If true: databases are removed and recreated before loading ebooks + * If false: append ebooks into databases + */ +$gConfig['create_db'] = true; + +/** + * Databases infos + * + * For each database: + * name: The database name to display + * db_path: The path where to create the database + * epub_path: The path where to look for the epub files to load + */ +$gConfig['databases'] = array(); +$gConfig['databases'][] = array('name' => 'Littérature classique', 'db_path' => '/opt/atoll/opds/demo', 'epub_path' => '/opt/atoll/external/demo'); +$gConfig['databases'][] = array('name' => 'Bibliothèque numérique romande', 'db_path' => '/opt/atoll/opds/bnr', 'epub_path' => '/opt/atoll/external/bnr'); +$gConfig['databases'][] = array('name' => 'La Bibliothèque d\'Ebooks', 'db_path' => '/opt/atoll/opds/bibebook', 'epub_path' => '/opt/atoll/external/bibebook'); +$gConfig['databases'][] = array('name' => 'La Bibliothèque électronique du Québec', 'db_path' => '/opt/atoll/opds/beq', 'epub_path' => '/opt/atoll/external/beq'); + +/** + * Available actions + */ +$gConfig['actions'] = array(); +$gConfig['actions']['cvs_export'] = 'Cvs export'; +$gConfig['actions']['db_load'] = 'Calibre database load'; diff --git a/resources/epub-loader/app/footer.php b/resources/epub-loader/app/footer.php new file mode 100644 index 0000000..43eab64 --- /dev/null +++ b/resources/epub-loader/app/footer.php @@ -0,0 +1,41 @@ + + + +' . "\n"; + $str .= '
' . "\n"; + $title = 'Errors (' . count($gErrorArray) . ')'; + $str .= ' ' . "\n"; + $str .= ' ' . "\n"; + $str .= ' ' . "\n"; + $str .= ' ' . "\n"; + foreach ($gErrorArray as $fileName => $error) { + // Display error + $str .= ' ' . "\n"; + $str .= ' ' . "\n"; + $str .= ' ' . "\n"; + $str .= ' ' . "\n"; + } + $str .= '
' . $title . '
' . $fileName . '' . $error . '
' . "\n"; + $str .= '
' . "\n"; + $str .= ' ' . "\n"; + echo $str; + } +?> + + + + + + + + diff --git a/resources/epub-loader/app/header.php b/resources/epub-loader/app/header.php new file mode 100644 index 0000000..47e0d47 --- /dev/null +++ b/resources/epub-loader/app/header.php @@ -0,0 +1,89 @@ + + + + + <?php echo DEF_AppName; ?> + + + + + + + +
+' . DEF_AppName . '' . "\n"; ?> +
+ + + +
diff --git a/resources/epub-loader/app/index.php b/resources/epub-loader/app/index.php new file mode 100644 index 0000000..8550ef0 --- /dev/null +++ b/resources/epub-loader/app/index.php @@ -0,0 +1,56 @@ + + */ + +//------------------------------------------------------------------------------ +// Global defines +//------------------------------------------------------------------------------ + +// Application version +define('DEF_AppVersion', '1.0'); +// Application name +define('DEF_AppName', 'epub loader'); +// Admin email +define('DEF_AppAdminMail', 'didier.corbiere@opale-concept.com'); + + +//------------------------------------------------------------------------------ +// Include files +//------------------------------------------------------------------------------ + +require_once(__DIR__ . DIRECTORY_SEPARATOR . 'config.php'); +require_once($gConfig['cops_directory'] . '/resources/epub-loader/CalibreDbLoader.class.php'); +require_once($gConfig['cops_directory'] . '/resources/epub-loader/BookExport.class.php'); + +//------------------------------------------------------------------------------ +// Start application +//------------------------------------------------------------------------------ + +// Global vars +$gErrorArray = array(); + +// Get the action to execute +$action = isset($_GET['action']) ? $_GET['action'] : null; + +require_once(__DIR__ . DIRECTORY_SEPARATOR . 'header.php'); +if (isset($action)) { + $phpFile = sprintf('%s%saction_%s.php', realpath(dirname(__FILE__)), DIRECTORY_SEPARATOR, $action); + require_once($phpFile); +} +else { + // Display the available actions + echo ' ' . "\n"; +} +require_once(__DIR__ . DIRECTORY_SEPARATOR . 'footer.php'); + +?>