FeedBuilder.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. // Search for untagged links
  86. if (isset($this->userInput['searchtags']) && empty($this->userInput['searchtags'])) {
  87. $this->userInput['searchtags'] = false;
  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['last_update'] = $this->getLatestDateFormatted();
  104. $data['show_dates'] = !$this->hideDates || $this->isLoggedIn;
  105. // Remove leading slash from REQUEST_URI.
  106. $data['self_link'] = escape(server_url($this->serverInfo))
  107. . escape($this->serverInfo['REQUEST_URI']);
  108. $data['index_url'] = $pageaddr;
  109. $data['usepermalinks'] = $this->usePermalinks === true;
  110. $data['links'] = $linkDisplayed;
  111. return $data;
  112. }
  113. /**
  114. * Build a feed item (one per shaare).
  115. *
  116. * @param array $link Single link array extracted from LinkDB.
  117. * @param string $pageaddr Index URL.
  118. *
  119. * @return array Link array with feed attributes.
  120. */
  121. protected function buildItem($link, $pageaddr)
  122. {
  123. $link['guid'] = $pageaddr .'?'. $link['shorturl'];
  124. // Check for both signs of a note: starting with ? and 7 chars long.
  125. if ($link['url'][0] === '?' && strlen($link['url']) === 7) {
  126. $link['url'] = $pageaddr . $link['url'];
  127. }
  128. if ($this->usePermalinks === true) {
  129. $permalink = '<a href="'. $link['url'] .'" title="'. t('Direct link') .'">'. t('Direct link') .'</a>';
  130. } else {
  131. $permalink = '<a href="'. $link['guid'] .'" title="'. t('Permalink') .'">'. t('Permalink') .'</a>';
  132. }
  133. $link['description'] = format_description($link['description'], '', false, $pageaddr);
  134. $link['description'] .= PHP_EOL .'<br>&#8212; '. $permalink;
  135. $pubDate = $link['created'];
  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 = $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. }
  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. * Set this to true to use permalinks instead of direct links.
  159. *
  160. * @param boolean $usePermalinks true to force permalinks.
  161. */
  162. public function setUsePermalinks($usePermalinks)
  163. {
  164. $this->usePermalinks = $usePermalinks;
  165. }
  166. /**
  167. * Set this to true to hide timestamps in feeds.
  168. *
  169. * @param boolean $hideDates true to enable.
  170. */
  171. public function setHideDates($hideDates)
  172. {
  173. $this->hideDates = $hideDates;
  174. }
  175. /**
  176. * Set the locale. Used to show feed language.
  177. *
  178. * @param string $locale The locale (eg. 'fr_FR.UTF8').
  179. */
  180. public function setLocale($locale)
  181. {
  182. $this->locale = strtolower($locale);
  183. }
  184. /**
  185. * Get the language according to the feed type, based on the locale:
  186. *
  187. * - RSS format: en-us (default: 'en-en').
  188. * - ATOM format: fr (default: 'en').
  189. *
  190. * @return string The language.
  191. */
  192. public function getTypeLanguage()
  193. {
  194. // Use the locale do define the language, if available.
  195. if (! empty($this->locale) && preg_match('/^\w{2}[_\-]\w{2}/', $this->locale)) {
  196. $length = ($this->feedType == self::$FEED_RSS) ? 5 : 2;
  197. return str_replace('_', '-', substr($this->locale, 0, $length));
  198. }
  199. return ($this->feedType == self::$FEED_RSS) ? 'en-en' : 'en';
  200. }
  201. /**
  202. * Format the latest item date found according to the feed type.
  203. *
  204. * Return an empty string if invalid DateTime is passed.
  205. *
  206. * @return string Formatted date.
  207. */
  208. protected function getLatestDateFormatted()
  209. {
  210. if (empty($this->latestDate) || !$this->latestDate instanceof DateTime) {
  211. return '';
  212. }
  213. $type = ($this->feedType == self::$FEED_RSS) ? DateTime::RSS : DateTime::ATOM;
  214. return $this->latestDate->format($type);
  215. }
  216. /**
  217. * Get ISO date from DateTime according to feed type.
  218. *
  219. * @param DateTime $date Date to format.
  220. * @param string|bool $format Force format.
  221. *
  222. * @return string Formatted date.
  223. */
  224. protected function getIsoDate(DateTime $date, $format = false)
  225. {
  226. if ($format !== false) {
  227. return $date->format($format);
  228. }
  229. if ($this->feedType == self::$FEED_RSS) {
  230. return $date->format(DateTime::RSS);
  231. }
  232. return $date->format(DateTime::ATOM);
  233. }
  234. /**
  235. * Returns the number of link to display according to 'nb' user input parameter.
  236. *
  237. * If 'nb' not set or invalid, default value: $DEFAULT_NB_LINKS.
  238. * If 'nb' is set to 'all', display all filtered links (max parameter).
  239. *
  240. * @param int $max maximum number of links to display.
  241. *
  242. * @return int number of links to display.
  243. */
  244. public function getNbLinks($max)
  245. {
  246. if (empty($this->userInput['nb'])) {
  247. return self::$DEFAULT_NB_LINKS;
  248. }
  249. if ($this->userInput['nb'] == 'all') {
  250. return $max;
  251. }
  252. $intNb = intval($this->userInput['nb']);
  253. if (! is_int($intNb) || $intNb == 0) {
  254. return self::$DEFAULT_NB_LINKS;
  255. }
  256. return $intNb;
  257. }
  258. }