Completed
Pull Request — master (#111)
by MusikAnimal
02:25
created

AdminScoreController::indexAction()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 2
nop 1
1
<?php
2
/**
3
 * This file contains only the AdminScoreController class.
4
 */
5
6
namespace AppBundle\Controller;
7
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
13
use DateTime;
14
use Xtools\ProjectRepository;
15
use Xtools\UserRepository;
16
17
/**
18
 * The AdminScoreController serves the search form and results page of the AdminScore tool
19
 */
20
class AdminScoreController extends XtoolsController
21
{
22
23
    /**
24
     * Get the tool's shortname.
25
     * @return string
26
     * @codeCoverageIgnore
27
     */
28
    public function getToolShortname()
29
    {
30
        return 'adminscore';
31
    }
32
33
    /**
34
     * Display the AdminScore search form.
35
     * @Route("/adminscore", name="adminscore")
36
     * @Route("/adminscore", name="AdminScore")
37
     * @Route("/adminscore/", name="AdminScoreSlash")
38
     * @Route("/adminscore/index.php", name="AdminScoreIndexPhp")
39
     * @Route("/scottywong tools/adminscore.php", name="AdminScoreLegacy")
40
     * @Route("/adminscore/{project}", name="AdminScoreProject")
41
     * @param Request $request The HTTP request.
42
     * @return Response
43
     */
44
    public function indexAction(Request $request)
45
    {
46
        $params = $this->parseQueryParams($request);
47
48
        // Redirect if we have a project and user.
49
        if (isset($params['project']) && isset($params['username'])) {
50
            return $this->redirectToRoute('AdminScoreResult', $params);
51
        }
52
53
        // Convert the given project (or default project) into a Project instance.
54
        $params['project'] = $this->getProjectFromQuery($params);
55
56
        return $this->render('adminscore/index.html.twig', [
57
            'xtPage' => 'adminscore',
58
            'xtPageTitle' => 'tool-adminscore',
59
            'xtSubtitle' => 'tool-adminscore-desc',
60
            'project' => $params['project'],
61
        ]);
62
    }
63
64
    /**
65
     * Display the AdminScore results.
66
     * @Route("/adminscore/{project}/{username}", name="AdminScoreResult")
67
     * @param Request $request The HTTP request.
68
     * @return Response
69
     * @todo Move SQL to a model.
70
     * @codeCoverageIgnore
71
     */
72
    public function resultAction(Request $request)
73
    {
74
        // Second parameter causes it return a Redirect to the index if the user has too many edits.
75
        $ret = $this->validateProjectAndUser($request, 'adminscore');
76
        if ($ret instanceof RedirectResponse) {
0 ignored issues
show
Bug introduced by
The class AppBundle\Controller\RedirectResponse does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
77
            return $ret;
78
        } else {
79
            list($projectData, $user) = $ret;
80
        }
81
82
        $dbName = $projectData->getDatabaseName();
83
        $projectRepo = $projectData->getRepository();
84
85
        $userTable = $projectRepo->getTableName($dbName, 'user');
86
        $pageTable = $projectRepo->getTableName($dbName, 'page');
87
        $loggingTable = $projectRepo->getTableName($dbName, 'logging', 'userindex');
88
        $revisionTable = $projectRepo->getTableName($dbName, 'revision');
89
        $archiveTable = $projectRepo->getTableName($dbName, 'archive');
90
91
        // MULTIPLIERS (to review)
92
        $multipliers = [
93
            'account-age-mult' => 1.25,             # 0 if = 365 jours
94
            'edit-count-mult' => 1.25,              # 0 if = 10 000
95
            'user-page-mult' => 0.1,                # 0 if =
96
            'patrols-mult' => 1,                    # 0 if =
97
            'blocks-mult' => 1.4,                   # 0 if = 10
98
            'afd-mult' => 1.15,
99
            'recent-activity-mult' => 0.9,          # 0 if =
100
            'aiv-mult' => 1.15,
101
            'edit-summaries-mult' => 0.8,           # 0 if =
102
            'namespaces-mult' => 1.0,               # 0 if =
103
            'pages-created-live-mult' => 1.4,       # 0 if =
104
            'pages-created-deleted-mult' => 1.4,    # 0 if =
105
            'rpp-mult' => 1.15,                     # 0 if =
106
            'user-rights-mult' => 0.75,             # 0 if =
107
        ];
108
109
        // Grab the connection to the replica database (which is separate from the above)
110
        $conn = $this->get('doctrine')->getManager("replicas")->getConnection();
111
112
        // Prepare the query and execute
113
        $resultQuery = $conn->prepare("
114
        SELECT 'account-age' AS source, user_registration AS value FROM $userTable
115
            WHERE user_name = :username
116
        UNION
117
        SELECT 'edit-count' AS source, user_editcount AS value FROM $userTable
118
            WHERE user_name = :username
119
        UNION
120
        SELECT 'user-page' AS source, page_len AS value FROM $pageTable
121
            WHERE page_namespace = 2 AND page_title = :username
122
        UNION
123
        SELECT 'patrols' AS source, COUNT(*) AS value FROM $loggingTable
124
            WHERE log_type = 'patrol'
125
                AND log_action = 'patrol'
126
                AND log_namespace = 0
127
                AND log_deleted = 0 AND log_user_text = :username
128
        UNION
129
        SELECT 'blocks' AS source, COUNT(*) AS value FROM $loggingTable l
130
            INNER JOIN $userTable u ON l.log_user = u.user_id
131
            WHERE l.log_type = 'block' AND l.log_action = 'block'
132
            AND l.log_namespace = 2 AND l.log_deleted = 0 AND u.user_name = :username
133
        UNION
134
        SELECT 'afd' AS source, COUNT(*) AS value FROM $revisionTable r
135
          INNER JOIN $pageTable p on p.page_id = r.rev_page
136
            WHERE p.page_title LIKE 'Articles_for_deletion/%'
137
                AND p.page_title NOT LIKE 'Articles_for_deletion/Log/%'
138
                AND r.rev_user_text = :username
139
        UNION
140
        SELECT 'recent-activity' AS source, COUNT(*) AS value FROM $revisionTable
141
            WHERE rev_user_text = :username AND rev_timestamp > (now()-INTERVAL 730 day) AND rev_timestamp < now()
142
        UNION
143
        SELECT 'aiv' AS source, COUNT(*) AS value FROM $revisionTable r
144
          INNER JOIN $pageTable p on p.page_id = r.rev_page
145
            WHERE p.page_title LIKE 'Administrator_intervention_against_vandalism%'
146
                AND r.rev_user_text = :username
147
        UNION
148
        SELECT 'edit-summaries' AS source, COUNT(*) AS value FROM $revisionTable JOIN $pageTable ON rev_page = page_id
149
            WHERE page_namespace = 0 AND rev_user_text = :username
150
        UNION
151
        SELECT 'namespaces' AS source, count(*) AS value FROM $revisionTable JOIN $pageTable ON rev_page = page_id
152
            WHERE rev_user_text = :username AND page_namespace = 0
153
        UNION
154
        SELECT 'pages-created-live' AS source, COUNT(*) AS value FROM $revisionTable
155
            WHERE rev_user_text = :username AND rev_parent_id = 0
156
        UNION
157
        SELECT 'pages-created-deleted' AS source, COUNT(*) AS value FROM $archiveTable
158
            WHERE ar_user_text = :username AND ar_parent_id = 0
159
        UNION
160
        SELECT 'rpp' AS source, COUNT(*) AS value FROM $revisionTable r
161
          INNER JOIN $pageTable p on p.page_id = r.rev_page
162
            WHERE p.page_title LIKE 'Requests_for_page_protection%'
163
                AND r.rev_user_text = :username;
164
        ");
165
166
        $username = $user->getUsername();
167
        $resultQuery->bindParam("username", $username);
168
        $resultQuery->execute();
169
170
        // Fetch the result data
171
        $results = $resultQuery->fetchAll();
172
173
        $master = [];
174
        $total = 0;
175
176
        foreach ($results as $row) {
177
            $key = $row['source'];
178
            $value = $row['value'];
179
180
            if ($key === 'account-age') {
181
                $now = new DateTime();
182
                $date = new DateTime($value);
183
                $diff = $date->diff($now);
184
                $formula = 365 * $diff->format('%y') + 30 * $diff->format('%m') + $diff->format('%d');
185
                $value = $formula - 365;
186
            }
187
188
            $multiplierKey = $row['source'] . '-mult';
189
            $multiplier = isset($multipliers[$multiplierKey]) ? $multipliers[$multiplierKey] : 1;
190
            $score = max(min($value * $multiplier, 100), -100);
191
            $master[$key]['mult'] = $multiplier;
192
            $master[$key]['value'] = $value;
193
            $master[$key]['score'] = $score;
194
            $total += $score;
195
        }
196
197
        return $this->render('adminscore/result.html.twig', [
198
            'xtPage' => 'adminscore',
199
            'xtTitle' => $username,
200
            'project' => $projectData,
201
            'user' => $user,
202
            'master' => $master,
203
            'total' => $total,
204
        ]);
205
    }
206
}
207