PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/rssaction.php

https://github.com/Br3nda/laconica
PHP | 362 lines | 240 code | 43 blank | 79 comment | 20 complexity | 81a5af582ac2ec03a60dcbd75f941a02 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. /**
  3. * Laconica, the distributed open-source microblogging tool
  4. *
  5. * Base class for RSS 1.0 feed actions
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Mail
  23. * @package Laconica
  24. * @author Evan Prodromou <evan@controlyourself.ca>
  25. * @author Earle Martin <earle@downlode.org>
  26. * @copyright 2008-9 Control Yourself, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://laconi.ca/
  29. */
  30. if (!defined('LACONICA')) { exit(1); }
  31. define('DEFAULT_RSS_LIMIT', 48);
  32. class Rss10Action extends Action
  33. {
  34. # This will contain the details of each feed item's author and be used to generate SIOC data.
  35. var $creators = array();
  36. var $limit = DEFAULT_RSS_LIMIT;
  37. var $notices = null;
  38. /**
  39. * Constructor
  40. *
  41. * Just wraps the Action constructor.
  42. *
  43. * @param string $output URI to output to, default = stdout
  44. * @param boolean $indent Whether to indent output, default true
  45. *
  46. * @see Action::__construct
  47. */
  48. function __construct($output='php://output', $indent=true)
  49. {
  50. parent::__construct($output, $indent);
  51. }
  52. /**
  53. * Do we need to write to the database?
  54. *
  55. * @return boolean true
  56. */
  57. function isReadonly()
  58. {
  59. return true;
  60. }
  61. /**
  62. * Read arguments and initialize members
  63. *
  64. * @param array $args Arguments from $_REQUEST
  65. * @return boolean success
  66. */
  67. function prepare($args)
  68. {
  69. parent::prepare($args);
  70. $this->limit = (int) $this->trimmed('limit');
  71. if ($this->limit == 0) {
  72. $this->limit = DEFAULT_RSS_LIMIT;
  73. }
  74. return true;
  75. }
  76. /**
  77. * Handle a request
  78. *
  79. * @param array $args Arguments from $_REQUEST
  80. *
  81. * @return void
  82. */
  83. function handle($args)
  84. {
  85. // Parent handling, including cache check
  86. parent::handle($args);
  87. // Get the list of notices
  88. if (empty($this->tag)) {
  89. $this->notices = $this->getNotices($this->limit);
  90. } else {
  91. $this->notices = $this->getTaggedNotices($this->tag, $this->limit);
  92. }
  93. $this->showRss();
  94. }
  95. /**
  96. * Get the notices to output in this stream
  97. *
  98. * @return array an array of Notice objects sorted in reverse chron
  99. */
  100. function getNotices()
  101. {
  102. return array();
  103. }
  104. /**
  105. * Get a description of the channel
  106. *
  107. * Returns an array with the following
  108. * @return array
  109. */
  110. function getChannel()
  111. {
  112. return array('url' => '',
  113. 'title' => '',
  114. 'link' => '',
  115. 'description' => '');
  116. }
  117. function getImage()
  118. {
  119. return null;
  120. }
  121. function showRss()
  122. {
  123. $this->initRss();
  124. $this->showChannel();
  125. $this->showImage();
  126. foreach ($this->notices as $n) {
  127. $this->showItem($n);
  128. }
  129. $this->showCreators();
  130. $this->endRss();
  131. }
  132. function showChannel()
  133. {
  134. $channel = $this->getChannel();
  135. $image = $this->getImage();
  136. $this->elementStart('channel', array('rdf:about' => $channel['url']));
  137. $this->element('title', null, $channel['title']);
  138. $this->element('link', null, $channel['link']);
  139. $this->element('description', null, $channel['description']);
  140. $this->element('cc:licence', array('rdf:resource' => common_config('license','url')));
  141. if ($image) {
  142. $this->element('image', array('rdf:resource' => $image));
  143. }
  144. $this->elementStart('items');
  145. $this->elementStart('rdf:Seq');
  146. foreach ($this->notices as $notice) {
  147. $this->element('rdf:li', array('rdf:resource' => $notice->uri));
  148. }
  149. $this->elementEnd('rdf:Seq');
  150. $this->elementEnd('items');
  151. $this->elementEnd('channel');
  152. }
  153. function showImage()
  154. {
  155. $image = $this->getImage();
  156. if ($image) {
  157. $channel = $this->getChannel();
  158. $this->elementStart('image', array('rdf:about' => $image));
  159. $this->element('title', null, $channel['title']);
  160. $this->element('link', null, $channel['link']);
  161. $this->element('url', null, $image);
  162. $this->elementEnd('image');
  163. }
  164. }
  165. // XXX: Surely there should be a common function to do this?
  166. function extract_tags ($string)
  167. {
  168. $count = preg_match_all('/(?:^|\s)#([A-Za-z0-9_\-\.]{1,64})/', strtolower($string), $match);
  169. if (!count)
  170. {
  171. return array();
  172. }
  173. $rv = array();
  174. foreach ($match[1] as $tag)
  175. {
  176. $rv[] = common_canonical_tag($tag);
  177. }
  178. return array_unique($rv);
  179. }
  180. function showItem($notice)
  181. {
  182. $profile = Profile::staticGet($notice->profile_id);
  183. $nurl = common_local_url('shownotice', array('notice' => $notice->id));
  184. $creator_uri = common_profile_uri($profile);
  185. $this->elementStart('item', array('rdf:about' => $notice->uri,
  186. 'rdf:type' => 'http://rdfs.org/sioc/types#MicroblogPost'));
  187. $title = $profile->nickname . ': ' . common_xml_safe_str(trim($notice->content));
  188. $this->element('title', null, $title);
  189. $this->element('link', null, $nurl);
  190. $this->element('description', null, $profile->nickname."'s status on ".common_exact_date($notice->created));
  191. if ($notice->rendered) {
  192. $this->element('content:encoded', null, common_xml_safe_str($notice->rendered));
  193. }
  194. $this->element('dc:date', null, common_date_w3dtf($notice->created));
  195. $this->element('dc:creator', null, ($profile->fullname) ? $profile->fullname : $profile->nickname);
  196. $this->element('foaf:maker', array('rdf:resource' => $creator_uri));
  197. $this->element('sioc:has_creator', array('rdf:resource' => $creator_uri.'#acct'));
  198. $this->element('laconica:postIcon', array('rdf:resource' => $profile->avatarUrl()));
  199. $this->element('cc:licence', array('rdf:resource' => common_config('license', 'url')));
  200. if ($notice->reply_to) {
  201. $replyurl = common_local_url('shownotice', array('notice' => $notice->reply_to));
  202. $this->element('sioc:reply_of', array('rdf:resource' => $replyurl));
  203. }
  204. $attachments = $notice->attachments();
  205. if($attachments){
  206. foreach($attachments as $attachment){
  207. if ($attachment->isEnclosure()) {
  208. // DO NOT move xmlns declaration to root element. Making it
  209. // the default namespace here improves compatibility with
  210. // real-world feed readers.
  211. $attribs = array(
  212. 'rdf:resource' => $attachment->url,
  213. 'url' => $attachment->url,
  214. 'xmlns' => 'http://purl.oclc.org/net/rss_2.0/enc#'
  215. );
  216. if ($attachment->title) {
  217. $attribs['dc:title'] = $attachment->title;
  218. }
  219. if ($attachment->modified) {
  220. $attribs['dc:date'] = common_date_w3dtf($attachment->modified);
  221. }
  222. if ($attachment->size) {
  223. $attribs['length'] = $attachment->size;
  224. }
  225. if ($attachment->mimetype) {
  226. $attribs['type'] = $attachment->mimetype;
  227. }
  228. $this->element('enclosure', $attribs);
  229. }
  230. $this->element('sioc:links_to', array('rdf:resource'=>$attachment->url));
  231. }
  232. }
  233. $tags = $this->extract_tags($notice->content);
  234. if (!empty($tags)) {
  235. foreach ($tags as $tag)
  236. {
  237. $tagpage = common_local_url('tag', array('tag' => $tag));
  238. $tagrss = common_local_url('tagrss', array('tag' => $tag));
  239. $this->elementStart('ctag:tagged');
  240. $this->elementStart('ctag:Tag', array('rdf:about'=>$tagpage.'#concept', 'ctag:label'=>$tag));
  241. $this->element('foaf:page', array('rdf:resource'=>$tagpage));
  242. $this->element('rdfs:seeAlso', array('rdf:resource'=>$tagrss));
  243. $this->elementEnd('ctag:Tag');
  244. $this->elementEnd('ctag:tagged');
  245. }
  246. }
  247. $this->elementEnd('item');
  248. $this->creators[$creator_uri] = $profile;
  249. }
  250. function showCreators()
  251. {
  252. foreach ($this->creators as $uri => $profile) {
  253. $id = $profile->id;
  254. $nickname = $profile->nickname;
  255. $this->elementStart('foaf:Agent', array('rdf:about' => $uri));
  256. $this->element('foaf:nick', null, $nickname);
  257. if ($profile->fullname) {
  258. $this->element('foaf:name', null, $profile->fullname);
  259. }
  260. $this->element('foaf:holdsAccount', array('rdf:resource' => $uri.'#acct'));
  261. $avatar = $profile->avatarUrl();
  262. $this->element('foaf:depiction', array('rdf:resource' => $avatar));
  263. $this->elementEnd('foaf:Agent');
  264. }
  265. }
  266. function initRss()
  267. {
  268. $channel = $this->getChannel();
  269. header('Content-Type: application/rdf+xml');
  270. $this->startXml();
  271. $this->elementStart('rdf:RDF', array('xmlns:rdf' =>
  272. 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
  273. 'xmlns:dc' =>
  274. 'http://purl.org/dc/elements/1.1/',
  275. 'xmlns:cc' =>
  276. 'http://creativecommons.org/ns#',
  277. 'xmlns:content' =>
  278. 'http://purl.org/rss/1.0/modules/content/',
  279. 'xmlns:ctag' =>
  280. 'http://commontag.org/ns#',
  281. 'xmlns:foaf' =>
  282. 'http://xmlns.com/foaf/0.1/',
  283. 'xmlns:sioc' =>
  284. 'http://rdfs.org/sioc/ns#',
  285. 'xmlns:sioct' =>
  286. 'http://rdfs.org/sioc/types#',
  287. 'xmlns:rdfs' =>
  288. 'http://www.w3.org/2000/01/rdf-schema#',
  289. 'xmlns:laconica' =>
  290. 'http://laconi.ca/ont/',
  291. 'xmlns' => 'http://purl.org/rss/1.0/'));
  292. $this->elementStart('sioc:Site', array('rdf:about' => common_root_url()));
  293. $this->element('sioc:name', null, common_config('site', 'name'));
  294. $this->elementStart('sioc:space_of');
  295. $this->element('sioc:Container', array('rdf:about' =>
  296. $channel['url']));
  297. $this->elementEnd('sioc:space_of');
  298. $this->elementEnd('sioc:Site');
  299. }
  300. function endRss()
  301. {
  302. $this->elementEnd('rdf:RDF');
  303. }
  304. /**
  305. * When was this page last modified?
  306. *
  307. */
  308. function lastModified()
  309. {
  310. if (empty($this->notices)) {
  311. return null;
  312. }
  313. if (count($this->notices) == 0) {
  314. return null;
  315. }
  316. // FIXME: doesn't handle modified profiles, avatars, deleted notices
  317. return strtotime($this->notices[0]->created);
  318. }
  319. }