diff --git a/ext/rating/info.php b/ext/rating/info.php index 1394252d..321b58f1 100644 --- a/ext/rating/info.php +++ b/ext/rating/info.php @@ -22,5 +22,5 @@ class RatingsInfo extends ExtensionInfo
  • rating=sq -- safe and questionable images "; - public $db_support = [DatabaseDriver::MYSQL, DatabaseDriver::PGSQL]; + //public $db_support = [DatabaseDriver::MYSQL, DatabaseDriver::PGSQL]; } diff --git a/ext/rating/main.php b/ext/rating/main.php index 80588899..8388ed89 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -8,24 +8,16 @@ $_shm_ratings = []; class ImageRating { - /** - * @var string - */ + /** @var string */ public $name = null; - /** - * @var string - */ + /** @var string */ public $code = null; - /** - * @var string - */ + /** @var string */ public $search_term = null; - /** - * @var int - */ + /** @var int */ public $order = 0; public function __construct(string $code, string $name, string $search_term, int $order) @@ -42,32 +34,27 @@ class ImageRating function clear_ratings() { global $_shm_ratings; - $keys = array_keys($_shm_ratings); + $keys = array_keys($_shm_ratings); foreach ($keys as $key) { - if ($key=="?") { - continue; + if ($key != "?") { + unset($_shm_ratings[$key]); } - unset($_shm_ratings[$key]); } } function add_rating(ImageRating $rating) { global $_shm_ratings; - - if ($rating->code=="?"&&array_key_exists("?", $_shm_ratings)) { + if ($rating->code == "?" && array_key_exists("?", $_shm_ratings)) { throw new RuntimeException("? is a reserved rating code that cannot be overridden"); } - - if ($rating->code!="?"&&in_array(strtolower($rating->search_term), Ratings::UNRATED_KEYWORDS)) { + if ($rating->code != "?" && in_array(strtolower($rating->search_term), Ratings::UNRATED_KEYWORDS)) { throw new RuntimeException("$rating->search_term is a reserved search term"); } - $_shm_ratings[$rating->code] = $rating; } add_rating(new ImageRating("?", "Unrated", "unrated", 99999)); - add_rating(new ImageRating("s", "Safe", "safe", 0)); add_rating(new ImageRating("q", "Questionable", "questionable", 500)); add_rating(new ImageRating("e", "Explicit", "explicit", 1000)); @@ -103,15 +90,13 @@ class Ratings extends Extension /** @var RatingsTheme */ protected $theme; - public const UNRATED_KEYWORDS = ["unknown","unrated"]; + public const UNRATED_KEYWORDS = ["unknown", "unrated"]; private $search_regexp; - public function __construct() + public function onInitExt(InitExtEvent $event) { - parent::__construct(); - - global $_shm_ratings; + global $config, $_shm_user_classes, $_shm_ratings; $codes = implode("", array_keys($_shm_ratings)); $search_terms = []; @@ -120,16 +105,6 @@ class Ratings extends Extension } $this->search_regexp = "/^rating[=|:](?:(\*|[" . $codes . "]+)|(" . implode("|", $search_terms) . "|".implode("|", self::UNRATED_KEYWORDS)."))$/D"; - } - - public function get_priority(): int - { - return 50; - } - - public function onInitExt(InitExtEvent $event) - { - global $config, $_shm_user_classes, $_shm_ratings; foreach (array_keys($_shm_user_classes) as $key) { if ($key == "base" || $key == "hellbanned") { @@ -148,7 +123,7 @@ class Ratings extends Extension { global $user; - $event->add__html( + $event->add_html( $this->theme->get_user_options( $user, self::get_user_default_ratings($user), @@ -181,15 +156,6 @@ class Ratings extends Extension $event->panel->add_block($sb); } - // public function onPostListBuilding(PostListBuildingEvent $event) - // { - // global $user; - // if ($user->can(Permissions::BULK_EDIT_IMAGE_RATING) && !empty($event->search_terms)) { - // $this->theme->display_bulk_rater(Tag::implode($event->search_terms)); - // } - // } - - public function onDisplayingImage(DisplayingImageEvent $event) { global $user, $page; @@ -215,12 +181,21 @@ class Ratings extends Extension public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event) { - $event->add_part($this->theme->get_rater_html($event->image->id, $event->image->rating, $this->can_rate()), 80); + global $user; + $event->add_part( + $this->theme->get_rater_html( + $event->image->id, + $event->image->rating, + $user->can(Permissions::EDIT_IMAGE_RATING) + ), + 80 + ); } public function onImageInfoSet(ImageInfoSetEvent $event) { - if ($this->can_rate() && isset($_POST["rating"])) { + global $user; + if ($user->can(Permissions::EDIT_IMAGE_RATING) && isset($_POST["rating"])) { $rating = $_POST["rating"]; if (Ratings::rating_is_valid($rating)) { send_event(new RatingSetEvent($event->image, $rating)); @@ -260,7 +235,6 @@ class Ratings extends Extension $event->add_querylet(new Querylet("rating IN ($set)")); } - if (preg_match($this->search_regexp, strtolower($event->term), $matches)) { $ratings = $matches[1] ? $matches[1] : $matches[2][0]; @@ -292,13 +266,9 @@ class Ratings extends Extension } $ratings = array_intersect(str_split($ratings), Ratings::get_user_class_privs($user)); - $rating = $ratings[0]; - $image = Image::by_id($event->id); - $re = new RatingSetEvent($image, $rating); - send_event($re); } @@ -307,7 +277,6 @@ class Ratings extends Extension } } - public function onAdminBuilding(AdminBuildingEvent $event) { global $database, $_shm_ratings; @@ -322,7 +291,6 @@ class Ratings extends Extension } } - $this->theme->display_form($original_values, self::get_sorted_ratings()); } @@ -350,7 +318,6 @@ class Ratings extends Extension } } - public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event) { global $user; @@ -500,18 +467,6 @@ class Ratings extends Extension return in_array($rating, array_keys($_shm_ratings)); } - /** - * FIXME: this is a bit ugly and guessey, should have proper options - */ - private function can_rate(): bool - { - global $user; - if ($user->can(Permissions::EDIT_IMAGE_RATING)) { - return true; - } - return false; - } - /** * #param string[] $context */ @@ -568,7 +523,6 @@ class Ratings extends Extension $config->set_array("ext_rating_admin_privs", str_split($value)); } - switch ($database->get_driver_name()) { case DatabaseDriver::MYSQL: $database->Execute("ALTER TABLE images CHANGE rating rating CHAR(1) NOT NULL DEFAULT '?'"); diff --git a/ext/rating/test.php b/ext/rating/test.php index 67e002e9..ad583c45 100644 --- a/ext/rating/test.php +++ b/ext/rating/test.php @@ -1,55 +1,42 @@ log_in_as_user(); $image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx"); - - # test for bug #735: user forced to set rating, can't - # set tags and leave unrated - $this->get_page("post/view/$image_id"); - $this->assert_title("Image $image_id: pbx"); - - $this->markTestIncomplete(); - - $this->set_field("tag_edit__tags", "new"); - $this->click("Set"); - $this->assert_title("Image $image_id: new"); - - # set safe - $this->set_field("rating", "s"); - $this->click("Set"); - $this->assert_title("Image $image_id: new"); + $image = Image::by_id($image_id); + send_event(new RatingSetEvent($image, "s")); # search for it in various ways - $this->get_page("post/list/rating=Safe/1"); - $this->assert_title("Image $image_id: new"); + $page = $this->get_page("post/list/rating=Safe/1"); + $this->assertEquals("/post/view/1", $page->redirect); - $this->get_page("post/list/rating=s/1"); - $this->assert_title("Image $image_id: new"); + $page = $this->get_page("post/list/rating=s/1"); + $this->assertEquals("/post/view/1", $page->redirect); - $this->get_page("post/list/rating=sqe/1"); - $this->assert_title("Image $image_id: new"); + $page = $this->get_page("post/list/rating=sqe/1"); + $this->assertEquals("/post/view/1", $page->redirect); # test that search by tag still works - $this->get_page("post/list/new/1"); - $this->assert_title("Image $image_id: new"); + $page = $this->get_page("post/list/pbx/1"); + $this->assertEquals("/post/view/1", $page->redirect); # searching for a different rating should return nothing - $this->get_page("post/list/rating=q/1"); - $this->assert_text("No Images Found"); + $page = $this->get_page("post/list/rating=q/1"); + $this->assertEquals("No Images Found", $page->heading); + } - # now set explicit, for the next test - $this->get_page("post/view/$image_id"); - $this->set_field("rating", "e"); - $this->click("Set"); - $this->assert_title("Image $image_id: new"); - - $this->log_out(); + public function testRatingExplicit() + { + $this->log_in_as_user(); + $image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx"); + $image = Image::by_id($image_id); + send_event(new RatingSetEvent($image, "e")); # the explicit image shouldn't show up in anon's searches - $this->get_page("post/list/new/1"); - $this->assert_text("No Images Found"); + $this->log_out(); + $page = $this->get_page("post/list/pbx/1"); + $this->assertEquals("No Images Found", $page->heading); } } diff --git a/ext/rating/theme.php b/ext/rating/theme.php index 0c3552d2..a8d74452 100644 --- a/ext/rating/theme.php +++ b/ext/rating/theme.php @@ -23,7 +23,6 @@ class RatingsTheme extends Themelet return $html; } - public function display_form(array $current_ratings, array $available_ratings) { global $page; @@ -44,26 +43,6 @@ class RatingsTheme extends Themelet $page->add_block(new Block("Update Ratings", $html)); } - - - // public function display_bulk_rater(string $terms) - // { - // global $page; - // $html = " - // ".make_form(make_link("admin/bulk_rate"))." - // - // - // - // - // "; - // $page->add_block(new Block("List Controls", $html, "left")); - // } - public function get_selection_rater_html(array $selected_options, bool $multiple = false, array $available_options = null) { $output = "