We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

apisearchjson.php 3.79 KB
Newer Older
1 2
<?php
/**
3
 * StatusNet, the distributed open-source microblogging tool
4
 *
5
 * Action for showing Twitter-like JSON search results
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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
23
 * @package   StatusNet
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/
28 29
 */

30
if (!defined('STATUSNET') && !defined('LACONICA')) {
31 32 33
    exit(1);
}

34
require_once INSTALLDIR.'/lib/apiprivateauth.php';
35
require_once INSTALLDIR.'/lib/jsonsearchresultslist.php';
36 37 38 39 40

/**
 * Action handler for Twitter-compatible API search
 *
 * @category Search
41
 * @package  StatusNet
42
 * @author   Zach Copley <zach@status.net>
43
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44
 * @link     http://status.net/
45
 * @see      ApiAction
46
 */
47
class ApiSearchJSONAction extends ApiPrivateAuthAction
48 49
{
    var $query;
50 51 52 53
    var $lang;
    var $rpp;
    var $page;
    var $since_id;
54
    var $limit;
55 56
    var $geocode;

57 58 59 60 61
    /**
     * Initialization.
     *
     * @param array $args Web and URL arguments
     *
62
     * @return boolean true if nothing goes wrong
63 64 65 66
     */
    function prepare($args)
    {
        parent::prepare($args);
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

        $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;
        }

86 87 88
        // TODO: Suppport max_id -- we need to tweak the backend
        // Search classes to support it.

89 90
        $this->since_id = $this->trimmed('since_id');
        $this->geocode  = $this->trimmed('geocode');
91 92 93

        return true;
    }
94

95 96 97 98 99 100 101 102 103 104
    /**
     * Handle a request
     *
     * @param array $args Arguments from $_REQUEST
     *
     * @return void
     */
    function handle($args)
    {
        parent::handle($args);
105
        $this->showResults();
106
    }
107

108 109 110 111 112
    /**
     * Show search results
     *
     * @return void
     */
113 114
    function showResults()
    {
115
        // TODO: Support search operators like from: and to:, boolean, etc.
116 117 118 119 120 121

        $notice = new Notice();

        // lcase it for comparison
        $q = strtolower($this->query);

122
        $search_engine = $notice->getSearchEngine('notice');
123 124
        $search_engine->set_sort_mode('chron');
        $search_engine->limit(($this->page - 1) * $this->rpp, $this->rpp + 1, true);
125 126 127 128 129
        if (false === $search_engine->query($q)) {
            $cnt = 0;
        } else {
            $cnt = $notice->find();
        }
130

131
        // TODO: max_id, lang, geocode
132

133
        $results = new JSONSearchResultsList($notice, $q, $this->rpp, $this->page, $this->since_id);
134

135
        $this->initDocument('json');
136
        $results->show();
137
        $this->endDocument('json');
138 139 140
    }

    /**
141
     * Do we need to write to the database?
142 143 144
     *
     * @return boolean true
     */
145
    function isReadOnly($args)
146
    {
147
        return true;
148
    }
149
}