Big refactoring of the Json handling, I followed what I did from OPDS. It doesn't still feel right but I think it's better.

This commit is contained in:
Sébastien Lucas 2014-01-07 21:22:35 +01:00
parent 3f5dd74361
commit 91aa062860
5 changed files with 219 additions and 216 deletions

207
JSON_renderer.php Normal file
View file

@ -0,0 +1,207 @@
<?php
/**
* COPS (Calibre OPDS PHP Server) class file
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Sébastien Lucas <sebastien@slucas.fr>
*/
require_once ("base.php");
require_once ("book.php");
class JSONRenderer
{
public static function getBookContentArray ($book) {
global $config;
$i = 0;
$preferedData = array ();
foreach ($config['cops_prefered_format'] as $format)
{
if ($i == 2) { break; }
if ($data = $book->getDataFormat ($format)) {
$i++;
array_push ($preferedData, array ("url" => $data->getHtmlLink (), "name" => $format));
}
}
$publisher = $book->getPublisher();
if (is_null ($publisher)) {
$pn = "";
$pu = "";
} else {
$pn = $publisher->name;
$link = new LinkNavigation ($publisher->getUri ());
$pu = $link->hrefXhtml ();
}
$serie = $book->getSerie ();
if (is_null ($serie)) {
$sn = "";
$scn = "";
$su = "";
} else {
$sn = $serie->name;
$scn = str_format (localize ("content.series.data"), $book->seriesIndex, $serie->name);
$link = new LinkNavigation ($serie->getUri ());
$su = $link->hrefXhtml ();
}
return array ("id" => $book->id,
"hasCover" => $book->hasCover,
"preferedData" => $preferedData,
"rating" => $book->getRating (),
"publisherName" => $pn,
"publisherurl" => $pu,
"pubDate" => $book->getPubDate (),
"languagesName" => $book->getLanguages (),
"authorsName" => $book->getAuthorsName (),
"tagsName" => $book->getTagsName (),
"seriesName" => $sn,
"seriesIndex" => $book->seriesIndex,
"seriesCompleteName" => $scn,
"seriesurl" => $su);
}
public static function getFullBookContentArray ($book) {
global $config;
$out = self::getBookContentArray ($book);
$out ["coverurl"] = Data::getLink ($book, "jpg", "image/jpeg", Link::OPDS_IMAGE_TYPE, "cover.jpg", NULL)->hrefXhtml ();
$out ["thumbnailurl"] = Data::getLink ($book, "jpg", "image/jpeg", Link::OPDS_THUMBNAIL_TYPE, "cover.jpg", NULL, NULL, $config['cops_html_thumbnail_height'] * 2)->hrefXhtml ();
$out ["content"] = $book->getComment (false);
$out ["datas"] = array ();
$dataKindle = $book->GetMostInterestingDataToSendToKindle ();
foreach ($book->getDatas() as $data) {
$tab = array ("id" => $data->id, "format" => $data->format, "url" => $data->getHtmlLink (), "mail" => 0);
if (!empty ($config['cops_mail_configuration']) && !is_null ($dataKindle) && $data->id == $dataKindle->id) {
$tab ["mail"] = 1;
}
array_push ($out ["datas"], $tab);
}
$out ["authors"] = array ();
foreach ($book->getAuthors () as $author) {
$link = new LinkNavigation ($author->getUri ());
array_push ($out ["authors"], array ("name" => $author->name, "url" => $link->hrefXhtml ()));
}
$out ["tags"] = array ();
foreach ($book->getTags () as $tag) {
$link = new LinkNavigation ($tag->getUri ());
array_push ($out ["tags"], array ("name" => $tag->name, "url" => $link->hrefXhtml ()));
}
;
return $out;
}
public static function getContentArray ($entry) {
if ($entry instanceof EntryBook) {
$out = array ( "title" => $entry->title);
$out ["book"] = self::getBookContentArray ($entry->book);
return $out;
}
return array ( "title" => $entry->title, "content" => $entry->content, "navlink" => $entry->getNavLink () );
}
public static function getJson ($complete = false) {
global $config;
$page = getURLParam ("page", Base::PAGE_INDEX);
$query = getURLParam ("query");
$search = getURLParam ("search");
$qid = getURLParam ("id");
$n = getURLParam ("n", "1");
$database = GetUrlParam (DB);
$currentPage = Page::getPage ($page, $qid, $query, $n);
$currentPage->InitializeContent ();
if ($search) {
return $currentPage->getContentArrayTypeahead ();
}
$out = array ( "title" => $currentPage->title);
$entries = array ();
foreach ($currentPage->entryArray as $entry) {
array_push ($entries, self::getContentArray ($entry));
}
if (!is_null ($currentPage->book)) {
$out ["book"] = self::getFullBookContentArray ($currentPage->book);
}
$out ["databaseId"] = GetUrlParam (DB, "");
$out ["databaseName"] = Base::getDbName ();
$out ["page"] = $page;
$out ["multipleDatabase"] = Base::isMultipleDatabaseEnabled () ? 1 : 0;
$out ["entries"] = $entries;
$out ["isPaginated"] = 0;
if ($currentPage->isPaginated ()) {
$prevLink = $currentPage->getPrevLink ();
$nextLink = $currentPage->getNextLink ();
$out ["isPaginated"] = 1;
$out ["prevLink"] = "";
if (!is_null ($prevLink)) {
$out ["prevLink"] = $prevLink->hrefXhtml ();
}
$out ["nextLink"] = "";
if (!is_null ($nextLink)) {
$out ["nextLink"] = $nextLink->hrefXhtml ();
}
$out ["maxPage"] = $currentPage->getMaxPage ();
$out ["currentPage"] = $currentPage->n;
}
if (!is_null (getURLParam ("complete")) || $complete) {
$out ["c"] = array ("version" => VERSION, "i18n" => array (
"coverAlt" => localize("i18n.coversection"),
"authorsTitle" => localize("authors.title"),
"bookwordTitle" => localize("bookword.title"),
"tagsTitle" => localize("tags.title"),
"seriesTitle" => localize("series.title"),
"customizeTitle" => localize ("customize.title"),
"aboutTitle" => localize ("about.title"),
"previousAlt" => localize ("paging.previous.alternate"),
"nextAlt" => localize ("paging.next.alternate"),
"searchAlt" => localize ("search.alternate"),
"sortAlt" => localize ("sort.alternate"),
"homeAlt" => localize ("home.alternate"),
"cogAlt" => localize ("cog.alternate"),
"permalinkAlt" => localize ("permalink.alternate"),
"publisherName" => localize("publisher.name"),
"pubdateTitle" => localize("pubdate.title"),
"languagesTitle" => localize("language.title"),
"contentTitle" => localize("content.summary"),
"sortorderAsc" => localize("search.sortorder.asc"),
"sortorderDesc" => localize("search.sortorder.desc"),
"customizeEmail" => localize("customize.email")),
"url" => array (
"detailUrl" => "index.php?page=13&id={0}&db={1}",
"coverUrl" => "fetch.php?id={0}&db={1}",
"thumbnailUrl" => "fetch.php?height=" . $config['cops_html_thumbnail_height'] . "&id={0}&db={1}"),
"config" => array (
"use_fancyapps" => $config ["cops_use_fancyapps"],
"max_item_per_page" => $config['cops_max_item_per_page'],
"server_side_rendering" => useServerSideRendering (),
"html_tag_filter" => $config['cops_html_tag_filter']));
if ($config['cops_thumbnail_handling'] == "1") {
$out ["c"]["url"]["thumbnailUrl"] = $out ["c"]["url"]["coverUrl"];
} else if (!empty ($config['cops_thumbnail_handling'])) {
$out ["c"]["url"]["thumbnailUrl"] = $config['cops_thumbnail_handling'];
}
}
$out ["containsBook"] = 0;
if ($currentPage->containsBook ()) {
$out ["containsBook"] = 1;
}
$out["abouturl"] = "index.php" . addURLParameter ("?page=16", DB, $database);
if ($page == Base::PAGE_ABOUT) {
$temp = preg_replace ("/\<h1\>About COPS\<\/h1\>/", "<h1>About COPS " . VERSION . "</h1>", file_get_contents('about.html'));
$out ["fullhtml"] = $temp;
}
$out ["homeurl"] = "index.php";
if ($page != Base::PAGE_INDEX && !is_null ($database)) $out ["homeurl"] = $out ["homeurl"] . "?" . addURLParameter ("", DB, $database);
return $out;
}
}

View file

@ -337,10 +337,6 @@ class Entry
return "#"; return "#";
} }
public function getContentArray () {
return array ( "title" => $this->title, "content" => $this->content, "navlink" => $this->getNavLink () );
}
public function __construct($ptitle, $pid, $pcontent, $pcontentType, $plinkArray, $pclass = "") { public function __construct($ptitle, $pid, $pcontent, $pcontentType, $plinkArray, $pclass = "") {
global $config; global $config;
$this->title = $ptitle; $this->title = $ptitle;
@ -375,12 +371,6 @@ class EntryBook extends Entry
$this->localUpdated = $pbook->timestamp; $this->localUpdated = $pbook->timestamp;
} }
public function getContentArray () {
$entry = array ( "title" => $this->title);
$entry ["book"] = $this->book->getContentArray ();
return $entry;
}
public function getCoverThumbnail () { public function getCoverThumbnail () {
foreach ($this->linkArray as $link) { foreach ($this->linkArray as $link) {
if ($link->rel == Link::OPDS_THUMBNAIL_TYPE) if ($link->rel == Link::OPDS_THUMBNAIL_TYPE)

193
book.php
View file

@ -125,92 +125,6 @@ class Book extends Base {
return $this->title; return $this->title;
} }
/* Json export */
public function getContentArray () {
global $config;
$i = 0;
$preferedData = array ();
foreach ($config['cops_prefered_format'] as $format)
{
if ($i == 2) { break; }
if ($data = $this->getDataFormat ($format)) {
$i++;
array_push ($preferedData, array ("url" => $data->getHtmlLink (), "name" => $format));
}
}
$publisher = $this->getPublisher();
if (is_null ($publisher)) {
$pn = "";
$pu = "";
} else {
$pn = $publisher->name;
$link = new LinkNavigation ($publisher->getUri ());
$pu = $link->hrefXhtml ();
}
$serie = $this->getSerie ();
if (is_null ($serie)) {
$sn = "";
$scn = "";
$su = "";
} else {
$sn = $serie->name;
$scn = str_format (localize ("content.series.data"), $this->seriesIndex, $serie->name);
$link = new LinkNavigation ($serie->getUri ());
$su = $link->hrefXhtml ();
}
return array ("id" => $this->id,
"hasCover" => $this->hasCover,
"preferedData" => $preferedData,
"rating" => $this->getRating (),
"publisherName" => $pn,
"publisherurl" => $pu,
"pubDate" => $this->getPubDate (),
"languagesName" => $this->getLanguages (),
"authorsName" => $this->getAuthorsName (),
"tagsName" => $this->getTagsName (),
"seriesName" => $sn,
"seriesIndex" => $this->seriesIndex,
"seriesCompleteName" => $scn,
"seriesurl" => $su);
}
public function getFullContentArray () {
global $config;
$out = $this->getContentArray ();
$out ["coverurl"] = Data::getLink ($this, "jpg", "image/jpeg", Link::OPDS_IMAGE_TYPE, "cover.jpg", NULL)->hrefXhtml ();
$out ["thumbnailurl"] = Data::getLink ($this, "jpg", "image/jpeg", Link::OPDS_THUMBNAIL_TYPE, "cover.jpg", NULL, NULL, $config['cops_html_thumbnail_height'] * 2)->hrefXhtml ();
$out ["content"] = $this->getComment (false);
$out ["datas"] = array ();
$dataKindle = $this->GetMostInterestingDataToSendToKindle ();
foreach ($this->getDatas() as $data) {
$tab = array ("id" => $data->id, "format" => $data->format, "url" => $data->getHtmlLink (), "mail" => 0);
if (!empty ($config['cops_mail_configuration']) && !is_null ($dataKindle) && $data->id == $dataKindle->id) {
$tab ["mail"] = 1;
}
array_push ($out ["datas"], $tab);
}
$out ["authors"] = array ();
foreach ($this->getAuthors () as $author) {
$link = new LinkNavigation ($author->getUri ());
array_push ($out ["authors"], array ("name" => $author->name, "url" => $link->hrefXhtml ()));
}
$out ["tags"] = array ();
foreach ($this->getTags () as $tag) {
$link = new LinkNavigation ($tag->getUri ());
array_push ($out ["tags"], array ("name" => $tag->name, "url" => $link->hrefXhtml ()));
}
;
return $out;
}
/* End of Json export */
/* Other class (author, series, tag, ...) initialization and accessors */ /* Other class (author, series, tag, ...) initialization and accessors */
public function getAuthors () { public function getAuthors () {
@ -270,7 +184,7 @@ class Book extends Base {
} }
return $this->tags; return $this->tags;
} }
public function getTagsName () { public function getTagsName () {
return implode (", ", array_map (function ($tag) { return $tag->name; }, $this->getTags ())); return implode (", ", array_map (function ($tag) { return $tag->name; }, $this->getTags ()));
} }
@ -337,7 +251,7 @@ class Book extends Base {
} }
return NULL; return NULL;
} }
public function getRating () { public function getRating () {
if (is_null ($this->rating) || $this->rating == 0) { if (is_null ($this->rating) || $this->rating == 0) {
return ""; return "";
@ -669,106 +583,3 @@ order by substr (upper (sort), 1, 1)");
} }
} }
function getJson ($complete = false) {
global $config;
$page = getURLParam ("page", Base::PAGE_INDEX);
$query = getURLParam ("query");
$search = getURLParam ("search");
$qid = getURLParam ("id");
$n = getURLParam ("n", "1");
$database = GetUrlParam (DB);
$currentPage = Page::getPage ($page, $qid, $query, $n);
$currentPage->InitializeContent ();
if ($search) {
return $currentPage->getContentArrayTypeahead ();
}
$out = array ( "title" => $currentPage->title);
$entries = array ();
foreach ($currentPage->entryArray as $entry) {
array_push ($entries, $entry->getContentArray ());
}
if (!is_null ($currentPage->book)) {
$out ["book"] = $currentPage->book->getFullContentArray ();
}
$out ["databaseId"] = GetUrlParam (DB, "");
$out ["databaseName"] = Base::getDbName ();
$out ["page"] = $page;
$out ["multipleDatabase"] = Base::isMultipleDatabaseEnabled () ? 1 : 0;
$out ["entries"] = $entries;
$out ["isPaginated"] = 0;
if ($currentPage->isPaginated ()) {
$prevLink = $currentPage->getPrevLink ();
$nextLink = $currentPage->getNextLink ();
$out ["isPaginated"] = 1;
$out ["prevLink"] = "";
if (!is_null ($prevLink)) {
$out ["prevLink"] = $prevLink->hrefXhtml ();
}
$out ["nextLink"] = "";
if (!is_null ($nextLink)) {
$out ["nextLink"] = $nextLink->hrefXhtml ();
}
$out ["maxPage"] = $currentPage->getMaxPage ();
$out ["currentPage"] = $currentPage->n;
}
if (!is_null (getURLParam ("complete")) || $complete) {
$out ["c"] = array ("version" => VERSION, "i18n" => array (
"coverAlt" => localize("i18n.coversection"),
"authorsTitle" => localize("authors.title"),
"bookwordTitle" => localize("bookword.title"),
"tagsTitle" => localize("tags.title"),
"seriesTitle" => localize("series.title"),
"customizeTitle" => localize ("customize.title"),
"aboutTitle" => localize ("about.title"),
"previousAlt" => localize ("paging.previous.alternate"),
"nextAlt" => localize ("paging.next.alternate"),
"searchAlt" => localize ("search.alternate"),
"sortAlt" => localize ("sort.alternate"),
"homeAlt" => localize ("home.alternate"),
"cogAlt" => localize ("cog.alternate"),
"permalinkAlt" => localize ("permalink.alternate"),
"publisherName" => localize("publisher.name"),
"pubdateTitle" => localize("pubdate.title"),
"languagesTitle" => localize("language.title"),
"contentTitle" => localize("content.summary"),
"sortorderAsc" => localize("search.sortorder.asc"),
"sortorderDesc" => localize("search.sortorder.desc"),
"customizeEmail" => localize("customize.email")),
"url" => array (
"detailUrl" => "index.php?page=13&id={0}&db={1}",
"coverUrl" => "fetch.php?id={0}&db={1}",
"thumbnailUrl" => "fetch.php?height=" . $config['cops_html_thumbnail_height'] . "&id={0}&db={1}"),
"config" => array (
"use_fancyapps" => $config ["cops_use_fancyapps"],
"max_item_per_page" => $config['cops_max_item_per_page'],
"server_side_rendering" => useServerSideRendering (),
"html_tag_filter" => $config['cops_html_tag_filter']));
if ($config['cops_thumbnail_handling'] == "1") {
$out ["c"]["url"]["thumbnailUrl"] = $out ["c"]["url"]["coverUrl"];
} else if (!empty ($config['cops_thumbnail_handling'])) {
$out ["c"]["url"]["thumbnailUrl"] = $config['cops_thumbnail_handling'];
}
}
$out ["containsBook"] = 0;
if ($currentPage->containsBook ()) {
$out ["containsBook"] = 1;
}
$out["abouturl"] = "index.php" . addURLParameter ("?page=16", DB, $database);
if ($page == Base::PAGE_ABOUT) {
$temp = preg_replace ("/\<h1\>About COPS\<\/h1\>/", "<h1>About COPS " . VERSION . "</h1>", file_get_contents('about.html'));
$out ["fullhtml"] = $temp;
}
$out ["homeurl"] = "index.php";
if ($page != Base::PAGE_INDEX && !is_null ($database)) $out ["homeurl"] = $out ["homeurl"] . "?" . addURLParameter ("", DB, $database);
return $out;
}

View file

@ -8,16 +8,10 @@
*/ */
require_once ("config.php"); require_once ("config.php");
require_once ("base.php"); require_once ("JSON_renderer.php");
require_once ("author.php");
require_once ("serie.php");
require_once ("tag.php");
require_once ("language.php");
require_once ("customcolumn.php");
require_once ("book.php");
header ("Content-Type:application/json;charset=utf-8"); header ("Content-Type:application/json;charset=utf-8");
echo json_encode (getJson ()); echo json_encode (JSONRenderer::getJson ());

View file

@ -8,6 +8,7 @@
require_once (dirname(__FILE__) . "/config_test.php"); require_once (dirname(__FILE__) . "/config_test.php");
require_once (dirname(__FILE__) . "/../book.php"); require_once (dirname(__FILE__) . "/../book.php");
require_once (dirname(__FILE__) . "/../JSON_renderer.php");
/* /*
Publishers: Publishers:
@ -321,7 +322,7 @@ class BookTest extends PHPUnit_Framework_TestCase
$_GET["query"] = "fic"; $_GET["query"] = "fic";
$_GET["search"] = "1"; $_GET["search"] = "1";
$array = getJson (); $array = JSONRenderer::getJson ();
$this->assertCount (3, $array); $this->assertCount (3, $array);
$this->assertEquals ("2 tags", $array[0]["title"]); $this->assertEquals ("2 tags", $array[0]["title"]);
@ -331,7 +332,7 @@ class BookTest extends PHPUnit_Framework_TestCase
$_GET["query"] = "car"; $_GET["query"] = "car";
$_GET["search"] = "1"; $_GET["search"] = "1";
$array = getJson (); $array = JSONRenderer::getJson ();
$this->assertCount (4, $array); $this->assertCount (4, $array);
$this->assertEquals ("1 book", $array[0]["title"]); $this->assertEquals ("1 book", $array[0]["title"]);
@ -342,7 +343,7 @@ class BookTest extends PHPUnit_Framework_TestCase
$_GET["query"] = "art"; $_GET["query"] = "art";
$_GET["search"] = "1"; $_GET["search"] = "1";
$array = getJson (); $array = JSONRenderer::getJson ();
$this->assertCount (4, $array); $this->assertCount (4, $array);
$this->assertEquals ("1 author", $array[0]["title"]); $this->assertEquals ("1 author", $array[0]["title"]);
@ -353,7 +354,7 @@ class BookTest extends PHPUnit_Framework_TestCase
$_GET["query"] = "Macmillan"; $_GET["query"] = "Macmillan";
$_GET["search"] = "1"; $_GET["search"] = "1";
$array = getJson (); $array = JSONRenderer::getJson ();
$this->assertCount (3, $array); $this->assertCount (3, $array);
$this->assertEquals ("2 publishers", $array[0]["title"]); $this->assertEquals ("2 publishers", $array[0]["title"]);
@ -372,7 +373,7 @@ class BookTest extends PHPUnit_Framework_TestCase
$_GET["search"] = "1"; $_GET["search"] = "1";
$config ['cops_ignored_categories'] = array ("author"); $config ['cops_ignored_categories'] = array ("author");
$array = getJson (); $array = JSONRenderer::getJson ();
$this->assertCount (2, $array); $this->assertCount (2, $array);
$this->assertEquals ("1 book", $array[0]["title"]); $this->assertEquals ("1 book", $array[0]["title"]);
@ -383,7 +384,7 @@ class BookTest extends PHPUnit_Framework_TestCase
$_GET["search"] = "1"; $_GET["search"] = "1";
$config ['cops_ignored_categories'] = array ("series"); $config ['cops_ignored_categories'] = array ("series");
$array = getJson (); $array = JSONRenderer::getJson ();
$this->assertCount (2, $array); $this->assertCount (2, $array);
$this->assertEquals ("1 author", $array[0]["title"]); $this->assertEquals ("1 author", $array[0]["title"]);
@ -404,7 +405,7 @@ class BookTest extends PHPUnit_Framework_TestCase
$config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/BaseWithSomeBooks/", $config['calibre_directory'] = array ("Some books" => dirname(__FILE__) . "/BaseWithSomeBooks/",
"One book" => dirname(__FILE__) . "/BaseWithOneBook/"); "One book" => dirname(__FILE__) . "/BaseWithOneBook/");
$array = getJson (); $array = JSONRenderer::getJson ();
$this->assertCount (5, $array); $this->assertCount (5, $array);
$this->assertEquals ("Some books", $array[0]["title"]); $this->assertEquals ("Some books", $array[0]["title"]);