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')) {
34 require_once INSTALLDIR.'/lib/apiprivateauth.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 ApiPrivateAuthAction
51 class ApiSearchAtomAction extends ApiPrivateAuthAction
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
71 function __construct($output='php://output', $indent=null)
73 parent::__construct($output, $indent);
77 * Do we need to write to the database?
79 * @return boolean true
87 * Read arguments and initialize members
89 * @param array $args Arguments from $_REQUEST
91 * @return boolean success
93 function prepare($args)
95 common_debug("in apisearchatom prepare()");
97 parent::prepare($args);
99 $this->query = $this->trimmed('q');
100 $this->lang = $this->trimmed('lang');
101 $this->rpp = $this->trimmed('rpp');
107 if ($this->rpp > 100) {
111 $this->page = $this->trimmed('page');
117 // TODO: Suppport max_id -- we need to tweak the backend
118 // Search classes to support it.
120 $this->since_id = $this->trimmed('since_id');
121 $this->geocode = $this->trimmed('geocode');
123 // TODO: Also, language and geocode
131 * @param array $args Arguments from $_REQUEST
135 function handle($args)
137 parent::handle($args);
138 common_debug("In apisearchatom handle()");
143 * Get the notices to output as results. This also sets some class
144 * attrs so we can use them to calculate pagination, and output
145 * since_id and max_id.
147 * @return array an array of Notice objects sorted in reverse chron
149 function getNotices()
151 // TODO: Support search operators like from: and to:, boolean, etc.
154 $notice = new Notice();
156 // lcase it for comparison
157 $q = strtolower($this->query);
159 $search_engine = $notice->getSearchEngine('notice');
160 $search_engine->set_sort_mode('chron');
161 $search_engine->limit(($this->page - 1) * $this->rpp,
162 $this->rpp + 1, true);
163 if (false === $search_engine->query($q)) {
166 $this->cnt = $notice->find();
172 if ($this->cnt > 0) {
173 while ($notice->fetch()) {
176 if (!$this->max_id) {
177 $this->max_id = $notice->id;
180 if ($this->since_id && $notice->id <= $this->since_id) {
184 if ($cnt > $this->rpp) {
188 $notices[] = clone($notice);
196 * Output search results as an Atom feed
202 $notices = $this->getNotices();
207 foreach ($notices as $n) {
208 $profile = $n->getProfile();
210 // Don't show notices from deleted users
212 if (!empty($profile)) {
213 $this->showEntry($n);
221 * Show feed specific Atom elements
227 // TODO: A9 OpenSearch stuff like search.twitter.com?
229 $server = common_config('site', 'server');
230 $sitename = common_config('site', 'name');
232 // XXX: Use xmlns:statusnet instead?
234 $this->elementStart('feed',
235 array('xmlns' => 'http://www.w3.org/2005/Atom',
237 // XXX: xmlns:twitter causes Atom validation to fail
238 // It's used for the source attr on notices
240 'xmlns:twitter' => 'http://api.twitter.com/',
241 'xml:lang' => 'en-US')); // XXX Other locales ?
243 $taguribase = TagURI::base();
244 $this->element('id', null, "tag:$taguribase:search/$server");
246 $site_uri = common_path(false);
248 $search_uri = $site_uri . 'api/search.atom?q=' . urlencode($this->query);
250 if ($this->rpp != 15) {
251 $search_uri .= '&rpp=' . $this->rpp;
254 // FIXME: this alternate link is not quite right because our
255 // web-based notice search doesn't support a rpp (responses per
258 $this->element('link', array('type' => 'text/html',
259 'rel' => 'alternate',
260 'href' => $site_uri . 'search/notice?q=' .
261 urlencode($this->query)));
265 $self_uri = $search_uri;
266 $self_uri .= ($this->page > 1) ? '&page=' . $this->page : '';
268 $this->element('link', array('type' => 'application/atom+xml',
270 'href' => $self_uri));
273 $this->element('title', null, "$this->query - $sitename Search");
274 $this->element('updated', null, common_date_iso8601('now'));
276 // XXX: The below "rel" links are not valid Atom, but it's what
281 $refresh_uri = $search_uri . "&since_id=" . $this->max_id;
283 $this->element('link', array('type' => 'application/atom+xml',
285 'href' => $refresh_uri));
289 if ($this->cnt > $this->rpp) {
291 $next_uri = $search_uri . "&max_id=" . $this->max_id .
292 '&page=' . ($this->page + 1);
294 $this->element('link', array('type' => 'application/atom+xml',
296 'href' => $next_uri));
299 if ($this->page > 1) {
301 $previous_uri = $search_uri . "&max_id=" . $this->max_id .
302 '&page=' . ($this->page - 1);
304 $this->element('link', array('type' => 'application/atom+xml',
306 'href' => $previous_uri));
311 * Build an Atom entry similar to search.twitter.com's based on
314 * @param Notice $notice the notice to use
318 function showEntry($notice)
320 $server = common_config('site', 'server');
321 $profile = $notice->getProfile();
322 $nurl = common_local_url('shownotice', array('notice' => $notice->id));
324 $this->elementStart('entry');
326 $taguribase = TagURI::base();
328 $this->element('id', null, "tag:$taguribase:$notice->id");
329 $this->element('published', null, common_date_w3dtf($notice->created));
330 $this->element('link', array('type' => 'text/html',
331 'rel' => 'alternate',
333 $this->element('title', null, common_xml_safe_str(trim($notice->content)));
334 $this->element('content', array('type' => 'html'), $notice->rendered);
335 $this->element('updated', null, common_date_w3dtf($notice->created));
336 $this->element('link', array('type' => 'image/png',
337 // XXX: Twitter uses rel="image" (not valid)
339 'href' => $profile->avatarUrl()));
341 // @todo: Here is where we'd put in a link to an atom feed for threads
345 $ns = $notice->getSource();
347 if (!empty($ns->name) && !empty($ns->url)) {
348 $source = '<a href="'
349 . htmlspecialchars($ns->url)
350 . '" rel="nofollow">'
351 . htmlspecialchars($ns->name)
358 $this->element("twitter:source", null, $source);
360 $this->elementStart('author');
362 $name = $profile->nickname;
364 if ($profile->fullname) {
365 // @todo Needs proper i18n?
366 $name .= ' (' . $profile->fullname . ')';
369 $this->element('name', null, $name);
370 $this->element('uri', null, common_profile_uri($profile));
371 $this->elementEnd('author');
373 $this->elementEnd('entry');
377 * Initialize the Atom output, send headers
383 header('Content-Type: application/atom+xml; charset=utf-8');
394 $this->elementEnd('feed');