diff --git a/includes/class-Page.php b/includes/class-Page.php index 03e2232..a10a758 100644 --- a/includes/class-Page.php +++ b/includes/class-Page.php @@ -1,230 +1,216 @@ . namespace itwikidelbot; use DateTime; use DateTimeZone; use cli\Input; use cli\Log; use wm\WikipediaIt; use mw\Tokens; /** * Handle a page */ class Page { /** * Time zone of Italian Wikipedia community. * * @var string */ const COMMUNITY_TIMEZONE = 'Europe/Rome'; /** * Enable this flag to ask for every changes * * @var bool */ public static $ASK_BEFORE_SAVING = false; /** * @var string Page title with prefix */ private $title; + /** + * Cache for the existing status of this page + * + * @var bool + */ + private $exists; + /** * Construct a Page * * @param $title Page title with its prefix */ public function __construct( $title ) { $this->title = $title; } /** * Get the page title with its prefix * * @return string */ public function getTitle() { return $this->title; } /** * Save this page * * @param $content string Page content * @param $summary string Edit summary - * @return bool|mixed False if not created + * @return mixed */ public function saveByContentSummary( $content, $summary ) { - return static::saveByTitleContentSummary( $this->getTitle(), $content, $summary ); + $title = $this->getTitle(); + $api = self::api(); + $args = [ + 'action' => 'edit', + 'title' => $title, + 'text' => $content, + 'summary' => $summary, + 'token' => $api->login()->getToken( Tokens::CSRF ), + 'bot' => 1, + ]; + + if( self::$ASK_BEFORE_SAVING ) { + print_r( $args ); + if( 'y' !== Input::yesNoQuestion( "Save?" ) ) { + return false; + } + } + + Log::info( "writing [[$title]]" ); + + return $api->post( $args ); } /** * Save this page if it does not exist * * @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 ); + if( ! $this->exists() ) { + return $this->saveByContentSummary( $content, $summary ); + } + return false; } /** - * Check if this page exists + * Set internally if this page exists * - * @return bool + * @param $exists bool + * @return bool|mixed False if not created */ - public function exists() { - return static::existsByTitle( $this->getTitle() ); + public function setIfExists( $exists ) { + $this->exists = $exists; } /** - * Save a page + * Check if this page exists (the result is cached) * - * @param $title string Page name with prefix - * @param $content string Page content - * @param $summary string Edit summary - * @return mixed Response + * @return bool */ - 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::$ASK_BEFORE_SAVING ) { - print_r( $args ); - if( 'y' !== Input::yesNoQuestion( "Save?" ) ) { - return false; + public function exists() { + if( null === $this->exists ) { + $result = self::api()->fetch( [ + 'action' => 'query', + 'prop' => 'info', + 'titles' => $this->getTitle(), + ] ); + foreach( $result->query->pages as $pageid => $page ) { + $this->exists = ! isset( $page->missing ); } } - - Log::info( "writing [[$title]]" ); - - return $api->post( $args ); + return $this->exists; } /** * Fetch the first revision date by direction * * @param $dir string direction * @return DateTime */ public function fetchFirstRevisionDateByDirection( $direction ) { $response = self::api()->fetch( [ 'action' => 'query', 'titles' => $this->getTitle(), 'prop' => 'revisions', 'rvprop' => [ 'timestamp' ], 'rvlimit' => 1, 'rvdir' => $direction, ] ); foreach( $response->query->pages as $page ) { if( isset( $page->revisions ) ) { foreach( $page->revisions as $revision ) { return self::createDateTimeFromString( $revision->timestamp ); } } } throw new \Exception( 'unable to fetch the creation date' ); } /** * Fetch the creation date of this page * * @return DateTime */ public function fetchCreationDate() { return $this->fetchFirstRevisionDateByDirection( 'newer' ); } /** * Fetch the creation date of this page * * @return DateTime */ public function fetchLasteditDate() { return $this->fetchFirstRevisionDateByDirection( 'older' ); } - /** - * Save a page if it does not exist - * - * @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 page title exists - * - * @param $title string Page name with prefix - * @return bool - */ - public static function existsByTitle( $title ) { - $result = self::api()->fetch( [ - 'action' => 'query', - 'prop' => 'info', - 'titles' => $title, - ] ); - foreach( $result->query->pages as $pageid => $page ) { - return ! isset( $page->missing ); - } - return false; - } - /** * Get the API related to this page * * @return mw\API */ public static function api() { return WikipediaIt::getInstance(); } /** * Create a DateTime object from a MediaWiki formatted date * * MediaWiki dates are formatted following the ISO8601 standard * and you may want to specify your community timezone. * * @param $datetime string * @return DateTime */ public static function createDateTimeFromString( $datetime ) { return DateTime::createFromFormat( DateTime::ISO8601, $datetime ) ->setTimezone( new DateTimeZone( self::COMMUNITY_TIMEZONE ) ); } } diff --git a/includes/class-PageTemplated.php b/includes/class-PageTemplated.php index 07b8915..7c9ff59 100644 --- a/includes/class-PageTemplated.php +++ b/includes/class-PageTemplated.php @@ -1,128 +1,125 @@ . namespace itwikidelbot; /** * Handle a generic page associated to a template */ abstract class PageTemplated extends Page { /** * 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 static function getTemplateName() { return static::TEMPLATE_NAME; } /** * Get the name of the template for the page title * * @return string */ public static function getTemplateTitleName() { return static::getTemplateName() . '.title'; } /** * Get the name of the template for the summary * * @return string */ public static function getTemplateSummaryName() { return static::getTemplateName() . '.summary'; } /** * Get the name of the template for the content */ public static function getTemplateContentName() { return static::getTemplateName() . '.content'; } /** * Get the template arguments * * @return array */ public abstract function getTemplateArguments(); /** * Get the edit summary for this page from its template * * @return string */ public function getTemplatedSummary() { return Template::get( static::getTemplateSummaryName(), $this->getTemplateArguments() ); } /** * Get the page title from its template * * @return string */ public function getTemplatedTitle() { return Template::get( static::getTemplateTitleName(), $this->getTemplateArguments() ); } /** * Get the page content for this page from its template * * @return string */ public function getTemplatedContent() { return Template::get( static::getTemplateContentName(), $this->getTemplateArguments() ); } /** * Save this page from the content of its template * * @return mixed */ public function save() { return $this->saveByContentSummary( $this->getTemplatedContent(), $this->getTemplatedSummary() ); } /** * Save this page from the content of its template if it does not exist * - * @return self + * @return mixed */ public function saveIfNotExists() { - if( ! $this->exists() ) { - $this->save(); - } - return $this; + return $this->saveByContentSummaryIfNotExists( $this->getTemplatedContent(), $this->getTemplatedSummary() ); } }