123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- if (!defined('STATUSNET') && !defined('LACONICA')) {
- exit(1);
- }
- require_once INSTALLDIR.'/lib/searchaction.php';
- class NoticesearchAction extends SearchAction
- {
- protected $q = null;
- function prepare(array $args = array())
- {
- parent::prepare($args);
- $this->q = $this->trimmed('q');
-
- if (preg_match('/^\#([\pL\pN_\-\.]{1,64})/u', $this->q)) {
- common_redirect(common_local_url('tag',
- array('tag' => common_canonical_tag(substr($this->q, 1)))),
- 303);
- }
- if (!empty($this->q)) {
- $stream = new SearchNoticeStream($this->q, $this->scoped);
- $page = $this->trimmed('page');
- if (empty($page)) {
- $page = 1;
- } else {
- $page = (int)$page;
- }
- $this->notice = $stream->getNotices((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
- }
- common_set_returnto($this->selfUrl());
- return true;
- }
-
- function getInstructions()
- {
-
-
- return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
- }
-
- function title()
- {
-
- return _('Text search');
- }
- function getFeeds()
- {
- $q = $this->trimmed('q');
- if (!$q) {
- return null;
- }
- return array(new Feed(Feed::RSS1, common_local_url('noticesearchrss',
- array('q' => $q)),
-
-
- sprintf(_('Search results for "%1$s" on %2$s'),
- $q, common_config('site', 'name'))));
- }
-
- function showResults($q, $page)
- {
- if (Event::handle('StartNoticeSearchShowResults', array($this, $q, $this->notice))) {
- if ($this->notice->N === 0) {
- $this->showEmptyResults($q, $page);
- } else {
- $terms = preg_split('/[\s,]+/', $q);
- $nl = new SearchNoticeList($this->notice, $this, $terms);
- $cnt = $nl->show();
- $this->pagination($page > 1,
- $cnt > NOTICES_PER_PAGE,
- $page,
- 'noticesearch',
- array('q' => $q));
- }
- Event::handle('EndNoticeSearchShowResults', array($this, $q, $this->notice));
- }
- }
- function showEmptyResults($q, $page)
- {
-
- $this->element('p', 'error', _('No results.'));
- $this->searchSuggestions($q);
- if (common_logged_in()) {
-
-
- $message = sprintf(_('Be the first to [post on this topic](%%%%action.newnotice%%%%?status_textarea=%s)!'), urlencode($q));
- }
- else {
-
-
- $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));
- }
- $this->elementStart('div', 'guide');
- $this->raw(common_markup_to_html($message));
- $this->elementEnd('div');
- return;
- }
- function showScripts()
- {
- parent::showScripts();
- $this->autofocus('q');
- }
- }
- class SearchNoticeList extends NoticeList {
- function __construct($notice, $out=null, $terms)
- {
- parent::__construct($notice, $out);
- $this->terms = $terms;
- }
- function newListItem(Notice $notice)
- {
- return new SearchNoticeListItem($notice, $this->out, $this->terms);
- }
- }
- class SearchNoticeListItem extends NoticeListItem {
- function __construct($notice, $out=null, $terms)
- {
- parent::__construct($notice, $out);
- $this->terms = $terms;
- }
- function showContent()
- {
-
- $this->out->elementStart('p', array('class' => 'e-content'));
- $this->out->raw($this->highlight($this->notice->getRendered(), $this->terms));
- $this->out->elementEnd('p');
- }
-
- function highlight($text, $terms)
- {
-
- $options = implode('|', array_map('preg_quote', array_map('htmlspecialchars', $terms),
- array_fill(0, sizeof($terms), '/')));
- $pattern = "/($options)/i";
- $result = '';
-
- $chunks = preg_split('/(<[^>]+>)/', $text, 0, PREG_SPLIT_DELIM_CAPTURE);
- foreach ($chunks as $i => $chunk) {
- if ($i % 2 == 1) {
-
- $result .= $chunk;
- } else {
-
- $result .= preg_replace($pattern, '<strong>\\1</strong>', $chunk);
- }
- }
- return $result;
- }
- }
|