|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains only the CategoryEdits class. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
declare(strict_types = 1); |
|
7
|
|
|
|
|
8
|
|
|
namespace AppBundle\Model; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* CategoryEdits returns statistics about edits made by a user to pages in given categories. |
|
12
|
|
|
*/ |
|
13
|
|
|
class CategoryEdits extends Model |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var string[] The categories. */ |
|
16
|
|
|
protected $categories; |
|
17
|
|
|
|
|
18
|
|
|
/** @var Edit[] The list of contributions. */ |
|
19
|
|
|
protected $categoryEdits; |
|
20
|
|
|
|
|
21
|
|
|
/** @var int Total number of edits. */ |
|
22
|
|
|
protected $editCount; |
|
23
|
|
|
|
|
24
|
|
|
/** @var int Total number of edits within the category. */ |
|
25
|
|
|
protected $categoryEditCount; |
|
26
|
|
|
|
|
27
|
|
|
/** @var array Counts of edits within each category, keyed by category name. */ |
|
28
|
|
|
protected $categoryCounts; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Constructor for the CategoryEdits class. |
|
32
|
|
|
* @param Project $project |
|
33
|
|
|
* @param User $user |
|
34
|
|
|
* @param array $categories |
|
35
|
|
|
* @param int|false $start As Unix timestamp. |
|
36
|
|
|
* @param int|false $end As Unix timestamp. |
|
37
|
|
|
* @param int $offset Used for pagination, offset results by N edits. |
|
38
|
|
|
*/ |
|
39
|
3 |
|
public function __construct( |
|
40
|
|
|
Project $project, |
|
41
|
|
|
User $user, |
|
42
|
|
|
array $categories, |
|
43
|
|
|
$start = false, |
|
44
|
|
|
$end = false, |
|
45
|
|
|
$offset = 0 |
|
46
|
|
|
) { |
|
47
|
3 |
|
$this->project = $project; |
|
48
|
3 |
|
$this->user = $user; |
|
49
|
3 |
|
$this->categories = array_map(function ($category) { |
|
50
|
3 |
|
return str_replace(' ', '_', $category); |
|
51
|
3 |
|
}, $categories); |
|
52
|
3 |
|
$this->start = false === $start ? '' : date('Y-m-d', $start); |
|
53
|
3 |
|
$this->end = false === $end ? '' : date('Y-m-d', $end); |
|
54
|
3 |
|
$this->offset = (int)$offset; |
|
55
|
3 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get the categories. |
|
59
|
|
|
* @return string[] |
|
60
|
|
|
*/ |
|
61
|
1 |
|
public function getCategories(): array |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->categories; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the categories as a piped string. |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function getCategoriesPiped(): string |
|
71
|
|
|
{ |
|
72
|
1 |
|
return implode('|', $this->categories); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Get the categories as an array of normalized strings (without namespace). |
|
77
|
|
|
* @return string[] |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getCategoriesNormalized(): array |
|
80
|
|
|
{ |
|
81
|
1 |
|
return array_map(function ($category) { |
|
82
|
1 |
|
return str_replace('_', ' ', $category); |
|
83
|
1 |
|
}, $this->categories); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get the raw edit count of the user. |
|
88
|
|
|
* @return int |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function getEditCount(): int |
|
91
|
|
|
{ |
|
92
|
1 |
|
if (!is_int($this->editCount)) { |
|
|
|
|
|
|
93
|
1 |
|
$this->editCount = $this->user->countEdits( |
|
94
|
1 |
|
$this->project, |
|
95
|
1 |
|
'all', |
|
96
|
1 |
|
$this->start, |
|
97
|
1 |
|
$this->end |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
return $this->editCount; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Get the number of edits this user made within the categories. |
|
106
|
|
|
* @return int Result of query, see below. |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function getCategoryEditCount(): int |
|
109
|
|
|
{ |
|
110
|
1 |
|
if (is_int($this->categoryEditCount)) { |
|
|
|
|
|
|
111
|
1 |
|
return $this->categoryEditCount; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
1 |
|
$this->categoryEditCount = (int)$this->getRepository()->countCategoryEdits( |
|
115
|
1 |
|
$this->project, |
|
116
|
1 |
|
$this->user, |
|
117
|
1 |
|
$this->categories, |
|
118
|
1 |
|
$this->start, |
|
119
|
1 |
|
$this->end |
|
120
|
|
|
); |
|
121
|
|
|
|
|
122
|
1 |
|
return $this->categoryEditCount; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get the percentage of all edits made to the categories. |
|
127
|
|
|
* @return float |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function getCategoryPercentage(): float |
|
130
|
|
|
{ |
|
131
|
1 |
|
return $this->getEditCount() > 0 |
|
132
|
1 |
|
? ($this->getCategoryEditCount() / $this->getEditCount()) * 100 |
|
133
|
1 |
|
: 0; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get the number of pages edited. |
|
138
|
|
|
* @return int |
|
139
|
|
|
*/ |
|
140
|
|
|
public function getCategoryPageCount(): int |
|
141
|
|
|
{ |
|
142
|
|
|
$pageCount = 0; |
|
143
|
|
|
foreach ($this->getCategoryCounts() as $categoryCount) { |
|
144
|
|
|
$pageCount += $categoryCount['pageCount']; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $pageCount; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get contributions made to the categories. |
|
152
|
|
|
* @param bool $raw Wether to return raw data from the database, or get Edit objects. |
|
153
|
|
|
* @return string[]|Edit[] |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public function getCategoryEdits(bool $raw = false) |
|
156
|
|
|
{ |
|
157
|
1 |
|
if (is_array($this->categoryEdits)) { |
|
|
|
|
|
|
158
|
1 |
|
return $this->categoryEdits; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
1 |
|
$revs = $this->getRepository()->getCategoryEdits( |
|
162
|
1 |
|
$this->project, |
|
163
|
1 |
|
$this->user, |
|
164
|
1 |
|
$this->categories, |
|
165
|
1 |
|
$this->start, |
|
166
|
1 |
|
$this->end, |
|
167
|
1 |
|
$this->offset |
|
168
|
|
|
); |
|
169
|
|
|
|
|
170
|
1 |
|
if ($raw) { |
|
171
|
1 |
|
return $revs; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
1 |
|
$this->categoryEdits = Edit::getEditsFromRevs($this->project, $this->user, $revs); |
|
175
|
|
|
|
|
176
|
1 |
|
return $this->categoryEdits; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Get counts of edits made to each individual category. |
|
181
|
|
|
* @return array Counts, keyed by category name. |
|
182
|
|
|
*/ |
|
183
|
1 |
|
public function getCategoryCounts(): array |
|
184
|
|
|
{ |
|
185
|
1 |
|
if (is_array($this->categoryCounts)) { |
|
|
|
|
|
|
186
|
1 |
|
return $this->categoryCounts; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
1 |
|
$this->categoryCounts = $this->getRepository()->getCategoryCounts( |
|
190
|
1 |
|
$this->project, |
|
191
|
1 |
|
$this->user, |
|
192
|
1 |
|
$this->categories, |
|
193
|
1 |
|
$this->start, |
|
194
|
1 |
|
$this->end |
|
195
|
|
|
); |
|
196
|
|
|
|
|
197
|
1 |
|
return $this->categoryCounts; |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|