dedupe page_number parsing

This commit is contained in:
Shish 2020-03-27 00:23:29 +00:00
parent b5f0bc7621
commit 880a702b42
3 changed files with 23 additions and 31 deletions

View file

@ -511,6 +511,24 @@ function no_escape(string $input): string
return $input;
}
/**
* Given a 1-indexed numeric-ish thing, return a zero-indexed
* number between 0 and $max
*/
function page_number(string $input, ?int $max=null): int
{
if (!is_numeric($input)) {
$pageNumber = 0;
} elseif ($input <= 0) {
$pageNumber = 0;
} elseif (!is_null($max) && $input >= $max) {
$pageNumber = $max - 1;
} else {
$pageNumber = $input - 1;
}
return $pageNumber;
}
function clamp(?int $val, ?int $min=null, ?int $max=null): int
{
if (!is_numeric($val) || (!is_null($min) && $val < $min)) {

View file

@ -249,19 +249,10 @@ class Forum extends Extension
{
global $config, $database;
$threadsPerPage = $config->get_int('forumThreadsPerPage', 15);
$totalPages = ceil($database->get_one("SELECT COUNT(*) FROM forum_threads") / $threadsPerPage);
$totalPages = (int)ceil($database->get_one("SELECT COUNT(*) FROM forum_threads") / $threadsPerPage);
if ($event->count_args() >= 2) {
$pageNumber = $event->get_arg(1);
if (!is_numeric($pageNumber)) {
$pageNumber = 0;
} elseif ($pageNumber <= 0) {
$pageNumber = 0;
} elseif ($pageNumber >= $totalPages) {
$pageNumber = $totalPages - 1;
} else {
$pageNumber--;
}
$pageNumber = page_number($event->get_arg(1), $totalPages);
} else {
$pageNumber = 0;
}
@ -286,20 +277,11 @@ class Forum extends Extension
global $config, $database;
$threadID = int_escape($event->get_arg(1));
$postsPerPage = $config->get_int('forumPostsPerPage', 15);
$totalPages = ceil($database->get_one("SELECT COUNT(*) FROM forum_posts WHERE thread_id = :id", ['id'=>$threadID]) / $postsPerPage);
$totalPages = (int)ceil($database->get_one("SELECT COUNT(*) FROM forum_posts WHERE thread_id = :id", ['id'=>$threadID]) / $postsPerPage);
$threadTitle = $this->get_thread_title($threadID);
if ($event->count_args() >= 3) {
$pageNumber = $event->get_arg(2);
if (!is_numeric($pageNumber)) {
$pageNumber = 0;
} elseif ($pageNumber <= 0) {
$pageNumber = 0;
} elseif ($pageNumber >= $totalPages) {
$pageNumber = $totalPages - 1;
} else {
$pageNumber--;
}
$pageNumber = page_number($event->get_arg(2), $totalPages);
} else {
$pageNumber = 0;
}

View file

@ -237,7 +237,7 @@ class Pools extends Extension
break;
case "updated":
$this->get_history(int_escape($event->get_arg(1)));
$this->get_history(page_number($event->get_arg(1)));
break;
case "revert":
@ -832,14 +832,6 @@ class Pools extends Extension
{
global $config, $database;
if (is_null($pageNumber) || !is_numeric($pageNumber)) {
$pageNumber = 0;
} elseif ($pageNumber <= 0) {
$pageNumber = 0;
} else {
$pageNumber--;
}
$historiesPerPage = $config->get_int(PoolsConfig::UPDATED_PER_PAGE);
$history = $database->get_all("