diff --git a/includes/class-CategoryYearMonthDay.php b/includes/class-CategoryYearMonthDay.php index b14b686..4c29c22 100644 --- a/includes/class-CategoryYearMonthDay.php +++ b/includes/class-CategoryYearMonthDay.php @@ -1,179 +1,54 @@ . namespace itwikidelbot; /** * Handle a daily category that directly contains semplified ("semplificate") PDC pages * It contains other sub-categories. * * e.g. https://it.wikipedia.org/wiki/Categoria:Cancellazioni_del_19_febbraio_2018 */ class CategoryYearMonthDay extends PageYearMonthDay { /** * Template name * * @override CategoryTemplated::TEMPLATE_NAME */ const TEMPLATE_NAME = 'CATEGORY_DAY'; /** * Template arguments * * @override CategoryYearMonth::getTemplateArguments() */ public function getTemplateArguments() { - return array_merge( [ - ( new CategoryYearMonth( $this->getYear(), $this->getMonth() ) ) - ->getTemplatedTitle() - ], + return array_merge( + [ ( new CategoryYearMonth( $this->getYear(), $this->getMonth() ) ) + ->getTemplatedTitle() ], parent::getTemplateArguments() ); } - /** - * Operate on every children page - */ - public function operateEveryChildrenPage() { - - // Categories for PDCs in order of importance (I think) - $Categories = [ - CategoryYearMonthDayTypeVoting::class, - CategoryYearMonthDayTypeProlonged::class, - CategoryYearMonthDayTypeConsensual::class, - CategoryYearMonthDayTypeOrdinary::class, - CategoryYearMonthDayTypeSimple::class, - ]; - - $pages_by_type = []; - foreach( $Categories as $Category ) { - $category = new $Category( $this->getYear(), $this->getMonth(), $this->getDay() ); - - // fetch the pages in this category - $pages = $category->fetchChildrenPages(); - if( count( $pages ) ) { - $category->saveIfNotExists(); - } - - // sort by last update - usort( $pages, function ( $a, $b ) { - return $a->getDate() > $b->getDate(); - } ); - - $pages_by_type[ $Category ] = $pages; - } - -// PageYearMonthDayPDCsCount::createFromPagePDCs( $this, $pages_by_type ) -// ->save(); - - PageYearMonthDayPDCsLog::createFromPagePDCs( $this, $pages_by_type ) - ->save(); - } - - /** - * Fetch children PDC pages from this category - * - * @return array - */ - protected function fetchChildrenPages() { - $api = self::api()->setApiData( [ - '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 (completly unuseful) - // 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(), - 'gcmnamespace' => 4, // Wikipedia namespace (not category) -// 'gcmtype' => 'page', // Note: Ignored when cmsort=timestamp is set. -// '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 - // TODO: probably the revisions parameter is unuseful. Info already provides "touched" (sometimes ._.) - '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 - // https://it.wikipedia.org/w/api.php?action=help&modules=query%2Binfo - 'clprop' => 'sortkey', - - // timestamp of the latest revision - // https://it.wikipedia.org/w/api.php?action=help&modules=query%2Brevisions -// 'rvprop' => 'timestamp', - ] ); - - $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 - // Note: pages are not directly saved with their pageid to preserve their order - $pageid = $page->pageid; - if( isset( $all[ $pageid ] ) ) { - foreach( $page as $property => $value ) { - $all[ $pageid ]->{$property} = $value; - } - } else { - $all[ $pageid ] = $page; - } - } - } - } - - $all_pdc = []; - foreach( $all as $page ) { - $pdc = $this->createPDCFromRaw( $page ); - if( $pdc && $pdc->isValid() ) { - $all_pdc[] = $pdc; - } - } - return $all_pdc; - } - - /** - * Create a PDF from raw (of this type) - * - * @param $pdc mixed - */ - public function createPDCFromRaw( $pdc ) { - return PDC::createFromRaw( static::class, $pdc ); - } - } diff --git a/includes/class-CategoryYearMonthDayType.php b/includes/class-CategoryYearMonthDayType.php index f9de63c..c4a0e48 100644 --- a/includes/class-CategoryYearMonthDayType.php +++ b/includes/class-CategoryYearMonthDayType.php @@ -1,64 +1,156 @@ . namespace itwikidelbot; /** * 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( static::getPDCType(), $this->getDateTime(), $pdc_raw ); + } + + /** + * Fetch PDCs of this type + * + * @return array + */ + public function fetchPDCs() { + $api = self::api()->setApiData( [ + '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 (completly unuseful) + // 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. + '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 + // https://it.wikipedia.org/w/api.php?action=help&modules=query%2Binfo + 'clprop' => 'sortkey', + + // 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 + // Note: pages are not directly saved with their pageid to preserve their order + $pageid = $page->pageid; + if( isset( $all[ $pageid ] ) ) { + foreach( $page as $property => $value ) { + $all[ $pageid ]->{$property} = $value; + } + } else { + $all[ $pageid ] = $page; + } + } + } + } + + $pdcs = []; + foreach( $all as $page ) { + $pdc = $this->createPDCFromRaw( $page ); + if( $pdc ) { + $pdcs[] = $pdc; + } + } + return $pdcs; + } + } diff --git a/includes/class-CategoryYearMonthDayTypeSimple.php b/includes/class-CategoryYearMonthDayTypeSimple.php index d8677d6..ee2c3f9 100644 --- a/includes/class-CategoryYearMonthDayTypeSimple.php +++ b/includes/class-CategoryYearMonthDayTypeSimple.php @@ -1,62 +1,59 @@ . namespace itwikidelbot; /** * Handle a daily category of semplified PDCs * * Pratically, a semplified PDC category is the daily PDC category. * So the template of this class cames from CategoryYearMonthDay. * * e.g. https://it.wikipedia.org/wiki/Categoria:Cancellazioni_del_1_aprile_2017 */ class CategoryYearMonthDayTypeSimple extends CategoryYearMonthDayType { /** - * PDC type + * The same value for CategoryYearMonthDay::TEMPLATE_NAME. * - * @override CategoryYearMonthDayType::PDC_TYPE + * @see CategoryYearMonthDayType::TEMPLATE_NAME */ - const PDC_TYPE = 'semplificate'; + const TEMPLATE_NAME = 'CATEGORY_DAY'; /** - * Template name of this category + * PDC type * - * @override CategoryTemplated::getTemplateName() - * @see CategoryYearMonthDay::TEMPLATE_NAME + * @override CategoryYearMonthDayType::PDC_TYPE */ - public function getTemplateName() { - return CategoryYearMonthDay::TEMPLATE_NAME; - } + const PDC_TYPE = 'semplificate'; /** * Template arguments * * @override CategoryYearMonth::getTemplateArguments() */ public function getTemplateArguments() { return CategoryYearMonthDay::getTemplateArguments(); } } diff --git a/includes/class-CategoryYearMonthDayTypes.php b/includes/class-CategoryYearMonthDayTypes.php new file mode 100644 index 0000000..9c888d1 --- /dev/null +++ b/includes/class-CategoryYearMonthDayTypes.php @@ -0,0 +1,82 @@ +. + +namespace itwikidelbot; + +/** + * Handler of all the PDCs in this day of the year + */ +class CategoryYearMonthDayTypes extends CategoryYearMonthDay { + + /** + * Operate on every PDC type + */ + public function run() { + + // Categories for PDCs in order of importance (I think) + $Categories = [ + CategoryYearMonthDayTypeVoting ::class, + CategoryYearMonthDayTypeProlonged ::class, + CategoryYearMonthDayTypeConsensual::class, + CategoryYearMonthDayTypeOrdinary ::class, + CategoryYearMonthDayTypeSimple ::class, + ]; + + $pdcs_by_type = []; + foreach( $Categories as $Category ) { + $category = new $Category( $this->getYear(), $this->getMonth(), $this->getDay() ); + + $pdcs = $category->fetchPDCs(); + if( count( $pdcs ) ) { + $category->saveIfNotExists(); + } + + // sort PDCs by last update + usort( $pdcs, function ( $a, $b ) { + return $a->getLasteditDate() > $b->getLasteditDate(); + } ); + + $pdcs_by_type[ $Category::getPDCType() ] = $pdcs; + } + + $pdcs_by_type = self::filterPDCs( $pdcs_by_type ); + + PageYearMonthDayPDCsCount::createFromPagePDCs( $this->getDateTime(), $pages_by_type ) + ->save(); + + PageYearMonthDayPDCsLog::createFromPagePDCs( $this->getDateTime(), $pages_by_type ) + ->save(); + } + + /** + * Make a bit of consistence in the specified PDCs + * + * @param $pdcs_by_type array + * @return array + * @TODO + */ + private static function filterPDCs( $pdcs_by_type ) { + return $pdcs_by_type; + } + +}