| Total Complexity | 43 |
| Total Lines | 312 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 1 | Features | 0 |
Complex classes like PageViewRequest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PageViewRequest, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class PageViewRequest extends InternalPageBase |
||
| 33 | { |
||
| 34 | use RequestData; |
||
| 35 | const STATUS_SYMBOL_OPEN = 'Ο'; |
||
| 36 | const STATUS_SYMBOL_ACCEPTED = '☑'; |
||
| 37 | const STATUS_SYMBOL_REJECTED = '☒'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Main function for this page, when no specific actions are called. |
||
| 41 | * @throws ApplicationLogicException |
||
| 42 | */ |
||
| 43 | protected function main() |
||
| 44 | { |
||
| 45 | // set up csrf protection |
||
| 46 | $this->assignCSRFToken(); |
||
| 47 | |||
| 48 | // get some useful objects |
||
| 49 | $database = $this->getDatabase(); |
||
| 50 | $request = $this->getRequest($database, WebRequest::getInt('id')); |
||
| 51 | $config = $this->getSiteConfiguration(); |
||
| 52 | $currentUser = User::getCurrent($database); |
||
| 53 | |||
| 54 | // FIXME: domains! |
||
| 55 | /** @var Domain $domain */ |
||
| 56 | $domain = Domain::getById(1, $this->getDatabase()); |
||
| 57 | $this->assign('mediawikiScriptPath', $domain->getWikiArticlePath()); |
||
| 58 | |||
| 59 | // Shows a page if the email is not confirmed. |
||
| 60 | if ($request->getEmailConfirm() !== 'Confirmed') { |
||
| 61 | // Show a banner if the user can manually confirm the request |
||
| 62 | $viewConfirm = $this->barrierTest(RoleConfiguration::MAIN, $currentUser, PageManuallyConfirm::class); |
||
| 63 | |||
| 64 | // If the request is purged, there's nothing to confirm! |
||
| 65 | if ($request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()) { |
||
| 66 | $viewConfirm = false; |
||
| 67 | } |
||
| 68 | |||
| 69 | // Render |
||
| 70 | $this->setTemplate("view-request/not-confirmed.tpl"); |
||
| 71 | $this->assign("requestId", $request->getId()); |
||
| 72 | $this->assign("requestVersion", $request->getUpdateVersion()); |
||
| 73 | $this->assign('canViewConfirmButton', $viewConfirm); |
||
| 74 | |||
| 75 | // Make sure to return, to prevent the leaking of other information. |
||
| 76 | return; |
||
| 77 | } |
||
| 78 | |||
| 79 | $this->setupBasicData($request, $config); |
||
| 80 | |||
| 81 | $this->setupUsernameData($request); |
||
| 82 | |||
| 83 | $this->setupTitle($request); |
||
| 84 | |||
| 85 | $this->setupReservationDetails($request->getReserved(), $database, $currentUser); |
||
| 86 | $this->setupGeneralData($database); |
||
| 87 | |||
| 88 | $this->assign('requestDataCleared', false); |
||
| 89 | if ($request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()) { |
||
| 90 | $this->assign('requestDataCleared', true); |
||
| 91 | } |
||
| 92 | |||
| 93 | $allowedPrivateData = $this->isAllowedPrivateData($request, $currentUser); |
||
| 94 | |||
| 95 | $this->setupCreationTypes($currentUser); |
||
| 96 | |||
| 97 | $this->setupLogData($request, $database); |
||
| 98 | |||
| 99 | $this->addJs("/api.php?action=templates&targetVariable=templateconfirms"); |
||
| 100 | |||
| 101 | $this->assign('showRevealLink', false); |
||
| 102 | if ($request->getReserved() === $currentUser->getId() || |
||
| 103 | $this->barrierTest('alwaysSeeHash', $currentUser, 'RequestData') |
||
| 104 | ) { |
||
| 105 | $this->assign('showRevealLink', true); |
||
| 106 | $this->assign('revealHash', $request->getRevealHash()); |
||
| 107 | } |
||
| 108 | |||
| 109 | $this->assign('canSeeRelatedRequests', false); |
||
| 110 | if ($allowedPrivateData || $this->barrierTest('seeRelatedRequests', $currentUser, 'RequestData')) { |
||
| 111 | $this->setupRelatedRequests($request, $config, $database); |
||
| 112 | } |
||
| 113 | |||
| 114 | $this->assign('canCreateLocalAccount', $this->barrierTest('createLocalAccount', $currentUser, 'RequestData')); |
||
| 115 | |||
| 116 | $closureDate = $request->getClosureDate(); |
||
| 117 | $date = new DateTime(); |
||
| 118 | $date->modify("-7 days"); |
||
| 119 | if ($request->getStatus() == "Closed" && $closureDate < $date) { |
||
| 120 | $this->assign('isOldRequest', true); |
||
| 121 | } |
||
| 122 | $this->assign('canResetOldRequest', $this->barrierTest('reopenOldRequest', $currentUser, 'RequestData')); |
||
| 123 | $this->assign('canResetPurgedRequest', $this->barrierTest('reopenClearedRequest', $currentUser, 'RequestData')); |
||
| 124 | |||
| 125 | $this->assign('requestEmailSent', $request->getEmailSent()); |
||
| 126 | |||
| 127 | if ($allowedPrivateData) { |
||
| 128 | $this->setTemplate('view-request/main-with-data.tpl'); |
||
| 129 | $this->setupPrivateData($request, $config); |
||
| 130 | $this->assign('canSetBan', $this->barrierTest('set', $currentUser, PageBan::class)); |
||
| 131 | $this->assign('canSeeCheckuserData', $this->barrierTest('seeUserAgentData', $currentUser, 'RequestData')); |
||
| 132 | |||
| 133 | if ($this->barrierTest('seeUserAgentData', $currentUser, 'RequestData')) { |
||
| 134 | $this->setTemplate('view-request/main-with-checkuser-data.tpl'); |
||
| 135 | $this->setupCheckUserData($request); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | else { |
||
| 139 | $this->setTemplate('view-request/main.tpl'); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param Request $request |
||
| 145 | */ |
||
| 146 | protected function setupTitle(Request $request) |
||
| 147 | { |
||
| 148 | $statusSymbol = self::STATUS_SYMBOL_OPEN; |
||
| 149 | if ($request->getStatus() === RequestStatus::CLOSED) { |
||
| 150 | if ($request->getWasCreated()) { |
||
| 151 | $statusSymbol = self::STATUS_SYMBOL_ACCEPTED; |
||
| 152 | } |
||
| 153 | else { |
||
| 154 | $statusSymbol = self::STATUS_SYMBOL_REJECTED; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | $this->setHtmlTitle($statusSymbol . ' #' . $request->getId()); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Sets up data unrelated to the request, such as the email template information |
||
| 163 | * |
||
| 164 | * @param PdoDatabase $database |
||
| 165 | */ |
||
| 166 | protected function setupGeneralData(PdoDatabase $database) |
||
| 167 | { |
||
| 168 | $this->assign('createAccountReason', 'Requested account at [[WP:ACC]], request #'); |
||
| 169 | |||
| 170 | // FIXME: domains |
||
| 171 | /** @var Domain $domain */ |
||
| 172 | $domain = Domain::getById(1, $database); |
||
| 173 | $this->assign('defaultRequestState', RequestQueue::getDefaultQueue($database, 1)->getApiName()); |
||
| 174 | $this->assign('activeRequestQueues', RequestQueue::getEnabledQueues($database)); |
||
| 175 | |||
| 176 | /** @var EmailTemplate $createdTemplate */ |
||
| 177 | $createdTemplate = EmailTemplate::getById($domain->getDefaultClose(), $database); |
||
| 178 | |||
| 179 | $this->assign('createdHasJsQuestion', $createdTemplate->getJsquestion() != ''); |
||
| 180 | $this->assign('createdId', $createdTemplate->getId()); |
||
| 181 | $this->assign('createdName', $createdTemplate->getName()); |
||
| 182 | |||
| 183 | $createReasons = EmailTemplate::getActiveNonpreloadTemplates(EmailTemplate::ACTION_CREATED, $database, $domain->getDefaultClose()); |
||
| 184 | $this->assign("createReasons", $createReasons); |
||
| 185 | $declineReasons = EmailTemplate::getActiveNonpreloadTemplates(EmailTemplate::ACTION_NOT_CREATED, $database); |
||
| 186 | $this->assign("declineReasons", $declineReasons); |
||
| 187 | |||
| 188 | $allCreateReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::ACTION_CREATED, $database); |
||
| 189 | $this->assign("allCreateReasons", $allCreateReasons); |
||
| 190 | $allDeclineReasons = EmailTemplate::getAllActiveTemplates(EmailTemplate::ACTION_NOT_CREATED, $database); |
||
| 191 | $this->assign("allDeclineReasons", $allDeclineReasons); |
||
| 192 | $allOtherReasons = EmailTemplate::getAllActiveTemplates(false, $database); |
||
| 193 | $this->assign("allOtherReasons", $allOtherReasons); |
||
| 194 | } |
||
| 195 | |||
| 196 | private function setupLogData(Request $request, PdoDatabase $database) |
||
| 197 | { |
||
| 198 | $currentUser = User::getCurrent($database); |
||
| 199 | |||
| 200 | $logs = LogHelper::getRequestLogsWithComments($request->getId(), $database, $this->getSecurityManager()); |
||
| 201 | $requestLogs = array(); |
||
| 202 | |||
| 203 | /** @var User[] $nameCache */ |
||
| 204 | $nameCache = array(); |
||
| 205 | |||
| 206 | $editableComments = $this->barrierTest('editOthers', $currentUser, PageEditComment::class); |
||
| 207 | |||
| 208 | $canFlag = $this->barrierTest(RoleConfiguration::MAIN, $currentUser, PageFlagComment::class); |
||
| 209 | $canUnflag = $this->barrierTest('unflag', $currentUser, PageFlagComment::class); |
||
| 210 | |||
| 211 | /** @var Log|Comment $entry */ |
||
| 212 | foreach ($logs as $entry) { |
||
| 213 | // both log and comment have a 'user' field |
||
| 214 | if (!array_key_exists($entry->getUser(), $nameCache)) { |
||
|
|
|||
| 215 | $entryUser = User::getById($entry->getUser(), $database); |
||
| 216 | $nameCache[$entry->getUser()] = $entryUser; |
||
| 217 | } |
||
| 218 | |||
| 219 | if ($entry instanceof Comment) { |
||
| 220 | $requestLogs[] = array( |
||
| 221 | 'type' => 'comment', |
||
| 222 | 'security' => $entry->getVisibility(), |
||
| 223 | 'user' => $entry->getVisibility() == 'requester' ? $request->getName() : $nameCache[$entry->getUser()]->getUsername(), |
||
| 224 | 'userid' => $entry->getUser() == -1 ? null : $entry->getUser(), |
||
| 225 | 'entry' => null, |
||
| 226 | 'time' => $entry->getTime(), |
||
| 227 | 'canedit' => ($editableComments || $entry->getUser() == $currentUser->getId()), |
||
| 228 | 'id' => $entry->getId(), |
||
| 229 | 'comment' => $entry->getComment(), |
||
| 230 | 'flagged' => $entry->getFlagged(), |
||
| 231 | 'canflag' => $canFlag && (!$entry->getFlagged() || ($entry->getFlagged() && $canUnflag)), |
||
| 232 | 'updateversion' => $entry->getUpdateVersion(), |
||
| 233 | 'edited' => $entry->getEdited() |
||
| 234 | ); |
||
| 235 | } |
||
| 236 | |||
| 237 | if ($entry instanceof Log) { |
||
| 238 | $invalidUserId = $entry->getUser() === -1 || $entry->getUser() === 0; |
||
| 239 | $entryUser = $invalidUserId ? User::getCommunity() : $nameCache[$entry->getUser()]; |
||
| 240 | |||
| 241 | $entryComment = $entry->getComment(); |
||
| 242 | |||
| 243 | if ($entry->getAction() === 'JobIssueRequest' || $entry->getAction() === 'JobCompletedRequest') { |
||
| 244 | $data = unserialize($entry->getComment()); |
||
| 245 | /** @var JobQueue $job */ |
||
| 246 | $job = JobQueue::getById($data['job'], $database); |
||
| 247 | $requestLogs[] = array( |
||
| 248 | 'type' => 'joblog', |
||
| 249 | 'security' => 'user', |
||
| 250 | 'userid' => $entry->getUser() == -1 ? null : $entry->getUser(), |
||
| 251 | 'user' => $entryUser->getUsername(), |
||
| 252 | 'entry' => LogHelper::getLogDescription($entry), |
||
| 253 | 'time' => $entry->getTimestamp(), |
||
| 254 | 'canedit' => false, |
||
| 255 | 'id' => $entry->getId(), |
||
| 256 | 'jobId' => $job->getId(), |
||
| 257 | 'jobDesc' => JobQueue::getTaskDescriptions()[$job->getTask()], |
||
| 258 | ); |
||
| 259 | } |
||
| 260 | else { |
||
| 261 | $requestLogs[] = array( |
||
| 262 | 'type' => 'log', |
||
| 263 | 'security' => 'user', |
||
| 264 | 'userid' => $entry->getUser() == -1 ? null : $entry->getUser(), |
||
| 265 | 'user' => $entryUser->getUsername(), |
||
| 266 | 'entry' => LogHelper::getLogDescription($entry), |
||
| 267 | 'time' => $entry->getTimestamp(), |
||
| 268 | 'canedit' => false, |
||
| 269 | 'id' => $entry->getId(), |
||
| 270 | 'comment' => $entryComment, |
||
| 271 | ); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | $this->addJs("/api.php?action=users&targetVariable=typeaheaddata"); |
||
| 277 | |||
| 278 | $this->assign("requestLogs", $requestLogs); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param Request $request |
||
| 283 | */ |
||
| 284 | protected function setupUsernameData(Request $request) |
||
| 285 | { |
||
| 286 | $blacklistData = $this->getBlacklistHelper()->isBlacklisted($request->getName()); |
||
| 287 | |||
| 288 | $this->assign('requestIsBlacklisted', $blacklistData !== false); |
||
| 289 | $this->assign('requestBlacklist', $blacklistData); |
||
| 290 | |||
| 291 | try { |
||
| 292 | $spoofs = $this->getAntiSpoofProvider()->getSpoofs($request->getName()); |
||
| 293 | } |
||
| 294 | catch (Exception $ex) { |
||
| 295 | $spoofs = $ex->getMessage(); |
||
| 296 | } |
||
| 297 | |||
| 298 | $this->assign("spoofs", $spoofs); |
||
| 299 | } |
||
| 300 | |||
| 301 | private function setupCreationTypes(User $user) |
||
| 344 | } |
||
| 345 | } |
||
| 346 | } |
||
| 347 |