format
This commit is contained in:
		
							parent
							
								
									6d3ca01424
								
							
						
					
					
						commit
						aa1637d128
					
				| @ -22,5 +22,5 @@ class RatingsInfo extends ExtensionInfo | |||||||
|       <li>rating=sq -- safe and questionable images |       <li>rating=sq -- safe and questionable images | ||||||
|     </ul> |     </ul> | ||||||
| </ul>";
 | </ul>";
 | ||||||
|     public $db_support = [DatabaseDriver::MYSQL, DatabaseDriver::PGSQL]; |     //public $db_support = [DatabaseDriver::MYSQL, DatabaseDriver::PGSQL];
 | ||||||
| } | } | ||||||
|  | |||||||
| @ -8,24 +8,16 @@ $_shm_ratings = []; | |||||||
| 
 | 
 | ||||||
| class ImageRating | class ImageRating | ||||||
| { | { | ||||||
|     /** |     /** @var string */ | ||||||
|      * @var string |  | ||||||
|      */ |  | ||||||
|     public $name = null; |     public $name = null; | ||||||
| 
 | 
 | ||||||
|     /** |     /** @var string */ | ||||||
|      * @var string |  | ||||||
|      */ |  | ||||||
|     public $code = null; |     public $code = null; | ||||||
| 
 | 
 | ||||||
|     /** |     /** @var string */ | ||||||
|      * @var string |  | ||||||
|      */ |  | ||||||
|     public $search_term = null; |     public $search_term = null; | ||||||
| 
 | 
 | ||||||
|     /** |     /** @var int */ | ||||||
|      * @var int |  | ||||||
|      */ |  | ||||||
|     public $order = 0; |     public $order = 0; | ||||||
| 
 | 
 | ||||||
|     public function __construct(string $code, string $name, string $search_term, int $order) |     public function __construct(string $code, string $name, string $search_term, int $order) | ||||||
| @ -44,30 +36,25 @@ function clear_ratings() | |||||||
|     global $_shm_ratings; |     global $_shm_ratings; | ||||||
|     $keys = array_keys($_shm_ratings); |     $keys = array_keys($_shm_ratings); | ||||||
|     foreach ($keys as $key) { |     foreach ($keys as $key) { | ||||||
|         if ($key=="?") { |         if ($key != "?") { | ||||||
|             continue; |  | ||||||
|         } |  | ||||||
|             unset($_shm_ratings[$key]); |             unset($_shm_ratings[$key]); | ||||||
|         } |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function add_rating(ImageRating $rating) | function add_rating(ImageRating $rating) | ||||||
| { | { | ||||||
|     global $_shm_ratings; |     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"); |         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"); |         throw new RuntimeException("$rating->search_term is a reserved search term"); | ||||||
|     } |     } | ||||||
| 
 |  | ||||||
|     $_shm_ratings[$rating->code] = $rating; |     $_shm_ratings[$rating->code] = $rating; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| add_rating(new ImageRating("?", "Unrated", "unrated", 99999)); | add_rating(new ImageRating("?", "Unrated", "unrated", 99999)); | ||||||
| 
 |  | ||||||
| add_rating(new ImageRating("s", "Safe", "safe", 0)); | add_rating(new ImageRating("s", "Safe", "safe", 0)); | ||||||
| add_rating(new ImageRating("q", "Questionable", "questionable", 500)); | add_rating(new ImageRating("q", "Questionable", "questionable", 500)); | ||||||
| add_rating(new ImageRating("e", "Explicit", "explicit", 1000)); | add_rating(new ImageRating("e", "Explicit", "explicit", 1000)); | ||||||
| @ -103,15 +90,13 @@ class Ratings extends Extension | |||||||
|     /** @var RatingsTheme */ |     /** @var RatingsTheme */ | ||||||
|     protected $theme; |     protected $theme; | ||||||
| 
 | 
 | ||||||
|     public const UNRATED_KEYWORDS = ["unknown","unrated"]; |     public const UNRATED_KEYWORDS = ["unknown", "unrated"]; | ||||||
| 
 | 
 | ||||||
|     private $search_regexp; |     private $search_regexp; | ||||||
| 
 | 
 | ||||||
|     public function __construct() |     public function onInitExt(InitExtEvent $event) | ||||||
|     { |     { | ||||||
|         parent::__construct(); |         global $config, $_shm_user_classes, $_shm_ratings; | ||||||
| 
 |  | ||||||
|         global $_shm_ratings; |  | ||||||
| 
 | 
 | ||||||
|         $codes = implode("", array_keys($_shm_ratings)); |         $codes = implode("", array_keys($_shm_ratings)); | ||||||
|         $search_terms = []; |         $search_terms = []; | ||||||
| @ -120,16 +105,6 @@ class Ratings extends Extension | |||||||
|         } |         } | ||||||
|         $this->search_regexp = "/^rating[=|:](?:(\*|[" . $codes . "]+)|(" . |         $this->search_regexp = "/^rating[=|:](?:(\*|[" . $codes . "]+)|(" . | ||||||
|             implode("|", $search_terms) . "|".implode("|", self::UNRATED_KEYWORDS)."))$/D"; |             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) { |         foreach (array_keys($_shm_user_classes) as $key) { | ||||||
|             if ($key == "base" || $key == "hellbanned") { |             if ($key == "base" || $key == "hellbanned") { | ||||||
| @ -148,7 +123,7 @@ class Ratings extends Extension | |||||||
|     { |     { | ||||||
|         global $user; |         global $user; | ||||||
| 
 | 
 | ||||||
|         $event->add__html( |         $event->add_html( | ||||||
|             $this->theme->get_user_options( |             $this->theme->get_user_options( | ||||||
|                 $user, |                 $user, | ||||||
|                 self::get_user_default_ratings($user), |                 self::get_user_default_ratings($user), | ||||||
| @ -181,15 +156,6 @@ class Ratings extends Extension | |||||||
|         $event->panel->add_block($sb); |         $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) |     public function onDisplayingImage(DisplayingImageEvent $event) | ||||||
|     { |     { | ||||||
|         global $user, $page; |         global $user, $page; | ||||||
| @ -215,12 +181,21 @@ class Ratings extends Extension | |||||||
| 
 | 
 | ||||||
|     public function onImageInfoBoxBuilding(ImageInfoBoxBuildingEvent $event) |     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) |     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"]; |             $rating = $_POST["rating"]; | ||||||
|             if (Ratings::rating_is_valid($rating)) { |             if (Ratings::rating_is_valid($rating)) { | ||||||
|                 send_event(new RatingSetEvent($event->image, $rating)); |                 send_event(new RatingSetEvent($event->image, $rating)); | ||||||
| @ -260,7 +235,6 @@ class Ratings extends Extension | |||||||
|             $event->add_querylet(new Querylet("rating IN ($set)")); |             $event->add_querylet(new Querylet("rating IN ($set)")); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|         if (preg_match($this->search_regexp, strtolower($event->term), $matches)) { |         if (preg_match($this->search_regexp, strtolower($event->term), $matches)) { | ||||||
|             $ratings = $matches[1] ? $matches[1] : $matches[2][0]; |             $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)); |             $ratings = array_intersect(str_split($ratings), Ratings::get_user_class_privs($user)); | ||||||
| 
 |  | ||||||
|             $rating = $ratings[0]; |             $rating = $ratings[0]; | ||||||
| 
 |  | ||||||
|             $image = Image::by_id($event->id); |             $image = Image::by_id($event->id); | ||||||
| 
 |  | ||||||
|             $re = new RatingSetEvent($image, $rating); |             $re = new RatingSetEvent($image, $rating); | ||||||
| 
 |  | ||||||
|             send_event($re); |             send_event($re); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| @ -307,7 +277,6 @@ class Ratings extends Extension | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     public function onAdminBuilding(AdminBuildingEvent $event) |     public function onAdminBuilding(AdminBuildingEvent $event) | ||||||
|     { |     { | ||||||
|         global $database, $_shm_ratings; |         global $database, $_shm_ratings; | ||||||
| @ -322,7 +291,6 @@ class Ratings extends Extension | |||||||
|             } |             } | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|         $this->theme->display_form($original_values, self::get_sorted_ratings()); |         $this->theme->display_form($original_values, self::get_sorted_ratings()); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -350,7 +318,6 @@ class Ratings extends Extension | |||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event) |     public function onBulkActionBlockBuilding(BulkActionBlockBuildingEvent $event) | ||||||
|     { |     { | ||||||
|         global $user; |         global $user; | ||||||
| @ -500,18 +467,6 @@ class Ratings extends Extension | |||||||
|         return in_array($rating, array_keys($_shm_ratings)); |         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
 |      * #param string[] $context
 | ||||||
|      */ |      */ | ||||||
| @ -568,7 +523,6 @@ class Ratings extends Extension | |||||||
|                 $config->set_array("ext_rating_admin_privs", str_split($value)); |                 $config->set_array("ext_rating_admin_privs", str_split($value)); | ||||||
|             } |             } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|             switch ($database->get_driver_name()) { |             switch ($database->get_driver_name()) { | ||||||
|                 case DatabaseDriver::MYSQL: |                 case DatabaseDriver::MYSQL: | ||||||
|                     $database->Execute("ALTER TABLE images CHANGE rating rating CHAR(1) NOT NULL DEFAULT '?'"); |                     $database->Execute("ALTER TABLE images CHANGE rating rating CHAR(1) NOT NULL DEFAULT '?'"); | ||||||
|  | |||||||
| @ -1,55 +1,42 @@ | |||||||
| <?php declare(strict_types=1); | <?php declare(strict_types=1); | ||||||
| class RatingsTest extends ShimmiePHPUnitTestCase | class RatingsTest extends ShimmiePHPUnitTestCase | ||||||
| { | { | ||||||
|     public function testRating() |     public function testRatingSafe() | ||||||
|     { |     { | ||||||
|         $this->log_in_as_user(); |         $this->log_in_as_user(); | ||||||
|         $image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx"); |         $image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx"); | ||||||
| 
 |         $image = Image::by_id($image_id); | ||||||
|         # test for bug #735: user forced to set rating, can't
 |         send_event(new RatingSetEvent($image, "s")); | ||||||
|         # 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"); |  | ||||||
| 
 | 
 | ||||||
|         # search for it in various ways
 |         # search for it in various ways
 | ||||||
|         $this->get_page("post/list/rating=Safe/1"); |         $page = $this->get_page("post/list/rating=Safe/1"); | ||||||
|         $this->assert_title("Image $image_id: new"); |         $this->assertEquals("/post/view/1", $page->redirect); | ||||||
| 
 | 
 | ||||||
|         $this->get_page("post/list/rating=s/1"); |         $page = $this->get_page("post/list/rating=s/1"); | ||||||
|         $this->assert_title("Image $image_id: new"); |         $this->assertEquals("/post/view/1", $page->redirect); | ||||||
| 
 | 
 | ||||||
|         $this->get_page("post/list/rating=sqe/1"); |         $page = $this->get_page("post/list/rating=sqe/1"); | ||||||
|         $this->assert_title("Image $image_id: new"); |         $this->assertEquals("/post/view/1", $page->redirect); | ||||||
| 
 | 
 | ||||||
|         # test that search by tag still works
 |         # test that search by tag still works
 | ||||||
|         $this->get_page("post/list/new/1"); |         $page = $this->get_page("post/list/pbx/1"); | ||||||
|         $this->assert_title("Image $image_id: new"); |         $this->assertEquals("/post/view/1", $page->redirect); | ||||||
| 
 | 
 | ||||||
|         # searching for a different rating should return nothing
 |         # searching for a different rating should return nothing
 | ||||||
|         $this->get_page("post/list/rating=q/1"); |         $page = $this->get_page("post/list/rating=q/1"); | ||||||
|         $this->assert_text("No Images Found"); |         $this->assertEquals("No Images Found", $page->heading); | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|         # now set explicit, for the next test
 |     public function testRatingExplicit() | ||||||
|         $this->get_page("post/view/$image_id"); |     { | ||||||
|         $this->set_field("rating", "e"); |         $this->log_in_as_user(); | ||||||
|         $this->click("Set"); |         $image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx"); | ||||||
|         $this->assert_title("Image $image_id: new"); |         $image = Image::by_id($image_id); | ||||||
| 
 |         send_event(new RatingSetEvent($image, "e")); | ||||||
|         $this->log_out(); |  | ||||||
| 
 | 
 | ||||||
|         # the explicit image shouldn't show up in anon's searches
 |         # the explicit image shouldn't show up in anon's searches
 | ||||||
|         $this->get_page("post/list/new/1"); |         $this->log_out(); | ||||||
|         $this->assert_text("No Images Found"); |         $page = $this->get_page("post/list/pbx/1"); | ||||||
|  |         $this->assertEquals("No Images Found", $page->heading); | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -23,7 +23,6 @@ class RatingsTheme extends Themelet | |||||||
|         return $html; |         return $html; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     public function display_form(array $current_ratings, array $available_ratings) |     public function display_form(array $current_ratings, array $available_ratings) | ||||||
|     { |     { | ||||||
|         global $page; |         global $page; | ||||||
| @ -44,26 +43,6 @@ class RatingsTheme extends Themelet | |||||||
|         $page->add_block(new Block("Update Ratings", $html)); |         $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"))."
 |  | ||||||
|     // 			<input type='hidden' name='query' value='".html_escape($terms)."'>
 |  | ||||||
|     // 			<select name='rating'>
 |  | ||||||
|     // 				<option value='s'>Safe</option>
 |  | ||||||
|     // 				<option value='q'>Questionable</option>
 |  | ||||||
|     // 				<option value='e'>Explicit</option>
 |  | ||||||
|     // 				<option value='u'>Unrated</option>
 |  | ||||||
|     // 			</select>
 |  | ||||||
|     // 			<input type='submit' value='Go'>
 |  | ||||||
|     // 		</form>
 |  | ||||||
|     // 	";
 |  | ||||||
|     //     $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) |     public function get_selection_rater_html(array $selected_options, bool $multiple = false, array $available_options = null) | ||||||
|     { |     { | ||||||
|         $output = "<select name='rating".($multiple ? "[]' multiple='multiple'" : "' ")." >"; |         $output = "<select name='rating".($multiple ? "[]' multiple='multiple'" : "' ")." >"; | ||||||
|  | |||||||
| @ -19,7 +19,7 @@ class UserOptionsBuildingEvent extends Event | |||||||
|     /** @var array  */ |     /** @var array  */ | ||||||
|     public $parts = []; |     public $parts = []; | ||||||
| 
 | 
 | ||||||
|     public function add__html(string $html) |     public function add_html(string $html) | ||||||
|     { |     { | ||||||
|         $this->parts[] = $html; |         $this->parts[] = $html; | ||||||
|     } |     } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user