cops/book.php

365 lines
12 KiB
PHP
Raw Normal View History

2012-05-28 08:01:33 +03:00
<?php
/**
* COPS (Calibre OPDS PHP Server) class file
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author S<EFBFBD>bastien Lucas <sebastien@slucas.fr>
*/
require_once('base.php');
2012-05-28 08:05:05 +03:00
require_once('serie.php');
require_once('author.php');
2012-06-11 22:30:25 +03:00
require_once('tag.php');
2012-05-28 08:01:33 +03:00
class Book extends Base {
const ALL_BOOKS_UUID = "urn:uuid";
2012-05-28 08:01:33 +03:00
const ALL_BOOKS_ID = "calibre:books";
const ALL_RECENT_BOOKS_ID = "calibre:recentbooks";
const BOOK_COLUMNS = "books.id as id, books.title as title, text as comment, path, timestamp, pubdate, series_index, uuid, has_cover";
2012-05-28 08:01:33 +03:00
public $id;
public $title;
public $timestamp;
public $pubdate;
public $path;
public $uuid;
public $hasCover;
2012-05-28 08:01:33 +03:00
public $relativePath;
public $seriesIndex;
public $comment;
public $authors = NULL;
public $serie = NULL;
public $tags = NULL;
2012-05-28 08:06:12 +03:00
public $format = array ();
2012-05-28 08:05:05 +03:00
public static $mimetypes = array(
'epub' => 'application/epub+zip',
'mobi' => 'application/x-mobipocket-ebook',
'pdf' => 'application/pdf'
);
2012-05-28 08:07:49 +03:00
public function __construct($line) {
2012-05-28 08:01:33 +03:00
global $config;
2012-05-28 08:07:49 +03:00
$this->id = $line->id;
$this->title = $line->title;
$this->timestamp = strtotime ($line->timestamp);
$this->pubdate = strtotime ($line->pubdate);
$this->path = $config['calibre_directory'] . $line->path;
$this->relativePath = $line->path;
$this->seriesIndex = $line->series_index;
$this->comment = $line->comment;
$this->uuid = $line->uuid;
$this->hasCover = $line->has_cover;
2012-05-28 08:01:33 +03:00
}
public function getEntryId () {
return self::ALL_BOOKS_UUID.":".$this->uuid;
2012-05-28 08:01:33 +03:00
}
2012-05-28 08:07:49 +03:00
public static function getEntryIdByLetter ($startingLetter) {
return self::ALL_BOOKS_ID.":letter:".$startingLetter;
}
2012-05-28 08:01:33 +03:00
public function getTitle () {
return $this->title;
}
public function getAuthors () {
if (is_null ($this->authors)) {
$this->authors = Author::getAuthorByBookId ($this->id);
}
return $this->authors;
}
2012-05-28 08:06:12 +03:00
public function getAuthorsName () {
$authorList = null;
foreach ($this->getAuthors () as $author) {
if ($authorList) {
$authorList = $authorList . ", " . $author->name;
}
else
{
$authorList = $author->name;
}
}
return $authorList;
}
2012-05-28 08:01:33 +03:00
public function getSerie () {
if (is_null ($this->serie)) {
$this->serie = Serie::getSerieByBookId ($this->id);
}
return $this->serie;
}
public function getTags () {
if (is_null ($this->tags)) {
$this->tags = array ();
2012-06-11 22:30:25 +03:00
$result = parent::getDb ()->prepare('select tags.id as id, name
2012-05-28 08:01:33 +03:00
from books_tags_link, tags
where tag = tags.id
and book = ?
order by name');
$result->execute (array ($this->id));
while ($post = $result->fetchObject ())
{
2012-06-11 22:30:25 +03:00
array_push ($this->tags, new Tag ($post->id, $post->name));
2012-05-28 08:01:33 +03:00
}
}
return $this->tags;
}
2012-05-28 08:06:12 +03:00
public function getTagsName () {
$tagList = null;
foreach ($this->getTags () as $tag) {
if ($tagList) {
2012-06-11 22:30:25 +03:00
$tagList = $tagList . ", " . $tag->name;
2012-05-28 08:06:12 +03:00
}
else
{
2012-06-11 22:30:25 +03:00
$tagList = $tag->name;
2012-05-28 08:06:12 +03:00
}
}
return $tagList;
}
public function getComment ($withSerie = true) {
2012-05-28 08:01:33 +03:00
$addition = "";
$se = $this->getSerie ();
if (!is_null ($se) && $withSerie) {
2012-06-18 18:02:05 +03:00
$addition = $addition . "<strong>" . localize("content.series") . "</strong>" . str_format (localize ("content.series.data"), $this->seriesIndex, htmlspecialchars ($se->name)) . "<br />\n";
2012-05-28 08:01:33 +03:00
}
2012-05-30 16:04:23 +03:00
if (preg_match ("/<\/(div|p|a)>/", $this->comment))
{
return $addition . str_replace ("<br>", "<br />", $this->comment);
}
else
{
return $addition . htmlspecialchars ($this->comment);
}
2012-05-28 08:01:33 +03:00
}
public function getFilePath ($extension, $relative = false)
{
if ($handle = opendir($this->path)) {
while (false !== ($file = readdir($handle))) {
if (preg_match ('/' . $extension . '$/', $file)) {
if ($relative)
{
return $this->relativePath."/".$file;
}
else
{
return $this->path."/".$file;
}
}
}
}
return NULL;
}
private function getLink ($type, $mime, $rel, $filename, $title = NULL)
{
global $config;
if (preg_match ('/^\//', $config['calibre_directory']))
{
return new Link ("fetch.php?id=$this->id&type=" . $type, $mime, $rel, $title);
}
else
{
return new Link (str_replace('%2F','/',rawurlencode ($this->path."/".$filename)), $mime, $rel, $title);
}
}
2012-05-28 08:01:33 +03:00
public function getLinkArray ()
{
global $config;
$linkArray = array();
if ($this->hasCover)
{
array_push ($linkArray, $this->getLink ("jpg", "image/jpeg", Link::OPDS_IMAGE_TYPE, "cover.jpg"));
$height = "50";
if (preg_match ('/feed.php/', $_SERVER["SCRIPT_NAME"])) {
$height = $config['cops_opds_thumbnail_height'];
}
else
{
$height = $config['cops_html_thumbnail_height'];
}
array_push ($linkArray, new Link ("fetch.php?id=$this->id&height=" . $height, "image/jpeg", Link::OPDS_THUMBNAIL_TYPE));
}
2012-05-28 08:01:33 +03:00
if ($handle = opendir($this->path)) {
while (false !== ($file = readdir($handle))) {
2012-05-28 08:05:05 +03:00
foreach (self::$mimetypes as $ext => $mime)
{
if (preg_match ('/'. $ext .'$/', $file)) {
2012-05-28 08:06:12 +03:00
$this->format [$ext] = $file;
array_push ($linkArray, $this->getLink ($ext, $mime, Link::OPDS_ACQUISITION_TYPE, $file, "Download"));
2012-05-28 08:01:33 +03:00
}
}
}
}
foreach ($this->getAuthors () as $author) {
array_push ($linkArray, new LinkNavigation ($author->getUri (), "related", str_format (localize ("bookentry.author"), localize ("splitByLetter.book.other"), $author->name)));
2012-05-28 08:01:33 +03:00
}
$serie = $this->getSerie ();
if (!is_null ($serie)) {
array_push ($linkArray, new LinkNavigation ($serie->getUri (), "related", str_format (localize ("content.series.data"), $this->seriesIndex, $serie->name)));
2012-05-28 08:01:33 +03:00
}
return $linkArray;
}
public function getEntry () {
2012-05-28 08:05:05 +03:00
return new EntryBook ($this->getTitle (), $this->getEntryId (),
2012-05-28 08:01:33 +03:00
$this->getComment (), "text/html",
2012-05-28 08:05:05 +03:00
$this->getLinkArray (), $this);
2012-05-28 08:01:33 +03:00
}
public static function getCount() {
2012-05-28 08:05:05 +03:00
global $config;
2012-05-28 08:01:33 +03:00
$nBooks = parent::getDb ()->query('select count(*) from books')->fetchColumn();
2012-05-28 08:05:05 +03:00
$result = array();
$entry = new Entry (localize ("allbooks.title"),
2012-05-28 08:01:33 +03:00
self::ALL_BOOKS_ID,
str_format (localize ("allbooks.alphabetical"), $nBooks), "text",
2012-05-28 08:06:12 +03:00
array ( new LinkNavigation ("?page=".parent::PAGE_ALL_BOOKS)));
2012-05-28 08:05:05 +03:00
array_push ($result, $entry);
$entry = new Entry (localize ("recent.title"),
2012-05-28 08:01:33 +03:00
self::ALL_RECENT_BOOKS_ID,
str_format (localize ("recent.list"), $config['cops_recentbooks_limit']), "text",
2012-05-28 08:06:12 +03:00
array ( new LinkNavigation ("?page=".parent::PAGE_ALL_RECENT_BOOKS)));
2012-05-28 08:05:05 +03:00
array_push ($result, $entry);
return $result;
2012-05-28 08:01:33 +03:00
}
public static function getBooksByAuthor($authorId) {
2012-05-28 08:07:49 +03:00
$result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . '
2012-05-28 08:01:33 +03:00
from books_authors_link, books left outer join comments on comments.book = books.id
where books_authors_link.book = books.id
and author = ?
order by pubdate');
2012-05-28 08:05:05 +03:00
$entryArray = array();
2012-05-28 08:01:33 +03:00
$result->execute (array ($authorId));
while ($post = $result->fetchObject ())
{
2012-05-28 08:07:49 +03:00
$book = new Book ($post);
2012-05-28 08:05:05 +03:00
array_push ($entryArray, $book->getEntry ());
2012-05-28 08:01:33 +03:00
}
2012-05-28 08:05:05 +03:00
return $entryArray;
2012-05-28 08:01:33 +03:00
}
public static function getBooksBySeries($serieId) {
2012-05-28 08:07:49 +03:00
$result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . '
2012-05-28 08:01:33 +03:00
from books_series_link, books left outer join comments on comments.book = books.id
where books_series_link.book = books.id and series = ?
order by series_index');
2012-05-28 08:05:05 +03:00
$entryArray = array();
2012-05-28 08:01:33 +03:00
$result->execute (array ($serieId));
while ($post = $result->fetchObject ())
{
2012-05-28 08:07:49 +03:00
$book = new Book ($post);
array_push ($entryArray, $book->getEntry ());
}
return $entryArray;
}
public static function getBooksByTag($tagId) {
$result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . '
from books_tags_link, books left outer join comments on comments.book = books.id
where books_tags_link.book = books.id and tag = ?
order by sort');
$entryArray = array();
$result->execute (array ($tagId));
while ($post = $result->fetchObject ())
{
$book = new Book ($post);
2012-05-28 08:05:05 +03:00
array_push ($entryArray, $book->getEntry ());
2012-05-28 08:01:33 +03:00
}
2012-05-28 08:05:05 +03:00
return $entryArray;
2012-05-28 08:01:33 +03:00
}
public static function getBookById($bookId) {
2012-05-28 08:07:49 +03:00
$result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . '
2012-05-28 08:01:33 +03:00
from books left outer join comments on book = books.id
where books.id = ?');
2012-05-28 08:05:05 +03:00
$entryArray = array();
2012-05-28 08:01:33 +03:00
$result->execute (array ($bookId));
while ($post = $result->fetchObject ())
{
2012-05-28 08:07:49 +03:00
$book = new Book ($post);
2012-05-28 08:01:33 +03:00
return $book;
}
return NULL;
}
public static function getBooksByQuery($query) {
2012-05-28 08:07:49 +03:00
$result = parent::getDb ()->prepare("select " . self::BOOK_COLUMNS . "
2012-05-28 08:01:33 +03:00
from books left outer join comments on book = books.id
where exists (select null from authors, books_authors_link where book = books.id and author = authors.id and authors.name like ?)
or title like ?");
2012-05-28 08:05:05 +03:00
$entryArray = array();
2012-05-28 08:01:33 +03:00
$queryLike = "%" . $query . "%";
$result->execute (array ($queryLike, $queryLike));
while ($post = $result->fetchObject ())
{
2012-05-28 08:07:49 +03:00
$book = new Book ($post);
2012-05-28 08:05:05 +03:00
array_push ($entryArray, $book->getEntry ());
2012-05-28 08:01:33 +03:00
}
2012-05-28 08:05:05 +03:00
return $entryArray;
2012-05-28 08:01:33 +03:00
}
public static function getAllBooks() {
$result = parent::getDb ()->query("select substr (upper (sort), 1, 1) as title, count(*) as count
from books
group by substr (upper (sort), 1, 1)
order by substr (upper (sort), 1, 1)");
2012-05-28 08:05:05 +03:00
$entryArray = array();
2012-05-28 08:01:33 +03:00
while ($post = $result->fetchObject ())
{
2012-05-28 08:07:49 +03:00
array_push ($entryArray, new Entry ($post->title, Book::getEntryIdByLetter ($post->title),
str_format (localize("bookword.many"), $post->count), "text",
2012-05-28 08:06:12 +03:00
array ( new LinkNavigation ("?page=".parent::PAGE_ALL_BOOKS_LETTER."&id=".$post->title))));
2012-05-28 08:01:33 +03:00
}
2012-05-28 08:05:05 +03:00
return $entryArray;
2012-05-28 08:01:33 +03:00
}
public static function getBooksByStartingLetter($letter) {
2012-05-28 08:07:49 +03:00
$result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . '
2012-05-28 08:01:33 +03:00
from books left outer join comments on book = books.id
where upper (books.sort) like ?');
2012-05-28 08:05:05 +03:00
$entryArray = array();
2012-05-28 08:01:33 +03:00
$queryLike = $letter . "%";
$result->execute (array ($queryLike));
while ($post = $result->fetchObject ())
{
2012-05-28 08:07:49 +03:00
$book = new Book ($post);
2012-05-28 08:05:05 +03:00
array_push ($entryArray, $book->getEntry ());
2012-05-28 08:01:33 +03:00
}
2012-05-28 08:05:05 +03:00
return $entryArray;
2012-05-28 08:01:33 +03:00
}
public static function getAllRecentBooks() {
global $config;
2012-05-28 08:07:49 +03:00
$result = parent::getDb ()->query("select " . self::BOOK_COLUMNS . "
2012-05-28 08:01:33 +03:00
from books left outer join comments on book = books.id
order by timestamp desc limit " . $config['cops_recentbooks_limit']);
2012-05-28 08:05:05 +03:00
$entryArray = array();
2012-05-28 08:01:33 +03:00
while ($post = $result->fetchObject ())
{
2012-05-28 08:07:49 +03:00
$book = new Book ($post);
2012-05-28 08:05:05 +03:00
array_push ($entryArray, $book->getEntry ());
2012-05-28 08:01:33 +03:00
}
2012-05-28 08:05:05 +03:00
return $entryArray;
2012-05-28 08:01:33 +03:00
}
}
?>