@@ -23,124 +23,124 @@ |
||
23 | 23 | */ |
24 | 24 | abstract class DataObject |
25 | 25 | { |
26 | - /** @var int 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 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 | } |
@@ -17,31 +17,31 @@ |
||
17 | 17 | |
18 | 18 | class StatsInactiveUsers extends InternalPageBase |
19 | 19 | { |
20 | - public function main() |
|
21 | - { |
|
22 | - $this->setHtmlTitle('Inactive Users :: Statistics'); |
|
23 | - |
|
24 | - $date = new DateTime(); |
|
25 | - $date->modify("-90 days"); |
|
26 | - |
|
27 | - $inactiveUsers = UserSearchHelper::get($this->getDatabase()) |
|
28 | - ->byStatus('Active') |
|
29 | - ->lastActiveBefore($date) |
|
30 | - ->getRoleMap($roleMap) |
|
31 | - ->fetch(); |
|
32 | - |
|
33 | - $this->assign('inactiveUsers', $inactiveUsers); |
|
34 | - $this->assign('roles', $roleMap); |
|
35 | - $this->assign('canSuspend', |
|
36 | - $this->barrierTest('suspend', User::getCurrent($this->getDatabase()), PageUserManagement::class)); |
|
37 | - |
|
38 | - $immuneUsers = $this->getDatabase() |
|
39 | - ->query("SELECT user FROM userrole WHERE role IN ('toolRoot', 'checkuser') GROUP BY user;") |
|
40 | - ->fetchAll(PDO::FETCH_COLUMN); |
|
20 | + public function main() |
|
21 | + { |
|
22 | + $this->setHtmlTitle('Inactive Users :: Statistics'); |
|
23 | + |
|
24 | + $date = new DateTime(); |
|
25 | + $date->modify("-90 days"); |
|
26 | + |
|
27 | + $inactiveUsers = UserSearchHelper::get($this->getDatabase()) |
|
28 | + ->byStatus('Active') |
|
29 | + ->lastActiveBefore($date) |
|
30 | + ->getRoleMap($roleMap) |
|
31 | + ->fetch(); |
|
32 | + |
|
33 | + $this->assign('inactiveUsers', $inactiveUsers); |
|
34 | + $this->assign('roles', $roleMap); |
|
35 | + $this->assign('canSuspend', |
|
36 | + $this->barrierTest('suspend', User::getCurrent($this->getDatabase()), PageUserManagement::class)); |
|
37 | + |
|
38 | + $immuneUsers = $this->getDatabase() |
|
39 | + ->query("SELECT user FROM userrole WHERE role IN ('toolRoot', 'checkuser') GROUP BY user;") |
|
40 | + ->fetchAll(PDO::FETCH_COLUMN); |
|
41 | 41 | |
42 | - $this->assign('immune', array_fill_keys($immuneUsers, true)); |
|
42 | + $this->assign('immune', array_fill_keys($immuneUsers, true)); |
|
43 | 43 | |
44 | - $this->setTemplate('statistics/inactive-users.tpl'); |
|
45 | - $this->assign('statsPageTitle', 'Inactive tool users'); |
|
46 | - } |
|
44 | + $this->setTemplate('statistics/inactive-users.tpl'); |
|
45 | + $this->assign('statsPageTitle', 'Inactive tool users'); |
|
46 | + } |
|
47 | 47 | } |
@@ -21,227 +21,227 @@ |
||
21 | 21 | |
22 | 22 | class PageCloseRequest extends RequestActionBase |
23 | 23 | { |
24 | - protected function main() |
|
25 | - { |
|
26 | - $this->processClose(); |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Main function for this page, when no specific actions are called. |
|
31 | - * @throws ApplicationLogicException |
|
32 | - */ |
|
33 | - final protected function processClose() |
|
34 | - { |
|
35 | - $this->checkPosted(); |
|
36 | - $database = $this->getDatabase(); |
|
37 | - |
|
38 | - $currentUser = User::getCurrent($database); |
|
39 | - $template = $this->getTemplate($database); |
|
40 | - $request = $this->getRequest($database); |
|
41 | - $request->setUpdateVersion(WebRequest::postInt('updateversion')); |
|
42 | - |
|
43 | - if ($request->getStatus() === 'Closed') { |
|
44 | - throw new ApplicationLogicException('Request is already closed'); |
|
45 | - } |
|
46 | - |
|
47 | - if ($this->confirmEmailAlreadySent($request, $template)) { |
|
48 | - return; |
|
49 | - } |
|
50 | - |
|
51 | - if ($this->confirmReserveOverride($request, $template, $currentUser, $database)) { |
|
52 | - return; |
|
53 | - } |
|
54 | - |
|
55 | - if ($this->confirmAccountCreated($request, $template)) { |
|
56 | - return; |
|
57 | - } |
|
58 | - |
|
59 | - // I think we're good here... |
|
60 | - $request->setStatus('Closed'); |
|
61 | - $request->setReserved(null); |
|
62 | - |
|
63 | - Logger::closeRequest($database, $request, $template->getId(), null); |
|
64 | - |
|
65 | - $request->save(); |
|
66 | - |
|
67 | - if ($currentUser->getWelcomeTemplate() !== null) { |
|
68 | - $this->enqueueWelcomeTask($request, null, $currentUser, $database); |
|
69 | - } |
|
70 | - |
|
71 | - // Perform the notifications and stuff *after* we've successfully saved, since the save can throw an OLE and |
|
72 | - // be rolled back. |
|
73 | - |
|
74 | - $this->getNotificationHelper()->requestClosed($request, $template->getName()); |
|
75 | - $sanitisedTemplateName = htmlentities($template->getName(), ENT_COMPAT, 'UTF-8'); |
|
76 | - SessionAlert::success("Request {$request->getId()} has been closed as {$sanitisedTemplateName}"); |
|
77 | - |
|
78 | - $this->sendMail($request, $template->getText(), $currentUser, false); |
|
79 | - |
|
80 | - $this->redirect(); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @param PdoDatabase $database |
|
85 | - * |
|
86 | - * @return EmailTemplate |
|
87 | - * @throws ApplicationLogicException |
|
88 | - */ |
|
89 | - protected function getTemplate(PdoDatabase $database) |
|
90 | - { |
|
91 | - $templateId = WebRequest::postInt('template'); |
|
92 | - if ($templateId === null) { |
|
93 | - throw new ApplicationLogicException('No template specified'); |
|
94 | - } |
|
95 | - |
|
96 | - /** @var EmailTemplate $template */ |
|
97 | - $template = EmailTemplate::getById($templateId, $database); |
|
98 | - if ($template === false || !$template->getActive()) { |
|
99 | - throw new ApplicationLogicException('Invalid or inactive template specified'); |
|
100 | - } |
|
101 | - |
|
102 | - return $template; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * @param Request $request |
|
107 | - * @param EmailTemplate $template |
|
108 | - * |
|
109 | - * @return bool |
|
110 | - */ |
|
111 | - protected function confirmEmailAlreadySent(Request $request, EmailTemplate $template) |
|
112 | - { |
|
113 | - if ($this->checkEmailAlreadySent($request)) { |
|
114 | - $this->showConfirmation($request, $template, 'close-confirmations/email-sent.tpl'); |
|
115 | - |
|
116 | - return true; |
|
117 | - } |
|
118 | - |
|
119 | - return false; |
|
120 | - } |
|
121 | - |
|
122 | - protected function checkEmailAlreadySent(Request $request) |
|
123 | - { |
|
124 | - if ($request->getEmailSent() && !WebRequest::postBoolean('emailSentOverride')) { |
|
125 | - return true; |
|
126 | - } |
|
127 | - |
|
128 | - return false; |
|
129 | - } |
|
130 | - |
|
131 | - protected function checkReserveOverride(Request $request, User $currentUser) |
|
132 | - { |
|
133 | - $reservationId = $request->getReserved(); |
|
134 | - |
|
135 | - if ($reservationId !== 0 && $reservationId !== null) { |
|
136 | - if (!WebRequest::postBoolean('reserveOverride')) { |
|
137 | - if ($currentUser->getId() !== $reservationId) { |
|
138 | - return true; |
|
139 | - } |
|
140 | - } |
|
141 | - } |
|
142 | - |
|
143 | - return false; |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * @param Request $request |
|
148 | - * @param EmailTemplate $template |
|
149 | - * @param User $currentUser |
|
150 | - * @param PdoDatabase $database |
|
151 | - * |
|
152 | - * @return bool |
|
153 | - */ |
|
154 | - protected function confirmReserveOverride( |
|
155 | - Request $request, |
|
156 | - EmailTemplate $template, |
|
157 | - User $currentUser, |
|
158 | - PdoDatabase $database |
|
159 | - ) { |
|
160 | - if ($this->checkReserveOverride($request, $currentUser)) { |
|
161 | - $this->assign('reserveUser', User::getById($request->getReserved(), $database)->getUsername()); |
|
162 | - $this->showConfirmation($request, $template, 'close-confirmations/reserve-override.tpl'); |
|
163 | - |
|
164 | - return true; |
|
165 | - } |
|
166 | - |
|
167 | - return false; |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * @param Request $request |
|
172 | - * @param EmailTemplate $template |
|
173 | - * |
|
174 | - * @return bool |
|
175 | - * @throws \Waca\Exceptions\CurlException |
|
176 | - */ |
|
177 | - protected function confirmAccountCreated(Request $request, EmailTemplate $template) |
|
178 | - { |
|
179 | - if ($this->checkAccountCreated($request, $template)) { |
|
180 | - $this->showConfirmation($request, $template, 'close-confirmations/account-created.tpl'); |
|
181 | - |
|
182 | - return true; |
|
183 | - } |
|
184 | - |
|
185 | - return false; |
|
186 | - } |
|
187 | - |
|
188 | - protected function checkAccountCreated(Request $request, EmailTemplate $template) |
|
189 | - { |
|
190 | - if ($template->getDefaultAction() === EmailTemplate::CREATED && !WebRequest::postBoolean('createOverride')) { |
|
191 | - $parameters = array( |
|
192 | - 'action' => 'query', |
|
193 | - 'list' => 'users', |
|
194 | - 'format' => 'php', |
|
195 | - 'ususers' => $request->getName(), |
|
196 | - ); |
|
197 | - |
|
198 | - $content = $this->getHttpHelper()->get($this->getSiteConfiguration()->getMediawikiWebServiceEndpoint(), |
|
199 | - $parameters); |
|
200 | - |
|
201 | - $apiResult = unserialize($content); |
|
202 | - $exists = !isset($apiResult['query']['users']['0']['missing']); |
|
203 | - |
|
204 | - if (!$exists) { |
|
205 | - return true; |
|
206 | - } |
|
207 | - } |
|
208 | - |
|
209 | - return false; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * @param Request $request |
|
214 | - * @param string $mailText |
|
215 | - * @param User $currentUser |
|
216 | - * @param boolean $ccMailingList |
|
217 | - */ |
|
218 | - protected function sendMail(Request $request, $mailText, User $currentUser, $ccMailingList) |
|
219 | - { |
|
220 | - $requestEmailHelper = new RequestEmailHelper($this->getEmailHelper()); |
|
221 | - $requestEmailHelper->sendMail($request, $mailText, $currentUser, $ccMailingList); |
|
222 | - } |
|
223 | - |
|
224 | - /** |
|
225 | - * @param Request $request |
|
226 | - * @param EmailTemplate $template |
|
227 | - * @param string $templateName |
|
228 | - * |
|
229 | - * @throws Exception |
|
230 | - * @return void |
|
231 | - */ |
|
232 | - protected function showConfirmation(Request $request, EmailTemplate $template, $templateName) |
|
233 | - { |
|
234 | - $this->assignCSRFToken(); |
|
235 | - |
|
236 | - $this->assign('request', $request->getId()); |
|
237 | - $this->assign('template', $template->getId()); |
|
238 | - |
|
239 | - $this->assign('updateversion', $request->getUpdateVersion()); |
|
240 | - |
|
241 | - $this->assign('emailSentOverride', WebRequest::postBoolean('emailSentOverride') ? 'true' : 'false'); |
|
242 | - $this->assign('reserveOverride', WebRequest::postBoolean('reserveOverride') ? 'true' : 'false'); |
|
243 | - $this->assign('createOverride', WebRequest::postBoolean('createOverride') ? 'true' : 'false'); |
|
244 | - |
|
245 | - $this->setTemplate($templateName); |
|
246 | - } |
|
24 | + protected function main() |
|
25 | + { |
|
26 | + $this->processClose(); |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Main function for this page, when no specific actions are called. |
|
31 | + * @throws ApplicationLogicException |
|
32 | + */ |
|
33 | + final protected function processClose() |
|
34 | + { |
|
35 | + $this->checkPosted(); |
|
36 | + $database = $this->getDatabase(); |
|
37 | + |
|
38 | + $currentUser = User::getCurrent($database); |
|
39 | + $template = $this->getTemplate($database); |
|
40 | + $request = $this->getRequest($database); |
|
41 | + $request->setUpdateVersion(WebRequest::postInt('updateversion')); |
|
42 | + |
|
43 | + if ($request->getStatus() === 'Closed') { |
|
44 | + throw new ApplicationLogicException('Request is already closed'); |
|
45 | + } |
|
46 | + |
|
47 | + if ($this->confirmEmailAlreadySent($request, $template)) { |
|
48 | + return; |
|
49 | + } |
|
50 | + |
|
51 | + if ($this->confirmReserveOverride($request, $template, $currentUser, $database)) { |
|
52 | + return; |
|
53 | + } |
|
54 | + |
|
55 | + if ($this->confirmAccountCreated($request, $template)) { |
|
56 | + return; |
|
57 | + } |
|
58 | + |
|
59 | + // I think we're good here... |
|
60 | + $request->setStatus('Closed'); |
|
61 | + $request->setReserved(null); |
|
62 | + |
|
63 | + Logger::closeRequest($database, $request, $template->getId(), null); |
|
64 | + |
|
65 | + $request->save(); |
|
66 | + |
|
67 | + if ($currentUser->getWelcomeTemplate() !== null) { |
|
68 | + $this->enqueueWelcomeTask($request, null, $currentUser, $database); |
|
69 | + } |
|
70 | + |
|
71 | + // Perform the notifications and stuff *after* we've successfully saved, since the save can throw an OLE and |
|
72 | + // be rolled back. |
|
73 | + |
|
74 | + $this->getNotificationHelper()->requestClosed($request, $template->getName()); |
|
75 | + $sanitisedTemplateName = htmlentities($template->getName(), ENT_COMPAT, 'UTF-8'); |
|
76 | + SessionAlert::success("Request {$request->getId()} has been closed as {$sanitisedTemplateName}"); |
|
77 | + |
|
78 | + $this->sendMail($request, $template->getText(), $currentUser, false); |
|
79 | + |
|
80 | + $this->redirect(); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @param PdoDatabase $database |
|
85 | + * |
|
86 | + * @return EmailTemplate |
|
87 | + * @throws ApplicationLogicException |
|
88 | + */ |
|
89 | + protected function getTemplate(PdoDatabase $database) |
|
90 | + { |
|
91 | + $templateId = WebRequest::postInt('template'); |
|
92 | + if ($templateId === null) { |
|
93 | + throw new ApplicationLogicException('No template specified'); |
|
94 | + } |
|
95 | + |
|
96 | + /** @var EmailTemplate $template */ |
|
97 | + $template = EmailTemplate::getById($templateId, $database); |
|
98 | + if ($template === false || !$template->getActive()) { |
|
99 | + throw new ApplicationLogicException('Invalid or inactive template specified'); |
|
100 | + } |
|
101 | + |
|
102 | + return $template; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * @param Request $request |
|
107 | + * @param EmailTemplate $template |
|
108 | + * |
|
109 | + * @return bool |
|
110 | + */ |
|
111 | + protected function confirmEmailAlreadySent(Request $request, EmailTemplate $template) |
|
112 | + { |
|
113 | + if ($this->checkEmailAlreadySent($request)) { |
|
114 | + $this->showConfirmation($request, $template, 'close-confirmations/email-sent.tpl'); |
|
115 | + |
|
116 | + return true; |
|
117 | + } |
|
118 | + |
|
119 | + return false; |
|
120 | + } |
|
121 | + |
|
122 | + protected function checkEmailAlreadySent(Request $request) |
|
123 | + { |
|
124 | + if ($request->getEmailSent() && !WebRequest::postBoolean('emailSentOverride')) { |
|
125 | + return true; |
|
126 | + } |
|
127 | + |
|
128 | + return false; |
|
129 | + } |
|
130 | + |
|
131 | + protected function checkReserveOverride(Request $request, User $currentUser) |
|
132 | + { |
|
133 | + $reservationId = $request->getReserved(); |
|
134 | + |
|
135 | + if ($reservationId !== 0 && $reservationId !== null) { |
|
136 | + if (!WebRequest::postBoolean('reserveOverride')) { |
|
137 | + if ($currentUser->getId() !== $reservationId) { |
|
138 | + return true; |
|
139 | + } |
|
140 | + } |
|
141 | + } |
|
142 | + |
|
143 | + return false; |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * @param Request $request |
|
148 | + * @param EmailTemplate $template |
|
149 | + * @param User $currentUser |
|
150 | + * @param PdoDatabase $database |
|
151 | + * |
|
152 | + * @return bool |
|
153 | + */ |
|
154 | + protected function confirmReserveOverride( |
|
155 | + Request $request, |
|
156 | + EmailTemplate $template, |
|
157 | + User $currentUser, |
|
158 | + PdoDatabase $database |
|
159 | + ) { |
|
160 | + if ($this->checkReserveOverride($request, $currentUser)) { |
|
161 | + $this->assign('reserveUser', User::getById($request->getReserved(), $database)->getUsername()); |
|
162 | + $this->showConfirmation($request, $template, 'close-confirmations/reserve-override.tpl'); |
|
163 | + |
|
164 | + return true; |
|
165 | + } |
|
166 | + |
|
167 | + return false; |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * @param Request $request |
|
172 | + * @param EmailTemplate $template |
|
173 | + * |
|
174 | + * @return bool |
|
175 | + * @throws \Waca\Exceptions\CurlException |
|
176 | + */ |
|
177 | + protected function confirmAccountCreated(Request $request, EmailTemplate $template) |
|
178 | + { |
|
179 | + if ($this->checkAccountCreated($request, $template)) { |
|
180 | + $this->showConfirmation($request, $template, 'close-confirmations/account-created.tpl'); |
|
181 | + |
|
182 | + return true; |
|
183 | + } |
|
184 | + |
|
185 | + return false; |
|
186 | + } |
|
187 | + |
|
188 | + protected function checkAccountCreated(Request $request, EmailTemplate $template) |
|
189 | + { |
|
190 | + if ($template->getDefaultAction() === EmailTemplate::CREATED && !WebRequest::postBoolean('createOverride')) { |
|
191 | + $parameters = array( |
|
192 | + 'action' => 'query', |
|
193 | + 'list' => 'users', |
|
194 | + 'format' => 'php', |
|
195 | + 'ususers' => $request->getName(), |
|
196 | + ); |
|
197 | + |
|
198 | + $content = $this->getHttpHelper()->get($this->getSiteConfiguration()->getMediawikiWebServiceEndpoint(), |
|
199 | + $parameters); |
|
200 | + |
|
201 | + $apiResult = unserialize($content); |
|
202 | + $exists = !isset($apiResult['query']['users']['0']['missing']); |
|
203 | + |
|
204 | + if (!$exists) { |
|
205 | + return true; |
|
206 | + } |
|
207 | + } |
|
208 | + |
|
209 | + return false; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * @param Request $request |
|
214 | + * @param string $mailText |
|
215 | + * @param User $currentUser |
|
216 | + * @param boolean $ccMailingList |
|
217 | + */ |
|
218 | + protected function sendMail(Request $request, $mailText, User $currentUser, $ccMailingList) |
|
219 | + { |
|
220 | + $requestEmailHelper = new RequestEmailHelper($this->getEmailHelper()); |
|
221 | + $requestEmailHelper->sendMail($request, $mailText, $currentUser, $ccMailingList); |
|
222 | + } |
|
223 | + |
|
224 | + /** |
|
225 | + * @param Request $request |
|
226 | + * @param EmailTemplate $template |
|
227 | + * @param string $templateName |
|
228 | + * |
|
229 | + * @throws Exception |
|
230 | + * @return void |
|
231 | + */ |
|
232 | + protected function showConfirmation(Request $request, EmailTemplate $template, $templateName) |
|
233 | + { |
|
234 | + $this->assignCSRFToken(); |
|
235 | + |
|
236 | + $this->assign('request', $request->getId()); |
|
237 | + $this->assign('template', $template->getId()); |
|
238 | + |
|
239 | + $this->assign('updateversion', $request->getUpdateVersion()); |
|
240 | + |
|
241 | + $this->assign('emailSentOverride', WebRequest::postBoolean('emailSentOverride') ? 'true' : 'false'); |
|
242 | + $this->assign('reserveOverride', WebRequest::postBoolean('reserveOverride') ? 'true' : 'false'); |
|
243 | + $this->assign('createOverride', WebRequest::postBoolean('createOverride') ? 'true' : 'false'); |
|
244 | + |
|
245 | + $this->setTemplate($templateName); |
|
246 | + } |
|
247 | 247 | } |
@@ -23,264 +23,264 @@ |
||
23 | 23 | |
24 | 24 | class PageCustomClose extends PageCloseRequest |
25 | 25 | { |
26 | - use RequestData; |
|
27 | - |
|
28 | - protected function main() |
|
29 | - { |
|
30 | - $database = $this->getDatabase(); |
|
31 | - |
|
32 | - $request = $this->getRequest($database); |
|
33 | - $currentUser = User::getCurrent($this->getDatabase()); |
|
34 | - |
|
35 | - if ($request->getStatus() === 'Closed') { |
|
36 | - throw new ApplicationLogicException('Request is already closed'); |
|
37 | - } |
|
38 | - |
|
39 | - // Dual-mode page |
|
40 | - if (WebRequest::wasPosted()) { |
|
41 | - $this->validateCSRFToken(); |
|
42 | - $this->doCustomClose($currentUser, $request, $database); |
|
43 | - |
|
44 | - $this->redirect(); |
|
45 | - } |
|
46 | - else { |
|
47 | - $this->assignCSRFToken(); |
|
48 | - $this->showCustomCloseForm($database, $request); |
|
49 | - } |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @param $database |
|
54 | - * |
|
55 | - * @return Request |
|
56 | - * @throws ApplicationLogicException |
|
57 | - */ |
|
58 | - protected function getRequest(PdoDatabase $database) |
|
59 | - { |
|
60 | - $requestId = WebRequest::getInt('request'); |
|
61 | - if ($requestId === null) { |
|
62 | - throw new ApplicationLogicException('Request ID not found'); |
|
63 | - } |
|
64 | - |
|
65 | - /** @var Request $request */ |
|
66 | - $request = Request::getById($requestId, $database); |
|
67 | - |
|
68 | - if ($request === false) { |
|
69 | - throw new ApplicationLogicException('Request not found'); |
|
70 | - } |
|
71 | - |
|
72 | - return $request; |
|
73 | - } |
|
74 | - |
|
75 | - /** |
|
76 | - * @param PdoDatabase $database |
|
77 | - * |
|
78 | - * @return EmailTemplate|null |
|
79 | - */ |
|
80 | - protected function getTemplate(PdoDatabase $database) |
|
81 | - { |
|
82 | - $templateId = WebRequest::getInt('template'); |
|
83 | - if ($templateId === null) { |
|
84 | - return null; |
|
85 | - } |
|
86 | - |
|
87 | - /** @var EmailTemplate $template */ |
|
88 | - $template = EmailTemplate::getById($templateId, $database); |
|
89 | - if ($template === false || !$template->getActive()) { |
|
90 | - return null; |
|
91 | - } |
|
92 | - |
|
93 | - return $template; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @param $database |
|
98 | - * @param $request |
|
99 | - * |
|
100 | - * @throws Exception |
|
101 | - */ |
|
102 | - protected function showCustomCloseForm(PdoDatabase $database, Request $request) |
|
103 | - { |
|
104 | - $currentUser = User::getCurrent($database); |
|
105 | - $config = $this->getSiteConfiguration(); |
|
106 | - |
|
107 | - $allowedPrivateData = $this->isAllowedPrivateData($request, $currentUser); |
|
108 | - if (!$allowedPrivateData) { |
|
109 | - // we probably shouldn't be showing the user this form if they're not allowed to access private data... |
|
110 | - throw new AccessDeniedException($this->getSecurityManager()); |
|
111 | - } |
|
112 | - |
|
113 | - $template = $this->getTemplate($database); |
|
114 | - |
|
115 | - // Preload data |
|
116 | - $this->assign('defaultAction', ''); |
|
117 | - $this->assign('preloadText', ''); |
|
118 | - $this->assign('preloadTitle', ''); |
|
119 | - |
|
120 | - if ($template !== null) { |
|
121 | - $this->assign('defaultAction', $template->getDefaultAction()); |
|
122 | - $this->assign('preloadText', $template->getText()); |
|
123 | - $this->assign('preloadTitle', $template->getName()); |
|
124 | - } |
|
125 | - |
|
126 | - // Static data |
|
127 | - $this->assign('requeststates', $config->getRequestStates()); |
|
128 | - |
|
129 | - // request data |
|
130 | - $this->assign('requestId', $request->getIp()); |
|
131 | - $this->assign('updateVersion', $request->getUpdateVersion()); |
|
132 | - $this->setupBasicData($request, $config); |
|
133 | - $this->setupReservationDetails($request->getReserved(), $database, $currentUser); |
|
134 | - $this->setupPrivateData($request, $currentUser, $this->getSiteConfiguration(), $database); |
|
135 | - |
|
136 | - // IP location |
|
137 | - $trustedIp = $this->getXffTrustProvider()->getTrustedClientIp($request->getIp(), $request->getForwardedIp()); |
|
138 | - $this->assign('iplocation', $this->getLocationProvider()->getIpLocation($trustedIp)); |
|
139 | - |
|
140 | - // Confirmations |
|
141 | - $this->assign('confirmEmailAlreadySent', $this->checkEmailAlreadySent($request)); |
|
142 | - $this->assign('confirmReserveOverride', $this->checkReserveOverride($request, $currentUser)); |
|
143 | - |
|
144 | - $this->assign('canSkipCcMailingList', $this->barrierTest('skipCcMailingList', $currentUser)); |
|
145 | - |
|
146 | - // template |
|
147 | - $this->setTemplate('custom-close.tpl'); |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @param User $currentUser |
|
152 | - * @param Request $request |
|
153 | - * @param PdoDatabase $database |
|
154 | - * |
|
155 | - * @throws ApplicationLogicException |
|
156 | - */ |
|
157 | - protected function doCustomClose(User $currentUser, Request $request, PdoDatabase $database) |
|
158 | - { |
|
159 | - $messageBody = WebRequest::postString('msgbody'); |
|
160 | - if ($messageBody === null || trim($messageBody) === '') { |
|
161 | - throw new ApplicationLogicException('Message body cannot be blank'); |
|
162 | - } |
|
163 | - |
|
164 | - $ccMailingList = true; |
|
165 | - if ($this->barrierTest('skipCcMailingList', $currentUser)) { |
|
166 | - $ccMailingList = WebRequest::postBoolean('ccMailingList'); |
|
167 | - } |
|
168 | - |
|
169 | - if ($request->getStatus() === 'Closed') { |
|
170 | - throw new ApplicationLogicException('Request is already closed'); |
|
171 | - } |
|
172 | - |
|
173 | - if (!(WebRequest::postBoolean('confirmEmailAlreadySent') |
|
174 | - && WebRequest::postBoolean('confirmReserveOverride')) |
|
175 | - ) { |
|
176 | - throw new ApplicationLogicException('Not all confirmations checked'); |
|
177 | - } |
|
178 | - |
|
179 | - $action = WebRequest::postString('action'); |
|
180 | - $availableRequestStates = $this->getSiteConfiguration()->getRequestStates(); |
|
181 | - |
|
182 | - if ($action === EmailTemplate::CREATED || $action === EmailTemplate::NOT_CREATED) { |
|
183 | - // Close request |
|
184 | - $this->closeRequest($request, $database, $action, $messageBody); |
|
185 | - |
|
186 | - // Send the mail after the save, since save can be rolled back |
|
187 | - $this->sendMail($request, $messageBody, $currentUser, $ccMailingList); |
|
188 | - } |
|
189 | - else { |
|
190 | - if (array_key_exists($action, $availableRequestStates)) { |
|
191 | - // Defer to other state |
|
192 | - $this->deferRequest($request, $database, $action, $availableRequestStates, $messageBody); |
|
193 | - |
|
194 | - // Send the mail after the save, since save can be rolled back |
|
195 | - $this->sendMail($request, $messageBody, $currentUser, $ccMailingList); |
|
196 | - } |
|
197 | - else { |
|
198 | - $request->setReserved(null); |
|
199 | - $request->setUpdateVersion(WebRequest::postInt('updateversion')); |
|
200 | - $request->save(); |
|
201 | - |
|
202 | - // Perform the notifications and stuff *after* we've successfully saved, since the save can throw an OLE |
|
203 | - // and be rolled back. |
|
204 | - |
|
205 | - // Send mail |
|
206 | - $this->sendMail($request, $messageBody, $currentUser, $ccMailingList); |
|
207 | - |
|
208 | - Logger::sentMail($database, $request, $messageBody); |
|
209 | - Logger::unreserve($database, $request); |
|
210 | - |
|
211 | - $this->getNotificationHelper()->sentMail($request); |
|
212 | - SessionAlert::success("Sent mail to Request {$request->getId()}"); |
|
213 | - } |
|
214 | - } |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * @param Request $request |
|
219 | - * @param PdoDatabase $database |
|
220 | - * @param string $action |
|
221 | - * @param string $messageBody |
|
222 | - * |
|
223 | - * @throws Exception |
|
224 | - * @throws OptimisticLockFailedException |
|
225 | - */ |
|
226 | - protected function closeRequest(Request $request, PdoDatabase $database, $action, $messageBody) |
|
227 | - { |
|
228 | - $request->setStatus('Closed'); |
|
229 | - $request->setReserved(null); |
|
230 | - $request->setUpdateVersion(WebRequest::postInt('updateversion')); |
|
231 | - $request->save(); |
|
232 | - |
|
233 | - // Perform the notifications and stuff *after* we've successfully saved, since the save can throw an OLE and |
|
234 | - // be rolled back. |
|
235 | - |
|
236 | - if ($action == EmailTemplate::CREATED) { |
|
237 | - $logCloseType = 'custom-y'; |
|
238 | - $notificationCloseType = "Custom, Created"; |
|
239 | - } |
|
240 | - else { |
|
241 | - $logCloseType = 'custom-n'; |
|
242 | - $notificationCloseType = "Custom, Not Created"; |
|
243 | - } |
|
244 | - |
|
245 | - Logger::closeRequest($database, $request, $logCloseType, $messageBody); |
|
246 | - $this->getNotificationHelper()->requestClosed($request, $notificationCloseType); |
|
247 | - |
|
248 | - $requestName = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
|
249 | - SessionAlert::success("Request {$request->getId()} ({$requestName}) closed as {$notificationCloseType}."); |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * @param Request $request |
|
254 | - * @param PdoDatabase $database |
|
255 | - * @param string $action |
|
256 | - * @param $availableRequestStates |
|
257 | - * @param string $messageBody |
|
258 | - * |
|
259 | - * @throws Exception |
|
260 | - * @throws OptimisticLockFailedException |
|
261 | - */ |
|
262 | - protected function deferRequest( |
|
263 | - Request $request, |
|
264 | - PdoDatabase $database, |
|
265 | - $action, |
|
266 | - $availableRequestStates, |
|
267 | - $messageBody |
|
268 | - ) { |
|
269 | - $request->setStatus($action); |
|
270 | - $request->setReserved(null); |
|
271 | - $request->setUpdateVersion(WebRequest::postInt('updateversion')); |
|
272 | - $request->save(); |
|
273 | - |
|
274 | - // Perform the notifications and stuff *after* we've successfully saved, since the save can throw an OLE |
|
275 | - // and be rolled back. |
|
276 | - |
|
277 | - $deferToLog = $availableRequestStates[$action]['defertolog']; |
|
278 | - Logger::sentMail($database, $request, $messageBody); |
|
279 | - Logger::deferRequest($database, $request, $deferToLog); |
|
280 | - |
|
281 | - $this->getNotificationHelper()->requestDeferredWithMail($request); |
|
282 | - |
|
283 | - $deferTo = $availableRequestStates[$action]['deferto']; |
|
284 | - SessionAlert::success("Request {$request->getId()} deferred to $deferTo, sending an email."); |
|
285 | - } |
|
26 | + use RequestData; |
|
27 | + |
|
28 | + protected function main() |
|
29 | + { |
|
30 | + $database = $this->getDatabase(); |
|
31 | + |
|
32 | + $request = $this->getRequest($database); |
|
33 | + $currentUser = User::getCurrent($this->getDatabase()); |
|
34 | + |
|
35 | + if ($request->getStatus() === 'Closed') { |
|
36 | + throw new ApplicationLogicException('Request is already closed'); |
|
37 | + } |
|
38 | + |
|
39 | + // Dual-mode page |
|
40 | + if (WebRequest::wasPosted()) { |
|
41 | + $this->validateCSRFToken(); |
|
42 | + $this->doCustomClose($currentUser, $request, $database); |
|
43 | + |
|
44 | + $this->redirect(); |
|
45 | + } |
|
46 | + else { |
|
47 | + $this->assignCSRFToken(); |
|
48 | + $this->showCustomCloseForm($database, $request); |
|
49 | + } |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @param $database |
|
54 | + * |
|
55 | + * @return Request |
|
56 | + * @throws ApplicationLogicException |
|
57 | + */ |
|
58 | + protected function getRequest(PdoDatabase $database) |
|
59 | + { |
|
60 | + $requestId = WebRequest::getInt('request'); |
|
61 | + if ($requestId === null) { |
|
62 | + throw new ApplicationLogicException('Request ID not found'); |
|
63 | + } |
|
64 | + |
|
65 | + /** @var Request $request */ |
|
66 | + $request = Request::getById($requestId, $database); |
|
67 | + |
|
68 | + if ($request === false) { |
|
69 | + throw new ApplicationLogicException('Request not found'); |
|
70 | + } |
|
71 | + |
|
72 | + return $request; |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * @param PdoDatabase $database |
|
77 | + * |
|
78 | + * @return EmailTemplate|null |
|
79 | + */ |
|
80 | + protected function getTemplate(PdoDatabase $database) |
|
81 | + { |
|
82 | + $templateId = WebRequest::getInt('template'); |
|
83 | + if ($templateId === null) { |
|
84 | + return null; |
|
85 | + } |
|
86 | + |
|
87 | + /** @var EmailTemplate $template */ |
|
88 | + $template = EmailTemplate::getById($templateId, $database); |
|
89 | + if ($template === false || !$template->getActive()) { |
|
90 | + return null; |
|
91 | + } |
|
92 | + |
|
93 | + return $template; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @param $database |
|
98 | + * @param $request |
|
99 | + * |
|
100 | + * @throws Exception |
|
101 | + */ |
|
102 | + protected function showCustomCloseForm(PdoDatabase $database, Request $request) |
|
103 | + { |
|
104 | + $currentUser = User::getCurrent($database); |
|
105 | + $config = $this->getSiteConfiguration(); |
|
106 | + |
|
107 | + $allowedPrivateData = $this->isAllowedPrivateData($request, $currentUser); |
|
108 | + if (!$allowedPrivateData) { |
|
109 | + // we probably shouldn't be showing the user this form if they're not allowed to access private data... |
|
110 | + throw new AccessDeniedException($this->getSecurityManager()); |
|
111 | + } |
|
112 | + |
|
113 | + $template = $this->getTemplate($database); |
|
114 | + |
|
115 | + // Preload data |
|
116 | + $this->assign('defaultAction', ''); |
|
117 | + $this->assign('preloadText', ''); |
|
118 | + $this->assign('preloadTitle', ''); |
|
119 | + |
|
120 | + if ($template !== null) { |
|
121 | + $this->assign('defaultAction', $template->getDefaultAction()); |
|
122 | + $this->assign('preloadText', $template->getText()); |
|
123 | + $this->assign('preloadTitle', $template->getName()); |
|
124 | + } |
|
125 | + |
|
126 | + // Static data |
|
127 | + $this->assign('requeststates', $config->getRequestStates()); |
|
128 | + |
|
129 | + // request data |
|
130 | + $this->assign('requestId', $request->getIp()); |
|
131 | + $this->assign('updateVersion', $request->getUpdateVersion()); |
|
132 | + $this->setupBasicData($request, $config); |
|
133 | + $this->setupReservationDetails($request->getReserved(), $database, $currentUser); |
|
134 | + $this->setupPrivateData($request, $currentUser, $this->getSiteConfiguration(), $database); |
|
135 | + |
|
136 | + // IP location |
|
137 | + $trustedIp = $this->getXffTrustProvider()->getTrustedClientIp($request->getIp(), $request->getForwardedIp()); |
|
138 | + $this->assign('iplocation', $this->getLocationProvider()->getIpLocation($trustedIp)); |
|
139 | + |
|
140 | + // Confirmations |
|
141 | + $this->assign('confirmEmailAlreadySent', $this->checkEmailAlreadySent($request)); |
|
142 | + $this->assign('confirmReserveOverride', $this->checkReserveOverride($request, $currentUser)); |
|
143 | + |
|
144 | + $this->assign('canSkipCcMailingList', $this->barrierTest('skipCcMailingList', $currentUser)); |
|
145 | + |
|
146 | + // template |
|
147 | + $this->setTemplate('custom-close.tpl'); |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @param User $currentUser |
|
152 | + * @param Request $request |
|
153 | + * @param PdoDatabase $database |
|
154 | + * |
|
155 | + * @throws ApplicationLogicException |
|
156 | + */ |
|
157 | + protected function doCustomClose(User $currentUser, Request $request, PdoDatabase $database) |
|
158 | + { |
|
159 | + $messageBody = WebRequest::postString('msgbody'); |
|
160 | + if ($messageBody === null || trim($messageBody) === '') { |
|
161 | + throw new ApplicationLogicException('Message body cannot be blank'); |
|
162 | + } |
|
163 | + |
|
164 | + $ccMailingList = true; |
|
165 | + if ($this->barrierTest('skipCcMailingList', $currentUser)) { |
|
166 | + $ccMailingList = WebRequest::postBoolean('ccMailingList'); |
|
167 | + } |
|
168 | + |
|
169 | + if ($request->getStatus() === 'Closed') { |
|
170 | + throw new ApplicationLogicException('Request is already closed'); |
|
171 | + } |
|
172 | + |
|
173 | + if (!(WebRequest::postBoolean('confirmEmailAlreadySent') |
|
174 | + && WebRequest::postBoolean('confirmReserveOverride')) |
|
175 | + ) { |
|
176 | + throw new ApplicationLogicException('Not all confirmations checked'); |
|
177 | + } |
|
178 | + |
|
179 | + $action = WebRequest::postString('action'); |
|
180 | + $availableRequestStates = $this->getSiteConfiguration()->getRequestStates(); |
|
181 | + |
|
182 | + if ($action === EmailTemplate::CREATED || $action === EmailTemplate::NOT_CREATED) { |
|
183 | + // Close request |
|
184 | + $this->closeRequest($request, $database, $action, $messageBody); |
|
185 | + |
|
186 | + // Send the mail after the save, since save can be rolled back |
|
187 | + $this->sendMail($request, $messageBody, $currentUser, $ccMailingList); |
|
188 | + } |
|
189 | + else { |
|
190 | + if (array_key_exists($action, $availableRequestStates)) { |
|
191 | + // Defer to other state |
|
192 | + $this->deferRequest($request, $database, $action, $availableRequestStates, $messageBody); |
|
193 | + |
|
194 | + // Send the mail after the save, since save can be rolled back |
|
195 | + $this->sendMail($request, $messageBody, $currentUser, $ccMailingList); |
|
196 | + } |
|
197 | + else { |
|
198 | + $request->setReserved(null); |
|
199 | + $request->setUpdateVersion(WebRequest::postInt('updateversion')); |
|
200 | + $request->save(); |
|
201 | + |
|
202 | + // Perform the notifications and stuff *after* we've successfully saved, since the save can throw an OLE |
|
203 | + // and be rolled back. |
|
204 | + |
|
205 | + // Send mail |
|
206 | + $this->sendMail($request, $messageBody, $currentUser, $ccMailingList); |
|
207 | + |
|
208 | + Logger::sentMail($database, $request, $messageBody); |
|
209 | + Logger::unreserve($database, $request); |
|
210 | + |
|
211 | + $this->getNotificationHelper()->sentMail($request); |
|
212 | + SessionAlert::success("Sent mail to Request {$request->getId()}"); |
|
213 | + } |
|
214 | + } |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * @param Request $request |
|
219 | + * @param PdoDatabase $database |
|
220 | + * @param string $action |
|
221 | + * @param string $messageBody |
|
222 | + * |
|
223 | + * @throws Exception |
|
224 | + * @throws OptimisticLockFailedException |
|
225 | + */ |
|
226 | + protected function closeRequest(Request $request, PdoDatabase $database, $action, $messageBody) |
|
227 | + { |
|
228 | + $request->setStatus('Closed'); |
|
229 | + $request->setReserved(null); |
|
230 | + $request->setUpdateVersion(WebRequest::postInt('updateversion')); |
|
231 | + $request->save(); |
|
232 | + |
|
233 | + // Perform the notifications and stuff *after* we've successfully saved, since the save can throw an OLE and |
|
234 | + // be rolled back. |
|
235 | + |
|
236 | + if ($action == EmailTemplate::CREATED) { |
|
237 | + $logCloseType = 'custom-y'; |
|
238 | + $notificationCloseType = "Custom, Created"; |
|
239 | + } |
|
240 | + else { |
|
241 | + $logCloseType = 'custom-n'; |
|
242 | + $notificationCloseType = "Custom, Not Created"; |
|
243 | + } |
|
244 | + |
|
245 | + Logger::closeRequest($database, $request, $logCloseType, $messageBody); |
|
246 | + $this->getNotificationHelper()->requestClosed($request, $notificationCloseType); |
|
247 | + |
|
248 | + $requestName = htmlentities($request->getName(), ENT_COMPAT, 'UTF-8'); |
|
249 | + SessionAlert::success("Request {$request->getId()} ({$requestName}) closed as {$notificationCloseType}."); |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * @param Request $request |
|
254 | + * @param PdoDatabase $database |
|
255 | + * @param string $action |
|
256 | + * @param $availableRequestStates |
|
257 | + * @param string $messageBody |
|
258 | + * |
|
259 | + * @throws Exception |
|
260 | + * @throws OptimisticLockFailedException |
|
261 | + */ |
|
262 | + protected function deferRequest( |
|
263 | + Request $request, |
|
264 | + PdoDatabase $database, |
|
265 | + $action, |
|
266 | + $availableRequestStates, |
|
267 | + $messageBody |
|
268 | + ) { |
|
269 | + $request->setStatus($action); |
|
270 | + $request->setReserved(null); |
|
271 | + $request->setUpdateVersion(WebRequest::postInt('updateversion')); |
|
272 | + $request->save(); |
|
273 | + |
|
274 | + // Perform the notifications and stuff *after* we've successfully saved, since the save can throw an OLE |
|
275 | + // and be rolled back. |
|
276 | + |
|
277 | + $deferToLog = $availableRequestStates[$action]['defertolog']; |
|
278 | + Logger::sentMail($database, $request, $messageBody); |
|
279 | + Logger::deferRequest($database, $request, $deferToLog); |
|
280 | + |
|
281 | + $this->getNotificationHelper()->requestDeferredWithMail($request); |
|
282 | + |
|
283 | + $deferTo = $availableRequestStates[$action]['deferto']; |
|
284 | + SessionAlert::success("Request {$request->getId()} deferred to $deferTo, sending an email."); |
|
285 | + } |
|
286 | 286 | } |
@@ -12,18 +12,18 @@ |
||
12 | 12 | |
13 | 13 | class PageRegisterOption extends InternalPageBase |
14 | 14 | { |
15 | - /** |
|
16 | - * Main function for this page, when no specific actions are called. |
|
17 | - * @return void |
|
18 | - */ |
|
19 | - protected function main() |
|
20 | - { |
|
21 | - $this->assign('allowRegistration', $this->getSiteConfiguration()->isRegistrationAllowed()); |
|
22 | - $this->setTemplate('registration/option.tpl'); |
|
23 | - } |
|
15 | + /** |
|
16 | + * Main function for this page, when no specific actions are called. |
|
17 | + * @return void |
|
18 | + */ |
|
19 | + protected function main() |
|
20 | + { |
|
21 | + $this->assign('allowRegistration', $this->getSiteConfiguration()->isRegistrationAllowed()); |
|
22 | + $this->setTemplate('registration/option.tpl'); |
|
23 | + } |
|
24 | 24 | |
25 | - protected function isProtectedPage() |
|
26 | - { |
|
27 | - return false; |
|
28 | - } |
|
25 | + protected function isProtectedPage() |
|
26 | + { |
|
27 | + return false; |
|
28 | + } |
|
29 | 29 | } |
@@ -21,204 +21,204 @@ |
||
21 | 21 | |
22 | 22 | abstract class PageRegisterBase extends InternalPageBase |
23 | 23 | { |
24 | - /** |
|
25 | - * Main function for this page, when no specific actions are called. |
|
26 | - * @throws AccessDeniedException |
|
27 | - */ |
|
28 | - protected function main() |
|
29 | - { |
|
30 | - $useOAuthSignup = $this->getSiteConfiguration()->getUseOAuthSignup(); |
|
31 | - if (! $this->getSiteConfiguration()->isRegistrationAllowed()) { |
|
32 | - throw new AccessDeniedException(); |
|
33 | - } |
|
34 | - |
|
35 | - // Dual-mode page |
|
36 | - if (WebRequest::wasPosted()) { |
|
37 | - $this->validateCSRFToken(); |
|
38 | - |
|
39 | - try { |
|
40 | - $this->handlePost($useOAuthSignup); |
|
41 | - } |
|
42 | - catch (ApplicationLogicException $ex) { |
|
43 | - SessionAlert::error($ex->getMessage()); |
|
44 | - $this->redirect('register'); |
|
45 | - } |
|
46 | - } |
|
47 | - else { |
|
48 | - $this->assignCSRFToken(); |
|
49 | - $this->assign("useOAuthSignup", $useOAuthSignup); |
|
50 | - $this->setTemplate($this->getRegistrationTemplate()); |
|
51 | - } |
|
52 | - } |
|
53 | - |
|
54 | - protected abstract function getRegistrationTemplate(); |
|
55 | - |
|
56 | - protected function isProtectedPage() |
|
57 | - { |
|
58 | - return false; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * @param string $emailAddress |
|
63 | - * |
|
64 | - * @throws ApplicationLogicException |
|
65 | - */ |
|
66 | - protected function validateUniqueEmail($emailAddress) |
|
67 | - { |
|
68 | - $query = 'SELECT COUNT(id) FROM user WHERE email = :email'; |
|
69 | - $statement = $this->getDatabase()->prepare($query); |
|
70 | - $statement->execute(array(':email' => $emailAddress)); |
|
71 | - |
|
72 | - if ($statement->fetchColumn() > 0) { |
|
73 | - throw new ApplicationLogicException('That email address is already in use on this system.'); |
|
74 | - } |
|
75 | - |
|
76 | - $statement->closeCursor(); |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @param $emailAddress |
|
81 | - * @param $password |
|
82 | - * @param $username |
|
83 | - * @param $useOAuthSignup |
|
84 | - * @param $confirmationId |
|
85 | - * @param $onwikiUsername |
|
86 | - * |
|
87 | - * @throws ApplicationLogicException |
|
88 | - */ |
|
89 | - protected function validateRequest( |
|
90 | - $emailAddress, |
|
91 | - $password, |
|
92 | - $username, |
|
93 | - $useOAuthSignup, |
|
94 | - $confirmationId, |
|
95 | - $onwikiUsername |
|
96 | - ) { |
|
97 | - if (!WebRequest::postBoolean('guidelines')) { |
|
98 | - throw new ApplicationLogicException('You must read the interface guidelines before your request may be submitted.'); |
|
99 | - } |
|
100 | - |
|
101 | - $this->validateGeneralInformation($emailAddress, $password, $username); |
|
102 | - $this->validateUniqueEmail($emailAddress); |
|
103 | - $this->validateNonOAuthFields($useOAuthSignup, $confirmationId, $onwikiUsername); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @param $useOAuthSignup |
|
108 | - * @param $confirmationId |
|
109 | - * @param $onwikiUsername |
|
110 | - * |
|
111 | - * @throws ApplicationLogicException |
|
112 | - */ |
|
113 | - protected function validateNonOAuthFields($useOAuthSignup, $confirmationId, $onwikiUsername) |
|
114 | - { |
|
115 | - if (!$useOAuthSignup) { |
|
116 | - if ($confirmationId === null || $confirmationId <= 0) { |
|
117 | - throw new ApplicationLogicException('Please enter the revision id of your confirmation edit.'); |
|
118 | - } |
|
119 | - |
|
120 | - if ($onwikiUsername === null) { |
|
121 | - throw new ApplicationLogicException('Please specify your on-wiki username.'); |
|
122 | - } |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @param $emailAddress |
|
128 | - * @param $password |
|
129 | - * @param $username |
|
130 | - * |
|
131 | - * @throws ApplicationLogicException |
|
132 | - */ |
|
133 | - protected function validateGeneralInformation($emailAddress, $password, $username) |
|
134 | - { |
|
135 | - if ($emailAddress === null) { |
|
136 | - throw new ApplicationLogicException('Your email address appears to be invalid!'); |
|
137 | - } |
|
138 | - |
|
139 | - if ($password !== WebRequest::postString('pass2')) { |
|
140 | - throw new ApplicationLogicException('Your passwords did not match, please try again.'); |
|
141 | - } |
|
142 | - |
|
143 | - if (User::getByUsername($username, $this->getDatabase()) !== false) { |
|
144 | - throw new ApplicationLogicException('That username is already in use on this system.'); |
|
145 | - } |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * @param $useOAuthSignup |
|
150 | - * |
|
151 | - * @throws ApplicationLogicException |
|
152 | - * @throws \Exception |
|
153 | - */ |
|
154 | - protected function handlePost($useOAuthSignup) |
|
155 | - { |
|
156 | - // Get the data |
|
157 | - $emailAddress = WebRequest::postEmail('email'); |
|
158 | - $password = WebRequest::postString('pass'); |
|
159 | - $username = WebRequest::postString('name'); |
|
160 | - |
|
161 | - // Only set if OAuth is disabled |
|
162 | - $confirmationId = WebRequest::postInt('conf_revid'); |
|
163 | - $onwikiUsername = WebRequest::postString('wname'); |
|
164 | - |
|
165 | - // Do some validation |
|
166 | - $this->validateRequest($emailAddress, $password, $username, $useOAuthSignup, $confirmationId, |
|
167 | - $onwikiUsername); |
|
168 | - |
|
169 | - $database = $this->getDatabase(); |
|
170 | - |
|
171 | - $user = new User(); |
|
172 | - $user->setDatabase($database); |
|
173 | - |
|
174 | - $user->setUsername($username); |
|
175 | - $user->setEmail($emailAddress); |
|
176 | - |
|
177 | - if (!$useOAuthSignup) { |
|
178 | - $user->setOnWikiName($onwikiUsername); |
|
179 | - $user->setConfirmationDiff($confirmationId); |
|
180 | - } |
|
181 | - |
|
182 | - $user->save(); |
|
183 | - |
|
184 | - $passwordCredentialProvider = new PasswordCredentialProvider($database, $this->getSiteConfiguration()); |
|
185 | - $passwordCredentialProvider->setCredential($user, 1, $password); |
|
186 | - |
|
187 | - $defaultRole = $this->getDefaultRole(); |
|
188 | - |
|
189 | - $role = new UserRole(); |
|
190 | - $role->setDatabase($database); |
|
191 | - $role->setUser($user->getId()); |
|
192 | - $role->setRole($defaultRole); |
|
193 | - $role->save(); |
|
194 | - |
|
195 | - // Log now to get the signup date. |
|
196 | - Logger::newUser($database, $user); |
|
197 | - Logger::userRolesEdited($database, $user, 'Registration', array($defaultRole), array()); |
|
198 | - |
|
199 | - if ($useOAuthSignup) { |
|
200 | - $oauthProtocolHelper = $this->getOAuthProtocolHelper(); |
|
201 | - $oauth = new OAuthUserHelper($user, $database, $oauthProtocolHelper, $this->getSiteConfiguration()); |
|
202 | - |
|
203 | - $authoriseUrl = $oauth->getRequestToken(); |
|
204 | - WebRequest::setOAuthPartialLogin($user); |
|
205 | - $this->redirectUrl($authoriseUrl); |
|
206 | - } |
|
207 | - else { |
|
208 | - // only notify if we're not using the oauth signup. |
|
209 | - $this->getNotificationHelper()->userNew($user); |
|
210 | - WebRequest::setLoggedInUser($user); |
|
211 | - $this->redirect('preferences'); |
|
212 | - } |
|
213 | - } |
|
214 | - |
|
215 | - protected abstract function getDefaultRole(); |
|
216 | - |
|
217 | - /** |
|
218 | - * Entry point for registration complete |
|
219 | - */ |
|
220 | - protected function done() |
|
221 | - { |
|
222 | - $this->setTemplate('registration/alert-registrationcomplete.tpl'); |
|
223 | - } |
|
24 | + /** |
|
25 | + * Main function for this page, when no specific actions are called. |
|
26 | + * @throws AccessDeniedException |
|
27 | + */ |
|
28 | + protected function main() |
|
29 | + { |
|
30 | + $useOAuthSignup = $this->getSiteConfiguration()->getUseOAuthSignup(); |
|
31 | + if (! $this->getSiteConfiguration()->isRegistrationAllowed()) { |
|
32 | + throw new AccessDeniedException(); |
|
33 | + } |
|
34 | + |
|
35 | + // Dual-mode page |
|
36 | + if (WebRequest::wasPosted()) { |
|
37 | + $this->validateCSRFToken(); |
|
38 | + |
|
39 | + try { |
|
40 | + $this->handlePost($useOAuthSignup); |
|
41 | + } |
|
42 | + catch (ApplicationLogicException $ex) { |
|
43 | + SessionAlert::error($ex->getMessage()); |
|
44 | + $this->redirect('register'); |
|
45 | + } |
|
46 | + } |
|
47 | + else { |
|
48 | + $this->assignCSRFToken(); |
|
49 | + $this->assign("useOAuthSignup", $useOAuthSignup); |
|
50 | + $this->setTemplate($this->getRegistrationTemplate()); |
|
51 | + } |
|
52 | + } |
|
53 | + |
|
54 | + protected abstract function getRegistrationTemplate(); |
|
55 | + |
|
56 | + protected function isProtectedPage() |
|
57 | + { |
|
58 | + return false; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * @param string $emailAddress |
|
63 | + * |
|
64 | + * @throws ApplicationLogicException |
|
65 | + */ |
|
66 | + protected function validateUniqueEmail($emailAddress) |
|
67 | + { |
|
68 | + $query = 'SELECT COUNT(id) FROM user WHERE email = :email'; |
|
69 | + $statement = $this->getDatabase()->prepare($query); |
|
70 | + $statement->execute(array(':email' => $emailAddress)); |
|
71 | + |
|
72 | + if ($statement->fetchColumn() > 0) { |
|
73 | + throw new ApplicationLogicException('That email address is already in use on this system.'); |
|
74 | + } |
|
75 | + |
|
76 | + $statement->closeCursor(); |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @param $emailAddress |
|
81 | + * @param $password |
|
82 | + * @param $username |
|
83 | + * @param $useOAuthSignup |
|
84 | + * @param $confirmationId |
|
85 | + * @param $onwikiUsername |
|
86 | + * |
|
87 | + * @throws ApplicationLogicException |
|
88 | + */ |
|
89 | + protected function validateRequest( |
|
90 | + $emailAddress, |
|
91 | + $password, |
|
92 | + $username, |
|
93 | + $useOAuthSignup, |
|
94 | + $confirmationId, |
|
95 | + $onwikiUsername |
|
96 | + ) { |
|
97 | + if (!WebRequest::postBoolean('guidelines')) { |
|
98 | + throw new ApplicationLogicException('You must read the interface guidelines before your request may be submitted.'); |
|
99 | + } |
|
100 | + |
|
101 | + $this->validateGeneralInformation($emailAddress, $password, $username); |
|
102 | + $this->validateUniqueEmail($emailAddress); |
|
103 | + $this->validateNonOAuthFields($useOAuthSignup, $confirmationId, $onwikiUsername); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @param $useOAuthSignup |
|
108 | + * @param $confirmationId |
|
109 | + * @param $onwikiUsername |
|
110 | + * |
|
111 | + * @throws ApplicationLogicException |
|
112 | + */ |
|
113 | + protected function validateNonOAuthFields($useOAuthSignup, $confirmationId, $onwikiUsername) |
|
114 | + { |
|
115 | + if (!$useOAuthSignup) { |
|
116 | + if ($confirmationId === null || $confirmationId <= 0) { |
|
117 | + throw new ApplicationLogicException('Please enter the revision id of your confirmation edit.'); |
|
118 | + } |
|
119 | + |
|
120 | + if ($onwikiUsername === null) { |
|
121 | + throw new ApplicationLogicException('Please specify your on-wiki username.'); |
|
122 | + } |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @param $emailAddress |
|
128 | + * @param $password |
|
129 | + * @param $username |
|
130 | + * |
|
131 | + * @throws ApplicationLogicException |
|
132 | + */ |
|
133 | + protected function validateGeneralInformation($emailAddress, $password, $username) |
|
134 | + { |
|
135 | + if ($emailAddress === null) { |
|
136 | + throw new ApplicationLogicException('Your email address appears to be invalid!'); |
|
137 | + } |
|
138 | + |
|
139 | + if ($password !== WebRequest::postString('pass2')) { |
|
140 | + throw new ApplicationLogicException('Your passwords did not match, please try again.'); |
|
141 | + } |
|
142 | + |
|
143 | + if (User::getByUsername($username, $this->getDatabase()) !== false) { |
|
144 | + throw new ApplicationLogicException('That username is already in use on this system.'); |
|
145 | + } |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * @param $useOAuthSignup |
|
150 | + * |
|
151 | + * @throws ApplicationLogicException |
|
152 | + * @throws \Exception |
|
153 | + */ |
|
154 | + protected function handlePost($useOAuthSignup) |
|
155 | + { |
|
156 | + // Get the data |
|
157 | + $emailAddress = WebRequest::postEmail('email'); |
|
158 | + $password = WebRequest::postString('pass'); |
|
159 | + $username = WebRequest::postString('name'); |
|
160 | + |
|
161 | + // Only set if OAuth is disabled |
|
162 | + $confirmationId = WebRequest::postInt('conf_revid'); |
|
163 | + $onwikiUsername = WebRequest::postString('wname'); |
|
164 | + |
|
165 | + // Do some validation |
|
166 | + $this->validateRequest($emailAddress, $password, $username, $useOAuthSignup, $confirmationId, |
|
167 | + $onwikiUsername); |
|
168 | + |
|
169 | + $database = $this->getDatabase(); |
|
170 | + |
|
171 | + $user = new User(); |
|
172 | + $user->setDatabase($database); |
|
173 | + |
|
174 | + $user->setUsername($username); |
|
175 | + $user->setEmail($emailAddress); |
|
176 | + |
|
177 | + if (!$useOAuthSignup) { |
|
178 | + $user->setOnWikiName($onwikiUsername); |
|
179 | + $user->setConfirmationDiff($confirmationId); |
|
180 | + } |
|
181 | + |
|
182 | + $user->save(); |
|
183 | + |
|
184 | + $passwordCredentialProvider = new PasswordCredentialProvider($database, $this->getSiteConfiguration()); |
|
185 | + $passwordCredentialProvider->setCredential($user, 1, $password); |
|
186 | + |
|
187 | + $defaultRole = $this->getDefaultRole(); |
|
188 | + |
|
189 | + $role = new UserRole(); |
|
190 | + $role->setDatabase($database); |
|
191 | + $role->setUser($user->getId()); |
|
192 | + $role->setRole($defaultRole); |
|
193 | + $role->save(); |
|
194 | + |
|
195 | + // Log now to get the signup date. |
|
196 | + Logger::newUser($database, $user); |
|
197 | + Logger::userRolesEdited($database, $user, 'Registration', array($defaultRole), array()); |
|
198 | + |
|
199 | + if ($useOAuthSignup) { |
|
200 | + $oauthProtocolHelper = $this->getOAuthProtocolHelper(); |
|
201 | + $oauth = new OAuthUserHelper($user, $database, $oauthProtocolHelper, $this->getSiteConfiguration()); |
|
202 | + |
|
203 | + $authoriseUrl = $oauth->getRequestToken(); |
|
204 | + WebRequest::setOAuthPartialLogin($user); |
|
205 | + $this->redirectUrl($authoriseUrl); |
|
206 | + } |
|
207 | + else { |
|
208 | + // only notify if we're not using the oauth signup. |
|
209 | + $this->getNotificationHelper()->userNew($user); |
|
210 | + WebRequest::setLoggedInUser($user); |
|
211 | + $this->redirect('preferences'); |
|
212 | + } |
|
213 | + } |
|
214 | + |
|
215 | + protected abstract function getDefaultRole(); |
|
216 | + |
|
217 | + /** |
|
218 | + * Entry point for registration complete |
|
219 | + */ |
|
220 | + protected function done() |
|
221 | + { |
|
222 | + $this->setTemplate('registration/alert-registrationcomplete.tpl'); |
|
223 | + } |
|
224 | 224 | } |
@@ -28,7 +28,7 @@ |
||
28 | 28 | protected function main() |
29 | 29 | { |
30 | 30 | $useOAuthSignup = $this->getSiteConfiguration()->getUseOAuthSignup(); |
31 | - if (! $this->getSiteConfiguration()->isRegistrationAllowed()) { |
|
31 | + if (!$this->getSiteConfiguration()->isRegistrationAllowed()) { |
|
32 | 32 | throw new AccessDeniedException(); |
33 | 33 | } |
34 | 34 |
@@ -18,34 +18,34 @@ |
||
18 | 18 | */ |
19 | 19 | class HelpAction extends ApiPageBase implements IApiAction |
20 | 20 | { |
21 | - public function executeApiAction(DOMElement $apiDocument) |
|
22 | - { |
|
23 | - $helpElement = $this->getHelpElement(); |
|
24 | - $apiDocument->appendChild($helpElement); |
|
25 | - |
|
26 | - return $apiDocument; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Gets the help information |
|
31 | - * @return DOMElement |
|
32 | - */ |
|
33 | - protected function getHelpElement() |
|
34 | - { |
|
21 | + public function executeApiAction(DOMElement $apiDocument) |
|
22 | + { |
|
23 | + $helpElement = $this->getHelpElement(); |
|
24 | + $apiDocument->appendChild($helpElement); |
|
25 | + |
|
26 | + return $apiDocument; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Gets the help information |
|
31 | + * @return DOMElement |
|
32 | + */ |
|
33 | + protected function getHelpElement() |
|
34 | + { |
|
35 | 35 | $helpInfo = "API help can be found at https://github.com/enwikipedia-acc/waca/wiki/API"; |
36 | 36 | |
37 | - $help = $this->document->createElement("help"); |
|
38 | - $helptext = $this->document->createElement("info", $helpInfo); |
|
39 | - $helpactions = $this->document->createElement("actions"); |
|
37 | + $help = $this->document->createElement("help"); |
|
38 | + $helptext = $this->document->createElement("info", $helpInfo); |
|
39 | + $helpactions = $this->document->createElement("actions"); |
|
40 | 40 | |
41 | - foreach (ApiRequestRouter::getActionList() as $action) { |
|
42 | - $actionElement = $this->document->createElement("action", $action); |
|
43 | - $helpactions->appendChild($actionElement); |
|
44 | - } |
|
41 | + foreach (ApiRequestRouter::getActionList() as $action) { |
|
42 | + $actionElement = $this->document->createElement("action", $action); |
|
43 | + $helpactions->appendChild($actionElement); |
|
44 | + } |
|
45 | 45 | |
46 | - $help->appendChild($helptext); |
|
47 | - $help->appendChild($helpactions); |
|
46 | + $help->appendChild($helptext); |
|
47 | + $help->appendChild($helpactions); |
|
48 | 48 | |
49 | - return $help; |
|
50 | - } |
|
49 | + return $help; |
|
50 | + } |
|
51 | 51 | } |
@@ -15,73 +15,73 @@ 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 < ? AND approvaldate.timestamp < ?'; |
|
84 | - $this->joinClause .= <<<'SQLFRAG' |
|
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 < ? AND approvaldate.timestamp < ?'; |
|
84 | + $this->joinClause .= <<<'SQLFRAG' |
|
85 | 85 | LEFT JOIN ( |
86 | 86 | SELECT objectid, MAX(timestamp) timestamp |
87 | 87 | FROM log |
@@ -89,16 +89,16 @@ discard block |
||
89 | 89 | GROUP BY objectid |
90 | 90 | ) approvaldate ON approvaldate.objectid = origin.id |
91 | 91 | SQLFRAG; |
92 | - $formattedDate = $instant->format("Y-m-d H:i:s"); |
|
93 | - $this->parameterList[] = $formattedDate; |
|
94 | - $this->parameterList[] = $formattedDate; |
|
92 | + $formattedDate = $instant->format("Y-m-d H:i:s"); |
|
93 | + $this->parameterList[] = $formattedDate; |
|
94 | + $this->parameterList[] = $formattedDate; |
|
95 | 95 | |
96 | - return $this; |
|
97 | - } |
|
96 | + return $this; |
|
97 | + } |
|
98 | 98 | |
99 | - public function getRoleMap(&$roleMap) |
|
100 | - { |
|
101 | - $query = <<<SQL |
|
99 | + public function getRoleMap(&$roleMap) |
|
100 | + { |
|
101 | + $query = <<<SQL |
|
102 | 102 | SELECT /* UserSearchHelper/roleMap */ |
103 | 103 | r.user user |
104 | 104 | , group_concat(r.role SEPARATOR ', ') roles |
@@ -107,22 +107,22 @@ discard block |
||
107 | 107 | GROUP BY r.user |
108 | 108 | SQL; |
109 | 109 | |
110 | - $statement = $this->database->prepare($query); |
|
111 | - $statement->execute($this->parameterList); |
|
110 | + $statement = $this->database->prepare($query); |
|
111 | + $statement->execute($this->parameterList); |
|
112 | 112 | |
113 | - $roleMap = array(); |
|
114 | - foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
115 | - $roleMap[$row['user']] = $row['roles']; |
|
116 | - } |
|
113 | + $roleMap = array(); |
|
114 | + foreach ($statement->fetchAll(PDO::FETCH_ASSOC) as $row) { |
|
115 | + $roleMap[$row['user']] = $row['roles']; |
|
116 | + } |
|
117 | 117 | |
118 | - return $this; |
|
119 | - } |
|
118 | + return $this; |
|
119 | + } |
|
120 | 120 | |
121 | - public function withReservedRequest() |
|
122 | - { |
|
123 | - $this->joinClause = ' INNER JOIN request req ON req.reserved = origin.id'; |
|
124 | - $this->groupByClause = ' GROUP BY origin.id, origin.username'; |
|
121 | + public function withReservedRequest() |
|
122 | + { |
|
123 | + $this->joinClause = ' INNER JOIN request req ON req.reserved = origin.id'; |
|
124 | + $this->groupByClause = ' GROUP BY origin.id, origin.username'; |
|
125 | 125 | |
126 | - return $this->fetchMap('username'); |
|
127 | - } |
|
126 | + return $this->fetchMap('username'); |
|
127 | + } |
|
128 | 128 | } |
@@ -14,30 +14,30 @@ discard block |
||
14 | 14 | |
15 | 15 | class PrecacheGeolocationTask extends ConsoleTaskBase |
16 | 16 | { |
17 | - public function execute() |
|
18 | - { |
|
19 | - $database = $this->getDatabase(); |
|
20 | - $locationProvider = $this->getLocationProvider(); |
|
17 | + public function execute() |
|
18 | + { |
|
19 | + $database = $this->getDatabase(); |
|
20 | + $locationProvider = $this->getLocationProvider(); |
|
21 | 21 | |
22 | - while (true) { |
|
23 | - echo "Beginning txn\n"; |
|
24 | - $database->beginTransaction(); |
|
22 | + while (true) { |
|
23 | + echo "Beginning txn\n"; |
|
24 | + $database->beginTransaction(); |
|
25 | 25 | |
26 | - try { |
|
27 | - echo ". Fetching data...\n"; |
|
26 | + try { |
|
27 | + echo ". Fetching data...\n"; |
|
28 | 28 | |
29 | - // fetch a bunch of un-geolocated IPs from the database. |
|
30 | - // Note we have to parse the forwardedip field in the database so we can test against the geolocation |
|
31 | - // table. |
|
32 | - // |
|
33 | - // This guarantees we get ten unlocated IPs back, unless there actually aren't 10 available. |
|
34 | - // |
|
35 | - // Alternatives include downloading a small set of forwarded IPs, splitting it in PHP, constructing an |
|
36 | - // IN() clause dynamically, sending that back to the database to check if there are geolocation entries, |
|
37 | - // then repeating until we have 10 to process - and the fact that we'd have to potentially retrieve all |
|
38 | - // IPs from the database before we find any at all. This way keeps all of that legwork in the database, |
|
39 | - // at the cost of a more complex query. |
|
40 | - $statement = $database->query(<<<SQL |
|
29 | + // fetch a bunch of un-geolocated IPs from the database. |
|
30 | + // Note we have to parse the forwardedip field in the database so we can test against the geolocation |
|
31 | + // table. |
|
32 | + // |
|
33 | + // This guarantees we get ten unlocated IPs back, unless there actually aren't 10 available. |
|
34 | + // |
|
35 | + // Alternatives include downloading a small set of forwarded IPs, splitting it in PHP, constructing an |
|
36 | + // IN() clause dynamically, sending that back to the database to check if there are geolocation entries, |
|
37 | + // then repeating until we have 10 to process - and the fact that we'd have to potentially retrieve all |
|
38 | + // IPs from the database before we find any at all. This way keeps all of that legwork in the database, |
|
39 | + // at the cost of a more complex query. |
|
40 | + $statement = $database->query(<<<SQL |
|
41 | 41 | SELECT /* PrecacheGeolocationTask */ p.prox |
42 | 42 | FROM ( |
43 | 43 | SELECT trim(substring_index(substring_index(r.forwardedip, ',', n.n), ',', -1)) prox |
@@ -54,42 +54,42 @@ discard block |
||
54 | 54 | WHERE NOT EXISTS (SELECT 1 FROM geolocation g WHERE g.address = p.prox FOR UPDATE) |
55 | 55 | LIMIT 10; |
56 | 56 | SQL |
57 | - ); |
|
57 | + ); |
|
58 | 58 | |
59 | - $missingIps = $statement->fetchAll(PDO::FETCH_COLUMN); |
|
59 | + $missingIps = $statement->fetchAll(PDO::FETCH_COLUMN); |
|
60 | 60 | |
61 | - $count = count($missingIps); |
|
62 | - if ($count === 0) { |
|
63 | - echo ". Found nothing to do.\n"; |
|
64 | - break; |
|
65 | - } |
|
61 | + $count = count($missingIps); |
|
62 | + if ($count === 0) { |
|
63 | + echo ". Found nothing to do.\n"; |
|
64 | + break; |
|
65 | + } |
|
66 | 66 | |
67 | - echo ". Picked {$count} IP addresses\n"; |
|
67 | + echo ". Picked {$count} IP addresses\n"; |
|
68 | 68 | |
69 | - foreach ($missingIps as $ip) { |
|
70 | - echo ". . Getting location for {$ip}...\n"; |
|
71 | - $data = json_encode($locationProvider->getIpLocation($ip)); |
|
72 | - echo ". . . {$data}\n"; |
|
73 | - } |
|
69 | + foreach ($missingIps as $ip) { |
|
70 | + echo ". . Getting location for {$ip}...\n"; |
|
71 | + $data = json_encode($locationProvider->getIpLocation($ip)); |
|
72 | + echo ". . . {$data}\n"; |
|
73 | + } |
|
74 | 74 | |
75 | - echo ". IP location fetch complete.\n"; |
|
76 | - $database->commit(); |
|
77 | - echo ". Committed txn.\n"; |
|
78 | - } |
|
79 | - catch (Exception $ex) { |
|
80 | - echo ". Encountered exception: " . $ex->getMessage() . "\n"; |
|
81 | - $database->rollBack(); |
|
82 | - echo ". Rolled back txn\n"; |
|
83 | - throw $ex; |
|
84 | - } |
|
85 | - finally { |
|
86 | - if ($database->hasActiveTransaction()) { |
|
87 | - $database->rollBack(); |
|
88 | - echo ". Rolled back txn\n"; |
|
89 | - } |
|
90 | - } |
|
91 | - } |
|
75 | + echo ". IP location fetch complete.\n"; |
|
76 | + $database->commit(); |
|
77 | + echo ". Committed txn.\n"; |
|
78 | + } |
|
79 | + catch (Exception $ex) { |
|
80 | + echo ". Encountered exception: " . $ex->getMessage() . "\n"; |
|
81 | + $database->rollBack(); |
|
82 | + echo ". Rolled back txn\n"; |
|
83 | + throw $ex; |
|
84 | + } |
|
85 | + finally { |
|
86 | + if ($database->hasActiveTransaction()) { |
|
87 | + $database->rollBack(); |
|
88 | + echo ". Rolled back txn\n"; |
|
89 | + } |
|
90 | + } |
|
91 | + } |
|
92 | 92 | |
93 | - echo "Done.\n"; |
|
94 | - } |
|
93 | + echo "Done.\n"; |
|
94 | + } |
|
95 | 95 | } |
96 | 96 | \ No newline at end of file |
@@ -77,7 +77,7 @@ |
||
77 | 77 | echo ". Committed txn.\n"; |
78 | 78 | } |
79 | 79 | catch (Exception $ex) { |
80 | - echo ". Encountered exception: " . $ex->getMessage() . "\n"; |
|
80 | + echo ". Encountered exception: ".$ex->getMessage()."\n"; |
|
81 | 81 | $database->rollBack(); |
82 | 82 | echo ". Rolled back txn\n"; |
83 | 83 | throw $ex; |