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')) {
34 require_once INSTALLDIR.'/lib/twitterapi.php';
37 * Action for outputting search results in Twitter compatible Atom
40 * TODO: abstract Atom stuff into a ruseable base class like
45 * @author Zach Copley <zach@status.net>
46 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47 * @link http://status.net/
49 * @see TwitterapiAction
52 class TwitapisearchatomAction extends TwitterapiAction
66 * Just wraps the Action constructor.
68 * @param string $output URI to output to, default = stdout
69 * @param boolean $indent Whether to indent output, default true
71 * @see Action::__construct
74 function __construct($output='php://output', $indent=true)
76 parent::__construct($output, $indent);
80 * Do we need to write to the database?
82 * @return boolean true
91 * Read arguments and initialize members
93 * @param array $args Arguments from $_REQUEST
95 * @return boolean success
99 function prepare($args)
101 parent::prepare($args);
103 $this->query = $this->trimmed('q');
104 $this->lang = $this->trimmed('lang');
105 $this->rpp = $this->trimmed('rpp');
111 if ($this->rpp > 100) {
115 $this->page = $this->trimmed('page');
121 // TODO: Suppport since_id -- we need to tweak the backend
122 // Search classes to support it.
124 $this->since_id = $this->trimmed('since_id');
125 $this->geocode = $this->trimmed('geocode');
127 // TODO: Also, language and geocode
135 * @param array $args Arguments from $_REQUEST
140 function handle($args)
142 parent::handle($args);
147 * Get the notices to output as results. This also sets some class
148 * attrs so we can use them to calculate pagination, and output
149 * since_id and max_id.
151 * @return array an array of Notice objects sorted in reverse chron
154 function getNotices()
156 // TODO: Support search operators like from: and to:, boolean, etc.
159 $notice = new Notice();
161 // lcase it for comparison
162 $q = strtolower($this->query);
164 $search_engine = $notice->getSearchEngine('identica_notices');
165 $search_engine->set_sort_mode('chron');
166 $search_engine->limit(($this->page - 1) * $this->rpp,
167 $this->rpp + 1, true);
168 if (false === $search_engine->query($q)) {
171 $this->cnt = $notice->find();
177 if ($this->cnt > 0) {
178 while ($notice->fetch()) {
182 if (!$this->max_id) {
183 $this->max_id = $notice->id;
186 if ($cnt > $this->rpp) {
190 $notices[] = clone($notice);
198 * Output search results as an Atom feed
205 $notices = $this->getNotices();
210 foreach ($notices as $n) {
211 $this->showEntry($n);
218 * Show feed specific Atom elements
225 // TODO: A9 OpenSearch stuff like search.twitter.com?
227 $server = common_config('site', 'server');
228 $sitename = common_config('site', 'name');
230 // XXX: Use xmlns:statusnet instead?
232 $this->elementStart('feed',
233 array('xmlns' => 'http://www.w3.org/2005/Atom',
235 // XXX: xmlns:twitter causes Atom validation to fail
236 // It's used for the source attr on notices
238 'xmlns:twitter' => 'http://api.twitter.com/',
239 'xml:lang' => 'en-US')); // XXX Other locales ?
241 $taguribase = common_config('integration', 'taguri');
242 $this->element('id', null, "tag:$taguribase:search/$server");
244 $site_uri = common_path(false);
246 $search_uri = $site_uri . 'api/search.atom?q=' . urlencode($this->query);
248 if ($this->rpp != 15) {
249 $search_uri .= '&rpp=' . $this->rpp;
252 // FIXME: this alternate link is not quite right because our
253 // web-based notice search doesn't support a rpp (responses per
256 $this->element('link', array('type' => 'text/html',
257 'rel' => 'alternate',
258 'href' => $site_uri . 'search/notice?q=' .
259 urlencode($this->query)));
263 $self_uri = $search_uri;
264 $self_uri .= ($this->page > 1) ? '&page=' . $this->page : '';
266 $this->element('link', array('type' => 'application/atom+xml',
268 'href' => $self_uri));
270 $this->element('title', null, "$this->query - $sitename Search");
271 $this->element('updated', null, common_date_iso8601('now'));
273 // XXX: The below "rel" links are not valid Atom, but it's what
278 $refresh_uri = $search_uri . "&since_id=" . $this->max_id;
280 $this->element('link', array('type' => 'application/atom+xml',
282 'href' => $refresh_uri));
286 if ($this->cnt > $this->rpp) {
288 $next_uri = $search_uri . "&max_id=" . $this->max_id .
289 '&page=' . ($this->page + 1);
291 $this->element('link', array('type' => 'application/atom+xml',
293 'href' => $next_uri));
296 if ($this->page > 1) {
298 $previous_uri = $search_uri . "&max_id=" . $this->max_id .
299 '&page=' . ($this->page - 1);
301 $this->element('link', array('type' => 'application/atom+xml',
303 'href' => $previous_uri));
309 * Build an Atom entry similar to search.twitter.com's based on
312 * @param Notice $notice the notice to use
317 function showEntry($notice)
319 $server = common_config('site', 'server');
320 $profile = $notice->getProfile();
321 $nurl = common_local_url('shownotice', array('notice' => $notice->id));
323 $this->elementStart('entry');
325 $taguribase = common_config('integration', 'taguri');
327 $this->element('id', null, "tag:$taguribase:$notice->id");
328 $this->element('published', null, common_date_w3dtf($notice->created));
329 $this->element('link', array('type' => 'text/html',
330 'rel' => 'alternate',
332 $this->element('title', null, common_xml_safe_str(trim($notice->content)));
333 $this->element('content', array('type' => 'html'), $notice->rendered);
334 $this->element('updated', null, common_date_w3dtf($notice->created));
335 $this->element('link', array('type' => 'image/png',
336 // XXX: Twitter uses rel="image" (not valid)
338 'href' => $profile->avatarUrl()));
340 // TODO: Here is where we'd put in a link to an atom feed for threads
342 $this->element("twitter:source", null,
343 htmlentities($this->source_link($notice->source)));
345 $this->elementStart('author');
347 $name = $profile->nickname;
349 if ($profile->fullname) {
350 $name .= ' (' . $profile->fullname . ')';
353 $this->element('name', null, $name);
354 $this->element('uri', null, common_profile_uri($profile));
355 $this->elementEnd('author');
357 $this->elementEnd('entry');
361 * Initialize the Atom output, send headers
368 header('Content-Type: application/atom+xml; charset=utf-8');
380 $this->elementEnd('feed');