@@ -13,14 +13,14 @@ |
||
13 | 13 | |
14 | 14 | class UpdateTorExitTask extends ConsoleTaskBase |
15 | 15 | { |
16 | - /** |
|
17 | - * @return void |
|
18 | - */ |
|
19 | - public function execute() |
|
20 | - { |
|
21 | - TorExitProvider::regenerate( |
|
22 | - $this->getDatabase(), |
|
23 | - $this->getHttpHelper(), |
|
24 | - $this->getSiteConfiguration()->getTorExitPaths()); |
|
25 | - } |
|
16 | + /** |
|
17 | + * @return void |
|
18 | + */ |
|
19 | + public function execute() |
|
20 | + { |
|
21 | + TorExitProvider::regenerate( |
|
22 | + $this->getDatabase(), |
|
23 | + $this->getHttpHelper(), |
|
24 | + $this->getSiteConfiguration()->getTorExitPaths()); |
|
25 | + } |
|
26 | 26 | } |
27 | 27 | \ No newline at end of file |
@@ -142,8 +142,7 @@ |
||
142 | 142 | |
143 | 143 | if ($siteConfiguration->getLocationProviderApiKey() === null) { |
144 | 144 | $page->setLocationProvider(new FakeLocationProvider()); |
145 | - } |
|
146 | - else { |
|
145 | + } else { |
|
147 | 146 | $page->setLocationProvider( |
148 | 147 | new IpLocationProvider( |
149 | 148 | $database, |
@@ -25,150 +25,150 @@ |
||
25 | 25 | |
26 | 26 | abstract class ApplicationBase |
27 | 27 | { |
28 | - private $configuration; |
|
29 | - |
|
30 | - public function __construct(SiteConfiguration $configuration) |
|
31 | - { |
|
32 | - $this->configuration = $configuration; |
|
33 | - } |
|
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 | - $actualVersion = (int)$database->query('SELECT version FROM schemaversion')->fetchColumn(); |
|
82 | - if ($actualVersion !== $this->getConfiguration()->getSchemaVersion()) { |
|
83 | - throw new EnvironmentException('Database schema is wrong version! Please either update configuration or database.'); |
|
84 | - } |
|
85 | - |
|
86 | - return $database; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @return SiteConfiguration |
|
91 | - */ |
|
92 | - public function getConfiguration() |
|
93 | - { |
|
94 | - return $this->configuration; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Main application logic |
|
99 | - * @return void |
|
100 | - */ |
|
101 | - abstract protected function main(); |
|
102 | - |
|
103 | - /** |
|
104 | - * Any cleanup tasks should go here |
|
105 | - * |
|
106 | - * Note that we need to be very careful here, as exceptions may have been thrown and handled. |
|
107 | - * This should *only* be for cleaning up, no logic should go here. |
|
108 | - * |
|
109 | - * @return void |
|
110 | - */ |
|
111 | - abstract protected function cleanupEnvironment(); |
|
112 | - |
|
113 | - /** |
|
114 | - * @param ITask $page |
|
115 | - * @param SiteConfiguration $siteConfiguration |
|
116 | - * @param PdoDatabase $database |
|
117 | - * @param PdoDatabase $notificationsDatabase |
|
118 | - * |
|
119 | - * @return void |
|
120 | - */ |
|
121 | - protected function setupHelpers( |
|
122 | - ITask $page, |
|
123 | - SiteConfiguration $siteConfiguration, |
|
124 | - PdoDatabase $database, |
|
125 | - PdoDatabase $notificationsDatabase = null |
|
126 | - ) { |
|
127 | - $page->setSiteConfiguration($siteConfiguration); |
|
128 | - |
|
129 | - // setup the global database object |
|
130 | - $page->setDatabase($database); |
|
131 | - |
|
132 | - // set up helpers and inject them into the page. |
|
133 | - $httpHelper = new HttpHelper($siteConfiguration); |
|
134 | - |
|
135 | - $page->setEmailHelper(new EmailHelper()); |
|
136 | - $page->setHttpHelper($httpHelper); |
|
137 | - $page->setWikiTextHelper(new WikiTextHelper($siteConfiguration, $page->getHttpHelper())); |
|
138 | - |
|
139 | - if ($siteConfiguration->getLocationProviderApiKey() === null) { |
|
140 | - $page->setLocationProvider(new FakeLocationProvider()); |
|
141 | - } |
|
142 | - else { |
|
143 | - $page->setLocationProvider( |
|
144 | - new IpLocationProvider( |
|
145 | - $database, |
|
146 | - $siteConfiguration->getLocationProviderApiKey(), |
|
147 | - $httpHelper |
|
148 | - )); |
|
149 | - } |
|
150 | - |
|
151 | - $page->setXffTrustProvider(new XffTrustProvider($siteConfiguration->getSquidList(), $database)); |
|
152 | - |
|
153 | - $page->setRdnsProvider(new CachedRDnsLookupProvider($database)); |
|
154 | - |
|
155 | - $page->setAntiSpoofProvider(new CachedApiAntispoofProvider( |
|
156 | - $database, |
|
157 | - $this->getConfiguration()->getMediawikiWebServiceEndpoint(), |
|
158 | - $httpHelper)); |
|
159 | - |
|
160 | - $page->setOAuthProtocolHelper(new OAuthProtocolHelper( |
|
161 | - $siteConfiguration->getOAuthBaseUrl(), |
|
162 | - $siteConfiguration->getOAuthConsumerToken(), |
|
163 | - $siteConfiguration->getOAuthConsumerSecret(), |
|
164 | - $siteConfiguration->getMediawikiWebServiceEndpoint() |
|
165 | - )); |
|
166 | - |
|
167 | - $page->setNotificationHelper(new IrcNotificationHelper( |
|
168 | - $siteConfiguration, |
|
169 | - $database, |
|
170 | - $notificationsDatabase)); |
|
171 | - |
|
172 | - $page->setTorExitProvider(new TorExitProvider($database)); |
|
173 | - } |
|
28 | + private $configuration; |
|
29 | + |
|
30 | + public function __construct(SiteConfiguration $configuration) |
|
31 | + { |
|
32 | + $this->configuration = $configuration; |
|
33 | + } |
|
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 | + $actualVersion = (int)$database->query('SELECT version FROM schemaversion')->fetchColumn(); |
|
82 | + if ($actualVersion !== $this->getConfiguration()->getSchemaVersion()) { |
|
83 | + throw new EnvironmentException('Database schema is wrong version! Please either update configuration or database.'); |
|
84 | + } |
|
85 | + |
|
86 | + return $database; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @return SiteConfiguration |
|
91 | + */ |
|
92 | + public function getConfiguration() |
|
93 | + { |
|
94 | + return $this->configuration; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Main application logic |
|
99 | + * @return void |
|
100 | + */ |
|
101 | + abstract protected function main(); |
|
102 | + |
|
103 | + /** |
|
104 | + * Any cleanup tasks should go here |
|
105 | + * |
|
106 | + * Note that we need to be very careful here, as exceptions may have been thrown and handled. |
|
107 | + * This should *only* be for cleaning up, no logic should go here. |
|
108 | + * |
|
109 | + * @return void |
|
110 | + */ |
|
111 | + abstract protected function cleanupEnvironment(); |
|
112 | + |
|
113 | + /** |
|
114 | + * @param ITask $page |
|
115 | + * @param SiteConfiguration $siteConfiguration |
|
116 | + * @param PdoDatabase $database |
|
117 | + * @param PdoDatabase $notificationsDatabase |
|
118 | + * |
|
119 | + * @return void |
|
120 | + */ |
|
121 | + protected function setupHelpers( |
|
122 | + ITask $page, |
|
123 | + SiteConfiguration $siteConfiguration, |
|
124 | + PdoDatabase $database, |
|
125 | + PdoDatabase $notificationsDatabase = null |
|
126 | + ) { |
|
127 | + $page->setSiteConfiguration($siteConfiguration); |
|
128 | + |
|
129 | + // setup the global database object |
|
130 | + $page->setDatabase($database); |
|
131 | + |
|
132 | + // set up helpers and inject them into the page. |
|
133 | + $httpHelper = new HttpHelper($siteConfiguration); |
|
134 | + |
|
135 | + $page->setEmailHelper(new EmailHelper()); |
|
136 | + $page->setHttpHelper($httpHelper); |
|
137 | + $page->setWikiTextHelper(new WikiTextHelper($siteConfiguration, $page->getHttpHelper())); |
|
138 | + |
|
139 | + if ($siteConfiguration->getLocationProviderApiKey() === null) { |
|
140 | + $page->setLocationProvider(new FakeLocationProvider()); |
|
141 | + } |
|
142 | + else { |
|
143 | + $page->setLocationProvider( |
|
144 | + new IpLocationProvider( |
|
145 | + $database, |
|
146 | + $siteConfiguration->getLocationProviderApiKey(), |
|
147 | + $httpHelper |
|
148 | + )); |
|
149 | + } |
|
150 | + |
|
151 | + $page->setXffTrustProvider(new XffTrustProvider($siteConfiguration->getSquidList(), $database)); |
|
152 | + |
|
153 | + $page->setRdnsProvider(new CachedRDnsLookupProvider($database)); |
|
154 | + |
|
155 | + $page->setAntiSpoofProvider(new CachedApiAntispoofProvider( |
|
156 | + $database, |
|
157 | + $this->getConfiguration()->getMediawikiWebServiceEndpoint(), |
|
158 | + $httpHelper)); |
|
159 | + |
|
160 | + $page->setOAuthProtocolHelper(new OAuthProtocolHelper( |
|
161 | + $siteConfiguration->getOAuthBaseUrl(), |
|
162 | + $siteConfiguration->getOAuthConsumerToken(), |
|
163 | + $siteConfiguration->getOAuthConsumerSecret(), |
|
164 | + $siteConfiguration->getMediawikiWebServiceEndpoint() |
|
165 | + )); |
|
166 | + |
|
167 | + $page->setNotificationHelper(new IrcNotificationHelper( |
|
168 | + $siteConfiguration, |
|
169 | + $database, |
|
170 | + $notificationsDatabase)); |
|
171 | + |
|
172 | + $page->setTorExitProvider(new TorExitProvider($database)); |
|
173 | + } |
|
174 | 174 | } |
@@ -10,21 +10,21 @@ |
||
10 | 10 | |
11 | 11 | abstract class PublicInterfacePageBase extends PageBase |
12 | 12 | { |
13 | - /** |
|
14 | - * PublicInterfaceInternalPageBase constructor. |
|
15 | - */ |
|
16 | - public function __construct() |
|
17 | - { |
|
18 | - $this->template = 'publicbase.tpl'; |
|
19 | - } |
|
13 | + /** |
|
14 | + * PublicInterfaceInternalPageBase constructor. |
|
15 | + */ |
|
16 | + public function __construct() |
|
17 | + { |
|
18 | + $this->template = 'publicbase.tpl'; |
|
19 | + } |
|
20 | 20 | |
21 | - final public function execute() |
|
22 | - { |
|
23 | - parent::execute(); |
|
24 | - } |
|
21 | + final public function execute() |
|
22 | + { |
|
23 | + parent::execute(); |
|
24 | + } |
|
25 | 25 | |
26 | - final public function finalisePage() |
|
27 | - { |
|
28 | - parent::finalisePage(); |
|
29 | - } |
|
26 | + final public function finalisePage() |
|
27 | + { |
|
28 | + parent::finalisePage(); |
|
29 | + } |
|
30 | 30 | } |
31 | 31 | \ No newline at end of file |
@@ -12,21 +12,21 @@ |
||
12 | 12 | |
13 | 13 | interface IRoutedTask extends ITask |
14 | 14 | { |
15 | - /** |
|
16 | - * Sets the route the request will take. Only should be called from the request router. |
|
17 | - * |
|
18 | - * @param $routeName string |
|
19 | - * |
|
20 | - * @return void |
|
21 | - * |
|
22 | - * @throws Exception |
|
23 | - * @category Security-Critical |
|
24 | - */ |
|
25 | - public function setRoute($routeName); |
|
15 | + /** |
|
16 | + * Sets the route the request will take. Only should be called from the request router. |
|
17 | + * |
|
18 | + * @param $routeName string |
|
19 | + * |
|
20 | + * @return void |
|
21 | + * |
|
22 | + * @throws Exception |
|
23 | + * @category Security-Critical |
|
24 | + */ |
|
25 | + public function setRoute($routeName); |
|
26 | 26 | |
27 | - /** |
|
28 | - * Gets the name of the route that has been passed from the request router. |
|
29 | - * @return string |
|
30 | - */ |
|
31 | - public function getRouteName(); |
|
27 | + /** |
|
28 | + * Gets the name of the route that has been passed from the request router. |
|
29 | + * @return string |
|
30 | + */ |
|
31 | + public function getRouteName(); |
|
32 | 32 | } |
33 | 33 | \ No newline at end of file |
@@ -10,8 +10,8 @@ |
||
10 | 10 | |
11 | 11 | class RegexConstants |
12 | 12 | { |
13 | - const IPV6_CIDR = '(?:/(?:12[0-8]|1[01][0-9]|[0-9]{1,2}))?'; |
|
14 | - const IPV4_CIDR = '(?:/(?:32|3[01]|[0-2]?[0-9]))?'; |
|
15 | - const IPV4 = '(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'; |
|
16 | - const IPV6 = '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))'; |
|
13 | + const IPV6_CIDR = '(?:/(?:12[0-8]|1[01][0-9]|[0-9]{1,2}))?'; |
|
14 | + const IPV4_CIDR = '(?:/(?:32|3[01]|[0-2]?[0-9]))?'; |
|
15 | + const IPV4 = '(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)'; |
|
16 | + const IPV6 = '(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))'; |
|
17 | 17 | } |
18 | 18 | \ No newline at end of file |
@@ -21,22 +21,22 @@ |
||
21 | 21 | */ |
22 | 22 | abstract class ReadableException extends Exception |
23 | 23 | { |
24 | - use TemplateOutput; |
|
24 | + use TemplateOutput; |
|
25 | 25 | |
26 | - /** |
|
27 | - * Returns a readable HTML error message that's displayable to the user using templates. |
|
28 | - * @return string |
|
29 | - */ |
|
30 | - abstract public function getReadableError(); |
|
26 | + /** |
|
27 | + * Returns a readable HTML error message that's displayable to the user using templates. |
|
28 | + * @return string |
|
29 | + */ |
|
30 | + abstract public function getReadableError(); |
|
31 | 31 | |
32 | - /** |
|
33 | - * @return SiteConfiguration |
|
34 | - */ |
|
35 | - protected function getSiteConfiguration() |
|
36 | - { |
|
37 | - // Uck. However, we have encountered an exception. |
|
38 | - global $siteConfiguration; |
|
32 | + /** |
|
33 | + * @return SiteConfiguration |
|
34 | + */ |
|
35 | + protected function getSiteConfiguration() |
|
36 | + { |
|
37 | + // Uck. However, we have encountered an exception. |
|
38 | + global $siteConfiguration; |
|
39 | 39 | |
40 | - return $siteConfiguration; |
|
41 | - } |
|
40 | + return $siteConfiguration; |
|
41 | + } |
|
42 | 42 | } |
43 | 43 | \ No newline at end of file |
@@ -21,13 +21,13 @@ |
||
21 | 21 | */ |
22 | 22 | class EnvironmentException extends Exception |
23 | 23 | { |
24 | - /** |
|
25 | - * EnvironmentException constructor. |
|
26 | - * |
|
27 | - * @param string $friendlyMessage |
|
28 | - */ |
|
29 | - public function __construct($friendlyMessage) |
|
30 | - { |
|
31 | - parent::__construct($friendlyMessage); |
|
32 | - } |
|
24 | + /** |
|
25 | + * EnvironmentException constructor. |
|
26 | + * |
|
27 | + * @param string $friendlyMessage |
|
28 | + */ |
|
29 | + public function __construct($friendlyMessage) |
|
30 | + { |
|
31 | + parent::__construct($friendlyMessage); |
|
32 | + } |
|
33 | 33 | } |
34 | 34 | \ No newline at end of file |
@@ -89,8 +89,7 @@ |
||
89 | 89 | // all over the rest of the code |
90 | 90 | if ($this->hasActiveTransaction) { |
91 | 91 | return false; |
92 | - } |
|
93 | - else { |
|
92 | + } else { |
|
94 | 93 | // set the transaction isolation level for every transaction. |
95 | 94 | $this->exec("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;"); |
96 | 95 |
@@ -15,113 +15,113 @@ |
||
15 | 15 | |
16 | 16 | class PdoDatabase extends PDO |
17 | 17 | { |
18 | - /** |
|
19 | - * @var PdoDatabase[] |
|
20 | - */ |
|
21 | - private static $connections = array(); |
|
22 | - /** |
|
23 | - * @var bool True if a transaction is active |
|
24 | - */ |
|
25 | - protected $hasActiveTransaction = false; |
|
26 | - |
|
27 | - /** |
|
28 | - * Unless you're doing low-level work, this is not the function you want. |
|
29 | - * |
|
30 | - * @param string $connectionName |
|
31 | - * |
|
32 | - * @return PdoDatabase |
|
33 | - * @throws Exception |
|
34 | - */ |
|
35 | - public static function getDatabaseConnection($connectionName) |
|
36 | - { |
|
37 | - if (!isset(self::$connections[$connectionName])) { |
|
38 | - global $cDatabaseConfig; |
|
39 | - |
|
40 | - if (!array_key_exists($connectionName, $cDatabaseConfig)) { |
|
41 | - throw new Exception("Database configuration not found for alias $connectionName"); |
|
42 | - } |
|
43 | - |
|
44 | - try { |
|
45 | - $databaseObject = new PdoDatabase( |
|
46 | - $cDatabaseConfig[$connectionName]["dsrcname"], |
|
47 | - $cDatabaseConfig[$connectionName]["username"], |
|
48 | - $cDatabaseConfig[$connectionName]["password"], |
|
49 | - $cDatabaseConfig[$connectionName]["options"] |
|
50 | - ); |
|
51 | - } |
|
52 | - catch (PDOException $ex) { |
|
53 | - // wrap around any potential stack traces which may include passwords |
|
54 | - throw new EnvironmentException("Error connecting to database '$connectionName': " . $ex->getMessage()); |
|
55 | - } |
|
56 | - |
|
57 | - $databaseObject->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
58 | - |
|
59 | - // emulating prepared statements gives a performance boost on MySQL. |
|
60 | - // |
|
61 | - // however, our version of PDO doesn't seem to understand parameter types when emulating |
|
62 | - // the prepared statements, so we're forced to turn this off for now. |
|
63 | - // -- stw 2014-02-11 |
|
64 | - $databaseObject->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); |
|
65 | - |
|
66 | - self::$connections[$connectionName] = $databaseObject; |
|
67 | - } |
|
68 | - |
|
69 | - return self::$connections[$connectionName]; |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Determines if this connection has a transaction in progress or not |
|
74 | - * @return boolean true if there is a transaction in progress. |
|
75 | - */ |
|
76 | - public function hasActiveTransaction() |
|
77 | - { |
|
78 | - return $this->hasActiveTransaction; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Summary of beginTransaction |
|
83 | - * @return bool |
|
84 | - */ |
|
85 | - public function beginTransaction() |
|
86 | - { |
|
87 | - // Override the pre-existing method, which doesn't stop you from |
|
88 | - // starting transactions within transactions - which doesn't work and |
|
89 | - // will throw an exception. This eliminates the need to catch exceptions |
|
90 | - // all over the rest of the code |
|
91 | - if ($this->hasActiveTransaction) { |
|
92 | - return false; |
|
93 | - } |
|
94 | - else { |
|
95 | - // set the transaction isolation level for every transaction. |
|
96 | - $this->exec("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;"); |
|
97 | - |
|
98 | - // start a new transaction, and return whether or not the start was |
|
99 | - // successful |
|
100 | - $this->hasActiveTransaction = parent::beginTransaction(); |
|
101 | - |
|
102 | - return $this->hasActiveTransaction; |
|
103 | - } |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Commits the active transaction |
|
108 | - */ |
|
109 | - public function commit() |
|
110 | - { |
|
111 | - if ($this->hasActiveTransaction) { |
|
112 | - parent::commit(); |
|
113 | - $this->hasActiveTransaction = false; |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Rolls back a transaction |
|
119 | - */ |
|
120 | - public function rollBack() |
|
121 | - { |
|
122 | - if ($this->hasActiveTransaction) { |
|
123 | - parent::rollback(); |
|
124 | - $this->hasActiveTransaction = false; |
|
125 | - } |
|
126 | - } |
|
18 | + /** |
|
19 | + * @var PdoDatabase[] |
|
20 | + */ |
|
21 | + private static $connections = array(); |
|
22 | + /** |
|
23 | + * @var bool True if a transaction is active |
|
24 | + */ |
|
25 | + protected $hasActiveTransaction = false; |
|
26 | + |
|
27 | + /** |
|
28 | + * Unless you're doing low-level work, this is not the function you want. |
|
29 | + * |
|
30 | + * @param string $connectionName |
|
31 | + * |
|
32 | + * @return PdoDatabase |
|
33 | + * @throws Exception |
|
34 | + */ |
|
35 | + public static function getDatabaseConnection($connectionName) |
|
36 | + { |
|
37 | + if (!isset(self::$connections[$connectionName])) { |
|
38 | + global $cDatabaseConfig; |
|
39 | + |
|
40 | + if (!array_key_exists($connectionName, $cDatabaseConfig)) { |
|
41 | + throw new Exception("Database configuration not found for alias $connectionName"); |
|
42 | + } |
|
43 | + |
|
44 | + try { |
|
45 | + $databaseObject = new PdoDatabase( |
|
46 | + $cDatabaseConfig[$connectionName]["dsrcname"], |
|
47 | + $cDatabaseConfig[$connectionName]["username"], |
|
48 | + $cDatabaseConfig[$connectionName]["password"], |
|
49 | + $cDatabaseConfig[$connectionName]["options"] |
|
50 | + ); |
|
51 | + } |
|
52 | + catch (PDOException $ex) { |
|
53 | + // wrap around any potential stack traces which may include passwords |
|
54 | + throw new EnvironmentException("Error connecting to database '$connectionName': " . $ex->getMessage()); |
|
55 | + } |
|
56 | + |
|
57 | + $databaseObject->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
58 | + |
|
59 | + // emulating prepared statements gives a performance boost on MySQL. |
|
60 | + // |
|
61 | + // however, our version of PDO doesn't seem to understand parameter types when emulating |
|
62 | + // the prepared statements, so we're forced to turn this off for now. |
|
63 | + // -- stw 2014-02-11 |
|
64 | + $databaseObject->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); |
|
65 | + |
|
66 | + self::$connections[$connectionName] = $databaseObject; |
|
67 | + } |
|
68 | + |
|
69 | + return self::$connections[$connectionName]; |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Determines if this connection has a transaction in progress or not |
|
74 | + * @return boolean true if there is a transaction in progress. |
|
75 | + */ |
|
76 | + public function hasActiveTransaction() |
|
77 | + { |
|
78 | + return $this->hasActiveTransaction; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Summary of beginTransaction |
|
83 | + * @return bool |
|
84 | + */ |
|
85 | + public function beginTransaction() |
|
86 | + { |
|
87 | + // Override the pre-existing method, which doesn't stop you from |
|
88 | + // starting transactions within transactions - which doesn't work and |
|
89 | + // will throw an exception. This eliminates the need to catch exceptions |
|
90 | + // all over the rest of the code |
|
91 | + if ($this->hasActiveTransaction) { |
|
92 | + return false; |
|
93 | + } |
|
94 | + else { |
|
95 | + // set the transaction isolation level for every transaction. |
|
96 | + $this->exec("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;"); |
|
97 | + |
|
98 | + // start a new transaction, and return whether or not the start was |
|
99 | + // successful |
|
100 | + $this->hasActiveTransaction = parent::beginTransaction(); |
|
101 | + |
|
102 | + return $this->hasActiveTransaction; |
|
103 | + } |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Commits the active transaction |
|
108 | + */ |
|
109 | + public function commit() |
|
110 | + { |
|
111 | + if ($this->hasActiveTransaction) { |
|
112 | + parent::commit(); |
|
113 | + $this->hasActiveTransaction = false; |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Rolls back a transaction |
|
119 | + */ |
|
120 | + public function rollBack() |
|
121 | + { |
|
122 | + if ($this->hasActiveTransaction) { |
|
123 | + parent::rollback(); |
|
124 | + $this->hasActiveTransaction = false; |
|
125 | + } |
|
126 | + } |
|
127 | 127 | } |
@@ -60,12 +60,10 @@ |
||
60 | 60 | |
61 | 61 | if ($statement->execute()) { |
62 | 62 | $this->id = (int)$this->dbObject->lastInsertId(); |
63 | - } |
|
64 | - else { |
|
63 | + } else { |
|
65 | 64 | throw new Exception($statement->errorInfo()); |
66 | 65 | } |
67 | - } |
|
68 | - else { |
|
66 | + } else { |
|
69 | 67 | // update |
70 | 68 | $statement = $this->dbObject->prepare(<<<SQL |
71 | 69 | UPDATE `geolocation` |
@@ -21,110 +21,110 @@ |
||
21 | 21 | */ |
22 | 22 | class GeoLocation extends DataObject |
23 | 23 | { |
24 | - private $address; |
|
25 | - private $data; |
|
26 | - private $creation; |
|
27 | - |
|
28 | - /** |
|
29 | - * @param string $address |
|
30 | - * @param PdoDatabase $database |
|
31 | - * @param bool $forUpdate |
|
32 | - * @return GeoLocation |
|
33 | - */ |
|
34 | - public static function getByAddress($address, PdoDatabase $database, $forUpdate = false) |
|
35 | - { |
|
36 | - $lockMode = $forUpdate ? ' FOR UPDATE' : ''; |
|
37 | - $sql = "SELECT * FROM geolocation WHERE address = :id LIMIT 1" . $lockMode; |
|
38 | - |
|
39 | - $statement = $database->prepare($sql); |
|
40 | - $statement->bindValue(":id", $address); |
|
41 | - |
|
42 | - $statement->execute(); |
|
43 | - |
|
44 | - $resultObject = $statement->fetchObject(get_called_class()); |
|
45 | - |
|
46 | - if ($resultObject != false) { |
|
47 | - $resultObject->setDatabase($database); |
|
48 | - } |
|
49 | - |
|
50 | - return $resultObject; |
|
51 | - } |
|
52 | - |
|
53 | - public function save() |
|
54 | - { |
|
55 | - if ($this->isNew()) { |
|
56 | - // insert |
|
57 | - $statement = $this->dbObject->prepare("INSERT INTO `geolocation` (address, data) VALUES (:address, :data) ON DUPLICATE KEY UPDATE address = address;"); |
|
58 | - |
|
59 | - $statement->bindValue(":address", $this->address); |
|
60 | - $statement->bindValue(":data", $this->data); |
|
61 | - |
|
62 | - if ($statement->execute()) { |
|
63 | - $this->id = (int)$this->dbObject->lastInsertId(); |
|
64 | - } |
|
65 | - else { |
|
66 | - throw new Exception($statement->errorInfo()); |
|
67 | - } |
|
68 | - } |
|
69 | - else { |
|
70 | - // update |
|
71 | - $statement = $this->dbObject->prepare(<<<SQL |
|
24 | + private $address; |
|
25 | + private $data; |
|
26 | + private $creation; |
|
27 | + |
|
28 | + /** |
|
29 | + * @param string $address |
|
30 | + * @param PdoDatabase $database |
|
31 | + * @param bool $forUpdate |
|
32 | + * @return GeoLocation |
|
33 | + */ |
|
34 | + public static function getByAddress($address, PdoDatabase $database, $forUpdate = false) |
|
35 | + { |
|
36 | + $lockMode = $forUpdate ? ' FOR UPDATE' : ''; |
|
37 | + $sql = "SELECT * FROM geolocation WHERE address = :id LIMIT 1" . $lockMode; |
|
38 | + |
|
39 | + $statement = $database->prepare($sql); |
|
40 | + $statement->bindValue(":id", $address); |
|
41 | + |
|
42 | + $statement->execute(); |
|
43 | + |
|
44 | + $resultObject = $statement->fetchObject(get_called_class()); |
|
45 | + |
|
46 | + if ($resultObject != false) { |
|
47 | + $resultObject->setDatabase($database); |
|
48 | + } |
|
49 | + |
|
50 | + return $resultObject; |
|
51 | + } |
|
52 | + |
|
53 | + public function save() |
|
54 | + { |
|
55 | + if ($this->isNew()) { |
|
56 | + // insert |
|
57 | + $statement = $this->dbObject->prepare("INSERT INTO `geolocation` (address, data) VALUES (:address, :data) ON DUPLICATE KEY UPDATE address = address;"); |
|
58 | + |
|
59 | + $statement->bindValue(":address", $this->address); |
|
60 | + $statement->bindValue(":data", $this->data); |
|
61 | + |
|
62 | + if ($statement->execute()) { |
|
63 | + $this->id = (int)$this->dbObject->lastInsertId(); |
|
64 | + } |
|
65 | + else { |
|
66 | + throw new Exception($statement->errorInfo()); |
|
67 | + } |
|
68 | + } |
|
69 | + else { |
|
70 | + // update |
|
71 | + $statement = $this->dbObject->prepare(<<<SQL |
|
72 | 72 | UPDATE `geolocation` |
73 | 73 | SET address = :address, data = :data, updateversion = updateversion + 1 |
74 | 74 | WHERE id = :id AND updateversion = :updateversion; |
75 | 75 | SQL |
76 | - ); |
|
77 | - |
|
78 | - $statement->bindValue(":id", $this->id); |
|
79 | - $statement->bindValue(":updateversion", $this->updateversion); |
|
80 | - |
|
81 | - $statement->bindValue(":address", $this->address); |
|
82 | - $statement->bindValue(":data", $this->data); |
|
83 | - |
|
84 | - if (!$statement->execute()) { |
|
85 | - throw new Exception($statement->errorInfo()); |
|
86 | - } |
|
87 | - |
|
88 | - if ($statement->rowCount() !== 1) { |
|
89 | - throw new OptimisticLockFailedException(); |
|
90 | - } |
|
91 | - |
|
92 | - $this->updateversion++; |
|
93 | - } |
|
94 | - } |
|
95 | - |
|
96 | - public function getAddress() |
|
97 | - { |
|
98 | - return $this->address; |
|
99 | - } |
|
100 | - |
|
101 | - /** |
|
102 | - * @param string $address |
|
103 | - */ |
|
104 | - public function setAddress($address) |
|
105 | - { |
|
106 | - $this->address = $address; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @return array |
|
111 | - */ |
|
112 | - public function getData() |
|
113 | - { |
|
114 | - return unserialize($this->data); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * @param array $data |
|
119 | - */ |
|
120 | - public function setData($data) |
|
121 | - { |
|
122 | - $this->data = serialize($data); |
|
123 | - } |
|
124 | - |
|
125 | - /** @return DateTimeImmutable */ |
|
126 | - public function getCreation() |
|
127 | - { |
|
128 | - return new DateTimeImmutable($this->creation); |
|
129 | - } |
|
76 | + ); |
|
77 | + |
|
78 | + $statement->bindValue(":id", $this->id); |
|
79 | + $statement->bindValue(":updateversion", $this->updateversion); |
|
80 | + |
|
81 | + $statement->bindValue(":address", $this->address); |
|
82 | + $statement->bindValue(":data", $this->data); |
|
83 | + |
|
84 | + if (!$statement->execute()) { |
|
85 | + throw new Exception($statement->errorInfo()); |
|
86 | + } |
|
87 | + |
|
88 | + if ($statement->rowCount() !== 1) { |
|
89 | + throw new OptimisticLockFailedException(); |
|
90 | + } |
|
91 | + |
|
92 | + $this->updateversion++; |
|
93 | + } |
|
94 | + } |
|
95 | + |
|
96 | + public function getAddress() |
|
97 | + { |
|
98 | + return $this->address; |
|
99 | + } |
|
100 | + |
|
101 | + /** |
|
102 | + * @param string $address |
|
103 | + */ |
|
104 | + public function setAddress($address) |
|
105 | + { |
|
106 | + $this->address = $address; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @return array |
|
111 | + */ |
|
112 | + public function getData() |
|
113 | + { |
|
114 | + return unserialize($this->data); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * @param array $data |
|
119 | + */ |
|
120 | + public function setData($data) |
|
121 | + { |
|
122 | + $this->data = serialize($data); |
|
123 | + } |
|
124 | + |
|
125 | + /** @return DateTimeImmutable */ |
|
126 | + public function getCreation() |
|
127 | + { |
|
128 | + return new DateTimeImmutable($this->creation); |
|
129 | + } |
|
130 | 130 | } |