| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2009-08-25 18:12:20 -04:00
										 |  |  |  * StatusNet, the distributed open-source microblogging tool | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * widget for displaying a list of notices | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 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-25 18:12:20 -04:00
										 |  |  |  * @package   StatusNet | 
					
						
							| 
									
										
										
										
											2009-08-25 18:19:04 -04:00
										 |  |  |  * @author    Zach Copley <zach@status.net> | 
					
						
							| 
									
										
										
										
											2009-08-25 18:12:20 -04:00
										 |  |  |  * @copyright 2009 StatusNet, Inc. | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 | 
					
						
							| 
									
										
										
										
											2009-08-25 18:16:46 -04:00
										 |  |  |  * @link      http://status.net/ | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-26 10:41:36 -04:00
										 |  |  | if (!defined('STATUSNET') && !defined('LACONICA')) { | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |     exit(1); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * widget-like class for showing JSON search results | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @category Search | 
					
						
							| 
									
										
										
										
											2009-08-25 18:12:20 -04:00
										 |  |  |  * @package  StatusNet | 
					
						
							| 
									
										
										
										
											2009-08-25 18:19:04 -04:00
										 |  |  |  * @author   Zach Copley <zach@status.net> | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 | 
					
						
							| 
									
										
										
										
											2009-08-25 18:16:46 -04:00
										 |  |  |  * @link     http://status.net/ | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |  * | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class JSONSearchResultsList | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     protected $notice;  // protected attrs invisible to json_encode()
 | 
					
						
							|  |  |  |     protected $rpp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // The below attributes are carefully named so the JSON output from
 | 
					
						
							|  |  |  |     // this obj matches the output from search.twitter.com
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var $results; | 
					
						
							|  |  |  |     var $since_id; | 
					
						
							|  |  |  |     var $max_id; | 
					
						
							|  |  |  |     var $refresh_url; | 
					
						
							|  |  |  |     var $results_per_page; | 
					
						
							|  |  |  |     var $completed_in; | 
					
						
							|  |  |  |     var $page; | 
					
						
							|  |  |  |     var $query; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * constructor | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2009-03-06 21:09:43 -08:00
										 |  |  |      * @param Notice $notice   stream of notices from DB_DataObject | 
					
						
							|  |  |  |      * @param string $query    the original search query | 
					
						
							|  |  |  |      * @param int    $rpp      the number of results to display per page | 
					
						
							|  |  |  |      * @param int    $page     a page offset | 
					
						
							|  |  |  |      * @param int    $since_id only display notices newer than this | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function __construct($notice, $query, $rpp, $page, $since_id = 0) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->notice           = $notice; | 
					
						
							|  |  |  |         $this->query            = urlencode($query); | 
					
						
							| 
									
										
										
										
											2009-03-06 21:09:43 -08:00
										 |  |  |         $this->results_per_page = $rpp; | 
					
						
							|  |  |  |         $this->rpp              = $rpp; | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |         $this->page             = $page; | 
					
						
							|  |  |  |         $this->since_id         = $since_id; | 
					
						
							|  |  |  |         $this->results          = array(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * show the list of search results | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2009-03-06 21:09:43 -08:00
										 |  |  |      * @return int $count of the search results listed. | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function show() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $cnt = 0; | 
					
						
							| 
									
										
										
										
											2009-06-12 20:03:18 +08:00
										 |  |  |         $this->max_id = 0; | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $time_start = microtime(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         while ($this->notice->fetch() && $cnt <= $this->rpp) { | 
					
						
							|  |  |  |             $cnt++; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // XXX: Hmmm. this depends on desc sort order
 | 
					
						
							|  |  |  |             if (!$this->max_id) { | 
					
						
							|  |  |  |                 $this->max_id = (int)$this->notice->id; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-11-19 14:00:22 -08:00
										 |  |  |             if ($this->since_id && $this->notice->id <= $this->since_id) { | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |             if ($cnt > $this->rpp) { | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-01-06 13:40:28 -08:00
										 |  |  |             $profile = $this->notice->getProfile(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             // Don't show notices from deleted users
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!empty($profile)) { | 
					
						
							|  |  |  |                 $item = new ResultItem($this->notice); | 
					
						
							|  |  |  |                 array_push($this->results, $item); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-06 21:09:43 -08:00
										 |  |  |         $time_end           = microtime(true); | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |         $this->completed_in = $time_end - $time_start; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Set other attrs
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->refresh_url = '?since_id=' . $this->max_id . | 
					
						
							|  |  |  |             '&q=' . $this->query; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // pagination stuff
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($cnt > $this->rpp) { | 
					
						
							|  |  |  |             $this->next_page = '?page=' . ($this->page + 1) . | 
					
						
							|  |  |  |                 '&max_id=' . $this->max_id; | 
					
						
							|  |  |  |             if ($this->rpp != 15) { | 
					
						
							|  |  |  |                 $this->next_page .= '&rpp=' . $this->rpp; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $this->next_page .= '&q=' . $this->query; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($this->page > 1) { | 
					
						
							|  |  |  |             $this->previous_page = '?page=' . ($this->page - 1) . | 
					
						
							|  |  |  |                 '&max_id=' . $this->max_id; | 
					
						
							|  |  |  |             if ($this->rpp != 15) { | 
					
						
							|  |  |  |                 $this->previous_page .= '&rpp=' . $this->rpp; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $this->previous_page .= '&q=' . $this->query; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         print json_encode($this); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $cnt; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * widget for displaying a single JSON search result | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @category UI | 
					
						
							| 
									
										
										
										
											2009-08-25 18:12:20 -04:00
										 |  |  |  * @package  StatusNet | 
					
						
							| 
									
										
										
										
											2009-08-25 18:19:04 -04:00
										 |  |  |  * @author   Zach Copley <zach@status.net> | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 | 
					
						
							| 
									
										
										
										
											2009-08-25 18:16:46 -04:00
										 |  |  |  * @link     http://status.net/ | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |  * @see      JSONSearchResultsList | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ResultItem | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** The notice this item is based on. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected $notice;  // protected attrs invisible to json_encode()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** The profile associated with the notice. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected $profile; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // The below attributes are carefully named so the JSON output from
 | 
					
						
							|  |  |  |     // this obj matches the output from search.twitter.com
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     var $text; | 
					
						
							|  |  |  |     var $to_user_id; | 
					
						
							|  |  |  |     var $to_user; | 
					
						
							|  |  |  |     var $from_user; | 
					
						
							|  |  |  |     var $id; | 
					
						
							|  |  |  |     var $from_user_id; | 
					
						
							|  |  |  |     var $iso_language_code; | 
					
						
							| 
									
										
										
										
											2016-09-02 00:55:46 +02:00
										 |  |  |     var $source = null; | 
					
						
							|  |  |  |     var $source_link = null; | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |     var $profile_image_url; | 
					
						
							|  |  |  |     var $created_at; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * constructor | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * Also initializes the profile attribute. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param Notice $notice The notice we'll display | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function __construct($notice) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->notice  = $notice; | 
					
						
							|  |  |  |         $this->profile = $notice->getProfile(); | 
					
						
							|  |  |  |         $this->buildResult(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Build a search result object | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * This populates the the result in preparation for JSON encoding. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return void | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function buildResult() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-03-06 21:09:43 -08:00
										 |  |  |         $this->text      = $this->notice->content; | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |         $replier_profile = null; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($this->notice->reply_to) { | 
					
						
							| 
									
										
										
										
											2013-08-18 13:04:58 +02:00
										 |  |  |             $reply = Notice::getKV(intval($this->notice->reply_to)); | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |             if ($reply) { | 
					
						
							|  |  |  |                 $replier_profile = $reply->getProfile(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->to_user_id = ($replier_profile) ? | 
					
						
							|  |  |  |             intval($replier_profile->id) : null; | 
					
						
							| 
									
										
										
										
											2009-03-06 21:09:43 -08:00
										 |  |  |         $this->to_user    = ($replier_profile) ? | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |             $replier_profile->nickname : null; | 
					
						
							| 
									
										
										
										
											2009-03-06 21:09:43 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $this->from_user    = $this->profile->nickname; | 
					
						
							|  |  |  |         $this->id           = $this->notice->id; | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |         $this->from_user_id = $this->profile->id; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-06 21:30:29 +02:00
										 |  |  |         $this->iso_language_code = Profile_prefs::getConfigData($this->profile, 'site', 'language'); | 
					
						
							| 
									
										
										
										
											2012-07-03 17:12:59 -04:00
										 |  |  |          | 
					
						
							| 
									
										
										
										
											2016-09-02 00:55:46 +02:00
										 |  |  |         // set source and source_link
 | 
					
						
							|  |  |  |         $this->setSourceData(); | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-01 11:37:59 +02:00
										 |  |  |         $this->profile_image_url = $this->profile->avatarUrl(AVATAR_STREAM_SIZE); | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-04-13 15:03:34 -07:00
										 |  |  |         $this->created_at = common_date_rfc2822($this->notice->created); | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2016-09-02 00:55:46 +02:00
										 |  |  |      * Set the notice's source data (api/app name and URL) | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |      * | 
					
						
							|  |  |  |      * Either the name (and link) of the API client that posted the notice, | 
					
						
							| 
									
										
										
										
											2016-09-02 00:55:46 +02:00
										 |  |  |      * or one of other other channels. Uses the local notice object. | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2016-09-02 00:55:46 +02:00
										 |  |  |      * @return void | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-09-02 00:55:46 +02:00
										 |  |  |     function setSourceData() | 
					
						
							| 
									
										
										
										
											2009-03-06 21:09:43 -08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-09-02 00:55:46 +02:00
										 |  |  |         $source = null; | 
					
						
							|  |  |  |         $source_link = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-06 21:09:43 -08:00
										 |  |  |         switch ($source) { | 
					
						
							|  |  |  |         case 'web': | 
					
						
							|  |  |  |         case 'xmpp': | 
					
						
							|  |  |  |         case 'mail': | 
					
						
							|  |  |  |         case 'omb': | 
					
						
							|  |  |  |         case 'api': | 
					
						
							| 
									
										
										
										
											2016-09-02 00:55:46 +02:00
										 |  |  |             // Gettext translations for the below source types are available.
 | 
					
						
							|  |  |  |             $source = _($this->notice->source); | 
					
						
							| 
									
										
										
										
											2009-03-06 21:09:43 -08:00
										 |  |  |             break; | 
					
						
							| 
									
										
										
										
											2016-09-02 00:55:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-03-06 21:09:43 -08:00
										 |  |  |         default: | 
					
						
							| 
									
										
										
										
											2016-09-02 00:55:46 +02:00
										 |  |  |             $ns = Notice_source::getKV($this->notice->source); | 
					
						
							| 
									
										
										
										
											2014-11-05 19:44:22 +01:00
										 |  |  |             if ($ns instanceof Notice_source) { | 
					
						
							| 
									
										
										
										
											2016-09-02 00:55:46 +02:00
										 |  |  |                 $source = $ns->code; | 
					
						
							|  |  |  |                 if (!empty($ns->url)) { | 
					
						
							|  |  |  |                     $source_link = $ns->url; | 
					
						
							|  |  |  |                     if (!empty($ns->name)) { | 
					
						
							|  |  |  |                         $source = $ns->name; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2009-03-06 21:09:43 -08:00
										 |  |  |             } | 
					
						
							|  |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-02 00:55:46 +02:00
										 |  |  |         $this->source = $source; | 
					
						
							|  |  |  |         $this->source_link = $source_link; | 
					
						
							| 
									
										
										
										
											2009-03-06 21:09:43 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2009-03-06 13:33:47 -08:00
										 |  |  | } |