From b59fe4c694b4b7c7474d5c20984acf4a588c2631 Mon Sep 17 00:00:00 2001 From: Shish Date: Thu, 28 Mar 2024 12:42:39 +0000 Subject: [PATCH] Have a common PartListBuildingEvent, fixes #1124 --- core/event.php | 31 +++++++++++ ext/comment/main.php | 2 +- ext/favorites/main.php | 2 +- ext/forum/main.php | 4 +- ext/help_pages/main.php | 15 +++--- ext/image/main.php | 2 +- ext/numeric_score/main.php | 2 +- ext/report_image/theme.php | 3 +- ext/user/events.php | 53 +++++++------------ ext/user/main.php | 19 +++---- ext/user/theme.php | 2 +- ext/user_config/main.php | 2 +- ext/user_config/theme.php | 8 ++- .../image_admin_block_building_event.php | 15 ++---- .../events/image_info_box_building_event.php | 15 ++---- ext/view/main.php | 6 +-- 16 files changed, 90 insertions(+), 91 deletions(-) diff --git a/core/event.php b/core/event.php index abf2be83..5b545eed 100644 --- a/core/event.php +++ b/core/event.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Shimmie2; +use MicroHTML\HTMLElement; + /** * Generic parent class for all events. * @@ -346,3 +348,32 @@ class LogEvent extends Event class DatabaseUpgradeEvent extends Event { } + +/** + * @template T + */ +abstract class PartListBuildingEvent extends Event +{ + /** @var T[] */ + private array $parts = []; + + /** + * @param T $html + */ + public function add_part(mixed $html, int $position = 50): void + { + while (isset($this->parts[$position])) { + $position++; + } + $this->parts[$position] = $html; + } + + /** + * @return array + */ + public function get_parts(): array + { + ksort($this->parts); + return $this->parts; + } +} diff --git a/ext/comment/main.php b/ext/comment/main.php index 0069864c..ddb77b66 100644 --- a/ext/comment/main.php +++ b/ext/comment/main.php @@ -323,7 +323,7 @@ class CommentList extends Extension $i_days_old = ((time() - \Safe\strtotime($event->display_user->join_date)) / 86400) + 1; $i_comment_count = Comment::count_comments_by_user($event->display_user); $h_comment_rate = sprintf("%.1f", ($i_comment_count / $i_days_old)); - $event->add_stats("Comments made: $i_comment_count, $h_comment_rate per day"); + $event->add_part("Comments made: $i_comment_count, $h_comment_rate per day"); $recent = $this->get_user_comments($event->display_user->id, 10); $this->theme->display_recent_user_comments($recent, $event->display_user); diff --git a/ext/favorites/main.php b/ext/favorites/main.php index 7a681ab4..3a493279 100644 --- a/ext/favorites/main.php +++ b/ext/favorites/main.php @@ -87,7 +87,7 @@ class Favorites extends Extension $i_days_old = ((time() - \Safe\strtotime($event->display_user->join_date)) / 86400) + 1; $h_favorites_rate = sprintf("%.1f", ($i_favorites_count / $i_days_old)); $favorites_link = search_link(["favorited_by={$event->display_user->name}"]); - $event->add_stats("Posts favorited: $i_favorites_count, $h_favorites_rate per day"); + $event->add_part("Posts favorited: $i_favorites_count, $h_favorites_rate per day"); } public function onImageInfoSet(ImageInfoSetEvent $event): void diff --git a/ext/forum/main.php b/ext/forum/main.php index 6e072f5e..f2f4c31f 100644 --- a/ext/forum/main.php +++ b/ext/forum/main.php @@ -87,8 +87,8 @@ class Forum extends Extension $threads_rate = sprintf("%.1f", ($threads_count / $days_old)); $posts_rate = sprintf("%.1f", ($posts_count / $days_old)); - $event->add_stats("Forum threads: $threads_count, $threads_rate per day"); - $event->add_stats("Forum posts: $posts_count, $posts_rate per day"); + $event->add_part("Forum threads: $threads_count, $threads_rate per day"); + $event->add_part("Forum posts: $posts_count, $posts_rate per day"); } public function onPageNavBuilding(PageNavBuildingEvent $event): void diff --git a/ext/help_pages/main.php b/ext/help_pages/main.php index a74ea31f..d06f8454 100644 --- a/ext/help_pages/main.php +++ b/ext/help_pages/main.php @@ -15,11 +15,12 @@ class HelpPageListBuildingEvent extends Event } } -class HelpPageBuildingEvent extends Event +/** + * @extends PartListBuildingEvent + */ +class HelpPageBuildingEvent extends PartListBuildingEvent { public string $key; - /** @var array */ - public array $blocks = []; public function __construct(string $key) { @@ -29,10 +30,7 @@ class HelpPageBuildingEvent extends Event public function add_block(Block $block, int $position = 50): void { - while (array_key_exists($position, $this->blocks)) { - $position++; - } - $this->blocks[$position] = $block; + $this->add_part($block, $position); } } @@ -58,8 +56,7 @@ class HelpPages extends Extension $this->theme->display_help_page($title); $hpbe = send_event(new HelpPageBuildingEvent($name)); - ksort($hpbe->blocks); - foreach ($hpbe->blocks as $block) { + foreach ($hpbe->get_parts() as $block) { $page->add_block($block); } } elseif ($event->page_matches("help")) { diff --git a/ext/image/main.php b/ext/image/main.php index 6cc7b4d8..089dd217 100644 --- a/ext/image/main.php +++ b/ext/image/main.php @@ -155,7 +155,7 @@ class ImageIO extends Extension $i_days_old = ((time() - \Safe\strtotime($event->display_user->join_date)) / 86400) + 1; $h_image_rate = sprintf("%.1f", ($i_image_count / $i_days_old)); $images_link = search_link(["user=$u_name"]); - $event->add_stats("Posts uploaded: $i_image_count, $h_image_rate per day"); + $event->add_part("Posts uploaded: $i_image_count, $h_image_rate per day"); } public function onSetupBuilding(SetupBuildingEvent $event): void diff --git a/ext/numeric_score/main.php b/ext/numeric_score/main.php index 97aa6137..52b44226 100644 --- a/ext/numeric_score/main.php +++ b/ext/numeric_score/main.php @@ -131,7 +131,7 @@ class NumericScore extends Extension $link_up = search_link(["upvoted_by={$event->display_user->name}"]); $n_down = Search::count_images(["downvoted_by={$event->display_user->name}"]); $link_down = search_link(["downvoted_by={$event->display_user->name}"]); - $event->add_stats("$n_up Upvotes / $n_down Downvotes"); + $event->add_part("$n_up Upvotes / $n_down Downvotes"); } public function onPageRequest(PageRequestEvent $event): void diff --git a/ext/report_image/theme.php b/ext/report_image/theme.php index 3560a7e6..57117f89 100644 --- a/ext/report_image/theme.php +++ b/ext/report_image/theme.php @@ -28,8 +28,7 @@ class ReportImageTheme extends Themelet $userlink = "$reporter_name"; $iabbe = send_event(new ImageAdminBlockBuildingEvent($image, $user, "report")); - ksort($iabbe->parts); - $actions = join("", $iabbe->parts); + $actions = join("", $iabbe->get_parts()); $h_reportedimages .= " diff --git a/ext/user/events.php b/ext/user/events.php index 8a558141..a96ed033 100644 --- a/ext/user/events.php +++ b/ext/user/events.php @@ -6,53 +6,40 @@ namespace Shimmie2; use MicroHTML\HTMLElement; -class UserBlockBuildingEvent extends Event +/** + * @extends PartListBuildingEvent + */ +class UserBlockBuildingEvent extends PartListBuildingEvent { - /** @var array */ - public array $parts = []; - public function add_link(string|HTMLElement $name, string $link, int $position = 50): void { - while (isset($this->parts[$position])) { - $position++; - } - $this->parts[$position] = ["name" => $name, "link" => $link]; + $this->add_part(["name" => $name, "link" => $link], $position); } } -class UserOperationsBuildingEvent extends Event +/** + * @extends PartListBuildingEvent + */ +class UserOperationsBuildingEvent extends PartListBuildingEvent { - /** @var string[] */ - public array $parts = []; - - public function __construct(public User $user, public BaseConfig $user_config) - { + public function __construct( + public User $user, + public BaseConfig $user_config, + ) { parent::__construct(); } - - public function add_html(string $html): void - { - $this->parts[] = $html; - } } -class UserPageBuildingEvent extends Event +/** + * @extends PartListBuildingEvent + */ +class UserPageBuildingEvent extends PartListBuildingEvent { - /** @var array */ - public array $stats = []; - - public function __construct(public User $display_user) - { + public function __construct( + public User $display_user, + ) { parent::__construct(); } - - public function add_stats(string $html, int $position = 50): void - { - while (isset($this->stats[$position])) { - $position++; - } - $this->stats[$position] = $html; - } } class UserCreationEvent extends Event diff --git a/ext/user/main.php b/ext/user/main.php index b5a2d831..8bec2cf1 100644 --- a/ext/user/main.php +++ b/ext/user/main.php @@ -340,22 +340,22 @@ class UserPage extends Extension $h_class = $event->display_user->class->name; } - $event->add_stats("Joined: $h_join_date", 10); + $event->add_part("Joined: $h_join_date", 10); if ($user->name == $event->display_user->name) { - $event->add_stats("Current IP: " . get_real_ip(), 80); + $event->add_part("Current IP: " . get_real_ip(), 80); } - $event->add_stats("Class: $h_class", 90); + $event->add_part("Class: $h_class", 90); $av = $event->display_user->get_avatar_html(); if ($av) { - $event->add_stats($av, 0); + $event->add_part($av, 0); } elseif ( ( $config->get_string("avatar_host") == "gravatar" ) && ($user->id == $event->display_user->id) ) { - $event->add_stats( + $event->add_part( "No avatar? This gallery uses Gravatar for avatar hosting, use the" . "
same email address here and there to have your avatar synced
", 0 @@ -377,8 +377,7 @@ class UserPage extends Extension { global $user, $page, $config; - ksort($event->stats); - $this->theme->display_user_page($event->display_user, $event->stats); + $this->theme->display_user_page($event->display_user, $event->get_parts()); if (!$user->is_anonymous()) { if ($user->id == $event->display_user->id || $user->can("edit_user_info")) { @@ -391,8 +390,7 @@ class UserPage extends Extension if ($user->id == $event->display_user->id) { $ubbe = send_event(new UserBlockBuildingEvent()); - ksort($ubbe->parts); - $this->theme->display_user_links($page, $user, $ubbe->parts); + $this->theme->display_user_links($page, $user, $ubbe->get_parts()); } if ( ($user->can(Permissions::VIEW_IP) || ($user->is_logged_in() && $user->id == $event->display_user->id)) && # admin or self-user @@ -606,8 +604,7 @@ class UserPage extends Extension $this->theme->display_login_block($page); } else { $ubbe = send_event(new UserBlockBuildingEvent()); - ksort($ubbe->parts); - $this->theme->display_user_block($page, $user, $ubbe->parts); + $this->theme->display_user_block($page, $user, $ubbe->get_parts()); } } diff --git a/ext/user/theme.php b/ext/user/theme.php index 7759327c..4b75004d 100644 --- a/ext/user/theme.php +++ b/ext/user/theme.php @@ -336,7 +336,7 @@ class UserPageTheme extends Themelet )); } - foreach ($event->parts as $part) { + foreach ($event->get_parts() as $part) { $html .= $part; } } diff --git a/ext/user_config/main.php b/ext/user_config/main.php index 9ade92f0..d3802d5b 100644 --- a/ext/user_config/main.php +++ b/ext/user_config/main.php @@ -166,7 +166,7 @@ class UserConfig extends Extension $key = generate_key(); $event->user_config->set_string(self::API_KEY, $key); } - $event->add_html($this->theme->get_user_operations($key)); + $event->add_part($this->theme->get_user_operations($key)); } } diff --git a/ext/user_config/theme.php b/ext/user_config/theme.php index 6e8593c3..69f410b2 100644 --- a/ext/user_config/theme.php +++ b/ext/user_config/theme.php @@ -4,9 +4,13 @@ declare(strict_types=1); namespace Shimmie2; +use MicroHTML\HTMLElement; + +use function MicroHTML\rawHTML; + class UserConfigTheme extends Themelet { - public function get_user_operations(string $key): string + public function get_user_operations(string $key): HTMLElement { $html = "

".make_form(make_link("user_admin/reset_api_key"))." @@ -24,7 +28,7 @@ class UserConfigTheme extends Themelet "; - return $html; + return rawHTML($html); } diff --git a/ext/view/events/image_admin_block_building_event.php b/ext/view/events/image_admin_block_building_event.php index 4be16016..13406843 100644 --- a/ext/view/events/image_admin_block_building_event.php +++ b/ext/view/events/image_admin_block_building_event.php @@ -8,10 +8,11 @@ use MicroHTML\HTMLElement; use function MicroHTML\{FORM,INPUT}; -class ImageAdminBlockBuildingEvent extends Event +/** + * @extends PartListBuildingEvent + */ +class ImageAdminBlockBuildingEvent extends PartListBuildingEvent { - /** @var HTMLElement[] */ - public array $parts = []; public Image $image; public User $user; public string $context; @@ -24,14 +25,6 @@ class ImageAdminBlockBuildingEvent extends Event $this->context = $context; } - public function add_part(HTMLElement $html, int $position = 50): void - { - while (isset($this->parts[$position])) { - $position++; - } - $this->parts[$position] = $html; - } - public function add_button(string $name, string $path, int $position = 50): void { $this->add_part( diff --git a/ext/view/events/image_info_box_building_event.php b/ext/view/events/image_info_box_building_event.php index de2585da..3413e832 100644 --- a/ext/view/events/image_info_box_building_event.php +++ b/ext/view/events/image_info_box_building_event.php @@ -6,10 +6,11 @@ namespace Shimmie2; use MicroHTML\HTMLElement; -class ImageInfoBoxBuildingEvent extends Event +/** + * @extends PartListBuildingEvent + */ +class ImageInfoBoxBuildingEvent extends PartListBuildingEvent { - /** @var HTMLElement[] */ - public array $parts = []; public Image $image; public User $user; @@ -19,12 +20,4 @@ class ImageInfoBoxBuildingEvent extends Event $this->image = $image; $this->user = $user; } - - public function add_part(HTMLElement $html, int $position = 50): void - { - while (isset($this->parts[$position])) { - $position++; - } - $this->parts[$position] = $html; - } } diff --git a/ext/view/main.php b/ext/view/main.php index 44314825..d31c7369 100644 --- a/ext/view/main.php +++ b/ext/view/main.php @@ -97,12 +97,10 @@ class ViewPost extends Extension $this->theme->display_meta_headers($image); $iibbe = send_event(new ImageInfoBoxBuildingEvent($image, $user)); - ksort($iibbe->parts); - $this->theme->display_page($image, $iibbe->parts); + $this->theme->display_page($image, $iibbe->get_parts()); $iabbe = send_event(new ImageAdminBlockBuildingEvent($image, $user, "view")); - ksort($iabbe->parts); - $this->theme->display_admin_block($page, $iabbe->parts); + $this->theme->display_admin_block($page, $iabbe->get_parts()); } public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event): void