PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/jsonsearchresultslist.php

https://github.com/Br3nda/laconica
PHP | 271 lines | 131 code | 46 blank | 94 comment | 15 complexity | 87134e29964e9e7e876ee08406615411 MD5 | raw file
Possible License(s): AGPL-3.0
  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. if (!defined('LACONICA')) {
  30. exit(1);
  31. }
  32. /**
  33. * widget-like class for showing JSON search results
  34. *
  35. * @category Search
  36. * @package Laconica
  37. * @author Zach Copley <zach@controlyourself.ca>
  38. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  39. * @link http://laconi.ca/
  40. *
  41. */
  42. class JSONSearchResultsList
  43. {
  44. protected $notice; // protected attrs invisible to json_encode()
  45. protected $rpp;
  46. // The below attributes are carefully named so the JSON output from
  47. // this obj matches the output from search.twitter.com
  48. var $results;
  49. var $since_id;
  50. var $max_id;
  51. var $refresh_url;
  52. var $results_per_page;
  53. var $completed_in;
  54. var $page;
  55. var $query;
  56. /**
  57. * constructor
  58. *
  59. * @param Notice $notice stream of notices from DB_DataObject
  60. * @param string $query the original search query
  61. * @param int $rpp the number of results to display per page
  62. * @param int $page a page offset
  63. * @param int $since_id only display notices newer than this
  64. */
  65. function __construct($notice, $query, $rpp, $page, $since_id = 0)
  66. {
  67. $this->notice = $notice;
  68. $this->query = urlencode($query);
  69. $this->results_per_page = $rpp;
  70. $this->rpp = $rpp;
  71. $this->page = $page;
  72. $this->since_id = $since_id;
  73. $this->results = array();
  74. }
  75. /**
  76. * show the list of search results
  77. *
  78. * @return int $count of the search results listed.
  79. */
  80. function show()
  81. {
  82. $cnt = 0;
  83. $this->max_id = 0;
  84. $time_start = microtime(true);
  85. while ($this->notice->fetch() && $cnt <= $this->rpp) {
  86. $cnt++;
  87. // XXX: Hmmm. this depends on desc sort order
  88. if (!$this->max_id) {
  89. $this->max_id = (int)$this->notice->id;
  90. }
  91. if ($cnt > $this->rpp) {
  92. break;
  93. }
  94. $item = new ResultItem($this->notice);
  95. array_push($this->results, $item);
  96. }
  97. $time_end = microtime(true);
  98. $this->completed_in = $time_end - $time_start;
  99. // Set other attrs
  100. $this->refresh_url = '?since_id=' . $this->max_id .
  101. '&q=' . $this->query;
  102. // pagination stuff
  103. if ($cnt > $this->rpp) {
  104. $this->next_page = '?page=' . ($this->page + 1) .
  105. '&max_id=' . $this->max_id;
  106. if ($this->rpp != 15) {
  107. $this->next_page .= '&rpp=' . $this->rpp;
  108. }
  109. $this->next_page .= '&q=' . $this->query;
  110. }
  111. if ($this->page > 1) {
  112. $this->previous_page = '?page=' . ($this->page - 1) .
  113. '&max_id=' . $this->max_id;
  114. if ($this->rpp != 15) {
  115. $this->previous_page .= '&rpp=' . $this->rpp;
  116. }
  117. $this->previous_page .= '&q=' . $this->query;
  118. }
  119. print json_encode($this);
  120. return $cnt;
  121. }
  122. }
  123. /**
  124. * widget for displaying a single JSON search result
  125. *
  126. * @category UI
  127. * @package Laconica
  128. * @author Zach Copley <zach@controlyourself.ca>
  129. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  130. * @link http://laconi.ca/
  131. * @see JSONSearchResultsList
  132. */
  133. class ResultItem
  134. {
  135. /** The notice this item is based on. */
  136. protected $notice; // protected attrs invisible to json_encode()
  137. /** The profile associated with the notice. */
  138. protected $profile;
  139. // The below attributes are carefully named so the JSON output from
  140. // this obj matches the output from search.twitter.com
  141. var $text;
  142. var $to_user_id;
  143. var $to_user;
  144. var $from_user;
  145. var $id;
  146. var $from_user_id;
  147. var $iso_language_code;
  148. var $source;
  149. var $profile_image_url;
  150. var $created_at;
  151. /**
  152. * constructor
  153. *
  154. * Also initializes the profile attribute.
  155. *
  156. * @param Notice $notice The notice we'll display
  157. */
  158. function __construct($notice)
  159. {
  160. $this->notice = $notice;
  161. $this->profile = $notice->getProfile();
  162. $this->buildResult();
  163. }
  164. /**
  165. * Build a search result object
  166. *
  167. * This populates the the result in preparation for JSON encoding.
  168. *
  169. * @return void
  170. */
  171. function buildResult()
  172. {
  173. $this->text = $this->notice->content;
  174. $replier_profile = null;
  175. if ($this->notice->reply_to) {
  176. $reply = Notice::staticGet(intval($notice->reply_to));
  177. if ($reply) {
  178. $replier_profile = $reply->getProfile();
  179. }
  180. }
  181. $this->to_user_id = ($replier_profile) ?
  182. intval($replier_profile->id) : null;
  183. $this->to_user = ($replier_profile) ?
  184. $replier_profile->nickname : null;
  185. $this->from_user = $this->profile->nickname;
  186. $this->id = $this->notice->id;
  187. $this->from_user_id = $this->profile->id;
  188. $user = User::staticGet('id', $this->profile->id);
  189. $this->iso_language_code = $this->user->language;
  190. $this->source = $this->getSourceLink($this->notice->source);
  191. $avatar = $this->profile->getAvatar(AVATAR_STREAM_SIZE);
  192. $this->profile_image_url = ($avatar) ?
  193. $avatar->displayUrl() : Avatar::defaultImage(AVATAR_STREAM_SIZE);
  194. $this->created_at = common_date_rfc2822($this->notice->created);
  195. }
  196. /**
  197. * Show the source of the notice
  198. *
  199. * Either the name (and link) of the API client that posted the notice,
  200. * or one of other other channels.
  201. *
  202. * @param string $source the source of the Notice
  203. *
  204. * @return string a fully rendered source of the Notice
  205. */
  206. function getSourceLink($source)
  207. {
  208. $source_name = _($source);
  209. switch ($source) {
  210. case 'web':
  211. case 'xmpp':
  212. case 'mail':
  213. case 'omb':
  214. case 'api':
  215. break;
  216. default:
  217. $ns = Notice_source::staticGet($source);
  218. if ($ns) {
  219. $source_name = '<a href="' . $ns->url . '">' . $ns->name . '</a>';
  220. }
  221. break;
  222. }
  223. return $source_name;
  224. }
  225. }