Passed
Push — master ( 9c80d9...628b52 )
by MusikAnimal
05:31
created

CategoryEdits   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 252
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 252
ccs 82
cts 82
cp 1
rs 10
c 0
b 0
f 0
wmc 21

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getEditCount() 0 12 2
A getStart() 0 3 1
A getCategoriesNormalized() 0 5 1
A getEditsFromRevs() 0 9 1
A getCategoryEditCount() 0 15 2
A getPageFromRev() 0 13 2
A getCategories() 0 3 1
A getCategoryPercentage() 0 5 2
A getOffset() 0 3 1
A __construct() 0 16 1
A getCategoriesPiped() 0 3 1
A getEnd() 0 3 1
A getCategoryCounts() 0 17 2
A getCategoryEdits() 0 22 3
1
<?php
2
/**
3
 * This file contains only the CategoryEdits class.
4
 */
5
6
namespace Xtools;
7
8
/**
9
 * CategoryEdits returns statistics about edits made by a user to pages in given categories.
10
 */
11
class CategoryEdits extends Model
12
{
13
    /** @var Project The project. */
14
    protected $project;
15
16
    /** @var User The user. */
17
    protected $user;
18
19
    /** @var string[] The categories. */
20
    protected $categories;
21
22
    /** @var string Start date. */
23
    protected $start;
24
25
    /** @var string End date. */
26
    protected $end;
27
28
    /** @var int Number of rows to OFFSET, used for pagination. */
29
    protected $offset;
30
31
    /** @var Edit[] The list of contributions. */
32
    protected $categoryEdits;
33
34
    /** @var int Total number of edits. */
35
    protected $editCount;
36
37
    /** @var int Total number of edits within the category. */
38
    protected $categoryEditCount;
39
40
    /** @var array Counts of edits within each category, keyed by category name. */
41
    protected $categoryCounts;
42
43
    /**
44
     * Constructor for the CategoryEdits class.
45
     * @param Project $project
46
     * @param User $user
47
     * @param array $categories
48
     * @param string $start Start date in a format accepted by strtotime()
49
     * @param string $end End date in a format accepted by strtotime()
50
     * @param int $offset Used for pagination, offset results by N edits.
51
     */
52 3
    public function __construct(
53
        Project $project,
54
        User $user,
55
        array $categories,
56
        $start = '',
57
        $end = '',
58
        $offset = 0
59
    ) {
60 3
        $this->project = $project;
61 3
        $this->user = $user;
62
        $this->categories = array_map(function ($category) {
63 3
            return str_replace(' ', '_', $category);
64 3
        }, $categories);
65 3
        $this->start = $start;
66 3
        $this->end = $end;
67 3
        $this->offset = (int)$offset;
68 3
    }
69
70
    /**
71
     * Get the categories.
72
     * @return string[]
73
     */
74 1
    public function getCategories()
75
    {
76 1
        return $this->categories;
77
    }
78
79
    /**
80
     * Get the categories as a piped string.
81
     * @return string
82
     */
83 1
    public function getCategoriesPiped()
84
    {
85 1
        return implode('|', $this->categories);
86
    }
87
88
    /**
89
     * Get the categories as an array of normalized strings (without namespace).
90
     * @return string
91
     */
92 1
    public function getCategoriesNormalized()
93
    {
94
        return array_map(function ($category) {
95 1
            return str_replace('_', ' ', $category);
96 1
        }, $this->categories);
97
    }
98
99
    /**
100
     * Get the start date.
101
     * @return string
102
     */
103 1
    public function getStart()
104
    {
105 1
        return $this->start;
106
    }
107
108
    /**
109
     * Get the end date.
110
     * @return string
111
     */
112 1
    public function getEnd()
113
    {
114 1
        return $this->end;
115
    }
116
117
    /**
118
     * Get the offset value.
119
     * @return int
120
     */
121 1
    public function getOffset()
122
    {
123 1
        return $this->offset;
124
    }
125
126
    /**
127
     * Get the raw edit count of the user.
128
     * @return int
129
     */
130 1
    public function getEditCount()
131
    {
132 1
        if (!is_int($this->editCount)) {
0 ignored issues
show
introduced by
The condition is_int($this->editCount) is always true.
Loading history...
133 1
            $this->editCount = $this->user->countEdits(
134 1
                $this->project,
135 1
                'all',
136 1
                $this->start,
137 1
                $this->end
138
            );
139
        }
140
141 1
        return $this->editCount;
142
    }
143
144
    /**
145
     * Get the number of edits this user made within the categories.
146
     * @return int Result of query, see below.
147
     */
148 1
    public function getCategoryEditCount()
149
    {
150 1
        if (is_int($this->categoryEditCount)) {
0 ignored issues
show
introduced by
The condition is_int($this->categoryEditCount) is always true.
Loading history...
151 1
            return $this->categoryEditCount;
152
        }
153
154 1
        $this->categoryEditCount = (int)$this->getRepository()->countCategoryEdits(
155 1
            $this->project,
156 1
            $this->user,
157 1
            $this->categories,
158 1
            $this->start,
159 1
            $this->end
160
        );
161
162 1
        return $this->categoryEditCount;
163
    }
164
165
    /**
166
     * Get the percentage of all edits made to the categories.
167
     * @return float
168
     */
169 1
    public function getCategoryPercentage()
170
    {
171 1
        return $this->getEditCount() > 0
172 1
            ? ($this->getCategoryEditCount() / $this->getEditCount()) * 100
173 1
            : 0;
174
    }
175
176
    /**
177
     * Get contributions made to the categories.
178
     * @param bool $raw Wether to return raw data from the database,
179
     *   or get Edit objects.
180
     * @return string[]|Edit[]
181
     */
182 1
    public function getCategoryEdits($raw = false)
183
    {
184 1
        if (is_array($this->categoryEdits)) {
0 ignored issues
show
introduced by
The condition is_array($this->categoryEdits) is always true.
Loading history...
185 1
            return $this->categoryEdits;
186
        }
187
188 1
        $revs = $this->getRepository()->getCategoryEdits(
189 1
            $this->project,
190 1
            $this->user,
191 1
            $this->categories,
192 1
            $this->start,
193 1
            $this->end,
194 1
            $this->offset
195
        );
196
197 1
        if ($raw) {
198 1
            return $revs;
199
        }
200
201 1
        $this->categoryEdits = $this->getEditsFromRevs($revs);
202
203 1
        return $this->categoryEdits;
204
    }
205
206
    /**
207
     * Transform database rows into Edit objects.
208
     * @param  string[] $revs
209
     * @return Edit[]
210
     */
211
    private function getEditsFromRevs(array $revs)
212
    {
213 1
        return array_map(function ($rev) {
214
            /* @var Page Page object to be passed to the Edit contructor. */
215 1
            $page = $this->getPageFromRev($rev);
216 1
            $rev['user'] = $this->user;
217
218 1
            return new Edit($page, $rev);
219 1
        }, $revs);
220
    }
221
222
    /**
223
     * Get a Page object given a revision row.
224
     * @param array $rev Revision as retrieved from the database.
225
     * @return Page
226
     */
227 1
    private function getPageFromRev(array $rev)
228
    {
229 1
        $namespaces = $this->project->getNamespaces();
230 1
        $pageTitle = $rev['page_title'];
231 1
        $fullPageTitle = '';
232
233 1
        if ((int)$rev['page_namespace'] === 0) {
234 1
            $fullPageTitle = $pageTitle;
235
        } else {
236 1
            $fullPageTitle = $namespaces[$rev['page_namespace']].":$pageTitle";
237
        }
238
239 1
        return new Page($this->project, $fullPageTitle);
240
    }
241
242
    /**
243
     * Get counts of edits made to each individual category.
244
     * @return array Counts, keyed by category name.
245
     */
246 1
    public function getCategoryCounts()
247
    {
248 1
        if (is_array($this->categoryCounts)) {
0 ignored issues
show
introduced by
The condition is_array($this->categoryCounts) is always true.
Loading history...
249 1
            return $this->categoryCounts;
250
        }
251
252 1
        $this->categoryCounts = $this->getRepository()->getCategoryCounts(
253 1
            $this->project,
254 1
            $this->user,
255 1
            $this->categories,
256 1
            $this->start,
257 1
            $this->end
258
        );
259
260 1
        arsort($this->categoryCounts);
261
262 1
        return $this->categoryCounts;
263
    }
264
}
265