From e6de4593be6e385ed0c4a53554793506b6efab07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Didier=20Corbi=C3=A8re?= Date: Tue, 17 Sep 2013 01:08:23 +0100 Subject: [PATCH 01/19] Add series --- resources/epub-loader/BookExport.class.php | 4 ++ resources/epub-loader/BookInfos.class.php | 29 +++++---- .../epub-loader/CalibreDbLoader.class.php | 62 ++++++++++++++++--- 3 files changed, 75 insertions(+), 20 deletions(-) diff --git a/resources/epub-loader/BookExport.class.php b/resources/epub-loader/BookExport.class.php index b53e553..2fee410 100644 --- a/resources/epub-loader/BookExport.class.php +++ b/resources/epub-loader/BookExport.class.php @@ -81,6 +81,8 @@ class BookExport $this->mExport->SetProperty($i++, 'Isbn'); $this->mExport->SetProperty($i++, 'Rights'); $this->mExport->SetProperty($i++, 'Publisher'); + $this->mExport->SetProperty($i++, 'Serie'); + $this->mExport->SetProperty($i++, 'SerieIndex'); $this->mExport->AddContent(); } @@ -101,6 +103,8 @@ class BookExport $this->mExport->SetProperty($i++, $inBookInfo->mIsbn); $this->mExport->SetProperty($i++, $inBookInfo->mRights); $this->mExport->SetProperty($i++, $inBookInfo->mPublisher); + $this->mExport->SetProperty($i++, $inBookInfo->mSerie); + $this->mExport->SetProperty($i++, $inBookInfo->mSerieIndex); $this->mExport->AddContent(); } diff --git a/resources/epub-loader/BookInfos.class.php b/resources/epub-loader/BookInfos.class.php index bda5003..fcfd75d 100644 --- a/resources/epub-loader/BookInfos.class.php +++ b/resources/epub-loader/BookInfos.class.php @@ -29,6 +29,8 @@ class BookInfos public $mIsbn = ''; public $mRights = ''; public $mPublisher = ''; + public $mSerie = ''; + public $mSerieIndex = ''; /** * Loads book infos from an epub file @@ -41,23 +43,26 @@ class BookInfos public function LoadFromEpub($inFileName) { // Load the epub file - $epub = new EPub($inFileName, 'ZipFile'); + $ePub = new EPub($inFileName, 'ZipFile'); // Get the epub infos $this->mFormat = 'epub'; $this->mPath = pathinfo($inFileName, PATHINFO_DIRNAME); $this->mName = pathinfo($inFileName, PATHINFO_FILENAME); - $this->mUuid = $epub->Uuid(); - $this->mUri = $epub->Uri(); - $this->mTitle = $epub->Title(); - $this->mAuthors = $epub->Authors(); - $this->mLanguage = $epub->Language(); - $this->mDescription = $epub->Description(); - $this->mSubjects = $epub->Subjects(); - $this->mCover = $epub->getCoverItem(); - $this->mIsbn = $epub->ISBN(); - $this->mRights = $epub->Copyright(); - $this->mPublisher = $epub->Publisher(); + $this->mUuid = $ePub->Uuid(); + $this->mUri = $ePub->Uri(); + $this->mTitle = $ePub->Title(); + $this->mAuthors = $ePub->Authors(); + $this->mLanguage = $ePub->Language(); + $this->mDescription = $ePub->Description(); + $this->mSubjects = $ePub->Subjects(); + $cover = $ePub->Cover(); + $this->mCover = ($cover['found'] !== false) ? $cover['found'] : ''; + $this->mIsbn = $ePub->ISBN(); + $this->mRights = $ePub->Copyright(); + $this->mPublisher = $ePub->Publisher(); + $this->mSerie = $ePub->Serie(); + $this->mSerieIndex = $ePub->SerieIndex(); } } diff --git a/resources/epub-loader/CalibreDbLoader.class.php b/resources/epub-loader/CalibreDbLoader.class.php index 1630f5d..c4bc2cb 100644 --- a/resources/epub-loader/CalibreDbLoader.class.php +++ b/resources/epub-loader/CalibreDbLoader.class.php @@ -141,10 +141,11 @@ class CalibreDbLoader */ private function AddBook($inBookInfo) { - $sql = 'insert into books(title, sort, uuid, path) values(:title, :sort, :uuid, :path)'; + $sql = 'insert into books(title, sort, series_index, uuid, path) values(:title, :sort, :serieindex, :uuid, :path)'; $stmt = $this->mDb->prepare($sql); $stmt->bindParam(':title', $inBookInfo->mTitle); $stmt->bindParam(':sort', $inBookInfo->mTitle); + $stmt->bindParam(':serieindex', $inBookInfo->mSerieIndex); $stmt->bindParam(':uuid', $inBookInfo->mUuid); $stmt->bindParam(':path', $inBookInfo->mPath); $stmt->execute(); @@ -184,7 +185,52 @@ class CalibreDbLoader $stmt->bindParam(':value', $inBookInfo->mUri); $stmt->execute(); } - // Add the authors in the db + // Add the book serie + if (!empty($inBookInfo->mSerie)) { + // Get the serie id + $sql = 'select id from series where name=:serie'; + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':serie', $inBookInfo->mSerie); + $stmt->execute(); + $post = $stmt->fetchObject(); + if ($post) { + $idSerie = $post->id; + } + else { + // Add a new serie + $sql = 'insert into series(name, sort) values(:serie, :sort)'; + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':serie', $inBookInfo->mSerie); + $stmt->bindParam(':sort', $inBookInfo->mSerie); + $stmt->execute(); + // Get the serie id + $sql = 'select id from series where name=:serie'; + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':serie', $inBookInfo->mSerie); + $stmt->execute(); + $idSerie = null; + while ($post = $stmt->fetchObject()) { + if (!isset($idSerie)) { + $idSerie = $post->id; + } + else { + $error = sprintf('Multiple series for name: %s', $inBookInfo->mSerie); + throw new Exception($error); + } + } + if (!isset($idSerie)) { + $error = sprintf('Cannot find serie id for name: %s', $inBookInfo->mSerie); + throw new Exception($error); + } + } + // Add the book serie link + $sql = 'insert into books_series_link(book, series) values(:idBook, :idSerie)'; + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':idBook', $idBook, PDO::PARAM_INT); + $stmt->bindParam(':idSerie', $idSerie, PDO::PARAM_INT); + $stmt->execute(); + } + // Add the book authors foreach ($inBookInfo->mAuthors as $authorSort => $author) { // Get the author id $sql = 'select id from authors where name=:author'; @@ -221,13 +267,13 @@ class CalibreDbLoader $error = sprintf('Cannot find author id for name: %s', $author); throw new Exception($error); } - // Add the book author link - $sql = 'insert into books_authors_link(book, author) values(:idBook, :idAuthor)'; - $stmt = $this->mDb->prepare($sql); - $stmt->bindParam(':idBook', $idBook, PDO::PARAM_INT); - $stmt->bindParam(':idAuthor', $idAuthor, PDO::PARAM_INT); - $stmt->execute(); } + // Add the book author link + $sql = 'insert into books_authors_link(book, author) values(:idBook, :idAuthor)'; + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':idBook', $idBook, PDO::PARAM_INT); + $stmt->bindParam(':idAuthor', $idAuthor, PDO::PARAM_INT); + $stmt->execute(); } } From 46d993d168359d448904e093d4be17968bc5a76f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Didier=20Corbi=C3=A8re?= Date: Tue, 17 Sep 2013 05:48:45 +0100 Subject: [PATCH 04/19] Add base path --- config.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.php b/config.php index ba70f9b..94df587 100644 --- a/config.php +++ b/config.php @@ -7,5 +7,5 @@ */ require_once 'config_default.php'; - if (file_exists('config_local.php')) + if (file_exists(dirname(__FILE__). '/config_local.php')) require_once 'config_local.php'; From 8d5109d7de915c35bd92d6b2ce84036d06f59ead Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Didier=20Corbi=C3=A8re?= Date: Wed, 25 Sep 2013 02:03:52 +0100 Subject: [PATCH 05/19] Add basic application in order to perform the tasks --- resources/epub-loader/BookExport.class.php | 2 +- .../epub-loader/CalibreDbLoader.class.php | 4 +- resources/epub-loader/CsvExport.class.php | 4 +- .../epub-loader/app/action_cvs_export.php | 30 +++++++ resources/epub-loader/app/action_db_load.php | 29 ++++++ resources/epub-loader/app/config.php | 45 ++++++++++ resources/epub-loader/app/footer.php | 41 +++++++++ resources/epub-loader/app/header.php | 89 +++++++++++++++++++ resources/epub-loader/app/index.php | 56 ++++++++++++ 9 files changed, 295 insertions(+), 5 deletions(-) create mode 100644 resources/epub-loader/app/action_cvs_export.php create mode 100644 resources/epub-loader/app/action_db_load.php create mode 100644 resources/epub-loader/app/config.php create mode 100644 resources/epub-loader/app/footer.php create mode 100644 resources/epub-loader/app/header.php create mode 100644 resources/epub-loader/app/index.php 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"; + foreach ($gConfig['actions'] as $action => $actionInfo) { + echo '
  • ' . "\n"; + echo ' ' . $actionInfo . '' . "\n"; + echo '
  • ' . "\n"; + } + echo '
' . "\n"; +} +require_once(__DIR__ . DIRECTORY_SEPARATOR . 'footer.php'); + +?> From 60a10f517b4d4f4b99dd4025495461d81a420bb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Didier=20Corbi=C3=A8re?= Date: Wed, 25 Sep 2013 02:13:44 +0100 Subject: [PATCH 06/19] Add readme file --- resources/epub-loader/README | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 resources/epub-loader/README diff --git a/resources/epub-loader/README b/resources/epub-loader/README new file mode 100644 index 0000000..550474e --- /dev/null +++ b/resources/epub-loader/README @@ -0,0 +1,9 @@ +## ============================================================================= +## epub-loader readme +## ============================================================================= + +epub-loader is a utility ressource for ebooks. + +- CalibreDbLoader class allows create Calibre databases and add ebooks +- BookExport class allows to export ebooks metadata in csv files +- The app directory contains samples and allows to run actions From 96e254c10ee62ca04fab3ad91d20bb5efb9099ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Didier=20Corbi=C3=A8re?= Date: Wed, 25 Sep 2013 03:39:18 +0100 Subject: [PATCH 07/19] Rename config file --- resources/epub-loader/app/config.php.example | 45 ++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 resources/epub-loader/app/config.php.example diff --git a/resources/epub-loader/app/config.php.example b/resources/epub-loader/app/config.php.example new file mode 100644 index 0000000..15d2cb0 --- /dev/null +++ b/resources/epub-loader/app/config.php.example @@ -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'; From acd9b72097406dbf0853e10460d7533dcb0bd628 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Didier=20Corbi=C3=A8re?= Date: Wed, 25 Sep 2013 03:40:01 +0100 Subject: [PATCH 08/19] Rename config file --- resources/epub-loader/app/config.php | 45 ---------------------------- 1 file changed, 45 deletions(-) delete mode 100644 resources/epub-loader/app/config.php diff --git a/resources/epub-loader/app/config.php b/resources/epub-loader/app/config.php deleted file mode 100644 index 15d2cb0..0000000 --- a/resources/epub-loader/app/config.php +++ /dev/null @@ -1,45 +0,0 @@ - - */ - -$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'; From 1acce6509c623701d2c9f22c03d524ad3a97e4c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Didier=20Corbi=C3=A8re?= Date: Wed, 25 Sep 2013 07:28:20 +0100 Subject: [PATCH 09/19] Init actions and db --- resources/epub-loader/README | 8 ++ .../epub-loader/app/action_cvs_export.php | 32 +++---- resources/epub-loader/app/action_db_load.php | 30 +++--- resources/epub-loader/app/config.php.example | 2 +- resources/epub-loader/app/header.php | 8 +- resources/epub-loader/app/index.php | 96 ++++++++++++++++--- 6 files changed, 124 insertions(+), 52 deletions(-) diff --git a/resources/epub-loader/README b/resources/epub-loader/README index 550474e..cde011a 100644 --- a/resources/epub-loader/README +++ b/resources/epub-loader/README @@ -7,3 +7,11 @@ epub-loader is a utility ressource for ebooks. - CalibreDbLoader class allows create Calibre databases and add ebooks - BookExport class allows to export ebooks metadata in csv files - The app directory contains samples and allows to run actions + + +## Installation +## ----------------------------------------------------------------------------- + +- If a first-time install, copy app/config.php.example to app/config.php +- Edit config.php to match your config +- Open the app directory url diff --git a/resources/epub-loader/app/action_cvs_export.php b/resources/epub-loader/app/action_cvs_export.php index ac5c707..aab698d 100644 --- a/resources/epub-loader/app/action_cvs_export.php +++ b/resources/epub-loader/app/action_cvs_export.php @@ -6,25 +6,23 @@ * @author Didier Corbière */ -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); - } +// 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(); } + $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 index 6f46b41..73f57e3 100644 --- a/resources/epub-loader/app/action_db_load.php +++ b/resources/epub-loader/app/action_db_load.php @@ -6,24 +6,22 @@ * @author Didier Corbière */ -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); - } +// 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(); - } +} +catch (Exception $e) { + $gErrorArray[$fileName] = $e->getMessage(); } ?> diff --git a/resources/epub-loader/app/config.php.example b/resources/epub-loader/app/config.php.example index 15d2cb0..a7e7e7f 100644 --- a/resources/epub-loader/app/config.php.example +++ b/resources/epub-loader/app/config.php.example @@ -42,4 +42,4 @@ $gConfig['databases'][] = array('name' => 'La Bibliothèque électronique du Qu */ $gConfig['actions'] = array(); $gConfig['actions']['cvs_export'] = 'Cvs export'; -$gConfig['actions']['db_load'] = 'Calibre database load'; +$gConfig['actions']['db_load'] = 'Create database'; diff --git a/resources/epub-loader/app/header.php b/resources/epub-loader/app/header.php index 47e0d47..2fb83dd 100644 --- a/resources/epub-loader/app/header.php +++ b/resources/epub-loader/app/header.php @@ -14,12 +14,12 @@ header('Content-type: text/html; charset=utf-8'); body { font-family: "Times New Roman", Times, serif; background-color: #ffffff; - color: #2F4769; + color: #333333; margin: 20px; } a { - color: #CC9966; - text-decoration: none; + color: #096DD1; + text-decoration: none; } a:hover { color: #DF7800; @@ -70,7 +70,7 @@ header('Content-type: text/html; charset=utf-8'); .footer { border-top: solid #2F4769 1px; margin-top: 15px; - padding-top: 5px; + padding-top: 10px; font-size: 80%; font-style: italic; } diff --git a/resources/epub-loader/app/index.php b/resources/epub-loader/app/index.php index 8550ef0..a357bb7 100644 --- a/resources/epub-loader/app/index.php +++ b/resources/epub-loader/app/index.php @@ -22,9 +22,31 @@ 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'); +// Include config file +$fileName = __DIR__ . DIRECTORY_SEPARATOR . 'config.php'; +if (!file_exists($fileName)) { + die ('Missing configuration file: ' . $fileName); +} +require_once($fileName); + +// Check Cops directory +if (!is_dir($gConfig['cops_directory'])) { + die ('Incorrect Cops directory: ' . $gConfig['cops_directory']); +} + +// Include Calibre database loader class +$fileName = $gConfig['cops_directory'] . '/resources/epub-loader/CalibreDbLoader.class.php'; +if (!file_exists($fileName)) { + die ('Incorrect include file: ' . $gConfig['cops_directory']); +} +require_once($fileName); + +// Include book export class +$fileName = $gConfig['cops_directory'] . '/resources/epub-loader/BookExport.class.php'; +if (!file_exists($fileName)) { + die ('Incorrect include file: ' . $gConfig['cops_directory']); +} +require_once($fileName); //------------------------------------------------------------------------------ // Start application @@ -33,24 +55,70 @@ require_once($gConfig['cops_directory'] . '/resources/epub-loader/BookExport.cla // Global vars $gErrorArray = array(); -// Get the action to execute +// Get the url parameters $action = isset($_GET['action']) ? $_GET['action'] : null; +$dbNum = isset($_GET['dbnum']) ? (int)$_GET['dbnum'] : null; +// Include html header 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); + +// Html content +if (isset($action) && isset($dbNum)) { + if (!isset($gConfig['databases'][$dbNum])) { + die ('Incorrect database num: ' . $dbNum); + } + $dbConfig = $gConfig['databases'][$dbNum]; + $fileName = sprintf('%s%saction_%s.php', __DIR__, DIRECTORY_SEPARATOR, $action); + if (!file_exists($fileName)) { + die ('Incorrect action file: ' . $fileName); + } + require_once($fileName); } else { - // Display the available actions - echo '
    ' . "\n"; - foreach ($gConfig['actions'] as $action => $actionInfo) { - echo '
  • ' . "\n"; - echo ' ' . $actionInfo . '' . "\n"; - echo '
  • ' . "\n"; + if (!isset($action)) { + // Display the available actions + $str = ''; + $str .= '
    ' . 'Select action' . '
    ' . "\n"; + $str .= '
      ' . "\n"; + foreach ($gConfig['actions'] as $action => $actionInfo) { + $str .= '
    • ' . "\n"; + $str .= ' ' . $actionInfo . '' . "\n"; + $str .= '
    • ' . "\n"; + } + $str .= '
    ' . "\n"; + echo $str; + } + else { + // Display databases + $str = ''; + $str .= '' . "\n"; + $str .= '' . "\n"; + $str .= '' . "\n"; + $str .= '' . "\n"; + $str .= '' . "\n"; + $str .= '' . "\n"; + $str .= '' . "\n"; + $str .= '' . "\n"; + $str .= '' . "\n"; + $actionTitle = $gConfig['actions'][$action]; + foreach ($gConfig['databases'] as $dbNum => $dbConfig) { + $fileList = glob($dbConfig['epub_path'] . DIRECTORY_SEPARATOR . '*.epub'); + $str .= '' . "\n"; + $str .= '' . "\n"; + $str .= '' . "\n"; + $str .= '' . "\n"; + $str .= '' . "\n"; + $str .= '' . "\n"; + $str .= '' . "\n"; + $str .= '' . "\n"; + $numWork++; + } + $str .= '
    ' . 'Db num' . '' . 'Db name' . '' . 'Action' . '' . 'Db Path' . '' . 'Epub path' . '' . 'Nb Files' . '
    ' . $dbNum . '' . $dbConfig['name'] . '' . '' . $actionTitle . '' . '' . $dbConfig['db_path'] . '' . $dbConfig['epub_path'] . '' . count($fileList) . '
    ' . "\n"; + echo $str; } - echo '
' . "\n"; } + +// Include html footer require_once(__DIR__ . DIRECTORY_SEPARATOR . 'footer.php'); ?> From 6964445c8f32abaca0821ed0a5bfb03b0449ce81 Mon Sep 17 00:00:00 2001 From: Mars Date: Wed, 25 Sep 2013 08:46:11 +0200 Subject: [PATCH 10/19] Ignore file --- resources/epub-loader/app/.gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 resources/epub-loader/app/.gitignore diff --git a/resources/epub-loader/app/.gitignore b/resources/epub-loader/app/.gitignore new file mode 100644 index 0000000..4e9b47a --- /dev/null +++ b/resources/epub-loader/app/.gitignore @@ -0,0 +1 @@ +config.php \ No newline at end of file From 89a27f10b865e121ce16e25061a2908f0b2ef093 Mon Sep 17 00:00:00 2001 From: Marsender Date: Mon, 30 Sep 2013 22:29:27 +0100 Subject: [PATCH 11/19] Some comments and config changes --- resources/epub-loader/app/action_cvs_export.php | 2 +- resources/epub-loader/app/action_db_load.php | 2 +- resources/epub-loader/app/config.php.example | 11 ++++++----- resources/epub-loader/app/header.php | 1 - resources/epub-loader/app/index.php | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/resources/epub-loader/app/action_cvs_export.php b/resources/epub-loader/app/action_cvs_export.php index aab698d..0287928 100644 --- a/resources/epub-loader/app/action_cvs_export.php +++ b/resources/epub-loader/app/action_cvs_export.php @@ -1,6 +1,6 @@ diff --git a/resources/epub-loader/app/action_db_load.php b/resources/epub-loader/app/action_db_load.php index 73f57e3..22b9303 100644 --- a/resources/epub-loader/app/action_db_load.php +++ b/resources/epub-loader/app/action_db_load.php @@ -1,6 +1,6 @@ diff --git a/resources/epub-loader/app/config.php.example b/resources/epub-loader/app/config.php.example index a7e7e7f..802b1ca 100644 --- a/resources/epub-loader/app/config.php.example +++ b/resources/epub-loader/app/config.php.example @@ -1,6 +1,6 @@ @@ -32,10 +32,9 @@ $gConfig['create_db'] = true; * 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'); +$gConfig['databases'][] = array('name' => 'Littérature classique', 'db_path' => '/opt/ebooks/calibre/demo', 'epub_path' => '/opt/ebooks/epub/demo'); +$gConfig['databases'][] = array('name' => 'Bibliothèque numérique romande', 'db_path' => '/opt/ebooks/calibre/bnr', 'epub_path' => '/opt/ebooks/epub/bnr'); +$gConfig['databases'][] = array('name' => 'La Bibliothèque d\'Ebooks', 'db_path' => '/opt/ebooks/calibre/bibebook', 'epub_path' => '/opt/ebooks/epub/bibebook'); /** * Available actions @@ -43,3 +42,5 @@ $gConfig['databases'][] = array('name' => 'La Bibliothèque électronique du Qu $gConfig['actions'] = array(); $gConfig['actions']['cvs_export'] = 'Cvs export'; $gConfig['actions']['db_load'] = 'Create database'; + +?> diff --git a/resources/epub-loader/app/header.php b/resources/epub-loader/app/header.php index 2fb83dd..db10ce0 100644 --- a/resources/epub-loader/app/header.php +++ b/resources/epub-loader/app/header.php @@ -1,5 +1,4 @@ Date: Mon, 7 Oct 2013 23:00:06 +0100 Subject: [PATCH 12/19] Add recursive glob for epub directory --- .../epub-loader/app/action_cvs_export.php | 2 +- resources/epub-loader/app/action_db_load.php | 2 +- resources/epub-loader/app/index.php | 40 ++++++++++++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/resources/epub-loader/app/action_cvs_export.php b/resources/epub-loader/app/action_cvs_export.php index 0287928..44bce7f 100644 --- a/resources/epub-loader/app/action_cvs_export.php +++ b/resources/epub-loader/app/action_cvs_export.php @@ -14,7 +14,7 @@ try { 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'); + $fileList = RecursiveGlob($dbConfig['epub_path'], '*.epub'); foreach ($fileList as $fileName) { $export->AddEpub($fileName); } diff --git a/resources/epub-loader/app/action_db_load.php b/resources/epub-loader/app/action_db_load.php index 22b9303..9d82522 100644 --- a/resources/epub-loader/app/action_db_load.php +++ b/resources/epub-loader/app/action_db_load.php @@ -14,7 +14,7 @@ try { 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'); + $fileList = RecursiveGlob($dbConfig['epub_path'], '*.epub'); foreach ($fileList as $fileName) { $db->AddEpub($fileName); } diff --git a/resources/epub-loader/app/index.php b/resources/epub-loader/app/index.php index 8ab70b1..150d846 100644 --- a/resources/epub-loader/app/index.php +++ b/resources/epub-loader/app/index.php @@ -62,6 +62,44 @@ $dbNum = isset($_GET['dbnum']) ? (int)$_GET['dbnum'] : null; // Include html header require_once(__DIR__ . DIRECTORY_SEPARATOR . 'header.php'); +/** + * Recursive get files + * + * @param string Base directory to search in + * @param string Search pattern + */ +function RecursiveGlob($inPath = '', $inPattern = '*') +{ + $res = array(); + + // Check path + if (!is_dir($inPath)) { + return $res; + } + + // Get the list of directories + if (substr($inPath, -1) != DIRECTORY_SEPARATOR) { + $inPath .= DIRECTORY_SEPARATOR; + } + + // Add files from the current directory + $files = glob($inPath . $inPattern, GLOB_MARK | GLOB_NOSORT); + foreach ($files as $item) { + if (substr($item, -1) == DIRECTORY_SEPARATOR) { + continue; + } + $res[] = $item; + } + + // Scan sub directories + $paths = glob($inPath . '*', GLOB_MARK | GLOB_ONLYDIR | GLOB_NOSORT); + foreach ($paths as $path) { + $res = array_merge($res, RecursiveGlob($path, $inPattern)); + } + + return $res; +} + // Html content if (isset($action) && isset($dbNum)) { if (!isset($gConfig['databases'][$dbNum])) { @@ -102,7 +140,7 @@ else { $str .= '' . "\n"; $actionTitle = $gConfig['actions'][$action]; foreach ($gConfig['databases'] as $dbNum => $dbConfig) { - $fileList = glob($dbConfig['epub_path'] . DIRECTORY_SEPARATOR . '*.epub'); + $fileList = RecursiveGlob($dbConfig['epub_path'], '*.epub'); $str .= '' . "\n"; $str .= '' . $dbNum . '' . "\n"; $str .= '' . $dbConfig['name'] . '' . "\n"; From fce60d40794d64aee2f82e48ae9a4c69c71c4b55 Mon Sep 17 00:00:00 2001 From: Marsender Date: Thu, 10 Oct 2013 01:16:43 +0100 Subject: [PATCH 13/19] Add Calibre fields: Description, Language, Subjects, Isbn, CreationDate, ModificationDate --- resources/epub-loader/BookExport.class.php | 25 ++- resources/epub-loader/BookInfos.class.php | 4 + .../epub-loader/CalibreDbLoader.class.php | 162 +++++++++++++++--- .../epub-loader/app/action_cvs_export.php | 5 +- resources/epub-loader/app/action_db_load.php | 5 +- resources/php-epub-meta/epub.php | 24 +++ 6 files changed, 196 insertions(+), 29 deletions(-) diff --git a/resources/epub-loader/BookExport.class.php b/resources/epub-loader/BookExport.class.php index 19e754a..643982e 100644 --- a/resources/epub-loader/BookExport.class.php +++ b/resources/epub-loader/BookExport.class.php @@ -42,15 +42,24 @@ class BookExport * @param string Epub file name * @throws Exception if error * - * @return void + * @return string Empty string or error if any */ public function AddEpub($inFileName) { - // Load the book infos - $bookInfos = new BookInfos(); - $bookInfos->LoadFromEpub($inFileName); - // Add the book - $this->AddBook($bookInfos); + $error = ''; + + try { + // Load the book infos + $bookInfos = new BookInfos(); + $bookInfos->LoadFromEpub($inFileName); + // Add the book + $this->AddBook($bookInfos); + } + catch (Exception $e) { + $error = $e->getMessage(); + } + + return $error; } /** @@ -83,6 +92,8 @@ class BookExport $this->mExport->SetProperty($i++, 'Publisher'); $this->mExport->SetProperty($i++, 'Serie'); $this->mExport->SetProperty($i++, 'SerieIndex'); + $this->mExport->SetProperty($i++, 'CreationDate'); + $this->mExport->SetProperty($i++, 'ModificationDate'); $this->mExport->AddContent(); } @@ -105,6 +116,8 @@ class BookExport $this->mExport->SetProperty($i++, $inBookInfo->mPublisher); $this->mExport->SetProperty($i++, $inBookInfo->mSerie); $this->mExport->SetProperty($i++, $inBookInfo->mSerieIndex); + $this->mExport->SetProperty($i++, $inBookInfo->mCreationDate); + $this->mExport->SetProperty($i++, $inBookInfo->mModificationDate); $this->mExport->AddContent(); } diff --git a/resources/epub-loader/BookInfos.class.php b/resources/epub-loader/BookInfos.class.php index fcfd75d..a4ca0c5 100644 --- a/resources/epub-loader/BookInfos.class.php +++ b/resources/epub-loader/BookInfos.class.php @@ -31,6 +31,8 @@ class BookInfos public $mPublisher = ''; public $mSerie = ''; public $mSerieIndex = ''; + public $mCreationDate = ''; + public $mModificationDate = ''; /** * Loads book infos from an epub file @@ -63,6 +65,8 @@ class BookInfos $this->mPublisher = $ePub->Publisher(); $this->mSerie = $ePub->Serie(); $this->mSerieIndex = $ePub->SerieIndex(); + $this->mCreationDate = $ePub->CreationDate(); + $this->mModificationDate = $ePub->ModificationDate(); } } diff --git a/resources/epub-loader/CalibreDbLoader.class.php b/resources/epub-loader/CalibreDbLoader.class.php index 3b30563..557f75b 100644 --- a/resources/epub-loader/CalibreDbLoader.class.php +++ b/resources/epub-loader/CalibreDbLoader.class.php @@ -120,15 +120,25 @@ class CalibreDbLoader * @param string Epub file name * @throws Exception if error * - * @return void + * @return string Empty string or error if any */ public function AddEpub($inFileName) { - // Load the book infos - $bookInfos = new BookInfos(); - $bookInfos->LoadFromEpub($inFileName); - // Add the book - $this->AddBook($bookInfos); + $error = ''; + + try { + // Load the book infos + $bookInfos = new BookInfos(); + $bookInfos->LoadFromEpub($inFileName); + // Add the book + $this->AddBook($bookInfos); + } + catch (Exception $e) { + $error = $e->getMessage(); + } + + return $error; + } /** @@ -141,28 +151,35 @@ class CalibreDbLoader */ private function AddBook($inBookInfo) { - $sql = 'insert into books(title, sort, series_index, uuid, path) values(:title, :sort, :serieindex, :uuid, :path)'; + // Check if the book uuid does not already exist + $sql = 'select b.id, b.title, b.path, d.name, d.format from books as b, data as d where d.book = b.id and uuid=:uuid'; + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':uuid', $inBookInfo->mUuid); + $stmt->execute(); + while ($post = $stmt->fetchObject()) { + $error = sprintf('Multiple book id for uuid: %s (already in file "%s/%s.%s" title "%s")', $inBookInfo->mUuid, $post->path, $post->name, $post->format, $post->title); + throw new Exception($error); + } + // Add the book + $sql = 'insert into books(title, sort, timestamp, pubdate, series_index, uuid, path) values(:title, :sort, :timestamp, :pubdate, :serieindex, :uuid, :path)'; $stmt = $this->mDb->prepare($sql); $stmt->bindParam(':title', $inBookInfo->mTitle); $stmt->bindParam(':sort', $inBookInfo->mTitle); + $stmt->bindParam(':timestamp', empty($inBookInfo->mModificationDate) ? null : $inBookInfo->mModificationDate); + $stmt->bindParam(':pubdate', empty($inBookInfo->mCreationDate) ? null : $inBookInfo->mCreationDate); $stmt->bindParam(':serieindex', $inBookInfo->mSerieIndex); $stmt->bindParam(':uuid', $inBookInfo->mUuid); $stmt->bindParam(':path', $inBookInfo->mPath); $stmt->execute(); // Get the book id - $sql = 'select id, title from books where uuid=:uuid'; + $sql = 'select id from books where uuid=:uuid'; $stmt = $this->mDb->prepare($sql); $stmt->bindParam(':uuid', $inBookInfo->mUuid); $stmt->execute(); $idBook = null; while ($post = $stmt->fetchObject()) { - if (!isset($idBook)) { - $idBook = $post->id; - } - else { - $error = sprintf('Multiple book id for uuid: %s (already in title "%s")', $inBookInfo->mUuid, $post->title); - throw new Exception($error); - } + $idBook = $post->id; + break; } if (!isset($idBook)) { $error = sprintf('Cannot find book id for uuid: %s', $inBookInfo->mUuid); @@ -175,15 +192,28 @@ class CalibreDbLoader $stmt->bindParam(':format', $inBookInfo->mFormat); $stmt->bindParam(':name', $inBookInfo->mName); $stmt->execute(); + // Add the book comments + $sql = 'insert into comments(book, text) values(:idBook, :text)'; + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':idBook', $idBook, PDO::PARAM_INT); + $stmt->bindParam(':text', $inBookInfo->mDescription); + $stmt->execute(); // Add the book identifiers if (!empty($inBookInfo->mUri)) { $sql = 'insert into identifiers(book, type, val) values(:idBook, :type, :value)'; - $stmt = $this->mDb->prepare($sql); - $type = 'URI'; - $stmt->bindParam(':idBook', $idBook, PDO::PARAM_INT); - $stmt->bindParam(':type', $type); - $stmt->bindParam(':value', $inBookInfo->mUri); - $stmt->execute(); + $identifiers = array(); + $identifiers['URI'] = $inBookInfo->mUri; + $identifiers['ISBN'] = $inBookInfo->mIsbn; + foreach ($identifiers as $key => $value) { + if (empty($value)) { + continue; + } + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':idBook', $idBook, PDO::PARAM_INT); + $stmt->bindParam(':type', $key); + $stmt->bindParam(':value', $value); + $stmt->execute(); + } } // Add the book serie if (!empty($inBookInfo->mSerie)) { @@ -275,6 +305,96 @@ class CalibreDbLoader $stmt->bindParam(':idAuthor', $idAuthor, PDO::PARAM_INT); $stmt->execute(); } + // Add the book language + { + // Get the language id + $sql = 'select id from languages where lang_code=:language'; + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':language', $inBookInfo->mLanguage); + $stmt->execute(); + $post = $stmt->fetchObject(); + if ($post) { + $idLanguage = $post->id; + } + else { + // Add a new language + $sql = 'insert into languages(lang_code) values(:language)'; + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':language', $inBookInfo->mLanguage); + $stmt->execute(); + // Get the language id + $sql = 'select id from languages where lang_code=:language'; + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':language', $inBookInfo->mLanguage); + $stmt->execute(); + $idLanguage = null; + while ($post = $stmt->fetchObject()) { + if (!isset($idLanguage)) { + $idLanguage = $post->id; + } + else { + $error = sprintf('Multiple languages for lang_code: %s', $inBookInfo->mLanguage); + throw new Exception($error); + } + } + if (!isset($idLanguage)) { + $error = sprintf('Cannot find language id for lang_code: %s', $inBookInfo->mLanguage); + throw new Exception($error); + } + } + // Add the book language link + $itemOder = 0; + $sql = 'insert into books_languages_link(book, lang_code, item_order) values(:idBook, :idLanguage, :itemOrder)'; + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':idBook', $idBook, PDO::PARAM_INT); + $stmt->bindParam(':idLanguage', $idLanguage, PDO::PARAM_INT); + $stmt->bindParam(':itemOrder', $itemOder, PDO::PARAM_INT); + $stmt->execute(); + } + // Add the book tags (subjects) + foreach ($inBookInfo->mSubjects as $subject) { + // Get the subject id + $sql = 'select id from tags where name=:subject'; + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':subject', $subject); + $stmt->execute(); + $post = $stmt->fetchObject(); + if ($post) { + $idSubject = $post->id; + } + else { + // Add a new subject + $sql = 'insert into tags(name) values(:subject)'; + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':subject', $subject); + $stmt->execute(); + // Get the subject id + $sql = 'select id from tags where name=:subject'; + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':subject', $subject); + $stmt->execute(); + $idSubject = null; + while ($post = $stmt->fetchObject()) { + if (!isset($idSubject)) { + $idSubject = $post->id; + } + else { + $error = sprintf('Multiple subjects for name: %s', $subject); + throw new Exception($error); + } + } + if (!isset($idSubject)) { + $error = sprintf('Cannot find subject id for name: %s', $subject); + throw new Exception($error); + } + } + // Add the book subject link + $sql = 'insert into books_tags_link(book, tag) values(:idBook, :idSubject)'; + $stmt = $this->mDb->prepare($sql); + $stmt->bindParam(':idBook', $idBook, PDO::PARAM_INT); + $stmt->bindParam(':idSubject', $idSubject, PDO::PARAM_INT); + $stmt->execute(); + } } /** diff --git a/resources/epub-loader/app/action_cvs_export.php b/resources/epub-loader/app/action_cvs_export.php index 44bce7f..b967bec 100644 --- a/resources/epub-loader/app/action_cvs_export.php +++ b/resources/epub-loader/app/action_cvs_export.php @@ -16,7 +16,10 @@ try { if (!empty($dbConfig['epub_path'])) { $fileList = RecursiveGlob($dbConfig['epub_path'], '*.epub'); foreach ($fileList as $fileName) { - $export->AddEpub($fileName); + $error = $export->AddEpub($fileName); + if (!empty($error)) { + $gErrorArray[$fileName] = $error; + } } } $export->SaveToFile(); diff --git a/resources/epub-loader/app/action_db_load.php b/resources/epub-loader/app/action_db_load.php index 9d82522..17a5b83 100644 --- a/resources/epub-loader/app/action_db_load.php +++ b/resources/epub-loader/app/action_db_load.php @@ -16,7 +16,10 @@ try { if (!empty($dbConfig['epub_path'])) { $fileList = RecursiveGlob($dbConfig['epub_path'], '*.epub'); foreach ($fileList as $fileName) { - $db->AddEpub($fileName); + $error = $db->AddEpub($fileName); + if (!empty($error)) { + $gErrorArray[$fileName] = $error; + } } } } diff --git a/resources/php-epub-meta/epub.php b/resources/php-epub-meta/epub.php index 6dcc19f..c7d6b28 100644 --- a/resources/php-epub-meta/epub.php +++ b/resources/php-epub-meta/epub.php @@ -322,6 +322,30 @@ class EPub { return $res; } + /** + * Set or get the book's creation date + * + * @param string Date eg: 2012-05-19T12:54:25Z + */ + public function CreationDate($date = false) + { + $res = $this->getset('dc:date', $date, 'opf:event', 'creation'); + + return $res; + } + + /** + * Set or get the book's modification date + * + * @param string Date eg: 2012-05-19T12:54:25Z + */ + public function ModificationDate($date = false) + { + $res = $this->getset('dc:date', $date, 'opf:event', 'modification'); + + return $res; + } + /** * Set or get the book's URI * From 35bbf55e19a68941c2a1d0d579d593806ea93ac4 Mon Sep 17 00:00:00 2001 From: Marsender Date: Thu, 10 Oct 2013 05:21:19 +0100 Subject: [PATCH 14/19] Epub loader: Add Cops feed loader Typo: Change cvs_export to csv_export Rename config.php file to epub-loader-config.php Config: Add app_name, admin_email --- resources/epub-loader/app/.gitignore | 2 +- ...n_cvs_export.php => action_csv_export.php} | 0 resources/epub-loader/app/cops-feed.php | 27 +++++++++++++++++++ ...example => epub-loader-config.php.example} | 22 ++++++++++++--- resources/epub-loader/app/footer.php | 8 +++++- resources/epub-loader/app/header.php | 4 +-- resources/epub-loader/app/index.php | 19 +------------ 7 files changed, 56 insertions(+), 26 deletions(-) rename resources/epub-loader/app/{action_cvs_export.php => action_csv_export.php} (100%) create mode 100644 resources/epub-loader/app/cops-feed.php rename resources/epub-loader/app/{config.php.example => epub-loader-config.php.example} (71%) diff --git a/resources/epub-loader/app/.gitignore b/resources/epub-loader/app/.gitignore index 4e9b47a..0d00eb9 100644 --- a/resources/epub-loader/app/.gitignore +++ b/resources/epub-loader/app/.gitignore @@ -1 +1 @@ -config.php \ No newline at end of file +epub-loader-config.php diff --git a/resources/epub-loader/app/action_cvs_export.php b/resources/epub-loader/app/action_csv_export.php similarity index 100% rename from resources/epub-loader/app/action_cvs_export.php rename to resources/epub-loader/app/action_csv_export.php diff --git a/resources/epub-loader/app/cops-feed.php b/resources/epub-loader/app/cops-feed.php new file mode 100644 index 0000000..698404b --- /dev/null +++ b/resources/epub-loader/app/cops-feed.php @@ -0,0 +1,27 @@ + + */ + +// Include config file +$fileName = __DIR__ . DIRECTORY_SEPARATOR . 'epub-loader-config.php'; +if (!file_exists($fileName)) { + die ('Missing configuration file: ' . $fileName); +} +require_once($fileName); + +// Add cops directory to include path +$includePath = ini_get('include_path'); +ini_set('include_path', $includePath . PATH_SEPARATOR . $gConfig['cops_directory']); + +// Include COPS feed +$fileName = $gConfig['cops_directory'] . '/feed.php'; +if (!file_exists($fileName)) { + die ('Incorrect include file: ' . $fileName); +} +require_once($fileName); + +?> diff --git a/resources/epub-loader/app/config.php.example b/resources/epub-loader/app/epub-loader-config.php.example similarity index 71% rename from resources/epub-loader/app/config.php.example rename to resources/epub-loader/app/epub-loader-config.php.example index 802b1ca..a0140d5 100644 --- a/resources/epub-loader/app/config.php.example +++ b/resources/epub-loader/app/epub-loader-config.php.example @@ -8,12 +8,25 @@ $gConfig = array(); +/** + * Application name + */ +$gConfig['app_name'] = 'Epub loader'; + +/** + * Admin email + */ +$gConfig['admin_email'] = 'didier.corbiere@opale-concept.com'; + /** * Cops directory * * This is the base path of Cops library */ $gConfig['cops_directory'] = dirname(dirname(dirname(__DIR__))); +if (!is_dir($gConfig['cops_directory'])) { + die ('Incorrect Cops directory: ' . $gConfig['cops_directory']); +} /** * Create Calibre databases ? @@ -30,17 +43,18 @@ $gConfig['create_db'] = true; * 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 + * pdf_path: The path where to look for pdf files */ $gConfig['databases'] = array(); -$gConfig['databases'][] = array('name' => 'Littérature classique', 'db_path' => '/opt/ebooks/calibre/demo', 'epub_path' => '/opt/ebooks/epub/demo'); -$gConfig['databases'][] = array('name' => 'Bibliothèque numérique romande', 'db_path' => '/opt/ebooks/calibre/bnr', 'epub_path' => '/opt/ebooks/epub/bnr'); -$gConfig['databases'][] = array('name' => 'La Bibliothèque d\'Ebooks', 'db_path' => '/opt/ebooks/calibre/bibebook', 'epub_path' => '/opt/ebooks/epub/bibebook'); +$gConfig['databases'][] = array('name' => 'Littérature classique', 'db_path' => '/opt/ebooks/calibre/demo', 'epub_path' => '/opt/ebooks/epub/demo', 'pdf_path' => ''); +$gConfig['databases'][] = array('name' => 'Bibliothèque numérique romande', 'db_path' => '/opt/ebooks/calibre/bnr', 'epub_path' => '/opt/ebooks/epub/bnr', 'pdf_path' => ''); +$gConfig['databases'][] = array('name' => 'La Bibliothèque d\'Ebooks', 'db_path' => '/opt/ebooks/calibre/bibebook', 'epub_path' => '/opt/ebooks/epub/bibebook', 'pdf_path' => ''); /** * Available actions */ $gConfig['actions'] = array(); -$gConfig['actions']['cvs_export'] = 'Cvs export'; +$gConfig['actions']['csv_export'] = 'Csv export'; $gConfig['actions']['db_load'] = 'Create database'; ?> diff --git a/resources/epub-loader/app/footer.php b/resources/epub-loader/app/footer.php index 43eab64..13b1631 100644 --- a/resources/epub-loader/app/footer.php +++ b/resources/epub-loader/app/footer.php @@ -28,12 +28,18 @@ diff --git a/resources/epub-loader/app/header.php b/resources/epub-loader/app/header.php index db10ce0..a4a93a2 100644 --- a/resources/epub-loader/app/header.php +++ b/resources/epub-loader/app/header.php @@ -5,7 +5,7 @@ header('Content-type: text/html; charset=utf-8'); "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - <?php echo DEF_AppName; ?> + <?php echo $gConfig['app_name']; ?>