From fce60d40794d64aee2f82e48ae9a4c69c71c4b55 Mon Sep 17 00:00:00 2001 From: Marsender Date: Thu, 10 Oct 2013 01:16:43 +0100 Subject: [PATCH] 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 *