Page MenuHomeGitPull.it

No OneTemporary

Authored By
Unknown
Size
7 KB
Referenced Files
None
Subscribers
None
diff --git a/bot.php b/bot.php
index dc1c477..44f87aa 100755
--- a/bot.php
+++ b/bot.php
@@ -1,40 +1,43 @@
#!/usr/bin/php
<?php
# Copyright (C) 2018 Valerio Bozzolan
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
namespace itwikidelbot;
// autoload classes
require __DIR__ . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'autoload.php';
use cli\Log;
// load credentials
require __DIR__ . DIRECTORY_SEPARATOR . 'config.php';
+// the bot must be in sync with the italian community
+date_default_timezone_set( 'Europe/Rome' );
+
// the first argument is the days to go backward
$DAYS = isset( $argv[ 1 ] ) ? (int) $argv[ 1 ] : 1;
// the second argument is a 'YYYY-MM-DD' date
$DATE = isset( $argv[ 2 ] ) ? $argv[ 2 ] : 'now';
Log::info( 'start' );
$bot = Bot::createFromString( $DATE );
for( $i = 0; $i < $DAYS; $i++ ) {
$bot->run()->previousDay();
}
Log::info( 'end' );
diff --git a/config-example.php b/config-example.php
index 3e14552..03bc0ac 100644
--- a/config-example.php
+++ b/config-example.php
@@ -1,15 +1,12 @@
<?php
// Fill this file and save-as "config.php"
// Decomment the next line to see more verbose messages
// cli\Log::$DEBUG = true;
// Decomment the next line to inspect content before saving
// itwikidelbot\Page::$ASK_BEFORE_SAVING = true;
-// Decomment and fill the next line with your time zone
-// itwikidelbot\Page::$COMMUNITY_TIMEZONE = 'Europe/Rome';
-
// Fill the next lines with your Wikimedia bot credentials
mw\API::$DEFAULT_USERNAME = 'Foo';
mw\API::$DEFAULT_PASSWORD = 'Bar';
diff --git a/includes/class-Page.php b/includes/class-Page.php
index 7d06595..c32d069 100644
--- a/includes/class-Page.php
+++ b/includes/class-Page.php
@@ -1,228 +1,228 @@
<?php
# it.wiki deletion bot in PHP
# Copyright (C) 2018 Valerio Bozzolan
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
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
*/
- public static $COMMUNITY_TIMEZONE = 'Europe/Rome';
+ 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;
/**
* 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
*/
public function saveByContentSummary( $content, $summary ) {
return static::saveByTitleContentSummary( $this->getTitle(), $content, $summary );
}
/**
* 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 );
}
/**
* Check if this page exists
*
* @return bool
*/
public function exists() {
return static::existsByTitle( $this->getTitle() );
}
/**
* Save a page
*
* @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::$ASK_BEFORE_SAVING ) {
print_r( $args );
if( 'y' !== Input::yesNoQuestion( "Save?" ) ) {
$save = false;
}
}
Log::info( "writing [[$title]]" );
return $api->post( $args );
}
/**
* 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 ) {
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
*/
protected 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 ) );
+ ->setTimezone( new DateTimeZone( self::COMMUNITY_TIMEZONE ) );
}
}

File Metadata

Mime Type
text/x-diff
Expires
Thu, Nov 19, 06:06 (11 h, 29 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2265078
Default Alt Text
(7 KB)

Event Timeline