diff --git a/includes/class-Bot.php b/includes/class-Bot.php index ed1a5f3..341ee87 100644 --- a/includes/class-Bot.php +++ b/includes/class-Bot.php @@ -1,266 +1,289 @@ . namespace itwikidelbot; use cli\Log; use DateTime; use DateInterval; use Exception; /** * The Bot class helps in running the bot */ class Bot { /** * The date * * @var DateTime */ private $dateTime; /** * A cache of already done dates. * It's an array of booleans like [year][month] = true. * * @var array */ private $cache = []; /** * Construct * * @param $date DateTime */ public function __construct( DateTime $date = null ) { if( ! $date ) { $date = new DateTime(); } $this->setDate( $date ); } /** * Static construct * * @param $date string */ public static function createFromString( $date = 'now' ) { return new self( new DateTime( $date ) ); } /** * Static construct * * @param $y int Year * @param $m int Month 1-12 * @param $d int Day 1-31 */ public static function createFromYearMonthDay( $y, $m, $d ) { return new self( DateTime::createFromFormat( "Y m d", "$y $m $d") ); } /** * Get the date * * @return DateTime */ public function getDate() { return $this->dateTime; } /** * Set the date * * @param $date DateTime * @return self */ public function setDate( DateTime $date ) { $this->dateTime = $date; } /** * Add a day * * @return self */ public function nextDay() { return $this->addDays( 1 ); } /** * Add a day * * @return self */ public function previousDay() { return $this->subDays( 1 ); } /** * Add a certain number of days * * @param $days int * @return self */ public function addDays( $days ) { $this->getDate()->add( new DateInterval( sprintf( 'P%dD', $days ) ) ); return $this; } /** * Subtract a certain number of days * * @param $days int * @return self */ public function subDays( $days ) { $this->getDate()->sub( new DateInterval( sprintf( 'P%dD', $days ) ) ); return $this; } /** * Fetch the last bot edit on the next page * * @return DateTime|null */ public function fetchLastedit() { $lastedit = null; try { $lastedit = PageYearMonthDayPDCsCount::createFromDateTime( $this->getDate() ) ->fetchLasteditDate(); } catch( Exception $e ) { // Unexisting. OK. } return $lastedit; } /** * Is the last edit date older than some seconds? (on the next page) * * @param $seconds int * @return bool */ public function isLasteditOlderThanSeconds( $seconds ) { $lastedit = $this->fetchLastedit(); if( ! $lastedit ) { return true; // Unexisting. OK. } return time() - $lastedit->format( 'U' ) > $seconds; } /** * Is the last edit date older than some minutes? (on the next page) * * @param $minutes int * @return bool */ public function isLasteditOlderThanMinutes( $minutes ) { return $this->isLasteditOlderThanSeconds( 60 * $minutes ); } /** * Run the bot at the internal date * * @TODO: do not repeat twice the same yearly and montly categories * @return self */ public function run() { + + $cache = & $this->cache; + // date initialization $date = $this->getDate(); $year = $date->format( 'Y' ); $month = $date->format( 'n' ); // 1-12 $day = $date->format( 'j' ); + // yearly category + $y_category = new CategoryYear( $year ); + + // monthly category + $m_category = new CategoryYearMonth( $year, $month ); + + // create all the PDC types + $category_types = []; + foreach( CategoryYearMonthDayTypes::all() as $CategoryType ) { + $category_types[] = new $CategoryType( $year, $month, $day ); + } + + // all the categories + $all_categories = $category_types; + if( ! isset( $cache[ $month ] ) ) { + $all_categories[] = $m_category; + } + if( ! isset( $cache[ $year ] ) ) { + $all_categories[] = $y_category; + } + + // check in bulk if the categories already exist + Pages::populateWheneverTheyExist( $all_categories ); + // create the yearly category (once) - $cache = & $this->cache; if( ! isset( $cache[ $year ] ) ) { $cache[ $year ] = []; - ( new CategoryYear( $year ) ) - ->saveIfNotExists(); + $y_category->saveIfNotExists(); } // create the monthly category (once) $cache = & $cache[ $year ]; if( ! isset( $cache[ $month ] ) ) { $cache[ $month ] = true; - ( new CategoryYearMonth( $year, $month ) ) - ->saveIfNotExists(); + $m_category->saveIfNotExists(); } Log::info( "work on $year/$month/$day" ); // PDCs indexed by page ID $pdcs = []; // handle every PDC type - foreach( CategoryYearMonthDayTypes::all() as $CategoryType ) { + foreach( $category_types as $category_type ) { // fetch PDCs from this type - $category_type = new $CategoryType( $year, $month, $day ); $category_type_pdcs = $category_type->fetchPDCs(); // save the specific daily category type only if it's not empty // ...or only if it's the main category (that can be without pages) if( $category_type_pdcs || get_class( $category_type ) === CategoryYearMonthDay::class ) { $category_type->saveIfNotExists(); } // merge the same PDCs into one foreach( $category_type_pdcs as $category_type_pdc ) { $id = $category_type_pdc->getID(); if( isset( $pdcs[ $id ] ) ) { $pdcs[ $id ]->merge( $category_type_pdc ); } else { $pdcs[ $id ] = $category_type_pdc; } } } // select only the PDCs that belong to this date $pdcs = PDCs::filterByDate( $pdcs, $date ); Log::info( sprintf( sprintf( "found %d PDCs", count( $pdcs ) ) ) ); // populate missing informations PDCs::populateMissingInformations( $pdcs ); // sort by creation date PDCs::sortByCreationDate( $pdcs ); // index then by their PDC_TYPE $pdcs_by_type = PDCs::indexByType( $pdcs ); // save the counting page PageYearMonthDayPDCsCount::createFromDateTimePDCs( $this->getDate(), $pdcs_by_type ) ->save(); // save the log page PageYearMonthDayPDCsLog::createFromDateTimePDCs( $this->getDate(), $pdcs_by_type ) ->save(); return $this; } } diff --git a/includes/class-Pages.php b/includes/class-Pages.php new file mode 100644 index 0000000..ee5d748 --- /dev/null +++ b/includes/class-Pages.php @@ -0,0 +1,74 @@ +. + +namespace itwikidelbot; + +use mw\API\PageMatcher; + +/** + * Handle multiple pages + */ +class Pages { + + /** + * Get page titles from pages + * + * @param $pages array + * @return array + */ + public static function titles( $pages ) { + return array_map( function ( $page ) { + return $page->getTitle(); + }, $pages ); + } + + /* + * Check if some pages exist + * + * This method is intended to do less API requests as possible. + * + * @param $pages array + */ + public static function populateWheneverTheyExist( $pages ) { + + // API query to check if these pages exist + $query = Page::api()->createQuery( [ + 'action' => 'query', + 'prop' => 'info', + 'titles' => self::titles( $pages ), + ] ); + + // callback to retrieve the page title + $page_title_callback = function ( $page ) { + return $page->getTitle(); + }; + + // callback fired for every match between response pages and my pages + $matching_callback = function ( $response_page, $my_page ) { + $my_page->setIfExists( + ! isset( $response_page->missing ) + ); + }; + + // query continuation + foreach( $query->getGenerator() as $response ) { + // match response pages with my pages + ( new PageMatcher( $response->query, $pages ) ) + ->matchByTitle( $matching_callback, $page_title_callback ); + } + } + +}