Completed
Branch newinternal (cdd491)
by Simon
04:39
created

PageViewRequest   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 0%

Importance

Changes 16
Bugs 3 Features 3
Metric Value
c 16
b 3
f 3
dl 0
loc 220
rs 10
ccs 0
cts 148
cp 0
wmc 27
lcom 1
cbo 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
B main() 0 50 6
A setupTitle() 0 14 3
B setupGeneralData() 0 34 1
F setupLogData() 0 76 15
A setupUsernameData() 0 16 2
1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
namespace Waca\Pages;
10
11
use Exception;
12
use Waca\DataObjects\Comment;
13
use Waca\DataObjects\EmailTemplate;
14
use Waca\DataObjects\Log;
15
use Waca\DataObjects\Request;
16
use Waca\DataObjects\User;
17
use Waca\Exceptions\ApplicationLogicException;
18
use Waca\Fragments\RequestData;
19
use Waca\Helpers\LogHelper;
20
use Waca\PdoDatabase;
21
use Waca\Tasks\InternalPageBase;
22
use Waca\WebRequest;
23
24
class PageViewRequest extends InternalPageBase
25
{
26
	use RequestData;
27
28
	const PRIVATE_DATA_BARRIER = 'privateData';
29
	const SET_BAN_BARRIER = 'setBan';
30
	const STATUS_SYMBOL_OPEN = '&#x2610';
31
	const STATUS_SYMBOL_ACCEPTED = '&#x2611';
32
	const STATUS_SYMBOL_REJECTED = '&#x2612';
33
34
	/**
35
	 * Main function for this page, when no specific actions are called.
36
	 * @throws ApplicationLogicException
37
	 */
38
	protected function main()
39
	{
40
		// set up csrf protection
41
		$this->assignCSRFToken();
42
43
		// get some useful objects
44
		$database = $this->getDatabase();
45
		$request = $this->getRequest($database, WebRequest::getInt('id'));
46
		$config = $this->getSiteConfiguration();
47
		$currentUser = User::getCurrent($database);
48
49
		// Test we should be able to look at this request
50
		if ($config->getEmailConfirmationEnabled()) {
51
			if ($request->getEmailConfirm() !== 'Confirmed') {
52
				// Not allowed to look at this yet.
53
				throw new ApplicationLogicException('The email address has not yet been confirmed for this request.');
54
			}
55
		}
56
57
		$this->setupBasicData($request, $config);
58
59
		$this->setupUsernameData($request);
60
61
		$this->setupTitle($request);
62
63
		$this->setupReservationDetails($request->getReserved(), $database, $currentUser);
64
		$this->setupGeneralData($database);
65
66
		$this->assign('requestDataCleared', false);
67
		if ($request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()) {
68
			$this->assign('requestDataCleared', true);
69
		}
70
71
		$allowedPrivateData = $this->isAllowedPrivateData($request, $currentUser);
72
73
		$this->setupLogData($request, $database);
74
75
		if ($allowedPrivateData) {
76
			$this->setTemplate('view-request/main-with-data.tpl');
77
			$this->setupPrivateData($request, $currentUser, $this->getSiteConfiguration(), $database);
78
79
			if ($currentUser->isCheckuser()) {
80
				$this->setTemplate('view-request/main-with-checkuser-data.tpl');
81
				$this->setupCheckUserData($request);
82
			}
83
		}
84
		else {
85
			$this->setTemplate('view-request/main.tpl');
86
		}
87
	}
88
89
	/**
90
	 * @param Request $request
91
	 */
92
	protected function setupTitle(Request $request)
93
	{
94
		$statusSymbol = self::STATUS_SYMBOL_OPEN;
95
		if ($request->getStatus() === 'Closed') {
96
			if ($request->getWasCreated()) {
97
				$statusSymbol = self::STATUS_SYMBOL_ACCEPTED;
98
			}
99
			else {
100
				$statusSymbol = self::STATUS_SYMBOL_REJECTED;
101
			}
102
		}
103
104
		$this->setHtmlTitle($statusSymbol . ' #' . $request->getId());
105
	}
106
107
	/**
108
	 * Sets up data unrelated to the request, such as the email template information
109
	 *
110
	 * @param PdoDatabase $database
111
	 */
112
	protected function setupGeneralData(PdoDatabase $database)
113
	{
114
		$config = $this->getSiteConfiguration();
115
116
		$this->assign('createAccountReason', 'Requested account at [[WP:ACC]], request #');
117
118
		$this->assign('defaultRequestState', $config->getDefaultRequestStateKey());
119
120
		$this->assign('requestStates', $config->getRequestStates());
121
122
		/** @var EmailTemplate $createdTemplate */
123
		$createdTemplate = EmailTemplate::getById($config->getDefaultCreatedTemplateId(), $database);
124
125
		$this->assign('createdHasJsQuestion', $createdTemplate->getJsquestion() != '');
126
		$this->assign('createdJsQuestion', $createdTemplate->getJsquestion());
127
		$this->assign('createdId', $createdTemplate->getId());
128
		$this->assign('createdName', $createdTemplate->getName());
129
130
		$createReasons = EmailTemplate::getActiveTemplates(EmailTemplate::CREATED, $database);
131
		$this->assign("createReasons", $createReasons);
132
		$declineReasons = EmailTemplate::getActiveTemplates(EmailTemplate::NOT_CREATED, $database);
133
		$this->assign("declineReasons", $declineReasons);
134
135
		$allCreateReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::CREATED, $database);
136
		$this->assign("allCreateReasons", $allCreateReasons);
137
		$allDeclineReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::NOT_CREATED, $database);
138
		$this->assign("allDeclineReasons", $allDeclineReasons);
139
		$allOtherReasons = EmailTemplate::getAllActiveTemplates(false, $database);
140
		$this->assign("allOtherReasons", $allOtherReasons);
141
142
		$this->getTypeAheadHelper()->defineTypeAheadSource('username-typeahead', function() use ($database) {
143
			return User::getAllUsernames($database, true);
144
		});
145
	}
146
147
	private function setupLogData(Request $request, PdoDatabase $database)
148
	{
149
		$currentUser = User::getCurrent($database);
150
151
		$logs = LogHelper::getRequestLogsWithComments($request->getId(), $database);
152
		$requestLogs = array();
153
154
		if (trim($request->getComment()) !== "") {
155
			$requestLogs[] = array(
156
				'type'     => 'comment',
157
				'security' => 'user',
158
				'userid'   => null,
159
				'user'     => $request->getName(),
160
				'entry'    => null,
161
				'time'     => $request->getDate(),
162
				'canedit'  => false,
163
				'id'       => $request->getId(),
164
				'comment'  => $request->getComment(),
165
			);
166
		}
167
168
		/** @var User[] $nameCache */
169
		$nameCache = array();
170
171
		$editableComments = false;
172
		if ($currentUser->isAdmin() || $currentUser->isCheckuser()) {
173
			$editableComments = true;
174
		}
175
176
		/** @var Log|Comment $entry */
177
		foreach ($logs as $entry) {
178
			if (!($entry instanceof User) && !($entry instanceof Log)) {
179
				// Something weird has happened here.
180
				continue;
181
			}
182
183
			// both log and comment have a 'user' field
184
			if (!array_key_exists($entry->getUser(), $nameCache)) {
185
				$entryUser = User::getById($entry->getUser(), $database);
186
				$nameCache[$entry->getUser()] = $entryUser;
187
			}
188
189
			if ($entry instanceof Comment) {
190
				$requestLogs[] = array(
191
					'type'     => 'comment',
192
					'security' => $entry->getVisibility(),
193
					'user'     => $nameCache[$entry->getUser()]->getUsername(),
194
					'userid'   => $entry->getUser() == -1 ? null : $entry->getUser(),
195
					'entry'    => null,
196
					'time'     => $entry->getTime(),
197
					'canedit'  => ($editableComments || $entry->getUser() == $currentUser->getId()),
198
					'id'       => $entry->getId(),
199
					'comment'  => $entry->getComment(),
200
				);
201
			}
202
203
			if ($entry instanceof Log) {
204
				$invalidUserId = $entry->getUser() === -1 || $entry->getUser() === 0;
205
				$entryUser = $invalidUserId ? User::getCommunity() : $nameCache[$entry->getUser()];
206
207
				$requestLogs[] = array(
208
					'type'     => 'log',
209
					'security' => 'user',
210
					'userid'   => $entry->getUser() == -1 ? null : $entry->getUser(),
211
					'user'     => $entryUser->getUsername(),
212
					'entry'    => LogHelper::getLogDescription($entry),
213
					'time'     => $entry->getTimestamp(),
214
					'canedit'  => false,
215
					'id'       => $entry->getId(),
216
					'comment'  => $entry->getComment(),
217
				);
218
			}
219
		}
220
221
		$this->assign("requestLogs", $requestLogs);
222
	}
223
224
	/**
225
	 * @param Request $request
226
	 */
227
	protected function setupUsernameData(Request $request)
228
	{
229
		$blacklistData = $this->getBlacklistHelper()->isBlacklisted($request->getName());
230
231
		$this->assign('requestIsBlacklisted', $blacklistData !== false);
232
		$this->assign('requestBlacklist', $blacklistData);
233
234
		try {
235
			$spoofs = $this->getAntiSpoofProvider()->getSpoofs($request->getName());
236
		}
237
		catch (Exception $ex) {
238
			$spoofs = $ex->getMessage();
239
		}
240
241
		$this->assign("spoofs", $spoofs);
242
	}
243
}