]> git.wh0rd.org - tt-rss.git/commitdiff
search keyword highlighting (combined mode only)
authorAndrew Dolgov <fox@madoka.volgo-balt.ru>
Wed, 31 Jul 2013 10:53:34 +0000 (14:53 +0400)
committerAndrew Dolgov <fox@madoka.volgo-balt.ru>
Wed, 31 Jul 2013 10:53:34 +0000 (14:53 +0400)
classes/feeds.php
css/tt-rss.css
include/functions.php

index 0849a7a8a915c33a18e332c437c61c375acbb199..874f767887a03421693b2fdc712e3d1de973d5ab 100644 (file)
@@ -255,6 +255,7 @@ class Feeds extends Handler_Protected {
                $last_error = $qfh_ret[3];
                $last_updated = strpos($qfh_ret[4], '1970-') === FALSE ?
                        make_local_datetime($qfh_ret[4], false) : __("Never");
+               $highlight_words = $qfh_ret[5];
 
                $vgroup_last_feed = $vgr_last_feed;
 
@@ -509,7 +510,7 @@ class Feeds extends Handler_Protected {
                                                $tags = false;
 
                                        $line["content"] = sanitize($line["content"],
-                                                       sql_bool_to_bool($line['hide_images']), false, $entry_site_url);
+                                                       sql_bool_to_bool($line['hide_images']), false, $entry_site_url, $highlight_words);
 
                                        foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_RENDER_ARTICLE_CDM) as $p) {
                                                $line = $p->hook_render_article_cdm($line);
index c79e28ad5f7a8ec8dbb9a2012b932f78b0cbbd30..e7514956fff305c740e33a50bfe1557ec9a9f651 100644 (file)
@@ -1168,3 +1168,8 @@ body#ttrssPrefs hr {
        position : relative;
        top : -2px;
 }
+
+span.highlight {
+       background-color : #ffff00;
+       color : #cc90cc;
+}
index deeb53bf10262dc3bd654c16dac66eb07bccfe71..6ceb20adc470993b1a4c858b1aff98dff7d5ae45 100644 (file)
 
                $keywords = explode(" ", $search);
                $query_keywords = array();
+               $search_words = array();
 
                foreach ($keywords as $k) {
                        if (strpos($k, "-") === 0) {
                                } else {
                                        array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
                                                        OR UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
+                                       array_push($search_words, $k);
                                }
                                break;
                        case "author":
                                } else {
                                        array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
                                                        OR UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
+                                       array_push($search_words, $k);
                                }
                                break;
                        case "note":
                                } else {
                                        array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
                                                        OR UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
+                                       if (!$not) array_push($search_words, $k);
                                }
                                break;
                        case "star":
                                } else {
                                        array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
                                                        OR UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
+                                       if (!$not) array_push($search_words, $k);
                                }
                                break;
                        case "pub":
                                } else {
                                        array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
                                                        OR UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
+                                       if (!$not) array_push($search_words, $k);
                                }
                                break;
                        default:
                                } else {
                                        array_push($query_keywords, "(UPPER(ttrss_entries.title) $not LIKE UPPER('%$k%')
                                                        OR UPPER(ttrss_entries.content) $not LIKE UPPER('%$k%'))");
+
+                                       if (!$not) array_push($search_words, $k);
                                }
                        }
                }
 
                $search_query_part = implode("AND", $query_keywords);
 
-               return $search_query_part;
+               return array($search_query_part, $search_words);
        }
 
        function getParentCategories($cat, $owner_uid) {
                if (!$owner_uid) $owner_uid = $_SESSION["uid"];
 
                $ext_tables_part = "";
+               $search_words = array();
 
                        if ($search) {
 
                                                $search_query_part = "ref_id = -1 AND ";
 
                                } else {
-                                       $search_query_part = search_to_sql($search);
+                                       list($search_query_part, $search_words) = search_to_sql($search);
                                        $search_query_part .= " AND ";
                                }
 
                                $result = db_query($select_qpart . $from_qpart . $where_qpart);
                        }
 
-                       return array($result, $feed_title, $feed_site_url, $last_error, $last_updated);
+                       return array($result, $feed_title, $feed_site_url, $last_error, $last_updated, $search_words);
 
        }
 
-       function sanitize($str, $force_remove_images = false, $owner = false, $site_url = false) {
+       function sanitize($str, $force_remove_images = false, $owner = false, $site_url = false, $highlight_words = false) {
                if (!$owner) $owner = $_SESSION["uid"];
 
                $res = trim($str); if (!$res) return '';
 
                $doc->removeChild($doc->firstChild); //remove doctype
                $doc = strip_harmful_tags($doc, $allowed_elements, $disallowed_attributes);
+
+               if ($highlight_words) {
+                       foreach ($highlight_words as $word) {
+
+                               $elements = $xpath->query('//*[contains(.,"'.$word.'")]');
+
+                               foreach ($elements as $element) {
+                                       foreach ($element->childNodes as $child) {
+
+                                               if (!$child instanceof DomText) continue;
+
+                                               $fragment = $doc->createDocumentFragment();
+                                     $text = $child->textContent;
+                                               $stubs = array();
+
+                                               while (($pos = stripos($text, $word)) !== false) {
+                                                       $fragment->appendChild(new DomText(substr($text, 0, $pos)));
+                                                       $word = substr($text, $pos, strlen($word));
+                                                       $highlight = $doc->createElement('span');
+                                                       $highlight->appendChild(new DomText($word));
+                                                       $highlight->setAttribute('class', 'highlight');
+                                                       $fragment->appendChild($highlight);
+                                                       $text = substr($text, $pos + strlen($word));
+                                               }
+
+                                               if (!empty($text)) $fragment->appendChild(new DomText($text));
+
+                                               $element->replaceChild($fragment, $child);
+                                       }
+                               }
+                       }
+               }
+
                $res = $doc->saveHTML();
+
                return $res;
        }