FeedBuilder.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 .'?'. $link['shorturl'];
  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'], '', $pageaddr);
  135. $link['description'] .= PHP_EOL .'<br>&#8212; '. $permalink;
  136. $pubDate = $link['created'];
  137. $link['pub_iso_date'] = $this->getIsoDate($pubDate);
  138. // atom:entry elements MUST contain exactly one atom:updated element.
  139. if (!empty($link['updated'])) {
  140. $upDate = $link['updated'];
  141. $link['up_iso_date'] = $this->getIsoDate($upDate, DateTime::ATOM);
  142. } else {
  143. $link['up_iso_date'] = $this->getIsoDate($pubDate, DateTime::ATOM);;
  144. }
  145. // Save the more recent item.
  146. if (empty($this->latestDate) || $this->latestDate < $pubDate) {
  147. $this->latestDate = $pubDate;
  148. }
  149. if (!empty($upDate) && $this->latestDate < $upDate) {
  150. $this->latestDate = $upDate;
  151. }
  152. $taglist = array_filter(explode(' ', $link['tags']), 'strlen');
  153. uasort($taglist, 'strcasecmp');
  154. $link['taglist'] = $taglist;
  155. return $link;
  156. }
  157. /**
  158. * Assign PubSub hub URL.
  159. *
  160. * @param string $pubsubhubUrl PubSub hub url.
  161. */
  162. public function setPubsubhubUrl($pubsubhubUrl)
  163. {
  164. $this->pubsubhubUrl = $pubsubhubUrl;
  165. }
  166. /**
  167. * Set this to true to use permalinks instead of direct links.
  168. *
  169. * @param boolean $usePermalinks true to force permalinks.
  170. */
  171. public function setUsePermalinks($usePermalinks)
  172. {
  173. $this->usePermalinks = $usePermalinks;
  174. }
  175. /**
  176. * Set this to true to hide timestamps in feeds.
  177. *
  178. * @param boolean $hideDates true to enable.
  179. */
  180. public function setHideDates($hideDates)
  181. {
  182. $this->hideDates = $hideDates;
  183. }
  184. /**
  185. * Set the locale. Used to show feed language.
  186. *
  187. * @param string $locale The locale (eg. 'fr_FR.UTF8').
  188. */
  189. public function setLocale($locale)
  190. {
  191. $this->locale = strtolower($locale);
  192. }
  193. /**
  194. * Get the language according to the feed type, based on the locale:
  195. *
  196. * - RSS format: en-us (default: 'en-en').
  197. * - ATOM format: fr (default: 'en').
  198. *
  199. * @return string The language.
  200. */
  201. public function getTypeLanguage()
  202. {
  203. // Use the locale do define the language, if available.
  204. if (! empty($this->locale) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale)) {
  205. $length = ($this->feedType == self::$FEED_RSS) ? 5 : 2;
  206. return str_replace('_', '-', substr($this->locale, 0, $length));
  207. }
  208. return ($this->feedType == self::$FEED_RSS) ? 'en-en' : 'en';
  209. }
  210. /**
  211. * Format the latest item date found according to the feed type.
  212. *
  213. * Return an empty string if invalid DateTime is passed.
  214. *
  215. * @return string Formatted date.
  216. */
  217. protected function getLatestDateFormatted()
  218. {
  219. if (empty($this->latestDate) || !$this->latestDate instanceof DateTime) {
  220. return '';
  221. }
  222. $type = ($this->feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM;
  223. return $this->latestDate->format($type);
  224. }
  225. /**
  226. * Get ISO date from DateTime according to feed type.
  227. *
  228. * @param DateTime $date Date to format.
  229. * @param string|bool $format Force format.
  230. *
  231. * @return string Formatted date.
  232. */
  233. protected function getIsoDate(DateTime $date, $format = false)
  234. {
  235. if ($format !== false) {
  236. return $date->format($format);
  237. }
  238. if ($this->feedType == self::$FEED_RSS) {
  239. return $date->format(DateTime::RSS);
  240. }
  241. return $date->format(DateTime::ATOM);
  242. }
  243. /**
  244. * Returns the number of link to display according to 'nb' user input parameter.
  245. *
  246. * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS.
  247. * If 'nb' is set to 'all', display all filtered links (max parameter).
  248. *
  249. * @param int $max maximum number of links to display.
  250. *
  251. * @return int number of links to display.
  252. */
  253. public function getNbLinks($max)
  254. {
  255. if (empty($this->userInput['nb'])) {
  256. return self::$DEFAULT_NB_LINKS;
  257. }
  258. if ($this->userInput['nb'] == 'all') {
  259. return $max;
  260. }
  261. $intNb = intval($this->userInput['nb']);
  262. if (! is_int($intNb) || $intNb == 0) {
  263. return self::$DEFAULT_NB_LINKS;
  264. }
  265. return $intNb;
  266. }
  267. }