diff --git a/public/changelog.php b/public/changelog.php index b5b16305..7bbf4e17 100644 --- a/public/changelog.php +++ b/public/changelog.php @@ -12,6 +12,7 @@ $changelogRange = 30; $changelogChange = (int)($_GET['c'] ?? 0); $changelogDate = $_GET['d'] ?? ''; $changelogUser = (int)($_GET['u'] ?? 0); +$changelogTags = $_GET['t'] ?? ''; $tpl->vars([ 'changelog_offset' => $changelogOffset, @@ -66,69 +67,18 @@ if (!empty($changelogDate)) { echo render_error(404); return; } - - $getChanges = $db->prepare(' - SELECT - c.`change_id`, c.`change_log`, - a.`action_name`, a.`action_colour`, a.`action_class`, - u.`user_id`, u.`username`, - DATE(`change_created`) as `change_date`, - !ISNULL(c.`change_text`) as `change_has_text`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour` - FROM `msz_changelog_changes` as c - LEFT JOIN `msz_users` as u - ON u.`user_id` = c.`user_id` - LEFT JOIN `msz_roles` as r - ON r.`role_id` = u.`display_role` - LEFT JOIN `msz_changelog_actions` as a - ON a.`action_id` = c.`action_id` - WHERE DATE(c.`change_created`) = :date - GROUP BY `change_created`, `change_id` - ORDER BY `change_created` DESC, `change_id` DESC - '); - $getChanges->bindValue('date', $changelogDate); - $changes = $getChanges->execute() ? $getChanges->fetchAll() : []; - - // settings the response code to 404, but rendering our own page anyway. - if (count($changes) < 1) { - http_response_code(404); - } - - echo $tpl->render('changelog.date', [ - 'changes' => $changes, - ]); - return; } -$changesCount = (int)$db->query(' - SELECT COUNT(`change_id`) - FROM `msz_changelog_changes` -')->fetchColumn(); +$changesCount = !empty($changelogDate) ? -1 : changelog_count_changes($changelogDate, $changelogUser); +$changes = changelog_get_changes($changelogDate, $changelogUser, $changelogOffset, $changelogRange); -$getChanges = $db->prepare(' - SELECT - c.`change_id`, c.`change_log`, - a.`action_name`, a.`action_colour`, a.`action_class`, - u.`user_id`, u.`username`, - DATE(`change_created`) as `change_date`, - !ISNULL(c.`change_text`) as `change_has_text`, - COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour` - FROM `msz_changelog_changes` as c - LEFT JOIN `msz_users` as u - ON u.`user_id` = c.`user_id` - LEFT JOIN `msz_roles` as r - ON r.`role_id` = u.`display_role` - LEFT JOIN `msz_changelog_actions` as a - ON a.`action_id` = c.`action_id` - GROUP BY `change_created`, `change_id` - ORDER BY `change_created` DESC, `change_id` DESC - LIMIT :offset, :take -'); -$getChanges->bindValue('offset', $changelogOffset); -$getChanges->bindValue('take', $changelogRange); -$changes = $getChanges->execute() ? $getChanges->fetchAll() : []; +if (!$changes) { + http_response_code(404); +} echo $tpl->render('changelog.index', [ 'changes' => $changes, 'changelog_count' => $changesCount, + 'changelog_date' => $changelogDate, + 'changelog_user' => $changelogUser, ]); diff --git a/src/Application.php b/src/Application.php index 26ea8c05..c10c355d 100644 --- a/src/Application.php +++ b/src/Application.php @@ -240,6 +240,7 @@ class Application extends ApplicationBase $this->templatingInstance->addFilter('byte_symbol'); $this->templatingInstance->addFilter('html_link'); $this->templatingInstance->addFilter('html_colour'); + $this->templatingInstance->addFilter('url_construct'); $this->templatingInstance->addFilter('country_name', 'get_country_name'); $this->templatingInstance->addFilter('flip', 'array_flip'); $this->templatingInstance->addFilter('first_paragraph'); diff --git a/src/changelog.php b/src/changelog.php index 75f88cbc..028348bf 100644 --- a/src/changelog.php +++ b/src/changelog.php @@ -41,3 +41,86 @@ function changelog_entry_create(int $userId, int $actionId, string $log, string return $createChange->execute() ? (int)$dbc->lastInsertId() : 0; } + +define('CHANGELOG_GET_QUERY', ' + SELECT + c.`change_id`, c.`change_log`, + a.`action_name`, a.`action_colour`, a.`action_class`, + u.`user_id`, u.`username`, + DATE(`change_created`) as `change_date`, + !ISNULL(c.`change_text`) as `change_has_text`, + COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `user_colour` + FROM `msz_changelog_changes` as c + LEFT JOIN `msz_users` as u + ON u.`user_id` = c.`user_id` + LEFT JOIN `msz_roles` as r + ON r.`role_id` = u.`display_role` + LEFT JOIN `msz_changelog_actions` as a + ON a.`action_id` = c.`action_id` + WHERE %s + AND %s + GROUP BY `change_created`, `change_id` + ORDER BY `change_created` DESC, `change_id` DESC + %s +'); + +function changelog_get_changes(string $date, int $user, int $offset, int $take): array +{ + $hasDate = strlen($date) > 0; + $hasUser = $user > 0; + + $query = sprintf( + CHANGELOG_GET_QUERY, + $hasDate ? 'DATE(c.`change_created`) = :date' : '1', + $hasUser ? 'c.`user_id` = :user' : '1', + !$hasDate ? 'LIMIT :offset, :take' : '' + ); + + $dbc = Database::connection(); + $prep = $dbc->prepare($query); + + if (!$hasDate) { + $prep->bindValue('offset', $offset); + $prep->bindValue('take', $take); + } else { + $prep->bindValue('date', $date); + } + + if ($hasUser) { + $prep->bindValue('user', $user); + } + + return $prep->execute() ? $prep->fetchAll(PDO::FETCH_ASSOC) : []; +} + +define('CHANGELOG_COUNT_QUERY', ' + SELECT COUNT(`change_id`) + FROM `msz_changelog_changes` + WHERE %s + AND %s +'); + +function changelog_count_changes(string $date, int $user): int +{ + $hasDate = strlen($date) > 0; + $hasUser = $user > 0; + + $query = sprintf( + CHANGELOG_COUNT_QUERY, + $hasDate ? 'DATE(`change_created`) = :date' : '1', + $hasUser ? '`user_id` = :user' : '1' + ); + + $dbc = Database::connection(); + $prep = $dbc->prepare($query); + + if ($hasDate) { + $prep->bindValue('date', $date); + } + + if ($hasUser) { + $prep->bindValue('user', $user); + } + + return $prep->execute() ? (int)$prep->fetchColumn() : 0; +} diff --git a/utility.php b/utility.php index 0dae411a..3fa4cad1 100644 --- a/utility.php +++ b/utility.php @@ -282,3 +282,20 @@ function html_colour(int $colour, array $attribs = []): string return $css; } + +function url_construct(string $path, array $query = [], string $host = ''): string +{ + $url = $host . $path; + + if (count($query)) { + $url .= '?'; + + foreach ($query as $key => $value) { + if ($value) { + $url .= urlencode($key) . '=' . urlencode($value) . '&'; + } + } + } + + return substr($url, 0, -1); +} diff --git a/views/mio/changelog/date.twig b/views/mio/changelog/date.twig deleted file mode 100644 index fe50699b..00000000 --- a/views/mio/changelog/date.twig +++ /dev/null @@ -1,23 +0,0 @@ -{% extends '@mio/changelog/master.twig' %} -{% from '@mio/macros.twig' import navigation, pagination %} -{% from '@mio/changelog/macros.twig' import changelog_listing %} - -{% set is_valid = changes|length > 0 %} -{% set title = 'Changelog » ' ~ (is_valid ? changes[0].change_date : 'Unknown date') %} - -{% if is_valid %} - {% set canonical_url = '/changelog.php?d=' ~ changes[0].change_date %} -{% endif %} - -{% block content %} -