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 since_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 ($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();
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');