Browse Source

fix extended syntax request processing

main
ghost 1 year ago
parent
commit
df29cd4e4c
  1. 40
      src/library/sphinx.php

40
src/library/sphinx.php

@ -4,28 +4,28 @@ class Sphinx {
private $_sphinx; private $_sphinx;
public function __construct(string $host, int $port) { public function __construct(string $host, int $port)
{
$this->_sphinx = new PDO('mysql:host=' . $host . ';port=' . $port . ';charset=utf8', false, false, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']); $this->_sphinx = new PDO('mysql:host=' . $host . ';port=' . $port . ';charset=utf8', false, false, [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8']);
$this->_sphinx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $this->_sphinx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->_sphinx->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ); $this->_sphinx->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
} }
public function searchMagnetsTotal(string $keyword) { public function searchMagnetsTotal(string $keyword) : int
{
$query = $this->_sphinx->prepare('SELECT COUNT(*) AS `total` FROM `magnet` WHERE MATCH(?)'); $query = $this->_sphinx->prepare('SELECT COUNT(*) AS `total` FROM `magnet` WHERE MATCH(?)');
$query->execute( $query->execute(
[ [
$keyword self::_match($keyword)
] ]
); );
return $query->fetch()->total; 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)
{
$query = $this->_sphinx->prepare("SELECT * $query = $this->_sphinx->prepare("SELECT *
FROM `magnet` FROM `magnet`
@ -40,10 +40,34 @@ class Sphinx {
$query->execute( $query->execute(
[ [
$keyword self::_match($keyword)
] ]
); );
return $query->fetchAll(); return $query->fetchAll();
} }
private static function _match(string $keyword) : string
{
$keyword = trim($keyword);
if (empty($keyword))
{
return $keyword;
}
$keyword = str_replace(['"'], ' ', $keyword);
$keyword = preg_replace('/[\W]/ui', ' ', $keyword);
$keyword = preg_replace('/[\s]+/ui', ' ', $keyword);
$keyword = trim($keyword);
$result = [];
foreach ((array) explode(' ', $keyword) as $value)
{
$result[] = sprintf('@"*%s*"', $value);
}
return implode(' | ', $result);
}
} }

Loading…
Cancel
Save