diff --git a/includes/boz-mw b/includes/boz-mw index ec819b0..2d30a61 160000 --- a/includes/boz-mw +++ b/includes/boz-mw @@ -1 +1 @@ -Subproject commit ec819b05b1bed3e703eaa5e79228f818270d6aa0 +Subproject commit 2d30a611070734a32f6aa1c1f86a4e615665c5a9 diff --git a/includes/class-CategoryYearMonthDayType.php b/includes/class-CategoryYearMonthDayType.php index 91b51df..1038f02 100644 --- a/includes/class-CategoryYearMonthDayType.php +++ b/includes/class-CategoryYearMonthDayType.php @@ -1,165 +1,173 @@ . namespace itwikidelbot; use \cli\Log; /** * Abstraction of a daily category with a specified PDC type */ abstract class CategoryYearMonthDayType extends CategoryYearMonthDay { /** * Template name of this category * * @override CategoryTemplated::TEMPLATE_NAME */ const TEMPLATE_NAME = 'CATEGORY_DAY_PDCTYPE'; /** * PDC type e.g. 'consensuale' * * To be overrided */ const PDC_TYPE = 'EXAMPLE'; /** * Get the PDC type * * @return string e.g. 'consensuale' */ public function getPDCType() { return static::PDC_TYPE; } /** * Get template arguments * * @override CategoryTemplated::getTemplateArguments() */ public function getTemplateArguments() { $parent_arguments = parent::getTemplateArguments(); $parent_arguments[] = $this->getPDCType(); return $parent_arguments; } /** * Create a PDF of this type (from raw) * * @param $pdc_raw mixed Raw PDC data * @see PDC::createFromRaw() */ public function createPDCFromRaw( $pdc_raw ) { return PDC::createFromRaw( $this, $pdc_raw ); } /** * Fetch PDCs of this type * * @return array */ public function fetchPDCs() { $api = self::api()->createQuery( [ 'action' => 'query', // generator=categorymembers: get pages in category // gmtitle=: specify the category title // gmtype=page: get sub-pages // gmsort=timestamp: order by insertion date in that category // gmdir=asc: ascending order // https://it.wikipedia.org/w/api.php?action=help&modules=query%2Bcategorymembers // Note: Generator parameter names must be prefixed with a 'g' 'generator' => 'categorymembers', 'gcmtitle' => $this->getTitle(), // 'gcmtype' => 'page', // Note: Ignored when cmsort=timestamp is set. 'gcmnamespace' => 4, // Wikipedia 'gcmlimit' => 100, 'gcmsort' => 'timestamp', 'gcmdir' => 'asc', // // for each page load infos, categories and latest revision // https://it.wikipedia.org/w/api.php?action=help&modules=query%2Binfo // https://it.wikipedia.org/w/api.php?action=help&modules=query%2Bcategories // https://it.wikipedia.org/w/api.php?action=help&modules=query%2Brevisions // Note: probably the revisions parameter is unuseful for last update date (already provided by "touched"). // Note: revisions can be still useful to know the creation date 'prop' => [ 'info' , 'categories'/*, 'revisions'*/ ], // inprop=protecion: list of the protection level of each page // https://it.wikipedia.org/w/api.php?action=help&modules=query%2Bcategories 'inprop' => 'protection', // clprop=sortkey: adds the sortkey and sortkey prefix for the category // clprop=timestamp: adds the timestamp of when the page was included // https://it.wikipedia.org/w/api.php?action=help&modules=query%2Binfo 'clprop' => [ 'sortkey', 'timestamp' ], // rvprop=timestamp: timestamp of the revision // rvlimit=1: only 1 revision // rvdir=older: order by oldest // https://it.wikipedia.org/w/api.php?action=help&modules=query%2Brevisions // 'rvprop' => 'timestamp', // 'rvdir' => 'older', // 'rvlimit' => 1, ] ); $all = []; while( $api->hasNext() ) { $next = $api->fetchNext(); if( isset( $next->query->pages ) ) { foreach( $next->query->pages as $page ) { // multiple call will add more infos on the same page // https://www.mediawiki.org/wiki/API:Query#Generators_and_continuation // TODO: use batchcomplete for better memory usage $pageid = $page->pageid; if( isset( $all[ $pageid ] ) ) { foreach( $page as $property => $value ) { - $all[ $pageid ]->{$property} = $value; + if( isset( $all[ $pageid ]->{$property} ) && is_array( $all[ $pageid ]->{$property} ) ) { + // merge categories values + $all[ $pageid ]->{$property} = array_merge( + $all[ $pageid ]->{$property}, + $value + ); + } else { + $all[ $pageid ]->{$property} = $value; + } } } else { $all[ $pageid ] = $page; } } } } $pdcs = []; foreach( $all as $page ) { try { $pdcs[] = $this->createPDCFromRaw( $page ); } catch( PDCException $e ) { Log::warn( sprintf( "exception in PDC '%s': %s", $page->title, $e->getMessage() ) ); } } return $pdcs; } } diff --git a/includes/class-CategoryYearMonthDayTypeProlonged.php b/includes/class-CategoryYearMonthDayTypeProlonged.php index adf6e1c..aad523c 100644 --- a/includes/class-CategoryYearMonthDayTypeProlonged.php +++ b/includes/class-CategoryYearMonthDayTypeProlonged.php @@ -1,39 +1,39 @@ . namespace itwikidelbot; /** * Handle a daily prolonged PDC category * * e.g. https://it.wikipedia.org/wiki/Categoria:Cancellazioni_consensuali_prorogate_del_11_aprile_2018 */ class CategoryYearMonthDayTypeProlonged extends CategoryYearMonthDayType { /** * PDC type * * @override CategoryYearMonthDayType::PDC_TYPE */ - const PDC_TYPE = 'prorogate'; + const PDC_TYPE = 'consensuali prorogate'; } diff --git a/includes/class-CategoryYearMonthDayTypes.php b/includes/class-CategoryYearMonthDayTypes.php index d0727f5..3860191 100644 --- a/includes/class-CategoryYearMonthDayTypes.php +++ b/includes/class-CategoryYearMonthDayTypes.php @@ -1,122 +1,119 @@ . namespace itwikidelbot; use \cli\Log; /** * 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, ]; /** * 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( $id == '7156617' ) { - var_dump( $category, $pdc ); - } 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(); } }