Issues (186)

includes/Pages/Statistics/StatsUsers.php (1 issue)

Severity
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\Statistics;
11
12
use PDO;
13
use Waca\DataObjects\Domain;
14
use Waca\DataObjects\EmailTemplate;
15
use Waca\DataObjects\Log;
16
use Waca\DataObjects\User;
17
use Waca\Exceptions\ApplicationLogicException;
18
use Waca\Helpers\LogHelper;
19
use Waca\Helpers\OAuthUserHelper;
20
use Waca\Helpers\SearchHelpers\LogSearchHelper;
21
use Waca\IdentificationVerifier;
22
use Waca\Pages\PageUserManagement;
23
use Waca\Tasks\InternalPageBase;
24
use Waca\WebRequest;
25
26
class StatsUsers extends InternalPageBase
27
{
28
    public function main()
29
    {
30
        $this->setHtmlTitle('Users :: Statistics');
31
32
        $database = $this->getDatabase();
33
34
        $query = <<<SQL
35
SELECT
36
    u.id
37
    , u.username
38
    , CASE WHEN ra.role IS NOT NULL THEN 'Yes' ELSE 'No' END tooladmin
39
    , CASE WHEN rc.role IS NOT NULL THEN 'Yes' ELSE 'No' END checkuser
40
    , CASE WHEN rs.role IS NOT NULL THEN 'Yes' ELSE 'No' END steward
41
    , CASE WHEN rr.role IS NOT NULL THEN 'Yes' ELSE 'No' END toolroot
42
FROM user u
43
    LEFT JOIN userrole ra ON ra.user = u.id AND ra.role = 'admin'
44
    LEFT JOIN userrole rc ON rc.user = u.id AND rc.role = 'checkuser'
45
    LEFT JOIN userrole rs ON rs.user = u.id AND rs.role = 'steward'
46
    LEFT JOIN userrole rr ON rr.user = u.id AND rr.role = 'toolRoot'
47
WHERE u.status = 'Active'
48
SQL;
49
50
        $users = $database->query($query)->fetchAll(PDO::FETCH_ASSOC);
51
        $this->assign('users', $users);
52
53
        $this->assign('statsPageTitle', 'Account Creation Tool users');
54
        $this->setTemplate("statistics/users.tpl");
55
    }
56
57
    /**
58
     * Entry point for the detail action.
59
     *
60
     * @throws ApplicationLogicException
61
     */
62
    protected function detail()
63
    {
64
        $userId = WebRequest::getInt('user');
65
        if ($userId === null) {
66
            throw new ApplicationLogicException("User not found");
67
        }
68
69
        $database = $this->getDatabase();
70
71
        $user = User::getById($userId, $database);
72
        if ($user == false) {
0 ignored issues
show
The condition $user == false is always false.
Loading history...
73
            throw new ApplicationLogicException('User not found');
74
        }
75
76
77
        $activitySummary = $database->prepare(<<<SQL
78
SELECT COALESCE(closes.mail_desc, log.action) AS action, COUNT(*) AS count
79
FROM log
80
INNER JOIN user ON log.user = user.id
81
LEFT JOIN closes ON log.action = closes.closes
82
WHERE user.username = :username
83
GROUP BY action;
84
SQL
85
        );
86
        $activitySummary->execute(array(":username" => $user->getUsername()));
87
        $activitySummaryData = $activitySummary->fetchAll(PDO::FETCH_ASSOC);
88
89
        $this->assign("user", $user);
90
        $this->assign("activity", $activitySummaryData);
91
92
        $usersCreatedQuery = $database->prepare(<<<SQL
93
SELECT log.timestamp time, request.name name, request.id id
94
FROM log
95
INNER JOIN request ON (request.id = log.objectid AND log.objecttype = 'Request')
96
INNER JOIN user ON log.user = user.id
97
LEFT JOIN emailtemplate ON concat('Closed ', emailtemplate.id) = log.action
98
WHERE user.username = :username
99
    AND log.action LIKE 'Closed %'
100
    AND (emailtemplate.defaultaction = :created OR log.action = 'Closed custom-y')
101
ORDER BY log.timestamp;
102
SQL
103
        );
104
        $usersCreatedQuery->execute(array(":username" => $user->getUsername(), ':created' => EmailTemplate::ACTION_CREATED));
105
        $usersCreated = $usersCreatedQuery->fetchAll(PDO::FETCH_ASSOC);
106
        $this->assign("created", $usersCreated);
107
108
        $usersNotCreatedQuery = $database->prepare(<<<SQL
109
SELECT log.timestamp time, request.name name, request.id id
110
FROM log
111
JOIN request ON request.id = log.objectid AND log.objecttype = 'Request'
112
JOIN user ON log.user = user.id
113
LEFT JOIN emailtemplate ON concat('Closed ', emailtemplate.id) = log.action
114
WHERE user.username = :username
115
    AND log.action LIKE 'Closed %'
116
    AND (emailtemplate.defaultaction = :created OR log.action = 'Closed custom-n' OR log.action = 'Closed 0')
117
ORDER BY log.timestamp;
118
SQL
119
        );
120
        $usersNotCreatedQuery->execute(array(":username" => $user->getUsername(), ':created' => EmailTemplate::ACTION_NOT_CREATED));
121
        $usersNotCreated = $usersNotCreatedQuery->fetchAll(PDO::FETCH_ASSOC);
122
        $this->assign("notcreated", $usersNotCreated);
123
124
        /** @var Log[] $logs */
125
        $logs = LogSearchHelper::get($database, Domain::getCurrent($database)->getId())
126
            ->byObjectType('User')
127
            ->byObjectId($user->getId())
128
            ->getRecordCount($logCount)
129
            ->fetch();
130
131
        if ($logCount === 0) {
132
            $this->assign('accountlog', array());
133
        }
134
        else {
135
            list($users, $logData) = LogHelper::prepareLogsForTemplate($logs, $database, $this->getSiteConfiguration(), $this->getSecurityManager());
136
137
            $this->assign("accountlog", $logData);
138
            $this->assign("users", $users);
139
        }
140
141
        $currentUser = User::getCurrent($database);
142
        $this->assign('canApprove', $this->barrierTest('approve', $currentUser, PageUserManagement::class));
143
        $this->assign('canDeactivate', $this->barrierTest('deactivate', $currentUser, PageUserManagement::class));
144
        $this->assign('canRename', $this->barrierTest('rename', $currentUser, PageUserManagement::class));
145
        $this->assign('canEditUser', $this->barrierTest('editUser', $currentUser, PageUserManagement::class));
146
        $this->assign('canEditRoles', $this->barrierTest('editRoles', $currentUser, PageUserManagement::class));
147
148
        $oauth = new OAuthUserHelper($user, $database, $this->getOAuthProtocolHelper(), $this->getSiteConfiguration());
149
        $this->assign('oauth', $oauth);
150
151
        if ($user->getForceIdentified() === null) {
152
            $idVerifier = new IdentificationVerifier($this->getHttpHelper(), $this->getSiteConfiguration(), $this->getDatabase());
153
            $this->assign('identificationStatus', $idVerifier->isUserIdentified($user->getOnWikiName()) ? 'detected' : 'missing');
154
        }
155
        else {
156
            $this->assign('identificationStatus', $user->getForceIdentified() == 1 ? 'forced-on' : 'forced-off');
157
        }
158
159
        if ($oauth->isFullyLinked()) {
160
            $this->assign('identity', $oauth->getIdentity(true));
161
            $this->assign('identityExpired', $oauth->identityExpired());
162
        }
163
164
        $this->assign('statsPageTitle', 'Account Creation Tool users');
165
166
        // FIXME: domains!
167
        /** @var Domain $domain */
168
        $domain = Domain::getById(1, $this->getDatabase());
169
        $this->assign('mediawikiScriptPath', $domain->getWikiArticlePath());
170
171
        $this->setHtmlTitle('{$user->getUsername()|escape} :: Users :: Statistics');
172
        $this->setTemplate("statistics/userdetail.tpl");
173
    }
174
}
175