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(array $args=array())
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(array $args=array())
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()) {
170 // Check scope of notice to current profile (including guests)
171 if (!$notice->isCurrentProfileInScope()) {
178 if (!$this->max_id) {
179 $this->max_id = $notice->id;
182 if ($this->since_id && $notice->id <= $this->since_id) {
186 if ($cnt > $this->rpp) {
190 $notices[] = clone($notice);
198 * Output search results as an Atom feed
204 $notices = $this->getNotices();
209 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
229 // TODO: A9 OpenSearch stuff like search.twitter.com?
231 $server = common_config('site', 'server');
232 $sitename = common_config('site', 'name');
234 // XXX: Use xmlns:statusnet instead?
236 $this->elementStart('feed',
237 array('xmlns' => 'http://www.w3.org/2005/Atom',
239 // XXX: xmlns:twitter causes Atom validation to fail
240 // It's used for the source attr on notices
242 'xmlns:twitter' => 'http://api.twitter.com/',
243 'xml:lang' => 'en-US')); // XXX Other locales ?
245 $taguribase = TagURI::base();
246 $this->element('id', null, "tag:$taguribase:search/$server");
248 $site_uri = common_path(false);
250 $search_uri = $site_uri . 'api/search.atom?q=' . urlencode($this->query);
252 if ($this->rpp != 15) {
253 $search_uri .= '&rpp=' . $this->rpp;
256 // FIXME: this alternate link is not quite right because our
257 // web-based notice search doesn't support a rpp (responses per
260 $this->element('link', array('type' => 'text/html',
261 'rel' => 'alternate',
262 'href' => $site_uri . 'search/notice?q=' .
263 urlencode($this->query)));
267 $self_uri = $search_uri;
268 $self_uri .= ($this->page > 1) ? '&page=' . $this->page : '';
270 $this->element('link', array('type' => 'application/atom+xml',
272 '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));
313 * Build an Atom entry similar to search.twitter.com's based on
316 * @param Notice $notice the notice to use
320 function showEntry($notice)
322 $server = common_config('site', 'server');
323 $profile = $notice->getProfile();
324 $nurl = common_local_url('shownotice', array('notice' => $notice->id));
326 $this->elementStart('entry');
328 $taguribase = TagURI::base();
330 $this->element('id', null, "tag:$taguribase:$notice->id");
331 $this->element('published', null, common_date_w3dtf($notice->created));
332 $this->element('link', array('type' => 'text/html',
333 'rel' => 'alternate',
335 $this->element('title', null, common_xml_safe_str(trim($notice->content)));
336 $this->element('content', array('type' => 'html'), $notice->rendered);
337 $this->element('updated', null, common_date_w3dtf($notice->created));
338 $this->element('link', array('type' => 'image/png',
339 // XXX: Twitter uses rel="image" (not valid)
341 'href' => $profile->avatarUrl()));
343 // @todo: Here is where we'd put in a link to an atom feed for threads
347 $ns = $notice->getSource();
348 if ($ns instanceof Notice_source) {
349 if (!empty($ns->name) && !empty($ns->url)) {
350 $source = '<a href="'
351 . htmlspecialchars($ns->url)
352 . '" rel="nofollow">'
353 . htmlspecialchars($ns->name)
360 $this->element("twitter:source", null, $source);
362 $this->elementStart('author');
364 $name = $profile->nickname;
366 if ($profile->fullname) {
367 // @todo Needs proper i18n?
368 $name .= ' (' . $profile->fullname . ')';
371 $this->element('name', null, $name);
372 $this->element('uri', null, common_profile_uri($profile));
373 $this->elementEnd('author');
375 $this->elementEnd('entry');
379 * Initialize the Atom output, send headers
385 header('Content-Type: application/atom+xml; charset=utf-8');
396 $this->elementEnd('feed');