diff --git a/config-example.php b/config-example.php index 4f9eb9e..270bc21 100644 --- a/config-example.php +++ b/config-example.php @@ -1,10 +1,10 @@ . namespace itwikidelbot; /** * Handle a yearly category * * e.g. https://it.wikipedia.org/wiki/Categoria:Cancellazioni_-_2017 */ -class CategoryYear extends CategoryTemplated { +class CategoryYear extends PageTemplated { /** * Template name of this category * * @override CategoryTemplated::TEMPLATE_NAME */ const TEMPLATE_NAME = 'CATEGORY_YEAR'; /** * Year * * @var int */ private $year; /** * Constructor * * @param $year int * @see CategoryTemplated::__construct() */ public function __construct( $year ) { $this->year = $year; parent::__construct(); } /** * Get the year * * @return int */ public function getYear() { return $this->year; } /** * Template arguments * * @override CategoryTemplated::getTemplateArguments() */ public function getTemplateArguments() { return [ $this->getYear() ]; } } diff --git a/includes/class-Category.php b/includes/class-Page.php similarity index 76% rename from includes/class-Category.php rename to includes/class-Page.php index a1bb6c9..0d649c4 100644 --- a/includes/class-Category.php +++ b/includes/class-Page.php @@ -1,162 +1,165 @@ . namespace itwikidelbot; use \wm\WikipediaIt; use \mw\Tokens; /** - * Handle a category + * Handle a page */ -class Category { +class Page { /** * Do nothing * * @var bool */ public static $PORCELAIN = false; /** * Debug mode * * @var bool */ public static $INSPECT = false; /** - * @var string Category title with prefix + * @var string Page title with prefix */ private $title; /** - * Construct a Category + * Construct a Page * - * @param $title Category title with its prefix + * @param $title Page title with its prefix */ public function __construct( $title ) { $this->title = $title; } /** - * Get the category title with its prefix + * Get the page title with its prefix * * @return string */ public function getTitle() { return $this->title; } /** - * Save this category + * Save this page * - * @param $content string Category content + * @param $content string Page content * @param $summary string Edit summary * @return bool|mixed False if not created */ public function saveByContentSummary( $content, $summary ) { return static::saveByTitleContentSummary( $this->getTitle(), $content, $summary ); } /** - * Save this category if it does not exist + * Save this page if it does not exist * - * @param $content string Category content + * @param $content string Page content * @param $summary string Edit summary * @return bool|mixed False if not created */ public function saveByContentSummaryIfNotExists( $content, $summary ) { return self::saveByTitleContentSummaryIfNotExists( $this->getTitle(), $content, $summary ); } /** - * Check if this category exists + * Check if this page exists * * @return bool */ public function exists() { return static::existsByTitle( $this->getTitle() ); } /** - * Save a category + * Save a page * - * @param $title string Category name with prefix - * @param $content string Category content + * @param $title string Page name with prefix + * @param $content string Page content * @param $summary string Edit summary * @return mixed Response */ public static function saveByTitleContentSummary( $title, $content, $summary ) { $api = self::api(); $args = [ 'action' => 'edit', 'title' => $title, 'text' => $content, 'summary' => $summary, 'token' => $api->login()->getToken( Tokens::CSRF ), 'bot' => 1, ]; if( self::$INSPECT ) { print_r( $args ); + $handle = fopen( 'php://stdin', 'r' ); + $line = fgets( $handle ); + fclose( $handle ); } if( ! self::$PORCELAIN ) { return $api->post( $args ); } } /** - * Save a category if it does not exist + * Save a page if it does not exist * - * @param $title string Category name with prefix - * @param $content string Category content + * @param $title string Page name with prefix + * @param $content string Page content * @param $summary string Edit summary * @return bool|mixed False if not created */ public static function saveByTitleContentSummaryIfNotExists( $title, $content, $summary ) { if( ! static::existsByTitle( $title ) ) { return static::saveByTitleContentSummary( $title, $content, $summary ); } return false; } /** - * Check if a category title exists + * Check if a page title exists * - * @param $title string Category name with prefix + * @param $title string Page name with prefix * @return bool */ public static function existsByTitle( $title ) { - $categoryinfo = self::api()->fetch( [ + $result = self::api()->fetch( [ 'action' => 'query', - 'prop' => 'categoryinfo', + 'prop' => 'info', 'titles' => $title, ] ); - foreach( $categoryinfo->query->pages as $pageid => $page ) { + foreach( $result->query->pages as $pageid => $page ) { return ! isset( $page->missing ); } return false; } /** - * Get the API related to this category + * Get the API related to this page * * @return mw\API */ protected static function api() { return WikipediaIt::getInstance(); } } diff --git a/includes/class-CategoryTemplated.php b/includes/class-PageTemplated.php similarity index 86% rename from includes/class-CategoryTemplated.php rename to includes/class-PageTemplated.php index 309a7ab..2753882 100644 --- a/includes/class-CategoryTemplated.php +++ b/includes/class-PageTemplated.php @@ -1,128 +1,128 @@ . namespace itwikidelbot; /** - * Handle a generic category associated to a template + * Handle a generic page associated to a template */ -abstract class CategoryTemplated extends Category { +abstract class PageTemplated extends Page { /** - * Template name of this category + * Template name of this page * * To be overrided */ const TEMPLATE_NAME = 'EXAMPLE_NAME'; /** * Constructor * * @param $args Template arguments */ public function __construct() { parent::__construct( $this->getTemplatedTitle() ); } /** * Get the name of the template * * @return string */ public function getTemplateName() { return static::TEMPLATE_NAME; } /** * Get the name of the template for the page title * * @return string */ public function getTemplateTitleName() { return $this->getTemplateName() . '.title'; } /** * Get the name of the template for the summary * * @return string */ public function getTemplateSummaryName() { return $this->getTemplateName() . '.summary'; } /** * Get the name of the template for the content */ public function getTemplateContentName() { return $this->getTemplateName() . '.content'; } /** * Get the template arguments * * @return array */ abstract public function getTemplateArguments(); /** - * Get the edit summary for this category from its template + * Get the edit summary for this page from its template * * @return string */ public function getTemplatedSummary() { return Template::get( $this->getTemplateSummaryName(), $this->getTemplateArguments() ); } /** * Get the page title from its template * * @return string */ public function getTemplatedTitle() { return Template::get( $this->getTemplateTitleName(), $this->getTemplateArguments() ); } /** - * Get the page content for this category from its template + * Get the page content for this page from its template * * @return string */ public function getTemplatedContent() { return Template::get( $this->getTemplateContentName(), $this->getTemplateArguments() ); } /** - * Save this category from the content of its template + * Save this page from the content of its template * * @return mixed */ public function save() { return $this->saveByContentSummary( $this->getTemplatedContent(), $this->getTemplatedSummary() ); } /** - * Save this category from the content of its template if it does not exist + * Save this page from the content of its template if it does not exist * * @return self */ public function saveIfNotExists() { if( ! $this->exists() ) { $this->save(); } return $this; } }