noticesearch.php 7.6 KB

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