diff --git a/api/search/index.php b/api/search/index.php index 8b977f08812c6260f5d908f24c45eec0199cdef2..bd996519f91de6c9a74579f79cd981693d995080 100644 --- a/api/search/index.php +++ b/api/search/index.php @@ -13,7 +13,7 @@ $backend = Backend::createFromConfig($config); try { $backend->authenticateFromHeader($_SERVER['HTTP_AUTHORIZATION'] ?: ""); - $renderer->renderJson($backend->findBuffers($_GET['q'] ?: "")); + $renderer->renderJson($backend->findBuffers($_GET['q'] ?: "", $_GET['since'] ?: null, $_GET['before'] ?: null, $_GET['buffer'] ?: null, $_GET['network'] ?: null)); } catch (\Exception $e) { $renderer->renderJson(["error" => $e->getMessage()]); } \ No newline at end of file diff --git a/api/searchbuffer/index.php b/api/searchbuffer/index.php index af96e4b0d76db69d3c32d2805ee76a03c85657f2..6fefe5e5c483fbc322882f756d2005ccebd156e5 100644 --- a/api/searchbuffer/index.php +++ b/api/searchbuffer/index.php @@ -13,7 +13,7 @@ $backend = Backend::createFromConfig($config); try { $backend->authenticateFromHeader($_SERVER['HTTP_AUTHORIZATION'] ?: ""); - $renderer->renderJson($backend->findInBuffer($_GET['q'] ?: "", $_GET['buffer'] ?: 0, $_GET['offset'] ?: 0, $_GET['limit'] ?: 0)); + $renderer->renderJson($backend->findInBuffer($_GET['query'] ?: "", $_GET['since'] ?: null, $_GET['before'] ?: null, $_GET['buffer'] ?: 0, $_GET['offset'] ?: 0, $_GET['limit'] ?: 20)); } catch (\Exception $e) { $renderer->renderJson(["error" => $e->getMessage()]); } \ No newline at end of file diff --git a/api/searchbuffers/index.php b/api/searchbuffers/index.php index ed64ceb78edc23f4a7ad988704927f8a354b97fe..ef0727a709c15fcf90fbec5158e427aeb14a95a8 100644 --- a/api/searchbuffers/index.php +++ b/api/searchbuffers/index.php @@ -13,7 +13,7 @@ $backend = Backend::createFromConfig($config); try { $backend->authenticateFromHeader($_SERVER['HTTP_AUTHORIZATION'] ?: ""); - $renderer->renderJson($backend->findInBufferMultiple($_GET['q'] ?: "", $_GET['limit'] ?: 4)); + $renderer->renderJson($backend->findInBufferMultiple($_GET['q'] ?: "", $_GET['since'] ?: null, $_GET['before'] ?: null, $_GET['limit'] ?: 4)); } catch (\Exception $e) { $renderer->renderJson(["error" => $e->getMessage()]); } \ No newline at end of file diff --git a/backend/Database.php b/backend/Database.php index aed4c6032d17a38fc394c04cb820f9e0bbb56675..34ca513a7a8339c559d998fabf380b7ab8e1d98b 100644 --- a/backend/Database.php +++ b/backend/Database.php @@ -2,6 +2,8 @@ namespace QuasselRestSearch; +use PDO; + require_once 'User.php'; require_once 'Config.php'; require_once 'helper/AuthHelper.php'; @@ -29,6 +31,10 @@ class Backend { plainto_tsquery('english'::REGCONFIG, :query) query WHERE (backlog.type & 23559) > 0 AND buffer.userid = :userid + AND (NOT(:_since) OR backlog.time > to_timestamp(:since)) + AND (NOT(:_before) OR backlog.time < to_timestamp(:before)) + AND (NOT(:_network) OR network.networkname ILIKE :network) + AND (NOT(:_buffer) OR buffer.buffername ILIKE :buffer) AND backlog.tsv @@ query GROUP BY backlog.bufferid, buffer.buffername, @@ -56,6 +62,8 @@ class Backend { JOIN buffer ON backlog.bufferid = buffer.bufferid, plainto_tsquery('english'::REGCONFIG, :query) query WHERE (backlog.type & 23559) > 0 + AND (NOT(:_since) OR backlog.time > to_timestamp(:since)) + AND (NOT(:_before) OR backlog.time < to_timestamp(:before)) AND buffer.userid = :userid AND backlog.tsv @@ query ORDER BY (1 + log(GREATEST(1, EXTRACT(EPOCH FROM (CURRENT_TIMESTAMP - TIME))))) * (1 - ts_rank(tsv, query, 32)) * (1 + ln(backlog.type)) ASC @@ -74,6 +82,8 @@ class Backend { JOIN buffer ON backlog.bufferid = buffer.bufferid, plainto_tsquery('english'::REGCONFIG, :query) query WHERE (backlog.type & 23559) > 0 + AND (NOT(:_since) OR backlog.time > to_timestamp(:since)) + AND (NOT(:_before) OR backlog.time < to_timestamp(:before)) AND buffer.userid = :userid AND backlog.bufferid = :bufferid AND backlog.tsv @@ query @@ -162,11 +172,11 @@ class Backend { return true; } - public function find(string $query, int $limitPerBuffer = 4) : array { + public function find(string $query, int $since = null, int $before = null, string $buffer = null, string $network = null, int $limitPerBuffer = 4) : array { $truncatedLimit = max(min($limitPerBuffer, 10), 0); - $buffers = $this->findBuffers($query); - $messages = $this->findInBufferMultiple($query, $truncatedLimit); + $buffers = $this->findBuffers($query, $since, $before, $buffer, $network); + $messages = $this->findInBufferMultiple($query, $since, $before, $truncatedLimit); $buffermap = []; foreach ($buffers as &$buffer) { @@ -175,34 +185,66 @@ class Backend { } foreach ($messages as $message) { - array_push($buffermap[$message['bufferid']]['messages'], $message); + if (!is_null($buffermap[$message['bufferid']])) + array_push($buffermap[$message['bufferid']]['messages'], $message); } return array_values($buffermap); } - public function findBuffers(string $query) : array { + public function findBuffers(string $query, int $since = null, int $before = null, string $buffer = null, string $network = null) : array { + $_since = $since!==null; + $_before = $before!==null; + $_buffer = $buffer!==null; + $_network = $network!==null; + $this->storedFindBuffers->bindParam(':userid', $this->user->userid); $this->storedFindBuffers->bindParam(':query', $query); + + $this->storedFindBuffers->bindValue(':since', $_since ? (int) $since : 0, PDO::PARAM_INT); + $this->storedFindBuffers->bindValue(':before', $_before ? (int) $before : 0, PDO::PARAM_INT); + $this->storedFindBuffers->bindValue(':buffer', $_buffer ? (string) $buffer : ""); + $this->storedFindBuffers->bindValue(':network', $_network ? (string) $network : ""); + $this->storedFindBuffers->bindParam(':_since', $_since, PDO::PARAM_INT); + $this->storedFindBuffers->bindParam(':_before', $_before, PDO::PARAM_INT); + $this->storedFindBuffers->bindParam(':_buffer', $_buffer, PDO::PARAM_INT); + $this->storedFindBuffers->bindParam(':_network', $_network, PDO::PARAM_INT); + $this->storedFindBuffers->execute(); return $this->storedFindBuffers->fetchAll(\PDO::FETCH_ASSOC); } - public function findInBufferMultiple(string $query, int $limit = 4) : array { + public function findInBufferMultiple(string $query, int $since = null, int $before = null, int $limit = 4) : array { + $_since = $since!==null; + $_before = $before!==null; + $this->storedFindInBufferMultiple->bindParam(':userid', $this->user->userid); $this->storedFindInBufferMultiple->bindParam(':query', $query); $this->storedFindInBufferMultiple->bindParam(':limit', $limit); + + $this->storedFindInBufferMultiple->bindValue(':since', $_since ? (int) $since : 0, PDO::PARAM_INT); + $this->storedFindInBufferMultiple->bindValue(':before', $_before ? (int) $before : 0, PDO::PARAM_INT); + $this->storedFindInBufferMultiple->bindParam(':_since', $_since, PDO::PARAM_INT); + $this->storedFindInBufferMultiple->bindParam(':_before', $_before, PDO::PARAM_INT); + $this->storedFindInBufferMultiple->execute(); return $this->storedFindInBufferMultiple->fetchAll(\PDO::FETCH_ASSOC); } - public function findInBuffer(string $query, int $bufferid, int $offset = 0, int $limit = 20) : array { + public function findInBuffer(string $query, int $since = null, int $before = null, int $bufferid, int $offset = 0, int $limit = 20) : array { $truncatedLimit = max(min($limit, 50), 0); + $_since = $since!==null; + $_before = $before!==null; $this->storedFindInBuffer->bindParam(':userid', $this->user->userid); $this->storedFindInBuffer->bindParam(':bufferid', $bufferid); $this->storedFindInBuffer->bindParam(':query', $query); + $this->storedFindInBuffer->bindValue(':since', $_since ? (int) $since : 0, PDO::PARAM_INT); + $this->storedFindInBuffer->bindValue(':before', $_before ? (int) $before : 0, PDO::PARAM_INT); + $this->storedFindInBuffer->bindParam(':_since', $_since, PDO::PARAM_INT); + $this->storedFindInBuffer->bindParam(':_before', $_before, PDO::PARAM_INT); + $this->storedFindInBuffer->bindParam(':limit', $truncatedLimit); $this->storedFindInBuffer->bindParam(':offset', $offset); @@ -220,7 +262,9 @@ class Backend { $this->loadBefore->bindParam(":userid", $this->user->userid); $this->loadBefore->bindParam(":bufferid", $buffer); $this->loadBefore->bindParam(":anchor", $anchor); + $this->loadBefore->bindParam(":limit", $truncatedLimit); + $this->loadBefore->execute(); return $this->loadBefore->fetchAll(\PDO::FETCH_ASSOC); } @@ -231,7 +275,9 @@ class Backend { $this->loadAfter->bindParam(":userid", $this->user->userid); $this->loadAfter->bindParam(":bufferid", $buffer); $this->loadAfter->bindParam(":anchor", $anchor); + $this->loadAfter->bindParam(":limit", $truncatedLimit); + $this->loadAfter->execute(); return $this->loadAfter->fetchAll(\PDO::FETCH_ASSOC); } diff --git a/web/search/index.php b/web/search/index.php index 8b4d8406a718022dfd6e9ab20456cd808ae24e8f..6e8f950e1613d66082e6aa6720034748a3d16fd4 100644 --- a/web/search/index.php +++ b/web/search/index.php @@ -16,5 +16,5 @@ if (!$backend->authenticate($session->username ?: '', $session->password ?: '')) $session->destroy(); $renderer->renderJsonError(false); } else { - $renderer->renderJson($backend->find($_GET['query'] ?: "", 4)); + $renderer->renderJson($backend->find($_GET['query'] ?: "", $_GET['since'] ?: null, $_GET['before'] ?: null, $_GET['buffer'] ?: null, $_GET['network'] ?: null, 4)); } \ No newline at end of file diff --git a/web/searchbuffer/index.php b/web/searchbuffer/index.php index 766ccff49b8c8ee524cb3f5e7a725b9e4c57b329..54d2b525082a8b24dc413085cf84af69f8e7aa79 100644 --- a/web/searchbuffer/index.php +++ b/web/searchbuffer/index.php @@ -16,5 +16,5 @@ if (!$backend->authenticate($session->username ?: '', $session->password ?: '')) $session->destroy(); $renderer->renderJsonError(false); } else { - $renderer->renderJson($backend->findInBuffer($_GET['query'] ?: "", $_GET['buffer'] ?: 0, $_GET['offset'] ?: 0, $_GET['limit'] ?: 20)); + $renderer->renderJson($backend->findInBuffer($_GET['query'] ?: "", $_GET['since'] ?: null, $_GET['before'] ?: null, $_GET['buffer'] ?: 0, $_GET['offset'] ?: 0, $_GET['limit'] ?: 20)); } \ No newline at end of file