3 * StatusNet, the distributed open-source microblogging tool
5 * widget for displaying a list of notices
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 2009 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 * widget-like class for showing JSON search results
39 * @author Zach Copley <zach@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://status.net/
45 class JSONSearchResultsList
47 protected $notice; // protected attrs invisible to json_encode()
50 // The below attributes are carefully named so the JSON output from
51 // this obj matches the output from search.twitter.com
57 var $results_per_page;
65 * @param Notice $notice stream of notices from DB_DataObject
66 * @param string $query the original search query
67 * @param int $rpp the number of results to display per page
68 * @param int $page a page offset
69 * @param int $since_id only display notices newer than this
72 function __construct($notice, $query, $rpp, $page, $since_id = 0)
74 $this->notice = $notice;
75 $this->query = urlencode($query);
76 $this->results_per_page = $rpp;
79 $this->since_id = $since_id;
80 $this->results = array();
84 * show the list of search results
86 * @return int $count of the search results listed.
94 $time_start = microtime(true);
96 while ($this->notice->fetch() && $cnt <= $this->rpp) {
99 // XXX: Hmmm. this depends on desc sort order
100 if (!$this->max_id) {
101 $this->max_id = (int)$this->notice->id;
104 if ($this->since_id && $this->notice->id <= $this->since_id) {
108 if ($cnt > $this->rpp) {
112 $profile = $this->notice->getProfile();
114 // Don't show notices from deleted users
116 if (!empty($profile)) {
117 $item = new ResultItem($this->notice);
118 array_push($this->results, $item);
122 $time_end = microtime(true);
123 $this->completed_in = $time_end - $time_start;
127 $this->refresh_url = '?since_id=' . $this->max_id .
128 '&q=' . $this->query;
132 if ($cnt > $this->rpp) {
133 $this->next_page = '?page=' . ($this->page + 1) .
134 '&max_id=' . $this->max_id;
135 if ($this->rpp != 15) {
136 $this->next_page .= '&rpp=' . $this->rpp;
138 $this->next_page .= '&q=' . $this->query;
141 if ($this->page > 1) {
142 $this->previous_page = '?page=' . ($this->page - 1) .
143 '&max_id=' . $this->max_id;
144 if ($this->rpp != 15) {
145 $this->previous_page .= '&rpp=' . $this->rpp;
147 $this->previous_page .= '&q=' . $this->query;
150 print json_encode($this);
157 * widget for displaying a single JSON search result
161 * @author Zach Copley <zach@status.net>
162 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
163 * @link http://status.net/
164 * @see JSONSearchResultsList
169 /** The notice this item is based on. */
171 protected $notice; // protected attrs invisible to json_encode()
173 /** The profile associated with the notice. */
177 // The below attributes are carefully named so the JSON output from
178 // this obj matches the output from search.twitter.com
186 var $iso_language_code;
188 var $source_link = null;
189 var $profile_image_url;
195 * Also initializes the profile attribute.
197 * @param Notice $notice The notice we'll display
200 function __construct($notice)
202 $this->notice = $notice;
203 $this->profile = $notice->getProfile();
204 $this->buildResult();
208 * Build a search result object
210 * This populates the the result in preparation for JSON encoding.
215 function buildResult()
217 $this->text = $this->notice->content;
218 $replier_profile = null;
220 if ($this->notice->reply_to) {
221 $reply = Notice::getKV(intval($this->notice->reply_to));
223 $replier_profile = $reply->getProfile();
227 $this->to_user_id = ($replier_profile) ?
228 intval($replier_profile->id) : null;
229 $this->to_user = ($replier_profile) ?
230 $replier_profile->nickname : null;
232 $this->from_user = $this->profile->nickname;
233 $this->id = $this->notice->id;
234 $this->from_user_id = $this->profile->id;
236 $this->iso_language_code = Profile_prefs::getConfigData($this->profile, 'site', 'language');
238 // set source and source_link
239 $this->setSourceData();
241 $this->profile_image_url = $this->profile->avatarUrl(AVATAR_STREAM_SIZE);
243 $this->created_at = common_date_rfc2822($this->notice->created);
247 * Set the notice's source data (api/app name and URL)
249 * Either the name (and link) of the API client that posted the notice,
250 * or one of other other channels. Uses the local notice object.
254 function setSourceData()
265 // Gettext translations for the below source types are available.
266 $source = _($this->notice->source);
270 $ns = Notice_source::getKV($this->notice->source);
271 if ($ns instanceof Notice_source) {
273 if (!empty($ns->url)) {
274 $source_link = $ns->url;
275 if (!empty($ns->name)) {
283 $this->source = $source;
284 $this->source_link = $source_link;