mirror of
https://github.com/YGGverse/YGGstate.git
synced 2025-01-09 22:37:54 +00:00
implement SphinxQL driver
This commit is contained in:
parent
5a4cd845de
commit
f4118d9f3c
65
src/library/sphinxql.php
Normal file
65
src/library/sphinxql.php
Normal file
@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
class SphinxQL {
|
||||
|
||||
private $_sphinx;
|
||||
|
||||
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->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
$this->_sphinx->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
|
||||
}
|
||||
|
||||
public function searchPeersTotal(string $keyword) {
|
||||
|
||||
$query = $this->_sphinx->prepare('SELECT COUNT(*) AS `total` FROM `peer` WHERE MATCH(?)');
|
||||
|
||||
$query->execute(
|
||||
[
|
||||
self::_match($keyword)
|
||||
]
|
||||
);
|
||||
|
||||
return $query->fetch()->total;
|
||||
}
|
||||
|
||||
public function searchPeers(string $keyword, int $start, int $limit, int $maxMatches) {
|
||||
|
||||
$query = $this->_sphinx->prepare("SELECT *
|
||||
|
||||
FROM `peer`
|
||||
|
||||
WHERE MATCH(?)
|
||||
|
||||
ORDER BY WEIGHT() DESC
|
||||
|
||||
LIMIT " . (int) ($start >= $maxMatches ? ($maxMatches > 0 ? $maxMatches - 1 : 0) : $start) . "," . (int) $limit . "
|
||||
|
||||
OPTION `max_matches`=" . (int) ($maxMatches >= 1 ? $maxMatches : 1));
|
||||
|
||||
$query->execute(
|
||||
[
|
||||
self::_match($keyword)
|
||||
]
|
||||
);
|
||||
|
||||
return $query->fetchAll();
|
||||
}
|
||||
|
||||
private static function _match(string $keyword) : string {
|
||||
|
||||
$keyword = preg_replace('/[\:]+/', ':', $keyword);
|
||||
|
||||
return sprintf(
|
||||
'@peerAddress "%s" | @peerKey "%s" | @peerCoordinatePort "%s" | @peerCoordinateRoute "%s" | @peerRemoteScheme "%s" | @peerRemoteHost "%s" | @peerRemotePort "%s"',
|
||||
preg_replace('/[^A-z0-9\:]/', '', $keyword),
|
||||
preg_replace('/[^A-z0-9]/', '', $keyword),
|
||||
preg_replace('/[^\d]/', '', $keyword),
|
||||
preg_replace('/[^\d]/', '', $keyword),
|
||||
preg_replace('/[^A-z]/', '', $keyword),
|
||||
preg_replace('/[^A-z0-9\.\:]/', '', $keyword),
|
||||
preg_replace('/[^\d]/', '', $keyword),
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user