From 2c62c940835360bb63eae117af78d528b5b48f52 Mon Sep 17 00:00:00 2001 From: ghost Date: Tue, 29 Aug 2023 14:47:03 +0300 Subject: [PATCH] implement similar magnets feature --- src/library/sphinx.php | 55 +++++++++++++++++++++++++++++++++--------- src/public/magnet.php | 44 +++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 11 deletions(-) diff --git a/src/library/sphinx.php b/src/library/sphinx.php index 84ea6d9..e36a8ef 100644 --- a/src/library/sphinx.php +++ b/src/library/sphinx.php @@ -11,20 +11,20 @@ class Sphinx { $this->_sphinx->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); } - public function searchMagnetsTotal(string $keyword) : int + public function searchMagnetsTotal(string $keyword, string $mode = 'default') : int { $query = $this->_sphinx->prepare('SELECT COUNT(*) AS `total` FROM `magnet` WHERE MATCH(?)'); $query->execute( [ - self::_match($keyword) + self::_match($keyword, $mode) ] ); return $query->fetch()->total; } - public function searchMagnets(string $keyword, int $start, int $limit, int $maxMatches) + public function searchMagnets(string $keyword, int $start, int $limit, int $maxMatches, string $mode = 'default') { $query = $this->_sphinx->prepare("SELECT * @@ -40,14 +40,14 @@ class Sphinx { $query->execute( [ - self::_match($keyword) + self::_match($keyword, $mode) ] ); return $query->fetchAll(); } - private static function _match(string $keyword) : string + private static function _match(string $keyword, string $mode = 'default') : string { $keyword = trim($keyword); @@ -61,13 +61,46 @@ class Sphinx { $keyword = preg_replace('/[\s]+/ui', ' ', $keyword); $keyword = trim($keyword); - $result = []; - - foreach ((array) explode(' ', $keyword) as $value) + switch ($mode) { - $result[] = sprintf('@"*%s*"', $value); - } + case 'similar': + + $result = []; + + foreach ((array) explode(' ', $keyword) as $i => $value) + { + if (mb_strlen($value) > 5) + { + $result[] = sprintf('@metaTitle "%s" | @dn "%s"', $value, $value); + + if ($i > 3) + { + break; + } + } + } + + if (empty($result)) + { + return '*'; + } + else + { + return implode(' | ', $result); + } - return implode(' | ', $result); + break; + + default: + + $result = []; + + foreach ((array) explode(' ', $keyword) as $value) + { + $result[] = sprintf('@"*%s*"', $value); + } + + return implode(' | ', $result); + } } } diff --git a/src/public/magnet.php b/src/public/magnet.php index 3a8936e..9b0eb42 100644 --- a/src/public/magnet.php +++ b/src/public/magnet.php @@ -2,10 +2,23 @@ // Load dependencies require_once (__DIR__ . '/../config/app.php'); +require_once (__DIR__ . '/../library/sphinx.php'); require_once (__DIR__ . '/../library/database.php'); require_once (__DIR__ . '/../library/time.php'); require_once (__DIR__ . '/../../vendor/autoload.php'); +// Connect Sphinx +try { + + $sphinx = new Sphinx(SPHINX_HOST, SPHINX_PORT); + +} catch(Exception $e) { + + var_dump($e); + + exit; +} + // Connect database try { @@ -358,6 +371,37 @@ echo '' . PHP_EOL ?> + searchMagnetsTotal($magnet->metaTitle ? $magnet->metaTitle : $magnet->dn, 'similar')) { ?> + 1) { // skip current magnet ?> +
+ +

+
+
+
+ searchMagnets( + $magnet->metaTitle ? $magnet->metaTitle : $magnet->dn, + 0, + 10, + $similarMagnetsTotal, + 'similar' + ) as $result) { ?> + getMagnet($result->magnetid)) { ?> + magnetid != $response->magnet->magnetId && // skip current magnet + ($response->user->address == $db->getUser($magnet->userId)->address || + in_array($response->user->address, MODERATOR_IP_LIST) || ($magnet->approved && $magnet->public))) { ?> + + + + +
+
+ + magnet->comments) { ?>