diff --git a/bot.php b/bot.php index e7d3a98..c6632b9 100755 --- a/bot.php +++ b/bot.php @@ -1,38 +1,39 @@ #!/usr/bin/php . namespace itwikidelbot; // autoload classes require __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'autoload.php'; // load credentials require __DIR__ . DIRECTORY_SEPARATOR . 'config.php'; // create the yearly category $year = date( 'Y' ); ( new CategoryYear( $year ) ) ->saveIfNotExists(); // create the monthly category $month = date( 'n' ); // 1-12 ( new CategoryYearMonth( $year, $month ) ) ->saveIfNotExists(); // create the daily category $day = date( 'j' ); ( new CategoryYearMonthDay( $year, $month, $day ) ) - ->saveIfNotExists(); + ->saveIfNotExists() + ->operateEveryChildrenPage(); diff --git a/includes/class-CategoryYearMonthDay.php b/includes/class-CategoryYearMonthDay.php index 98a434c..ba8756a 100644 --- a/includes/class-CategoryYearMonthDay.php +++ b/includes/class-CategoryYearMonthDay.php @@ -1,54 +1,178 @@ . namespace itwikidelbot; /** * Handle a daily category that contains semplified PDC pages * * 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() ], 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, + self::class, // semplified + ]; + + $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-PDC.php b/includes/class-PDC.php new file mode 100644 index 0000000..17e796f --- /dev/null +++ b/includes/class-PDC.php @@ -0,0 +1,169 @@ +. + +namespace itwikidelbot; + +use DateTime; + +/** + * Handle a "Procedura di cancellazione" + */ +class PDC extends Page { + + /** + * Prefix of every PDC + */ + const PREFIX = 'Wikipedia:Pagine da cancellare/'; + + /** + * Prefix of every multiple PDC + */ + const PREFIX_MULTIPLE = self::PREFIX . 'multiple/'; + + /** + * PDC type + * + * @var class + */ + private $type; + + /** + * Page id + * + * @var int + */ + private $id; + + /** + * Page title with prefix + * + * @var string + */ + private $title; + + /** + * Page length in bytes + * + * @var int + */ + private $length; + + /** + * Page date + * + * @var date + */ + private $date; + + /** + * Constructor + * + * @param $id Page id + * @param $title string Page title + * @param $length Page length + * @param $date Page last update + * @see Page::__construct() + */ + public function __construct( $type, $id, $title, $length, $date ) { + $this->type = $type; + $this->id = $id; + $this->title = $title; + $this->length = $length; + $this->date = $date; + parent::__construct( $title ); + } + + /** + * Statical constructor + * + * @param $type string Specified PDC class name + * @param $page mixed|null + */ + public static function createFromRaw( $type, $page ) { + if( ! isset( $page->touched, $page->pageid, $page->title, $page->length ) ) { + throw new \Exception( 'invalid PDC' ); + } + return new self( + $type, + $page->pageid, + $page->title, + $page->length, + DateTime::createFromFormat( DateTime::ISO8601, $page->touched ) // e.g. 2018-04-22T14:07:49Z + ); + } + + /** + * Get the page length + * + * @return int + */ + public function getLength() { + return $this->length; + } + + /** + * Get the latest update date + * + * @return DateTime + */ + public function getDate() { + return $this->date; + } + + /** + * Get the PDC type + * + * @return string e.g. 'consensuale' + */ + public function getType() { + return $this->type::getPDCType(); + } + + /** + * Check if this is a valid PDC + * + * @return bool + */ + public function isValid() { + return $this->titlehasPrefix( self::PREFIX ); + } + + /** + * Check if this PDC involves multiple PDCs + * + * @return bool + */ + public function isMultiple() { + return $this->titleHasPrefix( self::PREFIX_MULTIPLE ); + } + + /** + * Check if the title has a certain prefix + * + * @param $prefix string + * @return bool + */ + private function titleHasPrefix( $prefix ) { + return 0 === strpos( $this->getTitle(), $prefix ); + } +} diff --git a/includes/class-PageYearMonthDayPDCs.php b/includes/class-PageYearMonthDayPDCs.php new file mode 100644 index 0000000..ab8a43b --- /dev/null +++ b/includes/class-PageYearMonthDayPDCs.php @@ -0,0 +1,65 @@ +. + +namespace itwikidelbot; + +/** + * Abstraction of a page containing PDCs of a day + */ +abstract class PageYearMonthDayPDCs extends PageYearMonthDay { + + /** + * PDCs indexed by type + * + * @var array + */ + private $pdcsByType; + + /** + * Constructor + * + * @param $year int + * @param $month int 1-12 + * @param $day int 1-31 + * @param $pdcs_by_type array PDCs by type + * @see PageYearMonthDay::__construct() + */ + public function __construct( $year, $month, $day, $pdcs_by_type ) { + $this->pdcsByType = $pdcs_by_type; + parent::__construct( $year, $month, $day ); + } + + /** + * Static constructor from a PageYearMonthDay object + * + * @param $page PageYearMonthDay + * @param $pdcs PDCs by type + * @return self + */ + public static function createFromPagePDCs( PageYearMonthDay $page, $pdcs_by_type ) { + return new static( $page->getYear(), $page->getMonth(), $page->getDay(), $pdcs_by_type ); + } + + /** + * Get the PDCs indexed by type + * + * @return array + */ + public function getPDCsByType() { + return $this->pdcsByType; + } + +} diff --git a/bot.php b/includes/class-PageYearMonthDayPDCsCount.php old mode 100755 new mode 100644 similarity index 56% copy from bot.php copy to includes/class-PageYearMonthDayPDCsCount.php index e7d3a98..bab5642 --- a/bot.php +++ b/includes/class-PageYearMonthDayPDCsCount.php @@ -1,38 +1,31 @@ -#!/usr/bin/php . namespace itwikidelbot; -// autoload classes -require __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'autoload.php'; +/** + * Abstraction of a page containing PDCs' log of the day + */ +class PageYearMonthDayPDCsCount extends PageYearMonthDayPDCs { -// load credentials -require __DIR__ . DIRECTORY_SEPARATOR . 'config.php'; + /** + * Template name of this page + * + * @override CategoryTemplated::TEMPLATE_NAME + */ + const TEMPLATE_NAME = 'PAGE_COUNT'; -// create the yearly category -$year = date( 'Y' ); -( new CategoryYear( $year ) ) - ->saveIfNotExists(); - -// create the monthly category -$month = date( 'n' ); // 1-12 -( new CategoryYearMonth( $year, $month ) ) - ->saveIfNotExists(); - -// create the daily category -$day = date( 'j' ); -( new CategoryYearMonthDay( $year, $month, $day ) ) - ->saveIfNotExists(); +} diff --git a/includes/class-PageYearMonthDayPDCsLog.php b/includes/class-PageYearMonthDayPDCsLog.php new file mode 100644 index 0000000..1900b89 --- /dev/null +++ b/includes/class-PageYearMonthDayPDCsLog.php @@ -0,0 +1,69 @@ +. + +namespace itwikidelbot; + +/** + * Abstraction of a page containing PDCs daily log + * + * e.g. https://it.wikipedia.org/wiki/Wikipedia:Pagine_da_cancellare/Conta/2017_aprile_20 + */ +class PageYearMonthDayPDCsLog extends PageYearMonthDayPDCs { + + /** + * Template name of this page + * + * @override CategoryTemplated::TEMPLATE_NAME + */ + const TEMPLATE_NAME = 'PAGE_LOG'; + + /** + * Get the template arguments + * + * @override PageTemplated::getTemplateArguments() + * @return array + */ + public function getTemplateArguments() { + $args = parent::getTemplateArguments(); + + $sections = []; + foreach( $this->getPDCsByType() as $pdcs ) { + if( $pdcs ) { + $type = $pdcs[ 0 ]->getType(); + + // call the entry template for each PDC + $entries = []; + foreach( $pdcs as $pdc ) { + $entries[] = Template::get( self::TEMPLATE_NAME . '.entry', [ + $pdc->getTitle() + ] ); + } + + // call the section template for each PDC type + $entries_txt = implode( "\n", $entries ); + $sections[] = Template::get( self::TEMPLATE_NAME . '.section', [ + $pdc->getType(), + $entries_txt + ] ); + } + } + + $args[] = implode( "\n", $sections ); + + return $args; + } + +} diff --git a/templates/PAGE_LOG.content.tpl b/templates/PAGE_LOG.content.tpl new file mode 100644 index 0000000..ca28fcc --- /dev/null +++ b/templates/PAGE_LOG.content.tpl @@ -0,0 +1,32 @@ + +This is the template for the content of a daily log page. + +Placeholders: + %1$d: year + %2$d: month 1-12 + %2$02d: month 01-12 + %3$s: month name + %4$d: day 1-31 + %4$02d: day 01-31 + %5$s: PDC informations + They are generated looping through the "PAGE_LOG.section.tpl" template + + +{{Paginecancellare}} +== 20 aprile == +{{Wikipedia:Pagine da cancellare/Conta/2017 aprile 20}} + + +{{Wikipedia:Pagine da cancellare/ASD}} +{{Wikipedia:Pagine da cancellare/DSA}} + + +{{Wikipedia:Pagine da cancellare/GGH}} +{{Wikipedia:Pagine da cancellare/HGG}} + + +{{Paginecancellare}} +== %4$d %3$s == +{{Wikipedia:Pagine da cancellare/Conta/%1$d %3$s %4$d}} + +%5$s diff --git a/templates/PAGE_LOG.entry.tpl b/templates/PAGE_LOG.entry.tpl new file mode 100644 index 0000000..b1cf62f --- /dev/null +++ b/templates/PAGE_LOG.entry.tpl @@ -0,0 +1,13 @@ + +This is the template for an entry of the daily log page. + +This template is called multiple time from the "PAGE_LOG.section.tpl". + +Placeholders: + %1$s: page title + + +{{Wikipedia:Pagine da cancellare/ASD}} + + +{{%1$s}} diff --git a/templates/PAGE_LOG.section.tpl b/templates/PAGE_LOG.section.tpl new file mode 100644 index 0000000..4a05492 --- /dev/null +++ b/templates/PAGE_LOG.section.tpl @@ -0,0 +1,18 @@ + +This is the template for a section of the daily log page. + +This template will call multiple times the "PAGE_LOG.entry.tpl" template. + +Placeholders: + %1$s: PDC type e.g. 'con votazione' + %2$s: PDC entries + + + +{{Wikipedia:Pagine da cancellare/ASD}} +{{Wikipedia:Pagine da cancellare/DSA}} +{{Wikipedia:Pagine da cancellare/ASD}} + + + +%2$s diff --git a/templates/PAGE_LOG.summary.tpl b/templates/PAGE_LOG.summary.tpl new file mode 100644 index 0000000..05a61c5 --- /dev/null +++ b/templates/PAGE_LOG.summary.tpl @@ -0,0 +1,16 @@ + +This is the template for the edit summary of a daily log page. + +Placeholders: + %1$d: year + %2$d: month 1-12 + %2$02d: month 01-12 + %3$s: month name + %4$d: day 1-31 + %4$02d: day 01-31 + + +Bot: aggiornamento pagina di servizio giornaliera per il log del 19 aprile 2018 + + +Bot: aggiornamento pagina di servizio giornaliera per il log del %4$d %3$s %1$d diff --git a/templates/PAGE_LOG.title.tpl b/templates/PAGE_LOG.title.tpl new file mode 100644 index 0000000..06d2d49 --- /dev/null +++ b/templates/PAGE_LOG.title.tpl @@ -0,0 +1,16 @@ + +This is the template for the title of a daily log page. + +Placeholders: + %1$d: year + %2$d: month 1-12 + %2$02d: month 01-12 + %3$s: month name + %4$d: day 1-31 + %4$02d: day 01-31 + + +Wikipedia:Pagine da cancellare/Log/2018 aprile 20 + + +Wikipedia:Pagine da cancellare/Log/%1$d %3$s %4$d