]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/jsonsearchresultslist.php
Merge branch '0.8.x' into twitter-import
[quix0rs-gnu-social.git] / lib / jsonsearchresultslist.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * widget for displaying a list of notices
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  Search
23  * @package   Laconica
24  * @author    Zach Copley <zach@controlyourself.ca>
25  * @copyright 2009 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 /**
35  * widget-like class for showing JSON search results
36  *
37  * @category Search
38  * @package  Laconica
39  * @author   Zach Copley <zach@controlyourself.ca>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://laconi.ca/
42  *
43  */
44
45 class JSONSearchResultsList
46 {
47     protected $notice;  // protected attrs invisible to json_encode()
48     protected $rpp;
49
50     // The below attributes are carefully named so the JSON output from
51     // this obj matches the output from search.twitter.com
52
53     var $results;
54     var $since_id;
55     var $max_id;
56     var $refresh_url;
57     var $results_per_page;
58     var $completed_in;
59     var $page;
60     var $query;
61
62     /**
63      * constructor
64      *
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
70      */
71
72     function __construct($notice, $query, $rpp, $page, $since_id = 0)
73     {
74         $this->notice           = $notice;
75         $this->query            = urlencode($query);
76         $this->results_per_page = $rpp;
77         $this->rpp              = $rpp;
78         $this->page             = $page;
79         $this->since_id         = $since_id;
80         $this->results          = array();
81     }
82
83     /**
84      * show the list of search results
85      *
86      * @return int $count of the search results listed.
87      */
88
89     function show()
90     {
91         $cnt = 0;
92
93         $time_start = microtime(true);
94
95         while ($this->notice->fetch() && $cnt <= $this->rpp) {
96             $cnt++;
97
98             // XXX: Hmmm. this depends on desc sort order
99             if (!$this->max_id) {
100                 $this->max_id = (int)$this->notice->id;
101             }
102
103             if ($cnt > $this->rpp) {
104                 break;
105             }
106
107             $item = new ResultItem($this->notice);
108             array_push($this->results, $item);
109         }
110
111         $time_end           = microtime(true);
112         $this->completed_in = $time_end - $time_start;
113
114         // Set other attrs
115
116         $this->refresh_url = '?since_id=' . $this->max_id .
117             '&q=' . $this->query;
118
119         // pagination stuff
120
121         if ($cnt > $this->rpp) {
122             $this->next_page = '?page=' . ($this->page + 1) .
123                 '&max_id=' . $this->max_id;
124             if ($this->rpp != 15) {
125                 $this->next_page .= '&rpp=' . $this->rpp;
126             }
127             $this->next_page .= '&q=' . $this->query;
128         }
129
130         if ($this->page > 1) {
131             $this->previous_page = '?page=' . ($this->page - 1) .
132                 '&max_id=' . $this->max_id;
133             if ($this->rpp != 15) {
134                 $this->previous_page .= '&rpp=' . $this->rpp;
135             }
136             $this->previous_page .= '&q=' . $this->query;
137         }
138
139         print json_encode($this);
140
141         return $cnt;
142     }
143 }
144
145 /**
146  * widget for displaying a single JSON search result
147  *
148  * @category UI
149  * @package  Laconica
150  * @author   Zach Copley <zach@controlyourself.ca>
151  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
152  * @link     http://laconi.ca/
153  * @see      JSONSearchResultsList
154  */
155
156 class ResultItem
157 {
158     /** The notice this item is based on. */
159
160     protected $notice;  // protected attrs invisible to json_encode()
161
162     /** The profile associated with the notice. */
163
164     protected $profile;
165
166     // The below attributes are carefully named so the JSON output from
167     // this obj matches the output from search.twitter.com
168
169     var $text;
170     var $to_user_id;
171     var $to_user;
172     var $from_user;
173     var $id;
174     var $from_user_id;
175     var $iso_language_code;
176     var $source;
177     var $profile_image_url;
178     var $created_at;
179
180     /**
181      * constructor
182      *
183      * Also initializes the profile attribute.
184      *
185      * @param Notice $notice The notice we'll display
186      */
187
188     function __construct($notice)
189     {
190         $this->notice  = $notice;
191         $this->profile = $notice->getProfile();
192         $this->buildResult();
193     }
194
195     /**
196      * Build a search result object
197      *
198      * This populates the the result in preparation for JSON encoding.
199      *
200      * @return void
201      */
202
203     function buildResult()
204     {
205         $this->text      = $this->notice->content;
206         $replier_profile = null;
207
208         if ($this->notice->reply_to) {
209             $reply = Notice::staticGet(intval($notice->reply_to));
210             if ($reply) {
211                 $replier_profile = $reply->getProfile();
212             }
213         }
214
215         $this->to_user_id = ($replier_profile) ?
216             intval($replier_profile->id) : null;
217         $this->to_user    = ($replier_profile) ?
218             $replier_profile->nickname : null;
219
220         $this->from_user    = $this->profile->nickname;
221         $this->id           = $this->notice->id;
222         $this->from_user_id = $this->profile->id;
223
224         $user = User::staticGet('id', $this->profile->id);
225
226         $this->iso_language_code = $this->user->language;
227
228         $this->source = $this->getSourceLink($this->notice->source);
229
230         $avatar = $this->profile->getAvatar(AVATAR_STREAM_SIZE);
231
232         $this->profile_image_url = ($avatar) ?
233             $avatar->displayUrl() : Avatar::defaultImage(AVATAR_STREAM_SIZE);
234
235         $this->created_at = common_date_rfc2822($this->notice->created);
236     }
237
238     /**
239      * Show the source of the notice
240      *
241      * Either the name (and link) of the API client that posted the notice,
242      * or one of other other channels.
243      *
244      * @param string $source the source of the Notice
245      *
246      * @return string a fully rendered source of the Notice
247      */
248
249     function getSourceLink($source)
250     {
251         $source_name = _($source);
252         switch ($source) {
253         case 'web':
254         case 'xmpp':
255         case 'mail':
256         case 'omb':
257         case 'api':
258             break;
259         default:
260             $ns = Notice_source::staticGet($source);
261             if ($ns) {
262                 $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
263             }
264             break;
265         }
266
267         return $source_name;
268     }
269
270 }