FeedBuilder.php 8.2 KB

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