@@ -22,411 +22,411 @@ |
||
22 | 22 | |
23 | 23 | class OAuthUserHelper implements IMediaWikiClient |
24 | 24 | { |
25 | - const TOKEN_REQUEST = 'request'; |
|
26 | - const TOKEN_ACCESS = 'access'; |
|
27 | - /** @var PDOStatement */ |
|
28 | - private static $tokenCountStatement = null; |
|
29 | - /** @var PDOStatement */ |
|
30 | - private $getTokenStatement; |
|
31 | - /** |
|
32 | - * @var User |
|
33 | - */ |
|
34 | - private $user; |
|
35 | - /** |
|
36 | - * @var PdoDatabase |
|
37 | - */ |
|
38 | - private $database; |
|
39 | - /** |
|
40 | - * @var IOAuthProtocolHelper |
|
41 | - */ |
|
42 | - private $oauthProtocolHelper; |
|
43 | - /** |
|
44 | - * @var bool|null Is the user linked to OAuth |
|
45 | - */ |
|
46 | - private $linked; |
|
47 | - private $partiallyLinked; |
|
48 | - /** @var OAuthToken */ |
|
49 | - private $accessToken; |
|
50 | - /** @var bool */ |
|
51 | - private $accessTokenLoaded = false; |
|
52 | - /** |
|
53 | - * @var OAuthIdentity |
|
54 | - */ |
|
55 | - private $identity = null; |
|
56 | - /** |
|
57 | - * @var bool |
|
58 | - */ |
|
59 | - private $identityLoaded = false; |
|
60 | - /** |
|
61 | - * @var SiteConfiguration |
|
62 | - */ |
|
63 | - private $siteConfiguration; |
|
64 | - |
|
65 | - #region Static methods |
|
66 | - public static function findUserByRequestToken($requestToken, PdoDatabase $database) |
|
67 | - { |
|
68 | - $statement = $database->prepare(<<<'SQL' |
|
25 | + const TOKEN_REQUEST = 'request'; |
|
26 | + const TOKEN_ACCESS = 'access'; |
|
27 | + /** @var PDOStatement */ |
|
28 | + private static $tokenCountStatement = null; |
|
29 | + /** @var PDOStatement */ |
|
30 | + private $getTokenStatement; |
|
31 | + /** |
|
32 | + * @var User |
|
33 | + */ |
|
34 | + private $user; |
|
35 | + /** |
|
36 | + * @var PdoDatabase |
|
37 | + */ |
|
38 | + private $database; |
|
39 | + /** |
|
40 | + * @var IOAuthProtocolHelper |
|
41 | + */ |
|
42 | + private $oauthProtocolHelper; |
|
43 | + /** |
|
44 | + * @var bool|null Is the user linked to OAuth |
|
45 | + */ |
|
46 | + private $linked; |
|
47 | + private $partiallyLinked; |
|
48 | + /** @var OAuthToken */ |
|
49 | + private $accessToken; |
|
50 | + /** @var bool */ |
|
51 | + private $accessTokenLoaded = false; |
|
52 | + /** |
|
53 | + * @var OAuthIdentity |
|
54 | + */ |
|
55 | + private $identity = null; |
|
56 | + /** |
|
57 | + * @var bool |
|
58 | + */ |
|
59 | + private $identityLoaded = false; |
|
60 | + /** |
|
61 | + * @var SiteConfiguration |
|
62 | + */ |
|
63 | + private $siteConfiguration; |
|
64 | + |
|
65 | + #region Static methods |
|
66 | + public static function findUserByRequestToken($requestToken, PdoDatabase $database) |
|
67 | + { |
|
68 | + $statement = $database->prepare(<<<'SQL' |
|
69 | 69 | SELECT u.* FROM user u |
70 | 70 | INNER JOIN oauthtoken t ON t.user = u.id |
71 | 71 | WHERE t.type = :type AND t.token = :token |
72 | 72 | SQL |
73 | - ); |
|
74 | - $statement->execute(array(':type' => self::TOKEN_REQUEST, ':token' => $requestToken)); |
|
75 | - |
|
76 | - /** @var User $user */ |
|
77 | - $user = $statement->fetchObject(User::class); |
|
78 | - $statement->closeCursor(); |
|
79 | - |
|
80 | - if ($user === false) { |
|
81 | - throw new ApplicationLogicException('Token not found in store, please try again'); |
|
82 | - } |
|
83 | - |
|
84 | - $user->setDatabase($database); |
|
85 | - |
|
86 | - return $user; |
|
87 | - } |
|
88 | - |
|
89 | - public static function userIsFullyLinked(User $user, PdoDatabase $database = null) |
|
90 | - { |
|
91 | - if (self::$tokenCountStatement === null && $database === null) { |
|
92 | - throw new ApplicationLogicException('Static link request without initialised statement'); |
|
93 | - } |
|
94 | - |
|
95 | - return self::runTokenCount($user->getId(), $database, self::TOKEN_ACCESS); |
|
96 | - } |
|
97 | - |
|
98 | - public static function userIsPartiallyLinked(User $user, PdoDatabase $database = null) |
|
99 | - { |
|
100 | - if (self::$tokenCountStatement === null && $database === null) { |
|
101 | - throw new ApplicationLogicException('Static link request without initialised statement'); |
|
102 | - } |
|
103 | - |
|
104 | - if (self::userIsFullyLinked($user, $database)) { |
|
105 | - return false; |
|
106 | - } |
|
107 | - |
|
108 | - return self::runTokenCount($user->getId(), $database, self::TOKEN_REQUEST) |
|
109 | - || $user->getOnWikiName() == null; |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @param PdoDatabase $database |
|
114 | - */ |
|
115 | - public static function prepareTokenCountStatement(PdoDatabase $database) |
|
116 | - { |
|
117 | - if (self::$tokenCountStatement === null) { |
|
118 | - self::$tokenCountStatement = $database->prepare('SELECT COUNT(*) FROM oauthtoken WHERE user = :user AND type = :type'); |
|
119 | - } |
|
120 | - } |
|
121 | - |
|
122 | - private static function runTokenCount($userId, $database, $tokenType) |
|
123 | - { |
|
124 | - if (self::$tokenCountStatement === null) { |
|
125 | - self::prepareTokenCountStatement($database); |
|
126 | - } |
|
127 | - |
|
128 | - self::$tokenCountStatement->execute(array( |
|
129 | - ':user' => $userId, |
|
130 | - ':type' => $tokenType, |
|
131 | - )); |
|
132 | - |
|
133 | - $tokenCount = self::$tokenCountStatement->fetchColumn(); |
|
134 | - $linked = $tokenCount > 0; |
|
135 | - self::$tokenCountStatement->closeCursor(); |
|
136 | - |
|
137 | - return $linked; |
|
138 | - } |
|
139 | - |
|
140 | - #endregion Static methods |
|
141 | - |
|
142 | - /** |
|
143 | - * OAuthUserHelper constructor. |
|
144 | - * |
|
145 | - * @param User $user |
|
146 | - * @param PdoDatabase $database |
|
147 | - * @param IOAuthProtocolHelper $oauthProtocolHelper |
|
148 | - * @param SiteConfiguration $siteConfiguration |
|
149 | - */ |
|
150 | - public function __construct( |
|
151 | - User $user, |
|
152 | - PdoDatabase $database, |
|
153 | - IOAuthProtocolHelper $oauthProtocolHelper, |
|
154 | - SiteConfiguration $siteConfiguration |
|
155 | - ) { |
|
156 | - $this->user = $user; |
|
157 | - $this->database = $database; |
|
158 | - $this->oauthProtocolHelper = $oauthProtocolHelper; |
|
159 | - |
|
160 | - $this->linked = null; |
|
161 | - $this->partiallyLinked = null; |
|
162 | - $this->siteConfiguration = $siteConfiguration; |
|
163 | - |
|
164 | - self::prepareTokenCountStatement($database); |
|
165 | - $this->getTokenStatement = $this->database->prepare('SELECT * FROM oauthtoken WHERE user = :user AND type = :type'); |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Determines if the user is fully connected to OAuth. |
|
170 | - * |
|
171 | - * @return bool |
|
172 | - */ |
|
173 | - public function isFullyLinked() |
|
174 | - { |
|
175 | - if ($this->linked === null) { |
|
176 | - $this->linked = self::userIsFullyLinked($this->user, $this->database); |
|
177 | - } |
|
178 | - |
|
179 | - return $this->linked; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * Attempts to figure out if a user is partially linked to OAuth, and therefore needs to complete the OAuth |
|
184 | - * procedure before configuring. |
|
185 | - * @return bool |
|
186 | - */ |
|
187 | - public function isPartiallyLinked() |
|
188 | - { |
|
189 | - if ($this->partiallyLinked === null) { |
|
190 | - $this->partiallyLinked = self::userIsPartiallyLinked($this->user, $this->database); |
|
191 | - } |
|
192 | - |
|
193 | - return $this->partiallyLinked; |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * @throws OAuthException |
|
198 | - */ |
|
199 | - public function refreshIdentity() |
|
200 | - { |
|
201 | - $this->loadIdentity(); |
|
202 | - |
|
203 | - if ($this->identity === null) { |
|
204 | - $this->identity = new OAuthIdentity(); |
|
205 | - $this->identity->setUserId($this->user->getId()); |
|
206 | - $this->identity->setDatabase($this->database); |
|
207 | - } |
|
208 | - |
|
209 | - $token = $this->loadAccessToken(); |
|
210 | - |
|
211 | - $rawTicket = $this->oauthProtocolHelper->getIdentityTicket($token->getToken(), $token->getSecret()); |
|
212 | - |
|
213 | - $this->identity->populate($rawTicket); |
|
214 | - |
|
215 | - if (!$this->identityIsValid()) { |
|
216 | - throw new OAuthException('Identity ticket is not valid!'); |
|
217 | - } |
|
218 | - |
|
219 | - $this->identity->save(); |
|
220 | - |
|
221 | - $this->user->setOnWikiName($this->identity->getUsername()); |
|
222 | - $this->user->save(); |
|
223 | - } |
|
224 | - |
|
225 | - public function getRequestToken() |
|
226 | - { |
|
227 | - $token = $this->oauthProtocolHelper->getRequestToken(); |
|
228 | - |
|
229 | - $this->partiallyLinked = true; |
|
230 | - $this->linked = false; |
|
231 | - |
|
232 | - $this->database |
|
233 | - ->prepare('DELETE FROM oauthtoken WHERE user = :user AND type = :type') |
|
234 | - ->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_REQUEST)); |
|
235 | - |
|
236 | - $this->database |
|
237 | - ->prepare('INSERT INTO oauthtoken (user, type, token, secret, expiry) VALUES (:user, :type, :token, :secret, DATE_ADD(NOW(), INTERVAL 1 DAY))') |
|
238 | - ->execute(array( |
|
239 | - ':user' => $this->user->getId(), |
|
240 | - ':type' => self::TOKEN_REQUEST, |
|
241 | - ':token' => $token->key, |
|
242 | - ':secret' => $token->secret, |
|
243 | - )); |
|
244 | - |
|
245 | - return $this->oauthProtocolHelper->getAuthoriseUrl($token->key); |
|
246 | - } |
|
247 | - |
|
248 | - public function completeHandshake($verificationToken) |
|
249 | - { |
|
250 | - $this->getTokenStatement->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_REQUEST)); |
|
251 | - |
|
252 | - /** @var OAuthToken $token */ |
|
253 | - $token = $this->getTokenStatement->fetchObject(OAuthToken::class); |
|
254 | - $this->getTokenStatement->closeCursor(); |
|
255 | - |
|
256 | - if ($token === false) { |
|
257 | - throw new ApplicationLogicException('Cannot find request token'); |
|
258 | - } |
|
259 | - |
|
260 | - $token->setDatabase($this->database); |
|
261 | - |
|
262 | - $accessToken = $this->oauthProtocolHelper->callbackCompleted($token->getToken(), $token->getSecret(), |
|
263 | - $verificationToken); |
|
264 | - |
|
265 | - $clearStatement = $this->database->prepare('DELETE FROM oauthtoken WHERE user = :u AND type = :t'); |
|
266 | - $clearStatement->execute(array(':u' => $this->user->getId(), ':t' => self::TOKEN_ACCESS)); |
|
267 | - |
|
268 | - $token->setToken($accessToken->key); |
|
269 | - $token->setSecret($accessToken->secret); |
|
270 | - $token->setType(self::TOKEN_ACCESS); |
|
271 | - $token->setExpiry(null); |
|
272 | - $token->save(); |
|
273 | - |
|
274 | - $this->partiallyLinked = false; |
|
275 | - $this->linked = true; |
|
276 | - |
|
277 | - $this->refreshIdentity(); |
|
278 | - } |
|
279 | - |
|
280 | - public function detach() |
|
281 | - { |
|
282 | - $this->loadIdentity(); |
|
283 | - |
|
284 | - $this->identity->delete(); |
|
285 | - $statement = $this->database->prepare('DELETE FROM oauthtoken WHERE user = :user'); |
|
286 | - $statement->execute(array(':user' => $this->user->getId())); |
|
287 | - |
|
288 | - $this->identity = null; |
|
289 | - $this->linked = false; |
|
290 | - $this->partiallyLinked = false; |
|
291 | - } |
|
292 | - |
|
293 | - /** |
|
294 | - * @param bool $expiredOk |
|
295 | - * |
|
296 | - * @return OAuthIdentity |
|
297 | - * @throws OAuthException |
|
298 | - */ |
|
299 | - public function getIdentity($expiredOk = false) |
|
300 | - { |
|
301 | - $this->loadIdentity(); |
|
302 | - |
|
303 | - if (!$this->identityIsValid($expiredOk)) { |
|
304 | - throw new OAuthException('Stored identity is not valid.'); |
|
305 | - } |
|
306 | - |
|
307 | - return $this->identity; |
|
308 | - } |
|
309 | - |
|
310 | - public function doApiCall($params, $method) |
|
311 | - { |
|
312 | - // Ensure we're logged in |
|
313 | - $params['assert'] = 'user'; |
|
314 | - |
|
315 | - $token = $this->loadAccessToken(); |
|
316 | - return $this->oauthProtocolHelper->apiCall($params, $token->getToken(), $token->getSecret(), $method); |
|
317 | - } |
|
318 | - |
|
319 | - /** |
|
320 | - * @param bool $expiredOk |
|
321 | - * |
|
322 | - * @return bool |
|
323 | - */ |
|
324 | - private function identityIsValid($expiredOk = false) |
|
325 | - { |
|
326 | - $this->loadIdentity(); |
|
327 | - |
|
328 | - if ($this->identity === null) { |
|
329 | - return false; |
|
330 | - } |
|
331 | - |
|
332 | - if ($this->identity->getIssuedAtTime() === false |
|
333 | - || $this->identity->getExpirationTime() === false |
|
334 | - || $this->identity->getAudience() === false |
|
335 | - || $this->identity->getIssuer() === false |
|
336 | - ) { |
|
337 | - // this isn't populated properly. |
|
338 | - return false; |
|
339 | - } |
|
340 | - |
|
341 | - $issue = DateTimeImmutable::createFromFormat("U", $this->identity->getIssuedAtTime()); |
|
342 | - $now = new DateTimeImmutable(); |
|
343 | - |
|
344 | - if ($issue > $now) { |
|
345 | - // wat. |
|
346 | - return false; |
|
347 | - } |
|
348 | - |
|
349 | - if ($this->identityExpired() && !$expiredOk) { |
|
350 | - // soz. |
|
351 | - return false; |
|
352 | - } |
|
353 | - |
|
354 | - if ($this->identity->getAudience() !== $this->siteConfiguration->getOAuthConsumerToken()) { |
|
355 | - // token not issued for us |
|
356 | - return false; |
|
357 | - } |
|
358 | - |
|
359 | - if ($this->identity->getIssuer() !== $this->siteConfiguration->getOauthMediaWikiCanonicalServer()) { |
|
360 | - // token not issued by the right person |
|
361 | - return false; |
|
362 | - } |
|
363 | - |
|
364 | - // can't find a reason to not trust it |
|
365 | - return true; |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * @return bool |
|
370 | - */ |
|
371 | - public function identityExpired() |
|
372 | - { |
|
373 | - // allowed max age |
|
374 | - $gracePeriod = $this->siteConfiguration->getOauthIdentityGraceTime(); |
|
375 | - |
|
376 | - $expiry = DateTimeImmutable::createFromFormat("U", $this->identity->getExpirationTime()); |
|
377 | - $graceExpiry = $expiry->modify($gracePeriod); |
|
378 | - $now = new DateTimeImmutable(); |
|
379 | - |
|
380 | - return $graceExpiry < $now; |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * Loads the OAuth identity from the database for the current user. |
|
385 | - */ |
|
386 | - private function loadIdentity() |
|
387 | - { |
|
388 | - if ($this->identityLoaded) { |
|
389 | - return; |
|
390 | - } |
|
391 | - |
|
392 | - $statement = $this->database->prepare('SELECT * FROM oauthidentity WHERE user = :user'); |
|
393 | - $statement->execute(array(':user' => $this->user->getId())); |
|
394 | - /** @var OAuthIdentity $obj */ |
|
395 | - $obj = $statement->fetchObject(OAuthIdentity::class); |
|
396 | - |
|
397 | - if ($obj === false) { |
|
398 | - // failed to load identity. |
|
399 | - $this->identityLoaded = true; |
|
400 | - $this->identity = null; |
|
401 | - |
|
402 | - return; |
|
403 | - } |
|
404 | - |
|
405 | - $obj->setDatabase($this->database); |
|
406 | - $this->identityLoaded = true; |
|
407 | - $this->identity = $obj; |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * @return OAuthToken |
|
412 | - * @throws OAuthException |
|
413 | - */ |
|
414 | - private function loadAccessToken() |
|
415 | - { |
|
416 | - if (!$this->accessTokenLoaded) { |
|
417 | - $this->getTokenStatement->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_ACCESS)); |
|
418 | - /** @var OAuthToken $token */ |
|
419 | - $token = $this->getTokenStatement->fetchObject(OAuthToken::class); |
|
420 | - $this->getTokenStatement->closeCursor(); |
|
421 | - |
|
422 | - if ($token === false) { |
|
423 | - throw new OAuthException('Access token not found!'); |
|
424 | - } |
|
425 | - |
|
426 | - $this->accessToken = $token; |
|
427 | - $this->accessTokenLoaded = true; |
|
428 | - } |
|
429 | - |
|
430 | - return $this->accessToken; |
|
431 | - } |
|
73 | + ); |
|
74 | + $statement->execute(array(':type' => self::TOKEN_REQUEST, ':token' => $requestToken)); |
|
75 | + |
|
76 | + /** @var User $user */ |
|
77 | + $user = $statement->fetchObject(User::class); |
|
78 | + $statement->closeCursor(); |
|
79 | + |
|
80 | + if ($user === false) { |
|
81 | + throw new ApplicationLogicException('Token not found in store, please try again'); |
|
82 | + } |
|
83 | + |
|
84 | + $user->setDatabase($database); |
|
85 | + |
|
86 | + return $user; |
|
87 | + } |
|
88 | + |
|
89 | + public static function userIsFullyLinked(User $user, PdoDatabase $database = null) |
|
90 | + { |
|
91 | + if (self::$tokenCountStatement === null && $database === null) { |
|
92 | + throw new ApplicationLogicException('Static link request without initialised statement'); |
|
93 | + } |
|
94 | + |
|
95 | + return self::runTokenCount($user->getId(), $database, self::TOKEN_ACCESS); |
|
96 | + } |
|
97 | + |
|
98 | + public static function userIsPartiallyLinked(User $user, PdoDatabase $database = null) |
|
99 | + { |
|
100 | + if (self::$tokenCountStatement === null && $database === null) { |
|
101 | + throw new ApplicationLogicException('Static link request without initialised statement'); |
|
102 | + } |
|
103 | + |
|
104 | + if (self::userIsFullyLinked($user, $database)) { |
|
105 | + return false; |
|
106 | + } |
|
107 | + |
|
108 | + return self::runTokenCount($user->getId(), $database, self::TOKEN_REQUEST) |
|
109 | + || $user->getOnWikiName() == null; |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @param PdoDatabase $database |
|
114 | + */ |
|
115 | + public static function prepareTokenCountStatement(PdoDatabase $database) |
|
116 | + { |
|
117 | + if (self::$tokenCountStatement === null) { |
|
118 | + self::$tokenCountStatement = $database->prepare('SELECT COUNT(*) FROM oauthtoken WHERE user = :user AND type = :type'); |
|
119 | + } |
|
120 | + } |
|
121 | + |
|
122 | + private static function runTokenCount($userId, $database, $tokenType) |
|
123 | + { |
|
124 | + if (self::$tokenCountStatement === null) { |
|
125 | + self::prepareTokenCountStatement($database); |
|
126 | + } |
|
127 | + |
|
128 | + self::$tokenCountStatement->execute(array( |
|
129 | + ':user' => $userId, |
|
130 | + ':type' => $tokenType, |
|
131 | + )); |
|
132 | + |
|
133 | + $tokenCount = self::$tokenCountStatement->fetchColumn(); |
|
134 | + $linked = $tokenCount > 0; |
|
135 | + self::$tokenCountStatement->closeCursor(); |
|
136 | + |
|
137 | + return $linked; |
|
138 | + } |
|
139 | + |
|
140 | + #endregion Static methods |
|
141 | + |
|
142 | + /** |
|
143 | + * OAuthUserHelper constructor. |
|
144 | + * |
|
145 | + * @param User $user |
|
146 | + * @param PdoDatabase $database |
|
147 | + * @param IOAuthProtocolHelper $oauthProtocolHelper |
|
148 | + * @param SiteConfiguration $siteConfiguration |
|
149 | + */ |
|
150 | + public function __construct( |
|
151 | + User $user, |
|
152 | + PdoDatabase $database, |
|
153 | + IOAuthProtocolHelper $oauthProtocolHelper, |
|
154 | + SiteConfiguration $siteConfiguration |
|
155 | + ) { |
|
156 | + $this->user = $user; |
|
157 | + $this->database = $database; |
|
158 | + $this->oauthProtocolHelper = $oauthProtocolHelper; |
|
159 | + |
|
160 | + $this->linked = null; |
|
161 | + $this->partiallyLinked = null; |
|
162 | + $this->siteConfiguration = $siteConfiguration; |
|
163 | + |
|
164 | + self::prepareTokenCountStatement($database); |
|
165 | + $this->getTokenStatement = $this->database->prepare('SELECT * FROM oauthtoken WHERE user = :user AND type = :type'); |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Determines if the user is fully connected to OAuth. |
|
170 | + * |
|
171 | + * @return bool |
|
172 | + */ |
|
173 | + public function isFullyLinked() |
|
174 | + { |
|
175 | + if ($this->linked === null) { |
|
176 | + $this->linked = self::userIsFullyLinked($this->user, $this->database); |
|
177 | + } |
|
178 | + |
|
179 | + return $this->linked; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * Attempts to figure out if a user is partially linked to OAuth, and therefore needs to complete the OAuth |
|
184 | + * procedure before configuring. |
|
185 | + * @return bool |
|
186 | + */ |
|
187 | + public function isPartiallyLinked() |
|
188 | + { |
|
189 | + if ($this->partiallyLinked === null) { |
|
190 | + $this->partiallyLinked = self::userIsPartiallyLinked($this->user, $this->database); |
|
191 | + } |
|
192 | + |
|
193 | + return $this->partiallyLinked; |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * @throws OAuthException |
|
198 | + */ |
|
199 | + public function refreshIdentity() |
|
200 | + { |
|
201 | + $this->loadIdentity(); |
|
202 | + |
|
203 | + if ($this->identity === null) { |
|
204 | + $this->identity = new OAuthIdentity(); |
|
205 | + $this->identity->setUserId($this->user->getId()); |
|
206 | + $this->identity->setDatabase($this->database); |
|
207 | + } |
|
208 | + |
|
209 | + $token = $this->loadAccessToken(); |
|
210 | + |
|
211 | + $rawTicket = $this->oauthProtocolHelper->getIdentityTicket($token->getToken(), $token->getSecret()); |
|
212 | + |
|
213 | + $this->identity->populate($rawTicket); |
|
214 | + |
|
215 | + if (!$this->identityIsValid()) { |
|
216 | + throw new OAuthException('Identity ticket is not valid!'); |
|
217 | + } |
|
218 | + |
|
219 | + $this->identity->save(); |
|
220 | + |
|
221 | + $this->user->setOnWikiName($this->identity->getUsername()); |
|
222 | + $this->user->save(); |
|
223 | + } |
|
224 | + |
|
225 | + public function getRequestToken() |
|
226 | + { |
|
227 | + $token = $this->oauthProtocolHelper->getRequestToken(); |
|
228 | + |
|
229 | + $this->partiallyLinked = true; |
|
230 | + $this->linked = false; |
|
231 | + |
|
232 | + $this->database |
|
233 | + ->prepare('DELETE FROM oauthtoken WHERE user = :user AND type = :type') |
|
234 | + ->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_REQUEST)); |
|
235 | + |
|
236 | + $this->database |
|
237 | + ->prepare('INSERT INTO oauthtoken (user, type, token, secret, expiry) VALUES (:user, :type, :token, :secret, DATE_ADD(NOW(), INTERVAL 1 DAY))') |
|
238 | + ->execute(array( |
|
239 | + ':user' => $this->user->getId(), |
|
240 | + ':type' => self::TOKEN_REQUEST, |
|
241 | + ':token' => $token->key, |
|
242 | + ':secret' => $token->secret, |
|
243 | + )); |
|
244 | + |
|
245 | + return $this->oauthProtocolHelper->getAuthoriseUrl($token->key); |
|
246 | + } |
|
247 | + |
|
248 | + public function completeHandshake($verificationToken) |
|
249 | + { |
|
250 | + $this->getTokenStatement->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_REQUEST)); |
|
251 | + |
|
252 | + /** @var OAuthToken $token */ |
|
253 | + $token = $this->getTokenStatement->fetchObject(OAuthToken::class); |
|
254 | + $this->getTokenStatement->closeCursor(); |
|
255 | + |
|
256 | + if ($token === false) { |
|
257 | + throw new ApplicationLogicException('Cannot find request token'); |
|
258 | + } |
|
259 | + |
|
260 | + $token->setDatabase($this->database); |
|
261 | + |
|
262 | + $accessToken = $this->oauthProtocolHelper->callbackCompleted($token->getToken(), $token->getSecret(), |
|
263 | + $verificationToken); |
|
264 | + |
|
265 | + $clearStatement = $this->database->prepare('DELETE FROM oauthtoken WHERE user = :u AND type = :t'); |
|
266 | + $clearStatement->execute(array(':u' => $this->user->getId(), ':t' => self::TOKEN_ACCESS)); |
|
267 | + |
|
268 | + $token->setToken($accessToken->key); |
|
269 | + $token->setSecret($accessToken->secret); |
|
270 | + $token->setType(self::TOKEN_ACCESS); |
|
271 | + $token->setExpiry(null); |
|
272 | + $token->save(); |
|
273 | + |
|
274 | + $this->partiallyLinked = false; |
|
275 | + $this->linked = true; |
|
276 | + |
|
277 | + $this->refreshIdentity(); |
|
278 | + } |
|
279 | + |
|
280 | + public function detach() |
|
281 | + { |
|
282 | + $this->loadIdentity(); |
|
283 | + |
|
284 | + $this->identity->delete(); |
|
285 | + $statement = $this->database->prepare('DELETE FROM oauthtoken WHERE user = :user'); |
|
286 | + $statement->execute(array(':user' => $this->user->getId())); |
|
287 | + |
|
288 | + $this->identity = null; |
|
289 | + $this->linked = false; |
|
290 | + $this->partiallyLinked = false; |
|
291 | + } |
|
292 | + |
|
293 | + /** |
|
294 | + * @param bool $expiredOk |
|
295 | + * |
|
296 | + * @return OAuthIdentity |
|
297 | + * @throws OAuthException |
|
298 | + */ |
|
299 | + public function getIdentity($expiredOk = false) |
|
300 | + { |
|
301 | + $this->loadIdentity(); |
|
302 | + |
|
303 | + if (!$this->identityIsValid($expiredOk)) { |
|
304 | + throw new OAuthException('Stored identity is not valid.'); |
|
305 | + } |
|
306 | + |
|
307 | + return $this->identity; |
|
308 | + } |
|
309 | + |
|
310 | + public function doApiCall($params, $method) |
|
311 | + { |
|
312 | + // Ensure we're logged in |
|
313 | + $params['assert'] = 'user'; |
|
314 | + |
|
315 | + $token = $this->loadAccessToken(); |
|
316 | + return $this->oauthProtocolHelper->apiCall($params, $token->getToken(), $token->getSecret(), $method); |
|
317 | + } |
|
318 | + |
|
319 | + /** |
|
320 | + * @param bool $expiredOk |
|
321 | + * |
|
322 | + * @return bool |
|
323 | + */ |
|
324 | + private function identityIsValid($expiredOk = false) |
|
325 | + { |
|
326 | + $this->loadIdentity(); |
|
327 | + |
|
328 | + if ($this->identity === null) { |
|
329 | + return false; |
|
330 | + } |
|
331 | + |
|
332 | + if ($this->identity->getIssuedAtTime() === false |
|
333 | + || $this->identity->getExpirationTime() === false |
|
334 | + || $this->identity->getAudience() === false |
|
335 | + || $this->identity->getIssuer() === false |
|
336 | + ) { |
|
337 | + // this isn't populated properly. |
|
338 | + return false; |
|
339 | + } |
|
340 | + |
|
341 | + $issue = DateTimeImmutable::createFromFormat("U", $this->identity->getIssuedAtTime()); |
|
342 | + $now = new DateTimeImmutable(); |
|
343 | + |
|
344 | + if ($issue > $now) { |
|
345 | + // wat. |
|
346 | + return false; |
|
347 | + } |
|
348 | + |
|
349 | + if ($this->identityExpired() && !$expiredOk) { |
|
350 | + // soz. |
|
351 | + return false; |
|
352 | + } |
|
353 | + |
|
354 | + if ($this->identity->getAudience() !== $this->siteConfiguration->getOAuthConsumerToken()) { |
|
355 | + // token not issued for us |
|
356 | + return false; |
|
357 | + } |
|
358 | + |
|
359 | + if ($this->identity->getIssuer() !== $this->siteConfiguration->getOauthMediaWikiCanonicalServer()) { |
|
360 | + // token not issued by the right person |
|
361 | + return false; |
|
362 | + } |
|
363 | + |
|
364 | + // can't find a reason to not trust it |
|
365 | + return true; |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * @return bool |
|
370 | + */ |
|
371 | + public function identityExpired() |
|
372 | + { |
|
373 | + // allowed max age |
|
374 | + $gracePeriod = $this->siteConfiguration->getOauthIdentityGraceTime(); |
|
375 | + |
|
376 | + $expiry = DateTimeImmutable::createFromFormat("U", $this->identity->getExpirationTime()); |
|
377 | + $graceExpiry = $expiry->modify($gracePeriod); |
|
378 | + $now = new DateTimeImmutable(); |
|
379 | + |
|
380 | + return $graceExpiry < $now; |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * Loads the OAuth identity from the database for the current user. |
|
385 | + */ |
|
386 | + private function loadIdentity() |
|
387 | + { |
|
388 | + if ($this->identityLoaded) { |
|
389 | + return; |
|
390 | + } |
|
391 | + |
|
392 | + $statement = $this->database->prepare('SELECT * FROM oauthidentity WHERE user = :user'); |
|
393 | + $statement->execute(array(':user' => $this->user->getId())); |
|
394 | + /** @var OAuthIdentity $obj */ |
|
395 | + $obj = $statement->fetchObject(OAuthIdentity::class); |
|
396 | + |
|
397 | + if ($obj === false) { |
|
398 | + // failed to load identity. |
|
399 | + $this->identityLoaded = true; |
|
400 | + $this->identity = null; |
|
401 | + |
|
402 | + return; |
|
403 | + } |
|
404 | + |
|
405 | + $obj->setDatabase($this->database); |
|
406 | + $this->identityLoaded = true; |
|
407 | + $this->identity = $obj; |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * @return OAuthToken |
|
412 | + * @throws OAuthException |
|
413 | + */ |
|
414 | + private function loadAccessToken() |
|
415 | + { |
|
416 | + if (!$this->accessTokenLoaded) { |
|
417 | + $this->getTokenStatement->execute(array(':user' => $this->user->getId(), ':type' => self::TOKEN_ACCESS)); |
|
418 | + /** @var OAuthToken $token */ |
|
419 | + $token = $this->getTokenStatement->fetchObject(OAuthToken::class); |
|
420 | + $this->getTokenStatement->closeCursor(); |
|
421 | + |
|
422 | + if ($token === false) { |
|
423 | + throw new OAuthException('Access token not found!'); |
|
424 | + } |
|
425 | + |
|
426 | + $this->accessToken = $token; |
|
427 | + $this->accessTokenLoaded = true; |
|
428 | + } |
|
429 | + |
|
430 | + return $this->accessToken; |
|
431 | + } |
|
432 | 432 | } |
433 | 433 | \ No newline at end of file |
@@ -10,5 +10,5 @@ |
||
10 | 10 | |
11 | 11 | interface IMediaWikiClient |
12 | 12 | { |
13 | - function doApiCall($params, $method); |
|
13 | + function doApiCall($params, $method); |
|
14 | 14 | } |
15 | 15 | \ No newline at end of file |
@@ -19,52 +19,52 @@ |
||
19 | 19 | |
20 | 20 | interface IOAuthProtocolHelper |
21 | 21 | { |
22 | - /** |
|
23 | - * @return stdClass |
|
24 | - * |
|
25 | - * @throws Exception |
|
26 | - * @throws CurlException |
|
27 | - */ |
|
28 | - public function getRequestToken(); |
|
22 | + /** |
|
23 | + * @return stdClass |
|
24 | + * |
|
25 | + * @throws Exception |
|
26 | + * @throws CurlException |
|
27 | + */ |
|
28 | + public function getRequestToken(); |
|
29 | 29 | |
30 | - /** |
|
31 | - * @param string $requestToken |
|
32 | - * |
|
33 | - * @return string |
|
34 | - */ |
|
35 | - public function getAuthoriseUrl($requestToken); |
|
30 | + /** |
|
31 | + * @param string $requestToken |
|
32 | + * |
|
33 | + * @return string |
|
34 | + */ |
|
35 | + public function getAuthoriseUrl($requestToken); |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param string $oauthRequestToken |
|
39 | - * @param string $oauthRequestSecret |
|
40 | - * @param string $oauthVerifier |
|
41 | - * |
|
42 | - * @return stdClass |
|
43 | - * @throws CurlException |
|
44 | - * @throws Exception |
|
45 | - */ |
|
46 | - public function callbackCompleted($oauthRequestToken, $oauthRequestSecret, $oauthVerifier); |
|
37 | + /** |
|
38 | + * @param string $oauthRequestToken |
|
39 | + * @param string $oauthRequestSecret |
|
40 | + * @param string $oauthVerifier |
|
41 | + * |
|
42 | + * @return stdClass |
|
43 | + * @throws CurlException |
|
44 | + * @throws Exception |
|
45 | + */ |
|
46 | + public function callbackCompleted($oauthRequestToken, $oauthRequestSecret, $oauthVerifier); |
|
47 | 47 | |
48 | - /** |
|
49 | - * @param string $oauthAccessToken |
|
50 | - * @param string $oauthAccessSecret |
|
51 | - * |
|
52 | - * @return stdClass |
|
53 | - * @throws CurlException |
|
54 | - * @throws Exception |
|
55 | - */ |
|
56 | - public function getIdentityTicket($oauthAccessToken, $oauthAccessSecret); |
|
48 | + /** |
|
49 | + * @param string $oauthAccessToken |
|
50 | + * @param string $oauthAccessSecret |
|
51 | + * |
|
52 | + * @return stdClass |
|
53 | + * @throws CurlException |
|
54 | + * @throws Exception |
|
55 | + */ |
|
56 | + public function getIdentityTicket($oauthAccessToken, $oauthAccessSecret); |
|
57 | 57 | |
58 | - /** |
|
59 | - * @param array $apiParams array of parameters to send to the API |
|
60 | - * @param string $accessToken user's access token |
|
61 | - * @param string $accessSecret user's secret |
|
62 | - * @param string $method HTTP method |
|
63 | - * |
|
64 | - * @return stdClass |
|
65 | - * @throws ApplicationLogicException |
|
66 | - * @throws CurlException |
|
67 | - * @throws Exception |
|
68 | - */ |
|
69 | - public function apiCall($apiParams, $accessToken, $accessSecret, $method = 'GET'); |
|
58 | + /** |
|
59 | + * @param array $apiParams array of parameters to send to the API |
|
60 | + * @param string $accessToken user's access token |
|
61 | + * @param string $accessSecret user's secret |
|
62 | + * @param string $method HTTP method |
|
63 | + * |
|
64 | + * @return stdClass |
|
65 | + * @throws ApplicationLogicException |
|
66 | + * @throws CurlException |
|
67 | + * @throws Exception |
|
68 | + */ |
|
69 | + public function apiCall($apiParams, $accessToken, $accessSecret, $method = 'GET'); |
|
70 | 70 | } |
71 | 71 | \ No newline at end of file |
@@ -12,109 +12,109 @@ |
||
12 | 12 | |
13 | 13 | class HttpHelper |
14 | 14 | { |
15 | - private $curlHandle; |
|
16 | - |
|
17 | - /** |
|
18 | - * HttpHelper constructor. |
|
19 | - * |
|
20 | - * @param string $userAgent |
|
21 | - * @param boolean $disableVerifyPeer |
|
22 | - * @param string $cookieJar |
|
23 | - */ |
|
24 | - public function __construct($userAgent, $disableVerifyPeer, $cookieJar = null) |
|
25 | - { |
|
26 | - $this->curlHandle = curl_init(); |
|
27 | - |
|
28 | - curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true); |
|
29 | - curl_setopt($this->curlHandle, CURLOPT_USERAGENT, $userAgent); |
|
30 | - curl_setopt($this->curlHandle, CURLOPT_FAILONERROR, true); |
|
31 | - |
|
32 | - if ($disableVerifyPeer) { |
|
33 | - curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false); |
|
34 | - } |
|
35 | - |
|
36 | - if($cookieJar !== null) { |
|
37 | - curl_setopt($this->curlHandle, CURLOPT_COOKIEFILE, $cookieJar); |
|
38 | - curl_setopt($this->curlHandle, CURLOPT_COOKIEJAR, $cookieJar); |
|
39 | - } |
|
40 | - } |
|
41 | - |
|
42 | - public function __destruct() |
|
43 | - { |
|
44 | - curl_close($this->curlHandle); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Fetches the content of a URL, with an optional parameter set. |
|
49 | - * |
|
50 | - * @param string $url The URL to fetch. |
|
51 | - * @param null|array $parameters Key/value pair of GET parameters to add to the request. |
|
52 | - * Null lets you handle it yourself. |
|
53 | - * |
|
54 | - * @param array $headers |
|
55 | - * |
|
56 | - * @return string |
|
57 | - * @throws CurlException |
|
58 | - */ |
|
59 | - public function get($url, $parameters = null, $headers = array()) |
|
60 | - { |
|
61 | - if ($parameters !== null && is_array($parameters)) { |
|
62 | - $getString = '?' . http_build_query($parameters); |
|
63 | - $url .= $getString; |
|
64 | - } |
|
65 | - |
|
66 | - curl_setopt($this->curlHandle, CURLOPT_URL, $url); |
|
67 | - |
|
68 | - // Make sure we're doing a GET |
|
69 | - curl_setopt($this->curlHandle, CURLOPT_POST, false); |
|
70 | - |
|
71 | - curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers); |
|
72 | - |
|
73 | - $result = curl_exec($this->curlHandle); |
|
74 | - |
|
75 | - if ($result === false) { |
|
76 | - $error = curl_error($this->curlHandle); |
|
77 | - throw new CurlException('Remote request failed with error ' . $error); |
|
78 | - } |
|
79 | - |
|
80 | - return $result; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Posts data to a URL |
|
85 | - * |
|
86 | - * @param string $url The URL to fetch. |
|
87 | - * @param array $parameters Key/value pair of POST parameters to add to the request. |
|
88 | - * @param array $headers |
|
89 | - * |
|
90 | - * @return string |
|
91 | - * @throws CurlException |
|
92 | - */ |
|
93 | - public function post($url, $parameters, $headers = array()) |
|
94 | - { |
|
95 | - curl_setopt($this->curlHandle, CURLOPT_URL, $url); |
|
96 | - |
|
97 | - // Make sure we're doing a POST |
|
98 | - curl_setopt($this->curlHandle, CURLOPT_POST, true); |
|
99 | - curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, http_build_query($parameters)); |
|
100 | - |
|
101 | - curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers); |
|
102 | - |
|
103 | - $result = curl_exec($this->curlHandle); |
|
104 | - |
|
105 | - if ($result === false) { |
|
106 | - $error = curl_error($this->curlHandle); |
|
107 | - throw new CurlException('Remote request failed with error ' . $error); |
|
108 | - } |
|
109 | - |
|
110 | - return $result; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - public function getError() |
|
117 | - { |
|
118 | - return curl_error($this->curlHandle); |
|
119 | - } |
|
15 | + private $curlHandle; |
|
16 | + |
|
17 | + /** |
|
18 | + * HttpHelper constructor. |
|
19 | + * |
|
20 | + * @param string $userAgent |
|
21 | + * @param boolean $disableVerifyPeer |
|
22 | + * @param string $cookieJar |
|
23 | + */ |
|
24 | + public function __construct($userAgent, $disableVerifyPeer, $cookieJar = null) |
|
25 | + { |
|
26 | + $this->curlHandle = curl_init(); |
|
27 | + |
|
28 | + curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true); |
|
29 | + curl_setopt($this->curlHandle, CURLOPT_USERAGENT, $userAgent); |
|
30 | + curl_setopt($this->curlHandle, CURLOPT_FAILONERROR, true); |
|
31 | + |
|
32 | + if ($disableVerifyPeer) { |
|
33 | + curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false); |
|
34 | + } |
|
35 | + |
|
36 | + if($cookieJar !== null) { |
|
37 | + curl_setopt($this->curlHandle, CURLOPT_COOKIEFILE, $cookieJar); |
|
38 | + curl_setopt($this->curlHandle, CURLOPT_COOKIEJAR, $cookieJar); |
|
39 | + } |
|
40 | + } |
|
41 | + |
|
42 | + public function __destruct() |
|
43 | + { |
|
44 | + curl_close($this->curlHandle); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Fetches the content of a URL, with an optional parameter set. |
|
49 | + * |
|
50 | + * @param string $url The URL to fetch. |
|
51 | + * @param null|array $parameters Key/value pair of GET parameters to add to the request. |
|
52 | + * Null lets you handle it yourself. |
|
53 | + * |
|
54 | + * @param array $headers |
|
55 | + * |
|
56 | + * @return string |
|
57 | + * @throws CurlException |
|
58 | + */ |
|
59 | + public function get($url, $parameters = null, $headers = array()) |
|
60 | + { |
|
61 | + if ($parameters !== null && is_array($parameters)) { |
|
62 | + $getString = '?' . http_build_query($parameters); |
|
63 | + $url .= $getString; |
|
64 | + } |
|
65 | + |
|
66 | + curl_setopt($this->curlHandle, CURLOPT_URL, $url); |
|
67 | + |
|
68 | + // Make sure we're doing a GET |
|
69 | + curl_setopt($this->curlHandle, CURLOPT_POST, false); |
|
70 | + |
|
71 | + curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers); |
|
72 | + |
|
73 | + $result = curl_exec($this->curlHandle); |
|
74 | + |
|
75 | + if ($result === false) { |
|
76 | + $error = curl_error($this->curlHandle); |
|
77 | + throw new CurlException('Remote request failed with error ' . $error); |
|
78 | + } |
|
79 | + |
|
80 | + return $result; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Posts data to a URL |
|
85 | + * |
|
86 | + * @param string $url The URL to fetch. |
|
87 | + * @param array $parameters Key/value pair of POST parameters to add to the request. |
|
88 | + * @param array $headers |
|
89 | + * |
|
90 | + * @return string |
|
91 | + * @throws CurlException |
|
92 | + */ |
|
93 | + public function post($url, $parameters, $headers = array()) |
|
94 | + { |
|
95 | + curl_setopt($this->curlHandle, CURLOPT_URL, $url); |
|
96 | + |
|
97 | + // Make sure we're doing a POST |
|
98 | + curl_setopt($this->curlHandle, CURLOPT_POST, true); |
|
99 | + curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, http_build_query($parameters)); |
|
100 | + |
|
101 | + curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers); |
|
102 | + |
|
103 | + $result = curl_exec($this->curlHandle); |
|
104 | + |
|
105 | + if ($result === false) { |
|
106 | + $error = curl_error($this->curlHandle); |
|
107 | + throw new CurlException('Remote request failed with error ' . $error); |
|
108 | + } |
|
109 | + |
|
110 | + return $result; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + public function getError() |
|
117 | + { |
|
118 | + return curl_error($this->curlHandle); |
|
119 | + } |
|
120 | 120 | } |
121 | 121 | \ No newline at end of file |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | curl_setopt($this->curlHandle, CURLOPT_SSL_VERIFYPEER, false); |
34 | 34 | } |
35 | 35 | |
36 | - if($cookieJar !== null) { |
|
36 | + if ($cookieJar !== null) { |
|
37 | 37 | curl_setopt($this->curlHandle, CURLOPT_COOKIEFILE, $cookieJar); |
38 | 38 | curl_setopt($this->curlHandle, CURLOPT_COOKIEJAR, $cookieJar); |
39 | 39 | } |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | public function get($url, $parameters = null, $headers = array()) |
60 | 60 | { |
61 | 61 | if ($parameters !== null && is_array($parameters)) { |
62 | - $getString = '?' . http_build_query($parameters); |
|
62 | + $getString = '?'.http_build_query($parameters); |
|
63 | 63 | $url .= $getString; |
64 | 64 | } |
65 | 65 | |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | |
75 | 75 | if ($result === false) { |
76 | 76 | $error = curl_error($this->curlHandle); |
77 | - throw new CurlException('Remote request failed with error ' . $error); |
|
77 | + throw new CurlException('Remote request failed with error '.$error); |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | return $result; |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | |
105 | 105 | if ($result === false) { |
106 | 106 | $error = curl_error($this->curlHandle); |
107 | - throw new CurlException('Remote request failed with error ' . $error); |
|
107 | + throw new CurlException('Remote request failed with error '.$error); |
|
108 | 108 | } |
109 | 109 | |
110 | 110 | return $result; |
@@ -241,7 +241,8 @@ |
||
241 | 241 | * @param string $username |
242 | 242 | * @return bool |
243 | 243 | */ |
244 | - public function checkAccountExists($username) { |
|
244 | + public function checkAccountExists($username) |
|
245 | + { |
|
245 | 246 | $parameters = array( |
246 | 247 | 'action' => 'query', |
247 | 248 | 'list' => 'users', |
@@ -15,245 +15,245 @@ |
||
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 | - $parameters = array( |
|
246 | - 'action' => 'query', |
|
247 | - 'list' => 'users', |
|
248 | - 'format' => 'php', |
|
249 | - 'ususers' => $username, |
|
250 | - ); |
|
251 | - |
|
252 | - $apiResult = $this->mediaWikiClient->doApiCall($parameters, 'GET'); |
|
253 | - |
|
254 | - $entry = $apiResult->query->users[0]; |
|
255 | - $exists = !isset($entry->missing); |
|
256 | - |
|
257 | - return $exists; |
|
258 | - } |
|
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 | + $parameters = array( |
|
246 | + 'action' => 'query', |
|
247 | + 'list' => 'users', |
|
248 | + 'format' => 'php', |
|
249 | + 'ususers' => $username, |
|
250 | + ); |
|
251 | + |
|
252 | + $apiResult = $this->mediaWikiClient->doApiCall($parameters, 'GET'); |
|
253 | + |
|
254 | + $entry = $apiResult->query->users[0]; |
|
255 | + $exists = !isset($entry->missing); |
|
256 | + |
|
257 | + return $exists; |
|
258 | + } |
|
259 | 259 | } |
@@ -58,12 +58,12 @@ discard block |
||
58 | 58 | $response = $this->mediaWikiClient->doApiCall($tokenParams, 'POST'); |
59 | 59 | |
60 | 60 | if (isset($response->error)) { |
61 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
61 | + throw new MediaWikiApiException($response->error->code.': '.$response->error->info); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | $token = $response->query->tokens->createaccounttoken; |
65 | 65 | |
66 | - $callback = $this->siteConfiguration->getBaseUrl() . '/internal.php/oauth/createCallback'; |
|
66 | + $callback = $this->siteConfiguration->getBaseUrl().'/internal.php/oauth/createCallback'; |
|
67 | 67 | |
68 | 68 | $checkboxFields = array(); |
69 | 69 | $requiredFields = array(); |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | $createResponse = $this->mediaWikiClient->doApiCall($createParams, 'POST'); |
87 | 87 | |
88 | 88 | if (isset($createResponse->error)) { |
89 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
89 | + throw new MediaWikiApiException($response->error->code.': '.$response->error->info); |
|
90 | 90 | } |
91 | 91 | |
92 | 92 | if (!isset($createResponse->createaccount) || !isset($createResponse->createaccount->status)) { |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | return; |
103 | 103 | } |
104 | 104 | |
105 | - throw new Exception('API result reported status of ' . $createResponse->createaccount->status); |
|
105 | + throw new Exception('API result reported status of '.$createResponse->createaccount->status); |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | /** |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | $response = $this->mediaWikiClient->doApiCall($tokenParams, 'POST'); |
127 | 127 | |
128 | 128 | if (isset($response->error)) { |
129 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
129 | + throw new MediaWikiApiException($response->error->code.': '.$response->error->info); |
|
130 | 130 | } |
131 | 131 | |
132 | 132 | $token = $response->query->tokens->csrftoken; |
@@ -137,7 +137,7 @@ discard block |
||
137 | 137 | |
138 | 138 | $editParameters = array( |
139 | 139 | 'action' => 'edit', |
140 | - 'title' => 'User talk:' . $username, |
|
140 | + 'title' => 'User talk:'.$username, |
|
141 | 141 | 'section' => 'new', |
142 | 142 | 'sectiontitle' => $title, |
143 | 143 | 'summary' => $summary, |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | |
154 | 154 | if (!isset($response->edit)) { |
155 | 155 | if (isset($response->error)) { |
156 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
156 | + throw new MediaWikiApiException($response->error->code.': '.$response->error->info); |
|
157 | 157 | } |
158 | 158 | |
159 | 159 | throw new MediaWikiApiException('Unknown error encountered during editing.'); |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | return; |
165 | 165 | } |
166 | 166 | |
167 | - throw new MediaWikiApiException('Edit status unsuccessful: ' . $editResponse->result); |
|
167 | + throw new MediaWikiApiException('Edit status unsuccessful: '.$editResponse->result); |
|
168 | 168 | } |
169 | 169 | |
170 | 170 | public function getCreationFieldData(&$requiredFields, &$checkboxFields) |
@@ -179,7 +179,7 @@ discard block |
||
179 | 179 | $response = $this->mediaWikiClient->doApiCall($params, 'GET'); |
180 | 180 | |
181 | 181 | if (isset($response->error)) { |
182 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
182 | + throw new MediaWikiApiException($response->error->code.': '.$response->error->info); |
|
183 | 183 | } |
184 | 184 | |
185 | 185 | $requests = $response->query->authmanagerinfo->requests; |
@@ -13,67 +13,67 @@ |
||
13 | 13 | |
14 | 14 | class JobQueueSearchHelper extends SearchHelperBase |
15 | 15 | { |
16 | - protected function __construct(PdoDatabase $database) |
|
17 | - { |
|
18 | - parent::__construct($database, 'jobqueue', JobQueue::class, null); |
|
19 | - } |
|
20 | - |
|
21 | - /** |
|
22 | - * @param PdoDatabase $database |
|
23 | - * |
|
24 | - * @return JobQueueSearchHelper |
|
25 | - */ |
|
26 | - public static function get(PdoDatabase $database) { |
|
27 | - $helper = new JobQueueSearchHelper($database); |
|
28 | - return $helper; |
|
29 | - } |
|
30 | - |
|
31 | - /** |
|
32 | - * @param string[] $statuses |
|
33 | - * |
|
34 | - * @return $this |
|
35 | - */ |
|
36 | - public function statusIn($statuses) { |
|
37 | - $this->inClause('status', $statuses); |
|
38 | - |
|
39 | - return $this; |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * @return $this |
|
44 | - */ |
|
45 | - public function notAcknowledged() { |
|
46 | - $this->whereClause .= ' AND (acknowledged IS NULL OR acknowledged = 0)'; |
|
47 | - |
|
48 | - return $this; |
|
49 | - } |
|
50 | - |
|
51 | - public function byTask($task) { |
|
52 | - $this->whereClause .= ' AND task = ?'; |
|
53 | - $this->parameterList[] = $task; |
|
54 | - |
|
55 | - return $this; |
|
56 | - } |
|
57 | - |
|
58 | - public function byUser($userId) { |
|
59 | - $this->whereClause .= ' AND user = ?'; |
|
60 | - $this->parameterList[] = $userId; |
|
61 | - |
|
62 | - return $this; |
|
63 | - } |
|
64 | - |
|
65 | - public function byStatus($status) { |
|
66 | - $this->whereClause .= ' AND status = ?'; |
|
67 | - $this->parameterList[] = $status; |
|
68 | - |
|
69 | - return $this; |
|
70 | - } |
|
71 | - |
|
72 | - public function byRequest($request) |
|
73 | - { |
|
74 | - $this->whereClause .= ' AND request = ?'; |
|
75 | - $this->parameterList[] = $request; |
|
76 | - |
|
77 | - return $this; |
|
78 | - } |
|
16 | + protected function __construct(PdoDatabase $database) |
|
17 | + { |
|
18 | + parent::__construct($database, 'jobqueue', JobQueue::class, null); |
|
19 | + } |
|
20 | + |
|
21 | + /** |
|
22 | + * @param PdoDatabase $database |
|
23 | + * |
|
24 | + * @return JobQueueSearchHelper |
|
25 | + */ |
|
26 | + public static function get(PdoDatabase $database) { |
|
27 | + $helper = new JobQueueSearchHelper($database); |
|
28 | + return $helper; |
|
29 | + } |
|
30 | + |
|
31 | + /** |
|
32 | + * @param string[] $statuses |
|
33 | + * |
|
34 | + * @return $this |
|
35 | + */ |
|
36 | + public function statusIn($statuses) { |
|
37 | + $this->inClause('status', $statuses); |
|
38 | + |
|
39 | + return $this; |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * @return $this |
|
44 | + */ |
|
45 | + public function notAcknowledged() { |
|
46 | + $this->whereClause .= ' AND (acknowledged IS NULL OR acknowledged = 0)'; |
|
47 | + |
|
48 | + return $this; |
|
49 | + } |
|
50 | + |
|
51 | + public function byTask($task) { |
|
52 | + $this->whereClause .= ' AND task = ?'; |
|
53 | + $this->parameterList[] = $task; |
|
54 | + |
|
55 | + return $this; |
|
56 | + } |
|
57 | + |
|
58 | + public function byUser($userId) { |
|
59 | + $this->whereClause .= ' AND user = ?'; |
|
60 | + $this->parameterList[] = $userId; |
|
61 | + |
|
62 | + return $this; |
|
63 | + } |
|
64 | + |
|
65 | + public function byStatus($status) { |
|
66 | + $this->whereClause .= ' AND status = ?'; |
|
67 | + $this->parameterList[] = $status; |
|
68 | + |
|
69 | + return $this; |
|
70 | + } |
|
71 | + |
|
72 | + public function byRequest($request) |
|
73 | + { |
|
74 | + $this->whereClause .= ' AND request = ?'; |
|
75 | + $this->parameterList[] = $request; |
|
76 | + |
|
77 | + return $this; |
|
78 | + } |
|
79 | 79 | } |
80 | 80 | \ No newline at end of file |
@@ -23,7 +23,8 @@ discard block |
||
23 | 23 | * |
24 | 24 | * @return JobQueueSearchHelper |
25 | 25 | */ |
26 | - public static function get(PdoDatabase $database) { |
|
26 | + public static function get(PdoDatabase $database) |
|
27 | + { |
|
27 | 28 | $helper = new JobQueueSearchHelper($database); |
28 | 29 | return $helper; |
29 | 30 | } |
@@ -33,7 +34,8 @@ discard block |
||
33 | 34 | * |
34 | 35 | * @return $this |
35 | 36 | */ |
36 | - public function statusIn($statuses) { |
|
37 | + public function statusIn($statuses) |
|
38 | + { |
|
37 | 39 | $this->inClause('status', $statuses); |
38 | 40 | |
39 | 41 | return $this; |
@@ -42,27 +44,31 @@ discard block |
||
42 | 44 | /** |
43 | 45 | * @return $this |
44 | 46 | */ |
45 | - public function notAcknowledged() { |
|
47 | + public function notAcknowledged() |
|
48 | + { |
|
46 | 49 | $this->whereClause .= ' AND (acknowledged IS NULL OR acknowledged = 0)'; |
47 | 50 | |
48 | 51 | return $this; |
49 | 52 | } |
50 | 53 | |
51 | - public function byTask($task) { |
|
54 | + public function byTask($task) |
|
55 | + { |
|
52 | 56 | $this->whereClause .= ' AND task = ?'; |
53 | 57 | $this->parameterList[] = $task; |
54 | 58 | |
55 | 59 | return $this; |
56 | 60 | } |
57 | 61 | |
58 | - public function byUser($userId) { |
|
62 | + public function byUser($userId) |
|
63 | + { |
|
59 | 64 | $this->whereClause .= ' AND user = ?'; |
60 | 65 | $this->parameterList[] = $userId; |
61 | 66 | |
62 | 67 | return $this; |
63 | 68 | } |
64 | 69 | |
65 | - public function byStatus($status) { |
|
70 | + public function byStatus($status) |
|
71 | + { |
|
66 | 72 | $this->whereClause .= ' AND status = ?'; |
67 | 73 | $this->parameterList[] = $status; |
68 | 74 |
@@ -16,274 +16,274 @@ |
||
16 | 16 | |
17 | 17 | abstract class SearchHelperBase |
18 | 18 | { |
19 | - /** @var PdoDatabase */ |
|
20 | - protected $database; |
|
21 | - /** @var array */ |
|
22 | - protected $parameterList = array(); |
|
23 | - /** @var null|int */ |
|
24 | - private $limit = null; |
|
25 | - /** @var null|int */ |
|
26 | - private $offset = null; |
|
27 | - private $orderBy = null; |
|
28 | - /** |
|
29 | - * @var string The where clause. |
|
30 | - * |
|
31 | - * (the 1=1 condition will be optimised out of the query by the query planner, and simplifies our code here). Note |
|
32 | - * that we use positional parameters instead of named parameters because we don't know many times different options |
|
33 | - * will be called (looking at excluding() here, but there's the option for others). |
|
34 | - */ |
|
35 | - protected $whereClause = ' WHERE 1 = 1'; |
|
36 | - /** @var string */ |
|
37 | - protected $table; |
|
38 | - protected $joinClause = ''; |
|
39 | - protected $groupByClause = ''; |
|
40 | - protected $modifiersClause = ''; |
|
41 | - private $targetClass; |
|
42 | - |
|
43 | - /** |
|
44 | - * SearchHelperBase constructor. |
|
45 | - * |
|
46 | - * @param PdoDatabase $database |
|
47 | - * @param string $table |
|
48 | - * @param $targetClass |
|
49 | - * @param null|string $order Order by clause, excluding ORDER BY. |
|
50 | - */ |
|
51 | - protected function __construct(PdoDatabase $database, $table, $targetClass, $order = null) |
|
52 | - { |
|
53 | - $this->database = $database; |
|
54 | - $this->table = $table; |
|
55 | - $this->orderBy = $order; |
|
56 | - $this->targetClass = $targetClass; |
|
57 | - } |
|
58 | - |
|
59 | - /** |
|
60 | - * Finalises the database query, and executes it, returning a set of objects. |
|
61 | - * |
|
62 | - * @return DataObject[] |
|
63 | - */ |
|
64 | - public function fetch() |
|
65 | - { |
|
66 | - $statement = $this->getData(); |
|
67 | - |
|
68 | - /** @var DataObject[] $returnedObjects */ |
|
69 | - $returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, $this->targetClass); |
|
70 | - foreach ($returnedObjects as $req) { |
|
71 | - $req->setDatabase($this->database); |
|
72 | - } |
|
73 | - |
|
74 | - return $returnedObjects; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * @param string $whereClauseSection |
|
79 | - * @param array $values |
|
80 | - * |
|
81 | - * @return array |
|
82 | - */ |
|
83 | - protected function fetchByParameter($whereClauseSection, $values) |
|
84 | - { |
|
85 | - $this->whereClause .= $whereClauseSection; |
|
86 | - |
|
87 | - $countQuery = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
88 | - $countQuery .= $this->joinClause . $this->whereClause; |
|
89 | - |
|
90 | - $query = $this->buildQuery(array('*')); |
|
91 | - $query .= $this->applyOrder(); |
|
92 | - |
|
93 | - // shuffle around hackily TODO: fix this abomination - T593 |
|
94 | - $localParameterList = $this->parameterList; |
|
95 | - $this->parameterList = array(); |
|
96 | - |
|
97 | - $query .= $this->applyLimit(); |
|
98 | - |
|
99 | - $limitParameters = $this->parameterList; |
|
100 | - |
|
101 | - $statement = $this->database->prepare($query); |
|
102 | - $countStatement = $this->database->prepare($countQuery); |
|
103 | - |
|
104 | - $result = array(); |
|
105 | - foreach ($values as $v) { |
|
106 | - // reset parameter list |
|
107 | - $params = $localParameterList; |
|
108 | - $params[] = $v; |
|
109 | - |
|
110 | - $countStatement->execute($params); |
|
111 | - |
|
112 | - // reapply the limit parameters |
|
113 | - $params = array_merge($params, $limitParameters); |
|
114 | - |
|
115 | - $statement->execute($params); |
|
116 | - |
|
117 | - /** @var DataObject[] $returnedObjects */ |
|
118 | - $returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, $this->targetClass); |
|
119 | - foreach ($returnedObjects as $req) { |
|
120 | - $req->setDatabase($this->database); |
|
121 | - } |
|
122 | - |
|
123 | - $result[$v] = array( |
|
124 | - 'count' => $countStatement->fetchColumn(0), |
|
125 | - 'data' => $returnedObjects, |
|
126 | - ); |
|
127 | - } |
|
128 | - |
|
129 | - return $result; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Finalises the database query, and executes it, returning only the requested column. |
|
134 | - * |
|
135 | - * @param string $column The required column |
|
136 | - * |
|
137 | - * @param bool $distinct |
|
138 | - * |
|
139 | - * @return array |
|
140 | - * @throws ApplicationLogicException |
|
141 | - */ |
|
142 | - public function fetchColumn($column, $distinct = false) |
|
143 | - { |
|
144 | - if ($distinct) { |
|
145 | - if ($this->groupByClause !== '') { |
|
146 | - throw new ApplicationLogicException('Cannot apply distinct to column fetch already using group by'); |
|
147 | - } |
|
148 | - |
|
149 | - $this->groupByClause = ' GROUP BY origin.' . $column; |
|
150 | - } |
|
151 | - |
|
152 | - $statement = $this->getData(array($column)); |
|
153 | - |
|
154 | - return $statement->fetchAll(PDO::FETCH_COLUMN); |
|
155 | - } |
|
156 | - |
|
157 | - public function fetchMap($column) |
|
158 | - { |
|
159 | - $statement = $this->getData(array('id', $column)); |
|
160 | - |
|
161 | - $data = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
162 | - $map = array(); |
|
163 | - |
|
164 | - foreach ($data as $row) { |
|
165 | - $map[$row['id']] = $row[$column]; |
|
166 | - } |
|
167 | - |
|
168 | - return $map; |
|
169 | - } |
|
170 | - |
|
171 | - /** |
|
172 | - * @param int $count Returns the record count of the result set |
|
173 | - * |
|
174 | - * @return $this |
|
175 | - */ |
|
176 | - public function getRecordCount(&$count) |
|
177 | - { |
|
178 | - $query = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
179 | - $query .= $this->joinClause . $this->whereClause; |
|
180 | - |
|
181 | - $statement = $this->database->prepare($query); |
|
182 | - $statement->execute($this->parameterList); |
|
183 | - |
|
184 | - $count = $statement->fetchColumn(0); |
|
185 | - $statement->closeCursor(); |
|
186 | - |
|
187 | - return $this; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Limits the results |
|
192 | - * |
|
193 | - * @param integer $limit |
|
194 | - * @param integer|null $offset |
|
195 | - * |
|
196 | - * @return $this |
|
197 | - * |
|
198 | - */ |
|
199 | - public function limit($limit, $offset = null) |
|
200 | - { |
|
201 | - $this->limit = $limit; |
|
202 | - $this->offset = $offset; |
|
203 | - |
|
204 | - return $this; |
|
205 | - } |
|
206 | - |
|
207 | - private function applyLimit() |
|
208 | - { |
|
209 | - $clause = ''; |
|
210 | - if ($this->limit !== null) { |
|
211 | - $clause = ' LIMIT ?'; |
|
212 | - $this->parameterList[] = $this->limit; |
|
213 | - |
|
214 | - if ($this->offset !== null) { |
|
215 | - $clause .= ' OFFSET ?'; |
|
216 | - $this->parameterList[] = $this->offset; |
|
217 | - } |
|
218 | - } |
|
219 | - |
|
220 | - return $clause; |
|
221 | - } |
|
222 | - |
|
223 | - private function applyOrder() |
|
224 | - { |
|
225 | - if ($this->orderBy !== null) { |
|
226 | - return ' ORDER BY ' . $this->orderBy; |
|
227 | - } |
|
228 | - |
|
229 | - return ''; |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * @param array $columns |
|
234 | - * |
|
235 | - * @return PDOStatement |
|
236 | - */ |
|
237 | - private function getData($columns = array('*')) |
|
238 | - { |
|
239 | - $query = $this->buildQuery($columns); |
|
240 | - $query .= $this->applyOrder(); |
|
241 | - $query .= $this->applyLimit(); |
|
242 | - |
|
243 | - $statement = $this->database->prepare($query); |
|
244 | - $statement->execute($this->parameterList); |
|
245 | - |
|
246 | - return $statement; |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * @param array $columns |
|
251 | - * |
|
252 | - * @return string |
|
253 | - */ |
|
254 | - protected function buildQuery($columns) |
|
255 | - { |
|
256 | - $colData = array(); |
|
257 | - foreach ($columns as $c) { |
|
258 | - $colData[] = 'origin.' . $c; |
|
259 | - } |
|
260 | - |
|
261 | - $query = "SELECT {$this->modifiersClause} /* SearchHelper */ " . implode(', ', $colData) . ' FROM ' . $this->table . ' origin '; |
|
262 | - $query .= $this->joinClause . $this->whereClause . $this->groupByClause; |
|
263 | - |
|
264 | - return $query; |
|
265 | - } |
|
266 | - |
|
267 | - public function inIds($idList) |
|
268 | - { |
|
269 | - $this->inClause('id', $idList); |
|
270 | - |
|
271 | - return $this; |
|
272 | - } |
|
273 | - |
|
274 | - protected function inClause($column, $values) |
|
275 | - { |
|
276 | - if (count($values) === 0) { |
|
277 | - return; |
|
278 | - } |
|
279 | - |
|
280 | - // Urgh. OK. You can't use IN() with parameters directly, so let's munge something together. |
|
281 | - $valueCount = count($values); |
|
282 | - |
|
283 | - // Firstly, let's create a string of question marks, which will do as positional parameters. |
|
284 | - $inSection = str_repeat('?,', $valueCount - 1) . '?'; |
|
285 | - |
|
286 | - $this->whereClause .= " AND {$column} IN ({$inSection})"; |
|
287 | - $this->parameterList = array_merge($this->parameterList, $values); |
|
288 | - } |
|
19 | + /** @var PdoDatabase */ |
|
20 | + protected $database; |
|
21 | + /** @var array */ |
|
22 | + protected $parameterList = array(); |
|
23 | + /** @var null|int */ |
|
24 | + private $limit = null; |
|
25 | + /** @var null|int */ |
|
26 | + private $offset = null; |
|
27 | + private $orderBy = null; |
|
28 | + /** |
|
29 | + * @var string The where clause. |
|
30 | + * |
|
31 | + * (the 1=1 condition will be optimised out of the query by the query planner, and simplifies our code here). Note |
|
32 | + * that we use positional parameters instead of named parameters because we don't know many times different options |
|
33 | + * will be called (looking at excluding() here, but there's the option for others). |
|
34 | + */ |
|
35 | + protected $whereClause = ' WHERE 1 = 1'; |
|
36 | + /** @var string */ |
|
37 | + protected $table; |
|
38 | + protected $joinClause = ''; |
|
39 | + protected $groupByClause = ''; |
|
40 | + protected $modifiersClause = ''; |
|
41 | + private $targetClass; |
|
42 | + |
|
43 | + /** |
|
44 | + * SearchHelperBase constructor. |
|
45 | + * |
|
46 | + * @param PdoDatabase $database |
|
47 | + * @param string $table |
|
48 | + * @param $targetClass |
|
49 | + * @param null|string $order Order by clause, excluding ORDER BY. |
|
50 | + */ |
|
51 | + protected function __construct(PdoDatabase $database, $table, $targetClass, $order = null) |
|
52 | + { |
|
53 | + $this->database = $database; |
|
54 | + $this->table = $table; |
|
55 | + $this->orderBy = $order; |
|
56 | + $this->targetClass = $targetClass; |
|
57 | + } |
|
58 | + |
|
59 | + /** |
|
60 | + * Finalises the database query, and executes it, returning a set of objects. |
|
61 | + * |
|
62 | + * @return DataObject[] |
|
63 | + */ |
|
64 | + public function fetch() |
|
65 | + { |
|
66 | + $statement = $this->getData(); |
|
67 | + |
|
68 | + /** @var DataObject[] $returnedObjects */ |
|
69 | + $returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, $this->targetClass); |
|
70 | + foreach ($returnedObjects as $req) { |
|
71 | + $req->setDatabase($this->database); |
|
72 | + } |
|
73 | + |
|
74 | + return $returnedObjects; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * @param string $whereClauseSection |
|
79 | + * @param array $values |
|
80 | + * |
|
81 | + * @return array |
|
82 | + */ |
|
83 | + protected function fetchByParameter($whereClauseSection, $values) |
|
84 | + { |
|
85 | + $this->whereClause .= $whereClauseSection; |
|
86 | + |
|
87 | + $countQuery = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
88 | + $countQuery .= $this->joinClause . $this->whereClause; |
|
89 | + |
|
90 | + $query = $this->buildQuery(array('*')); |
|
91 | + $query .= $this->applyOrder(); |
|
92 | + |
|
93 | + // shuffle around hackily TODO: fix this abomination - T593 |
|
94 | + $localParameterList = $this->parameterList; |
|
95 | + $this->parameterList = array(); |
|
96 | + |
|
97 | + $query .= $this->applyLimit(); |
|
98 | + |
|
99 | + $limitParameters = $this->parameterList; |
|
100 | + |
|
101 | + $statement = $this->database->prepare($query); |
|
102 | + $countStatement = $this->database->prepare($countQuery); |
|
103 | + |
|
104 | + $result = array(); |
|
105 | + foreach ($values as $v) { |
|
106 | + // reset parameter list |
|
107 | + $params = $localParameterList; |
|
108 | + $params[] = $v; |
|
109 | + |
|
110 | + $countStatement->execute($params); |
|
111 | + |
|
112 | + // reapply the limit parameters |
|
113 | + $params = array_merge($params, $limitParameters); |
|
114 | + |
|
115 | + $statement->execute($params); |
|
116 | + |
|
117 | + /** @var DataObject[] $returnedObjects */ |
|
118 | + $returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, $this->targetClass); |
|
119 | + foreach ($returnedObjects as $req) { |
|
120 | + $req->setDatabase($this->database); |
|
121 | + } |
|
122 | + |
|
123 | + $result[$v] = array( |
|
124 | + 'count' => $countStatement->fetchColumn(0), |
|
125 | + 'data' => $returnedObjects, |
|
126 | + ); |
|
127 | + } |
|
128 | + |
|
129 | + return $result; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Finalises the database query, and executes it, returning only the requested column. |
|
134 | + * |
|
135 | + * @param string $column The required column |
|
136 | + * |
|
137 | + * @param bool $distinct |
|
138 | + * |
|
139 | + * @return array |
|
140 | + * @throws ApplicationLogicException |
|
141 | + */ |
|
142 | + public function fetchColumn($column, $distinct = false) |
|
143 | + { |
|
144 | + if ($distinct) { |
|
145 | + if ($this->groupByClause !== '') { |
|
146 | + throw new ApplicationLogicException('Cannot apply distinct to column fetch already using group by'); |
|
147 | + } |
|
148 | + |
|
149 | + $this->groupByClause = ' GROUP BY origin.' . $column; |
|
150 | + } |
|
151 | + |
|
152 | + $statement = $this->getData(array($column)); |
|
153 | + |
|
154 | + return $statement->fetchAll(PDO::FETCH_COLUMN); |
|
155 | + } |
|
156 | + |
|
157 | + public function fetchMap($column) |
|
158 | + { |
|
159 | + $statement = $this->getData(array('id', $column)); |
|
160 | + |
|
161 | + $data = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
162 | + $map = array(); |
|
163 | + |
|
164 | + foreach ($data as $row) { |
|
165 | + $map[$row['id']] = $row[$column]; |
|
166 | + } |
|
167 | + |
|
168 | + return $map; |
|
169 | + } |
|
170 | + |
|
171 | + /** |
|
172 | + * @param int $count Returns the record count of the result set |
|
173 | + * |
|
174 | + * @return $this |
|
175 | + */ |
|
176 | + public function getRecordCount(&$count) |
|
177 | + { |
|
178 | + $query = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
179 | + $query .= $this->joinClause . $this->whereClause; |
|
180 | + |
|
181 | + $statement = $this->database->prepare($query); |
|
182 | + $statement->execute($this->parameterList); |
|
183 | + |
|
184 | + $count = $statement->fetchColumn(0); |
|
185 | + $statement->closeCursor(); |
|
186 | + |
|
187 | + return $this; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Limits the results |
|
192 | + * |
|
193 | + * @param integer $limit |
|
194 | + * @param integer|null $offset |
|
195 | + * |
|
196 | + * @return $this |
|
197 | + * |
|
198 | + */ |
|
199 | + public function limit($limit, $offset = null) |
|
200 | + { |
|
201 | + $this->limit = $limit; |
|
202 | + $this->offset = $offset; |
|
203 | + |
|
204 | + return $this; |
|
205 | + } |
|
206 | + |
|
207 | + private function applyLimit() |
|
208 | + { |
|
209 | + $clause = ''; |
|
210 | + if ($this->limit !== null) { |
|
211 | + $clause = ' LIMIT ?'; |
|
212 | + $this->parameterList[] = $this->limit; |
|
213 | + |
|
214 | + if ($this->offset !== null) { |
|
215 | + $clause .= ' OFFSET ?'; |
|
216 | + $this->parameterList[] = $this->offset; |
|
217 | + } |
|
218 | + } |
|
219 | + |
|
220 | + return $clause; |
|
221 | + } |
|
222 | + |
|
223 | + private function applyOrder() |
|
224 | + { |
|
225 | + if ($this->orderBy !== null) { |
|
226 | + return ' ORDER BY ' . $this->orderBy; |
|
227 | + } |
|
228 | + |
|
229 | + return ''; |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * @param array $columns |
|
234 | + * |
|
235 | + * @return PDOStatement |
|
236 | + */ |
|
237 | + private function getData($columns = array('*')) |
|
238 | + { |
|
239 | + $query = $this->buildQuery($columns); |
|
240 | + $query .= $this->applyOrder(); |
|
241 | + $query .= $this->applyLimit(); |
|
242 | + |
|
243 | + $statement = $this->database->prepare($query); |
|
244 | + $statement->execute($this->parameterList); |
|
245 | + |
|
246 | + return $statement; |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * @param array $columns |
|
251 | + * |
|
252 | + * @return string |
|
253 | + */ |
|
254 | + protected function buildQuery($columns) |
|
255 | + { |
|
256 | + $colData = array(); |
|
257 | + foreach ($columns as $c) { |
|
258 | + $colData[] = 'origin.' . $c; |
|
259 | + } |
|
260 | + |
|
261 | + $query = "SELECT {$this->modifiersClause} /* SearchHelper */ " . implode(', ', $colData) . ' FROM ' . $this->table . ' origin '; |
|
262 | + $query .= $this->joinClause . $this->whereClause . $this->groupByClause; |
|
263 | + |
|
264 | + return $query; |
|
265 | + } |
|
266 | + |
|
267 | + public function inIds($idList) |
|
268 | + { |
|
269 | + $this->inClause('id', $idList); |
|
270 | + |
|
271 | + return $this; |
|
272 | + } |
|
273 | + |
|
274 | + protected function inClause($column, $values) |
|
275 | + { |
|
276 | + if (count($values) === 0) { |
|
277 | + return; |
|
278 | + } |
|
279 | + |
|
280 | + // Urgh. OK. You can't use IN() with parameters directly, so let's munge something together. |
|
281 | + $valueCount = count($values); |
|
282 | + |
|
283 | + // Firstly, let's create a string of question marks, which will do as positional parameters. |
|
284 | + $inSection = str_repeat('?,', $valueCount - 1) . '?'; |
|
285 | + |
|
286 | + $this->whereClause .= " AND {$column} IN ({$inSection})"; |
|
287 | + $this->parameterList = array_merge($this->parameterList, $values); |
|
288 | + } |
|
289 | 289 | } |
@@ -84,8 +84,8 @@ discard block |
||
84 | 84 | { |
85 | 85 | $this->whereClause .= $whereClauseSection; |
86 | 86 | |
87 | - $countQuery = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
88 | - $countQuery .= $this->joinClause . $this->whereClause; |
|
87 | + $countQuery = 'SELECT /* SearchHelper */ COUNT(*) FROM '.$this->table.' origin '; |
|
88 | + $countQuery .= $this->joinClause.$this->whereClause; |
|
89 | 89 | |
90 | 90 | $query = $this->buildQuery(array('*')); |
91 | 91 | $query .= $this->applyOrder(); |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | throw new ApplicationLogicException('Cannot apply distinct to column fetch already using group by'); |
147 | 147 | } |
148 | 148 | |
149 | - $this->groupByClause = ' GROUP BY origin.' . $column; |
|
149 | + $this->groupByClause = ' GROUP BY origin.'.$column; |
|
150 | 150 | } |
151 | 151 | |
152 | 152 | $statement = $this->getData(array($column)); |
@@ -175,8 +175,8 @@ discard block |
||
175 | 175 | */ |
176 | 176 | public function getRecordCount(&$count) |
177 | 177 | { |
178 | - $query = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
179 | - $query .= $this->joinClause . $this->whereClause; |
|
178 | + $query = 'SELECT /* SearchHelper */ COUNT(*) FROM '.$this->table.' origin '; |
|
179 | + $query .= $this->joinClause.$this->whereClause; |
|
180 | 180 | |
181 | 181 | $statement = $this->database->prepare($query); |
182 | 182 | $statement->execute($this->parameterList); |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | private function applyOrder() |
224 | 224 | { |
225 | 225 | if ($this->orderBy !== null) { |
226 | - return ' ORDER BY ' . $this->orderBy; |
|
226 | + return ' ORDER BY '.$this->orderBy; |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | return ''; |
@@ -255,11 +255,11 @@ discard block |
||
255 | 255 | { |
256 | 256 | $colData = array(); |
257 | 257 | foreach ($columns as $c) { |
258 | - $colData[] = 'origin.' . $c; |
|
258 | + $colData[] = 'origin.'.$c; |
|
259 | 259 | } |
260 | 260 | |
261 | - $query = "SELECT {$this->modifiersClause} /* SearchHelper */ " . implode(', ', $colData) . ' FROM ' . $this->table . ' origin '; |
|
262 | - $query .= $this->joinClause . $this->whereClause . $this->groupByClause; |
|
261 | + $query = "SELECT {$this->modifiersClause} /* SearchHelper */ ".implode(', ', $colData).' FROM '.$this->table.' origin '; |
|
262 | + $query .= $this->joinClause.$this->whereClause.$this->groupByClause; |
|
263 | 263 | |
264 | 264 | return $query; |
265 | 265 | } |
@@ -281,7 +281,7 @@ discard block |
||
281 | 281 | $valueCount = count($values); |
282 | 282 | |
283 | 283 | // Firstly, let's create a string of question marks, which will do as positional parameters. |
284 | - $inSection = str_repeat('?,', $valueCount - 1) . '?'; |
|
284 | + $inSection = str_repeat('?,', $valueCount - 1).'?'; |
|
285 | 285 | |
286 | 286 | $this->whereClause .= " AND {$column} IN ({$inSection})"; |
287 | 287 | $this->parameterList = array_merge($this->parameterList, $values); |
@@ -15,178 +15,178 @@ |
||
15 | 15 | |
16 | 16 | class RequestSearchHelper extends SearchHelperBase |
17 | 17 | { |
18 | - /** |
|
19 | - * RequestSearchHelper constructor. |
|
20 | - * |
|
21 | - * @param PdoDatabase $database |
|
22 | - */ |
|
23 | - protected function __construct(PdoDatabase $database) |
|
24 | - { |
|
25 | - parent::__construct($database, 'request', Request::class); |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * Initiates a search for requests |
|
30 | - * |
|
31 | - * @param PdoDatabase $database |
|
32 | - * |
|
33 | - * @return RequestSearchHelper |
|
34 | - */ |
|
35 | - public static function get(PdoDatabase $database) |
|
36 | - { |
|
37 | - $helper = new RequestSearchHelper($database); |
|
38 | - |
|
39 | - return $helper; |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * Filters the results by IP address |
|
44 | - * |
|
45 | - * @param string $ipAddress |
|
46 | - * |
|
47 | - * @return $this |
|
48 | - */ |
|
49 | - public function byIp($ipAddress) |
|
50 | - { |
|
51 | - $this->whereClause .= ' AND (ip LIKE ? OR forwardedip LIKE ?)'; |
|
52 | - $this->parameterList[] = $ipAddress; |
|
53 | - $this->parameterList[] = '%' . trim($ipAddress, '%') . '%'; |
|
54 | - |
|
55 | - return $this; |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Filters the results by email address |
|
60 | - * |
|
61 | - * @param string $emailAddress |
|
62 | - * |
|
63 | - * @return $this |
|
64 | - */ |
|
65 | - public function byEmailAddress($emailAddress) |
|
66 | - { |
|
67 | - $this->whereClause .= ' AND email LIKE ?'; |
|
68 | - $this->parameterList[] = $emailAddress; |
|
69 | - |
|
70 | - return $this; |
|
71 | - } |
|
72 | - |
|
73 | - /** |
|
74 | - * Filters the results by name |
|
75 | - * |
|
76 | - * @param string $name |
|
77 | - * |
|
78 | - * @return $this |
|
79 | - */ |
|
80 | - public function byName($name) |
|
81 | - { |
|
82 | - $this->whereClause .= ' AND name LIKE ?'; |
|
83 | - $this->parameterList[] = $name; |
|
84 | - |
|
85 | - return $this; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Filters the requests to those with a defined status |
|
90 | - * |
|
91 | - * @param $status |
|
92 | - * |
|
93 | - * @return $this |
|
94 | - */ |
|
95 | - public function byStatus($status) |
|
96 | - { |
|
97 | - $this->whereClause .= ' AND status = ?'; |
|
98 | - $this->parameterList[] = $status; |
|
99 | - |
|
100 | - return $this; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Excludes a request from the results |
|
105 | - * |
|
106 | - * @param int $requestId |
|
107 | - * |
|
108 | - * @return $this |
|
109 | - */ |
|
110 | - public function excludingRequest($requestId) |
|
111 | - { |
|
112 | - $this->whereClause .= ' AND id <> ?'; |
|
113 | - $this->parameterList[] = $requestId; |
|
114 | - |
|
115 | - return $this; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Filters the results to only those with a confirmed email address |
|
120 | - * |
|
121 | - * @return $this |
|
122 | - */ |
|
123 | - public function withConfirmedEmail() |
|
124 | - { |
|
125 | - $this->whereClause .= ' AND emailconfirm = ?'; |
|
126 | - $this->parameterList[] = 'Confirmed'; |
|
127 | - |
|
128 | - return $this; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Filters the results to exclude purged data |
|
133 | - * |
|
134 | - * @param SiteConfiguration $configuration |
|
135 | - * |
|
136 | - * @return $this |
|
137 | - */ |
|
138 | - public function excludingPurgedData(SiteConfiguration $configuration) |
|
139 | - { |
|
140 | - $this->whereClause .= ' AND ip <> ? AND email <> ?'; |
|
141 | - $this->parameterList[] = $configuration->getDataClearIp(); |
|
142 | - $this->parameterList[] = $configuration->getDataClearEmail(); |
|
143 | - |
|
144 | - return $this; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Filters the requests to those without a defined status |
|
149 | - * |
|
150 | - * @param $status |
|
151 | - * |
|
152 | - * @return $this |
|
153 | - */ |
|
154 | - public function excludingStatus($status) |
|
155 | - { |
|
156 | - $this->whereClause .= ' AND status <> ?'; |
|
157 | - $this->parameterList[] = $status; |
|
158 | - |
|
159 | - return $this; |
|
160 | - } |
|
161 | - |
|
162 | - /** |
|
163 | - * Filters the requests to those which have failed an auto-creation |
|
164 | - * |
|
165 | - * @return $this |
|
166 | - */ |
|
167 | - public function isHospitalised() |
|
168 | - { |
|
169 | - $this->whereClause .= ' AND status = ?'; |
|
170 | - $this->parameterList[] = RequestStatus::HOSPITAL; |
|
171 | - |
|
172 | - return $this; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Filters the requests to those which have not failed an auto-creation |
|
177 | - * |
|
178 | - * @return $this |
|
179 | - */ |
|
180 | - public function notHospitalised() |
|
181 | - { |
|
182 | - $this->whereClause .= ' AND status <> ?'; |
|
183 | - $this->parameterList[] = RequestStatus::HOSPITAL; |
|
184 | - |
|
185 | - return $this; |
|
186 | - } |
|
187 | - |
|
188 | - public function fetchByStatus($statuses) |
|
189 | - { |
|
190 | - return $this->fetchByParameter(' AND status = ?', $statuses); |
|
191 | - } |
|
18 | + /** |
|
19 | + * RequestSearchHelper constructor. |
|
20 | + * |
|
21 | + * @param PdoDatabase $database |
|
22 | + */ |
|
23 | + protected function __construct(PdoDatabase $database) |
|
24 | + { |
|
25 | + parent::__construct($database, 'request', Request::class); |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * Initiates a search for requests |
|
30 | + * |
|
31 | + * @param PdoDatabase $database |
|
32 | + * |
|
33 | + * @return RequestSearchHelper |
|
34 | + */ |
|
35 | + public static function get(PdoDatabase $database) |
|
36 | + { |
|
37 | + $helper = new RequestSearchHelper($database); |
|
38 | + |
|
39 | + return $helper; |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * Filters the results by IP address |
|
44 | + * |
|
45 | + * @param string $ipAddress |
|
46 | + * |
|
47 | + * @return $this |
|
48 | + */ |
|
49 | + public function byIp($ipAddress) |
|
50 | + { |
|
51 | + $this->whereClause .= ' AND (ip LIKE ? OR forwardedip LIKE ?)'; |
|
52 | + $this->parameterList[] = $ipAddress; |
|
53 | + $this->parameterList[] = '%' . trim($ipAddress, '%') . '%'; |
|
54 | + |
|
55 | + return $this; |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Filters the results by email address |
|
60 | + * |
|
61 | + * @param string $emailAddress |
|
62 | + * |
|
63 | + * @return $this |
|
64 | + */ |
|
65 | + public function byEmailAddress($emailAddress) |
|
66 | + { |
|
67 | + $this->whereClause .= ' AND email LIKE ?'; |
|
68 | + $this->parameterList[] = $emailAddress; |
|
69 | + |
|
70 | + return $this; |
|
71 | + } |
|
72 | + |
|
73 | + /** |
|
74 | + * Filters the results by name |
|
75 | + * |
|
76 | + * @param string $name |
|
77 | + * |
|
78 | + * @return $this |
|
79 | + */ |
|
80 | + public function byName($name) |
|
81 | + { |
|
82 | + $this->whereClause .= ' AND name LIKE ?'; |
|
83 | + $this->parameterList[] = $name; |
|
84 | + |
|
85 | + return $this; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Filters the requests to those with a defined status |
|
90 | + * |
|
91 | + * @param $status |
|
92 | + * |
|
93 | + * @return $this |
|
94 | + */ |
|
95 | + public function byStatus($status) |
|
96 | + { |
|
97 | + $this->whereClause .= ' AND status = ?'; |
|
98 | + $this->parameterList[] = $status; |
|
99 | + |
|
100 | + return $this; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Excludes a request from the results |
|
105 | + * |
|
106 | + * @param int $requestId |
|
107 | + * |
|
108 | + * @return $this |
|
109 | + */ |
|
110 | + public function excludingRequest($requestId) |
|
111 | + { |
|
112 | + $this->whereClause .= ' AND id <> ?'; |
|
113 | + $this->parameterList[] = $requestId; |
|
114 | + |
|
115 | + return $this; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Filters the results to only those with a confirmed email address |
|
120 | + * |
|
121 | + * @return $this |
|
122 | + */ |
|
123 | + public function withConfirmedEmail() |
|
124 | + { |
|
125 | + $this->whereClause .= ' AND emailconfirm = ?'; |
|
126 | + $this->parameterList[] = 'Confirmed'; |
|
127 | + |
|
128 | + return $this; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Filters the results to exclude purged data |
|
133 | + * |
|
134 | + * @param SiteConfiguration $configuration |
|
135 | + * |
|
136 | + * @return $this |
|
137 | + */ |
|
138 | + public function excludingPurgedData(SiteConfiguration $configuration) |
|
139 | + { |
|
140 | + $this->whereClause .= ' AND ip <> ? AND email <> ?'; |
|
141 | + $this->parameterList[] = $configuration->getDataClearIp(); |
|
142 | + $this->parameterList[] = $configuration->getDataClearEmail(); |
|
143 | + |
|
144 | + return $this; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Filters the requests to those without a defined status |
|
149 | + * |
|
150 | + * @param $status |
|
151 | + * |
|
152 | + * @return $this |
|
153 | + */ |
|
154 | + public function excludingStatus($status) |
|
155 | + { |
|
156 | + $this->whereClause .= ' AND status <> ?'; |
|
157 | + $this->parameterList[] = $status; |
|
158 | + |
|
159 | + return $this; |
|
160 | + } |
|
161 | + |
|
162 | + /** |
|
163 | + * Filters the requests to those which have failed an auto-creation |
|
164 | + * |
|
165 | + * @return $this |
|
166 | + */ |
|
167 | + public function isHospitalised() |
|
168 | + { |
|
169 | + $this->whereClause .= ' AND status = ?'; |
|
170 | + $this->parameterList[] = RequestStatus::HOSPITAL; |
|
171 | + |
|
172 | + return $this; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Filters the requests to those which have not failed an auto-creation |
|
177 | + * |
|
178 | + * @return $this |
|
179 | + */ |
|
180 | + public function notHospitalised() |
|
181 | + { |
|
182 | + $this->whereClause .= ' AND status <> ?'; |
|
183 | + $this->parameterList[] = RequestStatus::HOSPITAL; |
|
184 | + |
|
185 | + return $this; |
|
186 | + } |
|
187 | + |
|
188 | + public function fetchByStatus($statuses) |
|
189 | + { |
|
190 | + return $this->fetchByParameter(' AND status = ?', $statuses); |
|
191 | + } |
|
192 | 192 | } |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | { |
51 | 51 | $this->whereClause .= ' AND (ip LIKE ? OR forwardedip LIKE ?)'; |
52 | 52 | $this->parameterList[] = $ipAddress; |
53 | - $this->parameterList[] = '%' . trim($ipAddress, '%') . '%'; |
|
53 | + $this->parameterList[] = '%'.trim($ipAddress, '%').'%'; |
|
54 | 54 | |
55 | 55 | return $this; |
56 | 56 | } |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | public function isHospitalised() |
168 | 168 | { |
169 | 169 | $this->whereClause .= ' AND status = ?'; |
170 | - $this->parameterList[] = RequestStatus::HOSPITAL; |
|
170 | + $this->parameterList[] = RequestStatus::HOSPITAL; |
|
171 | 171 | |
172 | 172 | return $this; |
173 | 173 | } |
@@ -180,7 +180,7 @@ discard block |
||
180 | 180 | public function notHospitalised() |
181 | 181 | { |
182 | 182 | $this->whereClause .= ' AND status <> ?'; |
183 | - $this->parameterList[] = RequestStatus::HOSPITAL; |
|
183 | + $this->parameterList[] = RequestStatus::HOSPITAL; |
|
184 | 184 | |
185 | 185 | return $this; |
186 | 186 | } |
@@ -27,413 +27,413 @@ |
||
27 | 27 | |
28 | 28 | class LogHelper |
29 | 29 | { |
30 | - /** |
|
31 | - * Summary of getRequestLogsWithComments |
|
32 | - * |
|
33 | - * @param int $requestId |
|
34 | - * @param PdoDatabase $db |
|
35 | - * @param SecurityManager $securityManager |
|
36 | - * |
|
37 | - * @return \Waca\DataObject[] |
|
38 | - */ |
|
39 | - public static function getRequestLogsWithComments($requestId, PdoDatabase $db, SecurityManager $securityManager) |
|
40 | - { |
|
41 | - $logs = LogSearchHelper::get($db)->byObjectType('Request')->byObjectId($requestId)->fetch(); |
|
42 | - |
|
43 | - $currentUser = User::getCurrent($db); |
|
44 | - $securityResult = $securityManager->allows('RequestData', 'seeRestrictedComments', $currentUser); |
|
45 | - $showAllComments = $securityResult === SecurityManager::ALLOWED; |
|
46 | - |
|
47 | - $comments = Comment::getForRequest($requestId, $db, $showAllComments, $currentUser->getId()); |
|
48 | - |
|
49 | - $items = array_merge($logs, $comments); |
|
50 | - |
|
51 | - /** |
|
52 | - * @param DataObject $item |
|
53 | - * |
|
54 | - * @return int |
|
55 | - */ |
|
56 | - $sortKey = function(DataObject $item) { |
|
57 | - if ($item instanceof Log) { |
|
58 | - return $item->getTimestamp()->getTimestamp(); |
|
59 | - } |
|
60 | - |
|
61 | - if ($item instanceof Comment) { |
|
62 | - return $item->getTime()->getTimestamp(); |
|
63 | - } |
|
64 | - |
|
65 | - return 0; |
|
66 | - }; |
|
67 | - |
|
68 | - do { |
|
69 | - $flag = false; |
|
70 | - |
|
71 | - $loopLimit = (count($items) - 1); |
|
72 | - for ($i = 0; $i < $loopLimit; $i++) { |
|
73 | - // are these two items out of order? |
|
74 | - if ($sortKey($items[$i]) > $sortKey($items[$i + 1])) { |
|
75 | - // swap them |
|
76 | - $swap = $items[$i]; |
|
77 | - $items[$i] = $items[$i + 1]; |
|
78 | - $items[$i + 1] = $swap; |
|
79 | - |
|
80 | - // set a flag to say we've modified the array this time around |
|
81 | - $flag = true; |
|
82 | - } |
|
83 | - } |
|
84 | - } |
|
85 | - while ($flag); |
|
86 | - |
|
87 | - return $items; |
|
88 | - } |
|
89 | - |
|
90 | - /** |
|
91 | - * Summary of getLogDescription |
|
92 | - * |
|
93 | - * @param Log $entry |
|
94 | - * |
|
95 | - * @return string |
|
96 | - */ |
|
97 | - public static function getLogDescription(Log $entry) |
|
98 | - { |
|
99 | - $text = "Deferred to "; |
|
100 | - if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
101 | - // Deferred to a different queue |
|
102 | - // This is exactly what we want to display. |
|
103 | - return $entry->getAction(); |
|
104 | - } |
|
105 | - |
|
106 | - $text = "Closed custom-n"; |
|
107 | - if ($entry->getAction() == $text) { |
|
108 | - // Custom-closed |
|
109 | - return "closed (custom reason - account not created)"; |
|
110 | - } |
|
111 | - |
|
112 | - $text = "Closed custom-y"; |
|
113 | - if ($entry->getAction() == $text) { |
|
114 | - // Custom-closed |
|
115 | - return "closed (custom reason - account created)"; |
|
116 | - } |
|
117 | - |
|
118 | - $text = "Closed 0"; |
|
119 | - if ($entry->getAction() == $text) { |
|
120 | - // Dropped the request - short-circuit the lookup |
|
121 | - return "dropped request"; |
|
122 | - } |
|
123 | - |
|
124 | - $text = "Closed "; |
|
125 | - if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
126 | - // Closed with a reason - do a lookup here. |
|
127 | - $id = substr($entry->getAction(), strlen($text)); |
|
128 | - /** @var EmailTemplate $template */ |
|
129 | - $template = EmailTemplate::getById((int)$id, $entry->getDatabase()); |
|
130 | - |
|
131 | - if ($template != false) { |
|
132 | - return "closed (" . $template->getName() . ")"; |
|
133 | - } |
|
134 | - } |
|
135 | - |
|
136 | - // Fall back to the basic stuff |
|
137 | - $lookup = array( |
|
138 | - 'Reserved' => 'reserved', |
|
139 | - 'Email Confirmed' => 'email-confirmed', |
|
140 | - 'Unreserved' => 'unreserved', |
|
141 | - 'Approved' => 'approved', |
|
142 | - 'Suspended' => 'suspended', |
|
143 | - 'RoleChange' => 'changed roles', |
|
144 | - 'Banned' => 'banned', |
|
145 | - 'Edited' => 'edited interface message', |
|
146 | - 'Declined' => 'declined', |
|
147 | - 'EditComment-c' => 'edited a comment', |
|
148 | - 'EditComment-r' => 'edited a comment', |
|
149 | - 'Unbanned' => 'unbanned', |
|
150 | - 'Promoted' => 'promoted to tool admin', |
|
151 | - 'BreakReserve' => 'forcibly broke the reservation', |
|
152 | - 'Prefchange' => 'changed user preferences', |
|
153 | - 'Renamed' => 'renamed', |
|
154 | - 'Demoted' => 'demoted from tool admin', |
|
155 | - 'ReceiveReserved' => 'received the reservation', |
|
156 | - 'SendReserved' => 'sent the reservation', |
|
157 | - 'EditedEmail' => 'edited email', |
|
158 | - 'DeletedTemplate' => 'deleted template', |
|
159 | - 'EditedTemplate' => 'edited template', |
|
160 | - 'CreatedEmail' => 'created email', |
|
161 | - 'CreatedTemplate' => 'created template', |
|
162 | - 'SentMail' => 'sent an email to the requestor', |
|
163 | - 'Registered' => 'registered a tool account', |
|
164 | - 'JobIssue' => 'ran a background job unsuccessfully', |
|
165 | - 'JobCompleted' => 'completed a background job', |
|
166 | - 'JobAcknowledged' => 'acknowledged a job failure', |
|
167 | - 'JobRequeued' => 'requeued a job for re-execution', |
|
168 | - 'EnqueuedJobQueue' => 'scheduled for creation', |
|
169 | - 'Hospitalised' => 'sent to the hospital', |
|
170 | - ); |
|
171 | - |
|
172 | - if (array_key_exists($entry->getAction(), $lookup)) { |
|
173 | - return $lookup[$entry->getAction()]; |
|
174 | - } |
|
175 | - |
|
176 | - // OK, I don't know what this is. Fall back to something sane. |
|
177 | - return "performed an unknown action ({$entry->getAction()})"; |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @param PdoDatabase $database |
|
182 | - * |
|
183 | - * @return array |
|
184 | - */ |
|
185 | - public static function getLogActions(PdoDatabase $database) |
|
186 | - { |
|
187 | - $lookup = array( |
|
188 | - 'Reserved' => 'reserved', |
|
189 | - 'Email Confirmed' => 'email-confirmed', |
|
190 | - 'Unreserved' => 'unreserved', |
|
191 | - 'Approved' => 'approved', |
|
192 | - 'Suspended' => 'suspended', |
|
193 | - 'RoleChange' => 'changed roles', |
|
194 | - 'Banned' => 'banned', |
|
195 | - 'Edited' => 'edited interface message', |
|
196 | - 'Declined' => 'declined', |
|
197 | - 'EditComment-c' => 'edited a comment (by comment ID)', |
|
198 | - 'EditComment-r' => 'edited a comment (by request)', |
|
199 | - 'Unbanned' => 'unbanned', |
|
200 | - 'Promoted' => 'promoted to tool admin', |
|
201 | - 'BreakReserve' => 'forcibly broke the reservation', |
|
202 | - 'Prefchange' => 'changed user preferences', |
|
203 | - 'Renamed' => 'renamed', |
|
204 | - 'Demoted' => 'demoted from tool admin', |
|
205 | - 'ReceiveReserved' => 'received the reservation', |
|
206 | - 'SendReserved' => 'sent the reservation', |
|
207 | - 'EditedEmail' => 'edited email', |
|
208 | - 'DeletedTemplate' => 'deleted template', |
|
209 | - 'EditedTemplate' => 'edited template', |
|
210 | - 'CreatedEmail' => 'created email', |
|
211 | - 'CreatedTemplate' => 'created template', |
|
212 | - 'SentMail' => 'sent an email to the requestor', |
|
213 | - 'Registered' => 'registered a tool account', |
|
214 | - 'Closed 0' => 'dropped request', |
|
215 | - 'JobIssue' => 'ran a background job unsuccessfully', |
|
216 | - 'JobCompleted' => 'completed a background job', |
|
217 | - 'JobAcknowledged' => 'acknowledged a job failure', |
|
218 | - 'JobRequeued' => 'requeued a job for re-execution', |
|
219 | - 'EnqueuedJobQueue' => 'scheduled for creation', |
|
220 | - 'Hospitalised' => 'sent to the hospital', |
|
221 | - ); |
|
222 | - |
|
223 | - $statement = $database->query(<<<SQL |
|
30 | + /** |
|
31 | + * Summary of getRequestLogsWithComments |
|
32 | + * |
|
33 | + * @param int $requestId |
|
34 | + * @param PdoDatabase $db |
|
35 | + * @param SecurityManager $securityManager |
|
36 | + * |
|
37 | + * @return \Waca\DataObject[] |
|
38 | + */ |
|
39 | + public static function getRequestLogsWithComments($requestId, PdoDatabase $db, SecurityManager $securityManager) |
|
40 | + { |
|
41 | + $logs = LogSearchHelper::get($db)->byObjectType('Request')->byObjectId($requestId)->fetch(); |
|
42 | + |
|
43 | + $currentUser = User::getCurrent($db); |
|
44 | + $securityResult = $securityManager->allows('RequestData', 'seeRestrictedComments', $currentUser); |
|
45 | + $showAllComments = $securityResult === SecurityManager::ALLOWED; |
|
46 | + |
|
47 | + $comments = Comment::getForRequest($requestId, $db, $showAllComments, $currentUser->getId()); |
|
48 | + |
|
49 | + $items = array_merge($logs, $comments); |
|
50 | + |
|
51 | + /** |
|
52 | + * @param DataObject $item |
|
53 | + * |
|
54 | + * @return int |
|
55 | + */ |
|
56 | + $sortKey = function(DataObject $item) { |
|
57 | + if ($item instanceof Log) { |
|
58 | + return $item->getTimestamp()->getTimestamp(); |
|
59 | + } |
|
60 | + |
|
61 | + if ($item instanceof Comment) { |
|
62 | + return $item->getTime()->getTimestamp(); |
|
63 | + } |
|
64 | + |
|
65 | + return 0; |
|
66 | + }; |
|
67 | + |
|
68 | + do { |
|
69 | + $flag = false; |
|
70 | + |
|
71 | + $loopLimit = (count($items) - 1); |
|
72 | + for ($i = 0; $i < $loopLimit; $i++) { |
|
73 | + // are these two items out of order? |
|
74 | + if ($sortKey($items[$i]) > $sortKey($items[$i + 1])) { |
|
75 | + // swap them |
|
76 | + $swap = $items[$i]; |
|
77 | + $items[$i] = $items[$i + 1]; |
|
78 | + $items[$i + 1] = $swap; |
|
79 | + |
|
80 | + // set a flag to say we've modified the array this time around |
|
81 | + $flag = true; |
|
82 | + } |
|
83 | + } |
|
84 | + } |
|
85 | + while ($flag); |
|
86 | + |
|
87 | + return $items; |
|
88 | + } |
|
89 | + |
|
90 | + /** |
|
91 | + * Summary of getLogDescription |
|
92 | + * |
|
93 | + * @param Log $entry |
|
94 | + * |
|
95 | + * @return string |
|
96 | + */ |
|
97 | + public static function getLogDescription(Log $entry) |
|
98 | + { |
|
99 | + $text = "Deferred to "; |
|
100 | + if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
101 | + // Deferred to a different queue |
|
102 | + // This is exactly what we want to display. |
|
103 | + return $entry->getAction(); |
|
104 | + } |
|
105 | + |
|
106 | + $text = "Closed custom-n"; |
|
107 | + if ($entry->getAction() == $text) { |
|
108 | + // Custom-closed |
|
109 | + return "closed (custom reason - account not created)"; |
|
110 | + } |
|
111 | + |
|
112 | + $text = "Closed custom-y"; |
|
113 | + if ($entry->getAction() == $text) { |
|
114 | + // Custom-closed |
|
115 | + return "closed (custom reason - account created)"; |
|
116 | + } |
|
117 | + |
|
118 | + $text = "Closed 0"; |
|
119 | + if ($entry->getAction() == $text) { |
|
120 | + // Dropped the request - short-circuit the lookup |
|
121 | + return "dropped request"; |
|
122 | + } |
|
123 | + |
|
124 | + $text = "Closed "; |
|
125 | + if (substr($entry->getAction(), 0, strlen($text)) == $text) { |
|
126 | + // Closed with a reason - do a lookup here. |
|
127 | + $id = substr($entry->getAction(), strlen($text)); |
|
128 | + /** @var EmailTemplate $template */ |
|
129 | + $template = EmailTemplate::getById((int)$id, $entry->getDatabase()); |
|
130 | + |
|
131 | + if ($template != false) { |
|
132 | + return "closed (" . $template->getName() . ")"; |
|
133 | + } |
|
134 | + } |
|
135 | + |
|
136 | + // Fall back to the basic stuff |
|
137 | + $lookup = array( |
|
138 | + 'Reserved' => 'reserved', |
|
139 | + 'Email Confirmed' => 'email-confirmed', |
|
140 | + 'Unreserved' => 'unreserved', |
|
141 | + 'Approved' => 'approved', |
|
142 | + 'Suspended' => 'suspended', |
|
143 | + 'RoleChange' => 'changed roles', |
|
144 | + 'Banned' => 'banned', |
|
145 | + 'Edited' => 'edited interface message', |
|
146 | + 'Declined' => 'declined', |
|
147 | + 'EditComment-c' => 'edited a comment', |
|
148 | + 'EditComment-r' => 'edited a comment', |
|
149 | + 'Unbanned' => 'unbanned', |
|
150 | + 'Promoted' => 'promoted to tool admin', |
|
151 | + 'BreakReserve' => 'forcibly broke the reservation', |
|
152 | + 'Prefchange' => 'changed user preferences', |
|
153 | + 'Renamed' => 'renamed', |
|
154 | + 'Demoted' => 'demoted from tool admin', |
|
155 | + 'ReceiveReserved' => 'received the reservation', |
|
156 | + 'SendReserved' => 'sent the reservation', |
|
157 | + 'EditedEmail' => 'edited email', |
|
158 | + 'DeletedTemplate' => 'deleted template', |
|
159 | + 'EditedTemplate' => 'edited template', |
|
160 | + 'CreatedEmail' => 'created email', |
|
161 | + 'CreatedTemplate' => 'created template', |
|
162 | + 'SentMail' => 'sent an email to the requestor', |
|
163 | + 'Registered' => 'registered a tool account', |
|
164 | + 'JobIssue' => 'ran a background job unsuccessfully', |
|
165 | + 'JobCompleted' => 'completed a background job', |
|
166 | + 'JobAcknowledged' => 'acknowledged a job failure', |
|
167 | + 'JobRequeued' => 'requeued a job for re-execution', |
|
168 | + 'EnqueuedJobQueue' => 'scheduled for creation', |
|
169 | + 'Hospitalised' => 'sent to the hospital', |
|
170 | + ); |
|
171 | + |
|
172 | + if (array_key_exists($entry->getAction(), $lookup)) { |
|
173 | + return $lookup[$entry->getAction()]; |
|
174 | + } |
|
175 | + |
|
176 | + // OK, I don't know what this is. Fall back to something sane. |
|
177 | + return "performed an unknown action ({$entry->getAction()})"; |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @param PdoDatabase $database |
|
182 | + * |
|
183 | + * @return array |
|
184 | + */ |
|
185 | + public static function getLogActions(PdoDatabase $database) |
|
186 | + { |
|
187 | + $lookup = array( |
|
188 | + 'Reserved' => 'reserved', |
|
189 | + 'Email Confirmed' => 'email-confirmed', |
|
190 | + 'Unreserved' => 'unreserved', |
|
191 | + 'Approved' => 'approved', |
|
192 | + 'Suspended' => 'suspended', |
|
193 | + 'RoleChange' => 'changed roles', |
|
194 | + 'Banned' => 'banned', |
|
195 | + 'Edited' => 'edited interface message', |
|
196 | + 'Declined' => 'declined', |
|
197 | + 'EditComment-c' => 'edited a comment (by comment ID)', |
|
198 | + 'EditComment-r' => 'edited a comment (by request)', |
|
199 | + 'Unbanned' => 'unbanned', |
|
200 | + 'Promoted' => 'promoted to tool admin', |
|
201 | + 'BreakReserve' => 'forcibly broke the reservation', |
|
202 | + 'Prefchange' => 'changed user preferences', |
|
203 | + 'Renamed' => 'renamed', |
|
204 | + 'Demoted' => 'demoted from tool admin', |
|
205 | + 'ReceiveReserved' => 'received the reservation', |
|
206 | + 'SendReserved' => 'sent the reservation', |
|
207 | + 'EditedEmail' => 'edited email', |
|
208 | + 'DeletedTemplate' => 'deleted template', |
|
209 | + 'EditedTemplate' => 'edited template', |
|
210 | + 'CreatedEmail' => 'created email', |
|
211 | + 'CreatedTemplate' => 'created template', |
|
212 | + 'SentMail' => 'sent an email to the requestor', |
|
213 | + 'Registered' => 'registered a tool account', |
|
214 | + 'Closed 0' => 'dropped request', |
|
215 | + 'JobIssue' => 'ran a background job unsuccessfully', |
|
216 | + 'JobCompleted' => 'completed a background job', |
|
217 | + 'JobAcknowledged' => 'acknowledged a job failure', |
|
218 | + 'JobRequeued' => 'requeued a job for re-execution', |
|
219 | + 'EnqueuedJobQueue' => 'scheduled for creation', |
|
220 | + 'Hospitalised' => 'sent to the hospital', |
|
221 | + ); |
|
222 | + |
|
223 | + $statement = $database->query(<<<SQL |
|
224 | 224 | SELECT CONCAT('Closed ', id) AS k, CONCAT('closed (',name,')') AS v |
225 | 225 | FROM emailtemplate; |
226 | 226 | SQL |
227 | - ); |
|
228 | - foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
229 | - $lookup[$row['k']] = $row['v']; |
|
230 | - } |
|
231 | - |
|
232 | - return $lookup; |
|
233 | - } |
|
234 | - |
|
235 | - public static function getObjectTypes() |
|
236 | - { |
|
237 | - return array( |
|
238 | - 'Ban' => 'Ban', |
|
239 | - 'Comment' => 'Comment', |
|
240 | - 'EmailTemplate' => 'Email template', |
|
241 | - 'JobQueue' => 'Job queue item', |
|
242 | - 'Request' => 'Request', |
|
243 | - 'SiteNotice' => 'Site notice', |
|
244 | - 'User' => 'User', |
|
245 | - 'WelcomeTemplate' => 'Welcome template', |
|
246 | - ); |
|
247 | - } |
|
248 | - |
|
249 | - /** |
|
250 | - * This returns a HTML |
|
251 | - * |
|
252 | - * @param string $objectId |
|
253 | - * @param string $objectType |
|
254 | - * @param PdoDatabase $database |
|
255 | - * @param SiteConfiguration $configuration |
|
256 | - * |
|
257 | - * @return null|string |
|
258 | - * @category Security-Critical |
|
259 | - */ |
|
260 | - private static function getObjectDescription( |
|
261 | - $objectId, |
|
262 | - $objectType, |
|
263 | - PdoDatabase $database, |
|
264 | - SiteConfiguration $configuration |
|
265 | - ) { |
|
266 | - if ($objectType == '') { |
|
267 | - return null; |
|
268 | - } |
|
269 | - |
|
270 | - $baseurl = $configuration->getBaseUrl(); |
|
271 | - |
|
272 | - switch ($objectType) { |
|
273 | - case 'Ban': |
|
274 | - /** @var Ban $ban */ |
|
275 | - $ban = Ban::getById($objectId, $database); |
|
276 | - |
|
277 | - if ($ban === false) { |
|
278 | - return 'Ban #' . $objectId . "</a>"; |
|
279 | - } |
|
280 | - |
|
281 | - return 'Ban #' . $objectId . " (" . htmlentities($ban->getTarget()) . ")</a>"; |
|
282 | - case 'EmailTemplate': |
|
283 | - /** @var EmailTemplate $emailTemplate */ |
|
284 | - $emailTemplate = EmailTemplate::getById($objectId, $database); |
|
285 | - $name = htmlentities($emailTemplate->getName(), ENT_COMPAT, 'UTF-8'); |
|
286 | - |
|
287 | - return <<<HTML |
|
227 | + ); |
|
228 | + foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
229 | + $lookup[$row['k']] = $row['v']; |
|
230 | + } |
|
231 | + |
|
232 | + return $lookup; |
|
233 | + } |
|
234 | + |
|
235 | + public static function getObjectTypes() |
|
236 | + { |
|
237 | + return array( |
|
238 | + 'Ban' => 'Ban', |
|
239 | + 'Comment' => 'Comment', |
|
240 | + 'EmailTemplate' => 'Email template', |
|
241 | + 'JobQueue' => 'Job queue item', |
|
242 | + 'Request' => 'Request', |
|
243 | + 'SiteNotice' => 'Site notice', |
|
244 | + 'User' => 'User', |
|
245 | + 'WelcomeTemplate' => 'Welcome template', |
|
246 | + ); |
|
247 | + } |
|
248 | + |
|
249 | + /** |
|
250 | + * This returns a HTML |
|
251 | + * |
|
252 | + * @param string $objectId |
|
253 | + * @param string $objectType |
|
254 | + * @param PdoDatabase $database |
|
255 | + * @param SiteConfiguration $configuration |
|
256 | + * |
|
257 | + * @return null|string |
|
258 | + * @category Security-Critical |
|
259 | + */ |
|
260 | + private static function getObjectDescription( |
|
261 | + $objectId, |
|
262 | + $objectType, |
|
263 | + PdoDatabase $database, |
|
264 | + SiteConfiguration $configuration |
|
265 | + ) { |
|
266 | + if ($objectType == '') { |
|
267 | + return null; |
|
268 | + } |
|
269 | + |
|
270 | + $baseurl = $configuration->getBaseUrl(); |
|
271 | + |
|
272 | + switch ($objectType) { |
|
273 | + case 'Ban': |
|
274 | + /** @var Ban $ban */ |
|
275 | + $ban = Ban::getById($objectId, $database); |
|
276 | + |
|
277 | + if ($ban === false) { |
|
278 | + return 'Ban #' . $objectId . "</a>"; |
|
279 | + } |
|
280 | + |
|
281 | + return 'Ban #' . $objectId . " (" . htmlentities($ban->getTarget()) . ")</a>"; |
|
282 | + case 'EmailTemplate': |
|
283 | + /** @var EmailTemplate $emailTemplate */ |
|
284 | + $emailTemplate = EmailTemplate::getById($objectId, $database); |
|
285 | + $name = htmlentities($emailTemplate->getName(), ENT_COMPAT, 'UTF-8'); |
|
286 | + |
|
287 | + return <<<HTML |
|
288 | 288 | <a href="{$baseurl}/internal.php/emailManagement/view?id={$objectId}">Email Template #{$objectId} ({$name})</a> |
289 | 289 | HTML; |
290 | - case 'SiteNotice': |
|
291 | - return "<a href=\"{$baseurl}/internal.php/siteNotice\">the site notice</a>"; |
|
292 | - case 'Request': |
|
293 | - /** @var Request $request */ |
|
294 | - $request = Request::getById($objectId, $database); |
|
295 | - $name = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
|
296 | - |
|
297 | - return <<<HTML |
|
290 | + case 'SiteNotice': |
|
291 | + return "<a href=\"{$baseurl}/internal.php/siteNotice\">the site notice</a>"; |
|
292 | + case 'Request': |
|
293 | + /** @var Request $request */ |
|
294 | + $request = Request::getById($objectId, $database); |
|
295 | + $name = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
|
296 | + |
|
297 | + return <<<HTML |
|
298 | 298 | <a href="{$baseurl}/internal.php/viewRequest?id={$objectId}">Request #{$objectId} ({$name})</a> |
299 | 299 | HTML; |
300 | - case 'User': |
|
301 | - /** @var User $user */ |
|
302 | - $user = User::getById($objectId, $database); |
|
303 | - $username = htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8'); |
|
304 | - |
|
305 | - return "<a href=\"{$baseurl}/internal.php/statistics/users/detail?user={$objectId}\">{$username}</a>"; |
|
306 | - case 'WelcomeTemplate': |
|
307 | - /** @var WelcomeTemplate $welcomeTemplate */ |
|
308 | - $welcomeTemplate = WelcomeTemplate::getById($objectId, $database); |
|
309 | - |
|
310 | - // some old templates have been completely deleted and lost to the depths of time. |
|
311 | - if ($welcomeTemplate === false) { |
|
312 | - return "Welcome template #{$objectId}"; |
|
313 | - } |
|
314 | - else { |
|
315 | - $userCode = htmlentities($welcomeTemplate->getUserCode(), ENT_COMPAT, 'UTF-8'); |
|
316 | - |
|
317 | - return "<a href=\"{$baseurl}/internal.php/welcomeTemplates/view?template={$objectId}\">{$userCode}</a>"; |
|
318 | - } |
|
319 | - case 'JobQueue': |
|
320 | - /** @var JobQueue $job */ |
|
321 | - $job = JobQueue::getById($objectId, $database); |
|
322 | - |
|
323 | - $taskDescriptions = JobQueue::getTaskDescriptions(); |
|
324 | - |
|
325 | - $task = $job->getTask(); |
|
326 | - if(isset($taskDescriptions[$task])){ |
|
327 | - $description = $taskDescriptions[$task]; |
|
328 | - } else { |
|
329 | - $description = 'Unknown task'; |
|
330 | - } |
|
331 | - |
|
332 | - return "<a href=\"{$baseurl}/internal.php/jobQueue/view?id={$objectId}\">Job #{$job->getId()} ({$description})</a>"; |
|
333 | - default: |
|
334 | - return '[' . $objectType . " " . $objectId . ']'; |
|
335 | - } |
|
336 | - } |
|
337 | - |
|
338 | - /** |
|
339 | - * @param Log[] $logs |
|
340 | - * @param PdoDatabase $database |
|
341 | - * @param SiteConfiguration $configuration |
|
342 | - * |
|
343 | - * @return array |
|
344 | - * @throws Exception |
|
345 | - */ |
|
346 | - public static function prepareLogsForTemplate($logs, PdoDatabase $database, SiteConfiguration $configuration) |
|
347 | - { |
|
348 | - $userIds = array(); |
|
349 | - |
|
350 | - /** @var Log $logEntry */ |
|
351 | - foreach ($logs as $logEntry) { |
|
352 | - if (!$logEntry instanceof Log) { |
|
353 | - // if this happens, we've done something wrong with passing back the log data. |
|
354 | - throw new Exception('Log entry is not an instance of a Log, this should never happen.'); |
|
355 | - } |
|
356 | - |
|
357 | - $user = $logEntry->getUser(); |
|
358 | - if ($user === -1) { |
|
359 | - continue; |
|
360 | - } |
|
361 | - |
|
362 | - if (!array_search($user, $userIds)) { |
|
363 | - $userIds[] = $user; |
|
364 | - } |
|
365 | - } |
|
366 | - |
|
367 | - $users = UserSearchHelper::get($database)->inIds($userIds)->fetchMap('username'); |
|
368 | - $users[-1] = User::getCommunity()->getUsername(); |
|
369 | - |
|
370 | - $logData = array(); |
|
371 | - |
|
372 | - /** @var Log $logEntry */ |
|
373 | - foreach ($logs as $logEntry) { |
|
374 | - $objectDescription = self::getObjectDescription($logEntry->getObjectId(), $logEntry->getObjectType(), |
|
375 | - $database, $configuration); |
|
376 | - |
|
377 | - switch ($logEntry->getAction()) { |
|
378 | - case 'Renamed': |
|
379 | - $renameData = unserialize($logEntry->getComment()); |
|
380 | - $oldName = htmlentities($renameData['old'], ENT_COMPAT, 'UTF-8'); |
|
381 | - $newName = htmlentities($renameData['new'], ENT_COMPAT, 'UTF-8'); |
|
382 | - $comment = 'Renamed \'' . $oldName . '\' to \'' . $newName . '\'.'; |
|
383 | - break; |
|
384 | - case 'RoleChange': |
|
385 | - $roleChangeData = unserialize($logEntry->getComment()); |
|
386 | - |
|
387 | - $removed = array(); |
|
388 | - foreach ($roleChangeData['removed'] as $r) { |
|
389 | - $removed[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
390 | - } |
|
391 | - |
|
392 | - $added = array(); |
|
393 | - foreach ($roleChangeData['added'] as $r) { |
|
394 | - $added[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
395 | - } |
|
396 | - |
|
397 | - $reason = htmlentities($roleChangeData['reason'], ENT_COMPAT, 'UTF-8'); |
|
398 | - |
|
399 | - $roleDelta = 'Removed [' . implode(', ', $removed) . '], Added [' . implode(', ', $added) . ']'; |
|
400 | - $comment = $roleDelta . ' with comment: ' . $reason; |
|
401 | - break; |
|
402 | - case 'JobIssue': |
|
403 | - $jobIssueData = unserialize($logEntry->getComment()); |
|
404 | - $errorMessage = $jobIssueData['error']; |
|
405 | - $status = $jobIssueData['status']; |
|
406 | - |
|
407 | - $comment = 'Job ' . htmlentities($status, ENT_COMPAT, 'UTF-8') . ': '; |
|
408 | - $comment .= htmlentities($errorMessage, ENT_COMPAT, 'UTF-8'); |
|
409 | - break; |
|
410 | - case 'JobIssueRequest': |
|
411 | - case 'JobCompletedRequest': |
|
412 | - $jobData = unserialize($logEntry->getComment()); |
|
413 | - |
|
414 | - /** @var JobQueue $job */ |
|
415 | - $job = JobQueue::getById($jobData['job'], $database); |
|
416 | - $descs = JobQueue::getTaskDescriptions(); |
|
417 | - $comment = htmlentities($descs[$job->getTask()], ENT_COMPAT, 'UTF-8'); |
|
418 | - break; |
|
419 | - |
|
420 | - case 'JobCompleted': |
|
421 | - break; |
|
422 | - default: |
|
423 | - $comment = $logEntry->getComment(); |
|
424 | - break; |
|
425 | - } |
|
426 | - |
|
427 | - $logData[] = array( |
|
428 | - 'timestamp' => $logEntry->getTimestamp(), |
|
429 | - 'userid' => $logEntry->getUser(), |
|
430 | - 'username' => $users[$logEntry->getUser()], |
|
431 | - 'description' => self::getLogDescription($logEntry), |
|
432 | - 'objectdescription' => $objectDescription, |
|
433 | - 'comment' => $comment, |
|
434 | - ); |
|
435 | - } |
|
436 | - |
|
437 | - return array($users, $logData); |
|
438 | - } |
|
300 | + case 'User': |
|
301 | + /** @var User $user */ |
|
302 | + $user = User::getById($objectId, $database); |
|
303 | + $username = htmlentities($user->getUsername(), ENT_COMPAT, 'UTF-8'); |
|
304 | + |
|
305 | + return "<a href=\"{$baseurl}/internal.php/statistics/users/detail?user={$objectId}\">{$username}</a>"; |
|
306 | + case 'WelcomeTemplate': |
|
307 | + /** @var WelcomeTemplate $welcomeTemplate */ |
|
308 | + $welcomeTemplate = WelcomeTemplate::getById($objectId, $database); |
|
309 | + |
|
310 | + // some old templates have been completely deleted and lost to the depths of time. |
|
311 | + if ($welcomeTemplate === false) { |
|
312 | + return "Welcome template #{$objectId}"; |
|
313 | + } |
|
314 | + else { |
|
315 | + $userCode = htmlentities($welcomeTemplate->getUserCode(), ENT_COMPAT, 'UTF-8'); |
|
316 | + |
|
317 | + return "<a href=\"{$baseurl}/internal.php/welcomeTemplates/view?template={$objectId}\">{$userCode}</a>"; |
|
318 | + } |
|
319 | + case 'JobQueue': |
|
320 | + /** @var JobQueue $job */ |
|
321 | + $job = JobQueue::getById($objectId, $database); |
|
322 | + |
|
323 | + $taskDescriptions = JobQueue::getTaskDescriptions(); |
|
324 | + |
|
325 | + $task = $job->getTask(); |
|
326 | + if(isset($taskDescriptions[$task])){ |
|
327 | + $description = $taskDescriptions[$task]; |
|
328 | + } else { |
|
329 | + $description = 'Unknown task'; |
|
330 | + } |
|
331 | + |
|
332 | + return "<a href=\"{$baseurl}/internal.php/jobQueue/view?id={$objectId}\">Job #{$job->getId()} ({$description})</a>"; |
|
333 | + default: |
|
334 | + return '[' . $objectType . " " . $objectId . ']'; |
|
335 | + } |
|
336 | + } |
|
337 | + |
|
338 | + /** |
|
339 | + * @param Log[] $logs |
|
340 | + * @param PdoDatabase $database |
|
341 | + * @param SiteConfiguration $configuration |
|
342 | + * |
|
343 | + * @return array |
|
344 | + * @throws Exception |
|
345 | + */ |
|
346 | + public static function prepareLogsForTemplate($logs, PdoDatabase $database, SiteConfiguration $configuration) |
|
347 | + { |
|
348 | + $userIds = array(); |
|
349 | + |
|
350 | + /** @var Log $logEntry */ |
|
351 | + foreach ($logs as $logEntry) { |
|
352 | + if (!$logEntry instanceof Log) { |
|
353 | + // if this happens, we've done something wrong with passing back the log data. |
|
354 | + throw new Exception('Log entry is not an instance of a Log, this should never happen.'); |
|
355 | + } |
|
356 | + |
|
357 | + $user = $logEntry->getUser(); |
|
358 | + if ($user === -1) { |
|
359 | + continue; |
|
360 | + } |
|
361 | + |
|
362 | + if (!array_search($user, $userIds)) { |
|
363 | + $userIds[] = $user; |
|
364 | + } |
|
365 | + } |
|
366 | + |
|
367 | + $users = UserSearchHelper::get($database)->inIds($userIds)->fetchMap('username'); |
|
368 | + $users[-1] = User::getCommunity()->getUsername(); |
|
369 | + |
|
370 | + $logData = array(); |
|
371 | + |
|
372 | + /** @var Log $logEntry */ |
|
373 | + foreach ($logs as $logEntry) { |
|
374 | + $objectDescription = self::getObjectDescription($logEntry->getObjectId(), $logEntry->getObjectType(), |
|
375 | + $database, $configuration); |
|
376 | + |
|
377 | + switch ($logEntry->getAction()) { |
|
378 | + case 'Renamed': |
|
379 | + $renameData = unserialize($logEntry->getComment()); |
|
380 | + $oldName = htmlentities($renameData['old'], ENT_COMPAT, 'UTF-8'); |
|
381 | + $newName = htmlentities($renameData['new'], ENT_COMPAT, 'UTF-8'); |
|
382 | + $comment = 'Renamed \'' . $oldName . '\' to \'' . $newName . '\'.'; |
|
383 | + break; |
|
384 | + case 'RoleChange': |
|
385 | + $roleChangeData = unserialize($logEntry->getComment()); |
|
386 | + |
|
387 | + $removed = array(); |
|
388 | + foreach ($roleChangeData['removed'] as $r) { |
|
389 | + $removed[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
390 | + } |
|
391 | + |
|
392 | + $added = array(); |
|
393 | + foreach ($roleChangeData['added'] as $r) { |
|
394 | + $added[] = htmlentities($r, ENT_COMPAT, 'UTF-8'); |
|
395 | + } |
|
396 | + |
|
397 | + $reason = htmlentities($roleChangeData['reason'], ENT_COMPAT, 'UTF-8'); |
|
398 | + |
|
399 | + $roleDelta = 'Removed [' . implode(', ', $removed) . '], Added [' . implode(', ', $added) . ']'; |
|
400 | + $comment = $roleDelta . ' with comment: ' . $reason; |
|
401 | + break; |
|
402 | + case 'JobIssue': |
|
403 | + $jobIssueData = unserialize($logEntry->getComment()); |
|
404 | + $errorMessage = $jobIssueData['error']; |
|
405 | + $status = $jobIssueData['status']; |
|
406 | + |
|
407 | + $comment = 'Job ' . htmlentities($status, ENT_COMPAT, 'UTF-8') . ': '; |
|
408 | + $comment .= htmlentities($errorMessage, ENT_COMPAT, 'UTF-8'); |
|
409 | + break; |
|
410 | + case 'JobIssueRequest': |
|
411 | + case 'JobCompletedRequest': |
|
412 | + $jobData = unserialize($logEntry->getComment()); |
|
413 | + |
|
414 | + /** @var JobQueue $job */ |
|
415 | + $job = JobQueue::getById($jobData['job'], $database); |
|
416 | + $descs = JobQueue::getTaskDescriptions(); |
|
417 | + $comment = htmlentities($descs[$job->getTask()], ENT_COMPAT, 'UTF-8'); |
|
418 | + break; |
|
419 | + |
|
420 | + case 'JobCompleted': |
|
421 | + break; |
|
422 | + default: |
|
423 | + $comment = $logEntry->getComment(); |
|
424 | + break; |
|
425 | + } |
|
426 | + |
|
427 | + $logData[] = array( |
|
428 | + 'timestamp' => $logEntry->getTimestamp(), |
|
429 | + 'userid' => $logEntry->getUser(), |
|
430 | + 'username' => $users[$logEntry->getUser()], |
|
431 | + 'description' => self::getLogDescription($logEntry), |
|
432 | + 'objectdescription' => $objectDescription, |
|
433 | + 'comment' => $comment, |
|
434 | + ); |
|
435 | + } |
|
436 | + |
|
437 | + return array($users, $logData); |
|
438 | + } |
|
439 | 439 | } |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | $template = EmailTemplate::getById((int)$id, $entry->getDatabase()); |
130 | 130 | |
131 | 131 | if ($template != false) { |
132 | - return "closed (" . $template->getName() . ")"; |
|
132 | + return "closed (".$template->getName().")"; |
|
133 | 133 | } |
134 | 134 | } |
135 | 135 | |
@@ -275,10 +275,10 @@ discard block |
||
275 | 275 | $ban = Ban::getById($objectId, $database); |
276 | 276 | |
277 | 277 | if ($ban === false) { |
278 | - return 'Ban #' . $objectId . "</a>"; |
|
278 | + return 'Ban #'.$objectId."</a>"; |
|
279 | 279 | } |
280 | 280 | |
281 | - return 'Ban #' . $objectId . " (" . htmlentities($ban->getTarget()) . ")</a>"; |
|
281 | + return 'Ban #'.$objectId." (".htmlentities($ban->getTarget()).")</a>"; |
|
282 | 282 | case 'EmailTemplate': |
283 | 283 | /** @var EmailTemplate $emailTemplate */ |
284 | 284 | $emailTemplate = EmailTemplate::getById($objectId, $database); |
@@ -323,7 +323,7 @@ discard block |
||
323 | 323 | $taskDescriptions = JobQueue::getTaskDescriptions(); |
324 | 324 | |
325 | 325 | $task = $job->getTask(); |
326 | - if(isset($taskDescriptions[$task])){ |
|
326 | + if (isset($taskDescriptions[$task])) { |
|
327 | 327 | $description = $taskDescriptions[$task]; |
328 | 328 | } else { |
329 | 329 | $description = 'Unknown task'; |
@@ -331,7 +331,7 @@ discard block |
||
331 | 331 | |
332 | 332 | return "<a href=\"{$baseurl}/internal.php/jobQueue/view?id={$objectId}\">Job #{$job->getId()} ({$description})</a>"; |
333 | 333 | default: |
334 | - return '[' . $objectType . " " . $objectId . ']'; |
|
334 | + return '['.$objectType." ".$objectId.']'; |
|
335 | 335 | } |
336 | 336 | } |
337 | 337 | |
@@ -379,7 +379,7 @@ discard block |
||
379 | 379 | $renameData = unserialize($logEntry->getComment()); |
380 | 380 | $oldName = htmlentities($renameData['old'], ENT_COMPAT, 'UTF-8'); |
381 | 381 | $newName = htmlentities($renameData['new'], ENT_COMPAT, 'UTF-8'); |
382 | - $comment = 'Renamed \'' . $oldName . '\' to \'' . $newName . '\'.'; |
|
382 | + $comment = 'Renamed \''.$oldName.'\' to \''.$newName.'\'.'; |
|
383 | 383 | break; |
384 | 384 | case 'RoleChange': |
385 | 385 | $roleChangeData = unserialize($logEntry->getComment()); |
@@ -396,15 +396,15 @@ discard block |
||
396 | 396 | |
397 | 397 | $reason = htmlentities($roleChangeData['reason'], ENT_COMPAT, 'UTF-8'); |
398 | 398 | |
399 | - $roleDelta = 'Removed [' . implode(', ', $removed) . '], Added [' . implode(', ', $added) . ']'; |
|
400 | - $comment = $roleDelta . ' with comment: ' . $reason; |
|
399 | + $roleDelta = 'Removed ['.implode(', ', $removed).'], Added ['.implode(', ', $added).']'; |
|
400 | + $comment = $roleDelta.' with comment: '.$reason; |
|
401 | 401 | break; |
402 | 402 | case 'JobIssue': |
403 | 403 | $jobIssueData = unserialize($logEntry->getComment()); |
404 | 404 | $errorMessage = $jobIssueData['error']; |
405 | 405 | $status = $jobIssueData['status']; |
406 | 406 | |
407 | - $comment = 'Job ' . htmlentities($status, ENT_COMPAT, 'UTF-8') . ': '; |
|
407 | + $comment = 'Job '.htmlentities($status, ENT_COMPAT, 'UTF-8').': '; |
|
408 | 408 | $comment .= htmlentities($errorMessage, ENT_COMPAT, 'UTF-8'); |
409 | 409 | break; |
410 | 410 | case 'JobIssueRequest': |
@@ -323,9 +323,10 @@ |
||
323 | 323 | $taskDescriptions = JobQueue::getTaskDescriptions(); |
324 | 324 | |
325 | 325 | $task = $job->getTask(); |
326 | - if(isset($taskDescriptions[$task])){ |
|
326 | + if(isset($taskDescriptions[$task])) { |
|
327 | 327 | $description = $taskDescriptions[$task]; |
328 | - } else { |
|
328 | + } |
|
329 | + else { |
|
329 | 330 | $description = 'Unknown task'; |
330 | 331 | } |
331 | 332 |