@@ -20,58 +20,58 @@ |
||
20 | 20 | */ |
21 | 21 | class StatsAction extends ApiPageBase implements IApiAction |
22 | 22 | { |
23 | - /** |
|
24 | - * The target user |
|
25 | - * @var User $user |
|
26 | - */ |
|
27 | - private $user; |
|
23 | + /** |
|
24 | + * The target user |
|
25 | + * @var User $user |
|
26 | + */ |
|
27 | + private $user; |
|
28 | 28 | |
29 | - /** |
|
30 | - * Summary of execute |
|
31 | - * |
|
32 | - * @param \DOMElement $apiDocument |
|
33 | - * |
|
34 | - * @return \DOMElement |
|
35 | - * @throws ApiException |
|
36 | - * @throws \Exception |
|
37 | - */ |
|
38 | - public function executeApiAction(\DOMElement $apiDocument) |
|
39 | - { |
|
40 | - $username = WebRequest::getString('user'); |
|
41 | - $wikiusername = WebRequest::getString('wikiuser'); |
|
29 | + /** |
|
30 | + * Summary of execute |
|
31 | + * |
|
32 | + * @param \DOMElement $apiDocument |
|
33 | + * |
|
34 | + * @return \DOMElement |
|
35 | + * @throws ApiException |
|
36 | + * @throws \Exception |
|
37 | + */ |
|
38 | + public function executeApiAction(\DOMElement $apiDocument) |
|
39 | + { |
|
40 | + $username = WebRequest::getString('user'); |
|
41 | + $wikiusername = WebRequest::getString('wikiuser'); |
|
42 | 42 | |
43 | - if ($username === null && $wikiusername === null) { |
|
44 | - throw new ApiException("Please specify a username using either user or wikiuser parameters."); |
|
45 | - } |
|
43 | + if ($username === null && $wikiusername === null) { |
|
44 | + throw new ApiException("Please specify a username using either user or wikiuser parameters."); |
|
45 | + } |
|
46 | 46 | |
47 | - $userElement = $this->document->createElement("user"); |
|
48 | - $apiDocument->appendChild($userElement); |
|
47 | + $userElement = $this->document->createElement("user"); |
|
48 | + $apiDocument->appendChild($userElement); |
|
49 | 49 | |
50 | - if ($username !== null) { |
|
51 | - $user = User::getByUsername($username, $this->getDatabase()); |
|
52 | - } |
|
53 | - else { |
|
54 | - $user = User::getByOnWikiUsername($wikiusername, $this->getDatabase()); |
|
55 | - } |
|
50 | + if ($username !== null) { |
|
51 | + $user = User::getByUsername($username, $this->getDatabase()); |
|
52 | + } |
|
53 | + else { |
|
54 | + $user = User::getByOnWikiUsername($wikiusername, $this->getDatabase()); |
|
55 | + } |
|
56 | 56 | |
57 | - if ($user === false) { |
|
58 | - $userElement->setAttribute("missing", "true"); |
|
57 | + if ($user === false) { |
|
58 | + $userElement->setAttribute("missing", "true"); |
|
59 | 59 | |
60 | - return $apiDocument; |
|
61 | - } |
|
60 | + return $apiDocument; |
|
61 | + } |
|
62 | 62 | |
63 | - $this->user = $user; |
|
63 | + $this->user = $user; |
|
64 | 64 | |
65 | - $oauth = new OAuthUserHelper($user, $this->getDatabase(), $this->getOAuthProtocolHelper(), |
|
66 | - $this->getSiteConfiguration()); |
|
65 | + $oauth = new OAuthUserHelper($user, $this->getDatabase(), $this->getOAuthProtocolHelper(), |
|
66 | + $this->getSiteConfiguration()); |
|
67 | 67 | |
68 | - $userElement->setAttribute("username", $this->user->getUsername()); |
|
69 | - $userElement->setAttribute("status", $this->user->getStatus()); |
|
70 | - $userElement->setAttribute("lastactive", $this->user->getLastActive()); |
|
71 | - $userElement->setAttribute("welcome_template", $this->user->getWelcomeTemplate()); |
|
72 | - $userElement->setAttribute("onwikiname", $this->user->getOnWikiName()); |
|
73 | - $userElement->setAttribute("oauth", $oauth->isFullyLinked() ? "true" : "false"); |
|
68 | + $userElement->setAttribute("username", $this->user->getUsername()); |
|
69 | + $userElement->setAttribute("status", $this->user->getStatus()); |
|
70 | + $userElement->setAttribute("lastactive", $this->user->getLastActive()); |
|
71 | + $userElement->setAttribute("welcome_template", $this->user->getWelcomeTemplate()); |
|
72 | + $userElement->setAttribute("onwikiname", $this->user->getOnWikiName()); |
|
73 | + $userElement->setAttribute("oauth", $oauth->isFullyLinked() ? "true" : "false"); |
|
74 | 74 | |
75 | - return $apiDocument; |
|
76 | - } |
|
75 | + return $apiDocument; |
|
76 | + } |
|
77 | 77 | } |
@@ -16,136 +16,136 @@ |
||
16 | 16 | |
17 | 17 | class BotMediaWikiClient implements IMediaWikiClient |
18 | 18 | { |
19 | - /** |
|
20 | - * @var HttpHelper |
|
21 | - */ |
|
22 | - private $httpHelper; |
|
23 | - /** @var string */ |
|
24 | - private $mediawikiWebServiceEndpoint; |
|
25 | - /** @var string */ |
|
26 | - private $creationBotUsername; |
|
27 | - /** @var string */ |
|
28 | - private $creationBotPassword; |
|
29 | - /** @var bool */ |
|
30 | - private $knownLoggedIn = false; |
|
31 | - |
|
32 | - /** |
|
33 | - * BotMediaWikiClient constructor. |
|
34 | - * |
|
35 | - * @param SiteConfiguration $siteConfiguration |
|
36 | - */ |
|
37 | - public function __construct(SiteConfiguration $siteConfiguration) |
|
38 | - { |
|
39 | - $this->mediawikiWebServiceEndpoint = $siteConfiguration->getMediawikiWebServiceEndpoint(); |
|
40 | - |
|
41 | - $this->creationBotUsername = $siteConfiguration->getCreationBotUsername(); |
|
42 | - $this->creationBotPassword = $siteConfiguration->getCreationBotPassword(); |
|
43 | - |
|
44 | - $this->httpHelper = new HttpHelper( |
|
45 | - $siteConfiguration->getUserAgent(), |
|
46 | - $siteConfiguration->getCurlDisableVerifyPeer(), |
|
47 | - $siteConfiguration->getCurlCookieJar() |
|
48 | - ); |
|
49 | - } |
|
50 | - |
|
51 | - function doApiCall($apiParams, $method = 'GET') |
|
52 | - { |
|
53 | - $this->ensureLoggedIn(); |
|
54 | - $apiParams['assert'] = 'user'; |
|
55 | - |
|
56 | - return $this->callApi($apiParams, $method); |
|
57 | - } |
|
58 | - |
|
59 | - private function ensureLoggedIn() |
|
60 | - { |
|
61 | - if ($this->knownLoggedIn) { |
|
62 | - return; |
|
63 | - } |
|
64 | - |
|
65 | - $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
|
66 | - if (isset($userinfoResult->query->userinfo->anon)) { |
|
67 | - // not logged in. |
|
68 | - $this->logIn(); |
|
69 | - |
|
70 | - // retest |
|
71 | - $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
|
72 | - if (isset($userinfoResult->query->userinfo->anon)) { |
|
73 | - throw new MediaWikiApiException('Unable to log in.'); |
|
74 | - } |
|
75 | - else { |
|
76 | - $this->knownLoggedIn = true; |
|
77 | - } |
|
78 | - } |
|
79 | - else { |
|
80 | - $this->knownLoggedIn = true; |
|
81 | - } |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * @param $apiParams |
|
86 | - * @param $method |
|
87 | - * |
|
88 | - * @return mixed |
|
89 | - * @throws ApplicationLogicException |
|
90 | - * @throws CurlException |
|
91 | - */ |
|
92 | - private function callApi($apiParams, $method) |
|
93 | - { |
|
94 | - $apiParams['format'] = 'json'; |
|
95 | - |
|
96 | - if ($method == 'GET') { |
|
97 | - $data = $this->httpHelper->get($this->mediawikiWebServiceEndpoint, $apiParams); |
|
98 | - } |
|
99 | - elseif ($method == 'POST') { |
|
100 | - $data = $this->httpHelper->post($this->mediawikiWebServiceEndpoint, $apiParams); |
|
101 | - } |
|
102 | - else { |
|
103 | - throw new ApplicationLogicException('Unsupported HTTP Method'); |
|
104 | - } |
|
105 | - |
|
106 | - if ($data === false) { |
|
107 | - throw new CurlException('Curl error: ' . $this->httpHelper->getError()); |
|
108 | - } |
|
109 | - |
|
110 | - $result = json_decode($data); |
|
111 | - |
|
112 | - return $result; |
|
113 | - } |
|
114 | - |
|
115 | - private function logIn() |
|
116 | - { |
|
117 | - // get token |
|
118 | - $tokenParams = array( |
|
119 | - 'action' => 'query', |
|
120 | - 'meta' => 'tokens', |
|
121 | - 'type' => 'login', |
|
122 | - ); |
|
123 | - |
|
124 | - $response = $this->callApi($tokenParams, 'POST'); |
|
125 | - |
|
126 | - if (isset($response->error)) { |
|
127 | - throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
128 | - } |
|
129 | - |
|
130 | - $token = $response->query->tokens->logintoken; |
|
131 | - |
|
132 | - if ($token === null) { |
|
133 | - throw new MediaWikiApiException('Edit token could not be acquired'); |
|
134 | - } |
|
135 | - |
|
136 | - $params = array( |
|
137 | - 'action' => 'login', |
|
138 | - 'lgname' => $this->creationBotUsername, |
|
139 | - 'lgpassword' => $this->creationBotPassword, |
|
140 | - 'lgtoken' => $token, |
|
141 | - ); |
|
142 | - |
|
143 | - $loginResponse = $this->callApi($params, 'POST'); |
|
144 | - |
|
145 | - if($loginResponse->login->result == 'Success'){ |
|
146 | - return; |
|
147 | - } |
|
148 | - |
|
149 | - throw new ApplicationLogicException(json_encode($loginResponse)); |
|
150 | - } |
|
19 | + /** |
|
20 | + * @var HttpHelper |
|
21 | + */ |
|
22 | + private $httpHelper; |
|
23 | + /** @var string */ |
|
24 | + private $mediawikiWebServiceEndpoint; |
|
25 | + /** @var string */ |
|
26 | + private $creationBotUsername; |
|
27 | + /** @var string */ |
|
28 | + private $creationBotPassword; |
|
29 | + /** @var bool */ |
|
30 | + private $knownLoggedIn = false; |
|
31 | + |
|
32 | + /** |
|
33 | + * BotMediaWikiClient constructor. |
|
34 | + * |
|
35 | + * @param SiteConfiguration $siteConfiguration |
|
36 | + */ |
|
37 | + public function __construct(SiteConfiguration $siteConfiguration) |
|
38 | + { |
|
39 | + $this->mediawikiWebServiceEndpoint = $siteConfiguration->getMediawikiWebServiceEndpoint(); |
|
40 | + |
|
41 | + $this->creationBotUsername = $siteConfiguration->getCreationBotUsername(); |
|
42 | + $this->creationBotPassword = $siteConfiguration->getCreationBotPassword(); |
|
43 | + |
|
44 | + $this->httpHelper = new HttpHelper( |
|
45 | + $siteConfiguration->getUserAgent(), |
|
46 | + $siteConfiguration->getCurlDisableVerifyPeer(), |
|
47 | + $siteConfiguration->getCurlCookieJar() |
|
48 | + ); |
|
49 | + } |
|
50 | + |
|
51 | + function doApiCall($apiParams, $method = 'GET') |
|
52 | + { |
|
53 | + $this->ensureLoggedIn(); |
|
54 | + $apiParams['assert'] = 'user'; |
|
55 | + |
|
56 | + return $this->callApi($apiParams, $method); |
|
57 | + } |
|
58 | + |
|
59 | + private function ensureLoggedIn() |
|
60 | + { |
|
61 | + if ($this->knownLoggedIn) { |
|
62 | + return; |
|
63 | + } |
|
64 | + |
|
65 | + $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
|
66 | + if (isset($userinfoResult->query->userinfo->anon)) { |
|
67 | + // not logged in. |
|
68 | + $this->logIn(); |
|
69 | + |
|
70 | + // retest |
|
71 | + $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
|
72 | + if (isset($userinfoResult->query->userinfo->anon)) { |
|
73 | + throw new MediaWikiApiException('Unable to log in.'); |
|
74 | + } |
|
75 | + else { |
|
76 | + $this->knownLoggedIn = true; |
|
77 | + } |
|
78 | + } |
|
79 | + else { |
|
80 | + $this->knownLoggedIn = true; |
|
81 | + } |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * @param $apiParams |
|
86 | + * @param $method |
|
87 | + * |
|
88 | + * @return mixed |
|
89 | + * @throws ApplicationLogicException |
|
90 | + * @throws CurlException |
|
91 | + */ |
|
92 | + private function callApi($apiParams, $method) |
|
93 | + { |
|
94 | + $apiParams['format'] = 'json'; |
|
95 | + |
|
96 | + if ($method == 'GET') { |
|
97 | + $data = $this->httpHelper->get($this->mediawikiWebServiceEndpoint, $apiParams); |
|
98 | + } |
|
99 | + elseif ($method == 'POST') { |
|
100 | + $data = $this->httpHelper->post($this->mediawikiWebServiceEndpoint, $apiParams); |
|
101 | + } |
|
102 | + else { |
|
103 | + throw new ApplicationLogicException('Unsupported HTTP Method'); |
|
104 | + } |
|
105 | + |
|
106 | + if ($data === false) { |
|
107 | + throw new CurlException('Curl error: ' . $this->httpHelper->getError()); |
|
108 | + } |
|
109 | + |
|
110 | + $result = json_decode($data); |
|
111 | + |
|
112 | + return $result; |
|
113 | + } |
|
114 | + |
|
115 | + private function logIn() |
|
116 | + { |
|
117 | + // get token |
|
118 | + $tokenParams = array( |
|
119 | + 'action' => 'query', |
|
120 | + 'meta' => 'tokens', |
|
121 | + 'type' => 'login', |
|
122 | + ); |
|
123 | + |
|
124 | + $response = $this->callApi($tokenParams, 'POST'); |
|
125 | + |
|
126 | + if (isset($response->error)) { |
|
127 | + throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
|
128 | + } |
|
129 | + |
|
130 | + $token = $response->query->tokens->logintoken; |
|
131 | + |
|
132 | + if ($token === null) { |
|
133 | + throw new MediaWikiApiException('Edit token could not be acquired'); |
|
134 | + } |
|
135 | + |
|
136 | + $params = array( |
|
137 | + 'action' => 'login', |
|
138 | + 'lgname' => $this->creationBotUsername, |
|
139 | + 'lgpassword' => $this->creationBotPassword, |
|
140 | + 'lgtoken' => $token, |
|
141 | + ); |
|
142 | + |
|
143 | + $loginResponse = $this->callApi($params, 'POST'); |
|
144 | + |
|
145 | + if($loginResponse->login->result == 'Success'){ |
|
146 | + return; |
|
147 | + } |
|
148 | + |
|
149 | + throw new ApplicationLogicException(json_encode($loginResponse)); |
|
150 | + } |
|
151 | 151 | } |
152 | 152 | \ No newline at end of file |
@@ -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 |
@@ -15,80 +15,80 @@ discard block |
||
15 | 15 | |
16 | 16 | class UserSearchHelper extends SearchHelperBase |
17 | 17 | { |
18 | - /** |
|
19 | - * UserSearchHelper constructor. |
|
20 | - * |
|
21 | - * @param PdoDatabase $database |
|
22 | - */ |
|
23 | - public function __construct(PdoDatabase $database) |
|
24 | - { |
|
25 | - parent::__construct($database, 'user', User::class); |
|
26 | - } |
|
27 | - |
|
28 | - /** |
|
29 | - * Initiates a search for requests |
|
30 | - * |
|
31 | - * @param PdoDatabase $database |
|
32 | - * |
|
33 | - * @return UserSearchHelper |
|
34 | - */ |
|
35 | - public static function get(PdoDatabase $database) |
|
36 | - { |
|
37 | - $helper = new UserSearchHelper($database); |
|
38 | - |
|
39 | - return $helper; |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * @param string $status |
|
44 | - * |
|
45 | - * @return $this |
|
46 | - */ |
|
47 | - public function byStatus($status) |
|
48 | - { |
|
49 | - $this->whereClause .= ' AND status = ?'; |
|
50 | - $this->parameterList[] = $status; |
|
51 | - |
|
52 | - return $this; |
|
53 | - } |
|
54 | - |
|
55 | - public function statusIn($statuses) |
|
56 | - { |
|
57 | - $this->inClause('status', $statuses); |
|
58 | - |
|
59 | - return $this; |
|
60 | - } |
|
61 | - |
|
62 | - /** |
|
63 | - * @param string $role |
|
64 | - * |
|
65 | - * @return $this |
|
66 | - */ |
|
67 | - public function byRole($role) |
|
68 | - { |
|
69 | - $this->joinClause .= ' INNER JOIN userrole r on origin.id = r.user'; |
|
70 | - $this->whereClause .= ' AND r.role = ?'; |
|
71 | - $this->parameterList[] = $role; |
|
72 | - |
|
73 | - return $this; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * @param DateTime $instant |
|
78 | - * |
|
79 | - * @return $this |
|
80 | - */ |
|
81 | - public function lastActiveBefore(DateTime $instant) |
|
82 | - { |
|
83 | - $this->whereClause .= ' AND origin.lastactive < ?'; |
|
84 | - $this->parameterList[] = $instant->format("Y-m-d H:i:s"); |
|
85 | - |
|
86 | - return $this; |
|
87 | - } |
|
88 | - |
|
89 | - public function getRoleMap(&$roleMap) |
|
90 | - { |
|
91 | - $query = <<<SQL |
|
18 | + /** |
|
19 | + * UserSearchHelper constructor. |
|
20 | + * |
|
21 | + * @param PdoDatabase $database |
|
22 | + */ |
|
23 | + public function __construct(PdoDatabase $database) |
|
24 | + { |
|
25 | + parent::__construct($database, 'user', User::class); |
|
26 | + } |
|
27 | + |
|
28 | + /** |
|
29 | + * Initiates a search for requests |
|
30 | + * |
|
31 | + * @param PdoDatabase $database |
|
32 | + * |
|
33 | + * @return UserSearchHelper |
|
34 | + */ |
|
35 | + public static function get(PdoDatabase $database) |
|
36 | + { |
|
37 | + $helper = new UserSearchHelper($database); |
|
38 | + |
|
39 | + return $helper; |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * @param string $status |
|
44 | + * |
|
45 | + * @return $this |
|
46 | + */ |
|
47 | + public function byStatus($status) |
|
48 | + { |
|
49 | + $this->whereClause .= ' AND status = ?'; |
|
50 | + $this->parameterList[] = $status; |
|
51 | + |
|
52 | + return $this; |
|
53 | + } |
|
54 | + |
|
55 | + public function statusIn($statuses) |
|
56 | + { |
|
57 | + $this->inClause('status', $statuses); |
|
58 | + |
|
59 | + return $this; |
|
60 | + } |
|
61 | + |
|
62 | + /** |
|
63 | + * @param string $role |
|
64 | + * |
|
65 | + * @return $this |
|
66 | + */ |
|
67 | + public function byRole($role) |
|
68 | + { |
|
69 | + $this->joinClause .= ' INNER JOIN userrole r on origin.id = r.user'; |
|
70 | + $this->whereClause .= ' AND r.role = ?'; |
|
71 | + $this->parameterList[] = $role; |
|
72 | + |
|
73 | + return $this; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * @param DateTime $instant |
|
78 | + * |
|
79 | + * @return $this |
|
80 | + */ |
|
81 | + public function lastActiveBefore(DateTime $instant) |
|
82 | + { |
|
83 | + $this->whereClause .= ' AND origin.lastactive < ?'; |
|
84 | + $this->parameterList[] = $instant->format("Y-m-d H:i:s"); |
|
85 | + |
|
86 | + return $this; |
|
87 | + } |
|
88 | + |
|
89 | + public function getRoleMap(&$roleMap) |
|
90 | + { |
|
91 | + $query = <<<SQL |
|
92 | 92 | SELECT /* UserSearchHelper/roleMap */ |
93 | 93 | r.user user |
94 | 94 | , group_concat(r.role SEPARATOR ', ') roles |
@@ -97,22 +97,22 @@ discard block |
||
97 | 97 | GROUP BY r.user |
98 | 98 | SQL; |
99 | 99 | |
100 | - $statement = $this->database->prepare($query); |
|
101 | - $statement->execute($this->parameterList); |
|
100 | + $statement = $this->database->prepare($query); |
|
101 | + $statement->execute($this->parameterList); |
|
102 | 102 | |
103 | - $roleMap = array(); |
|
104 | - foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
105 | - $roleMap[$row['user']] = $row['roles']; |
|
106 | - } |
|
103 | + $roleMap = array(); |
|
104 | + foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
105 | + $roleMap[$row['user']] = $row['roles']; |
|
106 | + } |
|
107 | 107 | |
108 | - return $this; |
|
109 | - } |
|
108 | + return $this; |
|
109 | + } |
|
110 | 110 | |
111 | - public function withReservedRequest() |
|
112 | - { |
|
113 | - $this->joinClause = ' INNER JOIN request req ON req.reserved = origin.id'; |
|
114 | - $this->groupByClause = ' GROUP BY origin.id, origin.username'; |
|
111 | + public function withReservedRequest() |
|
112 | + { |
|
113 | + $this->joinClause = ' INNER JOIN request req ON req.reserved = origin.id'; |
|
114 | + $this->groupByClause = ' GROUP BY origin.id, origin.username'; |
|
115 | 115 | |
116 | - return $this->fetchMap('username'); |
|
117 | - } |
|
116 | + return $this->fetchMap('username'); |
|
117 | + } |
|
118 | 118 | } |
@@ -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 |
@@ -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 | } |