Skip to content

Commit

Permalink
Re-enable popular searches
Browse files Browse the repository at this point in the history
- Explicit number of IP addresses required.
- Reduce number of items listed to 5.
- Easier to add exclusions.
- Support for limiting by section.
  • Loading branch information
ajparsons committed Oct 1, 2024
1 parent 81b00a3 commit 25edaff
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion classes/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,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;
}
Expand Down
2 changes: 1 addition & 1 deletion classes/Homepage.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,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();
Expand Down
53 changes: 45 additions & 8 deletions www/includes/easyparliament/searchlog.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,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 = [];
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?
Expand All @@ -100,7 +133,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(['s' => $query, 'pop' => 1]);
$url = $this->SEARCHURL->generate();
Expand All @@ -119,6 +152,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;
Expand Down

0 comments on commit 25edaff

Please sign in to comment.