PageRenderTime 27ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/actions/twitapisearchatom.php

https://github.com/Br3nda/laconica
PHP | 383 lines | 172 code | 85 blank | 126 comment | 15 complexity | a4941cfb239714757f6462eb05a85ccc MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php
  2. /**
  3. * Laconica, the distributed open-source microblogging tool
  4. *
  5. * Action for showing Twitter-like Atom search results
  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 Search
  23. * @package Laconica
  24. * @author Zach Copley <zach@controlyourself.ca>
  25. * @copyright 2008-2009 Control Yourself, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://laconi.ca/
  28. */
  29. if (!defined('LACONICA')) {
  30. exit(1);
  31. }
  32. require_once INSTALLDIR.'/lib/twitterapi.php';
  33. /**
  34. * Action for outputting search results in Twitter compatible Atom
  35. * format.
  36. *
  37. * TODO: abstract Atom stuff into a ruseable base class like
  38. * RSS10Action.
  39. *
  40. * @category Search
  41. * @package Laconica
  42. * @author Zach Copley <zach@controlyourself.ca>
  43. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  44. * @link http://laconi.ca/
  45. *
  46. * @see TwitterapiAction
  47. */
  48. class TwitapisearchatomAction extends TwitterapiAction
  49. {
  50. var $cnt;
  51. var $query;
  52. var $lang;
  53. var $rpp;
  54. var $page;
  55. var $since_id;
  56. var $geocode;
  57. /**
  58. * Constructor
  59. *
  60. * Just wraps the Action constructor.
  61. *
  62. * @param string $output URI to output to, default = stdout
  63. * @param boolean $indent Whether to indent output, default true
  64. *
  65. * @see Action::__construct
  66. */
  67. function __construct($output='php://output', $indent=true)
  68. {
  69. parent::__construct($output, $indent);
  70. }
  71. /**
  72. * Do we need to write to the database?
  73. *
  74. * @return boolean true
  75. */
  76. function isReadonly()
  77. {
  78. return true;
  79. }
  80. /**
  81. * Read arguments and initialize members
  82. *
  83. * @param array $args Arguments from $_REQUEST
  84. *
  85. * @return boolean success
  86. *
  87. */
  88. function prepare($args)
  89. {
  90. parent::prepare($args);
  91. $this->query = $this->trimmed('q');
  92. $this->lang = $this->trimmed('lang');
  93. $this->rpp = $this->trimmed('rpp');
  94. if (!$this->rpp) {
  95. $this->rpp = 15;
  96. }
  97. if ($this->rpp > 100) {
  98. $this->rpp = 100;
  99. }
  100. $this->page = $this->trimmed('page');
  101. if (!$this->page) {
  102. $this->page = 1;
  103. }
  104. // TODO: Suppport since_id -- we need to tweak the backend
  105. // Search classes to support it.
  106. $this->since_id = $this->trimmed('since_id');
  107. $this->geocode = $this->trimmed('geocode');
  108. // TODO: Also, language and geocode
  109. return true;
  110. }
  111. /**
  112. * Handle a request
  113. *
  114. * @param array $args Arguments from $_REQUEST
  115. *
  116. * @return void
  117. */
  118. function handle($args)
  119. {
  120. parent::handle($args);
  121. $this->showAtom();
  122. }
  123. /**
  124. * Get the notices to output as results. This also sets some class
  125. * attrs so we can use them to calculate pagination, and output
  126. * since_id and max_id.
  127. *
  128. * @return array an array of Notice objects sorted in reverse chron
  129. */
  130. function getNotices()
  131. {
  132. // TODO: Support search operators like from: and to:, boolean, etc.
  133. $notices = array();
  134. $notice = new Notice();
  135. // lcase it for comparison
  136. $q = strtolower($this->query);
  137. $search_engine = $notice->getSearchEngine('identica_notices');
  138. $search_engine->set_sort_mode('chron');
  139. $search_engine->limit(($this->page - 1) * $this->rpp,
  140. $this->rpp + 1, true);
  141. if (false === $search_engine->query($q)) {
  142. $this->cnt = 0;
  143. } else {
  144. $this->cnt = $notice->find();
  145. }
  146. $cnt = 0;
  147. $this->max_id = 0;
  148. if ($this->cnt > 0) {
  149. while ($notice->fetch()) {
  150. ++$cnt;
  151. if (!$this->max_id) {
  152. $this->max_id = $notice->id;
  153. }
  154. if ($cnt > $this->rpp) {
  155. break;
  156. }
  157. $notices[] = clone($notice);
  158. }
  159. }
  160. return $notices;
  161. }
  162. /**
  163. * Output search results as an Atom feed
  164. *
  165. * @return void
  166. */
  167. function showAtom()
  168. {
  169. $notices = $this->getNotices();
  170. $this->initAtom();
  171. $this->showFeed();
  172. foreach ($notices as $n) {
  173. $this->showEntry($n);
  174. }
  175. $this->endAtom();
  176. }
  177. /**
  178. * Show feed specific Atom elements
  179. *
  180. * @return void
  181. */
  182. function showFeed()
  183. {
  184. // TODO: A9 OpenSearch stuff like search.twitter.com?
  185. $server = common_config('site', 'server');
  186. $sitename = common_config('site', 'name');
  187. // XXX: Use xmlns:laconica instead?
  188. $this->elementStart('feed',
  189. array('xmlns' => 'http://www.w3.org/2005/Atom',
  190. // XXX: xmlns:twitter causes Atom validation to fail
  191. // It's used for the source attr on notices
  192. 'xmlns:twitter' => 'http://api.twitter.com/',
  193. 'xml:lang' => 'en-US')); // XXX Other locales ?
  194. $taguribase = common_config('integration', 'taguri');
  195. $this->element('id', null, "tag:$taguribase:search/$server");
  196. $site_uri = common_path(false);
  197. $search_uri = $site_uri . 'api/search.atom?q=' . urlencode($this->query);
  198. if ($this->rpp != 15) {
  199. $search_uri .= '&rpp=' . $this->rpp;
  200. }
  201. // FIXME: this alternate link is not quite right because our
  202. // web-based notice search doesn't support a rpp (responses per
  203. // page) param yet
  204. $this->element('link', array('type' => 'text/html',
  205. 'rel' => 'alternate',
  206. 'href' => $site_uri . 'search/notice?q=' .
  207. urlencode($this->query)));
  208. // self link
  209. $self_uri = $search_uri;
  210. $self_uri .= ($this->page > 1) ? '&page=' . $this->page : '';
  211. $this->element('link', array('type' => 'application/atom+xml',
  212. 'rel' => 'self',
  213. 'href' => $self_uri));
  214. $this->element('title', null, "$this->query - $sitename Search");
  215. $this->element('updated', null, common_date_iso8601('now'));
  216. // XXX: The below "rel" links are not valid Atom, but it's what
  217. // Twitter does...
  218. // refresh link
  219. $refresh_uri = $search_uri . "&since_id=" . $this->max_id;
  220. $this->element('link', array('type' => 'application/atom+xml',
  221. 'rel' => 'refresh',
  222. 'href' => $refresh_uri));
  223. // pagination links
  224. if ($this->cnt > $this->rpp) {
  225. $next_uri = $search_uri . "&max_id=" . $this->max_id .
  226. '&page=' . ($this->page + 1);
  227. $this->element('link', array('type' => 'application/atom+xml',
  228. 'rel' => 'next',
  229. 'href' => $next_uri));
  230. }
  231. if ($this->page > 1) {
  232. $previous_uri = $search_uri . "&max_id=" . $this->max_id .
  233. '&page=' . ($this->page - 1);
  234. $this->element('link', array('type' => 'application/atom+xml',
  235. 'rel' => 'previous',
  236. 'href' => $previous_uri));
  237. }
  238. }
  239. /**
  240. * Build an Atom entry similar to search.twitter.com's based on
  241. * a given notice
  242. *
  243. * @param Notice $notice the notice to use
  244. *
  245. * @return void
  246. */
  247. function showEntry($notice)
  248. {
  249. $server = common_config('site', 'server');
  250. $profile = $notice->getProfile();
  251. $nurl = common_local_url('shownotice', array('notice' => $notice->id));
  252. $this->elementStart('entry');
  253. $taguribase = common_config('integration', 'taguri');
  254. $this->element('id', null, "tag:$taguribase:$notice->id");
  255. $this->element('published', null, common_date_w3dtf($notice->created));
  256. $this->element('link', array('type' => 'text/html',
  257. 'rel' => 'alternate',
  258. 'href' => $nurl));
  259. $this->element('title', null, common_xml_safe_str(trim($notice->content)));
  260. $this->element('content', array('type' => 'html'), $notice->rendered);
  261. $this->element('updated', null, common_date_w3dtf($notice->created));
  262. $this->element('link', array('type' => 'image/png',
  263. // XXX: Twitter uses rel="image" (not valid)
  264. 'rel' => 'related',
  265. 'href' => $profile->avatarUrl()));
  266. // TODO: Here is where we'd put in a link to an atom feed for threads
  267. $this->element("twitter:source", null,
  268. htmlentities($this->source_link($notice->source)));
  269. $this->elementStart('author');
  270. $name = $profile->nickname;
  271. if ($profile->fullname) {
  272. $name .= ' (' . $profile->fullname . ')';
  273. }
  274. $this->element('name', null, $name);
  275. $this->element('uri', null, common_profile_uri($profile));
  276. $this->elementEnd('author');
  277. $this->elementEnd('entry');
  278. }
  279. /**
  280. * Initialize the Atom output, send headers
  281. *
  282. * @return void
  283. */
  284. function initAtom()
  285. {
  286. header('Content-Type: application/atom+xml; charset=utf-8');
  287. $this->startXml();
  288. }
  289. /**
  290. * End the Atom feed
  291. *
  292. * @return void
  293. */
  294. function endAtom()
  295. {
  296. $this->elementEnd('feed');
  297. }
  298. }