First attempt to add paging in the OPDS side.

Need more polishing before being used everywhere.

--HG--
extra : rebase_source : 0fb3a4b12cf83d4b450a1fca81f82addd8d58c14
This commit is contained in:
Sébastien Lucas 2012-09-18 15:39:22 +02:00
parent 43f489051d
commit 4c9e8c67d0
5 changed files with 67 additions and 27 deletions

View file

@ -200,18 +200,27 @@ class OPDSRenderer
} }
public function render ($page) { public function render ($page) {
global $config;
self::startXmlDocument ($page->title, $page->idPage); self::startXmlDocument ($page->title, $page->idPage);
if ($page->query) if ($config['cops_max_item_per_page'] != -1 && $page->totalNumber != -1)
{ {
self::getXmlStream ()->startElement ("opensearch:totalResults"); self::getXmlStream ()->startElement ("opensearch:totalResults");
self::getXmlStream ()->text (count($page->entryArray)); self::getXmlStream ()->text ($page->totalNumber);
self::getXmlStream ()->endElement (); self::getXmlStream ()->endElement ();
self::getXmlStream ()->startElement ("opensearch:itemsPerPage"); self::getXmlStream ()->startElement ("opensearch:itemsPerPage");
self::getXmlStream ()->text (count($page->entryArray)); self::getXmlStream ()->text ($config['cops_max_item_per_page']);
self::getXmlStream ()->endElement (); self::getXmlStream ()->endElement ();
self::getXmlStream ()->startElement ("opensearch:startIndex"); self::getXmlStream ()->startElement ("opensearch:startIndex");
self::getXmlStream ()->text ("1"); self::getXmlStream ()->text (($page->n - 1) * $config['cops_max_item_per_page'] + 1);
self::getXmlStream ()->endElement (); self::getXmlStream ()->endElement ();
$currentUrl = $_SERVER['QUERY_STRING'];
$currentUrl = preg_replace ("/\&n=.*?$/", "", "?" . $_SERVER['QUERY_STRING']);
if ($page->n > 1) {
self::renderLink (new LinkNavigation ($currentUrl . "&n=" . ($page->n - 1), "previous", "Page precedente"));
}
if (($page->n + 1) * $config['cops_max_item_per_page'] < $page->totalNumber) {
self::renderLink (new LinkNavigation ($currentUrl . "&n=" . ($page->n + 1), "next", "Page suivante"));
}
} }
foreach ($page->entryArray as $entry) { foreach ($page->entryArray as $entry) {
self::getXmlStream ()->startElement ("entry"); self::getXmlStream ()->startElement ("entry");

View file

@ -80,6 +80,7 @@ class Link
const OPDS_IMAGE_TYPE = "http://opds-spec.org/image"; const OPDS_IMAGE_TYPE = "http://opds-spec.org/image";
const OPDS_ACQUISITION_TYPE = "http://opds-spec.org/acquisition"; const OPDS_ACQUISITION_TYPE = "http://opds-spec.org/acquisition";
const OPDS_NAVIGATION_TYPE = "application/atom+xml;profile=opds-catalog;kind=navigation"; const OPDS_NAVIGATION_TYPE = "application/atom+xml;profile=opds-catalog;kind=navigation";
const OPDS_PAGING_TYPE = "application/atom+xml;profile=opds-catalog;kind=acquisition";
public $href; public $href;
public $type; public $type;
@ -190,43 +191,46 @@ class Page
public $idPage; public $idPage;
public $idGet; public $idGet;
public $query; public $query;
public $n;
public $totalNumber = -1;
public $entryArray = array(); public $entryArray = array();
public static function getPage ($pageId, $id, $query) public static function getPage ($pageId, $id, $query, $n)
{ {
switch ($pageId) { switch ($pageId) {
case Base::PAGE_ALL_AUTHORS : case Base::PAGE_ALL_AUTHORS :
return new PageAllAuthors ($id, $query); return new PageAllAuthors ($id, $query, $n);
case Base::PAGE_AUTHOR_DETAIL : case Base::PAGE_AUTHOR_DETAIL :
return new PageAuthorDetail ($id, $query); return new PageAuthorDetail ($id, $query, $n);
case Base::PAGE_ALL_TAGS : case Base::PAGE_ALL_TAGS :
return new PageAllTags ($id, $query); return new PageAllTags ($id, $query, $n);
case Base::PAGE_TAG_DETAIL : case Base::PAGE_TAG_DETAIL :
return new PageTagDetail ($id, $query); return new PageTagDetail ($id, $query, $n);
case Base::PAGE_ALL_SERIES : case Base::PAGE_ALL_SERIES :
return new PageAllSeries ($id, $query); return new PageAllSeries ($id, $query, $n);
case Base::PAGE_ALL_BOOKS : case Base::PAGE_ALL_BOOKS :
return new PageAllBooks ($id, $query); return new PageAllBooks ($id, $query, $n);
case Base::PAGE_ALL_BOOKS_LETTER: case Base::PAGE_ALL_BOOKS_LETTER:
return new PageAllBooksLetter ($id, $query); return new PageAllBooksLetter ($id, $query, $n);
case Base::PAGE_ALL_RECENT_BOOKS : case Base::PAGE_ALL_RECENT_BOOKS :
return new PageRecentBooks ($id, $query); return new PageRecentBooks ($id, $query, $n);
case Base::PAGE_SERIE_DETAIL : case Base::PAGE_SERIE_DETAIL :
return new PageSerieDetail ($id, $query); return new PageSerieDetail ($id, $query, $n);
case Base::PAGE_OPENSEARCH_QUERY : case Base::PAGE_OPENSEARCH_QUERY :
return new PageQueryResult ($id, $query); return new PageQueryResult ($id, $query, $n);
break; break;
default: default:
$page = new Page ($id, $query); $page = new Page ($id, $query, $n);
$page->idPage = "cops:catalog"; $page->idPage = "cops:catalog";
return $page; return $page;
} }
} }
public function __construct($pid, $pquery) { public function __construct($pid, $pquery, $pn) {
$this->idGet = $pid; $this->idGet = $pid;
$this->query = $pquery; $this->query = $pquery;
} $this->n = $pn;
}
public function InitializeContent () public function InitializeContent ()
{ {
@ -318,7 +322,7 @@ class PageAllBooksLetter extends Page
public function InitializeContent () public function InitializeContent ()
{ {
$this->title = str_format (localize ("splitByLetter.letter"), localize ("bookword.title"), $this->idGet); $this->title = str_format (localize ("splitByLetter.letter"), localize ("bookword.title"), $this->idGet);
$this->entryArray = Book::getBooksByStartingLetter ($this->idGet); list ($this->entryArray, $this->totalNumber) = Book::getBooksByStartingLetter ($this->idGet, $this->n);
$this->idPage = Book::getEntryIdByLetter ($this->idGet); $this->idPage = Book::getEntryIdByLetter ($this->idGet);
} }
} }
@ -373,6 +377,28 @@ abstract class Base
} }
} }
return self::$db; return self::$db;
} }
public static function executeQuery($query, $columns, $params, $n) {
global $config;
$totalResult = -1;
if ($config['cops_max_item_per_page'] != -1)
{
// First check total number of results
$result = self::getDb ()->prepare (str_format ($query, "count(*)"));
$result->execute ($params);
$totalResult = $result->fetchColumn ();
// Next modify the query and params
$query .= " limit ?, ?";
array_push ($params, ($n - 1) * $config['cops_max_item_per_page'], $config['cops_max_item_per_page']);
}
$result = self::getDb ()->prepare(str_format ($query, $columns));
$result->execute ($params);
return array ($totalResult, $result);
}
} }
?> ?>

View file

@ -363,19 +363,17 @@ order by substr (upper (sort), 1, 1)");
return $entryArray; return $entryArray;
} }
public static function getBooksByStartingLetter($letter) { public static function getBooksByStartingLetter($letter, $n) {
$result = parent::getDb ()->prepare('select ' . self::BOOK_COLUMNS . ' list ($totalNumber, $result) = parent::executeQuery ('select {0}
from books left outer join comments on book = books.id from books left outer join comments on book = books.id
where upper (books.sort) like ?'); where upper (books.sort) like ?', self::BOOK_COLUMNS, array ($letter . "%"), $n);
$entryArray = array(); $entryArray = array();
$queryLike = $letter . "%";
$result->execute (array ($queryLike));
while ($post = $result->fetchObject ()) while ($post = $result->fetchObject ())
{ {
$book = new Book ($post); $book = new Book ($post);
array_push ($entryArray, $book->getEntry ()); array_push ($entryArray, $book->getEntry ());
} }
return $entryArray; return array ($entryArray, $totalNumber);
} }

View file

@ -97,5 +97,11 @@
* 0 : always generate valid OPDS code * 0 : always generate valid OPDS code
*/ */
$config['cops_generate_invalid_opds_stream'] = "0"; $config['cops_generate_invalid_opds_stream'] = "0";
/*
* Max number of items per page
*/
$config['cops_max_item_per_page'] = "2";
?> ?>

View file

@ -18,6 +18,7 @@
header ("Content-Type:application/xml"); header ("Content-Type:application/xml");
$page = getURLParam ("page", Base::PAGE_INDEX); $page = getURLParam ("page", Base::PAGE_INDEX);
$query = getURLParam ("query"); $query = getURLParam ("query");
$n = getURLParam ("n", "1");
if ($query) if ($query)
$page = Base::PAGE_OPENSEARCH_QUERY; $page = Base::PAGE_OPENSEARCH_QUERY;
$qid = getURLParam ("id"); $qid = getURLParam ("id");
@ -29,7 +30,7 @@
echo $OPDSRender->getOpenSearch (); echo $OPDSRender->getOpenSearch ();
return; return;
default: default:
$currentPage = Page::getPage ($page, $qid, $query); $currentPage = Page::getPage ($page, $qid, $query, $n);
$currentPage->InitializeContent (); $currentPage->InitializeContent ();
echo $OPDSRender->render ($currentPage); echo $OPDSRender->render ($currentPage);
return; return;