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
* @throws Exception if error
*
* @return void
* @return string Empty string or error if any
*/
public function AddEpub($inFileName)
{
$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;
}
/**
* Add a new book to the export
@ -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();
}

View file

@ -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();
}
}

View file

@ -120,16 +120,26 @@ 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)
{
$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;
}
/**
* Add a new book into the db
@ -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);
}
break;
}
if (!isset($idBook)) {
$error = sprintf('Cannot find book id for uuid: %s', $inBookInfo->mUuid);
@ -175,16 +192,29 @@ 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)';
$identifiers = array();
$identifiers['URI'] = $inBookInfo->mUri;
$identifiers['ISBN'] = $inBookInfo->mIsbn;
foreach ($identifiers as $key => $value) {
if (empty($value)) {
continue;
}
$stmt = $this->mDb->prepare($sql);
$type = 'URI';
$stmt->bindParam(':idBook', $idBook, PDO::PARAM_INT);
$stmt->bindParam(':type', $type);
$stmt->bindParam(':value', $inBookInfo->mUri);
$stmt->bindParam(':type', $key);
$stmt->bindParam(':value', $value);
$stmt->execute();
}
}
// Add the book serie
if (!empty($inBookInfo->mSerie)) {
// Get the serie id
@ -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();
}
}
/**

View file

@ -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();

View file

@ -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;
}
}
}
}

View file

@ -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
*