Passed
Push — exception-fixes ( 209cd0...a6f08f )
by Simon
06:49 queued 03:22
created

PageJobQueue::view()   B

Complexity

Conditions 8
Paths 10

Size

Total Lines 55
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 55
ccs 0
cts 40
cp 0
rs 8.1795
cc 8
nc 10
nop 0
crap 72

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Waca\Background\Task\BotCreationTask;
12
use Waca\Background\Task\UserCreationTask;
13
use Waca\DataObjects\EmailTemplate;
14
use Waca\DataObjects\JobQueue;
15
use Waca\DataObjects\Log;
16
use Waca\DataObjects\Request;
17
use Waca\DataObjects\User;
18
use Waca\Exceptions\ApplicationLogicException;
19
use Waca\Helpers\Logger;
20
use Waca\Helpers\LogHelper;
21
use Waca\Helpers\SearchHelpers\JobQueueSearchHelper;
22
use Waca\Helpers\SearchHelpers\LogSearchHelper;
23
use Waca\Helpers\SearchHelpers\RequestSearchHelper;
24
use Waca\Helpers\SearchHelpers\UserSearchHelper;
25
use Waca\RequestStatus;
26
use Waca\Tasks\PagedInternalPageBase;
27
use Waca\WebRequest;
28
29
class PageJobQueue extends PagedInternalPageBase
30
{
31
    /**
32
     * Main function for this page, when no specific actions are called.
33
     * @return void
34
     */
35
    protected function main()
36
    {
37
        $this->setHtmlTitle('Job Queue Management');
38
39
        $this->prepareMaps();
40
41
        $database = $this->getDatabase();
42
43
        /** @var JobQueue[] $jobList */
44
        $jobList = JobQueueSearchHelper::get($database)
45
            ->statusIn(array('ready', 'waiting', 'running', 'failed'))
46
            ->notAcknowledged()
47
            ->fetch();
48
49
        $userIds = array();
50
        $requestIds = array();
51
52
        foreach ($jobList as $job) {
53
            $userIds[] = $job->getTriggerUserId();
54
            $requestIds[] = $job->getRequest();
55
56
            $job->setDatabase($database);
57
        }
58
59
        $this->assign('canSeeAll', $this->barrierTest('all', User::getCurrent($database)));
60
61
        $this->assign('users', UserSearchHelper::get($database)->inIds($userIds)->fetchMap('username'));
62
        $this->assign('requests', RequestSearchHelper::get($database)->inIds($requestIds)->fetchMap('name'));
63
64
        $this->assign('joblist', $jobList);
65
        $this->setTemplate('jobqueue/main.tpl');
66
    }
67
68
    protected function all()
69
    {
70
        $this->setHtmlTitle('All Jobs');
71
72
        $this->prepareMaps();
73
74
        $database = $this->getDatabase();
75
76
        $searchHelper = JobQueueSearchHelper::get($database);
77
        $this->setSearchHelper($searchHelper);
78
        $this->setupLimits();
79
80
        $filterUser = WebRequest::getString('filterUser');
81
        $filterTask = WebRequest::getString('filterTask');
82
        $filterStatus = WebRequest::getString('filterStatus');
83
        $filterRequest = WebRequest::getString('filterRequest');
84
        $order = WebRequest::getString('order');
85
86
        if ($filterUser !== null) {
87
            $searchHelper->byUser(User::getByUsername($filterUser, $database)->getId());
88
        }
89
90
        if ($filterTask !== null) {
91
            $searchHelper->byTask($filterTask);
92
        }
93
94
        if ($filterStatus !== null) {
95
            $searchHelper->byStatus($filterStatus);
96
        }
97
98
        if ($filterRequest !== null) {
99
            $searchHelper->byRequest($filterRequest);
0 ignored issues
show
Bug introduced by
$filterRequest of type string is incompatible with the type integer expected by parameter $request of Waca\Helpers\SearchHelpe...archHelper::byRequest(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
            $searchHelper->byRequest(/** @scrutinizer ignore-type */ $filterRequest);
Loading history...
100
        }
101
        
102
        if ($order === null) {
103
            $searchHelper->newestFirst();
104
        }
105
106
        /** @var JobQueue[] $jobList */
107
        $jobList = $searchHelper->getRecordCount($count)->fetch();
108
109
        $this->setupPageData($count, array(
110
            'filterUser' => $filterUser,
111
            'filterTask' => $filterTask,
112
            'filterStatus' => $filterStatus,
113
            'filterRequest' => $filterRequest,
114
            'order' => $order,
115
        ));
116
117
        $userIds = array();
118
        $requestIds = array();
119
120
        foreach ($jobList as $job) {
121
            $userIds[] = $job->getTriggerUserId();
122
            $requestIds[] = $job->getRequest();
123
124
            $job->setDatabase($database);
125
        }
126
127
        $this->getTypeAheadHelper()->defineTypeAheadSource('username-typeahead', function() use ($database) {
128
            return UserSearchHelper::get($database)->fetchColumn('username');
129
        });
130
131
        $this->assign('users', UserSearchHelper::get($database)->inIds($userIds)->fetchMap('username'));
132
        $this->assign('requests', RequestSearchHelper::get($database)->inIds($requestIds)->fetchMap('name'));
133
134
        $this->assign('joblist', $jobList);
135
136
        $this->addJs("/api.php?action=users&all=true&targetVariable=typeaheaddata");
137
138
        $this->setTemplate('jobqueue/all.tpl');
139
    }
140
141
    protected function view()
142
    {
143
        $jobId = WebRequest::getInt('id');
144
        $database = $this->getDatabase();
145
146
        if ($jobId === null) {
147
            throw new ApplicationLogicException('No job specified');
148
        }
149
150
        /** @var JobQueue $job */
151
        $job = JobQueue::getById($jobId, $database);
152
153
        if ($job === false) {
0 ignored issues
show
introduced by
The condition $job === false is always false.
Loading history...
154
            throw new ApplicationLogicException('Could not find requested job');
155
        }
156
157
        $this->prepareMaps();
158
159
        $this->assign('user', User::getById($job->getTriggerUserId(), $database));
160
        $this->assign('request', Request::getById($job->getRequest(), $database));
161
        $this->assign('emailTemplate', EmailTemplate::getById($job->getEmailTemplate(), $database));
162
        $this->assign('parent', JobQueue::getById($job->getParent(), $database));
163
164
        /** @var Log[] $logs */
165
        $logs = LogSearchHelper::get($database)->byObjectType('JobQueue')
166
            ->byObjectId($job->getId())->getRecordCount($logCount)->fetch();
167
        if ($logCount === 0) {
168
            $this->assign('log', array());
169
        }
170
        else {
171
            list($users, $logData) = LogHelper::prepareLogsForTemplate($logs, $database, $this->getSiteConfiguration());
172
173
            $this->assign("log", $logData);
174
            $this->assign("users", $users);
175
        }
176
177
        $this->assignCSRFToken();
178
179
        $this->assign('job', $job);
180
181
        $this->assign('canAcknowledge', $this->barrierTest('acknowledge', User::getCurrent($database)));
182
        $this->assign('canRequeue', $this->barrierTest('requeue', User::getCurrent($database)));
183
184
        if ($job->getTask() === UserCreationTask::class || $job->getTask() === BotCreationTask::class) {
185
            if ($job->getEmailTemplate() === null) {
0 ignored issues
show
introduced by
The condition $job->getEmailTemplate() === null is always false.
Loading history...
186
                $params = json_decode($job->getParameters());
187
188
                if (isset($params->emailText)) {
189
                    $this->assign("creationEmailText", $params->emailText);
190
                }
191
            }
192
        }
193
194
        $this->setHtmlTitle('Job #{$job->getId()|escape}');
195
        $this->setTemplate('jobqueue/view.tpl');
196
    }
197
198
    protected function acknowledge()
199
    {
200
        if (!WebRequest::wasPosted()) {
201
            throw new ApplicationLogicException('This page does not support GET methods.');
202
        }
203
204
        $this->validateCSRFToken();
205
206
        $jobId = WebRequest::postInt('job');
207
        $database = $this->getDatabase();
208
209
        if ($jobId === null) {
210
            throw new ApplicationLogicException('No job specified');
211
        }
212
213
        /** @var JobQueue $job */
214
        $job = JobQueue::getById($jobId, $database);
215
216
        if ($job === false) {
0 ignored issues
show
introduced by
The condition $job === false is always false.
Loading history...
217
            throw new ApplicationLogicException('Could not find requested job');
218
        }
219
220
        $job->setUpdateVersion(WebRequest::postInt('updateVersion'));
221
        $job->setAcknowledged(true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $acknowledged of Waca\DataObjects\JobQueue::setAcknowledged(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

221
        $job->setAcknowledged(/** @scrutinizer ignore-type */ true);
Loading history...
222
        $job->save();
223
224
        Logger::backgroundJobAcknowledged($database, $job);
225
226
        $this->redirect('jobQueue', 'view', array('id' => $jobId));
227
    }
228
229
    protected function requeue()
230
    {
231
        if (!WebRequest::wasPosted()) {
232
            throw new ApplicationLogicException('This page does not support GET methods.');
233
        }
234
235
        $this->validateCSRFToken();
236
237
        $jobId = WebRequest::postInt('job');
238
        $database = $this->getDatabase();
239
240
        if ($jobId === null) {
241
            throw new ApplicationLogicException('No job specified');
242
        }
243
244
        /** @var JobQueue $job */
245
        $job = JobQueue::getById($jobId, $database);
246
247
        if ($job === false) {
0 ignored issues
show
introduced by
The condition $job === false is always false.
Loading history...
248
            throw new ApplicationLogicException('Could not find requested job');
249
        }
250
251
        $job->setStatus(JobQueue::STATUS_READY);
252
        $job->setUpdateVersion(WebRequest::postInt('updateVersion'));
253
        $job->setAcknowledged(null);
254
        $job->setError(null);
255
        $job->save();
256
257
        /** @var Request $request */
258
        $request = Request::getById($job->getRequest(), $database);
259
        $request->setStatus(RequestStatus::JOBQUEUE);
260
        $request->save();
261
262
        Logger::enqueuedJobQueue($database, $request);
263
        Logger::backgroundJobRequeued($database, $job);
264
265
        $this->redirect('jobQueue', 'view', array('id' => $jobId));
266
    }
267
268
    protected function prepareMaps()
269
    {
270
        $taskNameMap = JobQueue::getTaskDescriptions();
271
272
        $statusDecriptionMap = array(
273
            JobQueue::STATUS_CANCELLED => 'The job was cancelled',
274
            JobQueue::STATUS_COMPLETE  => 'The job completed successfully',
275
            JobQueue::STATUS_FAILED    => 'The job encountered an error',
276
            JobQueue::STATUS_READY     => 'The job is ready to be picked up by the next job runner execution',
277
            JobQueue::STATUS_RUNNING   => 'The job is being run right now by the job runner',
278
            JobQueue::STATUS_WAITING   => 'The job has been picked up by a job runner',
279
            JobQueue::STATUS_HELD      => 'The job has manually held from processing',
280
        );
281
        $this->assign('taskNameMap', $taskNameMap);
282
        $this->assign('statusDescriptionMap', $statusDecriptionMap);
283
    }
284
}
285