2021-12-14 18:32:47 +00:00
< ? php
declare ( strict_types = 1 );
2007-04-16 11:58:25 +00:00
2023-01-10 22:44:09 +00:00
namespace Shimmie2 ;
2019-09-29 13:32:51 +00:00
class ImageRating
{
2024-01-20 19:57:26 +00:00
/** @var array<string, ImageRating> */
public static array $known_ratings = [];
2021-03-14 23:43:50 +00:00
public string $name ;
public string $code ;
public string $search_term ;
public int $order = 0 ;
2019-06-13 19:41:03 +00:00
2019-09-29 13:32:51 +00:00
public function __construct ( string $code , string $name , string $search_term , int $order )
2019-06-13 19:41:03 +00:00
{
2023-11-11 21:49:12 +00:00
assert ( strlen ( $code ) == 1 , " Rating code must be exactly one character " );
2019-06-13 19:41:03 +00:00
$this -> name = $name ;
$this -> code = $code ;
$this -> search_term = $search_term ;
$this -> order = $order ;
2019-06-27 04:11:59 +00:00
}
}
2023-01-11 11:15:26 +00:00
function add_rating ( ImageRating $rating ) : void
2019-09-29 13:32:51 +00:00
{
2024-01-20 19:57:26 +00:00
if ( $rating -> code == " ? " && array_key_exists ( " ? " , ImageRating :: $known_ratings )) {
2023-01-10 22:44:09 +00:00
throw new \RuntimeException ( " ? is a reserved rating code that cannot be overridden " );
2019-06-27 04:11:59 +00:00
}
2020-01-29 01:47:43 +00:00
if ( $rating -> code != " ? " && in_array ( strtolower ( $rating -> search_term ), Ratings :: UNRATED_KEYWORDS )) {
2023-01-10 22:44:09 +00:00
throw new \RuntimeException ( " $rating->search_term is a reserved search term " );
2019-07-05 15:32:34 +00:00
}
2024-01-20 19:57:26 +00:00
ImageRating :: $known_ratings [ $rating -> code ] = $rating ;
2019-06-27 04:11:59 +00:00
}
2019-06-28 01:56:50 +00:00
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 ));
2020-03-13 09:23:54 +00:00
/** @noinspection PhpIncludeInspection */
2019-06-13 19:41:03 +00:00
@ include_once " data/config/ratings.conf.php " ;
2024-03-28 01:47:03 +00:00
class RatingSetException extends UserError
{
public ? string $redirect ;
public function __construct ( string $msg , ? string $redirect = null )
{
parent :: __construct ( $msg );
$this -> redirect = $redirect ;
}
}
2019-05-28 16:59:38 +00:00
class RatingSetEvent extends Event
{
2021-03-14 23:43:50 +00:00
public Image $image ;
public string $rating ;
2007-10-18 02:04:22 +00:00
2019-05-28 16:59:38 +00:00
public function __construct ( Image $image , string $rating )
{
2020-01-26 13:19:35 +00:00
parent :: __construct ();
2019-06-13 19:41:03 +00:00
2024-01-20 19:57:26 +00:00
assert ( in_array ( $rating , array_keys ( ImageRating :: $known_ratings )));
2014-04-28 07:26:35 +00:00
2019-05-28 16:59:38 +00:00
$this -> image = $image ;
$this -> rating = $rating ;
}
2007-10-18 02:04:22 +00:00
}
2019-09-29 13:32:51 +00:00
abstract class RatingsConfig
{
2021-12-14 18:32:47 +00:00
public const VERSION = " ext_ratings2_version " ;
public const USER_DEFAULTS = " ratings_default " ;
2019-06-27 04:11:59 +00:00
}
2019-05-28 16:59:38 +00:00
class Ratings extends Extension
{
2020-01-26 13:19:35 +00:00
/** @var RatingsTheme */
2023-06-27 14:56:49 +00:00
protected Themelet $theme ;
2020-01-26 13:19:35 +00:00
2020-01-29 01:47:43 +00:00
public const UNRATED_KEYWORDS = [ " unknown " , " unrated " ];
2019-07-05 15:32:34 +00:00
2021-03-14 23:43:50 +00:00
private string $search_regexp ;
2019-06-13 19:41:03 +00:00
2024-01-15 11:52:35 +00:00
public function onInitExt ( InitExtEvent $event ) : void
2019-06-27 04:11:59 +00:00
{
2024-01-20 19:57:26 +00:00
global $config ;
2019-06-13 19:41:03 +00:00
2024-01-20 19:57:26 +00:00
$codes = implode ( " " , array_keys ( ImageRating :: $known_ratings ));
2019-06-13 19:41:03 +00:00
$search_terms = [];
2024-01-20 19:57:26 +00:00
foreach ( ImageRating :: $known_ratings as $key => $rating ) {
2023-01-11 11:15:26 +00:00
$search_terms [] = $rating -> search_term ;
2019-06-13 19:41:03 +00:00
}
2019-06-27 04:11:59 +00:00
$this -> search_regexp = " /^rating[=|:](?:( \ *|[ " . $codes . " ]+)|( " .
2019-09-29 13:32:51 +00:00
implode ( " | " , $search_terms ) . " | " . implode ( " | " , self :: UNRATED_KEYWORDS ) . " )) $ /D " ;
2019-06-27 04:11:59 +00:00
2024-01-20 19:52:18 +00:00
foreach ( array_keys ( UserClass :: $known_classes ) as $key ) {
2019-06-27 04:11:59 +00:00
if ( $key == " base " || $key == " hellbanned " ) {
2019-06-13 19:41:03 +00:00
continue ;
}
2024-01-20 19:57:26 +00:00
$config -> set_default_array ( " ext_rating_ " . $key . " _privs " , array_keys ( ImageRating :: $known_ratings ));
2019-06-13 19:41:03 +00:00
}
2024-01-15 18:01:48 +00:00
Image :: $prop_types [ " rating " ] = ImagePropType :: STRING ;
2019-06-27 04:11:59 +00:00
}
2020-06-16 23:40:13 +00:00
private function check_permissions ( Image $image ) : bool
{
global $user ;
$user_view_level = Ratings :: get_user_class_privs ( $user );
2024-01-15 17:12:36 +00:00
if ( ! in_array ( $image [ 'rating' ], $user_view_level )) {
2020-06-16 23:40:13 +00:00
return false ;
}
return true ;
}
2024-01-15 11:52:35 +00:00
public function onInitUserConfig ( InitUserConfigEvent $event ) : void
2019-09-29 13:32:51 +00:00
{
2019-06-27 14:48:07 +00:00
$event -> user_config -> set_default_array ( RatingsConfig :: USER_DEFAULTS , self :: get_user_class_privs ( $event -> user ));
2019-05-28 16:59:38 +00:00
}
2019-07-05 15:32:34 +00:00
2024-01-15 11:52:35 +00:00
public function onImageDownloading ( ImageDownloadingEvent $event ) : void
2020-06-16 23:40:13 +00:00
{
/**
* Deny images upon insufficient permissions .
**/
if ( ! $this -> check_permissions ( $event -> image )) {
2024-02-11 15:47:40 +00:00
throw new PermissionDenied ( " Access denied " );
2020-06-16 23:40:13 +00:00
}
}
2024-01-15 11:52:35 +00:00
public function onUserOptionsBuilding ( UserOptionsBuildingEvent $event ) : void
2019-06-27 04:11:59 +00:00
{
2024-01-20 19:57:26 +00:00
global $user ;
2019-06-27 04:11:59 +00:00
2020-10-26 15:13:28 +00:00
$levels = self :: get_user_class_privs ( $user );
$options = [];
foreach ( $levels as $level ) {
2024-01-20 19:57:26 +00:00
$options [ ImageRating :: $known_ratings [ $level ] -> name ] = $level ;
2020-10-26 15:13:28 +00:00
}
$sb = $event -> panel -> create_new_block ( " Default Rating Filter " );
$sb -> start_table ();
2024-01-07 03:48:28 +00:00
$sb -> add_multichoice_option ( RatingsConfig :: USER_DEFAULTS , $options , " Default Ratings: " , true );
2020-10-26 15:13:28 +00:00
$sb -> end_table ();
$sb -> add_label ( " This controls the default rating search results will be filtered by, and nothing else. To override in your search results, add rating:* to your search. " );
2019-05-28 16:59:38 +00:00
}
2019-06-27 04:11:59 +00:00
2024-01-15 11:52:35 +00:00
public function onSetupBuilding ( SetupBuildingEvent $event ) : void
2019-05-28 16:59:38 +00:00
{
2019-06-27 04:11:59 +00:00
$ratings = self :: get_sorted_ratings ();
2019-06-13 19:41:03 +00:00
$options = [];
2019-06-27 04:11:59 +00:00
foreach ( $ratings as $key => $rating ) {
$options [ $rating -> name ] = $rating -> code ;
}
2012-02-09 05:40:01 +00:00
2023-03-22 23:33:16 +00:00
$sb = $event -> panel -> create_new_block ( " Post Rating Visibility " );
2019-06-27 04:11:59 +00:00
$sb -> start_table ();
2024-01-20 19:52:18 +00:00
foreach ( array_keys ( UserClass :: $known_classes ) as $key ) {
2019-06-27 04:11:59 +00:00
if ( $key == " base " || $key == " hellbanned " ) {
2019-06-13 19:41:03 +00:00
continue ;
}
2019-06-27 04:11:59 +00:00
$sb -> add_multichoice_option ( " ext_rating_ " . $key . " _privs " , $options , $key , true );
2019-06-13 19:41:03 +00:00
}
2019-06-27 04:11:59 +00:00
$sb -> end_table ();
2019-05-28 16:59:38 +00:00
}
2019-06-27 04:11:59 +00:00
2024-01-15 11:52:35 +00:00
public function onDisplayingImage ( DisplayingImageEvent $event ) : void
2019-05-28 16:59:38 +00:00
{
2020-06-16 23:40:13 +00:00
global $page ;
2019-05-28 16:59:38 +00:00
/**
* Deny images upon insufficient permissions .
**/
2020-06-16 23:40:13 +00:00
if ( ! $this -> check_permissions ( $event -> image )) {
2019-06-19 01:58:28 +00:00
$page -> set_mode ( PageMode :: REDIRECT );
2023-08-18 11:42:42 +00:00
$page -> set_redirect ( make_link ());
2019-05-28 16:59:38 +00:00
}
}
2019-06-27 04:11:59 +00:00
2024-01-15 11:52:35 +00:00
public function onBulkExport ( BulkExportEvent $event ) : void
2020-06-02 23:05:09 +00:00
{
2024-01-15 17:12:36 +00:00
$event -> fields [ " rating " ] = $event -> image [ 'rating' ];
2020-06-02 23:05:09 +00:00
}
2024-01-15 11:52:35 +00:00
public function onBulkImport ( BulkImportEvent $event ) : void
2020-06-02 23:05:09 +00:00
{
2023-01-11 11:15:26 +00:00
if ( array_key_exists ( " rating " , $event -> fields )
2023-06-27 16:45:35 +00:00
&& $event -> fields [ 'rating' ] !== null
2023-02-03 20:03:04 +00:00
&& Ratings :: rating_is_valid ( $event -> fields [ 'rating' ])) {
$this -> set_rating ( $event -> image -> id , $event -> fields [ 'rating' ], " " );
2020-06-02 23:05:09 +00:00
}
}
2024-01-15 11:52:35 +00:00
public function onRatingSet ( RatingSetEvent $event ) : void
2019-05-28 16:59:38 +00:00
{
2024-01-15 17:12:36 +00:00
if ( empty ( $event -> image [ 'rating' ])) {
2019-05-28 16:59:38 +00:00
$old_rating = " " ;
} else {
2024-01-15 17:12:36 +00:00
$old_rating = $event -> image [ 'rating' ];
2019-05-28 16:59:38 +00:00
}
$this -> set_rating ( $event -> image -> id , $event -> rating , $old_rating );
}
2019-06-27 04:11:59 +00:00
2024-01-15 11:52:35 +00:00
public function onImageInfoBoxBuilding ( ImageInfoBoxBuildingEvent $event ) : void
2019-05-28 16:59:38 +00:00
{
2020-01-29 01:47:43 +00:00
global $user ;
$event -> add_part (
2024-03-28 01:47:03 +00:00
$this -> theme -> get_image_rater_html (
2020-01-29 01:47:43 +00:00
$event -> image -> id ,
2024-01-15 17:12:36 +00:00
$event -> image [ 'rating' ],
2020-01-29 01:47:43 +00:00
$user -> can ( Permissions :: EDIT_IMAGE_RATING )
),
80
);
2019-05-28 16:59:38 +00:00
}
2019-06-27 04:11:59 +00:00
2024-01-15 11:52:35 +00:00
public function onImageInfoSet ( ImageInfoSetEvent $event ) : void
2019-05-28 16:59:38 +00:00
{
2024-03-28 01:47:03 +00:00
global $page , $user ;
if (
$user -> can ( Permissions :: EDIT_IMAGE_RATING ) && (
isset ( $event -> params [ 'rating' ])
|| isset ( $event -> params [ " rating { $event -> slot } " ])
)
) {
$common_rating = $event -> params [ 'rating' ] ? ? " " ;
$my_rating = $event -> params [ " rating { $event -> slot } " ] ? ? " " ;
$rating = Ratings :: rating_is_valid ( $my_rating ) ? $my_rating : $common_rating ;
2019-05-28 16:59:38 +00:00
if ( Ratings :: rating_is_valid ( $rating )) {
2024-03-28 01:47:03 +00:00
try {
send_event ( new RatingSetEvent ( $event -> image , $rating ));
} catch ( RatingSetException $e ) {
if ( $e -> redirect ) {
$page -> flash ( " { $e -> getMessage () } , please see { $e -> redirect } " );
} else {
$page -> flash ( $e -> getMessage ());
}
throw $e ;
}
2019-05-28 16:59:38 +00:00
}
}
}
2007-10-02 21:59:20 +00:00
2024-01-15 11:52:35 +00:00
public function onParseLinkTemplate ( ParseLinkTemplateEvent $event ) : void
2019-05-28 16:59:38 +00:00
{
2024-08-31 16:05:18 +00:00
if ( ! is_null ( $event -> image [ 'rating' ])) {
2024-01-15 17:12:36 +00:00
$event -> replace ( '$rating' , $this -> rating_to_human ( $event -> image [ 'rating' ]));
2023-12-21 15:26:30 +00:00
}
2019-05-28 16:59:38 +00:00
}
2012-02-09 05:40:01 +00:00
2024-01-15 11:52:35 +00:00
public function onHelpPageBuilding ( HelpPageBuildingEvent $event ) : void
2019-08-02 20:05:49 +00:00
{
2023-11-11 21:49:12 +00:00
if ( $event -> key === HelpPages :: SEARCH ) {
2019-08-02 20:05:49 +00:00
$ratings = self :: get_sorted_ratings ();
2023-06-29 17:44:57 +00:00
$event -> add_block ( new Block ( " Ratings " , $this -> theme -> get_help_html ( $ratings )));
2019-08-02 20:05:49 +00:00
}
}
2024-01-15 11:52:35 +00:00
public function onSearchTermParse ( SearchTermParseEvent $event ) : void
2019-05-28 16:59:38 +00:00
{
global $user ;
2019-06-27 04:11:59 +00:00
2019-05-28 16:59:38 +00:00
$matches = [];
if ( is_null ( $event -> term ) && $this -> no_rating_query ( $event -> context )) {
2021-03-14 23:43:50 +00:00
$set = Ratings :: privs_to_sql ( Ratings :: get_user_default_ratings ());
2019-05-28 16:59:38 +00:00
$event -> add_querylet ( new Querylet ( " rating IN ( $set ) " ));
}
2019-06-13 19:41:03 +00:00
2020-01-29 11:30:52 +00:00
if ( is_null ( $event -> term )) {
return ;
}
2019-06-13 19:41:03 +00:00
if ( preg_match ( $this -> search_regexp , strtolower ( $event -> term ), $matches )) {
2019-05-28 16:59:38 +00:00
$ratings = $matches [ 1 ] ? $matches [ 1 ] : $matches [ 2 ][ 0 ];
2019-06-13 19:41:03 +00:00
2023-11-11 21:49:12 +00:00
if ( count ( $matches ) > 2 && in_array ( $matches [ 2 ], self :: UNRATED_KEYWORDS )) {
2019-07-05 15:32:34 +00:00
$ratings = " ? " ;
}
2019-06-27 04:11:59 +00:00
if ( $ratings == '*' ) {
$ratings = Ratings :: get_user_class_privs ( $user );
} else {
$ratings = array_intersect ( str_split ( $ratings ), Ratings :: get_user_class_privs ( $user ));
}
2019-06-13 19:41:03 +00:00
2019-05-28 16:59:38 +00:00
$set = " ' " . join ( " ', ' " , $ratings ) . " ' " ;
$event -> add_querylet ( new Querylet ( " rating IN ( $set ) " ));
}
}
2009-08-02 07:19:43 +00:00
2024-01-15 11:52:35 +00:00
public function onTagTermCheck ( TagTermCheckEvent $event ) : void
2020-01-29 20:22:50 +00:00
{
if ( preg_match ( $this -> search_regexp , $event -> term )) {
$event -> metatag = true ;
}
}
2024-01-15 11:52:35 +00:00
public function onTagTermParse ( TagTermParseEvent $event ) : void
2019-06-11 14:59:06 +00:00
{
2019-06-14 14:46:32 +00:00
global $user ;
2019-06-11 14:59:06 +00:00
$matches = [];
2020-01-29 20:22:50 +00:00
if ( preg_match ( $this -> search_regexp , strtolower ( $event -> term ), $matches )) {
2019-06-11 14:59:06 +00:00
$ratings = $matches [ 1 ] ? $matches [ 1 ] : $matches [ 2 ][ 0 ];
2019-06-27 04:11:59 +00:00
2023-11-11 21:49:12 +00:00
if ( count ( $matches ) > 2 && in_array ( $matches [ 2 ], self :: UNRATED_KEYWORDS )) {
2019-07-05 15:32:34 +00:00
$ratings = " ? " ;
}
2019-06-27 04:11:59 +00:00
$ratings = array_intersect ( str_split ( $ratings ), Ratings :: get_user_class_privs ( $user ));
2019-06-11 14:59:06 +00:00
$rating = $ratings [ 0 ];
2024-02-20 00:22:25 +00:00
$image = Image :: by_id_ex ( $event -> image_id );
2023-02-04 20:50:26 +00:00
send_event ( new RatingSetEvent ( $image , $rating ));
2019-06-11 14:59:06 +00:00
}
}
2019-06-27 04:11:59 +00:00
2024-01-15 11:52:35 +00:00
public function onAdminBuilding ( AdminBuildingEvent $event ) : void
2019-06-27 04:11:59 +00:00
{
2024-01-20 19:57:26 +00:00
global $database ;
2019-06-27 04:11:59 +00:00
$results = $database -> get_col ( " SELECT DISTINCT rating FROM images ORDER BY rating " );
$original_values = [];
foreach ( $results as $result ) {
2024-01-20 20:48:47 +00:00
assert ( is_string ( $result ));
2024-01-20 19:57:26 +00:00
if ( array_key_exists ( $result , ImageRating :: $known_ratings )) {
$original_values [ $result ] = ImageRating :: $known_ratings [ $result ] -> name ;
2019-06-27 04:11:59 +00:00
} else {
$original_values [ $result ] = $result ;
}
}
2023-06-25 23:05:38 +00:00
$this -> theme -> display_form ( $original_values );
2019-06-27 04:11:59 +00:00
}
2024-01-15 11:52:35 +00:00
public function onAdminAction ( AdminActionEvent $event ) : void
2019-06-27 04:11:59 +00:00
{
global $database , $user ;
$action = $event -> action ;
switch ( $action ) {
case " update_ratings " :
$event -> redirect = true ;
2024-02-10 00:00:49 +00:00
if ( ! array_key_exists ( " rating_old " , $event -> params ) || empty ( $event -> params [ " rating_old " ])) {
2019-06-27 04:11:59 +00:00
return ;
}
2024-02-10 00:00:49 +00:00
if ( ! array_key_exists ( " rating_new " , $event -> params ) || empty ( $event -> params [ " rating_new " ])) {
2019-06-27 04:11:59 +00:00
return ;
}
2024-02-10 00:00:49 +00:00
$old = $event -> params [ " rating_old " ];
$new = $event -> params [ " rating_new " ];
2019-06-27 04:11:59 +00:00
2019-11-02 19:57:34 +00:00
if ( $user -> can ( Permissions :: BULK_EDIT_IMAGE_RATING )) {
2023-11-11 21:49:12 +00:00
$database -> execute ( " UPDATE images SET rating = :new WHERE rating = :old " , [ " new " => $new , " old " => $old ]);
2019-06-27 04:11:59 +00:00
}
break ;
}
}
2024-01-15 11:52:35 +00:00
public function onBulkActionBlockBuilding ( BulkActionBlockBuildingEvent $event ) : void
2019-06-05 23:03:22 +00:00
{
global $user ;
2019-07-09 14:10:21 +00:00
if ( $user -> can ( Permissions :: BULK_EDIT_IMAGE_RATING )) {
2023-06-29 05:50:32 +00:00
$event -> add_action ( " bulk_rate " , " Set (R)ating " , " r " , " " , ( string ) $this -> theme -> get_selection_rater_html ( selected_options : [ " ? " ]));
2019-06-05 23:03:22 +00:00
}
}
2024-01-15 11:52:35 +00:00
public function onBulkAction ( BulkActionEvent $event ) : void
2019-06-05 23:03:22 +00:00
{
2019-12-15 19:47:18 +00:00
global $page , $user ;
2019-06-05 23:03:22 +00:00
2019-06-14 12:47:50 +00:00
switch ( $event -> action ) {
2019-06-12 22:44:25 +00:00
case " bulk_rate " :
2024-02-10 00:05:33 +00:00
if ( ! isset ( $event -> params [ 'rating' ])) {
2019-06-05 23:03:22 +00:00
return ;
}
2019-07-09 14:10:21 +00:00
if ( $user -> can ( Permissions :: BULK_EDIT_IMAGE_RATING )) {
2024-02-10 00:05:33 +00:00
$rating = $event -> params [ 'rating' ];
2019-06-12 22:44:25 +00:00
$total = 0 ;
2019-07-05 15:24:46 +00:00
foreach ( $event -> items as $image ) {
2019-06-05 23:03:22 +00:00
send_event ( new RatingSetEvent ( $image , $rating ));
2019-06-14 12:47:50 +00:00
$total ++ ;
2019-06-05 23:03:22 +00:00
}
2019-12-15 19:47:18 +00:00
$page -> flash ( " Rating set for $total items " );
2019-06-05 23:03:22 +00:00
}
break ;
}
}
2024-01-15 11:52:35 +00:00
public function onPageRequest ( PageRequestEvent $event ) : void
2019-05-28 16:59:38 +00:00
{
2021-03-14 23:43:50 +00:00
global $user , $page ;
2019-06-27 04:11:59 +00:00
2024-02-10 23:03:14 +00:00
if ( $event -> page_matches ( " admin/bulk_rate " , method : " POST " , permission : Permissions :: BULK_EDIT_IMAGE_RATING )) {
$n = 0 ;
while ( true ) {
$images = Search :: find_images ( $n , 100 , Tag :: explode ( $event -> req_POST ( " query " )));
if ( count ( $images ) == 0 ) {
break ;
}
2019-06-27 04:11:59 +00:00
2024-02-10 23:03:14 +00:00
reset ( $images ); // rewind to first element in array.
2019-06-27 04:11:59 +00:00
2024-02-10 23:03:14 +00:00
foreach ( $images as $image ) {
send_event ( new RatingSetEvent ( $image , $event -> req_POST ( 'rating' )));
2019-05-28 16:59:38 +00:00
}
2024-02-10 23:03:14 +00:00
$n += 100 ;
2019-05-28 16:59:38 +00:00
}
2024-02-10 23:03:14 +00:00
$page -> set_mode ( PageMode :: REDIRECT );
$page -> set_redirect ( make_link ());
2019-05-28 16:59:38 +00:00
}
}
2014-04-28 07:26:35 +00:00
2024-03-28 01:47:03 +00:00
public function onUploadHeaderBuilding ( UploadHeaderBuildingEvent $event ) : void
{
$event -> add_part ( " Rating " );
}
public function onUploadSpecificBuilding ( UploadSpecificBuildingEvent $event ) : void
{
$event -> add_part ( $this -> theme -> get_upload_specific_rater_html ( $event -> suffix ));
}
2024-01-20 14:10:59 +00:00
/**
* @ return ImageRating []
*/
2019-06-27 04:11:59 +00:00
public static function get_sorted_ratings () : array
{
2024-01-20 19:57:26 +00:00
$ratings = array_values ( ImageRating :: $known_ratings );
2019-06-27 04:11:59 +00:00
usort ( $ratings , function ( $a , $b ) {
return $a -> order <=> $b -> order ;
});
return $ratings ;
2019-05-28 16:59:38 +00:00
}
2014-04-28 07:26:35 +00:00
2024-01-20 14:10:59 +00:00
/**
* @ param ImageRating [] | null $ratings
* @ return array < string , string >
*/
2024-08-31 23:04:34 +00:00
public static function get_ratings_dict ( ? array $ratings = null ) : array
2023-06-25 23:05:38 +00:00
{
if ( ! isset ( $ratings )) {
$ratings = self :: get_sorted_ratings ();
}
return array_combine (
array_map ( function ( $o ) {
return $o -> code ;
}, $ratings ),
array_map ( function ( $o ) {
return $o -> name ;
}, $ratings )
);
}
2023-12-31 18:48:56 +00:00
/**
* Figure out which ratings a user is allowed to see
*
* @ return string []
*/
2019-06-27 04:11:59 +00:00
public static function get_user_class_privs ( User $user ) : array
2019-05-28 16:59:38 +00:00
{
global $config ;
2014-04-28 07:26:35 +00:00
2019-06-13 19:41:03 +00:00
return $config -> get_array ( " ext_rating_ " . $user -> class -> name . " _privs " );
2019-05-28 16:59:38 +00:00
}
2009-11-15 05:45:50 +00:00
2023-12-31 18:48:56 +00:00
/**
* Figure out which ratings a user would like to see by default
* ( Which will be a subset of what they are allowed to see )
*
* @ return string []
*/
2020-10-26 15:13:28 +00:00
public static function get_user_default_ratings () : array
2019-06-27 04:11:59 +00:00
{
2020-10-26 15:13:28 +00:00
global $user_config , $user ;
2019-06-27 04:11:59 +00:00
$available = self :: get_user_class_privs ( $user );
$selected = $user_config -> get_array ( RatingsConfig :: USER_DEFAULTS );
return array_intersect ( $available , $selected );
}
2024-01-20 14:10:59 +00:00
/**
* @ param string [] $privs
*/
2019-06-14 14:46:32 +00:00
public static function privs_to_sql ( array $privs ) : string
2019-05-28 16:59:38 +00:00
{
$arr = [];
2019-09-29 13:32:51 +00:00
foreach ( $privs as $i ) {
2019-06-13 19:41:03 +00:00
$arr [] = " ' " . $i . " ' " ;
}
2023-11-11 21:49:12 +00:00
if ( sizeof ( $arr ) == 0 ) {
2019-06-13 19:41:03 +00:00
return " ' ' " ;
2019-05-28 16:59:38 +00:00
}
2020-01-26 13:19:35 +00:00
return join ( ', ' , $arr );
2019-05-28 16:59:38 +00:00
}
2009-11-15 05:45:50 +00:00
2019-05-28 16:59:38 +00:00
public static function rating_to_human ( string $rating ) : string
{
2024-01-20 19:57:26 +00:00
if ( array_key_exists ( $rating , ImageRating :: $known_ratings )) {
return ImageRating :: $known_ratings [ $rating ] -> name ;
2019-05-28 16:59:38 +00:00
}
2019-06-13 19:41:03 +00:00
return " Unknown " ;
2019-05-28 16:59:38 +00:00
}
2010-01-03 08:55:43 +00:00
2019-05-28 16:59:38 +00:00
public static function rating_is_valid ( string $rating ) : bool
{
2024-01-20 19:57:26 +00:00
return in_array ( $rating , array_keys ( ImageRating :: $known_ratings ));
2019-05-28 16:59:38 +00:00
}
2016-09-25 19:17:29 +00:00
2019-05-28 16:59:38 +00:00
/**
2024-01-01 03:27:39 +00:00
* @ param string [] $context
2019-05-28 16:59:38 +00:00
*/
private function no_rating_query ( array $context ) : bool
{
foreach ( $context as $term ) {
if ( preg_match ( " /^rating[=|:]/ " , $term )) {
return false ;
}
}
return true ;
}
2007-04-16 11:58:25 +00:00
2024-01-15 11:52:35 +00:00
public function onDatabaseUpgrade ( DatabaseUpgradeEvent $event ) : void
2019-05-28 16:59:38 +00:00
{
global $database , $config ;
2007-10-02 22:29:32 +00:00
2019-11-03 19:04:57 +00:00
if ( $this -> get_version ( RatingsConfig :: VERSION ) < 1 ) {
2020-10-25 21:34:52 +00:00
$database -> execute ( " ALTER TABLE images ADD COLUMN rating CHAR(1) NOT NULL DEFAULT '?' " );
$database -> execute ( " CREATE INDEX images__rating ON images(rating) " );
2019-11-03 19:04:57 +00:00
$this -> set_version ( RatingsConfig :: VERSION , 3 );
2019-05-28 16:59:38 +00:00
}
2007-10-02 22:29:32 +00:00
2019-11-03 19:04:57 +00:00
if ( $this -> get_version ( RatingsConfig :: VERSION ) < 2 ) {
2020-10-25 21:34:52 +00:00
$database -> execute ( " CREATE INDEX images__rating ON images(rating) " );
2019-11-03 19:04:57 +00:00
$this -> set_version ( RatingsConfig :: VERSION , 2 );
2019-05-28 16:59:38 +00:00
}
2009-01-22 07:21:56 +00:00
2019-11-03 19:04:57 +00:00
if ( $this -> get_version ( RatingsConfig :: VERSION ) < 3 ) {
2020-10-25 21:34:52 +00:00
$database -> execute ( " UPDATE images SET rating = 'u' WHERE rating is null " );
2022-10-28 00:45:35 +00:00
switch ( $database -> get_driver_id ()) {
case DatabaseDriverID :: MYSQL :
2020-10-25 21:34:52 +00:00
$database -> execute ( " ALTER TABLE images CHANGE rating rating CHAR(1) NOT NULL DEFAULT 'u' " );
2019-06-11 14:59:06 +00:00
break ;
2022-10-28 00:45:35 +00:00
case DatabaseDriverID :: PGSQL :
2020-10-25 21:34:52 +00:00
$database -> execute ( " ALTER TABLE images ALTER COLUMN rating SET DEFAULT 'u' " );
$database -> execute ( " ALTER TABLE images ALTER COLUMN rating SET NOT NULL " );
2019-06-11 14:59:06 +00:00
break ;
}
2019-11-03 19:04:57 +00:00
$this -> set_version ( RatingsConfig :: VERSION , 3 );
2019-09-29 13:32:51 +00:00
}
2019-06-13 19:41:03 +00:00
2019-11-03 19:04:57 +00:00
if ( $this -> get_version ( RatingsConfig :: VERSION ) < 4 ) {
2019-06-13 19:41:03 +00:00
$value = $config -> get_string ( " ext_rating_anon_privs " );
2019-09-29 13:32:51 +00:00
if ( ! empty ( $value )) {
2019-06-28 04:30:00 +00:00
$config -> set_array ( " ext_rating_anonymous_privs " , str_split ( $value ));
}
2019-06-13 19:41:03 +00:00
$value = $config -> get_string ( " ext_rating_user_privs " );
2019-09-29 13:32:51 +00:00
if ( ! empty ( $value )) {
2019-06-28 04:30:00 +00:00
$config -> set_array ( " ext_rating_user_privs " , str_split ( $value ));
}
2019-06-13 19:41:03 +00:00
$value = $config -> get_string ( " ext_rating_admin_privs " );
2019-09-29 13:32:51 +00:00
if ( ! empty ( $value )) {
2019-06-28 04:30:00 +00:00
$config -> set_array ( " ext_rating_admin_privs " , str_split ( $value ));
}
2019-06-27 04:11:59 +00:00
2022-10-28 00:45:35 +00:00
switch ( $database -> get_driver_id ()) {
case DatabaseDriverID :: MYSQL :
2020-10-25 21:34:52 +00:00
$database -> execute ( " ALTER TABLE images CHANGE rating rating CHAR(1) NOT NULL DEFAULT '?' " );
2019-06-28 02:58:24 +00:00
break ;
2022-10-28 00:45:35 +00:00
case DatabaseDriverID :: PGSQL :
2020-10-25 21:34:52 +00:00
$database -> execute ( " ALTER TABLE images ALTER COLUMN rating SET DEFAULT '?' " );
2019-06-28 02:58:24 +00:00
break ;
}
2021-09-22 14:42:41 +00:00
$database -> set_timeout ( null ); // These updates can take a little bit
2019-06-28 02:58:24 +00:00
2023-11-11 21:49:12 +00:00
$database -> execute ( " UPDATE images SET rating = :new WHERE rating = :old " , [ " new " => '?' , " old " => 'u' ]);
2019-06-27 04:11:59 +00:00
2019-11-03 19:04:57 +00:00
$this -> set_version ( RatingsConfig :: VERSION , 4 );
2019-05-28 16:59:38 +00:00
}
}
2007-04-16 11:58:25 +00:00
2024-01-20 14:10:59 +00:00
private function set_rating ( int $image_id , string $rating , string $old_rating ) : void
2019-05-28 16:59:38 +00:00
{
global $database ;
if ( $old_rating != $rating ) {
2023-11-11 21:49:12 +00:00
$database -> execute ( " UPDATE images SET rating=:rating WHERE id=:id " , [ 'rating' => $rating , 'id' => $image_id ]);
2020-10-09 12:47:42 +00:00
log_info ( " rating " , " Rating for >> { $image_id } set to: " . $this -> rating_to_human ( $rating ));
2019-05-28 16:59:38 +00:00
}
}
2007-04-16 11:58:25 +00:00
}