@@ -21,156 +21,156 @@ |
||
21 | 21 | |
22 | 22 | abstract class CreationTaskBase extends BackgroundTaskBase |
23 | 23 | { |
24 | - /** @var Request */ |
|
25 | - private $request; |
|
26 | - /** |
|
27 | - * @var MediaWikiHelper |
|
28 | - * Don't use this directly. |
|
29 | - */ |
|
30 | - private $mwHelper = null; |
|
31 | - |
|
32 | - public function execute() |
|
33 | - { |
|
34 | - $this->request = $this->getRequest(); |
|
35 | - $user = $this->getTriggerUser(); |
|
36 | - $parameters = $this->getParameters(); |
|
37 | - |
|
38 | - if ($this->request->getStatus() !== RequestStatus::JOBQUEUE) { |
|
39 | - $this->markCancelled('Request is not deferred to the job queue'); |
|
40 | - |
|
41 | - return; |
|
42 | - } |
|
43 | - |
|
44 | - if ($this->request->getEmailSent() != 0 && !isset($parameters->emailText)) { |
|
45 | - $this->markFailed('Request has already been sent a templated email'); |
|
46 | - |
|
47 | - return; |
|
48 | - } |
|
49 | - |
|
50 | - if ($this->request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()) { |
|
51 | - $this->markFailed('Private data of request has been purged.'); |
|
52 | - |
|
53 | - return; |
|
54 | - } |
|
55 | - |
|
56 | - $emailText = null; |
|
57 | - $ccMailingList = null; |
|
58 | - $logTarget = null; |
|
59 | - |
|
60 | - if (isset($parameters->emailText) && isset($parameters->ccMailingList)) { |
|
61 | - $emailText = $parameters->emailText; |
|
62 | - $ccMailingList = $parameters->ccMailingList; |
|
63 | - $logTarget = "custom-y"; |
|
64 | - } |
|
65 | - |
|
66 | - if ($this->getEmailTemplate() !== null) { |
|
67 | - $emailText = $this->getEmailTemplate()->getText(); |
|
68 | - $ccMailingList = false; |
|
69 | - $logTarget = $this->getEmailTemplate()->getId(); |
|
70 | - } |
|
71 | - |
|
72 | - if ($emailText === null || $ccMailingList === null) { |
|
73 | - $this->markFailed('Unable to get closure email text'); |
|
74 | - |
|
75 | - return; |
|
76 | - } |
|
77 | - |
|
78 | - try { |
|
79 | - $this->performCreation($user); |
|
80 | - |
|
81 | - $this->request->setStatus(RequestStatus::CLOSED); |
|
82 | - $this->request->setQueue(null); |
|
83 | - $this->request->setReserved(null); |
|
84 | - $this->request->setEmailSent(true); |
|
85 | - $this->request->save(); |
|
86 | - |
|
87 | - // Log the closure as the user |
|
88 | - $logComment = $this->getEmailTemplate() === null ? $emailText : null; |
|
89 | - Logger::closeRequest($this->getDatabase(), $this->request, $logTarget, $logComment, $this->getTriggerUser()); |
|
90 | - |
|
91 | - $requestEmailHelper = new RequestEmailHelper($this->getEmailHelper()); |
|
92 | - $requestEmailHelper->sendMail($this->request, $emailText, $this->getTriggerUser(), $ccMailingList); |
|
93 | - } |
|
94 | - catch (Exception $ex) { |
|
95 | - if (mb_strlen($ex->getMessage()) > 255) { |
|
96 | - ExceptionHandler::logExceptionToDisk($ex, $this->getSiteConfiguration()); |
|
97 | - } |
|
98 | - |
|
99 | - $this->markFailed(substr($ex->getMessage(), 0, 255)); |
|
100 | - |
|
101 | - return; |
|
102 | - } |
|
103 | - |
|
104 | - $this->markComplete(); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * @return IMediaWikiClient |
|
109 | - */ |
|
110 | - protected abstract function getMediaWikiClient(); |
|
111 | - |
|
112 | - protected function getMediaWikiHelper() |
|
113 | - { |
|
114 | - if ($this->mwHelper === null) { |
|
115 | - $this->mwHelper = new MediaWikiHelper($this->getMediaWikiClient(), $this->getSiteConfiguration()); |
|
116 | - } |
|
117 | - |
|
118 | - return $this->mwHelper; |
|
119 | - } |
|
120 | - |
|
121 | - /** @noinspection PhpUnusedParameterInspection */ |
|
122 | - protected function getCreationReason(Request $request, User $user) |
|
123 | - { |
|
124 | - return 'Requested account at [[WP:ACC]], request #' . $request->getId(); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @param string $name |
|
129 | - * |
|
130 | - * @return bool |
|
131 | - */ |
|
132 | - protected function checkAccountExists($name) |
|
133 | - { |
|
134 | - return $this->getMediaWikiHelper()->checkAccountExists($name); |
|
135 | - } |
|
136 | - |
|
137 | - protected function markFailed($reason = null, bool $acknowledged = false) |
|
138 | - { |
|
139 | - $this->request->setStatus(RequestStatus::HOSPITAL); |
|
140 | - $this->request->setQueue(null); |
|
141 | - $this->request->save(); |
|
142 | - |
|
143 | - $this->getNotificationHelper()->requestCreationFailed($this->request, $this->getTriggerUser()); |
|
144 | - |
|
145 | - Logger::hospitalised($this->getDatabase(), $this->request); |
|
146 | - |
|
147 | - // auto-acknowledge failed creation tasks, as these land in the hospital queue anyway. |
|
148 | - parent::markFailed($reason, true); |
|
149 | - Logger::backgroundJobAcknowledged($this->getDatabase(), $this->getJob(), "Auto-acknowledged due to request deferral to hospital queue"); |
|
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * @param $user |
|
154 | - * |
|
155 | - * @throws ApplicationLogicException |
|
156 | - */ |
|
157 | - protected function performCreation($user) |
|
158 | - { |
|
159 | - $mw = $this->getMediaWikiHelper(); |
|
160 | - |
|
161 | - $reason = $this->getCreationReason($this->request, $user); |
|
162 | - |
|
163 | - if ($this->checkAccountExists($this->request->getName())) { |
|
164 | - throw new ApplicationLogicException('Account already exists'); |
|
165 | - } |
|
166 | - |
|
167 | - $mw->createAccount($this->request->getName(), $this->request->getEmail(), $reason); |
|
168 | - |
|
169 | - if (!$this->checkAccountExists($this->request->getName())) { |
|
170 | - throw new ApplicationLogicException('Account creation appeared to succeed but account does not exist.'); |
|
171 | - } |
|
172 | - |
|
173 | - $this->request->setStatus(RequestStatus::CLOSED); |
|
174 | - $this->request->save(); |
|
175 | - } |
|
24 | + /** @var Request */ |
|
25 | + private $request; |
|
26 | + /** |
|
27 | + * @var MediaWikiHelper |
|
28 | + * Don't use this directly. |
|
29 | + */ |
|
30 | + private $mwHelper = null; |
|
31 | + |
|
32 | + public function execute() |
|
33 | + { |
|
34 | + $this->request = $this->getRequest(); |
|
35 | + $user = $this->getTriggerUser(); |
|
36 | + $parameters = $this->getParameters(); |
|
37 | + |
|
38 | + if ($this->request->getStatus() !== RequestStatus::JOBQUEUE) { |
|
39 | + $this->markCancelled('Request is not deferred to the job queue'); |
|
40 | + |
|
41 | + return; |
|
42 | + } |
|
43 | + |
|
44 | + if ($this->request->getEmailSent() != 0 && !isset($parameters->emailText)) { |
|
45 | + $this->markFailed('Request has already been sent a templated email'); |
|
46 | + |
|
47 | + return; |
|
48 | + } |
|
49 | + |
|
50 | + if ($this->request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()) { |
|
51 | + $this->markFailed('Private data of request has been purged.'); |
|
52 | + |
|
53 | + return; |
|
54 | + } |
|
55 | + |
|
56 | + $emailText = null; |
|
57 | + $ccMailingList = null; |
|
58 | + $logTarget = null; |
|
59 | + |
|
60 | + if (isset($parameters->emailText) && isset($parameters->ccMailingList)) { |
|
61 | + $emailText = $parameters->emailText; |
|
62 | + $ccMailingList = $parameters->ccMailingList; |
|
63 | + $logTarget = "custom-y"; |
|
64 | + } |
|
65 | + |
|
66 | + if ($this->getEmailTemplate() !== null) { |
|
67 | + $emailText = $this->getEmailTemplate()->getText(); |
|
68 | + $ccMailingList = false; |
|
69 | + $logTarget = $this->getEmailTemplate()->getId(); |
|
70 | + } |
|
71 | + |
|
72 | + if ($emailText === null || $ccMailingList === null) { |
|
73 | + $this->markFailed('Unable to get closure email text'); |
|
74 | + |
|
75 | + return; |
|
76 | + } |
|
77 | + |
|
78 | + try { |
|
79 | + $this->performCreation($user); |
|
80 | + |
|
81 | + $this->request->setStatus(RequestStatus::CLOSED); |
|
82 | + $this->request->setQueue(null); |
|
83 | + $this->request->setReserved(null); |
|
84 | + $this->request->setEmailSent(true); |
|
85 | + $this->request->save(); |
|
86 | + |
|
87 | + // Log the closure as the user |
|
88 | + $logComment = $this->getEmailTemplate() === null ? $emailText : null; |
|
89 | + Logger::closeRequest($this->getDatabase(), $this->request, $logTarget, $logComment, $this->getTriggerUser()); |
|
90 | + |
|
91 | + $requestEmailHelper = new RequestEmailHelper($this->getEmailHelper()); |
|
92 | + $requestEmailHelper->sendMail($this->request, $emailText, $this->getTriggerUser(), $ccMailingList); |
|
93 | + } |
|
94 | + catch (Exception $ex) { |
|
95 | + if (mb_strlen($ex->getMessage()) > 255) { |
|
96 | + ExceptionHandler::logExceptionToDisk($ex, $this->getSiteConfiguration()); |
|
97 | + } |
|
98 | + |
|
99 | + $this->markFailed(substr($ex->getMessage(), 0, 255)); |
|
100 | + |
|
101 | + return; |
|
102 | + } |
|
103 | + |
|
104 | + $this->markComplete(); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * @return IMediaWikiClient |
|
109 | + */ |
|
110 | + protected abstract function getMediaWikiClient(); |
|
111 | + |
|
112 | + protected function getMediaWikiHelper() |
|
113 | + { |
|
114 | + if ($this->mwHelper === null) { |
|
115 | + $this->mwHelper = new MediaWikiHelper($this->getMediaWikiClient(), $this->getSiteConfiguration()); |
|
116 | + } |
|
117 | + |
|
118 | + return $this->mwHelper; |
|
119 | + } |
|
120 | + |
|
121 | + /** @noinspection PhpUnusedParameterInspection */ |
|
122 | + protected function getCreationReason(Request $request, User $user) |
|
123 | + { |
|
124 | + return 'Requested account at [[WP:ACC]], request #' . $request->getId(); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @param string $name |
|
129 | + * |
|
130 | + * @return bool |
|
131 | + */ |
|
132 | + protected function checkAccountExists($name) |
|
133 | + { |
|
134 | + return $this->getMediaWikiHelper()->checkAccountExists($name); |
|
135 | + } |
|
136 | + |
|
137 | + protected function markFailed($reason = null, bool $acknowledged = false) |
|
138 | + { |
|
139 | + $this->request->setStatus(RequestStatus::HOSPITAL); |
|
140 | + $this->request->setQueue(null); |
|
141 | + $this->request->save(); |
|
142 | + |
|
143 | + $this->getNotificationHelper()->requestCreationFailed($this->request, $this->getTriggerUser()); |
|
144 | + |
|
145 | + Logger::hospitalised($this->getDatabase(), $this->request); |
|
146 | + |
|
147 | + // auto-acknowledge failed creation tasks, as these land in the hospital queue anyway. |
|
148 | + parent::markFailed($reason, true); |
|
149 | + Logger::backgroundJobAcknowledged($this->getDatabase(), $this->getJob(), "Auto-acknowledged due to request deferral to hospital queue"); |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * @param $user |
|
154 | + * |
|
155 | + * @throws ApplicationLogicException |
|
156 | + */ |
|
157 | + protected function performCreation($user) |
|
158 | + { |
|
159 | + $mw = $this->getMediaWikiHelper(); |
|
160 | + |
|
161 | + $reason = $this->getCreationReason($this->request, $user); |
|
162 | + |
|
163 | + if ($this->checkAccountExists($this->request->getName())) { |
|
164 | + throw new ApplicationLogicException('Account already exists'); |
|
165 | + } |
|
166 | + |
|
167 | + $mw->createAccount($this->request->getName(), $this->request->getEmail(), $reason); |
|
168 | + |
|
169 | + if (!$this->checkAccountExists($this->request->getName())) { |
|
170 | + throw new ApplicationLogicException('Account creation appeared to succeed but account does not exist.'); |
|
171 | + } |
|
172 | + |
|
173 | + $this->request->setStatus(RequestStatus::CLOSED); |
|
174 | + $this->request->save(); |
|
175 | + } |
|
176 | 176 | } |
177 | 177 | \ No newline at end of file |
@@ -23,268 +23,268 @@ |
||
23 | 23 | |
24 | 24 | abstract class BackgroundTaskBase |
25 | 25 | { |
26 | - /** @var JobQueue */ |
|
27 | - private $job; |
|
28 | - /** @var PdoDatabase */ |
|
29 | - private $database; |
|
30 | - /** @var IOAuthProtocolHelper */ |
|
31 | - private $oauthProtocolHelper; |
|
32 | - /** @var SiteConfiguration */ |
|
33 | - private $siteConfiguration; |
|
34 | - /** @var IEmailHelper */ |
|
35 | - private $emailHelper; |
|
36 | - /** @var HttpHelper */ |
|
37 | - private $httpHelper; |
|
38 | - /** @var IrcNotificationHelper */ |
|
39 | - private $notificationHelper; |
|
40 | - /** @var User */ |
|
41 | - private $triggerUser; |
|
42 | - /** @var Request */ |
|
43 | - private $request; |
|
44 | - /** @var EmailTemplate */ |
|
45 | - private $emailTemplate = null; |
|
46 | - /** @var mixed */ |
|
47 | - private $parameters; |
|
48 | - |
|
49 | - /** |
|
50 | - * @return JobQueue |
|
51 | - */ |
|
52 | - public function getJob() |
|
53 | - { |
|
54 | - return $this->job; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * @param JobQueue $job |
|
59 | - */ |
|
60 | - public function setJob(JobQueue $job) |
|
61 | - { |
|
62 | - $this->job = $job; |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * @return PdoDatabase |
|
67 | - */ |
|
68 | - public function getDatabase() |
|
69 | - { |
|
70 | - return $this->database; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * @param PdoDatabase $database |
|
75 | - */ |
|
76 | - public function setDatabase(PdoDatabase $database) |
|
77 | - { |
|
78 | - $this->database = $database; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * @return IOAuthProtocolHelper |
|
83 | - */ |
|
84 | - public function getOauthProtocolHelper() |
|
85 | - { |
|
86 | - return $this->oauthProtocolHelper; |
|
87 | - } |
|
88 | - |
|
89 | - /** |
|
90 | - * @param IOAuthProtocolHelper $oauthProtocolHelper |
|
91 | - */ |
|
92 | - public function setOauthProtocolHelper(IOAuthProtocolHelper $oauthProtocolHelper) |
|
93 | - { |
|
94 | - $this->oauthProtocolHelper = $oauthProtocolHelper; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @return SiteConfiguration |
|
99 | - */ |
|
100 | - public function getSiteConfiguration() |
|
101 | - { |
|
102 | - return $this->siteConfiguration; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @param SiteConfiguration $siteConfiguration |
|
107 | - */ |
|
108 | - public function setSiteConfiguration(SiteConfiguration $siteConfiguration) |
|
109 | - { |
|
110 | - $this->siteConfiguration = $siteConfiguration; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @return HttpHelper |
|
115 | - */ |
|
116 | - public function getHttpHelper() |
|
117 | - { |
|
118 | - return $this->httpHelper; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * @param HttpHelper $httpHelper |
|
123 | - */ |
|
124 | - public function setHttpHelper(HttpHelper $httpHelper) |
|
125 | - { |
|
126 | - $this->httpHelper = $httpHelper; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * @return IEmailHelper |
|
131 | - */ |
|
132 | - public function getEmailHelper() |
|
133 | - { |
|
134 | - return $this->emailHelper; |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param IEmailHelper $emailHelper |
|
139 | - */ |
|
140 | - public function setEmailHelper(IEmailHelper $emailHelper) |
|
141 | - { |
|
142 | - $this->emailHelper = $emailHelper; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @return IrcNotificationHelper |
|
147 | - */ |
|
148 | - public function getNotificationHelper() |
|
149 | - { |
|
150 | - return $this->notificationHelper; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * @param IrcNotificationHelper $notificationHelper |
|
155 | - */ |
|
156 | - public function setNotificationHelper($notificationHelper) |
|
157 | - { |
|
158 | - $this->notificationHelper = $notificationHelper; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * @return void |
|
163 | - */ |
|
164 | - protected abstract function execute(); |
|
165 | - |
|
166 | - public function run() |
|
167 | - { |
|
168 | - $this->triggerUser = User::getById($this->job->getTriggerUserId(), $this->getDatabase()); |
|
169 | - |
|
170 | - if ($this->triggerUser === false) { |
|
171 | - throw new ApplicationLogicException('Cannot locate trigger user'); |
|
172 | - } |
|
173 | - |
|
174 | - $this->request = Request::getById($this->job->getRequest(), $this->getDatabase()); |
|
175 | - |
|
176 | - if ($this->request === false) { |
|
177 | - throw new ApplicationLogicException('Cannot locate request'); |
|
178 | - } |
|
179 | - |
|
180 | - if ($this->job->getEmailTemplate() !== null) { |
|
181 | - $this->emailTemplate = EmailTemplate::getById($this->job->getEmailTemplate(), $this->getDatabase()); |
|
182 | - |
|
183 | - if ($this->emailTemplate === false) { |
|
184 | - throw new ApplicationLogicException('Cannot locate email template'); |
|
185 | - } |
|
186 | - } |
|
187 | - |
|
188 | - if ($this->job->getParameters() !== null) { |
|
189 | - $this->parameters = json_decode($this->job->getParameters()); |
|
190 | - |
|
191 | - if (json_last_error() !== JSON_ERROR_NONE) { |
|
192 | - throw new ApplicationLogicException('JSON decode: ' . json_last_error_msg()); |
|
193 | - } |
|
194 | - } |
|
195 | - |
|
196 | - // Should we wait for a parent job? |
|
197 | - if ($this->job->getParent() !== null) { |
|
198 | - /** @var JobQueue $parentJob */ |
|
199 | - $parentJob = JobQueue::getById($this->job->getParent(), $this->getDatabase()); |
|
200 | - |
|
201 | - if ($parentJob === false) { |
|
202 | - $this->markFailed("Parent job could not be found"); |
|
203 | - return; |
|
204 | - } |
|
205 | - |
|
206 | - switch ($parentJob->getStatus()) { |
|
207 | - case JobQueue::STATUS_CANCELLED: |
|
208 | - case JobQueue::STATUS_FAILED: |
|
209 | - $this->markCancelled('Parent job failed/cancelled'); |
|
210 | - return; |
|
211 | - case JobQueue::STATUS_WAITING: |
|
212 | - case JobQueue::STATUS_READY: |
|
213 | - case JobQueue::STATUS_QUEUED: |
|
214 | - case JobQueue::STATUS_RUNNING: |
|
215 | - case JobQueue::STATUS_HELD: |
|
216 | - // Defer to next execution |
|
217 | - $this->job->setStatus(JobQueue::STATUS_READY); |
|
218 | - $this->job->save(); |
|
219 | - return; |
|
220 | - case JobQueue::STATUS_COMPLETE: |
|
221 | - // do nothing |
|
222 | - break; |
|
223 | - } |
|
224 | - } |
|
225 | - |
|
226 | - $this->execute(); |
|
227 | - } |
|
228 | - |
|
229 | - protected function markComplete() |
|
230 | - { |
|
231 | - $this->job->setStatus(JobQueue::STATUS_COMPLETE); |
|
232 | - $this->job->setError(null); |
|
233 | - $this->job->setAcknowledged(null); |
|
234 | - $this->job->save(); |
|
235 | - |
|
236 | - Logger::backgroundJobComplete($this->getDatabase(), $this->getJob()); |
|
237 | - } |
|
238 | - |
|
239 | - protected function markCancelled($reason = null) |
|
240 | - { |
|
241 | - $this->job->setStatus(JobQueue::STATUS_CANCELLED); |
|
242 | - $this->job->setError($reason); |
|
243 | - $this->job->setAcknowledged(null); |
|
244 | - $this->job->save(); |
|
245 | - |
|
246 | - Logger::backgroundJobIssue($this->getDatabase(), $this->getJob()); |
|
247 | - } |
|
248 | - |
|
249 | - protected function markFailed($reason = null, bool $acknowledged = false) |
|
250 | - { |
|
251 | - $this->job->setStatus(JobQueue::STATUS_FAILED); |
|
252 | - $this->job->setError($reason); |
|
253 | - $this->job->setAcknowledged($acknowledged ? 1 : 0); |
|
254 | - $this->job->save(); |
|
255 | - |
|
256 | - Logger::backgroundJobIssue($this->getDatabase(), $this->getJob()); |
|
257 | - } |
|
258 | - |
|
259 | - /** |
|
260 | - * @return User |
|
261 | - */ |
|
262 | - public function getTriggerUser() |
|
263 | - { |
|
264 | - return $this->triggerUser; |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * @return Request |
|
269 | - */ |
|
270 | - public function getRequest() |
|
271 | - { |
|
272 | - return $this->request; |
|
273 | - } |
|
274 | - |
|
275 | - /** |
|
276 | - * @return EmailTemplate |
|
277 | - */ |
|
278 | - public function getEmailTemplate() |
|
279 | - { |
|
280 | - return $this->emailTemplate; |
|
281 | - } |
|
282 | - |
|
283 | - /** |
|
284 | - * @return mixed |
|
285 | - */ |
|
286 | - public function getParameters() |
|
287 | - { |
|
288 | - return $this->parameters; |
|
289 | - } |
|
26 | + /** @var JobQueue */ |
|
27 | + private $job; |
|
28 | + /** @var PdoDatabase */ |
|
29 | + private $database; |
|
30 | + /** @var IOAuthProtocolHelper */ |
|
31 | + private $oauthProtocolHelper; |
|
32 | + /** @var SiteConfiguration */ |
|
33 | + private $siteConfiguration; |
|
34 | + /** @var IEmailHelper */ |
|
35 | + private $emailHelper; |
|
36 | + /** @var HttpHelper */ |
|
37 | + private $httpHelper; |
|
38 | + /** @var IrcNotificationHelper */ |
|
39 | + private $notificationHelper; |
|
40 | + /** @var User */ |
|
41 | + private $triggerUser; |
|
42 | + /** @var Request */ |
|
43 | + private $request; |
|
44 | + /** @var EmailTemplate */ |
|
45 | + private $emailTemplate = null; |
|
46 | + /** @var mixed */ |
|
47 | + private $parameters; |
|
48 | + |
|
49 | + /** |
|
50 | + * @return JobQueue |
|
51 | + */ |
|
52 | + public function getJob() |
|
53 | + { |
|
54 | + return $this->job; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * @param JobQueue $job |
|
59 | + */ |
|
60 | + public function setJob(JobQueue $job) |
|
61 | + { |
|
62 | + $this->job = $job; |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * @return PdoDatabase |
|
67 | + */ |
|
68 | + public function getDatabase() |
|
69 | + { |
|
70 | + return $this->database; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * @param PdoDatabase $database |
|
75 | + */ |
|
76 | + public function setDatabase(PdoDatabase $database) |
|
77 | + { |
|
78 | + $this->database = $database; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * @return IOAuthProtocolHelper |
|
83 | + */ |
|
84 | + public function getOauthProtocolHelper() |
|
85 | + { |
|
86 | + return $this->oauthProtocolHelper; |
|
87 | + } |
|
88 | + |
|
89 | + /** |
|
90 | + * @param IOAuthProtocolHelper $oauthProtocolHelper |
|
91 | + */ |
|
92 | + public function setOauthProtocolHelper(IOAuthProtocolHelper $oauthProtocolHelper) |
|
93 | + { |
|
94 | + $this->oauthProtocolHelper = $oauthProtocolHelper; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @return SiteConfiguration |
|
99 | + */ |
|
100 | + public function getSiteConfiguration() |
|
101 | + { |
|
102 | + return $this->siteConfiguration; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @param SiteConfiguration $siteConfiguration |
|
107 | + */ |
|
108 | + public function setSiteConfiguration(SiteConfiguration $siteConfiguration) |
|
109 | + { |
|
110 | + $this->siteConfiguration = $siteConfiguration; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @return HttpHelper |
|
115 | + */ |
|
116 | + public function getHttpHelper() |
|
117 | + { |
|
118 | + return $this->httpHelper; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * @param HttpHelper $httpHelper |
|
123 | + */ |
|
124 | + public function setHttpHelper(HttpHelper $httpHelper) |
|
125 | + { |
|
126 | + $this->httpHelper = $httpHelper; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * @return IEmailHelper |
|
131 | + */ |
|
132 | + public function getEmailHelper() |
|
133 | + { |
|
134 | + return $this->emailHelper; |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param IEmailHelper $emailHelper |
|
139 | + */ |
|
140 | + public function setEmailHelper(IEmailHelper $emailHelper) |
|
141 | + { |
|
142 | + $this->emailHelper = $emailHelper; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @return IrcNotificationHelper |
|
147 | + */ |
|
148 | + public function getNotificationHelper() |
|
149 | + { |
|
150 | + return $this->notificationHelper; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * @param IrcNotificationHelper $notificationHelper |
|
155 | + */ |
|
156 | + public function setNotificationHelper($notificationHelper) |
|
157 | + { |
|
158 | + $this->notificationHelper = $notificationHelper; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * @return void |
|
163 | + */ |
|
164 | + protected abstract function execute(); |
|
165 | + |
|
166 | + public function run() |
|
167 | + { |
|
168 | + $this->triggerUser = User::getById($this->job->getTriggerUserId(), $this->getDatabase()); |
|
169 | + |
|
170 | + if ($this->triggerUser === false) { |
|
171 | + throw new ApplicationLogicException('Cannot locate trigger user'); |
|
172 | + } |
|
173 | + |
|
174 | + $this->request = Request::getById($this->job->getRequest(), $this->getDatabase()); |
|
175 | + |
|
176 | + if ($this->request === false) { |
|
177 | + throw new ApplicationLogicException('Cannot locate request'); |
|
178 | + } |
|
179 | + |
|
180 | + if ($this->job->getEmailTemplate() !== null) { |
|
181 | + $this->emailTemplate = EmailTemplate::getById($this->job->getEmailTemplate(), $this->getDatabase()); |
|
182 | + |
|
183 | + if ($this->emailTemplate === false) { |
|
184 | + throw new ApplicationLogicException('Cannot locate email template'); |
|
185 | + } |
|
186 | + } |
|
187 | + |
|
188 | + if ($this->job->getParameters() !== null) { |
|
189 | + $this->parameters = json_decode($this->job->getParameters()); |
|
190 | + |
|
191 | + if (json_last_error() !== JSON_ERROR_NONE) { |
|
192 | + throw new ApplicationLogicException('JSON decode: ' . json_last_error_msg()); |
|
193 | + } |
|
194 | + } |
|
195 | + |
|
196 | + // Should we wait for a parent job? |
|
197 | + if ($this->job->getParent() !== null) { |
|
198 | + /** @var JobQueue $parentJob */ |
|
199 | + $parentJob = JobQueue::getById($this->job->getParent(), $this->getDatabase()); |
|
200 | + |
|
201 | + if ($parentJob === false) { |
|
202 | + $this->markFailed("Parent job could not be found"); |
|
203 | + return; |
|
204 | + } |
|
205 | + |
|
206 | + switch ($parentJob->getStatus()) { |
|
207 | + case JobQueue::STATUS_CANCELLED: |
|
208 | + case JobQueue::STATUS_FAILED: |
|
209 | + $this->markCancelled('Parent job failed/cancelled'); |
|
210 | + return; |
|
211 | + case JobQueue::STATUS_WAITING: |
|
212 | + case JobQueue::STATUS_READY: |
|
213 | + case JobQueue::STATUS_QUEUED: |
|
214 | + case JobQueue::STATUS_RUNNING: |
|
215 | + case JobQueue::STATUS_HELD: |
|
216 | + // Defer to next execution |
|
217 | + $this->job->setStatus(JobQueue::STATUS_READY); |
|
218 | + $this->job->save(); |
|
219 | + return; |
|
220 | + case JobQueue::STATUS_COMPLETE: |
|
221 | + // do nothing |
|
222 | + break; |
|
223 | + } |
|
224 | + } |
|
225 | + |
|
226 | + $this->execute(); |
|
227 | + } |
|
228 | + |
|
229 | + protected function markComplete() |
|
230 | + { |
|
231 | + $this->job->setStatus(JobQueue::STATUS_COMPLETE); |
|
232 | + $this->job->setError(null); |
|
233 | + $this->job->setAcknowledged(null); |
|
234 | + $this->job->save(); |
|
235 | + |
|
236 | + Logger::backgroundJobComplete($this->getDatabase(), $this->getJob()); |
|
237 | + } |
|
238 | + |
|
239 | + protected function markCancelled($reason = null) |
|
240 | + { |
|
241 | + $this->job->setStatus(JobQueue::STATUS_CANCELLED); |
|
242 | + $this->job->setError($reason); |
|
243 | + $this->job->setAcknowledged(null); |
|
244 | + $this->job->save(); |
|
245 | + |
|
246 | + Logger::backgroundJobIssue($this->getDatabase(), $this->getJob()); |
|
247 | + } |
|
248 | + |
|
249 | + protected function markFailed($reason = null, bool $acknowledged = false) |
|
250 | + { |
|
251 | + $this->job->setStatus(JobQueue::STATUS_FAILED); |
|
252 | + $this->job->setError($reason); |
|
253 | + $this->job->setAcknowledged($acknowledged ? 1 : 0); |
|
254 | + $this->job->save(); |
|
255 | + |
|
256 | + Logger::backgroundJobIssue($this->getDatabase(), $this->getJob()); |
|
257 | + } |
|
258 | + |
|
259 | + /** |
|
260 | + * @return User |
|
261 | + */ |
|
262 | + public function getTriggerUser() |
|
263 | + { |
|
264 | + return $this->triggerUser; |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * @return Request |
|
269 | + */ |
|
270 | + public function getRequest() |
|
271 | + { |
|
272 | + return $this->request; |
|
273 | + } |
|
274 | + |
|
275 | + /** |
|
276 | + * @return EmailTemplate |
|
277 | + */ |
|
278 | + public function getEmailTemplate() |
|
279 | + { |
|
280 | + return $this->emailTemplate; |
|
281 | + } |
|
282 | + |
|
283 | + /** |
|
284 | + * @return mixed |
|
285 | + */ |
|
286 | + public function getParameters() |
|
287 | + { |
|
288 | + return $this->parameters; |
|
289 | + } |
|
290 | 290 | } |
@@ -15,270 +15,270 @@ |
||
15 | 15 | |
16 | 16 | class MediaWikiHelper |
17 | 17 | { |
18 | - /** |
|
19 | - * @var IMediaWikiClient |
|
20 | - */ |
|
21 | - private $mediaWikiClient; |
|
22 | - /** |
|
23 | - * @var SiteConfiguration |
|
24 | - */ |
|
25 | - private $siteConfiguration; |
|
26 | - |
|
27 | - /** |
|
28 | - * MediaWikiHelper constructor. |
|
29 | - * |
|
30 | - * @param IMediaWikiClient $mediaWikiClient |
|
31 | - * @param SiteConfiguration $siteConfiguration |
|
32 | - */ |
|
33 | - public function __construct(IMediaWikiClient $mediaWikiClient, SiteConfiguration $siteConfiguration) |
|
34 | - { |
|
35 | - $this->mediaWikiClient = $mediaWikiClient; |
|
36 | - $this->siteConfiguration = $siteConfiguration; |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * @todo handle override antispoof and titleblacklist issues |
|
41 | - * |
|
42 | - * @param string $username |
|
43 | - * @param string $emailAddress |
|
44 | - * @param string $reason |
|
45 | - * |
|
46 | - * @throws Exception |
|
47 | - * @throws MediaWikiApiException |
|
48 | - */ |
|
49 | - public function createAccount($username, $emailAddress, $reason) |
|
50 | - { |
|
51 | - // get token |
|
52 | - $tokenParams = array( |
|
53 | - 'action' => 'query', |
|
54 | - 'meta' => 'tokens', |
|
55 | - 'type' => 'createaccount', |
|
56 | - ); |
|
57 | - |
|
58 | - $response = $this->mediaWikiClient->doApiCall($tokenParams, 'POST'); |
|
59 | - |
|
60 | - if (isset($response->error)) { |
|
61 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
62 | - } |
|
63 | - |
|
64 | - $token = $response->query->tokens->createaccounttoken; |
|
65 | - |
|
66 | - $callback = $this->siteConfiguration->getBaseUrl() . '/internal.php/oauth/createCallback'; |
|
67 | - |
|
68 | - $checkboxFields = array(); |
|
69 | - $requiredFields = array(); |
|
70 | - $this->getCreationFieldData($requiredFields, $checkboxFields); |
|
71 | - |
|
72 | - $apiCallData = array( |
|
73 | - 'action' => 'createaccount', |
|
74 | - 'createreturnurl' => $callback, |
|
75 | - 'createtoken' => $token, |
|
76 | - 'createmessageformat' => 'html', |
|
77 | - ); |
|
78 | - |
|
79 | - $createParams = array_fill_keys($requiredFields, '') + $apiCallData; |
|
80 | - |
|
81 | - $createParams['username'] = $username; |
|
82 | - $createParams['mailpassword'] = true; |
|
83 | - $createParams['email'] = $emailAddress; |
|
84 | - $createParams['reason'] = $reason; |
|
85 | - |
|
86 | - $createResponse = $this->mediaWikiClient->doApiCall($createParams, 'POST'); |
|
87 | - |
|
88 | - if (isset($createResponse->error)) { |
|
89 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
90 | - } |
|
91 | - |
|
92 | - if (!isset($createResponse->createaccount) || !isset($createResponse->createaccount->status)) { |
|
93 | - throw new MediaWikiApiException('Unknown error creating account'); |
|
94 | - } |
|
95 | - |
|
96 | - if ($createResponse->createaccount->status === 'FAIL') { |
|
97 | - throw new MediaWikiApiException($createResponse->createaccount->message); |
|
98 | - } |
|
99 | - |
|
100 | - if ($createResponse->createaccount->status === 'PASS') { |
|
101 | - // success! |
|
102 | - return; |
|
103 | - } |
|
104 | - |
|
105 | - throw new Exception('API result reported status of ' . $createResponse->createaccount->status); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * @param string $username |
|
110 | - * @param string $title |
|
111 | - * @param $summary |
|
112 | - * @param string $message |
|
113 | - * @param bool $createOnly |
|
114 | - * |
|
115 | - * @throws MediaWikiApiException |
|
116 | - */ |
|
117 | - public function addTalkPageMessage($username, $title, $summary, $message, $createOnly = true) |
|
118 | - { |
|
119 | - // get token |
|
120 | - $tokenParams = array( |
|
121 | - 'action' => 'query', |
|
122 | - 'meta' => 'tokens', |
|
123 | - 'type' => 'csrf', |
|
124 | - ); |
|
125 | - |
|
126 | - $response = $this->mediaWikiClient->doApiCall($tokenParams, 'POST'); |
|
127 | - |
|
128 | - if (isset($response->error)) { |
|
129 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
130 | - } |
|
131 | - |
|
132 | - $token = $response->query->tokens->csrftoken; |
|
133 | - |
|
134 | - if ($token === null) { |
|
135 | - throw new MediaWikiApiException('Edit token could not be acquired'); |
|
136 | - } |
|
137 | - |
|
138 | - $editParameters = array( |
|
139 | - 'action' => 'edit', |
|
140 | - 'title' => 'User talk:' . $username, |
|
141 | - 'section' => 'new', |
|
142 | - 'sectiontitle' => $title, |
|
143 | - 'summary' => $summary, |
|
144 | - 'text' => $message, |
|
145 | - 'token' => $token, |
|
146 | - ); |
|
147 | - |
|
148 | - if ($createOnly) { |
|
149 | - $editParameters['createonly'] = true; |
|
150 | - } |
|
151 | - |
|
152 | - $response = $this->mediaWikiClient->doApiCall($editParameters, 'POST'); |
|
153 | - |
|
154 | - if (!isset($response->edit)) { |
|
155 | - if (isset($response->error)) { |
|
156 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
157 | - } |
|
158 | - |
|
159 | - throw new MediaWikiApiException('Unknown error encountered during editing.'); |
|
160 | - } |
|
161 | - |
|
162 | - $editResponse = $response->edit; |
|
163 | - if ($editResponse->result === "Success") { |
|
164 | - return; |
|
165 | - } |
|
166 | - |
|
167 | - throw new MediaWikiApiException('Edit status unsuccessful: ' . $editResponse->result); |
|
168 | - } |
|
169 | - |
|
170 | - public function getCreationFieldData(&$requiredFields, &$checkboxFields) |
|
171 | - { |
|
172 | - // get token |
|
173 | - $params = array( |
|
174 | - 'action' => 'query', |
|
175 | - 'meta' => 'authmanagerinfo', |
|
176 | - 'amirequestsfor' => 'create', |
|
177 | - ); |
|
178 | - |
|
179 | - $response = $this->mediaWikiClient->doApiCall($params, 'GET'); |
|
180 | - |
|
181 | - if (isset($response->error)) { |
|
182 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
183 | - } |
|
184 | - |
|
185 | - $requests = $response->query->authmanagerinfo->requests; |
|
186 | - |
|
187 | - // We don't want to deal with these providers ever. |
|
188 | - $discardList = array( |
|
189 | - // Requires a username and password |
|
190 | - 'MediaWiki\\Auth\\PasswordAuthenticationRequest', |
|
191 | - ); |
|
192 | - |
|
193 | - // We require these providers to function |
|
194 | - $requireList = array( |
|
195 | - 'MediaWiki\\Auth\\TemporaryPasswordAuthenticationRequest', |
|
196 | - 'MediaWiki\\Auth\\UsernameAuthenticationRequest', |
|
197 | - 'MediaWiki\\Auth\\UserDataAuthenticationRequest', |
|
198 | - 'MediaWiki\\Auth\\CreationReasonAuthenticationRequest', |
|
199 | - ); |
|
200 | - |
|
201 | - $requiredFields = array(); |
|
202 | - // Keep checkbox fields separate, since "required" actually means optional as absent == false. |
|
203 | - $checkboxFields = array(); |
|
204 | - |
|
205 | - foreach ($requests as $req) { |
|
206 | - // Immediately discard anything that is on the discard list. |
|
207 | - if (in_array($req->id, $discardList)) { |
|
208 | - continue; |
|
209 | - } |
|
210 | - |
|
211 | - $required = false; |
|
212 | - |
|
213 | - if ($req->required === 'primary-required' && !in_array($req->id, $requireList)) { |
|
214 | - // Only want one. |
|
215 | - continue; |
|
216 | - } |
|
217 | - |
|
218 | - if (in_array($req->id, $requireList)) { |
|
219 | - unset($requireList[$req->id]); |
|
220 | - $required = true; |
|
221 | - } |
|
222 | - |
|
223 | - if ($req->required === 'required') { |
|
224 | - $required = true; |
|
225 | - } |
|
226 | - |
|
227 | - if ($required) { |
|
228 | - foreach ($req->fields as $name => $data) { |
|
229 | - if ($data->type === 'checkbox') { |
|
230 | - $checkboxFields[] = $name; |
|
231 | - } |
|
232 | - else { |
|
233 | - $requiredFields[] = $name; |
|
234 | - } |
|
235 | - } |
|
236 | - } |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * @param string $username |
|
242 | - * @return bool |
|
243 | - */ |
|
244 | - public function checkAccountExists($username) |
|
245 | - { |
|
246 | - $parameters = array( |
|
247 | - 'action' => 'query', |
|
248 | - 'list' => 'users', |
|
249 | - 'format' => 'php', |
|
250 | - 'ususers' => $username, |
|
251 | - ); |
|
252 | - |
|
253 | - $apiResult = $this->mediaWikiClient->doApiCall($parameters, 'GET'); |
|
254 | - |
|
255 | - $entry = $apiResult->query->users[0]; |
|
256 | - $exists = !isset($entry->missing); |
|
257 | - |
|
258 | - return $exists; |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Gets the HTML for the provided wiki-markup |
|
263 | - * |
|
264 | - * @param string $wikiText |
|
265 | - * |
|
266 | - * @return string |
|
267 | - */ |
|
268 | - public function getHtmlForWikiText($wikiText) |
|
269 | - { |
|
270 | - $parameters = array( |
|
271 | - 'action' => 'parse', |
|
272 | - 'pst' => true, |
|
273 | - 'contentmodel' => 'wikitext', |
|
274 | - 'disablelimitreport' => true, |
|
275 | - 'disabletoc' => true, |
|
276 | - 'disableeditsection' => true, |
|
277 | - 'text' => $wikiText, |
|
278 | - ); |
|
279 | - |
|
280 | - $apiResult = $this->mediaWikiClient->doApiCall($parameters, 'GET'); |
|
281 | - |
|
282 | - return $apiResult->parse->text->{'*'}; |
|
283 | - } |
|
18 | + /** |
|
19 | + * @var IMediaWikiClient |
|
20 | + */ |
|
21 | + private $mediaWikiClient; |
|
22 | + /** |
|
23 | + * @var SiteConfiguration |
|
24 | + */ |
|
25 | + private $siteConfiguration; |
|
26 | + |
|
27 | + /** |
|
28 | + * MediaWikiHelper constructor. |
|
29 | + * |
|
30 | + * @param IMediaWikiClient $mediaWikiClient |
|
31 | + * @param SiteConfiguration $siteConfiguration |
|
32 | + */ |
|
33 | + public function __construct(IMediaWikiClient $mediaWikiClient, SiteConfiguration $siteConfiguration) |
|
34 | + { |
|
35 | + $this->mediaWikiClient = $mediaWikiClient; |
|
36 | + $this->siteConfiguration = $siteConfiguration; |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * @todo handle override antispoof and titleblacklist issues |
|
41 | + * |
|
42 | + * @param string $username |
|
43 | + * @param string $emailAddress |
|
44 | + * @param string $reason |
|
45 | + * |
|
46 | + * @throws Exception |
|
47 | + * @throws MediaWikiApiException |
|
48 | + */ |
|
49 | + public function createAccount($username, $emailAddress, $reason) |
|
50 | + { |
|
51 | + // get token |
|
52 | + $tokenParams = array( |
|
53 | + 'action' => 'query', |
|
54 | + 'meta' => 'tokens', |
|
55 | + 'type' => 'createaccount', |
|
56 | + ); |
|
57 | + |
|
58 | + $response = $this->mediaWikiClient->doApiCall($tokenParams, 'POST'); |
|
59 | + |
|
60 | + if (isset($response->error)) { |
|
61 | + throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
62 | + } |
|
63 | + |
|
64 | + $token = $response->query->tokens->createaccounttoken; |
|
65 | + |
|
66 | + $callback = $this->siteConfiguration->getBaseUrl() . '/internal.php/oauth/createCallback'; |
|
67 | + |
|
68 | + $checkboxFields = array(); |
|
69 | + $requiredFields = array(); |
|
70 | + $this->getCreationFieldData($requiredFields, $checkboxFields); |
|
71 | + |
|
72 | + $apiCallData = array( |
|
73 | + 'action' => 'createaccount', |
|
74 | + 'createreturnurl' => $callback, |
|
75 | + 'createtoken' => $token, |
|
76 | + 'createmessageformat' => 'html', |
|
77 | + ); |
|
78 | + |
|
79 | + $createParams = array_fill_keys($requiredFields, '') + $apiCallData; |
|
80 | + |
|
81 | + $createParams['username'] = $username; |
|
82 | + $createParams['mailpassword'] = true; |
|
83 | + $createParams['email'] = $emailAddress; |
|
84 | + $createParams['reason'] = $reason; |
|
85 | + |
|
86 | + $createResponse = $this->mediaWikiClient->doApiCall($createParams, 'POST'); |
|
87 | + |
|
88 | + if (isset($createResponse->error)) { |
|
89 | + throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
90 | + } |
|
91 | + |
|
92 | + if (!isset($createResponse->createaccount) || !isset($createResponse->createaccount->status)) { |
|
93 | + throw new MediaWikiApiException('Unknown error creating account'); |
|
94 | + } |
|
95 | + |
|
96 | + if ($createResponse->createaccount->status === 'FAIL') { |
|
97 | + throw new MediaWikiApiException($createResponse->createaccount->message); |
|
98 | + } |
|
99 | + |
|
100 | + if ($createResponse->createaccount->status === 'PASS') { |
|
101 | + // success! |
|
102 | + return; |
|
103 | + } |
|
104 | + |
|
105 | + throw new Exception('API result reported status of ' . $createResponse->createaccount->status); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * @param string $username |
|
110 | + * @param string $title |
|
111 | + * @param $summary |
|
112 | + * @param string $message |
|
113 | + * @param bool $createOnly |
|
114 | + * |
|
115 | + * @throws MediaWikiApiException |
|
116 | + */ |
|
117 | + public function addTalkPageMessage($username, $title, $summary, $message, $createOnly = true) |
|
118 | + { |
|
119 | + // get token |
|
120 | + $tokenParams = array( |
|
121 | + 'action' => 'query', |
|
122 | + 'meta' => 'tokens', |
|
123 | + 'type' => 'csrf', |
|
124 | + ); |
|
125 | + |
|
126 | + $response = $this->mediaWikiClient->doApiCall($tokenParams, 'POST'); |
|
127 | + |
|
128 | + if (isset($response->error)) { |
|
129 | + throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
130 | + } |
|
131 | + |
|
132 | + $token = $response->query->tokens->csrftoken; |
|
133 | + |
|
134 | + if ($token === null) { |
|
135 | + throw new MediaWikiApiException('Edit token could not be acquired'); |
|
136 | + } |
|
137 | + |
|
138 | + $editParameters = array( |
|
139 | + 'action' => 'edit', |
|
140 | + 'title' => 'User talk:' . $username, |
|
141 | + 'section' => 'new', |
|
142 | + 'sectiontitle' => $title, |
|
143 | + 'summary' => $summary, |
|
144 | + 'text' => $message, |
|
145 | + 'token' => $token, |
|
146 | + ); |
|
147 | + |
|
148 | + if ($createOnly) { |
|
149 | + $editParameters['createonly'] = true; |
|
150 | + } |
|
151 | + |
|
152 | + $response = $this->mediaWikiClient->doApiCall($editParameters, 'POST'); |
|
153 | + |
|
154 | + if (!isset($response->edit)) { |
|
155 | + if (isset($response->error)) { |
|
156 | + throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
157 | + } |
|
158 | + |
|
159 | + throw new MediaWikiApiException('Unknown error encountered during editing.'); |
|
160 | + } |
|
161 | + |
|
162 | + $editResponse = $response->edit; |
|
163 | + if ($editResponse->result === "Success") { |
|
164 | + return; |
|
165 | + } |
|
166 | + |
|
167 | + throw new MediaWikiApiException('Edit status unsuccessful: ' . $editResponse->result); |
|
168 | + } |
|
169 | + |
|
170 | + public function getCreationFieldData(&$requiredFields, &$checkboxFields) |
|
171 | + { |
|
172 | + // get token |
|
173 | + $params = array( |
|
174 | + 'action' => 'query', |
|
175 | + 'meta' => 'authmanagerinfo', |
|
176 | + 'amirequestsfor' => 'create', |
|
177 | + ); |
|
178 | + |
|
179 | + $response = $this->mediaWikiClient->doApiCall($params, 'GET'); |
|
180 | + |
|
181 | + if (isset($response->error)) { |
|
182 | + throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
183 | + } |
|
184 | + |
|
185 | + $requests = $response->query->authmanagerinfo->requests; |
|
186 | + |
|
187 | + // We don't want to deal with these providers ever. |
|
188 | + $discardList = array( |
|
189 | + // Requires a username and password |
|
190 | + 'MediaWiki\\Auth\\PasswordAuthenticationRequest', |
|
191 | + ); |
|
192 | + |
|
193 | + // We require these providers to function |
|
194 | + $requireList = array( |
|
195 | + 'MediaWiki\\Auth\\TemporaryPasswordAuthenticationRequest', |
|
196 | + 'MediaWiki\\Auth\\UsernameAuthenticationRequest', |
|
197 | + 'MediaWiki\\Auth\\UserDataAuthenticationRequest', |
|
198 | + 'MediaWiki\\Auth\\CreationReasonAuthenticationRequest', |
|
199 | + ); |
|
200 | + |
|
201 | + $requiredFields = array(); |
|
202 | + // Keep checkbox fields separate, since "required" actually means optional as absent == false. |
|
203 | + $checkboxFields = array(); |
|
204 | + |
|
205 | + foreach ($requests as $req) { |
|
206 | + // Immediately discard anything that is on the discard list. |
|
207 | + if (in_array($req->id, $discardList)) { |
|
208 | + continue; |
|
209 | + } |
|
210 | + |
|
211 | + $required = false; |
|
212 | + |
|
213 | + if ($req->required === 'primary-required' && !in_array($req->id, $requireList)) { |
|
214 | + // Only want one. |
|
215 | + continue; |
|
216 | + } |
|
217 | + |
|
218 | + if (in_array($req->id, $requireList)) { |
|
219 | + unset($requireList[$req->id]); |
|
220 | + $required = true; |
|
221 | + } |
|
222 | + |
|
223 | + if ($req->required === 'required') { |
|
224 | + $required = true; |
|
225 | + } |
|
226 | + |
|
227 | + if ($required) { |
|
228 | + foreach ($req->fields as $name => $data) { |
|
229 | + if ($data->type === 'checkbox') { |
|
230 | + $checkboxFields[] = $name; |
|
231 | + } |
|
232 | + else { |
|
233 | + $requiredFields[] = $name; |
|
234 | + } |
|
235 | + } |
|
236 | + } |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * @param string $username |
|
242 | + * @return bool |
|
243 | + */ |
|
244 | + public function checkAccountExists($username) |
|
245 | + { |
|
246 | + $parameters = array( |
|
247 | + 'action' => 'query', |
|
248 | + 'list' => 'users', |
|
249 | + 'format' => 'php', |
|
250 | + 'ususers' => $username, |
|
251 | + ); |
|
252 | + |
|
253 | + $apiResult = $this->mediaWikiClient->doApiCall($parameters, 'GET'); |
|
254 | + |
|
255 | + $entry = $apiResult->query->users[0]; |
|
256 | + $exists = !isset($entry->missing); |
|
257 | + |
|
258 | + return $exists; |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Gets the HTML for the provided wiki-markup |
|
263 | + * |
|
264 | + * @param string $wikiText |
|
265 | + * |
|
266 | + * @return string |
|
267 | + */ |
|
268 | + public function getHtmlForWikiText($wikiText) |
|
269 | + { |
|
270 | + $parameters = array( |
|
271 | + 'action' => 'parse', |
|
272 | + 'pst' => true, |
|
273 | + 'contentmodel' => 'wikitext', |
|
274 | + 'disablelimitreport' => true, |
|
275 | + 'disabletoc' => true, |
|
276 | + 'disableeditsection' => true, |
|
277 | + 'text' => $wikiText, |
|
278 | + ); |
|
279 | + |
|
280 | + $apiResult = $this->mediaWikiClient->doApiCall($parameters, 'GET'); |
|
281 | + |
|
282 | + return $apiResult->parse->text->{'*'}; |
|
283 | + } |
|
284 | 284 | } |
@@ -228,8 +228,7 @@ |
||
228 | 228 | foreach ($req->fields as $name => $data) { |
229 | 229 | if ($data->type === 'checkbox') { |
230 | 230 | $checkboxFields[] = $name; |
231 | - } |
|
232 | - else { |
|
231 | + } else { |
|
233 | 232 | $requiredFields[] = $name; |
234 | 233 | } |
235 | 234 | } |
@@ -15,57 +15,57 @@ |
||
15 | 15 | |
16 | 16 | class RequestEmailHelper |
17 | 17 | { |
18 | - /** |
|
19 | - * @var IEmailHelper |
|
20 | - */ |
|
21 | - private $emailHelper; |
|
18 | + /** |
|
19 | + * @var IEmailHelper |
|
20 | + */ |
|
21 | + private $emailHelper; |
|
22 | 22 | |
23 | - /** |
|
24 | - * RequestEmailHelper constructor. |
|
25 | - * |
|
26 | - * @param IEmailHelper $emailHelper |
|
27 | - */ |
|
28 | - public function __construct(IEmailHelper $emailHelper) |
|
29 | - { |
|
30 | - $this->emailHelper = $emailHelper; |
|
31 | - } |
|
23 | + /** |
|
24 | + * RequestEmailHelper constructor. |
|
25 | + * |
|
26 | + * @param IEmailHelper $emailHelper |
|
27 | + */ |
|
28 | + public function __construct(IEmailHelper $emailHelper) |
|
29 | + { |
|
30 | + $this->emailHelper = $emailHelper; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * @param Request $request |
|
35 | - * @param string $mailText |
|
36 | - * @param User $sendingUser The user sending the email |
|
37 | - * @param boolean $ccMailingList |
|
38 | - */ |
|
39 | - public function sendMail(Request $request, $mailText, User $sendingUser, $ccMailingList) |
|
40 | - { |
|
41 | - $headers = array( |
|
42 | - 'X-ACC-Request' => $request->getId(), |
|
43 | - 'X-ACC-UserID' => $sendingUser->getId(), |
|
44 | - ); |
|
33 | + /** |
|
34 | + * @param Request $request |
|
35 | + * @param string $mailText |
|
36 | + * @param User $sendingUser The user sending the email |
|
37 | + * @param boolean $ccMailingList |
|
38 | + */ |
|
39 | + public function sendMail(Request $request, $mailText, User $sendingUser, $ccMailingList) |
|
40 | + { |
|
41 | + $headers = array( |
|
42 | + 'X-ACC-Request' => $request->getId(), |
|
43 | + 'X-ACC-UserID' => $sendingUser->getId(), |
|
44 | + ); |
|
45 | 45 | |
46 | - // FIXME: domains! |
|
47 | - /** @var Domain $domain */ |
|
48 | - $domain = Domain::getById(1, $request->getDatabase()); |
|
46 | + // FIXME: domains! |
|
47 | + /** @var Domain $domain */ |
|
48 | + $domain = Domain::getById(1, $request->getDatabase()); |
|
49 | 49 | |
50 | - if ($ccMailingList) { |
|
51 | - $headers['Cc'] = $domain->getEmailReplyAddress(); |
|
52 | - } |
|
50 | + if ($ccMailingList) { |
|
51 | + $headers['Cc'] = $domain->getEmailReplyAddress(); |
|
52 | + } |
|
53 | 53 | |
54 | - $helper = $this->emailHelper; |
|
54 | + $helper = $this->emailHelper; |
|
55 | 55 | |
56 | - // FIXME: domains |
|
57 | - $preferenceManager = new PreferenceManager($request->getDatabase(), $sendingUser->getId(), 1); |
|
56 | + // FIXME: domains |
|
57 | + $preferenceManager = new PreferenceManager($request->getDatabase(), $sendingUser->getId(), 1); |
|
58 | 58 | |
59 | - $emailSig = $preferenceManager->getPreference(PreferenceManager::PREF_EMAIL_SIGNATURE); |
|
60 | - if ($emailSig !== '' || $emailSig !== null) { |
|
61 | - $emailSig = "\n\n" . $emailSig; |
|
62 | - } |
|
59 | + $emailSig = $preferenceManager->getPreference(PreferenceManager::PREF_EMAIL_SIGNATURE); |
|
60 | + if ($emailSig !== '' || $emailSig !== null) { |
|
61 | + $emailSig = "\n\n" . $emailSig; |
|
62 | + } |
|
63 | 63 | |
64 | - $subject = "RE: [ACC #{$request->getId()}] English Wikipedia Account Request"; |
|
65 | - $content = $mailText . $emailSig; |
|
64 | + $subject = "RE: [ACC #{$request->getId()}] English Wikipedia Account Request"; |
|
65 | + $content = $mailText . $emailSig; |
|
66 | 66 | |
67 | - $helper->sendMail($domain->getEmailReplyAddress(), $request->getEmail(), $subject, $content, $headers); |
|
67 | + $helper->sendMail($domain->getEmailReplyAddress(), $request->getEmail(), $subject, $content, $headers); |
|
68 | 68 | |
69 | - $request->setEmailSent(true); |
|
70 | - } |
|
69 | + $request->setEmailSent(true); |
|
70 | + } |
|
71 | 71 | } |
@@ -25,460 +25,460 @@ |
||
25 | 25 | |
26 | 26 | class OAuthUserHelper implements IMediaWikiClient |
27 | 27 | { |
28 | - const TOKEN_REQUEST = 'request'; |
|
29 | - const TOKEN_ACCESS = 'access'; |
|
30 | - /** @var PDOStatement */ |
|
31 | - private static $tokenCountStatement = null; |
|
32 | - /** @var PDOStatement */ |
|
33 | - private $getTokenStatement; |
|
34 | - /** |
|
35 | - * @var User |
|
36 | - */ |
|
37 | - private $user; |
|
38 | - /** |
|
39 | - * @var PdoDatabase |
|
40 | - */ |
|
41 | - private $database; |
|
42 | - /** |
|
43 | - * @var IOAuthProtocolHelper |
|
44 | - */ |
|
45 | - private $oauthProtocolHelper; |
|
46 | - /** |
|
47 | - * @var bool|null Is the user linked to OAuth |
|
48 | - */ |
|
49 | - private $linked; |
|
50 | - private $partiallyLinked; |
|
51 | - /** @var OAuthToken */ |
|
52 | - private $accessToken; |
|
53 | - /** @var bool */ |
|
54 | - private $accessTokenLoaded = false; |
|
55 | - /** |
|
56 | - * @var OAuthIdentity |
|
57 | - */ |
|
58 | - private $identity = null; |
|
59 | - /** |
|
60 | - * @var bool |
|
61 | - */ |
|
62 | - private $identityLoaded = false; |
|
63 | - /** |
|
64 | - * @var SiteConfiguration |
|
65 | - */ |
|
66 | - private $siteConfiguration; |
|
67 | - |
|
68 | - private $legacyTokens; |
|
69 | - |
|
70 | - #region Static methods |
|
71 | - |
|
72 | - public static function findUserByRequestToken($requestToken, PdoDatabase $database) |
|
73 | - { |
|
74 | - $statement = $database->prepare(<<<'SQL' |
|
28 | + const TOKEN_REQUEST = 'request'; |
|
29 | + const TOKEN_ACCESS = 'access'; |
|
30 | + /** @var PDOStatement */ |
|
31 | + private static $tokenCountStatement = null; |
|
32 | + /** @var PDOStatement */ |
|
33 | + private $getTokenStatement; |
|
34 | + /** |
|
35 | + * @var User |
|
36 | + */ |
|
37 | + private $user; |
|
38 | + /** |
|
39 | + * @var PdoDatabase |
|
40 | + */ |
|
41 | + private $database; |
|
42 | + /** |
|
43 | + * @var IOAuthProtocolHelper |
|
44 | + */ |
|
45 | + private $oauthProtocolHelper; |
|
46 | + /** |
|
47 | + * @var bool|null Is the user linked to OAuth |
|
48 | + */ |
|
49 | + private $linked; |
|
50 | + private $partiallyLinked; |
|
51 | + /** @var OAuthToken */ |
|
52 | + private $accessToken; |
|
53 | + /** @var bool */ |
|
54 | + private $accessTokenLoaded = false; |
|
55 | + /** |
|
56 | + * @var OAuthIdentity |
|
57 | + */ |
|
58 | + private $identity = null; |
|
59 | + /** |
|
60 | + * @var bool |
|
61 | + */ |
|
62 | + private $identityLoaded = false; |
|
63 | + /** |
|
64 | + * @var SiteConfiguration |
|
65 | + */ |
|
66 | + private $siteConfiguration; |
|
67 | + |
|
68 | + private $legacyTokens; |
|
69 | + |
|
70 | + #region Static methods |
|
71 | + |
|
72 | + public static function findUserByRequestToken($requestToken, PdoDatabase $database) |
|
73 | + { |
|
74 | + $statement = $database->prepare(<<<'SQL' |
|
75 | 75 | SELECT u.* FROM user u |
76 | 76 | INNER JOIN oauthtoken t ON t.user = u.id |
77 | 77 | WHERE t.type = :type AND t.token = :token |
78 | 78 | SQL |
79 | - ); |
|
80 | - $statement->execute(array(':type' => self::TOKEN_REQUEST, ':token' => $requestToken)); |
|
81 | - |
|
82 | - /** @var User $user */ |
|
83 | - $user = $statement->fetchObject(User::class); |
|
84 | - $statement->closeCursor(); |
|
85 | - |
|
86 | - if ($user === false) { |
|
87 | - throw new ApplicationLogicException('Token not found in store, please try again'); |
|
88 | - } |
|
89 | - |
|
90 | - $user->setDatabase($database); |
|
91 | - |
|
92 | - return $user; |
|
93 | - } |
|
94 | - |
|
95 | - public static function userIsFullyLinked(User $user, PdoDatabase $database = null) |
|
96 | - { |
|
97 | - if (self::$tokenCountStatement === null && $database === null) { |
|
98 | - throw new ApplicationLogicException('Static link request without initialised statement'); |
|
99 | - } |
|
100 | - |
|
101 | - return self::runTokenCount($user->getId(), $database, self::TOKEN_ACCESS); |
|
102 | - } |
|
103 | - |
|
104 | - public static function userIsPartiallyLinked(User $user, PdoDatabase $database = null) |
|
105 | - { |
|
106 | - if (self::$tokenCountStatement === null && $database === null) { |
|
107 | - throw new ApplicationLogicException('Static link request without initialised statement'); |
|
108 | - } |
|
109 | - |
|
110 | - if (self::userIsFullyLinked($user, $database)) { |
|
111 | - return false; |
|
112 | - } |
|
113 | - |
|
114 | - return self::runTokenCount($user->getId(), $database, self::TOKEN_REQUEST) |
|
115 | - || $user->getOnWikiName() == null; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @param PdoDatabase $database |
|
120 | - */ |
|
121 | - public static function prepareTokenCountStatement(PdoDatabase $database) |
|
122 | - { |
|
123 | - if (self::$tokenCountStatement === null) { |
|
124 | - self::$tokenCountStatement = $database->prepare('SELECT COUNT(*) FROM oauthtoken WHERE user = :user AND type = :type'); |
|
125 | - } |
|
126 | - } |
|
127 | - |
|
128 | - private static function runTokenCount($userId, $database, $tokenType) |
|
129 | - { |
|
130 | - if (self::$tokenCountStatement === null) { |
|
131 | - self::prepareTokenCountStatement($database); |
|
132 | - } |
|
133 | - |
|
134 | - self::$tokenCountStatement->execute(array( |
|
135 | - ':user' => $userId, |
|
136 | - ':type' => $tokenType, |
|
137 | - )); |
|
138 | - |
|
139 | - $tokenCount = self::$tokenCountStatement->fetchColumn(); |
|
140 | - $linked = $tokenCount > 0; |
|
141 | - self::$tokenCountStatement->closeCursor(); |
|
142 | - |
|
143 | - return $linked; |
|
144 | - } |
|
145 | - |
|
146 | - #endregion Static methods |
|
147 | - |
|
148 | - /** |
|
149 | - * OAuthUserHelper constructor. |
|
150 | - * |
|
151 | - * @param User $user |
|
152 | - * @param PdoDatabase $database |
|
153 | - * @param IOAuthProtocolHelper $oauthProtocolHelper |
|
154 | - * @param SiteConfiguration $siteConfiguration |
|
155 | - */ |
|
156 | - public function __construct( |
|
157 | - User $user, |
|
158 | - PdoDatabase $database, |
|
159 | - IOAuthProtocolHelper $oauthProtocolHelper, |
|
160 | - SiteConfiguration $siteConfiguration |
|
161 | - ) { |
|
162 | - $this->user = $user; |
|
163 | - $this->database = $database; |
|
164 | - $this->oauthProtocolHelper = $oauthProtocolHelper; |
|
165 | - |
|
166 | - $this->linked = null; |
|
167 | - $this->partiallyLinked = null; |
|
168 | - $this->siteConfiguration = $siteConfiguration; |
|
169 | - |
|
170 | - self::prepareTokenCountStatement($database); |
|
171 | - $this->getTokenStatement = $this->database->prepare('SELECT * FROM oauthtoken WHERE user = :user AND type = :type'); |
|
172 | - |
|
173 | - $this->legacyTokens = $this->siteConfiguration->getOauthLegacyConsumerTokens(); |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * Determines if the user is fully connected to OAuth. |
|
178 | - * |
|
179 | - * @return bool |
|
180 | - */ |
|
181 | - public function isFullyLinked() |
|
182 | - { |
|
183 | - if ($this->linked === null) { |
|
184 | - $this->linked = self::userIsFullyLinked($this->user, $this->database); |
|
185 | - } |
|
186 | - |
|
187 | - return $this->linked; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Attempts to figure out if a user is partially linked to OAuth, and therefore needs to complete the OAuth |
|
192 | - * procedure before configuring. |
|
193 | - * @return bool |
|
194 | - */ |
|
195 | - public function isPartiallyLinked() |
|
196 | - { |
|
197 | - if ($this->partiallyLinked === null) { |
|
198 | - $this->partiallyLinked = self::userIsPartiallyLinked($this->user, $this->database); |
|
199 | - } |
|
200 | - |
|
201 | - return $this->partiallyLinked; |
|
202 | - } |
|
203 | - |
|
204 | - public function canCreateAccount() |
|
205 | - { |
|
206 | - return $this->isFullyLinked() |
|
207 | - && $this->getIdentity(true)->getGrantBasic() |
|
208 | - && $this->getIdentity(true)->getGrantHighVolume() |
|
209 | - && $this->getIdentity(true)->getGrantCreateAccount(); |
|
210 | - } |
|
211 | - |
|
212 | - public function canWelcome() |
|
213 | - { |
|
214 | - return $this->isFullyLinked() |
|
215 | - && $this->getIdentity(true)->getGrantBasic() |
|
216 | - && $this->getIdentity(true)->getGrantHighVolume() |
|
217 | - && $this->getIdentity(true)->getGrantCreateEditMovePage(); |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * @throws OAuthException |
|
222 | - * @throws CurlException |
|
223 | - * @throws OptimisticLockFailedException |
|
224 | - * @throws Exception |
|
225 | - */ |
|
226 | - public function refreshIdentity() |
|
227 | - { |
|
228 | - $this->loadIdentity(); |
|
229 | - |
|
230 | - if ($this->identity === null) { |
|
231 | - $this->identity = new OAuthIdentity(); |
|
232 | - $this->identity->setUserId($this->user->getId()); |
|
233 | - $this->identity->setDatabase($this->database); |
|
234 | - } |
|
235 | - |
|
236 | - $token = $this->loadAccessToken(); |
|
237 | - |
|
238 | - try { |
|
239 | - $rawTicket = $this->oauthProtocolHelper->getIdentityTicket($token->getToken(), $token->getSecret()); |
|
240 | - } |
|
241 | - catch (Exception $ex) { |
|
242 | - if (strpos($ex->getMessage(), "mwoauthdatastore-access-token-not-found") !== false) { |
|
243 | - throw new OAuthException('No approved grants for this access token.', -1, $ex); |
|
244 | - } |
|
245 | - |
|
246 | - throw $ex; |
|
247 | - } |
|
248 | - |
|
249 | - $this->identity->populate($rawTicket); |
|
250 | - |
|
251 | - if (!$this->identityIsValid()) { |
|
252 | - throw new OAuthException('Identity ticket is not valid!'); |
|
253 | - } |
|
254 | - |
|
255 | - $this->identity->save(); |
|
256 | - |
|
257 | - $this->user->setOnWikiName($this->identity->getUsername()); |
|
258 | - $this->user->save(); |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * @return string |
|
263 | - * @throws CurlException |
|
264 | - */ |
|
265 | - public function getRequestToken() |
|
266 | - { |
|
267 | - $token = $this->oauthProtocolHelper->getRequestToken(); |
|
268 | - |
|
269 | - $this->partiallyLinked = true; |
|
270 | - $this->linked = false; |
|
271 | - |
|
272 | - $this->database |
|
273 | - ->prepare('DELETE FROM oauthtoken WHERE user = :user AND type = :type') |
|
274 | - ->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_REQUEST)); |
|
275 | - |
|
276 | - $this->database |
|
277 | - ->prepare('INSERT INTO oauthtoken (user, type, token, secret, expiry) VALUES (:user, :type, :token, :secret, DATE_ADD(NOW(), INTERVAL 1 DAY))') |
|
278 | - ->execute(array( |
|
279 | - ':user' => $this->user->getId(), |
|
280 | - ':type' => self::TOKEN_REQUEST, |
|
281 | - ':token' => $token->key, |
|
282 | - ':secret' => $token->secret, |
|
283 | - )); |
|
284 | - |
|
285 | - return $this->oauthProtocolHelper->getAuthoriseUrl($token->key); |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * @param $verificationToken |
|
290 | - * |
|
291 | - * @throws ApplicationLogicException |
|
292 | - * @throws CurlException |
|
293 | - * @throws OAuthException |
|
294 | - * @throws OptimisticLockFailedException |
|
295 | - */ |
|
296 | - public function completeHandshake($verificationToken) |
|
297 | - { |
|
298 | - $this->getTokenStatement->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_REQUEST)); |
|
299 | - |
|
300 | - /** @var OAuthToken $token */ |
|
301 | - $token = $this->getTokenStatement->fetchObject(OAuthToken::class); |
|
302 | - $this->getTokenStatement->closeCursor(); |
|
303 | - |
|
304 | - if ($token === false) { |
|
305 | - throw new ApplicationLogicException('Cannot find request token'); |
|
306 | - } |
|
307 | - |
|
308 | - $token->setDatabase($this->database); |
|
309 | - |
|
310 | - $accessToken = $this->oauthProtocolHelper->callbackCompleted($token->getToken(), $token->getSecret(), |
|
311 | - $verificationToken); |
|
312 | - |
|
313 | - $clearStatement = $this->database->prepare('DELETE FROM oauthtoken WHERE user = :u AND type = :t'); |
|
314 | - $clearStatement->execute(array(':u' => $this->user->getId(), ':t' => self::TOKEN_ACCESS)); |
|
315 | - |
|
316 | - $token->setToken($accessToken->key); |
|
317 | - $token->setSecret($accessToken->secret); |
|
318 | - $token->setType(self::TOKEN_ACCESS); |
|
319 | - $token->setExpiry(null); |
|
320 | - $token->save(); |
|
321 | - |
|
322 | - $this->partiallyLinked = false; |
|
323 | - $this->linked = true; |
|
324 | - |
|
325 | - $this->refreshIdentity(); |
|
326 | - } |
|
327 | - |
|
328 | - public function detach() |
|
329 | - { |
|
330 | - $this->loadIdentity(); |
|
331 | - |
|
332 | - $this->identity->delete(); |
|
333 | - $statement = $this->database->prepare('DELETE FROM oauthtoken WHERE user = :user'); |
|
334 | - $statement->execute(array(':user' => $this->user->getId())); |
|
335 | - |
|
336 | - $this->identity = null; |
|
337 | - $this->linked = false; |
|
338 | - $this->partiallyLinked = false; |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * @param bool $expiredOk |
|
343 | - * |
|
344 | - * @return OAuthIdentity |
|
345 | - * @throws OAuthException |
|
346 | - */ |
|
347 | - public function getIdentity($expiredOk = false) |
|
348 | - { |
|
349 | - $this->loadIdentity(); |
|
350 | - |
|
351 | - if (!$this->identityIsValid($expiredOk)) { |
|
352 | - throw new OAuthException('Stored identity is not valid.'); |
|
353 | - } |
|
354 | - |
|
355 | - return $this->identity; |
|
356 | - } |
|
357 | - |
|
358 | - public function doApiCall($params, $method) |
|
359 | - { |
|
360 | - // Ensure we're logged in |
|
361 | - $params['assert'] = 'user'; |
|
362 | - |
|
363 | - $token = $this->loadAccessToken(); |
|
364 | - return $this->oauthProtocolHelper->apiCall($params, $token->getToken(), $token->getSecret(), $method); |
|
365 | - } |
|
366 | - |
|
367 | - /** |
|
368 | - * @param bool $expiredOk |
|
369 | - * |
|
370 | - * @return bool |
|
371 | - */ |
|
372 | - private function identityIsValid($expiredOk = false) |
|
373 | - { |
|
374 | - $this->loadIdentity(); |
|
375 | - |
|
376 | - if ($this->identity === null) { |
|
377 | - return false; |
|
378 | - } |
|
379 | - |
|
380 | - if ($this->identity->getIssuedAtTime() === false |
|
381 | - || $this->identity->getExpirationTime() === false |
|
382 | - || $this->identity->getAudience() === false |
|
383 | - || $this->identity->getIssuer() === false |
|
384 | - ) { |
|
385 | - // this isn't populated properly. |
|
386 | - return false; |
|
387 | - } |
|
388 | - |
|
389 | - $issue = DateTimeImmutable::createFromFormat("U", $this->identity->getIssuedAtTime()); |
|
390 | - $now = new DateTimeImmutable(); |
|
391 | - |
|
392 | - if ($issue > $now) { |
|
393 | - // wat. |
|
394 | - return false; |
|
395 | - } |
|
396 | - |
|
397 | - if ($this->identityExpired() && !$expiredOk) { |
|
398 | - // soz. |
|
399 | - return false; |
|
400 | - } |
|
401 | - |
|
402 | - if ($this->identity->getAudience() !== $this->siteConfiguration->getOAuthConsumerToken()) { |
|
403 | - // token not issued for us |
|
404 | - |
|
405 | - // we allow cases where the cache is expired and the cache is for a legacy token |
|
406 | - if (!($expiredOk && in_array($this->identity->getAudience(), $this->legacyTokens))) { |
|
407 | - return false; |
|
408 | - } |
|
409 | - } |
|
410 | - |
|
411 | - if ($this->identity->getIssuer() !== $this->siteConfiguration->getOauthMediaWikiCanonicalServer()) { |
|
412 | - // token not issued by the right person |
|
413 | - return false; |
|
414 | - } |
|
415 | - |
|
416 | - // can't find a reason to not trust it |
|
417 | - return true; |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * @return bool |
|
422 | - */ |
|
423 | - public function identityExpired() |
|
424 | - { |
|
425 | - // allowed max age |
|
426 | - $gracePeriod = $this->siteConfiguration->getOauthIdentityGraceTime(); |
|
427 | - |
|
428 | - $expiry = DateTimeImmutable::createFromFormat("U", $this->identity->getExpirationTime()); |
|
429 | - $graceExpiry = $expiry->modify($gracePeriod); |
|
430 | - $now = new DateTimeImmutable(); |
|
431 | - |
|
432 | - return $graceExpiry < $now; |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * Loads the OAuth identity from the database for the current user. |
|
437 | - */ |
|
438 | - private function loadIdentity() |
|
439 | - { |
|
440 | - if ($this->identityLoaded) { |
|
441 | - return; |
|
442 | - } |
|
443 | - |
|
444 | - $statement = $this->database->prepare('SELECT * FROM oauthidentity WHERE user = :user'); |
|
445 | - $statement->execute(array(':user' => $this->user->getId())); |
|
446 | - /** @var OAuthIdentity $obj */ |
|
447 | - $obj = $statement->fetchObject(OAuthIdentity::class); |
|
448 | - |
|
449 | - if ($obj === false) { |
|
450 | - // failed to load identity. |
|
451 | - $this->identityLoaded = true; |
|
452 | - $this->identity = null; |
|
453 | - |
|
454 | - return; |
|
455 | - } |
|
456 | - |
|
457 | - $obj->setDatabase($this->database); |
|
458 | - $this->identityLoaded = true; |
|
459 | - $this->identity = $obj; |
|
460 | - } |
|
461 | - |
|
462 | - /** |
|
463 | - * @return OAuthToken |
|
464 | - * @throws OAuthException |
|
465 | - */ |
|
466 | - private function loadAccessToken() |
|
467 | - { |
|
468 | - if (!$this->accessTokenLoaded) { |
|
469 | - $this->getTokenStatement->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_ACCESS)); |
|
470 | - /** @var OAuthToken $token */ |
|
471 | - $token = $this->getTokenStatement->fetchObject(OAuthToken::class); |
|
472 | - $this->getTokenStatement->closeCursor(); |
|
473 | - |
|
474 | - if ($token === false) { |
|
475 | - throw new OAuthException('Access token not found!'); |
|
476 | - } |
|
477 | - |
|
478 | - $this->accessToken = $token; |
|
479 | - $this->accessTokenLoaded = true; |
|
480 | - } |
|
481 | - |
|
482 | - return $this->accessToken; |
|
483 | - } |
|
79 | + ); |
|
80 | + $statement->execute(array(':type' => self::TOKEN_REQUEST, ':token' => $requestToken)); |
|
81 | + |
|
82 | + /** @var User $user */ |
|
83 | + $user = $statement->fetchObject(User::class); |
|
84 | + $statement->closeCursor(); |
|
85 | + |
|
86 | + if ($user === false) { |
|
87 | + throw new ApplicationLogicException('Token not found in store, please try again'); |
|
88 | + } |
|
89 | + |
|
90 | + $user->setDatabase($database); |
|
91 | + |
|
92 | + return $user; |
|
93 | + } |
|
94 | + |
|
95 | + public static function userIsFullyLinked(User $user, PdoDatabase $database = null) |
|
96 | + { |
|
97 | + if (self::$tokenCountStatement === null && $database === null) { |
|
98 | + throw new ApplicationLogicException('Static link request without initialised statement'); |
|
99 | + } |
|
100 | + |
|
101 | + return self::runTokenCount($user->getId(), $database, self::TOKEN_ACCESS); |
|
102 | + } |
|
103 | + |
|
104 | + public static function userIsPartiallyLinked(User $user, PdoDatabase $database = null) |
|
105 | + { |
|
106 | + if (self::$tokenCountStatement === null && $database === null) { |
|
107 | + throw new ApplicationLogicException('Static link request without initialised statement'); |
|
108 | + } |
|
109 | + |
|
110 | + if (self::userIsFullyLinked($user, $database)) { |
|
111 | + return false; |
|
112 | + } |
|
113 | + |
|
114 | + return self::runTokenCount($user->getId(), $database, self::TOKEN_REQUEST) |
|
115 | + || $user->getOnWikiName() == null; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @param PdoDatabase $database |
|
120 | + */ |
|
121 | + public static function prepareTokenCountStatement(PdoDatabase $database) |
|
122 | + { |
|
123 | + if (self::$tokenCountStatement === null) { |
|
124 | + self::$tokenCountStatement = $database->prepare('SELECT COUNT(*) FROM oauthtoken WHERE user = :user AND type = :type'); |
|
125 | + } |
|
126 | + } |
|
127 | + |
|
128 | + private static function runTokenCount($userId, $database, $tokenType) |
|
129 | + { |
|
130 | + if (self::$tokenCountStatement === null) { |
|
131 | + self::prepareTokenCountStatement($database); |
|
132 | + } |
|
133 | + |
|
134 | + self::$tokenCountStatement->execute(array( |
|
135 | + ':user' => $userId, |
|
136 | + ':type' => $tokenType, |
|
137 | + )); |
|
138 | + |
|
139 | + $tokenCount = self::$tokenCountStatement->fetchColumn(); |
|
140 | + $linked = $tokenCount > 0; |
|
141 | + self::$tokenCountStatement->closeCursor(); |
|
142 | + |
|
143 | + return $linked; |
|
144 | + } |
|
145 | + |
|
146 | + #endregion Static methods |
|
147 | + |
|
148 | + /** |
|
149 | + * OAuthUserHelper constructor. |
|
150 | + * |
|
151 | + * @param User $user |
|
152 | + * @param PdoDatabase $database |
|
153 | + * @param IOAuthProtocolHelper $oauthProtocolHelper |
|
154 | + * @param SiteConfiguration $siteConfiguration |
|
155 | + */ |
|
156 | + public function __construct( |
|
157 | + User $user, |
|
158 | + PdoDatabase $database, |
|
159 | + IOAuthProtocolHelper $oauthProtocolHelper, |
|
160 | + SiteConfiguration $siteConfiguration |
|
161 | + ) { |
|
162 | + $this->user = $user; |
|
163 | + $this->database = $database; |
|
164 | + $this->oauthProtocolHelper = $oauthProtocolHelper; |
|
165 | + |
|
166 | + $this->linked = null; |
|
167 | + $this->partiallyLinked = null; |
|
168 | + $this->siteConfiguration = $siteConfiguration; |
|
169 | + |
|
170 | + self::prepareTokenCountStatement($database); |
|
171 | + $this->getTokenStatement = $this->database->prepare('SELECT * FROM oauthtoken WHERE user = :user AND type = :type'); |
|
172 | + |
|
173 | + $this->legacyTokens = $this->siteConfiguration->getOauthLegacyConsumerTokens(); |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * Determines if the user is fully connected to OAuth. |
|
178 | + * |
|
179 | + * @return bool |
|
180 | + */ |
|
181 | + public function isFullyLinked() |
|
182 | + { |
|
183 | + if ($this->linked === null) { |
|
184 | + $this->linked = self::userIsFullyLinked($this->user, $this->database); |
|
185 | + } |
|
186 | + |
|
187 | + return $this->linked; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Attempts to figure out if a user is partially linked to OAuth, and therefore needs to complete the OAuth |
|
192 | + * procedure before configuring. |
|
193 | + * @return bool |
|
194 | + */ |
|
195 | + public function isPartiallyLinked() |
|
196 | + { |
|
197 | + if ($this->partiallyLinked === null) { |
|
198 | + $this->partiallyLinked = self::userIsPartiallyLinked($this->user, $this->database); |
|
199 | + } |
|
200 | + |
|
201 | + return $this->partiallyLinked; |
|
202 | + } |
|
203 | + |
|
204 | + public function canCreateAccount() |
|
205 | + { |
|
206 | + return $this->isFullyLinked() |
|
207 | + && $this->getIdentity(true)->getGrantBasic() |
|
208 | + && $this->getIdentity(true)->getGrantHighVolume() |
|
209 | + && $this->getIdentity(true)->getGrantCreateAccount(); |
|
210 | + } |
|
211 | + |
|
212 | + public function canWelcome() |
|
213 | + { |
|
214 | + return $this->isFullyLinked() |
|
215 | + && $this->getIdentity(true)->getGrantBasic() |
|
216 | + && $this->getIdentity(true)->getGrantHighVolume() |
|
217 | + && $this->getIdentity(true)->getGrantCreateEditMovePage(); |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * @throws OAuthException |
|
222 | + * @throws CurlException |
|
223 | + * @throws OptimisticLockFailedException |
|
224 | + * @throws Exception |
|
225 | + */ |
|
226 | + public function refreshIdentity() |
|
227 | + { |
|
228 | + $this->loadIdentity(); |
|
229 | + |
|
230 | + if ($this->identity === null) { |
|
231 | + $this->identity = new OAuthIdentity(); |
|
232 | + $this->identity->setUserId($this->user->getId()); |
|
233 | + $this->identity->setDatabase($this->database); |
|
234 | + } |
|
235 | + |
|
236 | + $token = $this->loadAccessToken(); |
|
237 | + |
|
238 | + try { |
|
239 | + $rawTicket = $this->oauthProtocolHelper->getIdentityTicket($token->getToken(), $token->getSecret()); |
|
240 | + } |
|
241 | + catch (Exception $ex) { |
|
242 | + if (strpos($ex->getMessage(), "mwoauthdatastore-access-token-not-found") !== false) { |
|
243 | + throw new OAuthException('No approved grants for this access token.', -1, $ex); |
|
244 | + } |
|
245 | + |
|
246 | + throw $ex; |
|
247 | + } |
|
248 | + |
|
249 | + $this->identity->populate($rawTicket); |
|
250 | + |
|
251 | + if (!$this->identityIsValid()) { |
|
252 | + throw new OAuthException('Identity ticket is not valid!'); |
|
253 | + } |
|
254 | + |
|
255 | + $this->identity->save(); |
|
256 | + |
|
257 | + $this->user->setOnWikiName($this->identity->getUsername()); |
|
258 | + $this->user->save(); |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * @return string |
|
263 | + * @throws CurlException |
|
264 | + */ |
|
265 | + public function getRequestToken() |
|
266 | + { |
|
267 | + $token = $this->oauthProtocolHelper->getRequestToken(); |
|
268 | + |
|
269 | + $this->partiallyLinked = true; |
|
270 | + $this->linked = false; |
|
271 | + |
|
272 | + $this->database |
|
273 | + ->prepare('DELETE FROM oauthtoken WHERE user = :user AND type = :type') |
|
274 | + ->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_REQUEST)); |
|
275 | + |
|
276 | + $this->database |
|
277 | + ->prepare('INSERT INTO oauthtoken (user, type, token, secret, expiry) VALUES (:user, :type, :token, :secret, DATE_ADD(NOW(), INTERVAL 1 DAY))') |
|
278 | + ->execute(array( |
|
279 | + ':user' => $this->user->getId(), |
|
280 | + ':type' => self::TOKEN_REQUEST, |
|
281 | + ':token' => $token->key, |
|
282 | + ':secret' => $token->secret, |
|
283 | + )); |
|
284 | + |
|
285 | + return $this->oauthProtocolHelper->getAuthoriseUrl($token->key); |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * @param $verificationToken |
|
290 | + * |
|
291 | + * @throws ApplicationLogicException |
|
292 | + * @throws CurlException |
|
293 | + * @throws OAuthException |
|
294 | + * @throws OptimisticLockFailedException |
|
295 | + */ |
|
296 | + public function completeHandshake($verificationToken) |
|
297 | + { |
|
298 | + $this->getTokenStatement->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_REQUEST)); |
|
299 | + |
|
300 | + /** @var OAuthToken $token */ |
|
301 | + $token = $this->getTokenStatement->fetchObject(OAuthToken::class); |
|
302 | + $this->getTokenStatement->closeCursor(); |
|
303 | + |
|
304 | + if ($token === false) { |
|
305 | + throw new ApplicationLogicException('Cannot find request token'); |
|
306 | + } |
|
307 | + |
|
308 | + $token->setDatabase($this->database); |
|
309 | + |
|
310 | + $accessToken = $this->oauthProtocolHelper->callbackCompleted($token->getToken(), $token->getSecret(), |
|
311 | + $verificationToken); |
|
312 | + |
|
313 | + $clearStatement = $this->database->prepare('DELETE FROM oauthtoken WHERE user = :u AND type = :t'); |
|
314 | + $clearStatement->execute(array(':u' => $this->user->getId(), ':t' => self::TOKEN_ACCESS)); |
|
315 | + |
|
316 | + $token->setToken($accessToken->key); |
|
317 | + $token->setSecret($accessToken->secret); |
|
318 | + $token->setType(self::TOKEN_ACCESS); |
|
319 | + $token->setExpiry(null); |
|
320 | + $token->save(); |
|
321 | + |
|
322 | + $this->partiallyLinked = false; |
|
323 | + $this->linked = true; |
|
324 | + |
|
325 | + $this->refreshIdentity(); |
|
326 | + } |
|
327 | + |
|
328 | + public function detach() |
|
329 | + { |
|
330 | + $this->loadIdentity(); |
|
331 | + |
|
332 | + $this->identity->delete(); |
|
333 | + $statement = $this->database->prepare('DELETE FROM oauthtoken WHERE user = :user'); |
|
334 | + $statement->execute(array(':user' => $this->user->getId())); |
|
335 | + |
|
336 | + $this->identity = null; |
|
337 | + $this->linked = false; |
|
338 | + $this->partiallyLinked = false; |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * @param bool $expiredOk |
|
343 | + * |
|
344 | + * @return OAuthIdentity |
|
345 | + * @throws OAuthException |
|
346 | + */ |
|
347 | + public function getIdentity($expiredOk = false) |
|
348 | + { |
|
349 | + $this->loadIdentity(); |
|
350 | + |
|
351 | + if (!$this->identityIsValid($expiredOk)) { |
|
352 | + throw new OAuthException('Stored identity is not valid.'); |
|
353 | + } |
|
354 | + |
|
355 | + return $this->identity; |
|
356 | + } |
|
357 | + |
|
358 | + public function doApiCall($params, $method) |
|
359 | + { |
|
360 | + // Ensure we're logged in |
|
361 | + $params['assert'] = 'user'; |
|
362 | + |
|
363 | + $token = $this->loadAccessToken(); |
|
364 | + return $this->oauthProtocolHelper->apiCall($params, $token->getToken(), $token->getSecret(), $method); |
|
365 | + } |
|
366 | + |
|
367 | + /** |
|
368 | + * @param bool $expiredOk |
|
369 | + * |
|
370 | + * @return bool |
|
371 | + */ |
|
372 | + private function identityIsValid($expiredOk = false) |
|
373 | + { |
|
374 | + $this->loadIdentity(); |
|
375 | + |
|
376 | + if ($this->identity === null) { |
|
377 | + return false; |
|
378 | + } |
|
379 | + |
|
380 | + if ($this->identity->getIssuedAtTime() === false |
|
381 | + || $this->identity->getExpirationTime() === false |
|
382 | + || $this->identity->getAudience() === false |
|
383 | + || $this->identity->getIssuer() === false |
|
384 | + ) { |
|
385 | + // this isn't populated properly. |
|
386 | + return false; |
|
387 | + } |
|
388 | + |
|
389 | + $issue = DateTimeImmutable::createFromFormat("U", $this->identity->getIssuedAtTime()); |
|
390 | + $now = new DateTimeImmutable(); |
|
391 | + |
|
392 | + if ($issue > $now) { |
|
393 | + // wat. |
|
394 | + return false; |
|
395 | + } |
|
396 | + |
|
397 | + if ($this->identityExpired() && !$expiredOk) { |
|
398 | + // soz. |
|
399 | + return false; |
|
400 | + } |
|
401 | + |
|
402 | + if ($this->identity->getAudience() !== $this->siteConfiguration->getOAuthConsumerToken()) { |
|
403 | + // token not issued for us |
|
404 | + |
|
405 | + // we allow cases where the cache is expired and the cache is for a legacy token |
|
406 | + if (!($expiredOk && in_array($this->identity->getAudience(), $this->legacyTokens))) { |
|
407 | + return false; |
|
408 | + } |
|
409 | + } |
|
410 | + |
|
411 | + if ($this->identity->getIssuer() !== $this->siteConfiguration->getOauthMediaWikiCanonicalServer()) { |
|
412 | + // token not issued by the right person |
|
413 | + return false; |
|
414 | + } |
|
415 | + |
|
416 | + // can't find a reason to not trust it |
|
417 | + return true; |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * @return bool |
|
422 | + */ |
|
423 | + public function identityExpired() |
|
424 | + { |
|
425 | + // allowed max age |
|
426 | + $gracePeriod = $this->siteConfiguration->getOauthIdentityGraceTime(); |
|
427 | + |
|
428 | + $expiry = DateTimeImmutable::createFromFormat("U", $this->identity->getExpirationTime()); |
|
429 | + $graceExpiry = $expiry->modify($gracePeriod); |
|
430 | + $now = new DateTimeImmutable(); |
|
431 | + |
|
432 | + return $graceExpiry < $now; |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * Loads the OAuth identity from the database for the current user. |
|
437 | + */ |
|
438 | + private function loadIdentity() |
|
439 | + { |
|
440 | + if ($this->identityLoaded) { |
|
441 | + return; |
|
442 | + } |
|
443 | + |
|
444 | + $statement = $this->database->prepare('SELECT * FROM oauthidentity WHERE user = :user'); |
|
445 | + $statement->execute(array(':user' => $this->user->getId())); |
|
446 | + /** @var OAuthIdentity $obj */ |
|
447 | + $obj = $statement->fetchObject(OAuthIdentity::class); |
|
448 | + |
|
449 | + if ($obj === false) { |
|
450 | + // failed to load identity. |
|
451 | + $this->identityLoaded = true; |
|
452 | + $this->identity = null; |
|
453 | + |
|
454 | + return; |
|
455 | + } |
|
456 | + |
|
457 | + $obj->setDatabase($this->database); |
|
458 | + $this->identityLoaded = true; |
|
459 | + $this->identity = $obj; |
|
460 | + } |
|
461 | + |
|
462 | + /** |
|
463 | + * @return OAuthToken |
|
464 | + * @throws OAuthException |
|
465 | + */ |
|
466 | + private function loadAccessToken() |
|
467 | + { |
|
468 | + if (!$this->accessTokenLoaded) { |
|
469 | + $this->getTokenStatement->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_ACCESS)); |
|
470 | + /** @var OAuthToken $token */ |
|
471 | + $token = $this->getTokenStatement->fetchObject(OAuthToken::class); |
|
472 | + $this->getTokenStatement->closeCursor(); |
|
473 | + |
|
474 | + if ($token === false) { |
|
475 | + throw new OAuthException('Access token not found!'); |
|
476 | + } |
|
477 | + |
|
478 | + $this->accessToken = $token; |
|
479 | + $this->accessTokenLoaded = true; |
|
480 | + } |
|
481 | + |
|
482 | + return $this->accessToken; |
|
483 | + } |
|
484 | 484 | } |
@@ -17,136 +17,136 @@ |
||
17 | 17 | |
18 | 18 | class BotMediaWikiClient implements IMediaWikiClient |
19 | 19 | { |
20 | - /** |
|
21 | - * @var HttpHelper |
|
22 | - */ |
|
23 | - private $httpHelper; |
|
24 | - /** @var string */ |
|
25 | - private $mediawikiWebServiceEndpoint; |
|
26 | - /** @var string */ |
|
27 | - private $creationBotUsername; |
|
28 | - /** @var string */ |
|
29 | - private $creationBotPassword; |
|
30 | - /** @var bool */ |
|
31 | - private $knownLoggedIn = false; |
|
32 | - |
|
33 | - /** |
|
34 | - * BotMediaWikiClient constructor. |
|
35 | - * |
|
36 | - * @param SiteConfiguration $siteConfiguration |
|
37 | - * @param Domain $domain |
|
38 | - */ |
|
39 | - public function __construct(SiteConfiguration $siteConfiguration, Domain $domain) |
|
40 | - { |
|
41 | - $this->mediawikiWebServiceEndpoint = $domain->getWikiApiPath(); |
|
42 | - |
|
43 | - $this->creationBotUsername = $siteConfiguration->getCreationBotUsername(); |
|
44 | - $this->creationBotPassword = $siteConfiguration->getCreationBotPassword(); |
|
45 | - |
|
46 | - $this->httpHelper = new HttpHelper( |
|
47 | - $siteConfiguration, |
|
48 | - $siteConfiguration->getCurlCookieJar() |
|
49 | - ); |
|
50 | - } |
|
51 | - |
|
52 | - public function doApiCall($apiParams, $method = 'GET') |
|
53 | - { |
|
54 | - $this->ensureLoggedIn(); |
|
55 | - $apiParams['assert'] = 'user'; |
|
56 | - |
|
57 | - return $this->callApi($apiParams, $method); |
|
58 | - } |
|
59 | - |
|
60 | - private function ensureLoggedIn() |
|
61 | - { |
|
62 | - if ($this->knownLoggedIn) { |
|
63 | - return; |
|
64 | - } |
|
65 | - |
|
66 | - $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
|
67 | - if (isset($userinfoResult->query->userinfo->anon)) { |
|
68 | - // not logged in. |
|
69 | - $this->logIn(); |
|
70 | - |
|
71 | - // retest |
|
72 | - $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
|
73 | - if (isset($userinfoResult->query->userinfo->anon)) { |
|
74 | - throw new MediaWikiApiException('Unable to log in.'); |
|
75 | - } |
|
76 | - else { |
|
77 | - $this->knownLoggedIn = true; |
|
78 | - } |
|
79 | - } |
|
80 | - else { |
|
81 | - $this->knownLoggedIn = true; |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @param $apiParams |
|
87 | - * @param $method |
|
88 | - * |
|
89 | - * @return mixed |
|
90 | - * @throws ApplicationLogicException |
|
91 | - * @throws CurlException |
|
92 | - */ |
|
93 | - private function callApi($apiParams, $method) |
|
94 | - { |
|
95 | - $apiParams['format'] = 'json'; |
|
96 | - |
|
97 | - if ($method == 'GET') { |
|
98 | - $data = $this->httpHelper->get($this->mediawikiWebServiceEndpoint, $apiParams); |
|
99 | - } |
|
100 | - elseif ($method == 'POST') { |
|
101 | - $data = $this->httpHelper->post($this->mediawikiWebServiceEndpoint, $apiParams); |
|
102 | - } |
|
103 | - else { |
|
104 | - throw new ApplicationLogicException('Unsupported HTTP Method'); |
|
105 | - } |
|
106 | - |
|
107 | - if ($data === false) { |
|
108 | - throw new CurlException('Curl error: ' . $this->httpHelper->getError()); |
|
109 | - } |
|
110 | - |
|
111 | - $result = json_decode($data); |
|
112 | - |
|
113 | - return $result; |
|
114 | - } |
|
115 | - |
|
116 | - private function logIn() |
|
117 | - { |
|
118 | - // get token |
|
119 | - $tokenParams = array( |
|
120 | - 'action' => 'query', |
|
121 | - 'meta' => 'tokens', |
|
122 | - 'type' => 'login', |
|
123 | - ); |
|
124 | - |
|
125 | - $response = $this->callApi($tokenParams, 'POST'); |
|
126 | - |
|
127 | - if (isset($response->error)) { |
|
128 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
129 | - } |
|
130 | - |
|
131 | - $token = $response->query->tokens->logintoken; |
|
132 | - |
|
133 | - if ($token === null) { |
|
134 | - throw new MediaWikiApiException('Edit token could not be acquired'); |
|
135 | - } |
|
136 | - |
|
137 | - $params = array( |
|
138 | - 'action' => 'login', |
|
139 | - 'lgname' => $this->creationBotUsername, |
|
140 | - 'lgpassword' => $this->creationBotPassword, |
|
141 | - 'lgtoken' => $token, |
|
142 | - ); |
|
143 | - |
|
144 | - $loginResponse = $this->callApi($params, 'POST'); |
|
145 | - |
|
146 | - if ($loginResponse->login->result == 'Success') { |
|
147 | - return; |
|
148 | - } |
|
149 | - |
|
150 | - throw new ApplicationLogicException(json_encode($loginResponse)); |
|
151 | - } |
|
20 | + /** |
|
21 | + * @var HttpHelper |
|
22 | + */ |
|
23 | + private $httpHelper; |
|
24 | + /** @var string */ |
|
25 | + private $mediawikiWebServiceEndpoint; |
|
26 | + /** @var string */ |
|
27 | + private $creationBotUsername; |
|
28 | + /** @var string */ |
|
29 | + private $creationBotPassword; |
|
30 | + /** @var bool */ |
|
31 | + private $knownLoggedIn = false; |
|
32 | + |
|
33 | + /** |
|
34 | + * BotMediaWikiClient constructor. |
|
35 | + * |
|
36 | + * @param SiteConfiguration $siteConfiguration |
|
37 | + * @param Domain $domain |
|
38 | + */ |
|
39 | + public function __construct(SiteConfiguration $siteConfiguration, Domain $domain) |
|
40 | + { |
|
41 | + $this->mediawikiWebServiceEndpoint = $domain->getWikiApiPath(); |
|
42 | + |
|
43 | + $this->creationBotUsername = $siteConfiguration->getCreationBotUsername(); |
|
44 | + $this->creationBotPassword = $siteConfiguration->getCreationBotPassword(); |
|
45 | + |
|
46 | + $this->httpHelper = new HttpHelper( |
|
47 | + $siteConfiguration, |
|
48 | + $siteConfiguration->getCurlCookieJar() |
|
49 | + ); |
|
50 | + } |
|
51 | + |
|
52 | + public function doApiCall($apiParams, $method = 'GET') |
|
53 | + { |
|
54 | + $this->ensureLoggedIn(); |
|
55 | + $apiParams['assert'] = 'user'; |
|
56 | + |
|
57 | + return $this->callApi($apiParams, $method); |
|
58 | + } |
|
59 | + |
|
60 | + private function ensureLoggedIn() |
|
61 | + { |
|
62 | + if ($this->knownLoggedIn) { |
|
63 | + return; |
|
64 | + } |
|
65 | + |
|
66 | + $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
|
67 | + if (isset($userinfoResult->query->userinfo->anon)) { |
|
68 | + // not logged in. |
|
69 | + $this->logIn(); |
|
70 | + |
|
71 | + // retest |
|
72 | + $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
|
73 | + if (isset($userinfoResult->query->userinfo->anon)) { |
|
74 | + throw new MediaWikiApiException('Unable to log in.'); |
|
75 | + } |
|
76 | + else { |
|
77 | + $this->knownLoggedIn = true; |
|
78 | + } |
|
79 | + } |
|
80 | + else { |
|
81 | + $this->knownLoggedIn = true; |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @param $apiParams |
|
87 | + * @param $method |
|
88 | + * |
|
89 | + * @return mixed |
|
90 | + * @throws ApplicationLogicException |
|
91 | + * @throws CurlException |
|
92 | + */ |
|
93 | + private function callApi($apiParams, $method) |
|
94 | + { |
|
95 | + $apiParams['format'] = 'json'; |
|
96 | + |
|
97 | + if ($method == 'GET') { |
|
98 | + $data = $this->httpHelper->get($this->mediawikiWebServiceEndpoint, $apiParams); |
|
99 | + } |
|
100 | + elseif ($method == 'POST') { |
|
101 | + $data = $this->httpHelper->post($this->mediawikiWebServiceEndpoint, $apiParams); |
|
102 | + } |
|
103 | + else { |
|
104 | + throw new ApplicationLogicException('Unsupported HTTP Method'); |
|
105 | + } |
|
106 | + |
|
107 | + if ($data === false) { |
|
108 | + throw new CurlException('Curl error: ' . $this->httpHelper->getError()); |
|
109 | + } |
|
110 | + |
|
111 | + $result = json_decode($data); |
|
112 | + |
|
113 | + return $result; |
|
114 | + } |
|
115 | + |
|
116 | + private function logIn() |
|
117 | + { |
|
118 | + // get token |
|
119 | + $tokenParams = array( |
|
120 | + 'action' => 'query', |
|
121 | + 'meta' => 'tokens', |
|
122 | + 'type' => 'login', |
|
123 | + ); |
|
124 | + |
|
125 | + $response = $this->callApi($tokenParams, 'POST'); |
|
126 | + |
|
127 | + if (isset($response->error)) { |
|
128 | + throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
129 | + } |
|
130 | + |
|
131 | + $token = $response->query->tokens->logintoken; |
|
132 | + |
|
133 | + if ($token === null) { |
|
134 | + throw new MediaWikiApiException('Edit token could not be acquired'); |
|
135 | + } |
|
136 | + |
|
137 | + $params = array( |
|
138 | + 'action' => 'login', |
|
139 | + 'lgname' => $this->creationBotUsername, |
|
140 | + 'lgpassword' => $this->creationBotPassword, |
|
141 | + 'lgtoken' => $token, |
|
142 | + ); |
|
143 | + |
|
144 | + $loginResponse = $this->callApi($params, 'POST'); |
|
145 | + |
|
146 | + if ($loginResponse->login->result == 'Success') { |
|
147 | + return; |
|
148 | + } |
|
149 | + |
|
150 | + throw new ApplicationLogicException(json_encode($loginResponse)); |
|
151 | + } |
|
152 | 152 | } |
@@ -72,12 +72,10 @@ discard block |
||
72 | 72 | $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
73 | 73 | if (isset($userinfoResult->query->userinfo->anon)) { |
74 | 74 | throw new MediaWikiApiException('Unable to log in.'); |
75 | - } |
|
76 | - else { |
|
75 | + } else { |
|
77 | 76 | $this->knownLoggedIn = true; |
78 | 77 | } |
79 | - } |
|
80 | - else { |
|
78 | + } else { |
|
81 | 79 | $this->knownLoggedIn = true; |
82 | 80 | } |
83 | 81 | } |
@@ -96,11 +94,9 @@ discard block |
||
96 | 94 | |
97 | 95 | if ($method == 'GET') { |
98 | 96 | $data = $this->httpHelper->get($this->mediawikiWebServiceEndpoint, $apiParams); |
99 | - } |
|
100 | - elseif ($method == 'POST') { |
|
97 | + } elseif ($method == 'POST') { |
|
101 | 98 | $data = $this->httpHelper->post($this->mediawikiWebServiceEndpoint, $apiParams); |
102 | - } |
|
103 | - else { |
|
99 | + } else { |
|
104 | 100 | throw new ApplicationLogicException('Unsupported HTTP Method'); |
105 | 101 | } |
106 | 102 |
@@ -74,8 +74,7 @@ |
||
74 | 74 | |
75 | 75 | if ($domain !== null) { |
76 | 76 | $log->setDomain($domain); |
77 | - } |
|
78 | - elseif (method_exists($object, 'getDomain')) { |
|
77 | + } elseif (method_exists($object, 'getDomain')) { |
|
79 | 78 | $log->setDomain($object->getDomain()); |
80 | 79 | } |
81 | 80 |
@@ -35,427 +35,427 @@ |
||
35 | 35 | */ |
36 | 36 | class Logger |
37 | 37 | { |
38 | - /** |
|
39 | - * @param PdoDatabase $database |
|
40 | - * @param Request $object |
|
41 | - */ |
|
42 | - public static function emailConfirmed(PdoDatabase $database, Request $object) |
|
43 | - { |
|
44 | - self::createLogEntry($database, $object, "Email Confirmed", null, User::getCommunity()); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * @param PdoDatabase $database |
|
49 | - * @param DataObject $object |
|
50 | - * @param string $logAction |
|
51 | - * @param null|string $comment |
|
52 | - * @param User|null $user |
|
53 | - * @param int|null $domain |
|
54 | - * |
|
55 | - * @throws Exception |
|
56 | - */ |
|
57 | - private static function createLogEntry( |
|
58 | - PdoDatabase $database, |
|
59 | - DataObject $object, |
|
60 | - $logAction, |
|
61 | - $comment = null, |
|
62 | - $user = null, |
|
63 | - ?int $domain = null |
|
64 | - ) { |
|
65 | - if ($user == null) { |
|
66 | - $user = User::getCurrent($database); |
|
67 | - } |
|
68 | - |
|
69 | - $objectType = get_class($object); |
|
70 | - if (strpos($objectType, 'Waca\\DataObjects\\') !== false) { |
|
71 | - $objectType = str_replace('Waca\\DataObjects\\', '', $objectType); |
|
72 | - } |
|
73 | - |
|
74 | - $log = new Log(); |
|
75 | - |
|
76 | - if ($domain !== null) { |
|
77 | - $log->setDomain($domain); |
|
78 | - } |
|
79 | - elseif (method_exists($object, 'getDomain')) { |
|
80 | - $log->setDomain($object->getDomain()); |
|
81 | - } |
|
82 | - |
|
83 | - $log->setDatabase($database); |
|
84 | - $log->setAction($logAction); |
|
85 | - $log->setObjectId($object->getId()); |
|
86 | - $log->setObjectType($objectType); |
|
87 | - $log->setUser($user); |
|
88 | - $log->setComment($comment); |
|
89 | - $log->save(); |
|
90 | - } |
|
91 | - |
|
92 | - #region Users |
|
93 | - |
|
94 | - /** |
|
95 | - * @throws Exception |
|
96 | - */ |
|
97 | - public static function newUser(PdoDatabase $database, User $user) |
|
98 | - { |
|
99 | - self::createLogEntry($database, $user, 'Registered', null, User::getCommunity()); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * @throws Exception |
|
104 | - */ |
|
105 | - public static function approvedUser(PdoDatabase $database, User $object) |
|
106 | - { |
|
107 | - self::createLogEntry($database, $object, "Approved"); |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @throws Exception |
|
112 | - */ |
|
113 | - public static function deactivatedUser(PdoDatabase $database, User $object, string $comment) |
|
114 | - { |
|
115 | - self::createLogEntry($database, $object, 'DeactivatedUser', $comment); |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @throws Exception |
|
120 | - */ |
|
121 | - public static function requestedReactivation(PdoDatabase $database, User $object, string $comment) |
|
122 | - { |
|
123 | - self::createLogEntry($database, $object, 'RequestedReactivation', $comment); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @throws Exception |
|
128 | - */ |
|
129 | - public static function renamedUser(PdoDatabase $database, User $object, string $comment) |
|
130 | - { |
|
131 | - self::createLogEntry($database, $object, "Renamed", $comment); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @throws Exception |
|
136 | - */ |
|
137 | - public static function userPreferencesChange(PdoDatabase $database, User $object) |
|
138 | - { |
|
139 | - self::createLogEntry($database, $object, "Prefchange"); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @throws Exception |
|
144 | - */ |
|
145 | - public static function userRolesEdited( |
|
146 | - PdoDatabase $database, |
|
147 | - User $object, |
|
148 | - string $reason, |
|
149 | - array $added, |
|
150 | - array $removed, |
|
151 | - int $domain |
|
152 | - ) { |
|
153 | - $logData = serialize(array( |
|
154 | - 'added' => $added, |
|
155 | - 'removed' => $removed, |
|
156 | - 'reason' => $reason, |
|
157 | - )); |
|
158 | - |
|
159 | - self::createLogEntry($database, $object, "RoleChange", $logData, null, $domain); |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * @throws Exception |
|
164 | - */ |
|
165 | - public static function userGlobalRolesEdited( |
|
166 | - PdoDatabase $database, |
|
167 | - User $object, |
|
168 | - string $reason, |
|
169 | - array $added, |
|
170 | - array $removed |
|
171 | - ): void { |
|
172 | - $logData = serialize(array( |
|
173 | - 'added' => $added, |
|
174 | - 'removed' => $removed, |
|
175 | - 'reason' => $reason, |
|
176 | - )); |
|
177 | - |
|
178 | - self::createLogEntry($database, $object, "GlobalRoleChange", $logData); |
|
179 | - } |
|
180 | - |
|
181 | - #endregion |
|
182 | - |
|
183 | - /** |
|
184 | - * @param PdoDatabase $database |
|
185 | - * @param SiteNotice $object |
|
186 | - */ |
|
187 | - public static function siteNoticeEdited(PdoDatabase $database, SiteNotice $object) |
|
188 | - { |
|
189 | - self::createLogEntry($database, $object, "Edited"); |
|
190 | - } |
|
191 | - |
|
192 | - #region Welcome Templates |
|
193 | - |
|
194 | - /** |
|
195 | - * @param PdoDatabase $database |
|
196 | - * @param WelcomeTemplate $object |
|
197 | - */ |
|
198 | - public static function welcomeTemplateCreated(PdoDatabase $database, WelcomeTemplate $object) |
|
199 | - { |
|
200 | - self::createLogEntry($database, $object, "CreatedTemplate"); |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * @param PdoDatabase $database |
|
205 | - * @param WelcomeTemplate $object |
|
206 | - */ |
|
207 | - public static function welcomeTemplateEdited(PdoDatabase $database, WelcomeTemplate $object) |
|
208 | - { |
|
209 | - self::createLogEntry($database, $object, "EditedTemplate"); |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * @param PdoDatabase $database |
|
214 | - * @param WelcomeTemplate $object |
|
215 | - */ |
|
216 | - public static function welcomeTemplateDeleted(PdoDatabase $database, WelcomeTemplate $object) |
|
217 | - { |
|
218 | - self::createLogEntry($database, $object, "DeletedTemplate"); |
|
219 | - } |
|
220 | - |
|
221 | - #endregion |
|
222 | - |
|
223 | - #region Bans |
|
224 | - |
|
225 | - /** |
|
226 | - * @param PdoDatabase $database |
|
227 | - * @param Ban $object |
|
228 | - * @param string $reason |
|
229 | - */ |
|
230 | - public static function banned(PdoDatabase $database, Ban $object, $reason) |
|
231 | - { |
|
232 | - self::createLogEntry($database, $object, "Banned", $reason); |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * @param PdoDatabase $database |
|
237 | - * @param Ban $object |
|
238 | - * @param string $reason |
|
239 | - */ |
|
240 | - public static function unbanned(PdoDatabase $database, Ban $object, $reason) |
|
241 | - { |
|
242 | - self::createLogEntry($database, $object, "Unbanned", $reason); |
|
243 | - } |
|
244 | - |
|
245 | - public static function banReplaced(PdoDatabase $database, Ban $object) |
|
246 | - { |
|
247 | - self::createLogEntry($database, $object, "BanReplaced"); |
|
248 | - } |
|
249 | - |
|
250 | - #endregion |
|
251 | - |
|
252 | - #region Requests |
|
253 | - |
|
254 | - /** |
|
255 | - * @param PdoDatabase $database |
|
256 | - * @param Request $object |
|
257 | - * @param string $target |
|
258 | - */ |
|
259 | - public static function deferRequest(PdoDatabase $database, Request $object, $target) |
|
260 | - { |
|
261 | - self::createLogEntry($database, $object, "Deferred to $target"); |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * @param PdoDatabase $database |
|
266 | - * @param Request $object |
|
267 | - * @param integer $target |
|
268 | - * @param string $comment |
|
269 | - * @param User|null $logUser |
|
270 | - */ |
|
271 | - public static function closeRequest(PdoDatabase $database, Request $object, $target, $comment, User $logUser = null) |
|
272 | - { |
|
273 | - self::createLogEntry($database, $object, "Closed $target", $comment, $logUser); |
|
274 | - } |
|
275 | - |
|
276 | - public static function manuallyConfirmRequest(PdoDatabase $database, Request $object) |
|
277 | - { |
|
278 | - self::createLogEntry($database, $object, "Manually Confirmed"); |
|
279 | - } |
|
280 | - |
|
281 | - /** |
|
282 | - * @param PdoDatabase $database |
|
283 | - * @param Request $object |
|
284 | - */ |
|
285 | - public static function reserve(PdoDatabase $database, Request $object) |
|
286 | - { |
|
287 | - self::createLogEntry($database, $object, "Reserved"); |
|
288 | - } |
|
289 | - |
|
290 | - /** |
|
291 | - * @param PdoDatabase $database |
|
292 | - * @param Request $object |
|
293 | - */ |
|
294 | - public static function breakReserve(PdoDatabase $database, Request $object) |
|
295 | - { |
|
296 | - self::createLogEntry($database, $object, "BreakReserve"); |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * @param PdoDatabase $database |
|
301 | - * @param Request $object |
|
302 | - */ |
|
303 | - public static function unreserve(PdoDatabase $database, Request $object) |
|
304 | - { |
|
305 | - self::createLogEntry($database, $object, "Unreserved"); |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * @param PdoDatabase $database |
|
310 | - * @param Comment $object |
|
311 | - * @param Request $request |
|
312 | - */ |
|
313 | - public static function editComment(PdoDatabase $database, Comment $object, Request $request) |
|
314 | - { |
|
315 | - self::createLogEntry($database, $request, "EditComment-r"); |
|
316 | - self::createLogEntry($database, $object, "EditComment-c", null, null, $request->getDomain()); |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * @param PdoDatabase $database |
|
321 | - * @param Comment $object |
|
322 | - */ |
|
323 | - public static function flaggedComment(PdoDatabase $database, Comment $object, int $domain) |
|
324 | - { |
|
325 | - self::createLogEntry($database, $object, "FlaggedComment", null, null, $domain); |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * @param PdoDatabase $database |
|
330 | - * @param Comment $object |
|
331 | - */ |
|
332 | - public static function unflaggedComment(PdoDatabase $database, Comment $object, int $domain) |
|
333 | - { |
|
334 | - self::createLogEntry($database, $object, "UnflaggedComment", null, null, $domain); |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * @param PdoDatabase $database |
|
339 | - * @param Request $object |
|
340 | - * @param User $target |
|
341 | - */ |
|
342 | - public static function sendReservation(PdoDatabase $database, Request $object, User $target) |
|
343 | - { |
|
344 | - self::createLogEntry($database, $object, "SendReserved"); |
|
345 | - self::createLogEntry($database, $object, "ReceiveReserved", null, $target); |
|
346 | - } |
|
347 | - |
|
348 | - /** |
|
349 | - * @param PdoDatabase $database |
|
350 | - * @param Request $object |
|
351 | - * @param string $comment |
|
352 | - */ |
|
353 | - public static function sentMail(PdoDatabase $database, Request $object, $comment) |
|
354 | - { |
|
355 | - self::createLogEntry($database, $object, "SentMail", $comment); |
|
356 | - } |
|
357 | - |
|
358 | - /** |
|
359 | - * @param PdoDatabase $database |
|
360 | - * @param Request $object |
|
361 | - */ |
|
362 | - public static function enqueuedJobQueue(PdoDatabase $database, Request $object) |
|
363 | - { |
|
364 | - self::createLogEntry($database, $object, 'EnqueuedJobQueue'); |
|
365 | - } |
|
366 | - |
|
367 | - public static function hospitalised(PdoDatabase $database, Request $object) |
|
368 | - { |
|
369 | - self::createLogEntry($database, $object, 'Hospitalised'); |
|
370 | - } |
|
371 | - #endregion |
|
372 | - |
|
373 | - #region Email templates |
|
374 | - |
|
375 | - /** |
|
376 | - * @param PdoDatabase $database |
|
377 | - * @param EmailTemplate $object |
|
378 | - */ |
|
379 | - public static function createEmail(PdoDatabase $database, EmailTemplate $object) |
|
380 | - { |
|
381 | - self::createLogEntry($database, $object, "CreatedEmail"); |
|
382 | - } |
|
383 | - |
|
384 | - /** |
|
385 | - * @param PdoDatabase $database |
|
386 | - * @param EmailTemplate $object |
|
387 | - */ |
|
388 | - public static function editedEmail(PdoDatabase $database, EmailTemplate $object) |
|
389 | - { |
|
390 | - self::createLogEntry($database, $object, "EditedEmail"); |
|
391 | - } |
|
392 | - |
|
393 | - #endregion |
|
394 | - |
|
395 | - #region Display |
|
396 | - |
|
397 | - #endregion |
|
398 | - |
|
399 | - #region Automation |
|
400 | - |
|
401 | - public static function backgroundJobComplete(PdoDatabase $database, JobQueue $job) |
|
402 | - { |
|
403 | - self::createLogEntry($database, $job, 'JobCompleted', null, User::getCommunity()); |
|
404 | - } |
|
405 | - |
|
406 | - public static function backgroundJobIssue(PdoDatabase $database, JobQueue $job) |
|
407 | - { |
|
408 | - $data = array('status' => $job->getStatus(), 'error' => $job->getError()); |
|
409 | - self::createLogEntry($database, $job, 'JobIssue', serialize($data), User::getCommunity()); |
|
410 | - } |
|
411 | - |
|
412 | - public static function backgroundJobCancelled(PdoDatabase $database, JobQueue $job) |
|
413 | - { |
|
414 | - self::createLogEntry($database, $job, 'JobCancelled', $job->getError()); |
|
415 | - } |
|
416 | - |
|
417 | - public static function backgroundJobRequeued(PdoDatabase $database, JobQueue $job) |
|
418 | - { |
|
419 | - self::createLogEntry($database, $job, 'JobRequeued'); |
|
420 | - } |
|
421 | - |
|
422 | - public static function backgroundJobAcknowledged(PdoDatabase $database, JobQueue $job, $comment = null) |
|
423 | - { |
|
424 | - self::createLogEntry($database, $job, 'JobAcknowledged', $comment); |
|
425 | - } |
|
426 | - #endregion |
|
427 | - |
|
428 | - #region Request Queues |
|
429 | - public static function requestQueueCreated(PdoDatabase $database, RequestQueue $queue) |
|
430 | - { |
|
431 | - self::createLogEntry($database, $queue, 'QueueCreated'); |
|
432 | - } |
|
433 | - |
|
434 | - public static function requestQueueEdited(PdoDatabase $database, RequestQueue $queue) |
|
435 | - { |
|
436 | - self::createLogEntry($database, $queue, 'QueueEdited'); |
|
437 | - } |
|
438 | - #endregion |
|
439 | - #region Domains |
|
440 | - public static function domainCreated(PdoDatabase $database, Domain $domain) |
|
441 | - { |
|
442 | - self::createLogEntry($database, $domain, 'DomainCreated'); |
|
443 | - } |
|
444 | - |
|
445 | - public static function domainEdited(PdoDatabase $database, Domain $domain) |
|
446 | - { |
|
447 | - self::createLogEntry($database, $domain, 'DomainEdited'); |
|
448 | - } |
|
449 | - #endregion |
|
450 | - #region Request Forms |
|
451 | - public static function requestFormCreated(PdoDatabase $database, RequestForm $requestForm) |
|
452 | - { |
|
453 | - self::createLogEntry($database, $requestForm, 'RequestFormCreated'); |
|
454 | - } |
|
455 | - |
|
456 | - public static function requestFormEdited(PdoDatabase $database, RequestForm $requestForm) |
|
457 | - { |
|
458 | - self::createLogEntry($database, $requestForm, 'RequestFormEdited'); |
|
459 | - } |
|
460 | - #endregion |
|
38 | + /** |
|
39 | + * @param PdoDatabase $database |
|
40 | + * @param Request $object |
|
41 | + */ |
|
42 | + public static function emailConfirmed(PdoDatabase $database, Request $object) |
|
43 | + { |
|
44 | + self::createLogEntry($database, $object, "Email Confirmed", null, User::getCommunity()); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * @param PdoDatabase $database |
|
49 | + * @param DataObject $object |
|
50 | + * @param string $logAction |
|
51 | + * @param null|string $comment |
|
52 | + * @param User|null $user |
|
53 | + * @param int|null $domain |
|
54 | + * |
|
55 | + * @throws Exception |
|
56 | + */ |
|
57 | + private static function createLogEntry( |
|
58 | + PdoDatabase $database, |
|
59 | + DataObject $object, |
|
60 | + $logAction, |
|
61 | + $comment = null, |
|
62 | + $user = null, |
|
63 | + ?int $domain = null |
|
64 | + ) { |
|
65 | + if ($user == null) { |
|
66 | + $user = User::getCurrent($database); |
|
67 | + } |
|
68 | + |
|
69 | + $objectType = get_class($object); |
|
70 | + if (strpos($objectType, 'Waca\\DataObjects\\') !== false) { |
|
71 | + $objectType = str_replace('Waca\\DataObjects\\', '', $objectType); |
|
72 | + } |
|
73 | + |
|
74 | + $log = new Log(); |
|
75 | + |
|
76 | + if ($domain !== null) { |
|
77 | + $log->setDomain($domain); |
|
78 | + } |
|
79 | + elseif (method_exists($object, 'getDomain')) { |
|
80 | + $log->setDomain($object->getDomain()); |
|
81 | + } |
|
82 | + |
|
83 | + $log->setDatabase($database); |
|
84 | + $log->setAction($logAction); |
|
85 | + $log->setObjectId($object->getId()); |
|
86 | + $log->setObjectType($objectType); |
|
87 | + $log->setUser($user); |
|
88 | + $log->setComment($comment); |
|
89 | + $log->save(); |
|
90 | + } |
|
91 | + |
|
92 | + #region Users |
|
93 | + |
|
94 | + /** |
|
95 | + * @throws Exception |
|
96 | + */ |
|
97 | + public static function newUser(PdoDatabase $database, User $user) |
|
98 | + { |
|
99 | + self::createLogEntry($database, $user, 'Registered', null, User::getCommunity()); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * @throws Exception |
|
104 | + */ |
|
105 | + public static function approvedUser(PdoDatabase $database, User $object) |
|
106 | + { |
|
107 | + self::createLogEntry($database, $object, "Approved"); |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @throws Exception |
|
112 | + */ |
|
113 | + public static function deactivatedUser(PdoDatabase $database, User $object, string $comment) |
|
114 | + { |
|
115 | + self::createLogEntry($database, $object, 'DeactivatedUser', $comment); |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @throws Exception |
|
120 | + */ |
|
121 | + public static function requestedReactivation(PdoDatabase $database, User $object, string $comment) |
|
122 | + { |
|
123 | + self::createLogEntry($database, $object, 'RequestedReactivation', $comment); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @throws Exception |
|
128 | + */ |
|
129 | + public static function renamedUser(PdoDatabase $database, User $object, string $comment) |
|
130 | + { |
|
131 | + self::createLogEntry($database, $object, "Renamed", $comment); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @throws Exception |
|
136 | + */ |
|
137 | + public static function userPreferencesChange(PdoDatabase $database, User $object) |
|
138 | + { |
|
139 | + self::createLogEntry($database, $object, "Prefchange"); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @throws Exception |
|
144 | + */ |
|
145 | + public static function userRolesEdited( |
|
146 | + PdoDatabase $database, |
|
147 | + User $object, |
|
148 | + string $reason, |
|
149 | + array $added, |
|
150 | + array $removed, |
|
151 | + int $domain |
|
152 | + ) { |
|
153 | + $logData = serialize(array( |
|
154 | + 'added' => $added, |
|
155 | + 'removed' => $removed, |
|
156 | + 'reason' => $reason, |
|
157 | + )); |
|
158 | + |
|
159 | + self::createLogEntry($database, $object, "RoleChange", $logData, null, $domain); |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * @throws Exception |
|
164 | + */ |
|
165 | + public static function userGlobalRolesEdited( |
|
166 | + PdoDatabase $database, |
|
167 | + User $object, |
|
168 | + string $reason, |
|
169 | + array $added, |
|
170 | + array $removed |
|
171 | + ): void { |
|
172 | + $logData = serialize(array( |
|
173 | + 'added' => $added, |
|
174 | + 'removed' => $removed, |
|
175 | + 'reason' => $reason, |
|
176 | + )); |
|
177 | + |
|
178 | + self::createLogEntry($database, $object, "GlobalRoleChange", $logData); |
|
179 | + } |
|
180 | + |
|
181 | + #endregion |
|
182 | + |
|
183 | + /** |
|
184 | + * @param PdoDatabase $database |
|
185 | + * @param SiteNotice $object |
|
186 | + */ |
|
187 | + public static function siteNoticeEdited(PdoDatabase $database, SiteNotice $object) |
|
188 | + { |
|
189 | + self::createLogEntry($database, $object, "Edited"); |
|
190 | + } |
|
191 | + |
|
192 | + #region Welcome Templates |
|
193 | + |
|
194 | + /** |
|
195 | + * @param PdoDatabase $database |
|
196 | + * @param WelcomeTemplate $object |
|
197 | + */ |
|
198 | + public static function welcomeTemplateCreated(PdoDatabase $database, WelcomeTemplate $object) |
|
199 | + { |
|
200 | + self::createLogEntry($database, $object, "CreatedTemplate"); |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * @param PdoDatabase $database |
|
205 | + * @param WelcomeTemplate $object |
|
206 | + */ |
|
207 | + public static function welcomeTemplateEdited(PdoDatabase $database, WelcomeTemplate $object) |
|
208 | + { |
|
209 | + self::createLogEntry($database, $object, "EditedTemplate"); |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * @param PdoDatabase $database |
|
214 | + * @param WelcomeTemplate $object |
|
215 | + */ |
|
216 | + public static function welcomeTemplateDeleted(PdoDatabase $database, WelcomeTemplate $object) |
|
217 | + { |
|
218 | + self::createLogEntry($database, $object, "DeletedTemplate"); |
|
219 | + } |
|
220 | + |
|
221 | + #endregion |
|
222 | + |
|
223 | + #region Bans |
|
224 | + |
|
225 | + /** |
|
226 | + * @param PdoDatabase $database |
|
227 | + * @param Ban $object |
|
228 | + * @param string $reason |
|
229 | + */ |
|
230 | + public static function banned(PdoDatabase $database, Ban $object, $reason) |
|
231 | + { |
|
232 | + self::createLogEntry($database, $object, "Banned", $reason); |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * @param PdoDatabase $database |
|
237 | + * @param Ban $object |
|
238 | + * @param string $reason |
|
239 | + */ |
|
240 | + public static function unbanned(PdoDatabase $database, Ban $object, $reason) |
|
241 | + { |
|
242 | + self::createLogEntry($database, $object, "Unbanned", $reason); |
|
243 | + } |
|
244 | + |
|
245 | + public static function banReplaced(PdoDatabase $database, Ban $object) |
|
246 | + { |
|
247 | + self::createLogEntry($database, $object, "BanReplaced"); |
|
248 | + } |
|
249 | + |
|
250 | + #endregion |
|
251 | + |
|
252 | + #region Requests |
|
253 | + |
|
254 | + /** |
|
255 | + * @param PdoDatabase $database |
|
256 | + * @param Request $object |
|
257 | + * @param string $target |
|
258 | + */ |
|
259 | + public static function deferRequest(PdoDatabase $database, Request $object, $target) |
|
260 | + { |
|
261 | + self::createLogEntry($database, $object, "Deferred to $target"); |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * @param PdoDatabase $database |
|
266 | + * @param Request $object |
|
267 | + * @param integer $target |
|
268 | + * @param string $comment |
|
269 | + * @param User|null $logUser |
|
270 | + */ |
|
271 | + public static function closeRequest(PdoDatabase $database, Request $object, $target, $comment, User $logUser = null) |
|
272 | + { |
|
273 | + self::createLogEntry($database, $object, "Closed $target", $comment, $logUser); |
|
274 | + } |
|
275 | + |
|
276 | + public static function manuallyConfirmRequest(PdoDatabase $database, Request $object) |
|
277 | + { |
|
278 | + self::createLogEntry($database, $object, "Manually Confirmed"); |
|
279 | + } |
|
280 | + |
|
281 | + /** |
|
282 | + * @param PdoDatabase $database |
|
283 | + * @param Request $object |
|
284 | + */ |
|
285 | + public static function reserve(PdoDatabase $database, Request $object) |
|
286 | + { |
|
287 | + self::createLogEntry($database, $object, "Reserved"); |
|
288 | + } |
|
289 | + |
|
290 | + /** |
|
291 | + * @param PdoDatabase $database |
|
292 | + * @param Request $object |
|
293 | + */ |
|
294 | + public static function breakReserve(PdoDatabase $database, Request $object) |
|
295 | + { |
|
296 | + self::createLogEntry($database, $object, "BreakReserve"); |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * @param PdoDatabase $database |
|
301 | + * @param Request $object |
|
302 | + */ |
|
303 | + public static function unreserve(PdoDatabase $database, Request $object) |
|
304 | + { |
|
305 | + self::createLogEntry($database, $object, "Unreserved"); |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * @param PdoDatabase $database |
|
310 | + * @param Comment $object |
|
311 | + * @param Request $request |
|
312 | + */ |
|
313 | + public static function editComment(PdoDatabase $database, Comment $object, Request $request) |
|
314 | + { |
|
315 | + self::createLogEntry($database, $request, "EditComment-r"); |
|
316 | + self::createLogEntry($database, $object, "EditComment-c", null, null, $request->getDomain()); |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * @param PdoDatabase $database |
|
321 | + * @param Comment $object |
|
322 | + */ |
|
323 | + public static function flaggedComment(PdoDatabase $database, Comment $object, int $domain) |
|
324 | + { |
|
325 | + self::createLogEntry($database, $object, "FlaggedComment", null, null, $domain); |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * @param PdoDatabase $database |
|
330 | + * @param Comment $object |
|
331 | + */ |
|
332 | + public static function unflaggedComment(PdoDatabase $database, Comment $object, int $domain) |
|
333 | + { |
|
334 | + self::createLogEntry($database, $object, "UnflaggedComment", null, null, $domain); |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * @param PdoDatabase $database |
|
339 | + * @param Request $object |
|
340 | + * @param User $target |
|
341 | + */ |
|
342 | + public static function sendReservation(PdoDatabase $database, Request $object, User $target) |
|
343 | + { |
|
344 | + self::createLogEntry($database, $object, "SendReserved"); |
|
345 | + self::createLogEntry($database, $object, "ReceiveReserved", null, $target); |
|
346 | + } |
|
347 | + |
|
348 | + /** |
|
349 | + * @param PdoDatabase $database |
|
350 | + * @param Request $object |
|
351 | + * @param string $comment |
|
352 | + */ |
|
353 | + public static function sentMail(PdoDatabase $database, Request $object, $comment) |
|
354 | + { |
|
355 | + self::createLogEntry($database, $object, "SentMail", $comment); |
|
356 | + } |
|
357 | + |
|
358 | + /** |
|
359 | + * @param PdoDatabase $database |
|
360 | + * @param Request $object |
|
361 | + */ |
|
362 | + public static function enqueuedJobQueue(PdoDatabase $database, Request $object) |
|
363 | + { |
|
364 | + self::createLogEntry($database, $object, 'EnqueuedJobQueue'); |
|
365 | + } |
|
366 | + |
|
367 | + public static function hospitalised(PdoDatabase $database, Request $object) |
|
368 | + { |
|
369 | + self::createLogEntry($database, $object, 'Hospitalised'); |
|
370 | + } |
|
371 | + #endregion |
|
372 | + |
|
373 | + #region Email templates |
|
374 | + |
|
375 | + /** |
|
376 | + * @param PdoDatabase $database |
|
377 | + * @param EmailTemplate $object |
|
378 | + */ |
|
379 | + public static function createEmail(PdoDatabase $database, EmailTemplate $object) |
|
380 | + { |
|
381 | + self::createLogEntry($database, $object, "CreatedEmail"); |
|
382 | + } |
|
383 | + |
|
384 | + /** |
|
385 | + * @param PdoDatabase $database |
|
386 | + * @param EmailTemplate $object |
|
387 | + */ |
|
388 | + public static function editedEmail(PdoDatabase $database, EmailTemplate $object) |
|
389 | + { |
|
390 | + self::createLogEntry($database, $object, "EditedEmail"); |
|
391 | + } |
|
392 | + |
|
393 | + #endregion |
|
394 | + |
|
395 | + #region Display |
|
396 | + |
|
397 | + #endregion |
|
398 | + |
|
399 | + #region Automation |
|
400 | + |
|
401 | + public static function backgroundJobComplete(PdoDatabase $database, JobQueue $job) |
|
402 | + { |
|
403 | + self::createLogEntry($database, $job, 'JobCompleted', null, User::getCommunity()); |
|
404 | + } |
|
405 | + |
|
406 | + public static function backgroundJobIssue(PdoDatabase $database, JobQueue $job) |
|
407 | + { |
|
408 | + $data = array('status' => $job->getStatus(), 'error' => $job->getError()); |
|
409 | + self::createLogEntry($database, $job, 'JobIssue', serialize($data), User::getCommunity()); |
|
410 | + } |
|
411 | + |
|
412 | + public static function backgroundJobCancelled(PdoDatabase $database, JobQueue $job) |
|
413 | + { |
|
414 | + self::createLogEntry($database, $job, 'JobCancelled', $job->getError()); |
|
415 | + } |
|
416 | + |
|
417 | + public static function backgroundJobRequeued(PdoDatabase $database, JobQueue $job) |
|
418 | + { |
|
419 | + self::createLogEntry($database, $job, 'JobRequeued'); |
|
420 | + } |
|
421 | + |
|
422 | + public static function backgroundJobAcknowledged(PdoDatabase $database, JobQueue $job, $comment = null) |
|
423 | + { |
|
424 | + self::createLogEntry($database, $job, 'JobAcknowledged', $comment); |
|
425 | + } |
|
426 | + #endregion |
|
427 | + |
|
428 | + #region Request Queues |
|
429 | + public static function requestQueueCreated(PdoDatabase $database, RequestQueue $queue) |
|
430 | + { |
|
431 | + self::createLogEntry($database, $queue, 'QueueCreated'); |
|
432 | + } |
|
433 | + |
|
434 | + public static function requestQueueEdited(PdoDatabase $database, RequestQueue $queue) |
|
435 | + { |
|
436 | + self::createLogEntry($database, $queue, 'QueueEdited'); |
|
437 | + } |
|
438 | + #endregion |
|
439 | + #region Domains |
|
440 | + public static function domainCreated(PdoDatabase $database, Domain $domain) |
|
441 | + { |
|
442 | + self::createLogEntry($database, $domain, 'DomainCreated'); |
|
443 | + } |
|
444 | + |
|
445 | + public static function domainEdited(PdoDatabase $database, Domain $domain) |
|
446 | + { |
|
447 | + self::createLogEntry($database, $domain, 'DomainEdited'); |
|
448 | + } |
|
449 | + #endregion |
|
450 | + #region Request Forms |
|
451 | + public static function requestFormCreated(PdoDatabase $database, RequestForm $requestForm) |
|
452 | + { |
|
453 | + self::createLogEntry($database, $requestForm, 'RequestFormCreated'); |
|
454 | + } |
|
455 | + |
|
456 | + public static function requestFormEdited(PdoDatabase $database, RequestForm $requestForm) |
|
457 | + { |
|
458 | + self::createLogEntry($database, $requestForm, 'RequestFormEdited'); |
|
459 | + } |
|
460 | + #endregion |
|
461 | 461 | } |
@@ -18,135 +18,135 @@ |
||
18 | 18 | |
19 | 19 | class OAuthProtocolHelper implements Interfaces\IOAuthProtocolHelper |
20 | 20 | { |
21 | - private $authUrl; |
|
22 | - /** |
|
23 | - * @var PdoDatabase |
|
24 | - */ |
|
25 | - private $database; |
|
26 | - /** |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - private $consumerKey; |
|
30 | - /** |
|
31 | - * @var string |
|
32 | - */ |
|
33 | - private $consumerSecret; |
|
34 | - /** |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - private $userAgent; |
|
38 | - |
|
39 | - /** |
|
40 | - * OAuthHelper constructor. |
|
41 | - * |
|
42 | - * @param string $consumerKey |
|
43 | - * @param string $consumerSecret |
|
44 | - * @param PdoDatabase $database |
|
45 | - * @param string $userAgent |
|
46 | - */ |
|
47 | - public function __construct( |
|
48 | - $consumerKey, |
|
49 | - $consumerSecret, |
|
50 | - PdoDatabase $database, |
|
51 | - $userAgent |
|
52 | - ) { |
|
53 | - $this->consumerKey = $consumerKey; |
|
54 | - $this->consumerSecret = $consumerSecret; |
|
55 | - $this->userAgent = $userAgent; |
|
56 | - $this->database = $database; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * @inheritDoc |
|
61 | - */ |
|
62 | - public function getRequestToken() |
|
63 | - { |
|
64 | - /** @var Token $requestToken */ |
|
65 | - |
|
66 | - // FIXME: domains! |
|
67 | - /** @var Domain $domain */ |
|
68 | - $domain = Domain::getById(1, $this->database); |
|
69 | - |
|
70 | - list($authUrl, $requestToken) = $this->getClient($domain)->initiate(); |
|
71 | - $this->authUrl = $authUrl; |
|
72 | - return $requestToken; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @inheritDoc |
|
77 | - */ |
|
78 | - public function getAuthoriseUrl($requestToken) |
|
79 | - { |
|
80 | - return $this->authUrl; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @inheritDoc |
|
85 | - */ |
|
86 | - public function callbackCompleted($oauthRequestToken, $oauthRequestSecret, $oauthVerifier) |
|
87 | - { |
|
88 | - $requestToken = new Token($oauthRequestToken, $oauthRequestSecret); |
|
89 | - |
|
90 | - // FIXME: domains! |
|
91 | - /** @var Domain $domain */ |
|
92 | - $domain = Domain::getById(1, $this->database); |
|
93 | - |
|
94 | - return $this->getClient($domain)->complete($requestToken, $oauthVerifier); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * @inheritDoc |
|
99 | - */ |
|
100 | - public function getIdentityTicket($oauthAccessToken, $oauthAccessSecret) |
|
101 | - { |
|
102 | - // FIXME: domains! |
|
103 | - /** @var Domain $domain */ |
|
104 | - $domain = Domain::getById(1, $this->database); |
|
105 | - |
|
106 | - return $this->getClient($domain)->identify(new Token($oauthAccessToken, $oauthAccessSecret)); |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * @inheritDoc |
|
111 | - */ |
|
112 | - public function apiCall($apiParams, $accessToken, $accessSecret, $method = 'GET') |
|
113 | - { |
|
114 | - $userToken = new Token($accessToken, $accessSecret); |
|
115 | - |
|
116 | - $apiParams['format'] = 'json'; |
|
117 | - |
|
118 | - if ($apiParams === null || !is_array($apiParams)) { |
|
119 | - throw new CurlException("Invalid API call"); |
|
120 | - } |
|
121 | - |
|
122 | - // FIXME: domains! |
|
123 | - /** @var Domain $domain */ |
|
124 | - $domain = Domain::getById(1, $this->database); |
|
125 | - |
|
126 | - $url = $domain->getWikiApiPath(); |
|
127 | - $isPost = ($method === 'POST'); |
|
128 | - |
|
129 | - if ($method === 'GET') { |
|
130 | - $query = http_build_query($apiParams); |
|
131 | - $url .= '?' . $query; |
|
132 | - $apiParams = null; |
|
133 | - } |
|
134 | - |
|
135 | - $data = $this->getClient($domain)->makeOAuthCall($userToken, $url, $isPost, $apiParams); |
|
136 | - |
|
137 | - return json_decode($data); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * @param string $oauthEndpoint |
|
142 | - * |
|
143 | - * @return Client |
|
144 | - */ |
|
145 | - protected function getClient(Domain $domain) : Client |
|
146 | - { |
|
147 | - $oauthClientConfig = new ClientConfig($domain->getWikiArticlePath() . "?title=Special:OAuth"); |
|
148 | - $oauthClientConfig->setConsumer(new Consumer($this->consumerKey, $this->consumerSecret)); |
|
149 | - $oauthClientConfig->setUserAgent($this->userAgent); |
|
150 | - return new Client($oauthClientConfig); |
|
151 | - } |
|
21 | + private $authUrl; |
|
22 | + /** |
|
23 | + * @var PdoDatabase |
|
24 | + */ |
|
25 | + private $database; |
|
26 | + /** |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + private $consumerKey; |
|
30 | + /** |
|
31 | + * @var string |
|
32 | + */ |
|
33 | + private $consumerSecret; |
|
34 | + /** |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + private $userAgent; |
|
38 | + |
|
39 | + /** |
|
40 | + * OAuthHelper constructor. |
|
41 | + * |
|
42 | + * @param string $consumerKey |
|
43 | + * @param string $consumerSecret |
|
44 | + * @param PdoDatabase $database |
|
45 | + * @param string $userAgent |
|
46 | + */ |
|
47 | + public function __construct( |
|
48 | + $consumerKey, |
|
49 | + $consumerSecret, |
|
50 | + PdoDatabase $database, |
|
51 | + $userAgent |
|
52 | + ) { |
|
53 | + $this->consumerKey = $consumerKey; |
|
54 | + $this->consumerSecret = $consumerSecret; |
|
55 | + $this->userAgent = $userAgent; |
|
56 | + $this->database = $database; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * @inheritDoc |
|
61 | + */ |
|
62 | + public function getRequestToken() |
|
63 | + { |
|
64 | + /** @var Token $requestToken */ |
|
65 | + |
|
66 | + // FIXME: domains! |
|
67 | + /** @var Domain $domain */ |
|
68 | + $domain = Domain::getById(1, $this->database); |
|
69 | + |
|
70 | + list($authUrl, $requestToken) = $this->getClient($domain)->initiate(); |
|
71 | + $this->authUrl = $authUrl; |
|
72 | + return $requestToken; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @inheritDoc |
|
77 | + */ |
|
78 | + public function getAuthoriseUrl($requestToken) |
|
79 | + { |
|
80 | + return $this->authUrl; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @inheritDoc |
|
85 | + */ |
|
86 | + public function callbackCompleted($oauthRequestToken, $oauthRequestSecret, $oauthVerifier) |
|
87 | + { |
|
88 | + $requestToken = new Token($oauthRequestToken, $oauthRequestSecret); |
|
89 | + |
|
90 | + // FIXME: domains! |
|
91 | + /** @var Domain $domain */ |
|
92 | + $domain = Domain::getById(1, $this->database); |
|
93 | + |
|
94 | + return $this->getClient($domain)->complete($requestToken, $oauthVerifier); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * @inheritDoc |
|
99 | + */ |
|
100 | + public function getIdentityTicket($oauthAccessToken, $oauthAccessSecret) |
|
101 | + { |
|
102 | + // FIXME: domains! |
|
103 | + /** @var Domain $domain */ |
|
104 | + $domain = Domain::getById(1, $this->database); |
|
105 | + |
|
106 | + return $this->getClient($domain)->identify(new Token($oauthAccessToken, $oauthAccessSecret)); |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * @inheritDoc |
|
111 | + */ |
|
112 | + public function apiCall($apiParams, $accessToken, $accessSecret, $method = 'GET') |
|
113 | + { |
|
114 | + $userToken = new Token($accessToken, $accessSecret); |
|
115 | + |
|
116 | + $apiParams['format'] = 'json'; |
|
117 | + |
|
118 | + if ($apiParams === null || !is_array($apiParams)) { |
|
119 | + throw new CurlException("Invalid API call"); |
|
120 | + } |
|
121 | + |
|
122 | + // FIXME: domains! |
|
123 | + /** @var Domain $domain */ |
|
124 | + $domain = Domain::getById(1, $this->database); |
|
125 | + |
|
126 | + $url = $domain->getWikiApiPath(); |
|
127 | + $isPost = ($method === 'POST'); |
|
128 | + |
|
129 | + if ($method === 'GET') { |
|
130 | + $query = http_build_query($apiParams); |
|
131 | + $url .= '?' . $query; |
|
132 | + $apiParams = null; |
|
133 | + } |
|
134 | + |
|
135 | + $data = $this->getClient($domain)->makeOAuthCall($userToken, $url, $isPost, $apiParams); |
|
136 | + |
|
137 | + return json_decode($data); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * @param string $oauthEndpoint |
|
142 | + * |
|
143 | + * @return Client |
|
144 | + */ |
|
145 | + protected function getClient(Domain $domain) : Client |
|
146 | + { |
|
147 | + $oauthClientConfig = new ClientConfig($domain->getWikiArticlePath() . "?title=Special:OAuth"); |
|
148 | + $oauthClientConfig->setConsumer(new Consumer($this->consumerKey, $this->consumerSecret)); |
|
149 | + $oauthClientConfig->setUserAgent($this->userAgent); |
|
150 | + return new Client($oauthClientConfig); |
|
151 | + } |
|
152 | 152 | } |
@@ -15,73 +15,73 @@ |
||
15 | 15 | |
16 | 16 | class RequestQueueHelper |
17 | 17 | { |
18 | - /** |
|
19 | - * @param RequestQueue $queue |
|
20 | - * @param bool $enabled |
|
21 | - * @param bool $default |
|
22 | - * @param bool $antiSpoof |
|
23 | - * @param bool $titleBlacklist |
|
24 | - */ |
|
25 | - public function configureDefaults( |
|
26 | - RequestQueue $queue, |
|
27 | - bool $enabled, |
|
28 | - bool $default, |
|
29 | - bool $antiSpoof, |
|
30 | - bool $titleBlacklist, |
|
31 | - bool $isTarget |
|
32 | - ) { |
|
33 | - // always allow enabling a queue |
|
34 | - if ($enabled) { |
|
35 | - $queue->setEnabled($enabled); |
|
36 | - } |
|
18 | + /** |
|
19 | + * @param RequestQueue $queue |
|
20 | + * @param bool $enabled |
|
21 | + * @param bool $default |
|
22 | + * @param bool $antiSpoof |
|
23 | + * @param bool $titleBlacklist |
|
24 | + */ |
|
25 | + public function configureDefaults( |
|
26 | + RequestQueue $queue, |
|
27 | + bool $enabled, |
|
28 | + bool $default, |
|
29 | + bool $antiSpoof, |
|
30 | + bool $titleBlacklist, |
|
31 | + bool $isTarget |
|
32 | + ) { |
|
33 | + // always allow enabling a queue |
|
34 | + if ($enabled) { |
|
35 | + $queue->setEnabled($enabled); |
|
36 | + } |
|
37 | 37 | |
38 | - // only allow other enable-flag changes if we're not a default |
|
39 | - if (!($queue->isDefault() || $queue->isDefaultAntispoof() || $queue->isDefaultTitleBlacklist() || $isTarget)) { |
|
40 | - $queue->setEnabled($enabled); |
|
41 | - } |
|
38 | + // only allow other enable-flag changes if we're not a default |
|
39 | + if (!($queue->isDefault() || $queue->isDefaultAntispoof() || $queue->isDefaultTitleBlacklist() || $isTarget)) { |
|
40 | + $queue->setEnabled($enabled); |
|
41 | + } |
|
42 | 42 | |
43 | - // only allow enabling the default flags, and only when we're enabled. |
|
44 | - $queue->setDefault(($queue->isDefault() || $default) && $queue->isEnabled()); |
|
45 | - $queue->setDefaultAntispoof(($queue->isDefaultAntispoof() || $antiSpoof) && $queue->isEnabled()); |
|
46 | - $queue->setDefaultTitleBlacklist(($queue->isDefaultTitleBlacklist() || $titleBlacklist) && $queue->isEnabled()); |
|
47 | - } |
|
43 | + // only allow enabling the default flags, and only when we're enabled. |
|
44 | + $queue->setDefault(($queue->isDefault() || $default) && $queue->isEnabled()); |
|
45 | + $queue->setDefaultAntispoof(($queue->isDefaultAntispoof() || $antiSpoof) && $queue->isEnabled()); |
|
46 | + $queue->setDefaultTitleBlacklist(($queue->isDefaultTitleBlacklist() || $titleBlacklist) && $queue->isEnabled()); |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Returns true if the specified queue is a target of an enabled email template with a defer action. |
|
51 | - * |
|
52 | - * @param RequestQueue $queue |
|
53 | - * @param PdoDatabase $database |
|
54 | - * |
|
55 | - * @return bool |
|
56 | - */ |
|
57 | - public function isEmailTemplateTarget(RequestQueue $queue, PdoDatabase $database): bool |
|
58 | - { |
|
59 | - $isTarget = false; |
|
60 | - /** @var EmailTemplate[] $deferralTemplates */ |
|
61 | - $deferralTemplates = EmailTemplate::getAllActiveTemplates(EmailTemplate::ACTION_DEFER, $database, $queue->getDomain()); |
|
62 | - foreach ($deferralTemplates as $t) { |
|
63 | - if ($t->getQueue() === $queue->getId()) { |
|
64 | - $isTarget = true; |
|
65 | - break; |
|
66 | - } |
|
67 | - } |
|
49 | + /** |
|
50 | + * Returns true if the specified queue is a target of an enabled email template with a defer action. |
|
51 | + * |
|
52 | + * @param RequestQueue $queue |
|
53 | + * @param PdoDatabase $database |
|
54 | + * |
|
55 | + * @return bool |
|
56 | + */ |
|
57 | + public function isEmailTemplateTarget(RequestQueue $queue, PdoDatabase $database): bool |
|
58 | + { |
|
59 | + $isTarget = false; |
|
60 | + /** @var EmailTemplate[] $deferralTemplates */ |
|
61 | + $deferralTemplates = EmailTemplate::getAllActiveTemplates(EmailTemplate::ACTION_DEFER, $database, $queue->getDomain()); |
|
62 | + foreach ($deferralTemplates as $t) { |
|
63 | + if ($t->getQueue() === $queue->getId()) { |
|
64 | + $isTarget = true; |
|
65 | + break; |
|
66 | + } |
|
67 | + } |
|
68 | 68 | |
69 | - return $isTarget; |
|
70 | - } |
|
69 | + return $isTarget; |
|
70 | + } |
|
71 | 71 | |
72 | - public function isRequestFormTarget(RequestQueue $queue, PdoDatabase $database): bool |
|
73 | - { |
|
74 | - $isTarget = false; |
|
75 | - $forms = RequestForm::getAllForms($database, 1); // FIXME: domains |
|
76 | - foreach ($forms as $t) { |
|
77 | - if ($t->isEnabled()) { |
|
78 | - if ($t->getOverrideQueue() === $queue->getId()) { |
|
79 | - $isTarget = true; |
|
80 | - break; |
|
81 | - } |
|
82 | - } |
|
83 | - } |
|
72 | + public function isRequestFormTarget(RequestQueue $queue, PdoDatabase $database): bool |
|
73 | + { |
|
74 | + $isTarget = false; |
|
75 | + $forms = RequestForm::getAllForms($database, 1); // FIXME: domains |
|
76 | + foreach ($forms as $t) { |
|
77 | + if ($t->isEnabled()) { |
|
78 | + if ($t->getOverrideQueue() === $queue->getId()) { |
|
79 | + $isTarget = true; |
|
80 | + break; |
|
81 | + } |
|
82 | + } |
|
83 | + } |
|
84 | 84 | |
85 | - return $isTarget; |
|
86 | - } |
|
85 | + return $isTarget; |
|
86 | + } |
|
87 | 87 | } |
88 | 88 | \ No newline at end of file |