From 26fc3bc08c82295f2c0326c20011397cf9615b66 Mon Sep 17 00:00:00 2001 From: Alex Parsons Date: Mon, 23 Sep 2024 19:17:14 +0000 Subject: [PATCH] Re-enable popular searches - Explicit number of IP addresses required. - Reduce number of items listed to 5. - Easier to add exclusions. - Support for limiting by section. --- classes/Common.php | 2 +- classes/Homepage.php | 2 +- www/includes/easyparliament/searchlog.php | 53 +++++++++++++++++++---- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/classes/Common.php b/classes/Common.php index 8086a3c554..529ce95959 100644 --- a/classes/Common.php +++ b/classes/Common.php @@ -6,7 +6,7 @@ class Common { public function getPopularSearches() { global $SEARCHLOG; - $popular_searches = $SEARCHLOG->popular_recent(10, 2000); + $popular_searches = $SEARCHLOG->popular_recent(5, 2000, 5); return $popular_searches; } diff --git a/classes/Homepage.php b/classes/Homepage.php index 13aeda313b..80f24d54e5 100644 --- a/classes/Homepage.php +++ b/classes/Homepage.php @@ -41,7 +41,7 @@ public function display() { $data["commons_dissolved"] = isset($dissolution[1]); $data['regional'] = $this->getRegionalList(); - $data['popular_searches'] = []; #$common->getPopularSearches(); + $data['popular_searches'] = $common->getPopularSearches(); $data['urls'] = $this->getURLs(); $data['calendar'] = $this->getCalendarData(); $data['featured'] = $this->getEditorialContent(); diff --git a/www/includes/easyparliament/searchlog.php b/www/includes/easyparliament/searchlog.php index d3dbe568a7..6db8e914c1 100644 --- a/www/includes/easyparliament/searchlog.php +++ b/www/includes/easyparliament/searchlog.php @@ -69,17 +69,50 @@ public function add($searchlogdata) { } // Select popular queries - public function popular_recent($count, $max_chars = null) { + public function popular_recent(int $count, ?int $max_chars = null, ?int $minimum_hits = 5,?string $section_string = null) { + + // Banned terms + $banned_terms = ["twat"]; + + $query = "SELECT query_string, COUNT(DISTINCT ip_address) AS c FROM search_query_log + WHERE count_hits != 0 AND page_number=1 + AND query_time > date_sub(NOW(), INTERVAL 3 DAY)"; + + // allow limiting by section (e.g. to show a Scotland specific search) + if (!empty($section_string)) { + $query .= " AND query_string LIKE :section_string"; + } + + $query .= " GROUP BY query_string HAVING c >= :minimum_hits + ORDER BY c desc LIMIT :count;"; + + $params = [ + ':minimum_hits' => $minimum_hits, + ':count' => $count + ]; + + if (!empty($section_string)) { + $params[':section_string'] = '%' . $section_string . '%'; + } + + $q = $this->db->query($query, $params); - $q = $this->db->query("SELECT query_string, count(*) AS c FROM search_query_log - WHERE count_hits != 0 AND query_string != 'twat' - AND query_string != 'suffragettes' AND page_number=1 - AND query_time > date_sub(NOW(), INTERVAL 1 DAY) - GROUP BY query_string ORDER BY c desc LIMIT $count;"); $popular_searches = array(); foreach ($q as $row) { - array_push($popular_searches, $this->_db_row_to_array($row)); + $row_array = $this->_db_row_to_array($row, $section_string); + + // exclude all queries where a banned term is part of the query + $banned = false; + foreach ($banned_terms as $term) { + if (stripos($row_array['query'], $term) !== false) { + $banned = true; + break; + } + } + if (!$banned) { + array_push($popular_searches, $row_array); + } } //maximum number of chars? @@ -101,7 +134,7 @@ public function popular_recent($count, $max_chars = null) { return $popular_searches; } - public function _db_row_to_array($row) { + public function _db_row_to_array($row, $section_string = null) { $query = $row['query_string']; $this->SEARCHURL->insert(array('s'=>$query, 'pop'=>1)); $url = $this->SEARCHURL->generate(); @@ -120,6 +153,10 @@ public function _db_row_to_array($row) { } $visible_name = preg_replace('/"/', '', $query); + if (isset($section_string)) { + $visible_name = preg_replace('/' . $section_string . '/', '', $visible_name); + } + $rowarray = $row; $rowarray['query'] = $query; $rowarray['visible_name'] = $visible_name;