]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - actions/noticesearch.php
fix calls to show_rss_timeline
[quix0rs-gnu-social.git] / actions / noticesearch.php
index 721c843eed749cf2fb004cd60c50476aa18bf729..e6de21ae068be9c818a10c6ff1e7d037fa4c37db 100644 (file)
 if (!defined('LACONICA')) { exit(1); }
 
 require_once(INSTALLDIR.'/lib/searchaction.php');
-define(NOTICES_PER_PAGE, 20);
+define('NOTICES_PER_PAGE', 20);
 
 # XXX common parent for people and content search?
 
 class NoticesearchAction extends SearchAction {
-       
+
        function get_instructions() {
-               return _t('Search for notices on %%site.name%% by their contents. ' . 
-                                 'Separate search terms by spaces; they must be 3 characters or more.');
+               return _('Search for notices on %%site.name%% by their contents. Separate search terms by spaces; they must be 3 characters or more.');
        }
-       
+
        function get_title() {
-               return _t('Text search');
+               return _('Text search');
        }
-       
+
        function show_results($q, $page) {
-               
+
                $notice = new Notice();
 
                # lcase it for comparison
                $q = strtolower($q);
-               $notice->whereAdd('MATCH(content) against (\''.addslashes($q).'\')');
+
+               if(common_config('db','type')=='mysql') {
+                       $notice->whereAdd('MATCH(content) against (\''.addslashes($q).'\')');
+               } else {
+                       $notice->whereAdd('to_tsvector(\'english\', content) @@ plainto_tsquery(\''.addslashes($q).'\')');
+               }
 
                # Ask for an extra to see if there's more.
-               
+
                $notice->limit((($page-1)*NOTICES_PER_PAGE), NOTICES_PER_PAGE + 1);
 
                $cnt = $notice->find();
 
                if ($cnt > 0) {
                        $terms = preg_split('/[\s,]+/', $q);
-                       common_element_start('ul', array('id' => 'notice'));
+                       common_element_start('ul', array('id' => 'notices'));
                        for ($i = 0; $i < min($cnt, NOTICES_PER_PAGE); $i++) {
                                if ($notice->fetch()) {
                                        $this->show_notice($notice, $terms);
@@ -62,17 +66,35 @@ class NoticesearchAction extends SearchAction {
                        }
                        common_element_end('ul');
                } else {
-                       common_element('p', 'error', _t('No results'));
+                       common_element('p', 'error', _('No results'));
                }
-               
+
                common_pagination($page > 1, $cnt > NOTICES_PER_PAGE,
                                                  $page, 'noticesearch', array('q' => $q));
        }
 
+       function show_header($arr) {
+               if ($arr) {
+                       $q = $arr[0];
+               }
+               if ($q) {
+                       common_element('link', array('rel' => 'alternate',
+                                                                                'href' => common_local_url('noticesearchrss',
+                                                                                                                                       array('q' => $q)),
+                                                                                'type' => 'application/rss+xml',
+                                                                                'title' => _('Search Stream Feed')));
+               }
+       }
+
        # XXX: refactor and combine with StreamAction::show_notice()
-       
+
        function show_notice($notice, $terms) {
                $profile = $notice->getProfile();
+               if (!$profile) {
+                       common_log_db_error($notice, 'SELECT', __FILE__);
+                       $this->server_error(_('Notice without matching profile'));
+                       return;
+               }
                # XXX: RDFa
                common_element_start('li', array('class' => 'notice_single',
                                                                                  'id' => 'notice-' . $notice->id));
@@ -92,12 +114,12 @@ class NoticesearchAction extends SearchAction {
                # FIXME: URL, image, video, audio
                common_element_start('p', array('class' => 'content'));
                if ($notice->rendered) {
-                       common_raw($this->highlight($notice->rendered));
+                       common_raw($this->highlight($notice->rendered, $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)
-                       common_raw($this->highlight(common_render_content($notice->content, $notice)));
+                       common_raw($this->highlight(common_render_content($notice->content, $notice), $terms));
                }
                common_element_end('p');
                $noticeurl = common_local_url('shownotice', array('notice' => $notice->id));
@@ -111,14 +133,14 @@ class NoticesearchAction extends SearchAction {
                        common_text(' (');
                        common_element('a', array('class' => 'inreplyto',
                                                                          'href' => $replyurl),
-                                                  _t('in reply to...'));
+                                                  _('in reply to...'));
                        common_text(')');
                }
-               common_element_start('a', 
+               common_element_start('a',
                                                         array('href' => common_local_url('newnotice',
                                                                                                                          array('replyto' => $profile->nickname)),
                                                                   'onclick' => 'doreply("'.$profile->nickname.'"); return false',
-                                                                  'title' => _t('reply'),
+                                                                  'title' => _('reply'),
                                                                   'class' => 'replybutton'));
                common_raw('&rarr;');
                common_element_end('a');
@@ -127,8 +149,15 @@ class NoticesearchAction extends SearchAction {
        }
 
        function highlight($text, $terms) {
+               /* Highligh serach terms */
                $pattern = '/('.implode('|',array_map('htmlspecialchars', $terms)).')/i';
                $result = preg_replace($pattern, '<strong>\\1</strong>', $text);
+
+               /* Remove highlighting from inside links, loop incase multiple highlights in links */
+               $pattern = '/(href="[^"]*)<strong>('.implode('|',array_map('htmlspecialchars', $terms)).')<\/strong>([^"]*")/iU';
+               do {
+                       $result = preg_replace($pattern, '\\1\\2\\3', $result, -1, $count);
+               } while ($count);
                return $result;
        }
 }