2016-03-12 16:08:01 +01:00
|
|
|
<?php
|
2020-09-22 20:25:47 +02:00
|
|
|
|
2018-12-03 00:08:04 +01:00
|
|
|
namespace Shaarli\Feed;
|
|
|
|
|
|
|
|
use DateTime;
|
2019-05-25 15:52:27 +02:00
|
|
|
use Shaarli\Bookmark\Bookmark;
|
|
|
|
use Shaarli\Bookmark\BookmarkServiceInterface;
|
|
|
|
use Shaarli\Formatter\BookmarkFormatter;
|
2016-03-12 16:08:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* FeedBuilder class.
|
|
|
|
*
|
|
|
|
* Used to build ATOM and RSS feeds data.
|
|
|
|
*/
|
|
|
|
class FeedBuilder
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string Constant: RSS feed type.
|
|
|
|
*/
|
|
|
|
public static $FEED_RSS = 'rss';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Constant: ATOM feed type.
|
|
|
|
*/
|
|
|
|
public static $FEED_ATOM = 'atom';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Default language if the locale isn't set.
|
|
|
|
*/
|
|
|
|
public static $DEFAULT_LANGUAGE = 'en-en';
|
|
|
|
|
|
|
|
/**
|
2019-05-25 15:52:27 +02:00
|
|
|
* @var int Number of bookmarks to display in a feed by default.
|
2016-03-12 16:08:01 +01:00
|
|
|
*/
|
|
|
|
public static $DEFAULT_NB_LINKS = 50;
|
|
|
|
|
|
|
|
/**
|
2019-05-25 15:52:27 +02:00
|
|
|
* @var BookmarkServiceInterface instance.
|
2016-03-12 16:08:01 +01:00
|
|
|
*/
|
|
|
|
protected $linkDB;
|
|
|
|
|
2019-05-25 15:52:27 +02:00
|
|
|
/**
|
|
|
|
* @var BookmarkFormatter instance.
|
|
|
|
*/
|
|
|
|
protected $formatter;
|
|
|
|
|
2020-05-18 13:03:13 +02:00
|
|
|
/** @var mixed[] $_SERVER */
|
2016-03-12 16:08:01 +01:00
|
|
|
protected $serverInfo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean True if the user is currently logged in, false otherwise.
|
|
|
|
*/
|
|
|
|
protected $isLoggedIn;
|
|
|
|
|
|
|
|
/**
|
2019-05-25 15:52:27 +02:00
|
|
|
* @var boolean Use permalinks instead of direct bookmarks if true.
|
2016-03-12 16:08:01 +01:00
|
|
|
*/
|
|
|
|
protected $usePermalinks;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean true to hide dates in feeds.
|
|
|
|
*/
|
|
|
|
protected $hideDates;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string server locale.
|
|
|
|
*/
|
|
|
|
protected $locale;
|
|
|
|
/**
|
|
|
|
* @var DateTime Latest item date.
|
|
|
|
*/
|
|
|
|
protected $latestDate;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Feed constructor.
|
|
|
|
*
|
2019-05-25 15:52:27 +02:00
|
|
|
* @param BookmarkServiceInterface $linkDB LinkDB instance.
|
|
|
|
* @param BookmarkFormatter $formatter instance.
|
2018-12-03 01:10:39 +01:00
|
|
|
* @param array $serverInfo $_SERVER.
|
2019-05-25 15:52:27 +02:00
|
|
|
* @param boolean $isLoggedIn True if the user is currently logged in, false otherwise.
|
2016-03-12 16:08:01 +01:00
|
|
|
*/
|
2020-05-18 17:17:36 +02:00
|
|
|
public function __construct($linkDB, $formatter, $serverInfo, $isLoggedIn)
|
2016-03-12 16:08:01 +01:00
|
|
|
{
|
|
|
|
$this->linkDB = $linkDB;
|
2019-05-25 15:52:27 +02:00
|
|
|
$this->formatter = $formatter;
|
2016-03-12 16:08:01 +01:00
|
|
|
$this->serverInfo = $serverInfo;
|
|
|
|
$this->isLoggedIn = $isLoggedIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build data for feed templates.
|
|
|
|
*
|
2020-05-18 13:03:13 +02:00
|
|
|
* @param string $feedType Type of feed (RSS/ATOM).
|
|
|
|
* @param array $userInput $_GET.
|
|
|
|
*
|
2016-03-12 16:08:01 +01:00
|
|
|
* @return array Formatted data for feeds templates.
|
|
|
|
*/
|
2020-05-18 13:03:13 +02:00
|
|
|
public function buildData(string $feedType, ?array $userInput)
|
2016-03-12 16:08:01 +01:00
|
|
|
{
|
2019-05-25 15:52:27 +02:00
|
|
|
// Search for untagged bookmarks
|
2020-05-18 13:03:13 +02:00
|
|
|
if (isset($this->userInput['searchtags']) && empty($userInput['searchtags'])) {
|
|
|
|
$userInput['searchtags'] = false;
|
2017-04-01 12:17:37 +02:00
|
|
|
}
|
|
|
|
|
2016-03-12 16:08:01 +01:00
|
|
|
// Optionally filter the results:
|
2020-10-02 17:50:59 +02:00
|
|
|
$linksToDisplay = $this->linkDB->search($userInput ?? [], null, false, false, true);
|
2016-03-12 16:08:01 +01:00
|
|
|
|
2020-05-18 13:03:13 +02:00
|
|
|
$nblinksToDisplay = $this->getNbLinks(count($linksToDisplay), $userInput);
|
2016-03-12 16:08:01 +01:00
|
|
|
|
|
|
|
// Can't use array_keys() because $link is a LinkDB instance and not a real array.
|
2020-09-22 20:25:47 +02:00
|
|
|
$keys = [];
|
2016-03-12 16:08:01 +01:00
|
|
|
foreach ($linksToDisplay as $key => $value) {
|
|
|
|
$keys[] = $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pageaddr = escape(index_url($this->serverInfo));
|
2019-05-25 15:52:27 +02:00
|
|
|
$this->formatter->addContextData('index_url', $pageaddr);
|
2020-09-22 20:25:47 +02:00
|
|
|
$linkDisplayed = [];
|
2016-03-12 16:08:01 +01:00
|
|
|
for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++) {
|
2020-05-18 13:03:13 +02:00
|
|
|
$linkDisplayed[$keys[$i]] = $this->buildItem($feedType, $linksToDisplay[$keys[$i]], $pageaddr);
|
2016-03-12 16:08:01 +01:00
|
|
|
}
|
|
|
|
|
2020-05-18 13:03:13 +02:00
|
|
|
$data['language'] = $this->getTypeLanguage($feedType);
|
|
|
|
$data['last_update'] = $this->getLatestDateFormatted($feedType);
|
2016-03-12 16:08:01 +01:00
|
|
|
$data['show_dates'] = !$this->hideDates || $this->isLoggedIn;
|
2020-09-03 14:51:41 +02:00
|
|
|
// Remove leading path from REQUEST_URI (already contained in $pageaddr).
|
|
|
|
$requestUri = preg_replace('#(.*?/)(feed.*)#', '$2', escape($this->serverInfo['REQUEST_URI']));
|
|
|
|
$data['self_link'] = $pageaddr . $requestUri;
|
2016-03-12 16:08:01 +01:00
|
|
|
$data['index_url'] = $pageaddr;
|
|
|
|
$data['usepermalinks'] = $this->usePermalinks === true;
|
|
|
|
$data['links'] = $linkDisplayed;
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2020-05-18 13:03:13 +02:00
|
|
|
/**
|
|
|
|
* Set this to true to use permalinks instead of direct bookmarks.
|
|
|
|
*
|
|
|
|
* @param boolean $usePermalinks true to force permalinks.
|
|
|
|
*/
|
|
|
|
public function setUsePermalinks($usePermalinks)
|
|
|
|
{
|
|
|
|
$this->usePermalinks = $usePermalinks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set this to true to hide timestamps in feeds.
|
|
|
|
*
|
|
|
|
* @param boolean $hideDates true to enable.
|
|
|
|
*/
|
|
|
|
public function setHideDates($hideDates)
|
|
|
|
{
|
|
|
|
$this->hideDates = $hideDates;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the locale. Used to show feed language.
|
|
|
|
*
|
|
|
|
* @param string $locale The locale (eg. 'fr_FR.UTF8').
|
|
|
|
*/
|
|
|
|
public function setLocale($locale)
|
|
|
|
{
|
|
|
|
$this->locale = strtolower($locale);
|
|
|
|
}
|
|
|
|
|
2016-03-12 16:08:01 +01:00
|
|
|
/**
|
|
|
|
* Build a feed item (one per shaare).
|
|
|
|
*
|
2020-05-18 13:03:13 +02:00
|
|
|
* @param string $feedType Type of feed (RSS/ATOM).
|
2019-05-25 15:52:27 +02:00
|
|
|
* @param Bookmark $link Single link array extracted from LinkDB.
|
|
|
|
* @param string $pageaddr Index URL.
|
2016-03-12 16:08:01 +01:00
|
|
|
*
|
|
|
|
* @return array Link array with feed attributes.
|
|
|
|
*/
|
2020-05-18 13:03:13 +02:00
|
|
|
protected function buildItem(string $feedType, $link, $pageaddr)
|
2016-03-12 16:08:01 +01:00
|
|
|
{
|
2019-05-25 15:52:27 +02:00
|
|
|
$data = $this->formatter->format($link);
|
2020-07-28 20:46:11 +02:00
|
|
|
$data['guid'] = rtrim($pageaddr, '/') . '/shaare/' . $data['shorturl'];
|
2016-03-12 16:08:01 +01:00
|
|
|
if ($this->usePermalinks === true) {
|
2020-09-22 20:25:47 +02:00
|
|
|
$permalink = '<a href="' . $data['url'] . '" title="' . t('Direct link') . '">' . t('Direct link') . '</a>';
|
2016-03-12 16:08:01 +01:00
|
|
|
} else {
|
2020-09-22 20:25:47 +02:00
|
|
|
$permalink = '<a href="' . $data['guid'] . '" title="' . t('Permalink') . '">' . t('Permalink') . '</a>';
|
2016-03-12 16:08:01 +01:00
|
|
|
}
|
2019-05-25 15:52:27 +02:00
|
|
|
$data['description'] .= PHP_EOL . PHP_EOL . '<br>— ' . $permalink;
|
2016-03-12 16:08:01 +01:00
|
|
|
|
2020-05-18 13:03:13 +02:00
|
|
|
$data['pub_iso_date'] = $this->getIsoDate($feedType, $data['created']);
|
2016-03-12 16:08:01 +01:00
|
|
|
|
2016-08-03 09:45:28 +02:00
|
|
|
// atom:entry elements MUST contain exactly one atom:updated element.
|
2019-05-25 15:52:27 +02:00
|
|
|
if (!empty($link->getUpdated())) {
|
2020-05-18 13:03:13 +02:00
|
|
|
$data['up_iso_date'] = $this->getIsoDate($feedType, $data['updated'], DateTime::ATOM);
|
2016-03-12 16:08:01 +01:00
|
|
|
} else {
|
2020-05-18 13:03:13 +02:00
|
|
|
$data['up_iso_date'] = $this->getIsoDate($feedType, $data['created'], DateTime::ATOM);
|
2016-03-12 16:08:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the more recent item.
|
2019-05-25 15:52:27 +02:00
|
|
|
if (empty($this->latestDate) || $this->latestDate < $data['created']) {
|
|
|
|
$this->latestDate = $data['created'];
|
2016-08-03 09:45:28 +02:00
|
|
|
}
|
2019-05-25 15:52:27 +02:00
|
|
|
if (!empty($data['updated']) && $this->latestDate < $data['updated']) {
|
|
|
|
$this->latestDate = $data['updated'];
|
2016-03-12 16:08:01 +01:00
|
|
|
}
|
|
|
|
|
2019-05-25 15:52:27 +02:00
|
|
|
return $data;
|
2016-03-12 16:08:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the language according to the feed type, based on the locale:
|
|
|
|
*
|
|
|
|
* - RSS format: en-us (default: 'en-en').
|
|
|
|
* - ATOM format: fr (default: 'en').
|
|
|
|
*
|
2020-05-18 13:03:13 +02:00
|
|
|
* @param string $feedType Type of feed (RSS/ATOM).
|
|
|
|
*
|
2016-03-12 16:08:01 +01:00
|
|
|
* @return string The language.
|
|
|
|
*/
|
2020-05-18 13:03:13 +02:00
|
|
|
protected function getTypeLanguage(string $feedType)
|
2016-03-12 16:08:01 +01:00
|
|
|
{
|
|
|
|
// Use the locale do define the language, if available.
|
2018-12-03 00:08:04 +01:00
|
|
|
if (!empty($this->locale) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale)) {
|
2020-05-18 13:03:13 +02:00
|
|
|
$length = ($feedType === self::$FEED_RSS) ? 5 : 2;
|
2016-03-12 16:08:01 +01:00
|
|
|
return str_replace('_', '-', substr($this->locale, 0, $length));
|
|
|
|
}
|
2020-05-18 13:03:13 +02:00
|
|
|
return ($feedType === self::$FEED_RSS) ? 'en-en' : 'en';
|
2016-03-12 16:08:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format the latest item date found according to the feed type.
|
|
|
|
*
|
|
|
|
* Return an empty string if invalid DateTime is passed.
|
|
|
|
*
|
2020-05-18 13:03:13 +02:00
|
|
|
* @param string $feedType Type of feed (RSS/ATOM).
|
|
|
|
*
|
2016-03-12 16:08:01 +01:00
|
|
|
* @return string Formatted date.
|
|
|
|
*/
|
2020-05-18 13:03:13 +02:00
|
|
|
protected function getLatestDateFormatted(string $feedType)
|
2016-03-12 16:08:01 +01:00
|
|
|
{
|
|
|
|
if (empty($this->latestDate) || !$this->latestDate instanceof DateTime) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2020-05-18 13:03:13 +02:00
|
|
|
$type = ($feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM;
|
2016-03-12 16:08:01 +01:00
|
|
|
return $this->latestDate->format($type);
|
|
|
|
}
|
|
|
|
|
2016-08-03 09:45:28 +02:00
|
|
|
/**
|
|
|
|
* Get ISO date from DateTime according to feed type.
|
|
|
|
*
|
2020-05-18 13:03:13 +02:00
|
|
|
* @param string $feedType Type of feed (RSS/ATOM).
|
2016-08-03 09:45:28 +02:00
|
|
|
* @param DateTime $date Date to format.
|
|
|
|
* @param string|bool $format Force format.
|
|
|
|
*
|
|
|
|
* @return string Formatted date.
|
|
|
|
*/
|
2020-05-18 13:03:13 +02:00
|
|
|
protected function getIsoDate(string $feedType, DateTime $date, $format = false)
|
2016-08-03 09:45:28 +02:00
|
|
|
{
|
|
|
|
if ($format !== false) {
|
|
|
|
return $date->format($format);
|
|
|
|
}
|
2020-05-18 13:03:13 +02:00
|
|
|
if ($feedType == self::$FEED_RSS) {
|
2016-08-03 09:45:28 +02:00
|
|
|
return $date->format(DateTime::RSS);
|
|
|
|
}
|
|
|
|
return $date->format(DateTime::ATOM);
|
|
|
|
}
|
|
|
|
|
2016-03-12 16:08:01 +01:00
|
|
|
/**
|
|
|
|
* Returns the number of link to display according to 'nb' user input parameter.
|
|
|
|
*
|
|
|
|
* If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS.
|
2019-05-25 15:52:27 +02:00
|
|
|
* If 'nb' is set to 'all', display all filtered bookmarks (max parameter).
|
2016-03-12 16:08:01 +01:00
|
|
|
*
|
2020-05-18 13:03:13 +02:00
|
|
|
* @param int $max maximum number of bookmarks to display.
|
|
|
|
* @param array $userInput $_GET.
|
2016-03-12 16:08:01 +01:00
|
|
|
*
|
2019-05-25 15:52:27 +02:00
|
|
|
* @return int number of bookmarks to display.
|
2016-03-12 16:08:01 +01:00
|
|
|
*/
|
2020-05-18 13:03:13 +02:00
|
|
|
protected function getNbLinks($max, ?array $userInput)
|
2016-03-12 16:08:01 +01:00
|
|
|
{
|
2020-05-18 13:03:13 +02:00
|
|
|
if (empty($userInput['nb'])) {
|
2016-03-12 16:08:01 +01:00
|
|
|
return self::$DEFAULT_NB_LINKS;
|
|
|
|
}
|
|
|
|
|
2020-05-18 13:03:13 +02:00
|
|
|
if ($userInput['nb'] == 'all') {
|
2016-03-12 16:08:01 +01:00
|
|
|
return $max;
|
|
|
|
}
|
|
|
|
|
2020-05-18 13:03:13 +02:00
|
|
|
$intNb = intval($userInput['nb']);
|
2018-12-03 00:08:04 +01:00
|
|
|
if (!is_int($intNb) || $intNb == 0) {
|
2016-03-12 16:08:01 +01:00
|
|
|
return self::$DEFAULT_NB_LINKS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $intNb;
|
|
|
|
}
|
|
|
|
}
|