forked from seblucas/cops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.php
397 lines (354 loc) · 12.9 KB
/
book.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php
/**
* COPS (Calibre OPDS PHP Server) class file
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Sébastien Lucas <[email protected]>
*/
require_once('base.php');
require_once('serie.php');
require_once('author.php');
require_once('tag.php');
require_once('data.php');
class Book extends Base {
const ALL_BOOKS_UUID = "urn:uuid";
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";
public $id;
public $title;
public $timestamp;
public $pubdate;
public $path;
public $uuid;
public $hasCover;
public $relativePath;
public $seriesIndex;
public $comment;
public $datas = NULL;
public $authors = NULL;
public $serie = NULL;
public $tags = NULL;
public $format = array ();
public function __construct($line) {
global $config;
$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;
}
public function getEntryId () {
return self::ALL_BOOKS_UUID.":".$this->uuid;
}
public static function getEntryIdByLetter ($startingLetter) {
return self::ALL_BOOKS_ID.":letter:".$startingLetter;
}
public function getTitle () {
return $this->title;
}
public function getAuthors () {
if (is_null ($this->authors)) {
$this->authors = Author::getAuthorByBookId ($this->id);
}
return $this->authors;
}
public function getAuthorsName () {
$authorList = null;
foreach ($this->getAuthors () as $author) {
if ($authorList) {
$authorList = $authorList . ", " . $author->name;
}
else
{
$authorList = $author->name;
}
}
return $authorList;
}
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 ();
$result = parent::getDb ()->prepare('select tags.id as id, name
from books_tags_link, tags
where tag = tags.id
and book = ?
order by name');
$result->execute (array ($this->id));
while ($post = $result->fetchObject ())
{
array_push ($this->tags, new Tag ($post->id, $post->name));
}
}
return $this->tags;
}
public function getDatas ()
{
if (is_null ($this->datas)) {
$this->datas = array ();
$result = parent::getDb ()->prepare('select id, format, name
from data where book = ?');
$result->execute (array ($this->id));
while ($post = $result->fetchObject ())
{
array_push ($this->datas, new Data ($post, $this));
}
}
return $this->datas;
}
public function getTagsName () {
$tagList = null;
foreach ($this->getTags () as $tag) {
if ($tagList) {
$tagList = $tagList . ", " . $tag->name;
}
else
{
$tagList = $tag->name;
}
}
return $tagList;
}
public function getComment ($withSerie = true) {
$addition = "";
$se = $this->getSerie ();
if (!is_null ($se) && $withSerie) {
$addition = $addition . "<strong>" . localize("content.series") . "</strong>" . str_format (localize ("content.series.data"), $this->seriesIndex, htmlspecialchars ($se->name)) . "<br />\n";
}
if (preg_match ("/<\/(div|p|a)>/", $this->comment))
{
return $addition . preg_replace ("/<(br|hr)>/", "<$1 />", $this->comment);
}
else
{
return $addition . htmlspecialchars ($this->comment);
}
}
public function getDataFormat ($format) {
foreach ($this->getDatas () as $data)
{
if ($data->format == $format)
{
return $data;
}
}
return NULL;
}
public function getFilePath ($extension, $idData = NULL, $relative = false)
{
$file = NULL;
if ($extension == "jpg")
{
$file = "cover.jpg";
}
else
{
$result = parent::getDb ()->prepare('select format, name
from data where id = ?');
$result->execute (array ($idData));
while ($post = $result->fetchObject ())
{
$file = $post->name . "." . strtolower ($post->format);
}
}
if ($relative)
{
return $this->relativePath."/".$file;
}
else
{
return $this->path."/".$file;
}
}
public function getLinkArray ()
{
global $config;
$linkArray = array();
if ($this->hasCover)
{
array_push ($linkArray, Data::getLink ($this, "jpg", "image/jpeg", Link::OPDS_IMAGE_TYPE, "cover.jpg", NULL));
$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));
}
foreach ($this->getDatas () as $data)
{
if ($data->isKnownType ())
{
array_push ($linkArray, $data->getDataLink (Link::OPDS_ACQUISITION_TYPE, "Download"));
}
}
foreach ($this->getAuthors () as $author) {
array_push ($linkArray, new LinkNavigation ($author->getUri (), "related", str_format (localize ("bookentry.author"), localize ("splitByLetter.book.other"), $author->name)));
}
$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)));
}
return $linkArray;
}
public function getEntry () {
return new EntryBook ($this->getTitle (), $this->getEntryId (),
$this->getComment (), "text/html",
$this->getLinkArray (), $this);
}
public static function getCount() {
global $config;
$nBooks = parent::getDb ()->query('select count(*) from books')->fetchColumn();
$result = array();
$entry = new Entry (localize ("allbooks.title"),
self::ALL_BOOKS_ID,
str_format (localize ("allbooks.alphabetical"), $nBooks), "text",
array ( new LinkNavigation ("?page=".parent::PAGE_ALL_BOOKS)));
array_push ($result, $entry);
$entry = new Entry (localize ("recent.title"),
self::ALL_RECENT_BOOKS_ID,
str_format (localize ("recent.list"), $config['cops_recentbooks_limit']), "text",
array ( new LinkNavigation ("?page=".parent::PAGE_ALL_RECENT_BOOKS)));
array_push ($result, $entry);
return $result;
}
public static function getBooksByAuthor($authorId) {
$result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . '
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');
$entryArray = array();
$result->execute (array ($authorId));
while ($post = $result->fetchObject ())
{
$book = new Book ($post);
array_push ($entryArray, $book->getEntry ());
}
return $entryArray;
}
public static function getBooksBySeries($serieId) {
$result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . '
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');
$entryArray = array();
$result->execute (array ($serieId));
while ($post = $result->fetchObject ())
{
$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);
array_push ($entryArray, $book->getEntry ());
}
return $entryArray;
}
public static function getBookById($bookId) {
$result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . '
from books left outer join comments on book = books.id
where books.id = ?');
$entryArray = array();
$result->execute (array ($bookId));
while ($post = $result->fetchObject ())
{
$book = new Book ($post);
return $book;
}
return NULL;
}
public static function getBookByDataId($dataId) {
$result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . '
from data, books left outer join comments on comments.book = books.id
where data.book = books.id and data.id = ?');
$entryArray = array();
$result->execute (array ($dataId));
while ($post = $result->fetchObject ())
{
$book = new Book ($post);
return $book;
}
return NULL;
}
public static function getBooksByQuery($query) {
$result = parent::getDb ()->prepare("select " . self::BOOK_COLUMNS . "
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 ?");
$entryArray = array();
$queryLike = "%" . $query . "%";
$result->execute (array ($queryLike, $queryLike));
while ($post = $result->fetchObject ())
{
$book = new Book ($post);
array_push ($entryArray, $book->getEntry ());
}
return $entryArray;
}
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)");
$entryArray = array();
while ($post = $result->fetchObject ())
{
array_push ($entryArray, new Entry ($post->title, Book::getEntryIdByLetter ($post->title),
str_format (localize("bookword.many"), $post->count), "text",
array ( new LinkNavigation ("?page=".parent::PAGE_ALL_BOOKS_LETTER."&id=".$post->title))));
}
return $entryArray;
}
public static function getBooksByStartingLetter($letter) {
$result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . '
from books left outer join comments on book = books.id
where upper (books.sort) like ?');
$entryArray = array();
$queryLike = $letter . "%";
$result->execute (array ($queryLike));
while ($post = $result->fetchObject ())
{
$book = new Book ($post);
array_push ($entryArray, $book->getEntry ());
}
return $entryArray;
}
public static function getAllRecentBooks() {
global $config;
$result = parent::getDb ()->query("select " . self::BOOK_COLUMNS . "
from books left outer join comments on book = books.id
order by timestamp desc limit " . $config['cops_recentbooks_limit']);
$entryArray = array();
while ($post = $result->fetchObject ())
{
$book = new Book ($post);
array_push ($entryArray, $book->getEntry ());
}
return $entryArray;
}
}
?>