search.php: (untested) refactor, using SearchService
This commit is contained in:
parent
7c85de7eb7
commit
919326dce4
1 changed files with 45 additions and 143 deletions
188
search.php
188
search.php
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
use Vichan\Data\SearchQueries;
|
||||
use Vichan\Service\SearchService;
|
||||
|
||||
require 'inc/bootstrap.php';
|
||||
|
||||
|
@ -8,158 +8,60 @@ if (!$config['search']['enable']) {
|
|||
die(_("Post search is disabled"));
|
||||
}
|
||||
|
||||
if (isset($config['search']['boards'])) {
|
||||
$boards = $config['search']['boards'];
|
||||
} else {
|
||||
$boards = listBoards(TRUE);
|
||||
}
|
||||
$ctx = Vichan\build_context($config);
|
||||
$search_service = $ctx->get(SearchService::class);
|
||||
|
||||
if (isset($_GET['search']) && !empty($_GET['search'])) {
|
||||
$raw_search = $_GET['search'];
|
||||
$ip = $_SERVER['REMOTE_ADDR'];
|
||||
$fallback_board = (isset($_GET['board']) && !empty($_GET['board'])) ? $_GET['board'] : null;
|
||||
|
||||
|
||||
if ($search_service->checkFlood($ip, $raw_search)) {
|
||||
error(_('Wait a while before searching again, please.'));
|
||||
}
|
||||
|
||||
// Actually do the search.
|
||||
$parse_res = $search_service->parse($raw_search);
|
||||
$filters = $search_service->reduceAndWeight($parse_res);
|
||||
$search_res = $search_service->search($ip, $raw_search, $filters, $fallback_board);
|
||||
|
||||
|
||||
// Needed to set a global variable further down the stack, plus the template.
|
||||
$actual_board = $filter->board ?? $fallback_board;
|
||||
|
||||
if (isset($_GET['search']) && !empty($_GET['search']) && isset($_GET['board']) && in_array($_GET['board'], $boards)) {
|
||||
$body = Element('search_form.html', [
|
||||
'boards' => $boards,
|
||||
'boards' => $search_service->getSearchableBoards(),
|
||||
'board' => $_GET['board'],
|
||||
'search' => \str_replace('"', '"', utf8tohtml($_GET['search']))
|
||||
]);
|
||||
|
||||
$phrase = $_GET['search'];
|
||||
$ip = $_SERVER['REMOTE_ADDR'];
|
||||
$_body = '';
|
||||
|
||||
$ctx = Vichan\build_context($config);
|
||||
$search_queries = $ctx->get(SearchQueries::class);
|
||||
|
||||
if ($search_queries->checkFlood($ip, $phrase)) {
|
||||
error(_('Wait a while before searching again, please.'));
|
||||
}
|
||||
|
||||
_syslog(LOG_NOTICE, 'Searched /' . $_GET['board'] . '/ for "' . $phrase . '"');
|
||||
|
||||
openBoard($_GET['board']);
|
||||
|
||||
$filters = Array();
|
||||
|
||||
function search_filters($m) {
|
||||
global $filters;
|
||||
$name = $m[2];
|
||||
$value = isset($m[4]) ? $m[4] : $m[3];
|
||||
|
||||
if (!in_array($name, array('id', 'thread', 'subject', 'name'))) {
|
||||
// unknown filter
|
||||
return $m[0];
|
||||
}
|
||||
|
||||
$filters[$name] = $value;
|
||||
|
||||
return $m[1];
|
||||
}
|
||||
|
||||
$phrase = trim(preg_replace_callback('/(^|\s)(\w+):("(.*)?"|[^\s]*)/', 'search_filters', $phrase));
|
||||
|
||||
if (!preg_match('/[^*^\s]/', $phrase) && empty($filters)) {
|
||||
_syslog(LOG_WARNING, 'Query too broad.');
|
||||
$body .= '<p class="unimportant" style="text-align:center">(Query too broad.)</p>';
|
||||
echo Element('page.html', Array(
|
||||
'config'=>$config,
|
||||
'title'=>'Search',
|
||||
'body'=>$body,
|
||||
));
|
||||
exit;
|
||||
}
|
||||
|
||||
// Escape escape character
|
||||
$phrase = str_replace('!', '!!', $phrase);
|
||||
|
||||
// Remove SQL wildcard
|
||||
$phrase = str_replace('%', '!%', $phrase);
|
||||
|
||||
// Use asterisk as wildcard to suit convention
|
||||
$phrase = str_replace('*', '%', $phrase);
|
||||
|
||||
// Remove `, it's used by table prefix magic
|
||||
$phrase = str_replace('`', '!`', $phrase);
|
||||
|
||||
$like = '';
|
||||
$match = Array();
|
||||
|
||||
// Find exact phrases
|
||||
if (preg_match_all('/"(.+?)"/', $phrase, $m)) {
|
||||
foreach($m[1] as &$quote) {
|
||||
$phrase = str_replace("\"{$quote}\"", '', $phrase);
|
||||
$match[] = $pdo->quote($quote);
|
||||
}
|
||||
}
|
||||
|
||||
$words = explode(' ', $phrase);
|
||||
foreach($words as &$word) {
|
||||
if (empty($word)) {
|
||||
continue;
|
||||
}
|
||||
$match[] = $pdo->quote($word);
|
||||
}
|
||||
|
||||
$like = '';
|
||||
foreach($match as &$phrase) {
|
||||
if (!empty($like)) {
|
||||
$like .= ' AND ';
|
||||
}
|
||||
$phrase = preg_replace('/^\'(.+)\'$/', '\'%$1%\'', $phrase);
|
||||
$like .= '`body` LIKE ' . $phrase . ' ESCAPE \'!\'';
|
||||
}
|
||||
|
||||
foreach($filters as $name => $value) {
|
||||
if (!empty($like)) {
|
||||
$like .= ' AND ';
|
||||
}
|
||||
$like .= '`' . $name . '` = '. $pdo->quote($value);
|
||||
}
|
||||
|
||||
$like = str_replace('%', '%%', $like);
|
||||
|
||||
$search_limit = $config['search']['search_limit'];
|
||||
|
||||
$query = prepare(sprintf("SELECT * FROM ``posts_%s`` WHERE " . $like . " ORDER BY `time` DESC LIMIT :limit", $board['uri']));
|
||||
$query->bindValue(':limit', $search_limit, PDO::PARAM_INT);
|
||||
$query->execute() or error(db_error($query));
|
||||
|
||||
if ($query->rowCount() == $search_limit) {
|
||||
_syslog(LOG_WARNING, 'Query too broad.');
|
||||
$body .= '<p class="unimportant" style="text-align:center">('._('Query too broad.').')</p>';
|
||||
echo Element('page.html', Array(
|
||||
'config'=>$config,
|
||||
'title'=>'Search',
|
||||
'body'=>$body,
|
||||
));
|
||||
exit;
|
||||
}
|
||||
|
||||
$temp = '';
|
||||
while ($post = $query->fetch()) {
|
||||
if (!$post['thread']) {
|
||||
$po = new Thread($post);
|
||||
} else {
|
||||
$po = new Post($post);
|
||||
}
|
||||
$temp .= $po->build(true) . '<hr/>';
|
||||
}
|
||||
|
||||
if (!empty($temp))
|
||||
$_body .= '<fieldset><legend>' .
|
||||
sprintf(ngettext('%d result in', '%d results in', $query->rowCount()),
|
||||
$query->rowCount()) . ' <a href="/' .
|
||||
sprintf($config['board_path'], $board['uri']) . $config['file_index'] .
|
||||
'">' .
|
||||
sprintf($config['board_abbreviation'], $board['uri']) . ' - ' . $board['title'] .
|
||||
'</a></legend>' . $temp . '</fieldset>';
|
||||
|
||||
$body .= '<hr/>';
|
||||
if (!empty($_body)) {
|
||||
$body .= $_body;
|
||||
if (empty($search_res)) {
|
||||
$body .= '<hr/><p style="text-align:center" class="unimportant">(' . _('No results.') . ')</p>';
|
||||
} else {
|
||||
$body .= '<p style="text-align:center" class="unimportant">('._('No results.').')</p>';
|
||||
$body .= '<hr/>';
|
||||
|
||||
openBoard($actual_board);
|
||||
|
||||
$posts_html = '';
|
||||
foreach ($search_res as $post) {
|
||||
if (!$post['thread']) {
|
||||
$po = new Thread($post);
|
||||
} else {
|
||||
$po = new Post($post);
|
||||
}
|
||||
$posts_html .= $po->build(true) . '<hr/>';
|
||||
}
|
||||
|
||||
$body .= '<fieldset><legend>' .
|
||||
sprintf(ngettext('%d result in', '%d results in', \count($search_res)), \count($search_res)) . ' <a href="/' .
|
||||
sprintf($config['board_path'], $board['uri']) . $config['file_index'] . '">' .
|
||||
sprintf($config['board_abbreviation'], $board['uri']) . ' - ' . $board['title'] .
|
||||
'</a></legend>' . $posts_html . '</fieldset>';
|
||||
}
|
||||
} else {
|
||||
$body = Element('search_form.html', [
|
||||
'boards' => $boards,
|
||||
'boards' => $search_service->getSearchableBoards(),
|
||||
'board' => false,
|
||||
'search' => false
|
||||
]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue