2009-03-07 14:09:43 +09:00
|
|
|
<?php
|
|
|
|
/**
|
2009-08-26 07:12:20 +09:00
|
|
|
* StatusNet, the distributed open-source microblogging tool
|
2009-03-07 14:09:43 +09:00
|
|
|
*
|
|
|
|
* Action for showing Twitter-like Atom search results
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* LICENCE: This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @category Search
|
2009-08-26 07:12:20 +09:00
|
|
|
* @package StatusNet
|
2009-08-26 07:19:04 +09:00
|
|
|
* @author Zach Copley <zach@status.net>
|
2010-06-25 10:11:50 +09:00
|
|
|
* @copyright 2008-2010 StatusNet, Inc.
|
2009-03-07 14:09:43 +09:00
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
2009-08-26 07:16:46 +09:00
|
|
|
* @link http://status.net/
|
2009-03-07 14:09:43 +09:00
|
|
|
*/
|
|
|
|
|
2009-08-26 23:41:36 +09:00
|
|
|
if (!defined('STATUSNET') && !defined('LACONICA')) {
|
2009-03-07 14:09:43 +09:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Action for outputting search results in Twitter compatible Atom
|
|
|
|
* format.
|
|
|
|
*
|
|
|
|
* TODO: abstract Atom stuff into a ruseable base class like
|
|
|
|
* RSS10Action.
|
|
|
|
*
|
|
|
|
* @category Search
|
2009-08-26 07:12:20 +09:00
|
|
|
* @package StatusNet
|
2009-08-26 07:19:04 +09:00
|
|
|
* @author Zach Copley <zach@status.net>
|
2009-03-07 14:09:43 +09:00
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
|
2009-08-26 07:16:46 +09:00
|
|
|
* @link http://status.net/
|
2009-03-07 14:09:43 +09:00
|
|
|
*
|
2010-06-25 10:11:50 +09:00
|
|
|
* @see ApiPrivateAuthAction
|
2009-03-07 14:09:43 +09:00
|
|
|
*/
|
2010-06-25 10:11:50 +09:00
|
|
|
class ApiSearchAtomAction extends ApiPrivateAuthAction
|
2009-03-07 14:09:43 +09:00
|
|
|
{
|
|
|
|
var $cnt;
|
|
|
|
var $query;
|
|
|
|
var $lang;
|
|
|
|
var $rpp;
|
|
|
|
var $page;
|
|
|
|
var $since_id;
|
|
|
|
var $geocode;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* Just wraps the Action constructor.
|
|
|
|
*
|
|
|
|
* @param string $output URI to output to, default = stdout
|
|
|
|
* @param boolean $indent Whether to indent output, default true
|
|
|
|
*
|
|
|
|
* @see Action::__construct
|
|
|
|
*/
|
2009-12-05 15:03:04 +09:00
|
|
|
function __construct($output='php://output', $indent=null)
|
2009-03-07 14:09:43 +09:00
|
|
|
{
|
|
|
|
parent::__construct($output, $indent);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do we need to write to the database?
|
|
|
|
*
|
|
|
|
* @return boolean true
|
|
|
|
*/
|
|
|
|
function isReadonly()
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read arguments and initialize members
|
|
|
|
*
|
|
|
|
* @param array $args Arguments from $_REQUEST
|
|
|
|
*
|
|
|
|
* @return boolean success
|
|
|
|
*/
|
|
|
|
function prepare($args)
|
|
|
|
{
|
|
|
|
parent::prepare($args);
|
|
|
|
|
|
|
|
$this->query = $this->trimmed('q');
|
|
|
|
$this->lang = $this->trimmed('lang');
|
|
|
|
$this->rpp = $this->trimmed('rpp');
|
|
|
|
|
|
|
|
if (!$this->rpp) {
|
|
|
|
$this->rpp = 15;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->rpp > 100) {
|
|
|
|
$this->rpp = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->page = $this->trimmed('page');
|
|
|
|
|
|
|
|
if (!$this->page) {
|
|
|
|
$this->page = 1;
|
|
|
|
}
|
|
|
|
|
2010-11-20 07:00:22 +09:00
|
|
|
// TODO: Suppport max_id -- we need to tweak the backend
|
2009-03-07 14:09:43 +09:00
|
|
|
// Search classes to support it.
|
|
|
|
|
|
|
|
$this->since_id = $this->trimmed('since_id');
|
|
|
|
$this->geocode = $this->trimmed('geocode');
|
|
|
|
|
|
|
|
// TODO: Also, language and geocode
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a request
|
|
|
|
*
|
|
|
|
* @param array $args Arguments from $_REQUEST
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function handle($args)
|
|
|
|
{
|
|
|
|
parent::handle($args);
|
2010-06-25 10:11:50 +09:00
|
|
|
common_debug("In apisearchatom handle()");
|
2009-03-07 14:09:43 +09:00
|
|
|
$this->showAtom();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the notices to output as results. This also sets some class
|
|
|
|
* attrs so we can use them to calculate pagination, and output
|
|
|
|
* since_id and max_id.
|
|
|
|
*
|
|
|
|
* @return array an array of Notice objects sorted in reverse chron
|
|
|
|
*/
|
|
|
|
function getNotices()
|
|
|
|
{
|
|
|
|
// TODO: Support search operators like from: and to:, boolean, etc.
|
|
|
|
|
2009-03-11 10:17:20 +09:00
|
|
|
$notices = array();
|
2009-03-07 14:09:43 +09:00
|
|
|
$notice = new Notice();
|
|
|
|
|
|
|
|
// lcase it for comparison
|
|
|
|
$q = strtolower($this->query);
|
|
|
|
|
2009-11-04 09:57:39 +09:00
|
|
|
$search_engine = $notice->getSearchEngine('notice');
|
2009-03-07 14:09:43 +09:00
|
|
|
$search_engine->set_sort_mode('chron');
|
|
|
|
$search_engine->limit(($this->page - 1) * $this->rpp,
|
|
|
|
$this->rpp + 1, true);
|
2009-06-14 17:39:31 +09:00
|
|
|
if (false === $search_engine->query($q)) {
|
|
|
|
$this->cnt = 0;
|
|
|
|
} else {
|
|
|
|
$this->cnt = $notice->find();
|
|
|
|
}
|
2009-03-07 14:09:43 +09:00
|
|
|
|
|
|
|
$cnt = 0;
|
2009-06-12 21:03:18 +09:00
|
|
|
$this->max_id = 0;
|
2009-03-07 14:09:43 +09:00
|
|
|
|
2009-06-14 17:39:31 +09:00
|
|
|
if ($this->cnt > 0) {
|
|
|
|
while ($notice->fetch()) {
|
|
|
|
++$cnt;
|
2009-03-07 14:09:43 +09:00
|
|
|
|
2009-06-14 17:39:31 +09:00
|
|
|
if (!$this->max_id) {
|
|
|
|
$this->max_id = $notice->id;
|
|
|
|
}
|
2009-03-07 14:09:43 +09:00
|
|
|
|
2010-11-20 07:00:22 +09:00
|
|
|
if ($this->since_id && $notice->id <= $this->since_id) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-06-14 17:39:31 +09:00
|
|
|
if ($cnt > $this->rpp) {
|
|
|
|
break;
|
|
|
|
}
|
2009-03-07 14:09:43 +09:00
|
|
|
|
2009-06-14 17:39:31 +09:00
|
|
|
$notices[] = clone($notice);
|
|
|
|
}
|
2009-03-07 14:09:43 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return $notices;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output search results as an Atom feed
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function showAtom()
|
|
|
|
{
|
|
|
|
$notices = $this->getNotices();
|
|
|
|
|
|
|
|
$this->initAtom();
|
|
|
|
$this->showFeed();
|
|
|
|
|
|
|
|
foreach ($notices as $n) {
|
2010-01-07 06:40:28 +09:00
|
|
|
$profile = $n->getProfile();
|
|
|
|
|
|
|
|
// Don't show notices from deleted users
|
|
|
|
|
|
|
|
if (!empty($profile)) {
|
|
|
|
$this->showEntry($n);
|
|
|
|
}
|
2009-03-07 14:09:43 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->endAtom();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show feed specific Atom elements
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function showFeed()
|
|
|
|
{
|
|
|
|
// TODO: A9 OpenSearch stuff like search.twitter.com?
|
|
|
|
|
|
|
|
$server = common_config('site', 'server');
|
|
|
|
$sitename = common_config('site', 'name');
|
|
|
|
|
2009-08-26 07:53:24 +09:00
|
|
|
// XXX: Use xmlns:statusnet instead?
|
2009-03-07 14:09:43 +09:00
|
|
|
|
|
|
|
$this->elementStart('feed',
|
|
|
|
array('xmlns' => 'http://www.w3.org/2005/Atom',
|
2009-03-11 10:17:20 +09:00
|
|
|
|
|
|
|
// XXX: xmlns:twitter causes Atom validation to fail
|
|
|
|
// It's used for the source attr on notices
|
|
|
|
|
2009-03-07 14:09:43 +09:00
|
|
|
'xmlns:twitter' => 'http://api.twitter.com/',
|
2009-03-11 10:30:58 +09:00
|
|
|
'xml:lang' => 'en-US')); // XXX Other locales ?
|
2009-03-07 14:09:43 +09:00
|
|
|
|
2010-02-21 03:31:20 +09:00
|
|
|
$taguribase = TagURI::base();
|
2009-03-11 09:20:43 +09:00
|
|
|
$this->element('id', null, "tag:$taguribase:search/$server");
|
2009-03-07 14:09:43 +09:00
|
|
|
|
|
|
|
$site_uri = common_path(false);
|
|
|
|
|
|
|
|
$search_uri = $site_uri . 'api/search.atom?q=' . urlencode($this->query);
|
|
|
|
|
|
|
|
if ($this->rpp != 15) {
|
|
|
|
$search_uri .= '&rpp=' . $this->rpp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: this alternate link is not quite right because our
|
2009-11-10 04:01:46 +09:00
|
|
|
// web-based notice search doesn't support a rpp (responses per
|
2009-03-07 14:09:43 +09:00
|
|
|
// page) param yet
|
|
|
|
|
|
|
|
$this->element('link', array('type' => 'text/html',
|
|
|
|
'rel' => 'alternate',
|
|
|
|
'href' => $site_uri . 'search/notice?q=' .
|
|
|
|
urlencode($this->query)));
|
|
|
|
|
|
|
|
// self link
|
|
|
|
|
2009-03-11 10:17:20 +09:00
|
|
|
$self_uri = $search_uri;
|
|
|
|
$self_uri .= ($this->page > 1) ? '&page=' . $this->page : '';
|
2009-03-07 14:09:43 +09:00
|
|
|
|
|
|
|
$this->element('link', array('type' => 'application/atom+xml',
|
|
|
|
'rel' => 'self',
|
|
|
|
'href' => $self_uri));
|
|
|
|
|
2010-10-28 08:19:04 +09:00
|
|
|
// @todo Needs i18n?
|
2009-03-07 14:09:43 +09:00
|
|
|
$this->element('title', null, "$this->query - $sitename Search");
|
2009-03-11 09:20:43 +09:00
|
|
|
$this->element('updated', null, common_date_iso8601('now'));
|
2009-03-07 14:09:43 +09:00
|
|
|
|
2009-03-11 10:30:58 +09:00
|
|
|
// XXX: The below "rel" links are not valid Atom, but it's what
|
|
|
|
// Twitter does...
|
|
|
|
|
2009-03-07 14:09:43 +09:00
|
|
|
// refresh link
|
|
|
|
|
|
|
|
$refresh_uri = $search_uri . "&since_id=" . $this->max_id;
|
|
|
|
|
|
|
|
$this->element('link', array('type' => 'application/atom+xml',
|
|
|
|
'rel' => 'refresh',
|
|
|
|
'href' => $refresh_uri));
|
|
|
|
|
|
|
|
// pagination links
|
|
|
|
|
|
|
|
if ($this->cnt > $this->rpp) {
|
|
|
|
|
|
|
|
$next_uri = $search_uri . "&max_id=" . $this->max_id .
|
|
|
|
'&page=' . ($this->page + 1);
|
|
|
|
|
|
|
|
$this->element('link', array('type' => 'application/atom+xml',
|
|
|
|
'rel' => 'next',
|
|
|
|
'href' => $next_uri));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->page > 1) {
|
|
|
|
|
|
|
|
$previous_uri = $search_uri . "&max_id=" . $this->max_id .
|
|
|
|
'&page=' . ($this->page - 1);
|
|
|
|
|
|
|
|
$this->element('link', array('type' => 'application/atom+xml',
|
|
|
|
'rel' => 'previous',
|
|
|
|
'href' => $previous_uri));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build an Atom entry similar to search.twitter.com's based on
|
|
|
|
* a given notice
|
|
|
|
*
|
|
|
|
* @param Notice $notice the notice to use
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function showEntry($notice)
|
|
|
|
{
|
|
|
|
$server = common_config('site', 'server');
|
|
|
|
$profile = $notice->getProfile();
|
|
|
|
$nurl = common_local_url('shownotice', array('notice' => $notice->id));
|
|
|
|
|
|
|
|
$this->elementStart('entry');
|
|
|
|
|
2010-02-21 03:31:20 +09:00
|
|
|
$taguribase = TagURI::base();
|
2009-03-07 14:09:43 +09:00
|
|
|
|
2009-03-11 09:20:43 +09:00
|
|
|
$this->element('id', null, "tag:$taguribase:$notice->id");
|
2009-03-07 14:09:43 +09:00
|
|
|
$this->element('published', null, common_date_w3dtf($notice->created));
|
|
|
|
$this->element('link', array('type' => 'text/html',
|
|
|
|
'rel' => 'alternate',
|
|
|
|
'href' => $nurl));
|
|
|
|
$this->element('title', null, common_xml_safe_str(trim($notice->content)));
|
2009-03-11 10:17:20 +09:00
|
|
|
$this->element('content', array('type' => 'html'), $notice->rendered);
|
2009-03-07 14:09:43 +09:00
|
|
|
$this->element('updated', null, common_date_w3dtf($notice->created));
|
|
|
|
$this->element('link', array('type' => 'image/png',
|
2009-03-11 10:17:20 +09:00
|
|
|
// XXX: Twitter uses rel="image" (not valid)
|
|
|
|
'rel' => 'related',
|
2009-03-07 14:09:43 +09:00
|
|
|
'href' => $profile->avatarUrl()));
|
|
|
|
|
2010-05-06 16:20:10 +09:00
|
|
|
// @todo: Here is where we'd put in a link to an atom feed for threads
|
|
|
|
|
|
|
|
$source = null;
|
|
|
|
|
|
|
|
$ns = $notice->getSource();
|
|
|
|
if ($ns) {
|
|
|
|
if (!empty($ns->name) && !empty($ns->url)) {
|
2010-05-07 06:36:13 +09:00
|
|
|
$source = '<a href="'
|
2010-10-28 08:19:04 +09:00
|
|
|
. htmlspecialchars($ns->url)
|
|
|
|
. '" rel="nofollow">'
|
|
|
|
. htmlspecialchars($ns->name)
|
|
|
|
. '</a>';
|
2010-05-06 16:20:10 +09:00
|
|
|
} else {
|
|
|
|
$source = $ns->code;
|
|
|
|
}
|
|
|
|
}
|
2009-03-07 14:09:43 +09:00
|
|
|
|
2010-05-07 06:36:13 +09:00
|
|
|
$this->element("twitter:source", null, $source);
|
2009-03-07 14:09:43 +09:00
|
|
|
|
|
|
|
$this->elementStart('author');
|
|
|
|
|
|
|
|
$name = $profile->nickname;
|
|
|
|
|
|
|
|
if ($profile->fullname) {
|
2010-10-28 08:19:04 +09:00
|
|
|
// @todo Needs proper i18n?
|
2009-03-07 14:09:43 +09:00
|
|
|
$name .= ' (' . $profile->fullname . ')';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->element('name', null, $name);
|
|
|
|
$this->element('uri', null, common_profile_uri($profile));
|
|
|
|
$this->elementEnd('author');
|
|
|
|
|
|
|
|
$this->elementEnd('entry');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the Atom output, send headers
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function initAtom()
|
|
|
|
{
|
|
|
|
header('Content-Type: application/atom+xml; charset=utf-8');
|
|
|
|
$this->startXml();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* End the Atom feed
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function endAtom()
|
|
|
|
{
|
|
|
|
$this->elementEnd('feed');
|
|
|
|
}
|
|
|
|
}
|