diff --git a/includes/class-PDCs.php b/includes/class-PDCs.php index 24b641d..a684271 100644 --- a/includes/class-PDCs.php +++ b/includes/class-PDCs.php @@ -1,166 +1,166 @@ . namespace itwikidelbot; use DateTime; use mw\API\PageMatcher; /** * Handle some PDCs */ class PDCs { /** * Sort some PDCs by creation date * * @param $pdcs array */ public static function sortByCreationDate( & $pdcs ) { // sort PDCs by start date usort( $pdcs, function ( $a, $b ) { return $a->getCreationDate() > $b->getCreationDate(); } ); } /** * Index some PDCs by their type * * @param $pdcs array * @return array */ public static function indexByType( $pdcs ) { $pdcs_by_type = []; foreach( CategoryYearMonthDayTypes::all() as $Type ) { $pdcs_by_type[ $Type::PDC_TYPE ] = []; } foreach( $pdcs as $pdc ) { $type = $pdc->getType(); $pdcs_by_type[ $type ][] = $pdc; } return $pdcs_by_type; } /** * Discard all the PDCs that do not belong to a certain date * * @param $pdcs array * @param $date DateTime * @return array */ public static function filterByDate( $pdcs, DateTime $date ) { $y_m_d = $date->format( 'Y-m-d' ); return array_filter( $pdcs, function ( $pdc ) use ( $y_m_d ) { return $pdc->getStartDate()->format( 'Y-m-d' ) === $y_m_d; } ); } /** * Discard all the PDCs that are not multiple * * @param $pdcs array * @return $pdcs */ public static function filterNotMultiple( $pdcs ) { return array_filter( $pdcs, function ( $pdc ) { return ! $pdc->isMultiple(); } ); } /** * Get all the title subjects of the specified PDCs * * @param $pdcs array * @return array */ public static function titleSubjects( $pdcs ) { return array_map( function ( $pdc ) { return $pdc->getTitleSubject(); }, $pdcs ); } /** * Populate the specified PDCs that miss informations * * This method is intended to do less API requests as possible. * * @param $pdcs array * @return array */ public static function populateMissingInformations( $pdcs ) { self::populateMissingInformationsFromLastRevision( $pdcs ); } /** * Populate the specified PDCs with missing informations that can * be obtained fetching the last revision. * * This method is intended to do less API requests as possible. * * @param $pdcs */ private static function populateMissingInformationsFromLastRevision( $pdcs ) { if( ! $pdcs ) { return; } /* * fetch the last revision from all the PDCs * * @see https://it.wikipedia.org/w/api.php?action=help&modules=query%2Brevisions */ $query = Page::api()->createQuery( [ 'action' => 'query', 'titles' => self::titleSubjects( $pdcs ), 'prop' => 'revisions', 'rvprop' => [ 'content', 'timestamp', ], 'rvsection' => '0', ] ); // callback fired for every match between response pages and PDCs $matching_callback = function ( $page, $pdc ) { - if( isset( $page->revisions, $page->revisions[ 0 ] ) ) { + if( isset( $page->revisions[ 0 ] ) ) { // this is the only one $revision = $page->revisions[ 0 ]; // populate the subject themes $pdc->setSubjectThemesScrapingSubjectWikitext( $revision->{ '*' } ); // populate the lastedit date $lastedit_date = Page::createDateTimeFromString( $revision->timestamp ); $pdc->setLasteditDate( $lastedit_date ); } }; // callback that returns the PDC page title $pdc_page_title_callback = function ( $pdc ) { return $pdc->getTitleSubject(); }; // query continuation foreach( $query->getGenerator() as $response ) { // match page results and PDCs by title ( new PageMatcher( $response->query, $pdcs ) ) ->matchByTitle( $matching_callback, $pdc_page_title_callback ); } } }