diff --git a/includes/class-CategoryYearMonthDayTypes.php b/includes/class-CategoryYearMonthDayTypes.php index 5c86703..38be17d 100644 --- a/includes/class-CategoryYearMonthDayTypes.php +++ b/includes/class-CategoryYearMonthDayTypes.php @@ -1,155 +1,156 @@ . namespace itwikidelbot; use InvalidArgumentException; /** * Handler of all the PDC type categories */ class CategoryYearMonthDayTypes { /** * PDC types * * Categories in order of importance (I think) * * @var array */ public static $TYPES = [ CategoryYearMonthDayTypeVoting ::class, // voting CategoryYearMonthDayTypeOrdinary ::class, // ordinary CategoryYearMonthDayTypeProlonged ::class, // prolonged CategoryYearMonthDayTypeConsensual::class, // consensual CategoryYearMonthDay ::class, // simple ]; /** * Other types of categories * * @var array */ private static $OTHER_KNOWN_CATEGORIES = [ 'Categoria:Procedure di cancellazione protette', 'Categoria:Procedure di cancellazione multiple in corso', 'Categoria:Pagine protette - scadute', + 'Categoria:Pagine di servizio protette', ]; /** * Get all the types * * @return array */ public static function all() { return self::$TYPES; } /** * Create the corresponding category object from its title * * @param $title string Page title * @return CategoryYearMonthDay|false */ public static function createParsingTitle( $title ) { foreach( self::all() as $Type ) { $category = $Type::createParsingTitle( $title ); if( $category ) { return $category; } } if( ! in_array( $title, self::$OTHER_KNOWN_CATEGORIES, true ) ) { throw new PDCException( "unexpected PDC category '$title'" ); } return false; } /** * Get a "genericity" score of a certain PDC category class name. * * Useful to comparate PDC types. * * E.g. a "simple" PDC has an higher score over a "consensual" PDC. * * @param $category string Class name * @return int 0-4 */ public static function genericityFromClass( $category_class_name ) { $i = array_search( $category_class_name, self::all(), true ); if( false === $i ) { throw new InvalidArgumentException( "unknown class name $category_class_name" ); } return $i; } /** * Get a "genericity" score of a certain PDC category instance. * * @see self::genericity() * @param $category CategoryYearMonthDay * @return int 0-4 */ public static function genericityFromObject( CategoryYearMonthDay $category ) { return self::genericityFromClass( get_class( $category ) ); } /** * Find the best category (the most recent) from a list * * @param $categories CategoryYearMonthDay[] * @return CategoryYearMonthDay One of the $categories */ public static function findBestCategory( $categories ) { $best = null; foreach( $categories as $category ) { if( ! $best ) { $best = $category; } elseif( $category->getDateTime()->format('Y-m-d') === $best->getDateTime()->format('Y-m-d') ) { // the day is the same: take the most precise if( self::genericityFromObject( $category ) < self::genericityFromObject( $best ) ) { $best = $category; } } elseif( $category->getDateTime() > $best->getDateTime() ) { // take the newest $best = $category; } } if( ! $best ) { throw new PDCException( 'cannot find the best categories' ); } return $best; } /** * * * @param $year int * @param $month int * @param $day int * @return PDC[] */ public static function fetchYearMonthDayPDCs( $year, $month, $day ) { $pdcs_by_id = []; } }