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-2009 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/
50 class TwitapisearchatomAction extends ApiAction
64 * Just wraps the Action constructor.
66 * @param string $output URI to output to, default = stdout
67 * @param boolean $indent Whether to indent output, default true
69 * @see Action::__construct
72 function __construct($output='php://output', $indent=null)
74 parent::__construct($output, $indent);
78 * Do we need to write to the database?
80 * @return boolean true
89 * Read arguments and initialize members
91 * @param array $args Arguments from $_REQUEST
93 * @return boolean success
97 function prepare($args)
99 parent::prepare($args);
101 $this->query = $this->trimmed('q');
102 $this->lang = $this->trimmed('lang');
103 $this->rpp = $this->trimmed('rpp');
109 if ($this->rpp > 100) {
113 $this->page = $this->trimmed('page');
119 // TODO: Suppport since_id -- we need to tweak the backend
120 // Search classes to support it.
122 $this->since_id = $this->trimmed('since_id');
123 $this->geocode = $this->trimmed('geocode');
125 // TODO: Also, language and geocode
133 * @param array $args Arguments from $_REQUEST
138 function handle($args)
140 parent::handle($args);
145 * Get the notices to output as results. This also sets some class
146 * attrs so we can use them to calculate pagination, and output
147 * since_id and max_id.
149 * @return array an array of Notice objects sorted in reverse chron
152 function getNotices()
154 // TODO: Support search operators like from: and to:, boolean, etc.
157 $notice = new Notice();
159 // lcase it for comparison
160 $q = strtolower($this->query);
162 $search_engine = $notice->getSearchEngine('notice');
163 $search_engine->set_sort_mode('chron');
164 $search_engine->limit(($this->page - 1) * $this->rpp,
165 $this->rpp + 1, true);
166 if (false === $search_engine->query($q)) {
169 $this->cnt = $notice->find();
175 if ($this->cnt > 0) {
176 while ($notice->fetch()) {
180 if (!$this->max_id) {
181 $this->max_id = $notice->id;
184 if ($cnt > $this->rpp) {
188 $notices[] = clone($notice);
196 * Output search results as an Atom feed
203 $notices = $this->getNotices();
208 foreach ($notices as $n) {
210 $profile = $n->getProfile();
212 // Don't show notices from deleted users
214 if (!empty($profile)) {
215 $this->showEntry($n);
223 * Show feed specific Atom elements
230 // TODO: A9 OpenSearch stuff like search.twitter.com?
232 $server = common_config('site', 'server');
233 $sitename = common_config('site', 'name');
235 // XXX: Use xmlns:statusnet instead?
237 $this->elementStart('feed',
238 array('xmlns' => 'http://www.w3.org/2005/Atom',
240 // XXX: xmlns:twitter causes Atom validation to fail
241 // It's used for the source attr on notices
243 'xmlns:twitter' => 'http://api.twitter.com/',
244 'xml:lang' => 'en-US')); // XXX Other locales ?
246 $taguribase = TagURI::base();
247 $this->element('id', null, "tag:$taguribase:search/$server");
249 $site_uri = common_path(false);
251 $search_uri = $site_uri . 'api/search.atom?q=' . urlencode($this->query);
253 if ($this->rpp != 15) {
254 $search_uri .= '&rpp=' . $this->rpp;
257 // FIXME: this alternate link is not quite right because our
258 // web-based notice search doesn't support a rpp (responses per
261 $this->element('link', array('type' => 'text/html',
262 'rel' => 'alternate',
263 'href' => $site_uri . 'search/notice?q=' .
264 urlencode($this->query)));
268 $self_uri = $search_uri;
269 $self_uri .= ($this->page > 1) ? '&page=' . $this->page : '';
271 $this->element('link', array('type' => 'application/atom+xml',
273 'href' => $self_uri));
275 $this->element('title', null, "$this->query - $sitename Search");
276 $this->element('updated', null, common_date_iso8601('now'));
278 // XXX: The below "rel" links are not valid Atom, but it's what
283 $refresh_uri = $search_uri . "&since_id=" . $this->max_id;
285 $this->element('link', array('type' => 'application/atom+xml',
287 'href' => $refresh_uri));
291 if ($this->cnt > $this->rpp) {
293 $next_uri = $search_uri . "&max_id=" . $this->max_id .
294 '&page=' . ($this->page + 1);
296 $this->element('link', array('type' => 'application/atom+xml',
298 'href' => $next_uri));
301 if ($this->page > 1) {
303 $previous_uri = $search_uri . "&max_id=" . $this->max_id .
304 '&page=' . ($this->page - 1);
306 $this->element('link', array('type' => 'application/atom+xml',
308 'href' => $previous_uri));
314 * Build an Atom entry similar to search.twitter.com's based on
317 * @param Notice $notice the notice to use
322 function showEntry($notice)
324 $server = common_config('site', 'server');
325 $profile = $notice->getProfile();
326 $nurl = common_local_url('shownotice', array('notice' => $notice->id));
328 $this->elementStart('entry');
330 $taguribase = TagURI::base();
332 $this->element('id', null, "tag:$taguribase:$notice->id");
333 $this->element('published', null, common_date_w3dtf($notice->created));
334 $this->element('link', array('type' => 'text/html',
335 'rel' => 'alternate',
337 $this->element('title', null, common_xml_safe_str(trim($notice->content)));
338 $this->element('content', array('type' => 'html'), $notice->rendered);
339 $this->element('updated', null, common_date_w3dtf($notice->created));
340 $this->element('link', array('type' => 'image/png',
341 // XXX: Twitter uses rel="image" (not valid)
343 'href' => $profile->avatarUrl()));
345 // TODO: Here is where we'd put in a link to an atom feed for threads
347 $this->element("twitter:source", null,
348 htmlentities($this->sourceLink($notice->source)));
350 $this->elementStart('author');
352 $name = $profile->nickname;
354 if ($profile->fullname) {
355 $name .= ' (' . $profile->fullname . ')';
358 $this->element('name', null, $name);
359 $this->element('uri', null, common_profile_uri($profile));
360 $this->elementEnd('author');
362 $this->elementEnd('entry');
366 * Initialize the Atom output, send headers
373 header('Content-Type: application/atom+xml; charset=utf-8');
385 $this->elementEnd('feed');