2016-03-12 16:08:01 +01:00
|
|
|
<?php
|
2018-12-03 00:08:04 +01:00
|
|
|
namespace Shaarli\Feed;
|
|
|
|
|
|
|
|
use DateTime;
|
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';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int Number of links to display in a feed by default.
|
|
|
|
*/
|
|
|
|
public static $DEFAULT_NB_LINKS = 50;
|
|
|
|
|
|
|
|
/**
|
2018-12-03 01:10:39 +01:00
|
|
|
* @var \Shaarli\Bookmark\LinkDB instance.
|
2016-03-12 16:08:01 +01:00
|
|
|
*/
|
|
|
|
protected $linkDB;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string RSS or ATOM feed.
|
|
|
|
*/
|
|
|
|
protected $feedType;
|
|
|
|
|
|
|
|
/**
|
2018-12-03 00:08:04 +01:00
|
|
|
* @var array $_SERVER
|
2016-03-12 16:08:01 +01:00
|
|
|
*/
|
|
|
|
protected $serverInfo;
|
|
|
|
|
|
|
|
/**
|
2018-12-03 00:08:04 +01:00
|
|
|
* @var array $_GET
|
2016-03-12 16:08:01 +01:00
|
|
|
*/
|
|
|
|
protected $userInput;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean True if the user is currently logged in, false otherwise.
|
|
|
|
*/
|
|
|
|
protected $isLoggedIn;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var boolean Use permalinks instead of direct links if true.
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
2018-12-03 01:10:39 +01:00
|
|
|
* @param \Shaarli\Bookmark\LinkDB $linkDB LinkDB instance.
|
|
|
|
* @param string $feedType Type of feed.
|
|
|
|
* @param array $serverInfo $_SERVER.
|
|
|
|
* @param array $userInput $_GET.
|
|
|
|
* @param boolean $isLoggedIn True if the user is currently logged in,
|
|
|
|
* false otherwise.
|
2016-03-12 16:08:01 +01:00
|
|
|
*/
|
|
|
|
public function __construct($linkDB, $feedType, $serverInfo, $userInput, $isLoggedIn)
|
|
|
|
{
|
|
|
|
$this->linkDB = $linkDB;
|
|
|
|
$this->feedType = $feedType;
|
|
|
|
$this->serverInfo = $serverInfo;
|
|
|
|
$this->userInput = $userInput;
|
|
|
|
$this->isLoggedIn = $isLoggedIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build data for feed templates.
|
|
|
|
*
|
|
|
|
* @return array Formatted data for feeds templates.
|
|
|
|
*/
|
|
|
|
public function buildData()
|
|
|
|
{
|
2017-04-01 12:17:37 +02:00
|
|
|
// Search for untagged links
|
|
|
|
if (isset($this->userInput['searchtags']) && empty($this->userInput['searchtags'])) {
|
|
|
|
$this->userInput['searchtags'] = false;
|
|
|
|
}
|
|
|
|
|
2016-03-12 16:08:01 +01:00
|
|
|
// Optionally filter the results:
|
2016-03-21 21:40:49 +01:00
|
|
|
$linksToDisplay = $this->linkDB->filterSearch($this->userInput);
|
2016-03-12 16:08:01 +01:00
|
|
|
|
|
|
|
$nblinksToDisplay = $this->getNbLinks(count($linksToDisplay));
|
|
|
|
|
|
|
|
// Can't use array_keys() because $link is a LinkDB instance and not a real array.
|
|
|
|
$keys = array();
|
|
|
|
foreach ($linksToDisplay as $key => $value) {
|
|
|
|
$keys[] = $key;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pageaddr = escape(index_url($this->serverInfo));
|
|
|
|
$linkDisplayed = array();
|
|
|
|
for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++) {
|
|
|
|
$linkDisplayed[$keys[$i]] = $this->buildItem($linksToDisplay[$keys[$i]], $pageaddr);
|
|
|
|
}
|
|
|
|
|
|
|
|
$data['language'] = $this->getTypeLanguage();
|
|
|
|
$data['last_update'] = $this->getLatestDateFormatted();
|
|
|
|
$data['show_dates'] = !$this->hideDates || $this->isLoggedIn;
|
|
|
|
// Remove leading slash from REQUEST_URI.
|
2016-10-16 20:52:35 +02:00
|
|
|
$data['self_link'] = escape(server_url($this->serverInfo))
|
2018-12-03 00:08:04 +01:00
|
|
|
. escape($this->serverInfo['REQUEST_URI']);
|
2016-03-12 16:08:01 +01:00
|
|
|
$data['index_url'] = $pageaddr;
|
|
|
|
$data['usepermalinks'] = $this->usePermalinks === true;
|
|
|
|
$data['links'] = $linkDisplayed;
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build a feed item (one per shaare).
|
|
|
|
*
|
|
|
|
* @param array $link Single link array extracted from LinkDB.
|
|
|
|
* @param string $pageaddr Index URL.
|
|
|
|
*
|
|
|
|
* @return array Link array with feed attributes.
|
|
|
|
*/
|
|
|
|
protected function buildItem($link, $pageaddr)
|
|
|
|
{
|
2018-12-03 00:08:04 +01:00
|
|
|
$link['guid'] = $pageaddr . '?' . $link['shorturl'];
|
2019-02-09 14:13:08 +01:00
|
|
|
// Prepend the root URL for notes
|
|
|
|
if (is_note($link['url'])) {
|
2016-03-12 16:08:01 +01:00
|
|
|
$link['url'] = $pageaddr . $link['url'];
|
|
|
|
}
|
|
|
|
if ($this->usePermalinks === true) {
|
2018-12-03 00:08:04 +01:00
|
|
|
$permalink = '<a href="' . $link['url'] . '" title="' . t('Direct link') . '">' . t('Direct link') . '</a>';
|
2016-03-12 16:08:01 +01:00
|
|
|
} else {
|
2018-12-03 00:08:04 +01:00
|
|
|
$permalink = '<a href="' . $link['guid'] . '" title="' . t('Permalink') . '">' . t('Permalink') . '</a>';
|
2016-03-12 16:08:01 +01:00
|
|
|
}
|
2018-12-03 00:08:04 +01:00
|
|
|
$link['description'] = format_description($link['description'], '', false, $pageaddr);
|
|
|
|
$link['description'] .= PHP_EOL . '<br>— ' . $permalink;
|
2016-03-12 16:08:01 +01:00
|
|
|
|
2016-11-28 16:16:44 +01:00
|
|
|
$pubDate = $link['created'];
|
2016-08-03 09:45:28 +02:00
|
|
|
$link['pub_iso_date'] = $this->getIsoDate($pubDate);
|
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.
|
|
|
|
if (!empty($link['updated'])) {
|
2016-11-28 16:16:44 +01:00
|
|
|
$upDate = $link['updated'];
|
2016-08-03 09:45:28 +02:00
|
|
|
$link['up_iso_date'] = $this->getIsoDate($upDate, DateTime::ATOM);
|
2016-03-12 16:08:01 +01:00
|
|
|
} else {
|
2018-10-13 00:19:03 +02:00
|
|
|
$link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime::ATOM);
|
2016-03-12 16:08:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save the more recent item.
|
2016-08-03 09:45:28 +02:00
|
|
|
if (empty($this->latestDate) || $this->latestDate < $pubDate) {
|
|
|
|
$this->latestDate = $pubDate;
|
|
|
|
}
|
|
|
|
if (!empty($upDate) && $this->latestDate < $upDate) {
|
|
|
|
$this->latestDate = $upDate;
|
2016-03-12 16:08:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$taglist = array_filter(explode(' ', $link['tags']), 'strlen');
|
|
|
|
uasort($taglist, 'strcasecmp');
|
|
|
|
$link['taglist'] = $taglist;
|
|
|
|
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set this to true to use permalinks instead of direct links.
|
|
|
|
*
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the language according to the feed type, based on the locale:
|
|
|
|
*
|
|
|
|
* - RSS format: en-us (default: 'en-en').
|
|
|
|
* - ATOM format: fr (default: 'en').
|
|
|
|
*
|
|
|
|
* @return string The language.
|
|
|
|
*/
|
|
|
|
public function getTypeLanguage()
|
|
|
|
{
|
|
|
|
// 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)) {
|
|
|
|
$length = ($this->feedType === self::$FEED_RSS) ? 5 : 2;
|
2016-03-12 16:08:01 +01:00
|
|
|
return str_replace('_', '-', substr($this->locale, 0, $length));
|
|
|
|
}
|
2018-12-03 00:08:04 +01:00
|
|
|
return ($this->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.
|
|
|
|
*
|
|
|
|
* @return string Formatted date.
|
|
|
|
*/
|
|
|
|
protected function getLatestDateFormatted()
|
|
|
|
{
|
|
|
|
if (empty($this->latestDate) || !$this->latestDate instanceof DateTime) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = ($this->feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM;
|
|
|
|
return $this->latestDate->format($type);
|
|
|
|
}
|
|
|
|
|
2016-08-03 09:45:28 +02:00
|
|
|
/**
|
|
|
|
* Get ISO date from DateTime according to feed type.
|
|
|
|
*
|
|
|
|
* @param DateTime $date Date to format.
|
|
|
|
* @param string|bool $format Force format.
|
|
|
|
*
|
|
|
|
* @return string Formatted date.
|
|
|
|
*/
|
|
|
|
protected function getIsoDate(DateTime $date, $format = false)
|
|
|
|
{
|
|
|
|
if ($format !== false) {
|
|
|
|
return $date->format($format);
|
|
|
|
}
|
|
|
|
if ($this->feedType == self::$FEED_RSS) {
|
|
|
|
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.
|
|
|
|
* If 'nb' is set to 'all', display all filtered links (max parameter).
|
|
|
|
*
|
|
|
|
* @param int $max maximum number of links to display.
|
|
|
|
*
|
|
|
|
* @return int number of links to display.
|
|
|
|
*/
|
|
|
|
public function getNbLinks($max)
|
|
|
|
{
|
|
|
|
if (empty($this->userInput['nb'])) {
|
|
|
|
return self::$DEFAULT_NB_LINKS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->userInput['nb'] == 'all') {
|
|
|
|
return $max;
|
|
|
|
}
|
|
|
|
|
|
|
|
$intNb = intval($this->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;
|
|
|
|
}
|
|
|
|
}
|