|
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\API\Actions; |
|
10
|
|
|
|
|
11
|
|
|
use DOMElement; |
|
12
|
|
|
use Waca\API\ApiException as ApiException; |
|
13
|
|
|
use Waca\API\IApiAction as IApiAction; |
|
14
|
|
|
use Waca\DataObjects\User; |
|
15
|
|
|
use Waca\Tasks\ApiPageBase; |
|
16
|
|
|
use Waca\WebRequest; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* API Count action |
|
20
|
|
|
*/ |
|
21
|
|
|
class CountAction extends ApiPageBase implements IApiAction |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* The target user |
|
25
|
|
|
* @var User $user |
|
26
|
|
|
*/ |
|
27
|
|
|
private $user; |
|
28
|
|
|
|
|
29
|
|
|
public function executeApiAction(DOMElement $apiDocument) |
|
30
|
|
|
{ |
|
31
|
|
|
$username = WebRequest::getString('user'); |
|
32
|
|
|
if ($username === null) { |
|
33
|
|
|
throw new ApiException("Please specify a username"); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$userElement = $this->document->createElement("user"); |
|
|
|
|
|
|
37
|
|
|
$userElement->setAttribute("name", $username); |
|
|
|
|
|
|
38
|
|
|
$apiDocument->appendChild($userElement); |
|
39
|
|
|
|
|
40
|
|
|
$user = User::getByUsername($username, $this->getDatabase()); |
|
41
|
|
|
|
|
42
|
|
|
if ($user === false) { |
|
43
|
|
|
$userElement->setAttribute("missing", "true"); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
return $apiDocument; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this->user = $user; |
|
49
|
|
|
|
|
50
|
|
|
$userElement->setAttribute("level", $this->user->getStatus()); |
|
|
|
|
|
|
51
|
|
|
$userElement->setAttribute("created", $this->getAccountsCreated()); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
$userElement->setAttribute("today", $this->getToday()); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
// Let the IRC bot handle the result of this. |
|
56
|
|
|
$this->fetchAdminData($userElement); |
|
57
|
|
|
|
|
58
|
|
|
return $apiDocument; |
|
59
|
|
|
} |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
private function getAccountsCreated() |
|
62
|
|
|
{ |
|
63
|
|
|
$query = <<<QUERY |
|
64
|
|
|
SELECT COUNT(*) AS count |
|
65
|
|
|
FROM log |
|
66
|
|
|
LEFT JOIN emailtemplate ON concat('Closed ', emailtemplate.id) = log.action |
|
67
|
|
|
INNER JOIN user ON log.user = user.id |
|
68
|
|
|
WHERE |
|
69
|
|
|
(oncreated = '1' OR log.action = 'Closed custom-y') |
|
70
|
|
|
AND log.objecttype = 'Request' |
|
71
|
|
|
AND user.username = :username; |
|
72
|
|
|
QUERY; |
|
73
|
|
|
|
|
74
|
|
|
$statement = $this->getDatabase()->prepare($query); |
|
75
|
|
|
$statement->execute(array(":username" => $this->user->getUsername())); |
|
|
|
|
|
|
76
|
|
|
$result = $statement->fetchColumn(); |
|
77
|
|
|
$statement->closeCursor(); |
|
78
|
|
|
|
|
79
|
|
|
return $result; |
|
80
|
|
|
} |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
private function getToday() |
|
83
|
|
|
{ |
|
84
|
|
|
$query = <<<QUERY |
|
85
|
|
|
SELECT |
|
86
|
|
|
COUNT(*) AS count |
|
87
|
|
|
FROM log |
|
88
|
|
|
LEFT JOIN emailtemplate ON concat('Closed ', emailtemplate.id) = log.action |
|
89
|
|
|
INNER JOIN user ON log.user = user.id |
|
90
|
|
|
WHERE |
|
91
|
|
|
log.timestamp LIKE :date |
|
92
|
|
|
AND (oncreated = '1' OR log.action = 'Closed custom-y') |
|
93
|
|
|
AND user.username = :username; |
|
94
|
|
|
QUERY; |
|
95
|
|
|
|
|
96
|
|
|
$statement = $this->getDatabase()->prepare($query); |
|
97
|
|
|
$statement->bindValue(":username", $this->user->getUsername()); |
|
|
|
|
|
|
98
|
|
|
$statement->bindValue(":date", date('Y-m-d') . "%"); |
|
|
|
|
|
|
99
|
|
|
$statement->execute(); |
|
100
|
|
|
$today = $statement->fetchColumn(); |
|
101
|
|
|
$statement->closeCursor(); |
|
102
|
|
|
|
|
103
|
|
|
return $today; |
|
104
|
|
|
} |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
private function fetchAdminData(DOMElement $userElement) |
|
107
|
|
|
{ |
|
108
|
|
|
$query = "SELECT COUNT(*) AS count FROM log WHERE log.user = :userid AND log.action = :action;"; |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
$statement = $this->getDatabase()->prepare($query); |
|
111
|
|
|
$statement->bindValue(":userid", $this->user->getId()); |
|
|
|
|
|
|
112
|
|
|
$statement->bindValue(":action", "Suspended"); |
|
|
|
|
|
|
113
|
|
|
$statement->execute(); |
|
114
|
|
|
$sus = $statement->fetchColumn(); |
|
115
|
|
|
$userElement->setAttribute("suspended", $sus); |
|
|
|
|
|
|
116
|
|
|
$statement->closeCursor(); |
|
117
|
|
|
|
|
118
|
|
|
$statement->bindValue(":action", "Promoted"); |
|
|
|
|
|
|
119
|
|
|
$statement->execute(); |
|
120
|
|
|
$pro = $statement->fetchColumn(); |
|
121
|
|
|
$userElement->setAttribute("promoted", $pro); |
|
|
|
|
|
|
122
|
|
|
$statement->closeCursor(); |
|
123
|
|
|
|
|
124
|
|
|
$statement->bindValue(":action", "Approved"); |
|
|
|
|
|
|
125
|
|
|
$statement->execute(); |
|
126
|
|
|
$app = $statement->fetchColumn(); |
|
127
|
|
|
$userElement->setAttribute("approved", $app); |
|
|
|
|
|
|
128
|
|
|
$statement->closeCursor(); |
|
129
|
|
|
|
|
130
|
|
|
$statement->bindValue(":action", "Demoted"); |
|
|
|
|
|
|
131
|
|
|
$statement->execute(); |
|
132
|
|
|
$dem = $statement->fetchColumn(); |
|
133
|
|
|
$userElement->setAttribute("demoted", $dem); |
|
|
|
|
|
|
134
|
|
|
$statement->closeCursor(); |
|
135
|
|
|
|
|
136
|
|
|
$statement->bindValue(":action", "Declined"); |
|
|
|
|
|
|
137
|
|
|
$statement->execute(); |
|
138
|
|
|
$dec = $statement->fetchColumn(); |
|
139
|
|
|
$userElement->setAttribute("declined", $dec); |
|
|
|
|
|
|
140
|
|
|
$statement->closeCursor(); |
|
141
|
|
|
|
|
142
|
|
|
$statement->bindValue(":action", "Renamed"); |
|
|
|
|
|
|
143
|
|
|
$statement->execute(); |
|
144
|
|
|
$rnc = $statement->fetchColumn(); |
|
145
|
|
|
$userElement->setAttribute("renamed", $rnc); |
|
|
|
|
|
|
146
|
|
|
$statement->closeCursor(); |
|
147
|
|
|
|
|
148
|
|
|
$statement->bindValue(":action", "Edited"); |
|
|
|
|
|
|
149
|
|
|
$statement->execute(); |
|
150
|
|
|
$mec = $statement->fetchColumn(); |
|
151
|
|
|
$userElement->setAttribute("edited", $mec); |
|
|
|
|
|
|
152
|
|
|
$statement->closeCursor(); |
|
153
|
|
|
|
|
154
|
|
|
$statement->bindValue(":action", "Prefchange"); |
|
|
|
|
|
|
155
|
|
|
$statement->execute(); |
|
156
|
|
|
$pcc = $statement->fetchColumn(); |
|
157
|
|
|
$userElement->setAttribute("prefchange", $pcc); |
|
|
|
|
|
|
158
|
|
|
$statement->closeCursor(); |
|
159
|
|
|
|
|
160
|
|
|
// Combine all three actions affecting Welcome templates into one count. |
|
161
|
|
|
$combinedquery = $this->getDatabase()->prepare(<<<SQL |
|
162
|
|
|
SELECT |
|
163
|
|
|
COUNT(*) AS count |
|
164
|
|
|
FROM log |
|
165
|
|
|
WHERE log.user = :userid |
|
166
|
|
|
AND log.action IN ('CreatedTemplate', 'EditedTemplate', 'DeletedTemplate'); |
|
167
|
|
|
SQL |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
|
|
$combinedquery->bindValue(":userid", $this->user->getId()); |
|
|
|
|
|
|
171
|
|
|
$combinedquery->execute(); |
|
172
|
|
|
$dtc = $combinedquery->fetchColumn(); |
|
173
|
|
|
$userElement->setAttribute("welctempchange", $dtc); |
|
|
|
|
|
|
174
|
|
|
$combinedquery->closeCursor(); |
|
175
|
|
|
|
|
176
|
|
|
// Combine both actions affecting Email templates into one count. |
|
177
|
|
|
$combinedquery = $this->getDatabase()->prepare(<<<SQL |
|
178
|
|
|
SELECT COUNT(*) AS count |
|
179
|
|
|
FROM log |
|
180
|
|
|
WHERE log.user = :userid |
|
181
|
|
|
AND log.action IN ('CreatedEmail', 'EditedEmail'); |
|
182
|
|
|
SQL |
|
183
|
|
|
); |
|
184
|
|
|
|
|
185
|
|
|
$combinedquery->bindValue(":userid", $this->user->getId()); |
|
|
|
|
|
|
186
|
|
|
$combinedquery->execute(); |
|
187
|
|
|
$cec = $combinedquery->fetchColumn(); |
|
188
|
|
|
$userElement->setAttribute("emailtempchange", $cec); |
|
|
|
|
|
|
189
|
|
|
$combinedquery->closeCursor(); |
|
190
|
|
|
} |
|
|
|
|
|
|
191
|
|
|
} |
|
|
|
|
|
|
192
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'or with double quotes"literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\') and the backslash (\\). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is ValueIf your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.