summaryrefslogtreecommitdiff
path: root/inc/mod
diff options
context:
space:
mode:
authorMichael Foster <savetheinternet@tinyboard.org>2013-07-29 16:18:06 -0400
committerMichael Foster <savetheinternet@tinyboard.org>2013-07-29 16:18:06 -0400
commit58306bdc63b7dcaf964ab3541fe4b5a8e0c813b0 (patch)
tree00b9f59d0de0ef3ef2165345e94d83ed5269fd88 /inc/mod
parentc0e0a5db7d566aed16d5d9d9036a1f46f658e9eb (diff)
Search posts
Diffstat (limited to 'inc/mod')
-rw-r--r--inc/mod/pages.php41
1 files changed, 36 insertions, 5 deletions
diff --git a/inc/mod/pages.php b/inc/mod/pages.php
index 01794d2e..8289d0b0 100644
--- a/inc/mod/pages.php
+++ b/inc/mod/pages.php
@@ -225,7 +225,7 @@ function mod_search($type, $search_query_escaped, $page_no = 1) {
// Which `field` to search?
if ($type == 'posts')
- $sql_field = 'body';
+ $sql_field = 'body_nomarkup';
if ($type == 'IP_notes')
$sql_field = 'body';
if ($type == 'bans')
@@ -246,7 +246,27 @@ function mod_search($type, $search_query_escaped, $page_no = 1) {
// Compile SQL query
if ($type == 'posts') {
- error('Searching posts is under development. Sorry.');
+ $query = '';
+
+ $boards = listBoards();
+ if (empty($boards))
+ error(_('There are no boards to search!'));
+
+ foreach ($boards as $board) {
+ openBoard($board['uri']);
+ if (!hasPermission($config['mod']['search_posts'], $board['uri']))
+ continue;
+
+ if (!empty($query))
+ $query .= ' UNION ALL ';
+ $query .= sprintf("SELECT *, '%s' AS `board` FROM `posts_%s` WHERE %s", $board['uri'], $board['uri'], $sql_like);
+ }
+
+ // You weren't allowed to search any boards
+ if (empty($query))
+ error($config['error']['noaccess']);
+
+ $query .= ' ORDER BY `sticky` DESC, `id` DESC';
}
if ($type == 'IP_notes') {
@@ -273,10 +293,15 @@ function mod_search($type, $search_query_escaped, $page_no = 1) {
// Execute SQL query (with pages)
$q = query($query . ' LIMIT ' . (($page_no - 1) * $config['mod']['search_page']) . ', ' . $config['mod']['search_page']) or error(db_error());
$results = $q->fetchAll(PDO::FETCH_ASSOC);
-
+
// Get total result count
- $q = query('SELECT COUNT(*) FROM `' . $sql_table . '` WHERE ' . $sql_like) or error(db_error());
- $result_count = $q->fetchColumn();
+ if ($type == 'posts') {
+ $q = query("SELECT COUNT(*) FROM ($query) AS `tmp_table`") or error(db_error());
+ $result_count = $q->fetchColumn();
+ } else {
+ $q = query('SELECT COUNT(*) FROM `' . $sql_table . '` WHERE ' . $sql_like) or error(db_error());
+ $result_count = $q->fetchColumn();
+ }
if ($type == 'bans') {
foreach ($results as &$ban) {
@@ -285,6 +310,12 @@ function mod_search($type, $search_query_escaped, $page_no = 1) {
}
}
+ if ($type == 'posts') {
+ foreach ($results as &$post) {
+ $post['snippet'] = pm_snippet($post['body']);
+ }
+ }
+
// $results now contains the search results
mod_page(_('Search results'), 'mod/search_results.html', array(