noticesearch.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. protected $q = null;
  49. function prepare(array $args = array())
  50. {
  51. parent::prepare($args);
  52. $this->q = $this->trimmed('q');
  53. // FIXME: very dependent on tag format
  54. if (preg_match('/^\#([\pL\pN_\-\.]{1,64})/u', $this->q)) {
  55. common_redirect(common_local_url('tag',
  56. array('tag' => common_canonical_tag(substr($this->q, 1)))),
  57. 303);
  58. }
  59. if (!empty($this->q)) {
  60. Event::handle('StartNoticeSearch', [$this->q]);
  61. $stream = new SearchNoticeStream($this->q, $this->scoped);
  62. $page = $this->trimmed('page');
  63. if (empty($page)) {
  64. $page = 1;
  65. } else {
  66. $page = (int)$page;
  67. }
  68. $this->notice = $stream->getNotices((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
  69. }
  70. common_set_returnto($this->selfUrl());
  71. return true;
  72. }
  73. /**
  74. * Get instructions
  75. *
  76. * @return string instruction text
  77. */
  78. function getInstructions()
  79. {
  80. // TRANS: Instructions for Notice search page.
  81. // TRANS: %%site.name%% is the name of the StatusNet site.
  82. return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
  83. }
  84. /**
  85. * Get title
  86. *
  87. * @return string title
  88. */
  89. function title()
  90. {
  91. // TRANS: Title of the page where users can search for notices.
  92. return _('Text search');
  93. }
  94. function getFeeds()
  95. {
  96. $q = $this->trimmed('q');
  97. if (!$q) {
  98. return null;
  99. }
  100. return array(new Feed(Feed::RSS1, common_local_url('noticesearchrss',
  101. array('q' => $q)),
  102. // TRANS: Test in RSS notice search.
  103. // TRANS: %1$s is the query, %2$s is the StatusNet site name.
  104. sprintf(_('Search results for "%1$s" on %2$s'),
  105. $q, common_config('site', 'name'))));
  106. }
  107. /**
  108. * Show results
  109. *
  110. * @param string $q search query
  111. * @param integer $page page number
  112. *
  113. * @return void
  114. */
  115. function showResults($q, $page)
  116. {
  117. if (Event::handle('StartNoticeSearchShowResults', array($this, $q, $this->notice))) {
  118. if ($this->notice->N === 0) {
  119. $this->showEmptyResults($q, $page);
  120. } else {
  121. $terms = preg_split('/[\s,]+/', $q);
  122. $nl = new SearchNoticeList($this->notice, $this, $terms);
  123. $cnt = $nl->show();
  124. $this->pagination($page > 1,
  125. $cnt > NOTICES_PER_PAGE,
  126. $page,
  127. 'noticesearch',
  128. array('q' => $q));
  129. }
  130. Event::handle('EndNoticeSearchShowResults', array($this, $q, $this->notice));
  131. }
  132. }
  133. function showEmptyResults($q, $page)
  134. {
  135. // TRANS: Text for notice search results is the query had no results.
  136. $this->element('p', 'error', _('No results.'));
  137. $this->searchSuggestions($q);
  138. if (common_logged_in()) {
  139. // TRANS: Text for logged in users making a query for notices without results.
  140. // TRANS: This message contains a Markdown link.
  141. $message = sprintf(_('Be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
  142. }
  143. else {
  144. // TRANS: Text for not logged in users making a query for notices without results.
  145. // TRANS: This message contains Markdown links.
  146. $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));
  147. }
  148. $this->elementStart('div', 'guide');
  149. $this->raw(common_markup_to_html($message));
  150. $this->elementEnd('div');
  151. return;
  152. }
  153. function showScripts()
  154. {
  155. parent::showScripts();
  156. $this->autofocus('q');
  157. }
  158. }
  159. class SearchNoticeList extends NoticeList {
  160. function __construct($notice, $out=null, $terms)
  161. {
  162. parent::__construct($notice, $out);
  163. $this->terms = $terms;
  164. }
  165. function newListItem(Notice $notice)
  166. {
  167. return new SearchNoticeListItem($notice, $this->out, $this->terms);
  168. }
  169. }
  170. class SearchNoticeListItem extends NoticeListItem {
  171. function __construct($notice, $out=null, $terms)
  172. {
  173. parent::__construct($notice, $out);
  174. $this->terms = $terms;
  175. }
  176. function showContent()
  177. {
  178. // FIXME: URL, image, video, audio
  179. $this->out->elementStart('p', array('class' => 'e-content'));
  180. $this->out->raw($this->highlight($this->notice->getRendered(), $this->terms));
  181. $this->out->elementEnd('p');
  182. }
  183. /**
  184. * Highlist query terms
  185. *
  186. * @param string $text notice text
  187. * @param array $terms terms to highlight
  188. *
  189. * @return void
  190. */
  191. function highlight($text, $terms)
  192. {
  193. /* Highligh search terms */
  194. $options = implode('|', array_map('preg_quote', array_map('htmlspecialchars', $terms),
  195. array_fill(0, sizeof($terms), '/')));
  196. $pattern = "/($options)/i";
  197. $result = '';
  198. /* Divide up into text (highlight me) and tags (don't touch) */
  199. $chunks = preg_split('/(<[^>]+>)/', $text, 0, PREG_SPLIT_DELIM_CAPTURE);
  200. foreach ($chunks as $i => $chunk) {
  201. if ($i % 2 == 1) {
  202. // odd: delimiter (tag)
  203. $result .= $chunk;
  204. } else {
  205. // even: freetext between tags
  206. $result .= preg_replace($pattern, '<strong>\\1</strong>', $chunk);
  207. }
  208. }
  209. return $result;
  210. }
  211. }