Issues (186)

Branch: master

includes/Pages/RequestAction/PageDeferRequest.php (2 issues)

1
<?php
2
/******************************************************************************
3
 * Wikipedia Account Creation Assistance tool                                 *
4
 * ACC Development Team. Please see team.json for a list of contributors.     *
5
 *                                                                            *
6
 * This is free and unencumbered software released into the public domain.    *
7
 * Please see LICENSE.md for the full licencing statement.                    *
8
 ******************************************************************************/
9
10
namespace Waca\Pages\RequestAction;
11
12
use DateTime;
13
use Waca\DataObjects\JobQueue;
14
use Waca\DataObjects\RequestQueue;
15
use Waca\DataObjects\User;
16
use Waca\Exceptions\ApplicationLogicException;
17
use Waca\Helpers\Logger;
18
use Waca\Helpers\SearchHelpers\JobQueueSearchHelper;
19
use Waca\RequestStatus;
20
use Waca\SessionAlert;
21
use Waca\WebRequest;
22
23
class PageDeferRequest extends RequestActionBase
24
{
25
    /**
26
     * Main function for this page, when no specific actions are called.
27
     * @throws ApplicationLogicException
28
     */
29
    protected function main()
30
    {
31
        $this->checkPosted();
32
        $database = $this->getDatabase();
33
        $request = $this->getRequest($database);
34
        $currentUser = User::getCurrent($database);
35
36
        $target = WebRequest::postString('target');
37
38
        // FIXME: domains!
39
        $requestQueue = RequestQueue::getByApiName($database, $target, 1);
0 ignored issues
show
It seems like $target can also be of type null; however, parameter $apiName of Waca\DataObjects\RequestQueue::getByApiName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

39
        $requestQueue = RequestQueue::getByApiName($database, /** @scrutinizer ignore-type */ $target, 1);
Loading history...
40
41
        if ($requestQueue === false) {
42
            throw new ApplicationLogicException('Defer target not valid');
43
        }
44
45
        if ($request->getQueue() == $requestQueue->getId() && $request->getStatus() == RequestStatus::OPEN) {
46
            SessionAlert::warning('This request is already in the specified queue.');
47
            $this->redirect('viewRequest', null, array('id' => $request->getId()));
48
49
            return;
50
        }
51
52
        $closureDate = $request->getClosureDate();
53
        $date = new DateTime();
54
        $date->modify("-7 days");
55
56
        if ($request->getStatus() == RequestStatus::CLOSED && $closureDate < $date) {
57
            if (!$this->barrierTest('reopenOldRequest', $currentUser, 'RequestData')) {
58
                throw new ApplicationLogicException(
59
                    "You are not allowed to re-open a request that has been closed for over a week.");
60
            }
61
        }
62
63
        if ($request->getEmail() === $this->getSiteConfiguration()->getDataClearEmail()) {
64
            if (!$this->barrierTest('reopenClearedRequest', $currentUser, 'RequestData')) {
65
                throw new ApplicationLogicException(
66
                    "You are not allowed to re-open a request for which the private data has been purged.");
67
            }
68
        }
69
70
        if ($request->getStatus() === RequestStatus::JOBQUEUE) {
71
            /** @var JobQueue[] $pendingJobs */
72
            // FIXME: domains
73
            $pendingJobs = JobQueueSearchHelper::get($database, 1)
74
                ->byRequest($request->getId())
75
                ->statusIn([
76
                    JobQueue::STATUS_QUEUED,
77
                    JobQueue::STATUS_READY,
78
                    JobQueue::STATUS_WAITING,
79
                ])
80
                ->fetch();
81
82
            foreach ($pendingJobs as $job) {
83
                $job->setStatus(JobQueue::STATUS_CANCELLED);
84
                $job->setError('Cancelled by request deferral');
85
                $job->save();
86
87
                Logger::backgroundJobCancelled($database, $job);
88
            }
89
        }
90
91
        $request->setReserved(null);
92
        $request->setStatus(RequestStatus::OPEN);
93
        $request->setQueue($requestQueue->getId());
94
        $request->setUpdateVersion(WebRequest::postInt('updateversion'));
95
        $request->save();
96
97
        Logger::deferRequest($database, $request, $requestQueue->getLogName());
0 ignored issues
show
Deprecated Code introduced by
The function Waca\DataObjects\RequestQueue::getLogName() has been deprecated. ( Ignorable by Annotation )

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

97
        Logger::deferRequest($database, $request, /** @scrutinizer ignore-deprecated */ $requestQueue->getLogName());
Loading history...
98
99
        $this->getNotificationHelper()->requestDeferred($request);
100
101
        $deto = htmlentities($requestQueue->getDisplayName(), ENT_COMPAT, 'UTF-8');
102
        SessionAlert::success("Request {$request->getId()} deferred to {$deto}");
103
104
        $this->redirect();
105
    }
106
}
107