Add Calibre fields: Description, Language, Subjects, Isbn, CreationDate, ModificationDate

This commit is contained in:
Marsender 2013-10-10 01:16:43 +01:00
parent 356a1e263b
commit fce60d4079
6 changed files with 196 additions and 29 deletions

View file

@ -42,16 +42,25 @@ class BookExport
* @param string Epub file name * @param string Epub file name
* @throws Exception if error * @throws Exception if error
* *
* @return void * @return string Empty string or error if any
*/ */
public function AddEpub($inFileName) public function AddEpub($inFileName)
{ {
$error = '';
try {
// Load the book infos // Load the book infos
$bookInfos = new BookInfos(); $bookInfos = new BookInfos();
$bookInfos->LoadFromEpub($inFileName); $bookInfos->LoadFromEpub($inFileName);
// Add the book // Add the book
$this->AddBook($bookInfos); $this->AddBook($bookInfos);
} }
catch (Exception $e) {
$error = $e->getMessage();
}
return $error;
}
/** /**
* Add a new book to the export * Add a new book to the export
@ -83,6 +92,8 @@ class BookExport
$this->mExport->SetProperty($i++, 'Publisher'); $this->mExport->SetProperty($i++, 'Publisher');
$this->mExport->SetProperty($i++, 'Serie'); $this->mExport->SetProperty($i++, 'Serie');
$this->mExport->SetProperty($i++, 'SerieIndex'); $this->mExport->SetProperty($i++, 'SerieIndex');
$this->mExport->SetProperty($i++, 'CreationDate');
$this->mExport->SetProperty($i++, 'ModificationDate');
$this->mExport->AddContent(); $this->mExport->AddContent();
} }
@ -105,6 +116,8 @@ class BookExport
$this->mExport->SetProperty($i++, $inBookInfo->mPublisher); $this->mExport->SetProperty($i++, $inBookInfo->mPublisher);
$this->mExport->SetProperty($i++, $inBookInfo->mSerie); $this->mExport->SetProperty($i++, $inBookInfo->mSerie);
$this->mExport->SetProperty($i++, $inBookInfo->mSerieIndex); $this->mExport->SetProperty($i++, $inBookInfo->mSerieIndex);
$this->mExport->SetProperty($i++, $inBookInfo->mCreationDate);
$this->mExport->SetProperty($i++, $inBookInfo->mModificationDate);
$this->mExport->AddContent(); $this->mExport->AddContent();
} }

View file

@ -31,6 +31,8 @@ class BookInfos
public $mPublisher = ''; public $mPublisher = '';
public $mSerie = ''; public $mSerie = '';
public $mSerieIndex = ''; public $mSerieIndex = '';
public $mCreationDate = '';
public $mModificationDate = '';
/** /**
* Loads book infos from an epub file * Loads book infos from an epub file
@ -63,6 +65,8 @@ class BookInfos
$this->mPublisher = $ePub->Publisher(); $this->mPublisher = $ePub->Publisher();
$this->mSerie = $ePub->Serie(); $this->mSerie = $ePub->Serie();
$this->mSerieIndex = $ePub->SerieIndex(); $this->mSerieIndex = $ePub->SerieIndex();
$this->mCreationDate = $ePub->CreationDate();
$this->mModificationDate = $ePub->ModificationDate();
} }
} }

View file

@ -120,16 +120,26 @@ class CalibreDbLoader
* @param string Epub file name * @param string Epub file name
* @throws Exception if error * @throws Exception if error
* *
* @return void * @return string Empty string or error if any
*/ */
public function AddEpub($inFileName) public function AddEpub($inFileName)
{ {
$error = '';
try {
// Load the book infos // Load the book infos
$bookInfos = new BookInfos(); $bookInfos = new BookInfos();
$bookInfos->LoadFromEpub($inFileName); $bookInfos->LoadFromEpub($inFileName);
// Add the book // Add the book
$this->AddBook($bookInfos); $this->AddBook($bookInfos);
} }
catch (Exception $e) {
$error = $e->getMessage();
}
return $error;
}
/** /**
* Add a new book into the db * Add a new book into the db
@ -141,28 +151,35 @@ class CalibreDbLoader
*/ */
private function AddBook($inBookInfo) 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 = $this->mDb->prepare($sql);
$stmt->bindParam(':title', $inBookInfo->mTitle); $stmt->bindParam(':title', $inBookInfo->mTitle);
$stmt->bindParam(':sort', $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(':serieindex', $inBookInfo->mSerieIndex);
$stmt->bindParam(':uuid', $inBookInfo->mUuid); $stmt->bindParam(':uuid', $inBookInfo->mUuid);
$stmt->bindParam(':path', $inBookInfo->mPath); $stmt->bindParam(':path', $inBookInfo->mPath);
$stmt->execute(); $stmt->execute();
// Get the book id // 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 = $this->mDb->prepare($sql);
$stmt->bindParam(':uuid', $inBookInfo->mUuid); $stmt->bindParam(':uuid', $inBookInfo->mUuid);
$stmt->execute(); $stmt->execute();
$idBook = null; $idBook = null;
while ($post = $stmt->fetchObject()) { while ($post = $stmt->fetchObject()) {
if (!isset($idBook)) {
$idBook = $post->id; $idBook = $post->id;
} break;
else {
$error = sprintf('Multiple book id for uuid: %s (already in title "%s")', $inBookInfo->mUuid, $post->title);
throw new Exception($error);
}
} }
if (!isset($idBook)) { if (!isset($idBook)) {
$error = sprintf('Cannot find book id for uuid: %s', $inBookInfo->mUuid); $error = sprintf('Cannot find book id for uuid: %s', $inBookInfo->mUuid);
@ -175,16 +192,29 @@ class CalibreDbLoader
$stmt->bindParam(':format', $inBookInfo->mFormat); $stmt->bindParam(':format', $inBookInfo->mFormat);
$stmt->bindParam(':name', $inBookInfo->mName); $stmt->bindParam(':name', $inBookInfo->mName);
$stmt->execute(); $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 // Add the book identifiers
if (!empty($inBookInfo->mUri)) { if (!empty($inBookInfo->mUri)) {
$sql = 'insert into identifiers(book, type, val) values(:idBook, :type, :value)'; $sql = 'insert into identifiers(book, type, val) values(:idBook, :type, :value)';
$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 = $this->mDb->prepare($sql);
$type = 'URI';
$stmt->bindParam(':idBook', $idBook, PDO::PARAM_INT); $stmt->bindParam(':idBook', $idBook, PDO::PARAM_INT);
$stmt->bindParam(':type', $type); $stmt->bindParam(':type', $key);
$stmt->bindParam(':value', $inBookInfo->mUri); $stmt->bindParam(':value', $value);
$stmt->execute(); $stmt->execute();
} }
}
// Add the book serie // Add the book serie
if (!empty($inBookInfo->mSerie)) { if (!empty($inBookInfo->mSerie)) {
// Get the serie id // Get the serie id
@ -275,6 +305,96 @@ class CalibreDbLoader
$stmt->bindParam(':idAuthor', $idAuthor, PDO::PARAM_INT); $stmt->bindParam(':idAuthor', $idAuthor, PDO::PARAM_INT);
$stmt->execute(); $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();
}
} }
/** /**

View file

@ -16,7 +16,10 @@ try {
if (!empty($dbConfig['epub_path'])) { if (!empty($dbConfig['epub_path'])) {
$fileList = RecursiveGlob($dbConfig['epub_path'], '*.epub'); $fileList = RecursiveGlob($dbConfig['epub_path'], '*.epub');
foreach ($fileList as $fileName) { foreach ($fileList as $fileName) {
$export->AddEpub($fileName); $error = $export->AddEpub($fileName);
if (!empty($error)) {
$gErrorArray[$fileName] = $error;
}
} }
} }
$export->SaveToFile(); $export->SaveToFile();

View file

@ -16,7 +16,10 @@ try {
if (!empty($dbConfig['epub_path'])) { if (!empty($dbConfig['epub_path'])) {
$fileList = RecursiveGlob($dbConfig['epub_path'], '*.epub'); $fileList = RecursiveGlob($dbConfig['epub_path'], '*.epub');
foreach ($fileList as $fileName) { foreach ($fileList as $fileName) {
$db->AddEpub($fileName); $error = $db->AddEpub($fileName);
if (!empty($error)) {
$gErrorArray[$fileName] = $error;
}
} }
} }
} }

View file

@ -322,6 +322,30 @@ class EPub {
return $res; 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 * Set or get the book's URI
* *