dedupe page_number parsing
This commit is contained in:
parent
b5f0bc7621
commit
880a702b42
3 changed files with 23 additions and 31 deletions
|
@ -511,6 +511,24 @@ function no_escape(string $input): string
|
||||||
return $input;
|
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
|
function clamp(?int $val, ?int $min=null, ?int $max=null): int
|
||||||
{
|
{
|
||||||
if (!is_numeric($val) || (!is_null($min) && $val < $min)) {
|
if (!is_numeric($val) || (!is_null($min) && $val < $min)) {
|
||||||
|
|
|
@ -249,19 +249,10 @@ class Forum extends Extension
|
||||||
{
|
{
|
||||||
global $config, $database;
|
global $config, $database;
|
||||||
$threadsPerPage = $config->get_int('forumThreadsPerPage', 15);
|
$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) {
|
if ($event->count_args() >= 2) {
|
||||||
$pageNumber = $event->get_arg(1);
|
$pageNumber = page_number($event->get_arg(1), $totalPages);
|
||||||
if (!is_numeric($pageNumber)) {
|
|
||||||
$pageNumber = 0;
|
|
||||||
} elseif ($pageNumber <= 0) {
|
|
||||||
$pageNumber = 0;
|
|
||||||
} elseif ($pageNumber >= $totalPages) {
|
|
||||||
$pageNumber = $totalPages - 1;
|
|
||||||
} else {
|
|
||||||
$pageNumber--;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
$pageNumber = 0;
|
$pageNumber = 0;
|
||||||
}
|
}
|
||||||
|
@ -286,20 +277,11 @@ class Forum extends Extension
|
||||||
global $config, $database;
|
global $config, $database;
|
||||||
$threadID = int_escape($event->get_arg(1));
|
$threadID = int_escape($event->get_arg(1));
|
||||||
$postsPerPage = $config->get_int('forumPostsPerPage', 15);
|
$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);
|
$threadTitle = $this->get_thread_title($threadID);
|
||||||
|
|
||||||
if ($event->count_args() >= 3) {
|
if ($event->count_args() >= 3) {
|
||||||
$pageNumber = $event->get_arg(2);
|
$pageNumber = page_number($event->get_arg(2), $totalPages);
|
||||||
if (!is_numeric($pageNumber)) {
|
|
||||||
$pageNumber = 0;
|
|
||||||
} elseif ($pageNumber <= 0) {
|
|
||||||
$pageNumber = 0;
|
|
||||||
} elseif ($pageNumber >= $totalPages) {
|
|
||||||
$pageNumber = $totalPages - 1;
|
|
||||||
} else {
|
|
||||||
$pageNumber--;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
$pageNumber = 0;
|
$pageNumber = 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -237,7 +237,7 @@ class Pools extends Extension
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "updated":
|
case "updated":
|
||||||
$this->get_history(int_escape($event->get_arg(1)));
|
$this->get_history(page_number($event->get_arg(1)));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "revert":
|
case "revert":
|
||||||
|
@ -832,14 +832,6 @@ class Pools extends Extension
|
||||||
{
|
{
|
||||||
global $config, $database;
|
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);
|
$historiesPerPage = $config->get_int(PoolsConfig::UPDATED_PER_PAGE);
|
||||||
|
|
||||||
$history = $database->get_all("
|
$history = $database->get_all("
|
||||||
|
|
Reference in a new issue