From 82ac1d1e676f93b1567eba608c00c6edaf401a9e Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Mon, 8 Aug 2022 12:04:02 +0200 Subject: Refactor entry-to-GReader API format (#4490) * Refactor entry to GReader API format Some code was copied in two locations and not completely uniform. Cleaning of related variables and functions (e.g. better types for entries and categories as objects vs. as IDs). Usecase: I need to call the same GReader-compatible serialization from an extension * Fixed some edge cases * Keep summary instead of content `summary` and `content` seems to be used interchangeably in the Google Reader API. We have been using `summary` for our client API and `content` in our export/import, so stick to that. --- p/api/greader.php | 86 +++++++------------------------------------------------ 1 file changed, 11 insertions(+), 75 deletions(-) (limited to 'p/api/greader.php') diff --git a/p/api/greader.php b/p/api/greader.php index 9a96823d7..1ceb68f0d 100644 --- a/p/api/greader.php +++ b/p/api/greader.php @@ -29,13 +29,6 @@ require(LIB_PATH . '/lib_rss.php'); //Includes class autoloader $ORIGINAL_INPUT = file_get_contents('php://input', false, null, 0, 1048576); if (PHP_INT_SIZE < 8) { //32-bit - /** - * @param string|int $dec - * @return string - */ - function dec2hex($dec) { - return str_pad(gmp_strval(gmp_init($dec, 10), 16), 16, '0', STR_PAD_LEFT); - } /** * @param string $hex * @return string @@ -45,14 +38,6 @@ if (PHP_INT_SIZE < 8) { //32-bit return gmp_strval(gmp_init($hex, 16), 10); } } else { //64-bit - /** - * @param string|int $dec - * @return string - */ - function dec2hex($dec) { - //http://code.google.com/p/google-reader-api/wiki/ItemId - return str_pad(dechex($dec), 16, '0', STR_PAD_LEFT); - } /** * @param string $hex * @return string @@ -526,8 +511,9 @@ function entriesToArray($entries) { if (empty($entries)) { return array(); } - $feedDAO = FreshRSS_Factory::createFeedDao(); - $arrayFeedCategoryNames = $feedDAO->arrayFeedCategoryNames(); + $catDAO = FreshRSS_Factory::createCategoryDao(); + $categories = $catDAO->listCategories(true); + $tagDAO = FreshRSS_Factory::createTagDao(); $entryIdsTagNames = $tagDAO->getEntryIdsTagNames($entries); if ($entryIdsTagNames == false) { @@ -541,65 +527,15 @@ function entriesToArray($entries) { if ($entry == null) { continue; } - $f_id = $entry->feed(); - if (isset($arrayFeedCategoryNames[$f_id])) { - $c_name = $arrayFeedCategoryNames[$f_id]['c_name']; - $f_name = $arrayFeedCategoryNames[$f_id]['name']; - } else { - $c_name = '_'; - $f_name = '_'; - } - $item = array( - 'id' => 'tag:google.com,2005:reader/item/' . dec2hex($entry->id()), //64-bit hexa http://code.google.com/p/google-reader-api/wiki/ItemId - 'crawlTimeMsec' => substr($entry->dateAdded(true, true), 0, -3), - 'timestampUsec' => '' . $entry->dateAdded(true, true), //EasyRSS & Reeder - 'published' => $entry->date(true), - 'title' => escapeToUnicodeAlternative($entry->title(), false), - 'summary' => array('content' => $entry->content()), - 'canonical' => array( - array('href' => htmlspecialchars_decode($entry->link(), ENT_QUOTES)), - ), - 'alternate' => array( - array('href' => htmlspecialchars_decode($entry->link(), ENT_QUOTES)), - ), - 'categories' => array( - 'user/-/state/com.google/reading-list', - 'user/-/label/' . htmlspecialchars_decode($c_name, ENT_QUOTES), - ), - 'origin' => array( - 'streamId' => 'feed/' . $f_id, - 'title' => escapeToUnicodeAlternative($f_name, true), //EasyRSS - //'htmlUrl' => $line['f_website'], - ), - ); - foreach ($entry->enclosures() as $enclosure) { - if (!empty($enclosure['url']) && !empty($enclosure['type'])) { - $media = [ - 'href' => $enclosure['url'], - 'type' => $enclosure['type'], - ]; - if (!empty($enclosure['length'])) { - $media['length'] = intval($enclosure['length']); - } - $item['enclosure'][] = $media; - } - } - $author = $entry->authors(true); - $author = trim($author, '; '); - if ($author != '') { - $item['author'] = escapeToUnicodeAlternative($author, false); - } - if ($entry->isRead()) { - $item['categories'][] = 'user/-/state/com.google/read'; - } - if ($entry->isFavorite()) { - $item['categories'][] = 'user/-/state/com.google/starred'; - } - $tagNames = isset($entryIdsTagNames['e_' . $entry->id()]) ? $entryIdsTagNames['e_' . $entry->id()] : array(); - foreach ($tagNames as $tagName) { - $item['categories'][] = 'user/-/label/' . htmlspecialchars_decode($tagName, ENT_QUOTES); + + $feed = FreshRSS_CategoryDAO::findFeed($categories, $entry->feedId()); + $entry->_feed($feed); + + if (isset($entryIdsTagNames['e_' . $entry->id()])) { + $entry->_tags($entryIdsTagNames['e_' . $entry->id()]); } - $items[] = $item; + + $items[] = $entry->toGReader('compat'); } return $items; } -- cgit v1.2.3