Passed
Push — master ( 95d2e4...8875f5 )
by MusikAnimal
05:41
created

AutoEdits::getOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file contains only the AutoEdits class.
4
 */
5
6
namespace Xtools;
7
8
/**
9
 * AutoEdits returns statistics about automated edits made by a user.
10
 */
11
class AutoEdits extends Model
12
{
13
    /** @var null|string The tool we're searching for when fetching (semi-)automated edits. */
14
    protected $tool;
15
16
    /** @var Edit[] The list of non-automated contributions. */
17
    protected $nonAutomatedEdits;
18
19
    /** @var Edit[] The list of automated contributions. */
20
    protected $automatedEdits;
21
22
    /** @var int Total number of edits. */
23
    protected $editCount;
24
25
    /** @var int Total number of non-automated edits. */
26
    protected $automatedCount;
27
28
    /** @var array Counts of known automated tools used by the given user. */
29
    protected $toolCounts;
30
31
    /** @var int Total number of edits made with the tools. */
32
    protected $toolsTotal;
33
34
    /**
35
     * Constructor for the AutoEdits class.
36
     * @param Project $project
37
     * @param User $user
38
     * @param int|string $namespace Namespace ID or 'all'
39
     * @param int|false $start Start date in a format accepted by strtotime()
40
     * @param int|false $end End date in a format accepted by strtotime()
41
     * @param string $tool The tool we're searching for when fetching (semi-)automated edits.
42
     * @param int $offset Used for pagination, offset results by N edits.
43
     */
44 5
    public function __construct(
45
        Project $project,
46
        User $user,
47
        $namespace = 0,
48
        $start = false,
49
        $end = false,
50
        $tool = null,
51
        $offset = 0
52
    ) {
53 5
        $this->project = $project;
54 5
        $this->user = $user;
55 5
        $this->namespace = $namespace;
56 5
        $this->start = false === $start ? '' : date('Y-m-d', $start);
57 5
        $this->end = false === $end ? '' : date('Y-m-d', $end);
58 5
        $this->tool = $tool;
59 5
        $this->offset = $offset;
60 5
    }
61
62
    /**
63
     * The tool we're limiting the results to when fetching
64
     * (semi-)automated contributions.
65
     * @return null|string
66
     */
67 1
    public function getTool()
68
    {
69 1
        return $this->tool;
70
    }
71
72
    /**
73
     * Get the raw edit count of the user.
74
     * @return int
75
     */
76 1
    public function getEditCount()
77
    {
78 1
        if (!is_int($this->editCount)) {
0 ignored issues
show
introduced by
The condition is_int($this->editCount) is always true.
Loading history...
79 1
            $this->editCount = $this->user->countEdits(
80 1
                $this->project,
81 1
                $this->namespace,
82 1
                $this->start,
83 1
                $this->end
84
            );
85
        }
86
87 1
        return $this->editCount;
88
    }
89
90
    /**
91
     * Get the number of edits this user made using semi-automated tools.
92
     * This is not the same as self::getToolCounts because the regex can overlap.
93
     * @return int Result of query, see below.
94
     */
95 1
    public function getAutomatedCount()
96
    {
97 1
        if (is_int($this->automatedCount)) {
0 ignored issues
show
introduced by
The condition is_int($this->automatedCount) is always true.
Loading history...
98 1
            return $this->automatedCount;
99
        }
100
101 1
        $this->automatedCount = (int)$this->getRepository()->countAutomatedEdits(
102 1
            $this->project,
103 1
            $this->user,
104 1
            $this->namespace,
105 1
            $this->start,
106 1
            $this->end
107
        );
108
109 1
        return $this->automatedCount;
110
    }
111
112
    /**
113
     * Get the percentage of all edits made using automated tools.
114
     * @return float
115
     */
116 1
    public function getAutomatedPercentage()
117
    {
118 1
        return $this->getEditCount() > 0
119 1
            ? ($this->getAutomatedCount() / $this->getEditCount()) * 100
120 1
            : 0;
121
    }
122
123
    /**
124
     * Get non-automated contributions for this user.
125
     * @param bool $raw Wether to return raw data from the database,
126
     *   or get Edit objects.
127
     * @return string[]|Edit[]
128
     */
129 1
    public function getNonAutomatedEdits($raw = false)
130
    {
131 1
        if (is_array($this->nonAutomatedEdits)) {
0 ignored issues
show
introduced by
The condition is_array($this->nonAutomatedEdits) is always true.
Loading history...
132 1
            return $this->nonAutomatedEdits;
133
        }
134
135 1
        $revs = $this->getRepository()->getNonAutomatedEdits(
136 1
            $this->project,
137 1
            $this->user,
138 1
            $this->namespace,
139 1
            $this->start,
140 1
            $this->end,
141 1
            $this->offset
142
        );
143
144 1
        if ($raw) {
145 1
            return $revs;
146
        }
147
148 1
        $this->nonAutomatedEdits = $this->getEditsFromRevs($revs);
149
150 1
        return $this->nonAutomatedEdits;
151
    }
152
153
    /**
154
     * Get automated contributions for this user.
155
     * @param bool $raw Whether to return raw data from the database,
156
     *   or get Edit objects.
157
     * @return Edit[]
158
     */
159 1
    public function getAutomatedEdits($raw = false)
160
    {
161 1
        if (is_array($this->automatedEdits)) {
0 ignored issues
show
introduced by
The condition is_array($this->automatedEdits) is always true.
Loading history...
162 1
            return $this->automatedEdits;
163
        }
164
165 1
        $revs = $this->getRepository()->getAutomatedEdits(
166 1
            $this->project,
167 1
            $this->user,
168 1
            $this->namespace,
169 1
            $this->start,
170 1
            $this->end,
171 1
            $this->tool,
172 1
            $this->offset
173
        );
174
175 1
        if ($raw) {
176 1
            return $revs;
177
        }
178
179 1
        $this->automatedEdits = $this->getEditsFromRevs($revs);
180
181 1
        return $this->automatedEdits;
182
    }
183
184
    /**
185
     * Transform database rows into Edit objects.
186
     * @param string[] $revs
187
     * @return Edit[]
188
     */
189 2
    private function getEditsFromRevs(array $revs)
190
    {
191
        return array_map(function ($rev) {
192
            /* @var Page Page object to be passed to the Edit contructor. */
193 2
            $page = $this->getPageFromRev($rev);
194 2
            $rev['user'] = $this->user;
195
196 2
            return new Edit($page, $rev);
197 2
        }, $revs);
198
    }
199
200
    /**
201
     * Get a Page object given a revision row.
202
     * @param array $rev Revision as retrieved from the database.
203
     * @return Page
204
     */
205 2
    private function getPageFromRev(array $rev)
206
    {
207 2
        $namespaces = $this->project->getNamespaces();
208 2
        $pageTitle = $rev['page_title'];
209
210 2
        if ((int)$rev['page_namespace'] === 0) {
211 1
            $fullPageTitle = $pageTitle;
212
        } else {
213 1
            $fullPageTitle = $namespaces[$rev['page_namespace']].":$pageTitle";
214
        }
215
216 2
        return new Page($this->project, $fullPageTitle);
217
    }
218
219
    /**
220
     * Get counts of known automated tools used by the given user.
221
     * @return string[] Each tool that they used along with the count and link:
222
     *                  [
223
     *                      'Twinkle' => [
224
     *                          'count' => 50,
225
     *                          'link' => 'Wikipedia:Twinkle',
226
     *                      ],
227
     *                  ]
228
     */
229 1
    public function getToolCounts()
230
    {
231 1
        if (is_array($this->toolCounts)) {
0 ignored issues
show
introduced by
The condition is_array($this->toolCounts) is always true.
Loading history...
232 1
            return $this->toolCounts;
233
        }
234
235 1
        $this->toolCounts = $this->getRepository()->getToolCounts(
236 1
            $this->project,
237 1
            $this->user,
238 1
            $this->namespace,
239 1
            $this->start,
240 1
            $this->end
241
        );
242
243 1
        return $this->toolCounts;
244
    }
245
246
    /**
247
     * Get the combined number of edits made with each tool. This is calculated
248
     * separately from self::getAutomatedCount() because the regex can sometimes
249
     * overlap, and the counts are actually different.
250
     * @return int
251
     */
252 1
    public function getToolsTotal()
253
    {
254 1
        if (!is_int($this->toolsTotal)) {
0 ignored issues
show
introduced by
The condition is_int($this->toolsTotal) is always true.
Loading history...
255 1
            $this->toolsTotal = array_reduce($this->getToolCounts(), function ($a, $b) {
256 1
                return $a + $b['count'];
257 1
            });
258
        }
259
260 1
        return $this->toolsTotal;
261
    }
262
}
263