X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=actions%2Fnoticesearch.php;h=bcd2eb5065009597d1f079e5b89f109fed6d3a3b;hb=d6b28c64830f632bb2f4b6f3c9369b9e56ad217a;hp=de1e7001f49fdae45731031114e2cbdeb477af3a;hpb=65045a26f3be7a958286d7f9e04f0a73e605e6d4;p=quix0rs-gnu-social.git diff --git a/actions/noticesearch.php b/actions/noticesearch.php index de1e7001f4..bcd2eb5065 100644 --- a/actions/noticesearch.php +++ b/actions/noticesearch.php @@ -48,10 +48,36 @@ require_once INSTALLDIR.'/lib/searchaction.php'; */ class NoticesearchAction extends SearchAction { - function prepare($args) + protected $q = null; + + function prepare(array $args=array()) { parent::prepare($args); + $this->q = $this->trimmed('q'); + + // FIXME: very dependent on tag format + if (preg_match('/^#([\pL\pN_\-\.]{1,64})/ue', $this->q)) { + common_redirect(common_local_url('tag', + array('tag' => common_canonical_tag(substr($this->q, 1)))), + 303); + } + + if (!empty($this->q)) { + + $profile = Profile::current(); + $stream = new SearchNoticeStream($this->q, $profile); + $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; @@ -106,18 +132,25 @@ class NoticesearchAction extends SearchAction */ function showResults($q, $page) { - $notice = new Notice(); - - $search_engine = $notice->getSearchEngine('notice'); - $search_engine->set_sort_mode('chron'); - // Ask for an extra to see if there's more. - $search_engine->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1); - if (false === $search_engine->query($q)) { - $cnt = 0; - } else { - $cnt = $notice->find(); + 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)); } - if ($cnt === 0) { + } + + function showEmptyResults($q, $page) + { // TRANS: Text for notice search results is the query had no results. $this->element('p', 'error', _('No results.')); @@ -137,12 +170,6 @@ class NoticesearchAction extends SearchAction $this->raw(common_markup_to_html($message)); $this->elementEnd('div'); return; - } - $terms = preg_split('/[\s,]+/', $q); - $nl = new SearchNoticeList($notice, $this, $terms); - $cnt = $nl->show(); - $this->pagination($page > 1, $cnt > NOTICES_PER_PAGE, - $page, 'noticesearch', array('q' => $q)); } function showScripts() @@ -159,7 +186,7 @@ class SearchNoticeList extends NoticeList { $this->terms = $terms; } - function newListItem($notice) + function newListItem(Notice $notice) { return new SearchNoticeListItem($notice, $this->out, $this->terms); } @@ -175,15 +202,8 @@ class SearchNoticeListItem extends NoticeListItem { function showContent() { // FIXME: URL, image, video, audio - $this->out->elementStart('p', array('class' => 'entry-content')); - if ($this->notice->rendered) { - $this->out->raw($this->highlight($this->notice->rendered, $this->terms)); - } else { - // XXX: may be some uncooked notices in the DB, - // we cook them right now. This should probably disappear in future - // versions (>> 0.4.x) - $this->out->raw($this->highlight(common_render_content($this->notice->content, $this->notice), $this->terms)); - } + $this->out->elementStart('p', array('class' => 'e-content')); + $this->out->raw($this->highlight($this->notice->getRendered(), $this->terms)); $this->out->elementEnd('p'); } @@ -202,13 +222,20 @@ class SearchNoticeListItem extends NoticeListItem { $options = implode('|', array_map('preg_quote', array_map('htmlspecialchars', $terms), array_fill(0, sizeof($terms), '/'))); $pattern = "/($options)/i"; - $result = preg_replace($pattern, '\\1', $text); + $result = ''; + + /* Divide up into text (highlight me) and tags (don't touch) */ + $chunks = preg_split('/(<[^>]+>)/', $text, 0, PREG_SPLIT_DELIM_CAPTURE); + foreach ($chunks as $i => $chunk) { + if ($i % 2 == 1) { + // odd: delimiter (tag) + $result .= $chunk; + } else { + // even: freetext between tags + $result .= preg_replace($pattern, '\\1', $chunk); + } + } - /* Remove highlighting from inside links, loop incase multiple highlights in links */ - $pattern = '/(\w+="[^"]*)('.$options.')<\/strong>([^"]*")/iU'; - do { - $result = preg_replace($pattern, '\\1\\2\\3', $result, -1, $count); - } while ($count); return $result; } }