123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- <?php
- if (!defined('STATUSNET') && !defined('LACONICA')) {
- exit(1);
- }
- class ApiSearchAtomAction extends ApiPrivateAuthAction
- {
- var $cnt;
- var $query;
- var $lang;
- var $rpp;
- var $page;
- var $since_id;
- var $geocode;
-
- function __construct($output='php://output', $indent=null)
- {
- parent::__construct($output, $indent);
- }
-
- function isReadonly()
- {
- return true;
- }
-
- function prepare(array $args = array())
- {
- 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;
- }
-
-
- $this->since_id = $this->trimmed('since_id');
- $this->geocode = $this->trimmed('geocode');
-
- return true;
- }
-
- function handle()
- {
- parent::handle();
- common_debug("In apisearchatom handle()");
- $this->showAtom();
- }
-
- function getNotices()
- {
-
- $notices = array();
- $notice = new Notice();
-
- $q = strtolower($this->query);
- $search_engine = $notice->getSearchEngine('notice');
- $search_engine->set_sort_mode('chron');
- $search_engine->limit(($this->page - 1) * $this->rpp,
- $this->rpp + 1, true);
- if (false === $search_engine->query($q)) {
- $this->cnt = 0;
- } else {
- $this->cnt = $notice->find();
- }
- $cnt = 0;
- $this->max_id = 0;
- if ($this->cnt > 0) {
- while ($notice->fetch()) {
- ++$cnt;
- if (!$this->max_id) {
- $this->max_id = $notice->id;
- }
- if ($this->since_id && $notice->id <= $this->since_id) {
- break;
- }
- if ($cnt > $this->rpp) {
- break;
- }
- $notices[] = clone($notice);
- }
- }
- return $notices;
- }
-
- function showAtom()
- {
- $notices = $this->getNotices();
- $this->initAtom();
- $this->showFeed();
- foreach ($notices as $n) {
- $profile = $n->getProfile();
-
- if (!empty($profile)) {
- $this->showEntry($n);
- }
- }
- $this->endAtom();
- }
-
- function showFeed()
- {
-
- $server = common_config('site', 'server');
- $sitename = common_config('site', 'name');
-
- $this->elementStart('feed',
- array('xmlns' => 'http://www.w3.org/2005/Atom',
-
-
- 'xmlns:twitter' => 'http://api.twitter.com/',
- 'xml:lang' => 'en-US'));
- $taguribase = TagURI::base();
- $this->element('id', null, "tag:$taguribase:search/$server");
- $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;
- }
-
-
-
- $this->element('link', array('type' => 'text/html',
- 'rel' => 'alternate',
- 'href' => $site_uri . 'search/notice?q=' .
- urlencode($this->query)));
-
- $self_uri = $search_uri;
- $self_uri .= ($this->page > 1) ? '&page=' . $this->page : '';
- $this->element('link', array('type' => 'application/atom+xml',
- 'rel' => 'self',
- 'href' => $self_uri));
-
- $this->element('title', null, "$this->query - $sitename Search");
- $this->element('updated', null, common_date_iso8601('now'));
-
-
-
- $refresh_uri = $search_uri . "&since_id=" . $this->max_id;
- $this->element('link', array('type' => 'application/atom+xml',
- 'rel' => 'refresh',
- 'href' => $refresh_uri));
-
- 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));
- }
- }
-
- function showEntry($notice)
- {
- $server = common_config('site', 'server');
- $profile = $notice->getProfile();
- $nurl = common_local_url('shownotice', array('notice' => $notice->id));
- $this->elementStart('entry');
- $taguribase = TagURI::base();
- $this->element('id', null, "tag:$taguribase:$notice->id");
- $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)));
- $this->element('content', array('type' => 'html'), $notice->getRendered());
- $this->element('updated', null, common_date_w3dtf($notice->created));
- $this->element('link', array('type' => 'image/png',
-
- 'rel' => 'related',
- 'href' => $profile->avatarUrl()));
-
- $source = null;
- $source_link = null;
- $ns = $notice->getSource();
- if ($ns instanceof Notice_source) {
- $source = $ns->code;
- if (!empty($ns->url)) {
- $source_link = $ns->url;
- if (!empty($ns->name)) {
- $source = $ns->name;
- }
- }
- }
- $this->element("twitter:source", null, $source);
- $this->element("twitter:source_link", null, $source_link);
- $this->elementStart('author');
- $name = $profile->nickname;
- if ($profile->fullname) {
-
- $name .= ' (' . $profile->fullname . ')';
- }
- $this->element('name', null, $name);
- $this->element('uri', null, common_profile_uri($profile));
- $this->elementEnd('author');
- $this->elementEnd('entry');
- }
-
- function initAtom()
- {
- header('Content-Type: application/atom+xml; charset=utf-8');
- $this->startXml();
- }
-
- function endAtom()
- {
- $this->elementEnd('feed');
- }
- }
|