@@ -17,16 +17,16 @@ |
||
17 | 17 | */ |
18 | 18 | interface IEmailHelper |
19 | 19 | { |
20 | - /** |
|
21 | - * Sends an email to the specified email address. |
|
22 | - * |
|
23 | - * @param string $replyAddress |
|
24 | - * @param string $to |
|
25 | - * @param string $subject |
|
26 | - * @param string $content |
|
27 | - * @param array $headers Extra headers to include |
|
28 | - * |
|
29 | - * @return void |
|
30 | - */ |
|
31 | - public function sendMail(?string $replyAddress, $to, $subject, $content, $headers = array()); |
|
20 | + /** |
|
21 | + * Sends an email to the specified email address. |
|
22 | + * |
|
23 | + * @param string $replyAddress |
|
24 | + * @param string $to |
|
25 | + * @param string $subject |
|
26 | + * @param string $content |
|
27 | + * @param array $headers Extra headers to include |
|
28 | + * |
|
29 | + * @return void |
|
30 | + */ |
|
31 | + public function sendMail(?string $replyAddress, $to, $subject, $content, $headers = array()); |
|
32 | 32 | } |
33 | 33 | \ No newline at end of file |
@@ -13,21 +13,21 @@ |
||
13 | 13 | |
14 | 14 | interface IBanHelper |
15 | 15 | { |
16 | - /** |
|
17 | - * @param Request $request |
|
18 | - * |
|
19 | - * @return bool |
|
20 | - */ |
|
21 | - public function isBlockBanned(Request $request): bool; |
|
16 | + /** |
|
17 | + * @param Request $request |
|
18 | + * |
|
19 | + * @return bool |
|
20 | + */ |
|
21 | + public function isBlockBanned(Request $request): bool; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @param Request $request |
|
25 | - * |
|
26 | - * @return Ban[] |
|
27 | - */ |
|
28 | - public function getBans(Request $request): array; |
|
23 | + /** |
|
24 | + * @param Request $request |
|
25 | + * |
|
26 | + * @return Ban[] |
|
27 | + */ |
|
28 | + public function getBans(Request $request): array; |
|
29 | 29 | |
30 | - public function canUnban(Ban $ban): bool; |
|
30 | + public function canUnban(Ban $ban): bool; |
|
31 | 31 | |
32 | - public function isActive(Ban $ban): bool; |
|
32 | + public function isActive(Ban $ban): bool; |
|
33 | 33 | } |
@@ -13,112 +13,112 @@ |
||
13 | 13 | |
14 | 14 | class HttpHelper |
15 | 15 | { |
16 | - private $curlHandle; |
|
17 | - |
|
18 | - /** |
|
19 | - * HttpHelper constructor. |
|
20 | - * |
|
21 | - * @param SiteConfiguration $siteConfiguration |
|
22 | - * @param string $cookieJar |
|
23 | - */ |
|
24 | - public function __construct($siteConfiguration, $cookieJar = null) |
|
25 | - { |
|
26 | - $this->curlHandle = curl_init(); |
|
27 | - |
|
28 | - curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true); |
|
29 | - curl_setopt($this->curlHandle, CURLOPT_USERAGENT, $siteConfiguration->getUserAgent()); |
|
30 | - curl_setopt($this->curlHandle, CURLOPT_FAILONERROR, true); |
|
31 | - |
|
32 | - if ($siteConfiguration->getCurlDisableVerifyPeer()) { |
|
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 | - * @param int $timeout Timeout in ms |
|
56 | - * |
|
57 | - * @return string |
|
58 | - * @throws CurlException |
|
59 | - */ |
|
60 | - public function get($url, $parameters = null, $headers = array(), $timeout = 300000) |
|
61 | - { |
|
62 | - if ($parameters !== null && is_array($parameters)) { |
|
63 | - $getString = '?' . http_build_query($parameters); |
|
64 | - $url .= $getString; |
|
65 | - } |
|
66 | - |
|
67 | - curl_setopt($this->curlHandle, CURLOPT_URL, $url); |
|
68 | - |
|
69 | - // Make sure we're doing a GET |
|
70 | - curl_setopt($this->curlHandle, CURLOPT_POST, false); |
|
71 | - |
|
72 | - curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers); |
|
73 | - |
|
74 | - curl_setopt($this->curlHandle, CURLOPT_CONNECTTIMEOUT_MS, $timeout); |
|
75 | - curl_setopt($this->curlHandle, CURLOPT_TIMEOUT_MS, $timeout); |
|
76 | - |
|
77 | - $result = curl_exec($this->curlHandle); |
|
78 | - |
|
79 | - if ($result === false) { |
|
80 | - $error = curl_error($this->curlHandle); |
|
81 | - throw new CurlException('Remote request failed with error ' . $error); |
|
82 | - } |
|
83 | - |
|
84 | - return $result; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * Posts data to a URL |
|
89 | - * |
|
90 | - * @param string $url The URL to fetch. |
|
91 | - * @param array $parameters Key/value pair of POST parameters to add to the request. |
|
92 | - * @param array $headers |
|
93 | - * |
|
94 | - * @return string |
|
95 | - * @throws CurlException |
|
96 | - */ |
|
97 | - public function post($url, $parameters, $headers = array()) |
|
98 | - { |
|
99 | - curl_setopt($this->curlHandle, CURLOPT_URL, $url); |
|
100 | - |
|
101 | - // Make sure we're doing a POST |
|
102 | - curl_setopt($this->curlHandle, CURLOPT_POST, true); |
|
103 | - curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, http_build_query($parameters)); |
|
104 | - |
|
105 | - curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers); |
|
106 | - |
|
107 | - $result = curl_exec($this->curlHandle); |
|
108 | - |
|
109 | - if ($result === false) { |
|
110 | - $error = curl_error($this->curlHandle); |
|
111 | - throw new CurlException('Remote request failed with error ' . $error); |
|
112 | - } |
|
113 | - |
|
114 | - return $result; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * @return string |
|
119 | - */ |
|
120 | - public function getError() |
|
121 | - { |
|
122 | - return curl_error($this->curlHandle); |
|
123 | - } |
|
16 | + private $curlHandle; |
|
17 | + |
|
18 | + /** |
|
19 | + * HttpHelper constructor. |
|
20 | + * |
|
21 | + * @param SiteConfiguration $siteConfiguration |
|
22 | + * @param string $cookieJar |
|
23 | + */ |
|
24 | + public function __construct($siteConfiguration, $cookieJar = null) |
|
25 | + { |
|
26 | + $this->curlHandle = curl_init(); |
|
27 | + |
|
28 | + curl_setopt($this->curlHandle, CURLOPT_RETURNTRANSFER, true); |
|
29 | + curl_setopt($this->curlHandle, CURLOPT_USERAGENT, $siteConfiguration->getUserAgent()); |
|
30 | + curl_setopt($this->curlHandle, CURLOPT_FAILONERROR, true); |
|
31 | + |
|
32 | + if ($siteConfiguration->getCurlDisableVerifyPeer()) { |
|
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 | + * @param int $timeout Timeout in ms |
|
56 | + * |
|
57 | + * @return string |
|
58 | + * @throws CurlException |
|
59 | + */ |
|
60 | + public function get($url, $parameters = null, $headers = array(), $timeout = 300000) |
|
61 | + { |
|
62 | + if ($parameters !== null && is_array($parameters)) { |
|
63 | + $getString = '?' . http_build_query($parameters); |
|
64 | + $url .= $getString; |
|
65 | + } |
|
66 | + |
|
67 | + curl_setopt($this->curlHandle, CURLOPT_URL, $url); |
|
68 | + |
|
69 | + // Make sure we're doing a GET |
|
70 | + curl_setopt($this->curlHandle, CURLOPT_POST, false); |
|
71 | + |
|
72 | + curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers); |
|
73 | + |
|
74 | + curl_setopt($this->curlHandle, CURLOPT_CONNECTTIMEOUT_MS, $timeout); |
|
75 | + curl_setopt($this->curlHandle, CURLOPT_TIMEOUT_MS, $timeout); |
|
76 | + |
|
77 | + $result = curl_exec($this->curlHandle); |
|
78 | + |
|
79 | + if ($result === false) { |
|
80 | + $error = curl_error($this->curlHandle); |
|
81 | + throw new CurlException('Remote request failed with error ' . $error); |
|
82 | + } |
|
83 | + |
|
84 | + return $result; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * Posts data to a URL |
|
89 | + * |
|
90 | + * @param string $url The URL to fetch. |
|
91 | + * @param array $parameters Key/value pair of POST parameters to add to the request. |
|
92 | + * @param array $headers |
|
93 | + * |
|
94 | + * @return string |
|
95 | + * @throws CurlException |
|
96 | + */ |
|
97 | + public function post($url, $parameters, $headers = array()) |
|
98 | + { |
|
99 | + curl_setopt($this->curlHandle, CURLOPT_URL, $url); |
|
100 | + |
|
101 | + // Make sure we're doing a POST |
|
102 | + curl_setopt($this->curlHandle, CURLOPT_POST, true); |
|
103 | + curl_setopt($this->curlHandle, CURLOPT_POSTFIELDS, http_build_query($parameters)); |
|
104 | + |
|
105 | + curl_setopt($this->curlHandle, CURLOPT_HTTPHEADER, $headers); |
|
106 | + |
|
107 | + $result = curl_exec($this->curlHandle); |
|
108 | + |
|
109 | + if ($result === false) { |
|
110 | + $error = curl_error($this->curlHandle); |
|
111 | + throw new CurlException('Remote request failed with error ' . $error); |
|
112 | + } |
|
113 | + |
|
114 | + return $result; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * @return string |
|
119 | + */ |
|
120 | + public function getError() |
|
121 | + { |
|
122 | + return curl_error($this->curlHandle); |
|
123 | + } |
|
124 | 124 | } |
@@ -15,215 +15,215 @@ |
||
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 | - * @param int|null $domain |
|
33 | - * |
|
34 | - * @return RequestSearchHelper |
|
35 | - */ |
|
36 | - public static function get(PdoDatabase $database, ?int $domain) |
|
37 | - { |
|
38 | - $helper = new RequestSearchHelper($database); |
|
39 | - |
|
40 | - if ($domain !== null) { |
|
41 | - $helper->whereClause .= ' AND domain = ?'; |
|
42 | - $helper->parameterList[] = $domain; |
|
43 | - } |
|
44 | - |
|
45 | - return $helper; |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Filters the results by IP address |
|
50 | - * |
|
51 | - * @param string $ipAddress |
|
52 | - * |
|
53 | - * @return $this |
|
54 | - */ |
|
55 | - public function byIp($ipAddress) |
|
56 | - { |
|
57 | - $this->whereClause .= ' AND (ip LIKE ? OR forwardedip LIKE ?)'; |
|
58 | - $this->parameterList[] = $ipAddress; |
|
59 | - $this->parameterList[] = '%' . trim($ipAddress, '%') . '%'; |
|
60 | - |
|
61 | - return $this; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Filters the results by email address |
|
66 | - * |
|
67 | - * @param string $emailAddress |
|
68 | - * |
|
69 | - * @return $this |
|
70 | - */ |
|
71 | - public function byEmailAddress($emailAddress) |
|
72 | - { |
|
73 | - $this->whereClause .= ' AND email LIKE ?'; |
|
74 | - $this->parameterList[] = $emailAddress; |
|
75 | - |
|
76 | - return $this; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * Filters the results by name |
|
81 | - * |
|
82 | - * @param string $name |
|
83 | - * |
|
84 | - * @return $this |
|
85 | - */ |
|
86 | - public function byName($name) |
|
87 | - { |
|
88 | - $this->whereClause .= ' AND name LIKE ?'; |
|
89 | - $this->parameterList[] = $name; |
|
90 | - |
|
91 | - return $this; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Filters the results by comment |
|
96 | - * |
|
97 | - * @param string $comment |
|
98 | - * |
|
99 | - * @return $this |
|
100 | - */ |
|
101 | - public function byComment($comment) |
|
102 | - { |
|
103 | - $this->modifiersClause = 'DISTINCT'; |
|
104 | - $this->joinClause .= ' INNER JOIN comment c ON origin.id = c.request'; |
|
105 | - $this->whereClause .= ' AND c.comment LIKE ?'; |
|
106 | - $this->parameterList[] = $comment; |
|
107 | - |
|
108 | - return $this; |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Filters the results by comment security |
|
113 | - * |
|
114 | - * @param array $security List of allowed values for the security clause |
|
115 | - * |
|
116 | - * @return $this |
|
117 | - */ |
|
118 | - public function byCommentSecurity(array $security) |
|
119 | - { |
|
120 | - $this->inClause('c.visibility', $security); |
|
121 | - |
|
122 | - return $this; |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * Filters the requests to those with a defined status |
|
127 | - * |
|
128 | - * @param $status |
|
129 | - * |
|
130 | - * @return $this |
|
131 | - */ |
|
132 | - public function byStatus($status) |
|
133 | - { |
|
134 | - $this->whereClause .= ' AND status = ?'; |
|
135 | - $this->parameterList[] = $status; |
|
136 | - |
|
137 | - return $this; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Excludes a request from the results |
|
142 | - * |
|
143 | - * @param int $requestId |
|
144 | - * |
|
145 | - * @return $this |
|
146 | - */ |
|
147 | - public function excludingRequest($requestId) |
|
148 | - { |
|
149 | - $this->whereClause .= ' AND id <> ?'; |
|
150 | - $this->parameterList[] = $requestId; |
|
151 | - |
|
152 | - return $this; |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Filters the results to only those with a confirmed email address |
|
157 | - * |
|
158 | - * @return $this |
|
159 | - */ |
|
160 | - public function withConfirmedEmail() |
|
161 | - { |
|
162 | - $this->whereClause .= ' AND emailconfirm = ?'; |
|
163 | - $this->parameterList[] = 'Confirmed'; |
|
164 | - |
|
165 | - return $this; |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Filters the results to exclude purged data |
|
170 | - * |
|
171 | - * @param SiteConfiguration $configuration |
|
172 | - * |
|
173 | - * @return $this |
|
174 | - */ |
|
175 | - public function excludingPurgedData(SiteConfiguration $configuration) |
|
176 | - { |
|
177 | - $this->whereClause .= ' AND ip <> ? AND email <> ?'; |
|
178 | - $this->parameterList[] = $configuration->getDataClearIp(); |
|
179 | - $this->parameterList[] = $configuration->getDataClearEmail(); |
|
180 | - |
|
181 | - return $this; |
|
182 | - } |
|
183 | - |
|
184 | - /** |
|
185 | - * Filters the requests to those without a defined status |
|
186 | - * |
|
187 | - * @param $status |
|
188 | - * |
|
189 | - * @return $this |
|
190 | - */ |
|
191 | - public function excludingStatus($status) |
|
192 | - { |
|
193 | - $this->whereClause .= ' AND status <> ?'; |
|
194 | - $this->parameterList[] = $status; |
|
195 | - |
|
196 | - return $this; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Filters the requests to those which have failed an auto-creation |
|
201 | - * |
|
202 | - * @return $this |
|
203 | - */ |
|
204 | - public function isHospitalised() |
|
205 | - { |
|
206 | - $this->whereClause .= ' AND status = ?'; |
|
207 | - $this->parameterList[] = RequestStatus::HOSPITAL; |
|
208 | - |
|
209 | - return $this; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * Filters the requests to those which have not failed an auto-creation |
|
214 | - * |
|
215 | - * @return $this |
|
216 | - */ |
|
217 | - public function notHospitalised() |
|
218 | - { |
|
219 | - $this->whereClause .= ' AND status <> ?'; |
|
220 | - $this->parameterList[] = RequestStatus::HOSPITAL; |
|
221 | - |
|
222 | - return $this; |
|
223 | - } |
|
224 | - |
|
225 | - public function fetchByQueue($queues) |
|
226 | - { |
|
227 | - return $this->fetchByParameter(' AND queue = ?', $queues); |
|
228 | - } |
|
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 | + * @param int|null $domain |
|
33 | + * |
|
34 | + * @return RequestSearchHelper |
|
35 | + */ |
|
36 | + public static function get(PdoDatabase $database, ?int $domain) |
|
37 | + { |
|
38 | + $helper = new RequestSearchHelper($database); |
|
39 | + |
|
40 | + if ($domain !== null) { |
|
41 | + $helper->whereClause .= ' AND domain = ?'; |
|
42 | + $helper->parameterList[] = $domain; |
|
43 | + } |
|
44 | + |
|
45 | + return $helper; |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Filters the results by IP address |
|
50 | + * |
|
51 | + * @param string $ipAddress |
|
52 | + * |
|
53 | + * @return $this |
|
54 | + */ |
|
55 | + public function byIp($ipAddress) |
|
56 | + { |
|
57 | + $this->whereClause .= ' AND (ip LIKE ? OR forwardedip LIKE ?)'; |
|
58 | + $this->parameterList[] = $ipAddress; |
|
59 | + $this->parameterList[] = '%' . trim($ipAddress, '%') . '%'; |
|
60 | + |
|
61 | + return $this; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Filters the results by email address |
|
66 | + * |
|
67 | + * @param string $emailAddress |
|
68 | + * |
|
69 | + * @return $this |
|
70 | + */ |
|
71 | + public function byEmailAddress($emailAddress) |
|
72 | + { |
|
73 | + $this->whereClause .= ' AND email LIKE ?'; |
|
74 | + $this->parameterList[] = $emailAddress; |
|
75 | + |
|
76 | + return $this; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * Filters the results by name |
|
81 | + * |
|
82 | + * @param string $name |
|
83 | + * |
|
84 | + * @return $this |
|
85 | + */ |
|
86 | + public function byName($name) |
|
87 | + { |
|
88 | + $this->whereClause .= ' AND name LIKE ?'; |
|
89 | + $this->parameterList[] = $name; |
|
90 | + |
|
91 | + return $this; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Filters the results by comment |
|
96 | + * |
|
97 | + * @param string $comment |
|
98 | + * |
|
99 | + * @return $this |
|
100 | + */ |
|
101 | + public function byComment($comment) |
|
102 | + { |
|
103 | + $this->modifiersClause = 'DISTINCT'; |
|
104 | + $this->joinClause .= ' INNER JOIN comment c ON origin.id = c.request'; |
|
105 | + $this->whereClause .= ' AND c.comment LIKE ?'; |
|
106 | + $this->parameterList[] = $comment; |
|
107 | + |
|
108 | + return $this; |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Filters the results by comment security |
|
113 | + * |
|
114 | + * @param array $security List of allowed values for the security clause |
|
115 | + * |
|
116 | + * @return $this |
|
117 | + */ |
|
118 | + public function byCommentSecurity(array $security) |
|
119 | + { |
|
120 | + $this->inClause('c.visibility', $security); |
|
121 | + |
|
122 | + return $this; |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * Filters the requests to those with a defined status |
|
127 | + * |
|
128 | + * @param $status |
|
129 | + * |
|
130 | + * @return $this |
|
131 | + */ |
|
132 | + public function byStatus($status) |
|
133 | + { |
|
134 | + $this->whereClause .= ' AND status = ?'; |
|
135 | + $this->parameterList[] = $status; |
|
136 | + |
|
137 | + return $this; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Excludes a request from the results |
|
142 | + * |
|
143 | + * @param int $requestId |
|
144 | + * |
|
145 | + * @return $this |
|
146 | + */ |
|
147 | + public function excludingRequest($requestId) |
|
148 | + { |
|
149 | + $this->whereClause .= ' AND id <> ?'; |
|
150 | + $this->parameterList[] = $requestId; |
|
151 | + |
|
152 | + return $this; |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Filters the results to only those with a confirmed email address |
|
157 | + * |
|
158 | + * @return $this |
|
159 | + */ |
|
160 | + public function withConfirmedEmail() |
|
161 | + { |
|
162 | + $this->whereClause .= ' AND emailconfirm = ?'; |
|
163 | + $this->parameterList[] = 'Confirmed'; |
|
164 | + |
|
165 | + return $this; |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Filters the results to exclude purged data |
|
170 | + * |
|
171 | + * @param SiteConfiguration $configuration |
|
172 | + * |
|
173 | + * @return $this |
|
174 | + */ |
|
175 | + public function excludingPurgedData(SiteConfiguration $configuration) |
|
176 | + { |
|
177 | + $this->whereClause .= ' AND ip <> ? AND email <> ?'; |
|
178 | + $this->parameterList[] = $configuration->getDataClearIp(); |
|
179 | + $this->parameterList[] = $configuration->getDataClearEmail(); |
|
180 | + |
|
181 | + return $this; |
|
182 | + } |
|
183 | + |
|
184 | + /** |
|
185 | + * Filters the requests to those without a defined status |
|
186 | + * |
|
187 | + * @param $status |
|
188 | + * |
|
189 | + * @return $this |
|
190 | + */ |
|
191 | + public function excludingStatus($status) |
|
192 | + { |
|
193 | + $this->whereClause .= ' AND status <> ?'; |
|
194 | + $this->parameterList[] = $status; |
|
195 | + |
|
196 | + return $this; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Filters the requests to those which have failed an auto-creation |
|
201 | + * |
|
202 | + * @return $this |
|
203 | + */ |
|
204 | + public function isHospitalised() |
|
205 | + { |
|
206 | + $this->whereClause .= ' AND status = ?'; |
|
207 | + $this->parameterList[] = RequestStatus::HOSPITAL; |
|
208 | + |
|
209 | + return $this; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * Filters the requests to those which have not failed an auto-creation |
|
214 | + * |
|
215 | + * @return $this |
|
216 | + */ |
|
217 | + public function notHospitalised() |
|
218 | + { |
|
219 | + $this->whereClause .= ' AND status <> ?'; |
|
220 | + $this->parameterList[] = RequestStatus::HOSPITAL; |
|
221 | + |
|
222 | + return $this; |
|
223 | + } |
|
224 | + |
|
225 | + public function fetchByQueue($queues) |
|
226 | + { |
|
227 | + return $this->fetchByParameter(' AND queue = ?', $queues); |
|
228 | + } |
|
229 | 229 | } |
@@ -16,276 +16,276 @@ |
||
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 | - protected $orderBy; |
|
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 parameters |
|
94 | - // applyLimit() appends parameters to the parameter list, which is useless when we want to run |
|
95 | - // many queries with different parameters. As such, we back up the parameter list, wipe it, apply the limit |
|
96 | - // parameters, and hold them separately, merging again prior to running the actual query. |
|
97 | - $localParameterList = $this->parameterList; |
|
98 | - $this->parameterList = array(); |
|
99 | - $query .= $this->applyLimit(); |
|
100 | - $limitParameters = $this->parameterList; |
|
101 | - |
|
102 | - $statement = $this->database->prepare($query); |
|
103 | - $countStatement = $this->database->prepare($countQuery); |
|
104 | - |
|
105 | - $result = array(); |
|
106 | - foreach ($values as $v) { |
|
107 | - // reset parameter list |
|
108 | - $params = $localParameterList; |
|
109 | - $params[] = $v; |
|
110 | - |
|
111 | - $countStatement->execute($params); |
|
112 | - |
|
113 | - // reapply the limit parameters |
|
114 | - $params = array_merge($params, $limitParameters); |
|
115 | - |
|
116 | - $statement->execute($params); |
|
117 | - |
|
118 | - /** @var DataObject[] $returnedObjects */ |
|
119 | - $returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, $this->targetClass); |
|
120 | - foreach ($returnedObjects as $req) { |
|
121 | - $req->setDatabase($this->database); |
|
122 | - } |
|
123 | - |
|
124 | - $result[$v] = array( |
|
125 | - 'count' => $countStatement->fetchColumn(0), |
|
126 | - 'data' => $returnedObjects, |
|
127 | - ); |
|
128 | - } |
|
129 | - |
|
130 | - return $result; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Finalises the database query, and executes it, returning only the requested column. |
|
135 | - * |
|
136 | - * @param string $column The required column |
|
137 | - * |
|
138 | - * @param bool $distinct |
|
139 | - * |
|
140 | - * @return array |
|
141 | - * @throws ApplicationLogicException |
|
142 | - */ |
|
143 | - public function fetchColumn($column, $distinct = false) |
|
144 | - { |
|
145 | - if ($distinct) { |
|
146 | - if ($this->groupByClause !== '') { |
|
147 | - throw new ApplicationLogicException('Cannot apply distinct to column fetch already using group by'); |
|
148 | - } |
|
149 | - |
|
150 | - $this->groupByClause = ' GROUP BY origin.' . $column; |
|
151 | - } |
|
152 | - |
|
153 | - $statement = $this->getData(array($column)); |
|
154 | - |
|
155 | - return $statement->fetchAll(PDO::FETCH_COLUMN); |
|
156 | - } |
|
157 | - |
|
158 | - public function fetchMap($column) |
|
159 | - { |
|
160 | - $statement = $this->getData(array('id', $column)); |
|
161 | - |
|
162 | - $data = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
163 | - $map = array(); |
|
164 | - |
|
165 | - foreach ($data as $row) { |
|
166 | - $map[$row['id']] = $row[$column]; |
|
167 | - } |
|
168 | - |
|
169 | - return $map; |
|
170 | - } |
|
171 | - |
|
172 | - /** |
|
173 | - * @param int $count Returns the record count of the result set |
|
174 | - * |
|
175 | - * @return $this |
|
176 | - */ |
|
177 | - public function getRecordCount(&$count) |
|
178 | - { |
|
179 | - $query = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
180 | - $query .= $this->joinClause . $this->whereClause; |
|
181 | - |
|
182 | - $statement = $this->database->prepare($query); |
|
183 | - $statement->execute($this->parameterList); |
|
184 | - |
|
185 | - $count = $statement->fetchColumn(0); |
|
186 | - $statement->closeCursor(); |
|
187 | - |
|
188 | - return $this; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Limits the results |
|
193 | - * |
|
194 | - * @param integer $limit |
|
195 | - * @param integer|null $offset |
|
196 | - * |
|
197 | - * @return $this |
|
198 | - * |
|
199 | - */ |
|
200 | - public function limit($limit, $offset = null) |
|
201 | - { |
|
202 | - $this->limit = $limit; |
|
203 | - $this->offset = $offset; |
|
204 | - |
|
205 | - return $this; |
|
206 | - } |
|
207 | - |
|
208 | - private function applyLimit() |
|
209 | - { |
|
210 | - $clause = ''; |
|
211 | - if ($this->limit !== null) { |
|
212 | - $clause = ' LIMIT ?'; |
|
213 | - $this->parameterList[] = $this->limit; |
|
214 | - |
|
215 | - if ($this->offset !== null) { |
|
216 | - $clause .= ' OFFSET ?'; |
|
217 | - $this->parameterList[] = $this->offset; |
|
218 | - } |
|
219 | - } |
|
220 | - |
|
221 | - return $clause; |
|
222 | - } |
|
223 | - |
|
224 | - private function applyOrder() |
|
225 | - { |
|
226 | - if ($this->orderBy !== null) { |
|
227 | - return ' ORDER BY ' . $this->orderBy; |
|
228 | - } |
|
229 | - |
|
230 | - return ''; |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * @param array $columns |
|
235 | - * |
|
236 | - * @return PDOStatement |
|
237 | - */ |
|
238 | - private function getData($columns = array('*')) |
|
239 | - { |
|
240 | - $query = $this->buildQuery($columns); |
|
241 | - $query .= $this->applyOrder(); |
|
242 | - $query .= $this->applyLimit(); |
|
243 | - |
|
244 | - /** @var PDOStatement $statement */ |
|
245 | - $statement = $this->database->prepare($query); |
|
246 | - $statement->execute($this->parameterList); |
|
247 | - |
|
248 | - return $statement; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * @param array $columns |
|
253 | - * |
|
254 | - * @return string |
|
255 | - */ |
|
256 | - protected function buildQuery($columns) |
|
257 | - { |
|
258 | - $colData = array(); |
|
259 | - foreach ($columns as $c) { |
|
260 | - $colData[] = 'origin.' . $c; |
|
261 | - } |
|
262 | - |
|
263 | - $query = "SELECT {$this->modifiersClause} /* " . get_called_class() . " */ " . implode(', ', $colData) . ' FROM ' . $this->table . ' origin '; |
|
264 | - $query .= $this->joinClause . $this->whereClause . $this->groupByClause; |
|
265 | - |
|
266 | - return $query; |
|
267 | - } |
|
268 | - |
|
269 | - public function inIds($idList) |
|
270 | - { |
|
271 | - $this->inClause('id', $idList); |
|
272 | - |
|
273 | - return $this; |
|
274 | - } |
|
275 | - |
|
276 | - protected function inClause($column, $values) |
|
277 | - { |
|
278 | - if (count($values) === 0) { |
|
279 | - $this->whereClause .= ' AND 1 = 2 /* empty IN() */'; |
|
280 | - return; |
|
281 | - } |
|
282 | - |
|
283 | - // You can't use IN() with parameters directly, so let's munge something together. |
|
284 | - // Let's create a string of question marks, which will do as positional parameters. |
|
285 | - $valueCount = count($values); |
|
286 | - $inSection = str_repeat('?,', $valueCount - 1) . '?'; |
|
287 | - |
|
288 | - $this->whereClause .= " AND {$column} IN ({$inSection})"; |
|
289 | - $this->parameterList = array_merge($this->parameterList, $values); |
|
290 | - } |
|
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 | + protected $orderBy; |
|
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 parameters |
|
94 | + // applyLimit() appends parameters to the parameter list, which is useless when we want to run |
|
95 | + // many queries with different parameters. As such, we back up the parameter list, wipe it, apply the limit |
|
96 | + // parameters, and hold them separately, merging again prior to running the actual query. |
|
97 | + $localParameterList = $this->parameterList; |
|
98 | + $this->parameterList = array(); |
|
99 | + $query .= $this->applyLimit(); |
|
100 | + $limitParameters = $this->parameterList; |
|
101 | + |
|
102 | + $statement = $this->database->prepare($query); |
|
103 | + $countStatement = $this->database->prepare($countQuery); |
|
104 | + |
|
105 | + $result = array(); |
|
106 | + foreach ($values as $v) { |
|
107 | + // reset parameter list |
|
108 | + $params = $localParameterList; |
|
109 | + $params[] = $v; |
|
110 | + |
|
111 | + $countStatement->execute($params); |
|
112 | + |
|
113 | + // reapply the limit parameters |
|
114 | + $params = array_merge($params, $limitParameters); |
|
115 | + |
|
116 | + $statement->execute($params); |
|
117 | + |
|
118 | + /** @var DataObject[] $returnedObjects */ |
|
119 | + $returnedObjects = $statement->fetchAll(PDO::FETCH_CLASS, $this->targetClass); |
|
120 | + foreach ($returnedObjects as $req) { |
|
121 | + $req->setDatabase($this->database); |
|
122 | + } |
|
123 | + |
|
124 | + $result[$v] = array( |
|
125 | + 'count' => $countStatement->fetchColumn(0), |
|
126 | + 'data' => $returnedObjects, |
|
127 | + ); |
|
128 | + } |
|
129 | + |
|
130 | + return $result; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Finalises the database query, and executes it, returning only the requested column. |
|
135 | + * |
|
136 | + * @param string $column The required column |
|
137 | + * |
|
138 | + * @param bool $distinct |
|
139 | + * |
|
140 | + * @return array |
|
141 | + * @throws ApplicationLogicException |
|
142 | + */ |
|
143 | + public function fetchColumn($column, $distinct = false) |
|
144 | + { |
|
145 | + if ($distinct) { |
|
146 | + if ($this->groupByClause !== '') { |
|
147 | + throw new ApplicationLogicException('Cannot apply distinct to column fetch already using group by'); |
|
148 | + } |
|
149 | + |
|
150 | + $this->groupByClause = ' GROUP BY origin.' . $column; |
|
151 | + } |
|
152 | + |
|
153 | + $statement = $this->getData(array($column)); |
|
154 | + |
|
155 | + return $statement->fetchAll(PDO::FETCH_COLUMN); |
|
156 | + } |
|
157 | + |
|
158 | + public function fetchMap($column) |
|
159 | + { |
|
160 | + $statement = $this->getData(array('id', $column)); |
|
161 | + |
|
162 | + $data = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
163 | + $map = array(); |
|
164 | + |
|
165 | + foreach ($data as $row) { |
|
166 | + $map[$row['id']] = $row[$column]; |
|
167 | + } |
|
168 | + |
|
169 | + return $map; |
|
170 | + } |
|
171 | + |
|
172 | + /** |
|
173 | + * @param int $count Returns the record count of the result set |
|
174 | + * |
|
175 | + * @return $this |
|
176 | + */ |
|
177 | + public function getRecordCount(&$count) |
|
178 | + { |
|
179 | + $query = 'SELECT /* SearchHelper */ COUNT(*) FROM ' . $this->table . ' origin '; |
|
180 | + $query .= $this->joinClause . $this->whereClause; |
|
181 | + |
|
182 | + $statement = $this->database->prepare($query); |
|
183 | + $statement->execute($this->parameterList); |
|
184 | + |
|
185 | + $count = $statement->fetchColumn(0); |
|
186 | + $statement->closeCursor(); |
|
187 | + |
|
188 | + return $this; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Limits the results |
|
193 | + * |
|
194 | + * @param integer $limit |
|
195 | + * @param integer|null $offset |
|
196 | + * |
|
197 | + * @return $this |
|
198 | + * |
|
199 | + */ |
|
200 | + public function limit($limit, $offset = null) |
|
201 | + { |
|
202 | + $this->limit = $limit; |
|
203 | + $this->offset = $offset; |
|
204 | + |
|
205 | + return $this; |
|
206 | + } |
|
207 | + |
|
208 | + private function applyLimit() |
|
209 | + { |
|
210 | + $clause = ''; |
|
211 | + if ($this->limit !== null) { |
|
212 | + $clause = ' LIMIT ?'; |
|
213 | + $this->parameterList[] = $this->limit; |
|
214 | + |
|
215 | + if ($this->offset !== null) { |
|
216 | + $clause .= ' OFFSET ?'; |
|
217 | + $this->parameterList[] = $this->offset; |
|
218 | + } |
|
219 | + } |
|
220 | + |
|
221 | + return $clause; |
|
222 | + } |
|
223 | + |
|
224 | + private function applyOrder() |
|
225 | + { |
|
226 | + if ($this->orderBy !== null) { |
|
227 | + return ' ORDER BY ' . $this->orderBy; |
|
228 | + } |
|
229 | + |
|
230 | + return ''; |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * @param array $columns |
|
235 | + * |
|
236 | + * @return PDOStatement |
|
237 | + */ |
|
238 | + private function getData($columns = array('*')) |
|
239 | + { |
|
240 | + $query = $this->buildQuery($columns); |
|
241 | + $query .= $this->applyOrder(); |
|
242 | + $query .= $this->applyLimit(); |
|
243 | + |
|
244 | + /** @var PDOStatement $statement */ |
|
245 | + $statement = $this->database->prepare($query); |
|
246 | + $statement->execute($this->parameterList); |
|
247 | + |
|
248 | + return $statement; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * @param array $columns |
|
253 | + * |
|
254 | + * @return string |
|
255 | + */ |
|
256 | + protected function buildQuery($columns) |
|
257 | + { |
|
258 | + $colData = array(); |
|
259 | + foreach ($columns as $c) { |
|
260 | + $colData[] = 'origin.' . $c; |
|
261 | + } |
|
262 | + |
|
263 | + $query = "SELECT {$this->modifiersClause} /* " . get_called_class() . " */ " . implode(', ', $colData) . ' FROM ' . $this->table . ' origin '; |
|
264 | + $query .= $this->joinClause . $this->whereClause . $this->groupByClause; |
|
265 | + |
|
266 | + return $query; |
|
267 | + } |
|
268 | + |
|
269 | + public function inIds($idList) |
|
270 | + { |
|
271 | + $this->inClause('id', $idList); |
|
272 | + |
|
273 | + return $this; |
|
274 | + } |
|
275 | + |
|
276 | + protected function inClause($column, $values) |
|
277 | + { |
|
278 | + if (count($values) === 0) { |
|
279 | + $this->whereClause .= ' AND 1 = 2 /* empty IN() */'; |
|
280 | + return; |
|
281 | + } |
|
282 | + |
|
283 | + // You can't use IN() with parameters directly, so let's munge something together. |
|
284 | + // Let's create a string of question marks, which will do as positional parameters. |
|
285 | + $valueCount = count($values); |
|
286 | + $inSection = str_repeat('?,', $valueCount - 1) . '?'; |
|
287 | + |
|
288 | + $this->whereClause .= " AND {$column} IN ({$inSection})"; |
|
289 | + $this->parameterList = array_merge($this->parameterList, $values); |
|
290 | + } |
|
291 | 291 | } |
@@ -13,85 +13,85 @@ |
||
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 | - * @param int $domain |
|
24 | - * |
|
25 | - * @return JobQueueSearchHelper |
|
26 | - */ |
|
27 | - public static function get(PdoDatabase $database, int $domain) |
|
28 | - { |
|
29 | - $helper = new JobQueueSearchHelper($database); |
|
30 | - |
|
31 | - $helper->whereClause .= ' AND domain = ?'; |
|
32 | - $helper->parameterList[] = $domain; |
|
33 | - |
|
34 | - return $helper; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * @param string[] $statuses |
|
39 | - * |
|
40 | - * @return $this |
|
41 | - */ |
|
42 | - public function statusIn($statuses) |
|
43 | - { |
|
44 | - $this->inClause('status', $statuses); |
|
45 | - |
|
46 | - return $this; |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * @return $this |
|
51 | - */ |
|
52 | - public function notAcknowledged() |
|
53 | - { |
|
54 | - $this->whereClause .= ' AND (acknowledged IS NULL OR acknowledged = 0)'; |
|
55 | - |
|
56 | - return $this; |
|
57 | - } |
|
58 | - |
|
59 | - public function byTask($task) |
|
60 | - { |
|
61 | - $this->whereClause .= ' AND task = ?'; |
|
62 | - $this->parameterList[] = $task; |
|
63 | - |
|
64 | - return $this; |
|
65 | - } |
|
66 | - |
|
67 | - public function byUser($userId) |
|
68 | - { |
|
69 | - $this->whereClause .= ' AND user = ?'; |
|
70 | - $this->parameterList[] = $userId; |
|
71 | - |
|
72 | - return $this; |
|
73 | - } |
|
74 | - |
|
75 | - public function byStatus($status) |
|
76 | - { |
|
77 | - $this->whereClause .= ' AND status = ?'; |
|
78 | - $this->parameterList[] = $status; |
|
79 | - |
|
80 | - return $this; |
|
81 | - } |
|
82 | - |
|
83 | - public function byRequest(int $request) : JobQueueSearchHelper |
|
84 | - { |
|
85 | - $this->whereClause .= ' AND request = ?'; |
|
86 | - $this->parameterList[] = $request; |
|
87 | - |
|
88 | - return $this; |
|
89 | - } |
|
16 | + protected function __construct(PdoDatabase $database) |
|
17 | + { |
|
18 | + parent::__construct($database, 'jobqueue', JobQueue::class, null); |
|
19 | + } |
|
20 | + |
|
21 | + /** |
|
22 | + * @param PdoDatabase $database |
|
23 | + * @param int $domain |
|
24 | + * |
|
25 | + * @return JobQueueSearchHelper |
|
26 | + */ |
|
27 | + public static function get(PdoDatabase $database, int $domain) |
|
28 | + { |
|
29 | + $helper = new JobQueueSearchHelper($database); |
|
30 | + |
|
31 | + $helper->whereClause .= ' AND domain = ?'; |
|
32 | + $helper->parameterList[] = $domain; |
|
33 | + |
|
34 | + return $helper; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * @param string[] $statuses |
|
39 | + * |
|
40 | + * @return $this |
|
41 | + */ |
|
42 | + public function statusIn($statuses) |
|
43 | + { |
|
44 | + $this->inClause('status', $statuses); |
|
45 | + |
|
46 | + return $this; |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * @return $this |
|
51 | + */ |
|
52 | + public function notAcknowledged() |
|
53 | + { |
|
54 | + $this->whereClause .= ' AND (acknowledged IS NULL OR acknowledged = 0)'; |
|
55 | + |
|
56 | + return $this; |
|
57 | + } |
|
58 | + |
|
59 | + public function byTask($task) |
|
60 | + { |
|
61 | + $this->whereClause .= ' AND task = ?'; |
|
62 | + $this->parameterList[] = $task; |
|
63 | + |
|
64 | + return $this; |
|
65 | + } |
|
66 | + |
|
67 | + public function byUser($userId) |
|
68 | + { |
|
69 | + $this->whereClause .= ' AND user = ?'; |
|
70 | + $this->parameterList[] = $userId; |
|
71 | + |
|
72 | + return $this; |
|
73 | + } |
|
74 | + |
|
75 | + public function byStatus($status) |
|
76 | + { |
|
77 | + $this->whereClause .= ' AND status = ?'; |
|
78 | + $this->parameterList[] = $status; |
|
79 | + |
|
80 | + return $this; |
|
81 | + } |
|
82 | + |
|
83 | + public function byRequest(int $request) : JobQueueSearchHelper |
|
84 | + { |
|
85 | + $this->whereClause .= ' AND request = ?'; |
|
86 | + $this->parameterList[] = $request; |
|
87 | + |
|
88 | + return $this; |
|
89 | + } |
|
90 | 90 | |
91 | - public function newestFirst() |
|
92 | - { |
|
93 | - $this->orderBy = 'id DESC'; |
|
91 | + public function newestFirst() |
|
92 | + { |
|
93 | + $this->orderBy = 'id DESC'; |
|
94 | 94 | |
95 | - return $this; |
|
96 | - } |
|
95 | + return $this; |
|
96 | + } |
|
97 | 97 | } |
@@ -13,98 +13,98 @@ |
||
13 | 13 | |
14 | 14 | class LogSearchHelper extends SearchHelperBase |
15 | 15 | { |
16 | - /** |
|
17 | - * LogSearchHelper constructor. |
|
18 | - * |
|
19 | - * @param PdoDatabase $database |
|
20 | - */ |
|
21 | - protected function __construct(PdoDatabase $database) |
|
22 | - { |
|
23 | - parent::__construct($database, 'log', Log::class, 'timestamp DESC'); |
|
24 | - } |
|
16 | + /** |
|
17 | + * LogSearchHelper constructor. |
|
18 | + * |
|
19 | + * @param PdoDatabase $database |
|
20 | + */ |
|
21 | + protected function __construct(PdoDatabase $database) |
|
22 | + { |
|
23 | + parent::__construct($database, 'log', Log::class, 'timestamp DESC'); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Initiates a search for logs |
|
28 | - * |
|
29 | - * @param PdoDatabase $database |
|
30 | - * @param int|null $domain The domain to search for. This will retrieve both logs for the specified domain and |
|
31 | - * global logs if a value is specified. If null, only global logs are returned. |
|
32 | - * |
|
33 | - * @return LogSearchHelper |
|
34 | - */ |
|
35 | - public static function get(PdoDatabase $database, ?int $domain) |
|
36 | - { |
|
37 | - $helper = new LogSearchHelper($database); |
|
26 | + /** |
|
27 | + * Initiates a search for logs |
|
28 | + * |
|
29 | + * @param PdoDatabase $database |
|
30 | + * @param int|null $domain The domain to search for. This will retrieve both logs for the specified domain and |
|
31 | + * global logs if a value is specified. If null, only global logs are returned. |
|
32 | + * |
|
33 | + * @return LogSearchHelper |
|
34 | + */ |
|
35 | + public static function get(PdoDatabase $database, ?int $domain) |
|
36 | + { |
|
37 | + $helper = new LogSearchHelper($database); |
|
38 | 38 | |
39 | - if ($domain === null) { |
|
40 | - $helper->whereClause .= ' AND domain IS NULL'; |
|
41 | - } |
|
42 | - else { |
|
43 | - $helper->whereClause .= ' AND coalesce(domain, ?) = ?'; |
|
44 | - $helper->parameterList[] = $domain; |
|
45 | - $helper->parameterList[] = $domain; |
|
46 | - } |
|
39 | + if ($domain === null) { |
|
40 | + $helper->whereClause .= ' AND domain IS NULL'; |
|
41 | + } |
|
42 | + else { |
|
43 | + $helper->whereClause .= ' AND coalesce(domain, ?) = ?'; |
|
44 | + $helper->parameterList[] = $domain; |
|
45 | + $helper->parameterList[] = $domain; |
|
46 | + } |
|
47 | 47 | |
48 | - return $helper; |
|
49 | - } |
|
48 | + return $helper; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Filters the results by user |
|
53 | - * |
|
54 | - * @param int $userId |
|
55 | - * |
|
56 | - * @return $this |
|
57 | - */ |
|
58 | - public function byUser($userId) |
|
59 | - { |
|
60 | - $this->whereClause .= ' AND user = ?'; |
|
61 | - $this->parameterList[] = $userId; |
|
51 | + /** |
|
52 | + * Filters the results by user |
|
53 | + * |
|
54 | + * @param int $userId |
|
55 | + * |
|
56 | + * @return $this |
|
57 | + */ |
|
58 | + public function byUser($userId) |
|
59 | + { |
|
60 | + $this->whereClause .= ' AND user = ?'; |
|
61 | + $this->parameterList[] = $userId; |
|
62 | 62 | |
63 | - return $this; |
|
64 | - } |
|
63 | + return $this; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Filters the results by log action |
|
68 | - * |
|
69 | - * @param string $action |
|
70 | - * |
|
71 | - * @return $this |
|
72 | - */ |
|
73 | - public function byAction($action) |
|
74 | - { |
|
75 | - $this->whereClause .= ' AND action = ?'; |
|
76 | - $this->parameterList[] = $action; |
|
66 | + /** |
|
67 | + * Filters the results by log action |
|
68 | + * |
|
69 | + * @param string $action |
|
70 | + * |
|
71 | + * @return $this |
|
72 | + */ |
|
73 | + public function byAction($action) |
|
74 | + { |
|
75 | + $this->whereClause .= ' AND action = ?'; |
|
76 | + $this->parameterList[] = $action; |
|
77 | 77 | |
78 | - return $this; |
|
79 | - } |
|
78 | + return $this; |
|
79 | + } |
|
80 | 80 | |
81 | - /** |
|
82 | - * Filters the results by object type |
|
83 | - * |
|
84 | - * @param string $objectType |
|
85 | - * |
|
86 | - * @return $this |
|
87 | - */ |
|
88 | - public function byObjectType($objectType) |
|
89 | - { |
|
90 | - $this->whereClause .= ' AND objecttype = ?'; |
|
91 | - $this->parameterList[] = $objectType; |
|
81 | + /** |
|
82 | + * Filters the results by object type |
|
83 | + * |
|
84 | + * @param string $objectType |
|
85 | + * |
|
86 | + * @return $this |
|
87 | + */ |
|
88 | + public function byObjectType($objectType) |
|
89 | + { |
|
90 | + $this->whereClause .= ' AND objecttype = ?'; |
|
91 | + $this->parameterList[] = $objectType; |
|
92 | 92 | |
93 | - return $this; |
|
94 | - } |
|
93 | + return $this; |
|
94 | + } |
|
95 | 95 | |
96 | - /** |
|
97 | - * Filters the results by object type |
|
98 | - * |
|
99 | - * @param integer $objectId |
|
100 | - * |
|
101 | - * @return $this |
|
102 | - */ |
|
103 | - public function byObjectId($objectId) |
|
104 | - { |
|
105 | - $this->whereClause .= ' AND objectid = ?'; |
|
106 | - $this->parameterList[] = $objectId; |
|
96 | + /** |
|
97 | + * Filters the results by object type |
|
98 | + * |
|
99 | + * @param integer $objectId |
|
100 | + * |
|
101 | + * @return $this |
|
102 | + */ |
|
103 | + public function byObjectId($objectId) |
|
104 | + { |
|
105 | + $this->whereClause .= ' AND objectid = ?'; |
|
106 | + $this->parameterList[] = $objectId; |
|
107 | 107 | |
108 | - return $this; |
|
109 | - } |
|
108 | + return $this; |
|
109 | + } |
|
110 | 110 | } |
111 | 111 | \ No newline at end of file |
@@ -38,8 +38,7 @@ |
||
38 | 38 | |
39 | 39 | if ($domain === null) { |
40 | 40 | $helper->whereClause .= ' AND domain IS NULL'; |
41 | - } |
|
42 | - else { |
|
41 | + } else { |
|
43 | 42 | $helper->whereClause .= ' AND coalesce(domain, ?) = ?'; |
44 | 43 | $helper->parameterList[] = $domain; |
45 | 44 | $helper->parameterList[] = $domain; |
@@ -23,124 +23,124 @@ |
||
23 | 23 | */ |
24 | 24 | abstract class DataObject |
25 | 25 | { |
26 | - /** @var int|null ID of the object */ |
|
27 | - protected $id = null; |
|
28 | - /** @var int update version for optimistic locking */ |
|
29 | - protected $updateversion = 0; |
|
30 | - /** |
|
31 | - * @var PdoDatabase |
|
32 | - */ |
|
33 | - protected $dbObject; |
|
34 | - |
|
35 | - /** |
|
36 | - * Retrieves a data object by it's row ID. |
|
37 | - * |
|
38 | - * @param int $id |
|
39 | - * @param PdoDatabase $database |
|
40 | - * |
|
41 | - * @return DataObject|false |
|
42 | - */ |
|
43 | - public static function getById($id, PdoDatabase $database) |
|
44 | - { |
|
45 | - $array = explode('\\', get_called_class()); |
|
46 | - $realClassName = strtolower(end($array)); |
|
47 | - |
|
48 | - $statement = $database->prepare("SELECT * FROM {$realClassName} WHERE id = :id LIMIT 1;"); |
|
49 | - $statement->bindValue(":id", $id); |
|
50 | - |
|
51 | - $statement->execute(); |
|
52 | - |
|
53 | - $resultObject = $statement->fetchObject(get_called_class()); |
|
54 | - |
|
55 | - if ($resultObject != false) { |
|
56 | - $resultObject->setDatabase($database); |
|
57 | - } |
|
58 | - |
|
59 | - return $resultObject; |
|
60 | - } |
|
61 | - |
|
62 | - public function setDatabase(PdoDatabase $db) |
|
63 | - { |
|
64 | - $this->dbObject = $db; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Gets the database associated with this data object. |
|
69 | - * @return PdoDatabase |
|
70 | - */ |
|
71 | - public function getDatabase() |
|
72 | - { |
|
73 | - return $this->dbObject; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Saves a data object to the database, either updating or inserting a record. |
|
78 | - * |
|
79 | - * @return void |
|
80 | - */ |
|
81 | - abstract public function save(); |
|
82 | - |
|
83 | - /** |
|
84 | - * Retrieves the ID attribute |
|
85 | - */ |
|
86 | - public function getId() |
|
87 | - { |
|
88 | - return (int)$this->id; |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Deletes the object from the database |
|
93 | - */ |
|
94 | - public function delete() |
|
95 | - { |
|
96 | - if ($this->id === null) { |
|
97 | - // wtf? |
|
98 | - return; |
|
99 | - } |
|
100 | - |
|
101 | - $array = explode('\\', get_called_class()); |
|
102 | - $realClassName = strtolower(end($array)); |
|
103 | - |
|
104 | - $deleteQuery = "DELETE FROM {$realClassName} WHERE id = :id AND updateversion = :updateversion;"; |
|
105 | - $statement = $this->dbObject->prepare($deleteQuery); |
|
106 | - |
|
107 | - $statement->bindValue(":id", $this->id); |
|
108 | - $statement->bindValue(":updateversion", $this->updateversion); |
|
109 | - $statement->execute(); |
|
110 | - |
|
111 | - if ($statement->rowCount() !== 1) { |
|
112 | - throw new OptimisticLockFailedException(); |
|
113 | - } |
|
114 | - |
|
115 | - $this->id = null; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * @return int |
|
120 | - */ |
|
121 | - public function getUpdateVersion() |
|
122 | - { |
|
123 | - return $this->updateversion; |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * Sets the update version. |
|
128 | - * |
|
129 | - * You should never call this to change the value of the update version. You should only call it when passing user |
|
130 | - * input through. |
|
131 | - * |
|
132 | - * @param int $updateVersion |
|
133 | - */ |
|
134 | - public function setUpdateVersion($updateVersion) |
|
135 | - { |
|
136 | - $this->updateversion = $updateVersion; |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * @return bool |
|
141 | - */ |
|
142 | - public function isNew() |
|
143 | - { |
|
144 | - return $this->id === null; |
|
145 | - } |
|
26 | + /** @var int|null ID of the object */ |
|
27 | + protected $id = null; |
|
28 | + /** @var int update version for optimistic locking */ |
|
29 | + protected $updateversion = 0; |
|
30 | + /** |
|
31 | + * @var PdoDatabase |
|
32 | + */ |
|
33 | + protected $dbObject; |
|
34 | + |
|
35 | + /** |
|
36 | + * Retrieves a data object by it's row ID. |
|
37 | + * |
|
38 | + * @param int $id |
|
39 | + * @param PdoDatabase $database |
|
40 | + * |
|
41 | + * @return DataObject|false |
|
42 | + */ |
|
43 | + public static function getById($id, PdoDatabase $database) |
|
44 | + { |
|
45 | + $array = explode('\\', get_called_class()); |
|
46 | + $realClassName = strtolower(end($array)); |
|
47 | + |
|
48 | + $statement = $database->prepare("SELECT * FROM {$realClassName} WHERE id = :id LIMIT 1;"); |
|
49 | + $statement->bindValue(":id", $id); |
|
50 | + |
|
51 | + $statement->execute(); |
|
52 | + |
|
53 | + $resultObject = $statement->fetchObject(get_called_class()); |
|
54 | + |
|
55 | + if ($resultObject != false) { |
|
56 | + $resultObject->setDatabase($database); |
|
57 | + } |
|
58 | + |
|
59 | + return $resultObject; |
|
60 | + } |
|
61 | + |
|
62 | + public function setDatabase(PdoDatabase $db) |
|
63 | + { |
|
64 | + $this->dbObject = $db; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Gets the database associated with this data object. |
|
69 | + * @return PdoDatabase |
|
70 | + */ |
|
71 | + public function getDatabase() |
|
72 | + { |
|
73 | + return $this->dbObject; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Saves a data object to the database, either updating or inserting a record. |
|
78 | + * |
|
79 | + * @return void |
|
80 | + */ |
|
81 | + abstract public function save(); |
|
82 | + |
|
83 | + /** |
|
84 | + * Retrieves the ID attribute |
|
85 | + */ |
|
86 | + public function getId() |
|
87 | + { |
|
88 | + return (int)$this->id; |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Deletes the object from the database |
|
93 | + */ |
|
94 | + public function delete() |
|
95 | + { |
|
96 | + if ($this->id === null) { |
|
97 | + // wtf? |
|
98 | + return; |
|
99 | + } |
|
100 | + |
|
101 | + $array = explode('\\', get_called_class()); |
|
102 | + $realClassName = strtolower(end($array)); |
|
103 | + |
|
104 | + $deleteQuery = "DELETE FROM {$realClassName} WHERE id = :id AND updateversion = :updateversion;"; |
|
105 | + $statement = $this->dbObject->prepare($deleteQuery); |
|
106 | + |
|
107 | + $statement->bindValue(":id", $this->id); |
|
108 | + $statement->bindValue(":updateversion", $this->updateversion); |
|
109 | + $statement->execute(); |
|
110 | + |
|
111 | + if ($statement->rowCount() !== 1) { |
|
112 | + throw new OptimisticLockFailedException(); |
|
113 | + } |
|
114 | + |
|
115 | + $this->id = null; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * @return int |
|
120 | + */ |
|
121 | + public function getUpdateVersion() |
|
122 | + { |
|
123 | + return $this->updateversion; |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * Sets the update version. |
|
128 | + * |
|
129 | + * You should never call this to change the value of the update version. You should only call it when passing user |
|
130 | + * input through. |
|
131 | + * |
|
132 | + * @param int $updateVersion |
|
133 | + */ |
|
134 | + public function setUpdateVersion($updateVersion) |
|
135 | + { |
|
136 | + $this->updateversion = $updateVersion; |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * @return bool |
|
141 | + */ |
|
142 | + public function isNew() |
|
143 | + { |
|
144 | + return $this->id === null; |
|
145 | + } |
|
146 | 146 | } |
@@ -77,14 +77,12 @@ |
||
77 | 77 | { |
78 | 78 | if ($this->checkIdentificationCache($onWikiName)) { |
79 | 79 | return true; |
80 | - } |
|
81 | - else { |
|
80 | + } else { |
|
82 | 81 | if ($this->isIdentifiedOnWiki($onWikiName)) { |
83 | 82 | $this->cacheIdentificationStatus($onWikiName); |
84 | 83 | |
85 | 84 | return true; |
86 | - } |
|
87 | - else { |
|
85 | + } else { |
|
88 | 86 | return false; |
89 | 87 | } |
90 | 88 | } |
@@ -27,170 +27,170 @@ |
||
27 | 27 | */ |
28 | 28 | class IdentificationVerifier implements IIdentificationVerifier |
29 | 29 | { |
30 | - /** |
|
31 | - * This field is an array of parameters, in key => value format, that should be appended to the Meta Wikimedia |
|
32 | - * Web Service Endpoint URL to query if a user is listed on the Identification Noticeboard. Note that URL encoding |
|
33 | - * of these values is *not* necessary; this is done automatically. |
|
34 | - * |
|
35 | - * @var string[] |
|
36 | - * @category Security-Critical |
|
37 | - */ |
|
38 | - private static $apiQueryParameters = array( |
|
39 | - 'action' => 'query', |
|
40 | - 'format' => 'json', |
|
41 | - 'prop' => 'links', |
|
42 | - // Populated from SiteConfiguration->getIdentificationNoticeboardPage |
|
43 | - 'titles' => '', |
|
44 | - // Username of the user to be checked, with User: prefix, goes here! Set in isIdentifiedOnWiki() |
|
45 | - 'pltitles' => '', |
|
46 | - ); |
|
47 | - |
|
48 | - private HttpHelper $httpHelper; |
|
49 | - private SiteConfiguration $siteConfiguration; |
|
50 | - private PdoDatabase $dbObject; |
|
51 | - |
|
52 | - /** |
|
53 | - * IdentificationVerifier constructor. |
|
54 | - * |
|
55 | - * @param HttpHelper $httpHelper |
|
56 | - * @param SiteConfiguration $siteConfiguration |
|
57 | - * @param PdoDatabase $dbObject |
|
58 | - */ |
|
59 | - public function __construct(HttpHelper $httpHelper, SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
60 | - { |
|
61 | - $this->httpHelper = $httpHelper; |
|
62 | - $this->siteConfiguration = $siteConfiguration; |
|
63 | - $this->dbObject = $dbObject; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Checks if the given user is identified to the Wikimedia Foundation. |
|
68 | - * |
|
69 | - * @param string $onWikiName The Wikipedia username of the user |
|
70 | - * |
|
71 | - * @category Security-Critical |
|
72 | - * @throws EnvironmentException |
|
73 | - */ |
|
74 | - public function isUserIdentified(string $onWikiName): bool |
|
75 | - { |
|
76 | - if ($this->checkIdentificationCache($onWikiName)) { |
|
77 | - return true; |
|
78 | - } |
|
79 | - else { |
|
80 | - if ($this->isIdentifiedOnWiki($onWikiName)) { |
|
81 | - $this->cacheIdentificationStatus($onWikiName); |
|
82 | - |
|
83 | - return true; |
|
84 | - } |
|
85 | - else { |
|
86 | - return false; |
|
87 | - } |
|
88 | - } |
|
89 | - } |
|
90 | - |
|
91 | - /** |
|
92 | - * Checks if the given user has a valid entry in the idcache table. |
|
93 | - * |
|
94 | - * @param string $onWikiName The Wikipedia username of the user |
|
95 | - * |
|
96 | - * @category Security-Critical |
|
97 | - */ |
|
98 | - private function checkIdentificationCache(string $onWikiName): bool |
|
99 | - { |
|
100 | - $interval = $this->siteConfiguration->getIdentificationCacheExpiry(); |
|
101 | - |
|
102 | - $query = <<<SQL |
|
30 | + /** |
|
31 | + * This field is an array of parameters, in key => value format, that should be appended to the Meta Wikimedia |
|
32 | + * Web Service Endpoint URL to query if a user is listed on the Identification Noticeboard. Note that URL encoding |
|
33 | + * of these values is *not* necessary; this is done automatically. |
|
34 | + * |
|
35 | + * @var string[] |
|
36 | + * @category Security-Critical |
|
37 | + */ |
|
38 | + private static $apiQueryParameters = array( |
|
39 | + 'action' => 'query', |
|
40 | + 'format' => 'json', |
|
41 | + 'prop' => 'links', |
|
42 | + // Populated from SiteConfiguration->getIdentificationNoticeboardPage |
|
43 | + 'titles' => '', |
|
44 | + // Username of the user to be checked, with User: prefix, goes here! Set in isIdentifiedOnWiki() |
|
45 | + 'pltitles' => '', |
|
46 | + ); |
|
47 | + |
|
48 | + private HttpHelper $httpHelper; |
|
49 | + private SiteConfiguration $siteConfiguration; |
|
50 | + private PdoDatabase $dbObject; |
|
51 | + |
|
52 | + /** |
|
53 | + * IdentificationVerifier constructor. |
|
54 | + * |
|
55 | + * @param HttpHelper $httpHelper |
|
56 | + * @param SiteConfiguration $siteConfiguration |
|
57 | + * @param PdoDatabase $dbObject |
|
58 | + */ |
|
59 | + public function __construct(HttpHelper $httpHelper, SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
60 | + { |
|
61 | + $this->httpHelper = $httpHelper; |
|
62 | + $this->siteConfiguration = $siteConfiguration; |
|
63 | + $this->dbObject = $dbObject; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Checks if the given user is identified to the Wikimedia Foundation. |
|
68 | + * |
|
69 | + * @param string $onWikiName The Wikipedia username of the user |
|
70 | + * |
|
71 | + * @category Security-Critical |
|
72 | + * @throws EnvironmentException |
|
73 | + */ |
|
74 | + public function isUserIdentified(string $onWikiName): bool |
|
75 | + { |
|
76 | + if ($this->checkIdentificationCache($onWikiName)) { |
|
77 | + return true; |
|
78 | + } |
|
79 | + else { |
|
80 | + if ($this->isIdentifiedOnWiki($onWikiName)) { |
|
81 | + $this->cacheIdentificationStatus($onWikiName); |
|
82 | + |
|
83 | + return true; |
|
84 | + } |
|
85 | + else { |
|
86 | + return false; |
|
87 | + } |
|
88 | + } |
|
89 | + } |
|
90 | + |
|
91 | + /** |
|
92 | + * Checks if the given user has a valid entry in the idcache table. |
|
93 | + * |
|
94 | + * @param string $onWikiName The Wikipedia username of the user |
|
95 | + * |
|
96 | + * @category Security-Critical |
|
97 | + */ |
|
98 | + private function checkIdentificationCache(string $onWikiName): bool |
|
99 | + { |
|
100 | + $interval = $this->siteConfiguration->getIdentificationCacheExpiry(); |
|
101 | + |
|
102 | + $query = <<<SQL |
|
103 | 103 | SELECT COUNT(`id`) |
104 | 104 | FROM `idcache` |
105 | 105 | WHERE `onwikiusername` = :onwikiname |
106 | 106 | AND DATE_ADD(`checktime`, INTERVAL {$interval}) >= NOW(); |
107 | 107 | SQL; |
108 | - $stmt = $this->dbObject->prepare($query); |
|
109 | - $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
110 | - $stmt->execute(); |
|
111 | - |
|
112 | - // Guaranteed by the query to only return a single row with a single column |
|
113 | - $results = $stmt->fetch(PDO::FETCH_NUM); |
|
114 | - |
|
115 | - // I don't expect this to ever be a value other than 0 or 1 since the `onwikiusername` column is declared as a |
|
116 | - // unique key - but meh. |
|
117 | - return $results[0] != 0; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Does pretty much exactly what it says on the label - this method will clear all expired idcache entries from the |
|
122 | - * idcache table. Meant to be called periodically by a maintenance script. |
|
123 | - * |
|
124 | - * @param SiteConfiguration $siteConfiguration |
|
125 | - * @param PdoDatabase $dbObject |
|
126 | - * |
|
127 | - * @return void |
|
128 | - */ |
|
129 | - public static function clearExpiredCacheEntries(SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
130 | - { |
|
131 | - $interval = $siteConfiguration->getIdentificationCacheExpiry(); |
|
132 | - |
|
133 | - $query = "DELETE FROM idcache WHERE DATE_ADD(checktime, INTERVAL {$interval}) < NOW();"; |
|
134 | - $dbObject->prepare($query)->execute(); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * This method will add an entry to the idcache that the given Wikipedia user has been verified as identified. This |
|
139 | - * is so we don't have to hit the API every single time we check. The cache entry is valid for as long as specified |
|
140 | - * in the ACC configuration (validity enforced by checkIdentificationCache() and clearExpiredCacheEntries()). |
|
141 | - * |
|
142 | - * @param string $onWikiName The Wikipedia username of the user |
|
143 | - * |
|
144 | - * @category Security-Critical |
|
145 | - */ |
|
146 | - private function cacheIdentificationStatus(string $onWikiName): void |
|
147 | - { |
|
148 | - $query = <<<SQL |
|
108 | + $stmt = $this->dbObject->prepare($query); |
|
109 | + $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
110 | + $stmt->execute(); |
|
111 | + |
|
112 | + // Guaranteed by the query to only return a single row with a single column |
|
113 | + $results = $stmt->fetch(PDO::FETCH_NUM); |
|
114 | + |
|
115 | + // I don't expect this to ever be a value other than 0 or 1 since the `onwikiusername` column is declared as a |
|
116 | + // unique key - but meh. |
|
117 | + return $results[0] != 0; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Does pretty much exactly what it says on the label - this method will clear all expired idcache entries from the |
|
122 | + * idcache table. Meant to be called periodically by a maintenance script. |
|
123 | + * |
|
124 | + * @param SiteConfiguration $siteConfiguration |
|
125 | + * @param PdoDatabase $dbObject |
|
126 | + * |
|
127 | + * @return void |
|
128 | + */ |
|
129 | + public static function clearExpiredCacheEntries(SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
130 | + { |
|
131 | + $interval = $siteConfiguration->getIdentificationCacheExpiry(); |
|
132 | + |
|
133 | + $query = "DELETE FROM idcache WHERE DATE_ADD(checktime, INTERVAL {$interval}) < NOW();"; |
|
134 | + $dbObject->prepare($query)->execute(); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * This method will add an entry to the idcache that the given Wikipedia user has been verified as identified. This |
|
139 | + * is so we don't have to hit the API every single time we check. The cache entry is valid for as long as specified |
|
140 | + * in the ACC configuration (validity enforced by checkIdentificationCache() and clearExpiredCacheEntries()). |
|
141 | + * |
|
142 | + * @param string $onWikiName The Wikipedia username of the user |
|
143 | + * |
|
144 | + * @category Security-Critical |
|
145 | + */ |
|
146 | + private function cacheIdentificationStatus(string $onWikiName): void |
|
147 | + { |
|
148 | + $query = <<<SQL |
|
149 | 149 | INSERT INTO idcache (onwikiusername) |
150 | 150 | VALUES (:onwikiname) |
151 | 151 | ON DUPLICATE KEY UPDATE |
152 | 152 | onwikiusername = VALUES(onwikiusername), |
153 | 153 | checktime = CURRENT_TIMESTAMP; |
154 | 154 | SQL; |
155 | - $stmt = $this->dbObject->prepare($query); |
|
156 | - $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
157 | - $stmt->execute(); |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * Queries the Wikimedia API to determine if the specified user is listed on the identification noticeboard. |
|
162 | - * |
|
163 | - * @param string $onWikiName The Wikipedia username of the user |
|
164 | - * |
|
165 | - * @throws EnvironmentException |
|
166 | - * @category Security-Critical |
|
167 | - */ |
|
168 | - private function isIdentifiedOnWiki(string $onWikiName): bool |
|
169 | - { |
|
170 | - $strings = new StringFunctions(); |
|
171 | - |
|
172 | - // First character of Wikipedia usernames is always capitalized. |
|
173 | - $onWikiName = $strings->upperCaseFirst($onWikiName); |
|
174 | - |
|
175 | - $parameters = self::$apiQueryParameters; |
|
176 | - $parameters['pltitles'] = 'User:' . $onWikiName; |
|
177 | - $parameters['titles'] = $this->siteConfiguration->getIdentificationNoticeboardPage(); |
|
178 | - |
|
179 | - try { |
|
180 | - $endpoint = $this->siteConfiguration->getIdentificationNoticeboardWebserviceEndpoint(); |
|
181 | - $response = $this->httpHelper->get($endpoint, $parameters); |
|
182 | - $response = json_decode($response, true); |
|
183 | - } |
|
184 | - catch (CurlException $ex) { |
|
185 | - // failed getting identification status, so throw a nicer error. |
|
186 | - $message = 'Could not contact metawiki API to determine user\' identification status. ' |
|
187 | - . 'This is probably a transient error, so please try again.'; |
|
188 | - |
|
189 | - throw new EnvironmentException($message); |
|
190 | - } |
|
191 | - |
|
192 | - $page = @array_pop($response['query']['pages']); |
|
193 | - |
|
194 | - return @$page['links'][0]['title'] === 'User:' . $onWikiName; |
|
195 | - } |
|
155 | + $stmt = $this->dbObject->prepare($query); |
|
156 | + $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
157 | + $stmt->execute(); |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * Queries the Wikimedia API to determine if the specified user is listed on the identification noticeboard. |
|
162 | + * |
|
163 | + * @param string $onWikiName The Wikipedia username of the user |
|
164 | + * |
|
165 | + * @throws EnvironmentException |
|
166 | + * @category Security-Critical |
|
167 | + */ |
|
168 | + private function isIdentifiedOnWiki(string $onWikiName): bool |
|
169 | + { |
|
170 | + $strings = new StringFunctions(); |
|
171 | + |
|
172 | + // First character of Wikipedia usernames is always capitalized. |
|
173 | + $onWikiName = $strings->upperCaseFirst($onWikiName); |
|
174 | + |
|
175 | + $parameters = self::$apiQueryParameters; |
|
176 | + $parameters['pltitles'] = 'User:' . $onWikiName; |
|
177 | + $parameters['titles'] = $this->siteConfiguration->getIdentificationNoticeboardPage(); |
|
178 | + |
|
179 | + try { |
|
180 | + $endpoint = $this->siteConfiguration->getIdentificationNoticeboardWebserviceEndpoint(); |
|
181 | + $response = $this->httpHelper->get($endpoint, $parameters); |
|
182 | + $response = json_decode($response, true); |
|
183 | + } |
|
184 | + catch (CurlException $ex) { |
|
185 | + // failed getting identification status, so throw a nicer error. |
|
186 | + $message = 'Could not contact metawiki API to determine user\' identification status. ' |
|
187 | + . 'This is probably a transient error, so please try again.'; |
|
188 | + |
|
189 | + throw new EnvironmentException($message); |
|
190 | + } |
|
191 | + |
|
192 | + $page = @array_pop($response['query']['pages']); |
|
193 | + |
|
194 | + return @$page['links'][0]['title'] === 'User:' . $onWikiName; |
|
195 | + } |
|
196 | 196 | } |