Unify username and format replay search internals (#1346)

This commit is contained in:
Kirk Scheibelhut 2019-08-12 13:26:48 +02:00 committed by Guangcong Luo
parent 5e2f1be9a4
commit db3d655648
2 changed files with 41 additions and 26 deletions

View File

@ -77,17 +77,50 @@ class Replays {
return preg_replace('/[^a-z0-9]+/','',$name);
}
function search($term, $page = 0, $isPrivate) {
function search($args) {
$page = args["page"] ?? 0;
if (!$this->db) return [];
if ($page > 100) return [];
$limit1 = intval(50*($page-1));
if ($limit1 < 0) $limit1 = 0;
$term = $this->toID($term);
$isPrivate = $isPrivate ? 1 : 0;
// $res = $this->db->prepare("SELECT uploadtime, id, format, p1, p2 FROM ps_replays WHERE private = 0 AND (p1id = ? OR p2id = ?) ORDER BY uploadtime DESC LIMIT ?, 51");
$res = $this->db->prepare("(SELECT uploadtime, id, format, p1, p2, password FROM ps_replays FORCE INDEX (p1) WHERE private = ? AND p1id = ? ORDER BY uploadtime DESC) UNION (SELECT uploadtime, id, format, p1, p2, password FROM ps_replays FORCE INDEX (p2) WHERE private = ? AND p2id = ? ORDER BY uploadtime DESC) ORDER BY uploadtime DESC LIMIT ?, 51;");
$res->execute([$isPrivate, $term, $isPrivate, $term, $limit1]);
$isPrivate = ($args["isPrivate"] ?? null) ? 1 : 0;
$byRating = args["byRating"] ?? null;
if ($args["username"] ?? null) {
$order = $byRating ? "rating" : "uploadtime";
$userid = $this->toId($args["username"]);
if ($args["username2"] ?? null) {
$userid2 = $this->toId($args["username2"]);
if ($args["format"] ?? null) {
$format = $this->toId($args["format"]);
$res = $this->db->prepare("(SELECT uploadtime, id, format, p1, p2, password FROM ps_replays FORCE INDEX (p1) WHERE private = ? AND p1id = ? AND p2id = ? AND format = ? ORDER BY ? DESC) UNION (SELECT uploadtime, id, format, p1, p2, password FROM ps_replays FORCE INDEX (p1) WHERE private = ? AND p1id = ? AND p2id = ? AND format = ? ORDER BY ? DESC) ORDER BY ? DESC LIMIT ?, 51;");
$res->execute([$isPrivate, $userid, $userid2, $format, $order, $isPrivate, $userid2, $userid, $format, $order, $limit1]);
} else {
$res = $this->db->prepare("(SELECT uploadtime, id, format, p1, p2, password FROM ps_replays FORCE INDEX (p1) WHERE private = ? AND p1id = ? AND p2id = ? ORDER BY ? DESC) UNION (SELECT uploadtime, id, format, p1, p2, password FROM ps_replays FORCE INDEX (p1) WHERE private = ? AND p1id = ? AND p2id = ? ORDER BY ? DESC) ORDER BY ? DESC LIMIT ?, 51;");
$res->execute([$isPrivate, $userid, $userid2, $order, $isPrivate, $userid2, $userid, $order, $limit1]);
}
} else {
if ($args["format"] ?? null) {
$res = $this->db->prepare("(SELECT uploadtime, id, format, p1, p2, password FROM ps_replays FORCE INDEX (p1) WHERE private = ? AND p1id = ? AND format = ? ORDER BY ? DESC) UNION (SELECT uploadtime, id, format, p1, p2, password FROM ps_replays FORCE INDEX (p2) WHERE private = ? AND p2id = ? AND format = ? ORDER BY uploadtime DESC) ORDER BY ? DESC LIMIT ?, 51;");
$res->execute([$isPrivate, $userid, $this->toId($args["format"]), $order, $isPrivate, $userid, $format, $order, $limit1]);
} else {
$res = $this->db->prepare("(SELECT uploadtime, id, format, p1, p2, password FROM ps_replays FORCE INDEX (p1) WHERE private = ? AND p1id = ? ORDER BY ? DESC) UNION (SELECT uploadtime, id, format, p1, p2, password FROM ps_replays FORCE INDEX (p2) WHERE private = ? AND p2id = ? ORDER BY uploadtime DESC) ORDER BY ? DESC LIMIT ?, 51;");
$res->execute([$isPrivate, $userid, $order, $isPrivate, $userid, $order, $limit1]);
}
}
return $res->fetchAll();
}
$res = null;
if ($byRating) {
$res = $this->db->prepare("SELECT uploadtime, id, format, p1, p2, rating, password FROM ps_replays FORCE INDEX (top) WHERE private = ? AND formatid = ? ORDER BY rating DESC LIMIT ?, 51");
} else {
$res = $this->db->prepare("SELECT uploadtime, id, format, p1, p2, password FROM ps_replays FORCE INDEX (format) WHERE private = ? AND formatid = ? ORDER BY uploadtime DESC LIMIT ?, 51");
}
$res->execute([$isPrivate, $this->toId($args["format"]), $limit1]);
return $res->fetchAll();
}
@ -122,24 +155,6 @@ class Replays {
return $res->fetchAll();
}
function searchFormat($term, $page = 0, $byRating = false) {
if (!$this->db) return [];
if ($page > 100) return [];
$limit1 = intval(50*($page-1));
if ($limit1 < 0) $limit1 = 0;
$term = $this->toID($term);
$res = null;
if ($byRating) {
$res = $this->db->prepare("SELECT uploadtime, id, format, p1, p2, rating, password FROM ps_replays FORCE INDEX (top) WHERE private = 0 AND formatid = ? ORDER BY rating DESC LIMIT ?, 51");
} else {
$res = $this->db->prepare("SELECT uploadtime, id, format, p1, p2, password FROM ps_replays FORCE INDEX (format) WHERE private = 0 AND formatid = ? ORDER BY uploadtime DESC LIMIT ?, 51");
}
$res->execute([$term, $limit1]);
return $res->fetchAll();
}
function recent() {
if (!$this->db) return [];
$res = $this->db->query("SELECT uploadtime, id, format, p1, p2 FROM ps_replays FORCE INDEX (recent) WHERE private = 0 ORDER BY uploadtime DESC LIMIT 50");

View File

@ -95,12 +95,12 @@ if ($username || $format || $contains) {
// no
} else if ($username) {
if (!$isPrivate || $isPrivateAllowed) {
$replays = $Replays->search($username, $page, $isPrivate);
$replays = $Replays->search(["username" => $username, "isPrivate" => $isPrivate, "page" => $page]);
}
} else if ($contains) {
$replays = $Replays->fullSearch($contains, $page);
} else {
$replays = $Replays->searchFormat($format, $page, $byRating);
$replays = $Replays->search(["format" => $format, "byRating" => $byRating, "page" => $page]);
}
$time = time();