FeedBuilder.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * FeedBuilder class.
  4. *
  5. * Used to build ATOM and RSS feeds data.
  6. */
  7. class FeedBuilder
  8. {
  9. /**
  10. * @var string Constant: RSS feed type.
  11. */
  12. public static $FEED_RSS = 'rss';
  13. /**
  14. * @var string Constant: ATOM feed type.
  15. */
  16. public static $FEED_ATOM = 'atom';
  17. /**
  18. * @var string Default language if the locale isn't set.
  19. */
  20. public static $DEFAULT_LANGUAGE = 'en-en';
  21. /**
  22. * @var int Number of links to display in a feed by default.
  23. */
  24. public static $DEFAULT_NB_LINKS = 50;
  25. /**
  26. * @var LinkDB instance.
  27. */
  28. protected $linkDB;
  29. /**
  30. * @var string RSS or ATOM feed.
  31. */
  32. protected $feedType;
  33. /**
  34. * @var array $_SERVER.
  35. */
  36. protected $serverInfo;
  37. /**
  38. * @var array $_GET.
  39. */
  40. protected $userInput;
  41. /**
  42. * @var boolean True if the user is currently logged in, false otherwise.
  43. */
  44. protected $isLoggedIn;
  45. /**
  46. * @var boolean Use permalinks instead of direct links if true.
  47. */
  48. protected $usePermalinks;
  49. /**
  50. * @var boolean true to hide dates in feeds.
  51. */
  52. protected $hideDates;
  53. /**
  54. * @var string PubSub hub URL.
  55. */
  56. protected $pubsubhubUrl;
  57. /**
  58. * @var string server locale.
  59. */
  60. protected $locale;
  61. /**
  62. * @var DateTime Latest item date.
  63. */
  64. protected $latestDate;
  65. /**
  66. * Feed constructor.
  67. *
  68. * @param LinkDB $linkDB LinkDB instance.
  69. * @param string $feedType Type of feed.
  70. * @param array $serverInfo $_SERVER.
  71. * @param array $userInput $_GET.
  72. * @param boolean $isLoggedIn True if the user is currently logged in, false otherwise.
  73. */
  74. public function __construct($linkDB, $feedType, $serverInfo, $userInput, $isLoggedIn)
  75. {
  76. $this->linkDB = $linkDB;
  77. $this->feedType = $feedType;
  78. $this->serverInfo = $serverInfo;
  79. $this->userInput = $userInput;
  80. $this->isLoggedIn = $isLoggedIn;
  81. }
  82. /**
  83. * Build data for feed templates.
  84. *
  85. * @return array Formatted data for feeds templates.
  86. */
  87. public function buildData()
  88. {
  89. // Optionally filter the results:
  90. $linksToDisplay = $this->linkDB->filterSearch($this->userInput);
  91. $nblinksToDisplay = $this->getNbLinks(count($linksToDisplay));
  92. // Can't use array_keys() because $link is a LinkDB instance and not a real array.
  93. $keys = array();
  94. foreach ($linksToDisplay as $key => $value) {
  95. $keys[] = $key;
  96. }
  97. $pageaddr = escape(index_url($this->serverInfo));
  98. $linkDisplayed = array();
  99. for ($i = 0; $i < $nblinksToDisplay && $i < count($keys); $i++) {
  100. $linkDisplayed[$keys[$i]] = $this->buildItem($linksToDisplay[$keys[$i]], $pageaddr);
  101. }
  102. $data['language'] = $this->getTypeLanguage();
  103. $data['pubsubhub_url'] = $this->pubsubhubUrl;
  104. $data['last_update'] = $this->getLatestDateFormatted();
  105. $data['show_dates'] = !$this->hideDates || $this->isLoggedIn;
  106. // Remove leading slash from REQUEST_URI.
  107. $data['self_link'] = escape(server_url($this->serverInfo))
  108. . escape($this->serverInfo['REQUEST_URI']);
  109. $data['index_url'] = $pageaddr;
  110. $data['usepermalinks'] = $this->usePermalinks === true;
  111. $data['links'] = $linkDisplayed;
  112. return $data;
  113. }
  114. /**
  115. * Build a feed item (one per shaare).
  116. *
  117. * @param array $link Single link array extracted from LinkDB.
  118. * @param string $pageaddr Index URL.
  119. *
  120. * @return array Link array with feed attributes.
  121. */
  122. protected function buildItem($link, $pageaddr)
  123. {
  124. $link['guid'] = $pageaddr .'?'. smallHash($link['linkdate']);
  125. // Check for both signs of a note: starting with ? and 7 chars long.
  126. if ($link['url'][0] === '?' && strlen($link['url']) === 7) {
  127. $link['url'] = $pageaddr . $link['url'];
  128. }
  129. if ($this->usePermalinks === true) {
  130. $permalink = '<a href="'. $link['url'] .'" title="Direct link">Direct link</a>';
  131. } else {
  132. $permalink = '<a href="'. $link['guid'] .'" title="Permalink">Permalink</a>';
  133. }
  134. $link['description'] = format_description($link['description']) . PHP_EOL .'<br>&#8212; '. $permalink;
  135. $pubDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['linkdate']);
  136. $link['pub_iso_date'] = $this->getIsoDate($pubDate);
  137. // atom:entry elements MUST contain exactly one atom:updated element.
  138. if (!empty($link['updated'])) {
  139. $upDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $link['updated']);
  140. $link['up_iso_date'] = $this->getIsoDate($upDate, DateTime::ATOM);
  141. } else {
  142. $link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime::ATOM);;
  143. }
  144. // Save the more recent item.
  145. if (empty($this->latestDate) || $this->latestDate < $pubDate) {
  146. $this->latestDate = $pubDate;
  147. }
  148. if (!empty($upDate) && $this->latestDate < $upDate) {
  149. $this->latestDate = $upDate;
  150. }
  151. $taglist = array_filter(explode(' ', $link['tags']), 'strlen');
  152. uasort($taglist, 'strcasecmp');
  153. $link['taglist'] = $taglist;
  154. return $link;
  155. }
  156. /**
  157. * Assign PubSub hub URL.
  158. *
  159. * @param string $pubsubhubUrl PubSub hub url.
  160. */
  161. public function setPubsubhubUrl($pubsubhubUrl)
  162. {
  163. $this->pubsubhubUrl = $pubsubhubUrl;
  164. }
  165. /**
  166. * Set this to true to use permalinks instead of direct links.
  167. *
  168. * @param boolean $usePermalinks true to force permalinks.
  169. */
  170. public function setUsePermalinks($usePermalinks)
  171. {
  172. $this->usePermalinks = $usePermalinks;
  173. }
  174. /**
  175. * Set this to true to hide timestamps in feeds.
  176. *
  177. * @param boolean $hideDates true to enable.
  178. */
  179. public function setHideDates($hideDates)
  180. {
  181. $this->hideDates = $hideDates;
  182. }
  183. /**
  184. * Set the locale. Used to show feed language.
  185. *
  186. * @param string $locale The locale (eg. 'fr_FR.UTF8').
  187. */
  188. public function setLocale($locale)
  189. {
  190. $this->locale = strtolower($locale);
  191. }
  192. /**
  193. * Get the language according to the feed type, based on the locale:
  194. *
  195. * - RSS format: en-us (default: 'en-en').
  196. * - ATOM format: fr (default: 'en').
  197. *
  198. * @return string The language.
  199. */
  200. public function getTypeLanguage()
  201. {
  202. // Use the locale do define the language, if available.
  203. if (! empty($this->locale) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale)) {
  204. $length = ($this->feedType == self::$FEED_RSS) ? 5 : 2;
  205. return str_replace('_', '-', substr($this->locale, 0, $length));
  206. }
  207. return ($this->feedType == self::$FEED_RSS) ? 'en-en' : 'en';
  208. }
  209. /**
  210. * Format the latest item date found according to the feed type.
  211. *
  212. * Return an empty string if invalid DateTime is passed.
  213. *
  214. * @return string Formatted date.
  215. */
  216. protected function getLatestDateFormatted()
  217. {
  218. if (empty($this->latestDate) || !$this->latestDate instanceof DateTime) {
  219. return '';
  220. }
  221. $type = ($this->feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM;
  222. return $this->latestDate->format($type);
  223. }
  224. /**
  225. * Get ISO date from DateTime according to feed type.
  226. *
  227. * @param DateTime $date Date to format.
  228. * @param string|bool $format Force format.
  229. *
  230. * @return string Formatted date.
  231. */
  232. protected function getIsoDate(DateTime $date, $format = false)
  233. {
  234. if ($format !== false) {
  235. return $date->format($format);
  236. }
  237. if ($this->feedType == self::$FEED_RSS) {
  238. return $date->format(DateTime::RSS);
  239. }
  240. return $date->format(DateTime::ATOM);
  241. }
  242. /**
  243. * Returns the number of link to display according to 'nb' user input parameter.
  244. *
  245. * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS.
  246. * If 'nb' is set to 'all', display all filtered links (max parameter).
  247. *
  248. * @param int $max maximum number of links to display.
  249. *
  250. * @return int number of links to display.
  251. */
  252. public function getNbLinks($max)
  253. {
  254. if (empty($this->userInput['nb'])) {
  255. return self::$DEFAULT_NB_LINKS;
  256. }
  257. if ($this->userInput['nb'] == 'all') {
  258. return $max;
  259. }
  260. $intNb = intval($this->userInput['nb']);
  261. if (! is_int($intNb) || $intNb == 0) {
  262. return self::$DEFAULT_NB_LINKS;
  263. }
  264. return $intNb;
  265. }
  266. }