diff --git a/includes/class-CategoryYearMonthDayTypes.php b/includes/class-CategoryYearMonthDayTypes.php index 3860191..d73a133 100644 --- a/includes/class-CategoryYearMonthDayTypes.php +++ b/includes/class-CategoryYearMonthDayTypes.php @@ -1,119 +1,160 @@ . namespace itwikidelbot; use \cli\Log; +use \InvalidArgumentException; /** * Handler of all the PDCs in this day of the year */ class CategoryYearMonthDayTypes extends CategoryYearMonthDay { /** * PDC types * * Categories in order of importance (I think) * * @var array */ public static $TYPES = [ CategoryYearMonthDayTypeVoting ::class, CategoryYearMonthDayTypeProlonged ::class, CategoryYearMonthDayTypeConsensual::class, CategoryYearMonthDayTypeOrdinary ::class, - CategoryYearMonthDayTypeSimple ::class, + CategoryYearMonthDay ::class, // simple ]; + /** + * Get the corresponding class of a certain PDC type + * + * @param $type string PDC type + * @return class + */ + public static function type2class( $type ) { + foreach( self::getAll() as $Type ) { + if( $Type::PDC_TYPE === $type ) { + return $Type; + } + } + throw new InvalidArgumentException( "unknown PDC type $pdc_type" ); + } + + /** + * Get the corresponding class of a certain PDC type + * + * @param $title string Page title + * @return CategoryYearMonthDay + */ + public static function title2object( $title ) { + foreach( self::getAll() as $Type ) { + $category = $Type::createParsingTitle( $title ); + if( $category ) { + return $category; + } + } + throw new InvalidArgumentException( "unexpected PDC category '$title'" ); + } + + /** + * Get all the types + * + * @return array + */ + public static function getAll() { + return self::$TYPES; + } + /** * Return the newer PDC, but setting the older start date. * * @param $pdc1 PDC * @param $pdc2 PDC * @return $pdc PDC */ public function mergePDCs( $pdc1, $pdc2 ) { if( $pdc2->getDurationDays() > $pdc1->getDurationDays() ) { return $pdc2->setStartDate( $pdc1->getStartDate() ); } return $pdc1; } /** * Operate on every PDC type */ public function run() { $pdcs_by_id = []; foreach( self::$TYPES as $Category ) { $category = new $Category( $this->getYear(), $this->getMonth(), $this->getDay() ); $pdcs = $category->fetchPDCs(); if( count( $pdcs ) ) { $category->saveIfNotExists(); } // merge the same PDCs foreach( $pdcs as $pdc ) { $id = $pdc->getId(); if( isset( $pdcs_by_id[ $id ] ) ) { $pdcs_by_id[ $id ] = self::mergePDCs( $pdcs_by_id[ $id ], $pdc ); } else { $pdcs_by_id[ $id ] = $pdc; } } } // sort PDCs by start date usort( $pdcs_by_id, function ( $a, $b ) { return $a->getStartDate() > $b->getStartDate(); } ); foreach( $pdcs_by_id as $i => $pdc ) { if( $pdc->getDurationDays() > 7 ) { Log::info( sprintf( 'the PDC %s is expired after %d days', $pdc->getTitle(), $pdc->getDurationDays() ) ); } } // index PDcs by their type $pdcs_by_type = []; foreach( $pdcs_by_id as $pdc ) { $type = $pdc->getType(); if( ! isset( $pdcs_by_type[ $type ] ) ) { $pdcs_by_type[ $type ] = []; } $pdcs_by_type[ $type ][] = $pdc; } PageYearMonthDayPDCsCount::createFromDateTimePDCs( $this->getDateTime(), $pdcs_by_type ) ->save(); PageYearMonthDayPDCsLog::createFromDateTimePDCs( $this->getDateTime(), $pdcs_by_type ) ->save(); } }