3 * StatusNet, the distributed open-source microblogging tool
5 * Action for showing Twitter-like Atom search results
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.
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.
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/>.
24 * @author Zach Copley <zach@status.net>
25 * @copyright 2008-2010 StatusNet, Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 * Action for outputting search results in Twitter compatible Atom
38 * TODO: abstract Atom stuff into a ruseable base class like
43 * @author Zach Copley <zach@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
47 * @see ApiPrivateAuthAction
49 class ApiSearchAtomAction extends ApiPrivateAuthAction
62 * Just wraps the Action constructor.
64 * @param string $output URI to output to, default = stdout
65 * @param boolean $indent Whether to indent output, default true
67 * @see Action::__construct
69 function __construct($output='php://output', $indent=null)
71 parent::__construct($output, $indent);
75 * Do we need to write to the database?
77 * @return boolean true
85 * Read arguments and initialize members
87 * @param array $args Arguments from $_REQUEST
89 * @return boolean success
91 function prepare($args)
93 parent::prepare($args);
95 $this->query = $this->trimmed('q');
96 $this->lang = $this->trimmed('lang');
97 $this->rpp = $this->trimmed('rpp');
103 if ($this->rpp > 100) {
107 $this->page = $this->trimmed('page');
113 // TODO: Suppport max_id -- we need to tweak the backend
114 // Search classes to support it.
116 $this->since_id = $this->trimmed('since_id');
117 $this->geocode = $this->trimmed('geocode');
119 // TODO: Also, language and geocode
127 * @param array $args Arguments from $_REQUEST
131 function handle($args)
133 parent::handle($args);
134 common_debug("In apisearchatom handle()");
139 * Get the notices to output as results. This also sets some class
140 * attrs so we can use them to calculate pagination, and output
141 * since_id and max_id.
143 * @return array an array of Notice objects sorted in reverse chron
145 function getNotices()
147 // TODO: Support search operators like from: and to:, boolean, etc.
150 $notice = new Notice();
152 // lcase it for comparison
153 $q = strtolower($this->query);
155 $search_engine = $notice->getSearchEngine('notice');
156 $search_engine->set_sort_mode('chron');
157 $search_engine->limit(($this->page - 1) * $this->rpp,
158 $this->rpp + 1, true);
159 if (false === $search_engine->query($q)) {
162 $this->cnt = $notice->find();
168 if ($this->cnt > 0) {
169 while ($notice->fetch()) {
172 if (!$this->max_id) {
173 $this->max_id = $notice->id;
176 if ($this->since_id && $notice->id <= $this->since_id) {
180 if ($cnt > $this->rpp) {
184 $notices[] = clone($notice);
192 * Output search results as an Atom feed
198 $notices = $this->getNotices();
203 foreach ($notices as $n) {
204 $profile = $n->getProfile();
206 // Don't show notices from deleted users
208 if (!empty($profile)) {
209 $this->showEntry($n);
217 * Show feed specific Atom elements
223 // TODO: A9 OpenSearch stuff like search.twitter.com?
225 $server = common_config('site', 'server');
226 $sitename = common_config('site', 'name');
228 // XXX: Use xmlns:statusnet instead?
230 $this->elementStart('feed',
231 array('xmlns' => 'http://www.w3.org/2005/Atom',
233 // XXX: xmlns:twitter causes Atom validation to fail
234 // It's used for the source attr on notices
236 'xmlns:twitter' => 'http://api.twitter.com/',
237 'xml:lang' => 'en-US')); // XXX Other locales ?
239 $taguribase = TagURI::base();
240 $this->element('id', null, "tag:$taguribase:search/$server");
242 $site_uri = common_path(false);
244 $search_uri = $site_uri . 'api/search.atom?q=' . urlencode($this->query);
246 if ($this->rpp != 15) {
247 $search_uri .= '&rpp=' . $this->rpp;
250 // FIXME: this alternate link is not quite right because our
251 // web-based notice search doesn't support a rpp (responses per
254 $this->element('link', array('type' => 'text/html',
255 'rel' => 'alternate',
256 'href' => $site_uri . 'search/notice?q=' .
257 urlencode($this->query)));
261 $self_uri = $search_uri;
262 $self_uri .= ($this->page > 1) ? '&page=' . $this->page : '';
264 $this->element('link', array('type' => 'application/atom+xml',
266 'href' => $self_uri));
269 $this->element('title', null, "$this->query - $sitename Search");
270 $this->element('updated', null, common_date_iso8601('now'));
272 // XXX: The below "rel" links are not valid Atom, but it's what
277 $refresh_uri = $search_uri . "&since_id=" . $this->max_id;
279 $this->element('link', array('type' => 'application/atom+xml',
281 'href' => $refresh_uri));
285 if ($this->cnt > $this->rpp) {
287 $next_uri = $search_uri . "&max_id=" . $this->max_id .
288 '&page=' . ($this->page + 1);
290 $this->element('link', array('type' => 'application/atom+xml',
292 'href' => $next_uri));
295 if ($this->page > 1) {
297 $previous_uri = $search_uri . "&max_id=" . $this->max_id .
298 '&page=' . ($this->page - 1);
300 $this->element('link', array('type' => 'application/atom+xml',
302 'href' => $previous_uri));
307 * Build an Atom entry similar to search.twitter.com's based on
310 * @param Notice $notice the notice to use
314 function showEntry($notice)
316 $server = common_config('site', 'server');
317 $profile = $notice->getProfile();
318 $nurl = common_local_url('shownotice', array('notice' => $notice->id));
320 $this->elementStart('entry');
322 $taguribase = TagURI::base();
324 $this->element('id', null, "tag:$taguribase:$notice->id");
325 $this->element('published', null, common_date_w3dtf($notice->created));
326 $this->element('link', array('type' => 'text/html',
327 'rel' => 'alternate',
329 $this->element('title', null, common_xml_safe_str(trim($notice->content)));
330 $this->element('content', array('type' => 'html'), $notice->rendered);
331 $this->element('updated', null, common_date_w3dtf($notice->created));
332 $this->element('link', array('type' => 'image/png',
333 // XXX: Twitter uses rel="image" (not valid)
335 'href' => $profile->avatarUrl()));
337 // @todo: Here is where we'd put in a link to an atom feed for threads
341 $ns = $notice->getSource();
342 if ($ns instanceof Notice_source) {
343 if (!empty($ns->name) && !empty($ns->url)) {
344 $source = '<a href="'
345 . htmlspecialchars($ns->url)
346 . '" rel="nofollow">'
347 . htmlspecialchars($ns->name)
354 $this->element("twitter:source", null, $source);
356 $this->elementStart('author');
358 $name = $profile->nickname;
360 if ($profile->fullname) {
361 // @todo Needs proper i18n?
362 $name .= ' (' . $profile->fullname . ')';
365 $this->element('name', null, $name);
366 $this->element('uri', null, common_profile_uri($profile));
367 $this->elementEnd('author');
369 $this->elementEnd('entry');
373 * Initialize the Atom output, send headers
379 header('Content-Type: application/atom+xml; charset=utf-8');
390 $this->elementEnd('feed');