Passed
Branch master (91bf77)
by Daimona
02:04 queued 27s
created

anonymous()

Size

Total Lines 2
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
nc 1
nop 4
dl 0
loc 2
c 0
b 0
f 0
1
<?php declare( strict_types=1 );
2
/**
3
 * Entry point for the bot, called by CLI
4
 */
5
6
require __DIR__ . '/vendor/autoload.php';
7
8
use BotRiconferme\Bot;
9
use BotRiconferme\CLI;
10
use BotRiconferme\Config;
11
use BotRiconferme\Exception\MissingPageException;
12
use BotRiconferme\MessageProvider;
13
14
if ( !CLI::isCLI() ) {
15
	exit( 'CLI only!' );
16
}
17
18
define( 'BOT_VERSION', '2.1' );
19
20
date_default_timezone_set( 'Europe/Rome' );
21
22
$cli = new CLI();
23
24
$url = $cli->getURL() ?? 'https://it.wikipedia.org/w/api.php';
25
$localUserIdentifier = '@itwiki';
26
$centralURL = 'https://meta.wikimedia.org/w/api.php';
27
28
/* START */
29
30
$errTitle = $cli->getOpt( 'error-title' );
31
32
$simpleLogger = new \BotRiconferme\Logger\SimpleLogger();
33
$loginInfo = new \BotRiconferme\Wiki\LoginInfo(
34
	$cli->getOpt( 'username' ),
1 ignored issue
show
Bug introduced by
It seems like $cli->getOpt('username') can also be of type null; however, parameter $username of BotRiconferme\Wiki\LoginInfo::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
	/** @scrutinizer ignore-type */ $cli->getOpt( 'username' ),
Loading history...
35
	$cli->getOpt( 'password' )
1 ignored issue
show
Bug introduced by
It seems like $cli->getOpt('password') can also be of type null; however, parameter $password of BotRiconferme\Wiki\LoginInfo::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
	/** @scrutinizer ignore-type */ $cli->getOpt( 'password' )
Loading history...
36
);
37
// @fixme A bit of dependency hell here
38
$rf = new \BotRiconferme\Request\RequestFactory( $url );
39
$wiki = new \BotRiconferme\Wiki\Wiki( $loginInfo, $simpleLogger, $rf );
40
$centralRF = new \BotRiconferme\Request\RequestFactory( $centralURL );
41
$centralWiki = new \BotRiconferme\Wiki\Wiki( $loginInfo, $simpleLogger, $centralRF );
42
$centralWiki->setLocalUserIdentifier( $localUserIdentifier );
43
44
try {
45
	$confValues = json_decode( $wiki->getPageContent( $cli->getOpt( 'config-title' ) ), true );
1 ignored issue
show
Bug introduced by
It seems like $cli->getOpt('config-title') can also be of type null; however, parameter $title of BotRiconferme\Wiki\Wiki::getPageContent() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
	$confValues = json_decode( $wiki->getPageContent( /** @scrutinizer ignore-type */ $cli->getOpt( 'config-title' ) ), true );
Loading history...
46
} catch ( MissingPageException $_ ) {
47
	exit( 'Please create a config page.' );
48
}
49
50
Config::init( $confValues );
51
$mp = new MessageProvider( $wiki, $cli->getOpt( 'msg-title' ) );
1 ignored issue
show
Bug introduced by
It seems like $cli->getOpt('msg-title') can also be of type null; however, parameter $msgTitle of BotRiconferme\MessageProvider::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
$mp = new MessageProvider( $wiki, /** @scrutinizer ignore-type */ $cli->getOpt( 'msg-title' ) );
Loading history...
52
53
$wiki->setEditsAsBot( Config::getInstance()->get( 'bot-edits' ) );
54
$centralWiki->setEditsAsBot( Config::getInstance()->get( 'bot-edits' ) );
55
56
if ( $errTitle !== null ) {
57
	// Use a different Wiki with higher min level.
58
	$wikiLoggerLogger = new \BotRiconferme\Logger\SimpleLogger( \Psr\Log\LogLevel::ERROR );
59
	$wikiLoggerWiki = new \BotRiconferme\Wiki\Wiki( $loginInfo, $wikiLoggerLogger, $rf );
60
	$wikiLoggerWiki->setEditsAsBot( Config::getInstance()->get( 'bot-edits' ) );
61
	$errPage = new \BotRiconferme\Wiki\Page\Page( $errTitle, $wikiLoggerWiki );
62
	$wikiLogger = new \BotRiconferme\Logger\WikiLogger(
63
		$errPage,
64
		$mp->getMessage( 'error-page-summary' )->text(),
65
		\Psr\Log\LogLevel::ERROR
66
	);
67
	$mainLogger = new \BotRiconferme\Logger\MultiLogger( $simpleLogger, $wikiLogger );
68
} else {
69
	$mainLogger = $simpleLogger;
70
}
71
72
$errorHandler = function ( $errno, $errstr, $errfile, $errline ) {
73
	throw new \ErrorException( $errstr, 0, $errno, $errfile, $errline );
74
};
75
// @phan-suppress-next-line PhanTypeMismatchArgumentInternal
76
set_error_handler( $errorHandler );
77
78
$wikiGroup = new \BotRiconferme\Wiki\WikiGroup( $wiki, $centralWiki );
79
80
$bot = new Bot(
81
	$mainLogger,
82
	$wikiGroup,
83
	$mp,
84
	\BotRiconferme\Wiki\Page\PageBotList::get( $wiki, $cli->getOpt( 'list-title' ) )
1 ignored issue
show
Bug introduced by
It seems like $cli->getOpt('list-title') can also be of type null; however, parameter $listTitle of BotRiconferme\Wiki\Page\PageBotList::get() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
	\BotRiconferme\Wiki\Page\PageBotList::get( $wiki, /** @scrutinizer ignore-type */ $cli->getOpt( 'list-title' ) )
Loading history...
85
);
86
$taskOpt = $cli->getTaskOpt();
87
$type = current( array_keys( $taskOpt ) );
88
try {
89
	if ( $type === 'task' ) {
90
		$bot->runTask( $taskOpt['task'] );
91
	} elseif ( $type === 'subtask' ) {
92
		$bot->runSubtask( $taskOpt['subtask'] );
93
	} else {
94
		$bot->runAll();
95
	}
96
} catch ( Throwable $e ) {
97
	$mainLogger->error( "$e" );
98
} finally {
99
	$mainLogger->flush();
100
}
101