Browse Source

implement similar magnets feature

main
ghost 1 year ago
parent
commit
2c62c94083
  1. 55
      src/library/sphinx.php
  2. 44
      src/public/magnet.php

55
src/library/sphinx.php

@ -11,20 +11,20 @@ class Sphinx {
$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) : int public function searchMagnetsTotal(string $keyword, string $mode = 'default') : 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(
[ [
self::_match($keyword) self::_match($keyword, $mode)
] ]
); );
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, string $mode = 'default')
{ {
$query = $this->_sphinx->prepare("SELECT * $query = $this->_sphinx->prepare("SELECT *
@ -40,14 +40,14 @@ class Sphinx {
$query->execute( $query->execute(
[ [
self::_match($keyword) self::_match($keyword, $mode)
] ]
); );
return $query->fetchAll(); return $query->fetchAll();
} }
private static function _match(string $keyword) : string private static function _match(string $keyword, string $mode = 'default') : string
{ {
$keyword = trim($keyword); $keyword = trim($keyword);
@ -61,13 +61,46 @@ class Sphinx {
$keyword = preg_replace('/[\s]+/ui', ' ', $keyword); $keyword = preg_replace('/[\s]+/ui', ' ', $keyword);
$keyword = trim($keyword); $keyword = trim($keyword);
$result = []; switch ($mode)
foreach ((array) explode(' ', $keyword) as $value)
{ {
$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);
}
} }
} }

44
src/public/magnet.php

@ -2,10 +2,23 @@
// Load dependencies // Load dependencies
require_once (__DIR__ . '/../config/app.php'); require_once (__DIR__ . '/../config/app.php');
require_once (__DIR__ . '/../library/sphinx.php');
require_once (__DIR__ . '/../library/database.php'); require_once (__DIR__ . '/../library/database.php');
require_once (__DIR__ . '/../library/time.php'); require_once (__DIR__ . '/../library/time.php');
require_once (__DIR__ . '/../../vendor/autoload.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 // Connect database
try { try {
@ -358,6 +371,37 @@ echo '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL ?>
</span> </span>
</div> </div>
</div> </div>
<?php if ($similarMagnetsTotal = $sphinx->searchMagnetsTotal($magnet->metaTitle ? $magnet->metaTitle : $magnet->dn, 'similar')) { ?>
<?php if ($similarMagnetsTotal > 1) { // skip current magnet ?>
<div class="padding-y-8 padding-x-16">
<a name="similar"></a>
<h3><?php echo _('Similar') ?></h3>
</div>
<div class="padding-x-16 margin-b-8">
<div class="padding-16 margin-t-8 border-radius-3 background-color-night">
<?php foreach ( $sphinx->searchMagnets(
$magnet->metaTitle ? $magnet->metaTitle : $magnet->dn,
0,
10,
$similarMagnetsTotal,
'similar'
) as $result) { ?>
<?php if ($magnet = $db->getMagnet($result->magnetid)) { ?>
<?php if ($result->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))) { ?>
<div class="margin-y-8">
<a href="<?php echo sprintf('%s/magnet.php?magnetId=%s', WEBSITE_URL, $magnet->magnetId) ?>" class="margin-b-16">
<?php echo nl2br(htmlentities($magnet->metaTitle ? $magnet->metaTitle : $magnet->dn)) ?>
</a>
</div>
<?php } ?>
<?php } ?>
<?php } ?>
</div>
</div>
<?php } ?>
<?php } ?>
<?php if ($response->magnet->comments) { ?> <?php if ($response->magnet->comments) { ?>
<div class="padding-y-8 padding-x-16"> <div class="padding-y-8 padding-x-16">
<a name="comment"></a> <a name="comment"></a>

Loading…
Cancel
Save