Completed
Branch newinternal (e32466)
by Simon
03:39
created

ApplicationBase::getConfiguration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca;
10
11
use Exception;
12
use Waca\Exceptions\EnvironmentException;
13
use Waca\Helpers\EmailHelper;
14
use Waca\Helpers\HttpHelper;
15
use Waca\Helpers\IrcNotificationHelper;
16
use Waca\Helpers\OAuthHelper;
17
use Waca\Helpers\WikiTextHelper;
18
use Waca\Providers\CachedApiAntispoofProvider;
19
use Waca\Providers\CachedRDnsLookupProvider;
20
use Waca\Providers\FakeLocationProvider;
21
use Waca\Providers\IpLocationProvider;
22
use Waca\Providers\TorExitProvider;
23
use Waca\Providers\XffTrustProvider;
24
use Waca\Tasks\ITask;
25
26
abstract class ApplicationBase
27
{
28
	private $configuration;
29
30 1
	public function __construct(SiteConfiguration $configuration)
31
	{
32 1
		$this->configuration = $configuration;
33 1
	}
34
35
	/**
36
	 * Application entry point.
37
	 *
38
	 * Sets up the environment and runs the application, performing any global cleanup operations when done.
39
	 */
40
	public function run()
41
	{
42
		try {
43
			if ($this->setupEnvironment()) {
44
				$this->main();
45
			}
46
		}
47
		catch (Exception $ex) {
48
			print $ex->getMessage();
49
		}
50
		finally {
51
			$this->cleanupEnvironment();
52
		}
53
	}
54
55
	/**
56
	 * Environment setup
57
	 *
58
	 * This method initialises the tool environment. If the tool cannot be initialised correctly, it will return false
59
	 * and shut down prematurely.
60
	 *
61
	 * @return bool
62
	 * @throws EnvironmentException
63
	 */
64
	protected function setupEnvironment()
65
	{
66
		$this->setupDatabase();
67
68
		return true;
69
	}
70
71
	/**
72
	 * @return PdoDatabase
73
	 * @throws EnvironmentException
74
	 * @throws Exception
75
	 */
76
	protected function setupDatabase()
77
	{
78
		// check the schema version
79
		$database = PdoDatabase::getDatabaseConnection('acc');
80
81
		/** @var int $actualVersion */
82
		$actualVersion = (int)$database->query('SELECT version FROM schemaversion')->fetchColumn();
83
		if ($actualVersion !== $this->getConfiguration()->getSchemaVersion()) {
84
			throw new EnvironmentException('Database schema is wrong version! Please either update configuration or database.');
85
		}
86
87
		return $database;
88
	}
89
90
	/**
91
	 * @return SiteConfiguration
92
	 */
93
	public function getConfiguration()
94
	{
95
		return $this->configuration;
96
	}
97
98
	/**
99
	 * Main application logic
100
	 * @return void
101
	 */
102
	abstract protected function main();
103
104
	/**
105
	 * Any cleanup tasks should go here
106
	 *
107
	 * Note that we need to be very careful here, as exceptions may have been thrown and handled.
108
	 * This should *only* be for cleaning up, no logic should go here.
109
	 *
110
	 * @return void
111
	 */
112
	abstract protected function cleanupEnvironment();
113
114
	/**
115
	 * @param ITask             $page
116
	 * @param SiteConfiguration $siteConfiguration
117
	 * @param PdoDatabase       $database
118
	 * @param PdoDatabase       $notificationsDatabase
0 ignored issues
show
Documentation introduced by
Should the type for parameter $notificationsDatabase not be null|PdoDatabase?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
119
	 *
120
	 * @return void
121
	 */
122
	protected function setupHelpers(
123
		ITask $page,
124
		SiteConfiguration $siteConfiguration,
125
		PdoDatabase $database,
126
		PdoDatabase $notificationsDatabase = null
127
	) {
128
		$page->setSiteConfiguration($siteConfiguration);
129
130
		// setup the global database object
131
		$page->setDatabase($database);
132
133
		// set up helpers and inject them into the page.
134
		$httpHelper = new HttpHelper(
135
			$siteConfiguration->getUserAgent(),
136
			$siteConfiguration->getCurlDisableVerifyPeer()
137
		);
138
139
		$page->setEmailHelper(new EmailHelper());
140
		$page->setHttpHelper($httpHelper);
141
		$page->setWikiTextHelper(new WikiTextHelper($siteConfiguration, $page->getHttpHelper()));
142
143
		if ($siteConfiguration->getLocationProviderApiKey() === null) {
144
			$page->setLocationProvider(new FakeLocationProvider());
145
		}
146
		else {
147
			$page->setLocationProvider(
148
				new IpLocationProvider(
149
					$database,
150
					$siteConfiguration->getLocationProviderApiKey(),
151
					$httpHelper
152
				));
153
		}
154
155
		$page->setXffTrustProvider(new XffTrustProvider($siteConfiguration->getSquidList(), $database));
156
157
		$page->setRdnsProvider(new CachedRDnsLookupProvider($database));
158
159
		$page->setAntiSpoofProvider(new CachedApiAntispoofProvider(
160
			$database,
161
			$this->getConfiguration()->getMediawikiWebServiceEndpoint(),
162
			$httpHelper));
163
164
		$page->setOAuthHelper(new OAuthHelper(
165
			$siteConfiguration->getOAuthBaseUrl(),
166
			$siteConfiguration->getOAuthConsumerToken(),
167
			$siteConfiguration->getOAuthConsumerSecret(),
168
			$httpHelper,
169
			$siteConfiguration->getMediawikiWebServiceEndpoint()
170
		));
171
172
		$page->setNotificationHelper(new IrcNotificationHelper(
173
			$siteConfiguration,
174
			$database,
175
			$notificationsDatabase));
176
177
		$page->setTorExitProvider(new TorExitProvider($database));
178
	}
179
}