noticesearch.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Notice search action class.
  4. *
  5. * PHP version 5
  6. *
  7. * @category Action
  8. * @package StatusNet
  9. * @author Evan Prodromou <evan@status.net>
  10. * @author Robin Millette <millette@status.net>
  11. * @author Sarven Capadisli <csarven@status.net>
  12. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  13. * @link http://status.net/
  14. *
  15. * StatusNet - the distributed open-source microblogging tool
  16. * Copyright (C) 2008, 2009, StatusNet, Inc.
  17. *
  18. * This program is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License as published by
  20. * the Free Software Foundation, either version 3 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. */
  31. if (!defined('STATUSNET') && !defined('LACONICA')) {
  32. exit(1);
  33. }
  34. require_once INSTALLDIR . '/lib/search/searchaction.php';
  35. /**
  36. * Notice search action class.
  37. *
  38. * @category Action
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @author Robin Millette <millette@status.net>
  42. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  43. * @link http://status.net/
  44. * @todo common parent for people and content search?
  45. */
  46. class NoticesearchAction extends SearchAction
  47. {
  48. public $notice;
  49. protected $q = null;
  50. function prepare(array $args = array())
  51. {
  52. parent::prepare($args);
  53. $this->q = $this->trimmed('q');
  54. // FIXME: very dependent on tag format
  55. if (preg_match('/^\#([\pL\pN_\-\.]{1,64})/u', $this->q)) {
  56. common_redirect(common_local_url('tag',
  57. array('tag' => common_canonical_tag(substr($this->q, 1)))),
  58. 303);
  59. }
  60. if (!empty($this->q)) {
  61. Event::handle('StartNoticeSearch', [$this->q]);
  62. $stream = new SearchNoticeStream($this->q, $this->scoped);
  63. $page = $this->trimmed('page');
  64. if (empty($page)) {
  65. $page = 1;
  66. } else {
  67. $page = (int)$page;
  68. }
  69. $this->notice = $stream->getNotices((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
  70. }
  71. common_set_returnto($this->selfUrl());
  72. return true;
  73. }
  74. /**
  75. * Get instructions
  76. *
  77. * @return string instruction text
  78. */
  79. function getInstructions()
  80. {
  81. // TRANS: Instructions for Notice search page.
  82. // TRANS: %%site.name%% is the name of the StatusNet site.
  83. return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
  84. }
  85. /**
  86. * Get title
  87. *
  88. * @return string title
  89. */
  90. function title()
  91. {
  92. // TRANS: Title of the page where users can search for notices.
  93. return _('Text search');
  94. }
  95. function getFeeds()
  96. {
  97. $q = $this->trimmed('q');
  98. if (!$q) {
  99. return null;
  100. }
  101. return array(new Feed(Feed::RSS1, common_local_url('noticesearchrss',
  102. array('q' => $q)),
  103. // TRANS: Test in RSS notice search.
  104. // TRANS: %1$s is the query, %2$s is the StatusNet site name.
  105. sprintf(_('Search results for "%1$s" on %2$s'),
  106. $q, common_config('site', 'name'))));
  107. }
  108. /**
  109. * Show results
  110. *
  111. * @param string $q search query
  112. * @param integer $page page number
  113. *
  114. * @return void
  115. */
  116. function showResults($q, $page)
  117. {
  118. if (Event::handle('StartNoticeSearchShowResults', array($this, $q, $this->notice))) {
  119. if ($this->notice->N === 0) {
  120. $this->showEmptyResults($q, $page);
  121. } else {
  122. $terms = preg_split('/[\s,]+/', $q);
  123. $nl = new SearchNoticeList($this->notice, $this, $terms);
  124. $cnt = $nl->show();
  125. $this->pagination($page > 1,
  126. $cnt > NOTICES_PER_PAGE,
  127. $page,
  128. 'noticesearch',
  129. array('q' => $q));
  130. }
  131. Event::handle('EndNoticeSearchShowResults', array($this, $q, $this->notice));
  132. }
  133. }
  134. function showEmptyResults($q, $page)
  135. {
  136. // TRANS: Text for notice search results is the query had no results.
  137. $this->element('p', 'error', _('No results.'));
  138. $this->searchSuggestions($q);
  139. if (common_logged_in()) {
  140. // TRANS: Text for logged in users making a query for notices without results.
  141. // TRANS: This message contains a Markdown link.
  142. $message = sprintf(_('Be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
  143. }
  144. else {
  145. // TRANS: Text for not logged in users making a query for notices without results.
  146. // TRANS: This message contains Markdown links.
  147. $message = sprintf(_('Why not [register an account](%%%%action.register%%%%) and be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
  148. }
  149. $this->elementStart('div', 'guide');
  150. $this->raw(common_markup_to_html($message));
  151. $this->elementEnd('div');
  152. return;
  153. }
  154. function showScripts()
  155. {
  156. parent::showScripts();
  157. $this->autofocus('q');
  158. }
  159. }
  160. class SearchNoticeList extends NoticeList {
  161. public $terms;
  162. function __construct($notice, $out=null, $terms=null)
  163. {
  164. parent::__construct($notice, $out);
  165. $this->terms = $terms;
  166. }
  167. function newListItem(Notice $notice)
  168. {
  169. return new SearchNoticeListItem($notice, $this->out, $this->terms);
  170. }
  171. }
  172. class SearchNoticeListItem extends NoticeListItem {
  173. public $terms;
  174. function __construct($notice, $out=null, $terms=null)
  175. {
  176. parent::__construct($notice, $out);
  177. $this->terms = $terms;
  178. }
  179. function showContent()
  180. {
  181. // FIXME: URL, image, video, audio
  182. $this->out->elementStart('p', array('class' => 'e-content'));
  183. $this->out->raw($this->highlight($this->notice->getRendered(), $this->terms));
  184. $this->out->elementEnd('p');
  185. }
  186. /**
  187. * Highlist query terms
  188. *
  189. * @param string $text notice text
  190. * @param array $terms terms to highlight
  191. *
  192. * @return void
  193. */
  194. function highlight($text, $terms)
  195. {
  196. /* Highligh search terms */
  197. $options = implode('|', array_map('preg_quote', array_map('htmlspecialchars', $terms),
  198. array_fill(0, sizeof($terms), '/')));
  199. $pattern = "/($options)/i";
  200. $result = '';
  201. /* Divide up into text (highlight me) and tags (don't touch) */
  202. $chunks = preg_split('/(<[^>]+>)/', $text, 0, PREG_SPLIT_DELIM_CAPTURE);
  203. foreach ($chunks as $i => $chunk) {
  204. if ($i % 2 == 1) {
  205. // odd: delimiter (tag)
  206. $result .= $chunk;
  207. } else {
  208. // even: freetext between tags
  209. $result .= preg_replace($pattern, '<strong>\\1</strong>', $chunk);
  210. }
  211. }
  212. return $result;
  213. }
  214. }