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

Pages::getNamespace()   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
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
/**
3
 * This file contains only the Pages class.
4
 */
5
6
namespace Xtools;
7
8
use DateTime;
9
10
/**
11
 * A Pages provides statistics about the pages created by a given User.
12
 */
13
class Pages extends Model
14
{
15
    const RESULTS_LIMIT_SINGLE_NAMESPACE = 1000;
16
    const RESULTS_LIMIT_ALL_NAMESPACES = 100;
17
18
    /** @var string One of 'noredirects', 'onlyredirects' or 'all' for both. */
19
    protected $redirects;
20
21
    /** @var string One of 'live', 'deleted' or 'all' for both. */
22
    protected $deleted;
23
24
    /** @var int Pagination offset. */
25
    protected $offset;
26
27
    /** @var array The list of pages including various statistics. */
28
    protected $pages;
29
30
    /** @var array Number of redirects/pages that were created/deleted, broken down by namespace. */
31
    protected $countsByNamespace;
32
33
    /**
34
     * Pages constructor.
35
     * @param Project $project
36
     * @param User $user
37
     * @param string|int $namespace Namespace ID or 'all'.
38
     * @param string $redirects One of 'noredirects', 'onlyredirects' or 'all' for both.
39
     * @param string $deleted One of 'live', 'deleted' or 'all' for both.
40
     * @param int $offset Pagination offset.
41
     */
42 2
    public function __construct(
43
        Project $project,
44
        User $user,
45
        $namespace = 0,
46
        $redirects = 'noredirects',
47
        $deleted = 'all',
48
        $offset = 0
49
    ) {
50 2
        $this->project = $project;
51 2
        $this->user = $user;
52 2
        $this->namespace = $namespace === 'all' ? 'all' : (string)$namespace;
53 2
        $this->redirects = $redirects ?: 'noredirects';
54 2
        $this->deleted = $deleted ?: 'all';
55 2
        $this->offset = $offset;
56 2
    }
57
58
    /**
59
     * The redirects option associated with this Pages instance.
60
     * @return string
61
     */
62 1
    public function getRedirects()
63
    {
64 1
        return $this->redirects;
65
    }
66
67
    /**
68
     * The deleted pages option associated with this Page instance.
69
     * @return string
70
     */
71
    public function getDeleted()
72
    {
73
        return $this->deleted;
74
    }
75
76
    /**
77
     * Fetch and prepare the pages created by the user.
78
     * @param bool $all Whether to get *all* results. This should only be used for
79
     *     export options. HTTP and JSON should paginate.
80
     * @return array
81
     * @codeCoverageIgnore
82
     */
83
    public function prepareData($all = false)
84
    {
85
        $this->pages = [];
86
87
        foreach ($this->getNamespaces() as $ns) {
88
            $data = $this->fetchPagesCreated($ns, $all);
89
            $this->pages[$ns] = count($data) > 0
90
                ? $this->formatPages($data)[$ns]
91
                : [];
92
        }
93
94
        // $this->recreatedPages = $this->fetchRecreatedPages();
95
96
        return $this->pages;
97
    }
98
99
    /**
100
     * The public function to get the list of all pages created by the user,
101
     * up to self::resultsPerPage(), across all namespaces.
102
     * @param bool $all Whether to get *all* results. This should only be used for
103
     *     export options. HTTP and JSON should paginate.
104
     * @return array
105
     */
106 1
    public function getResults($all = false)
107
    {
108 1
        if ($this->pages === null) {
109
            $this->prepareData($all);
110
        }
111 1
        return $this->pages;
112
    }
113
114
    /**
115
     * Get the total number of pages the user has created.
116
     * @return int
117
     */
118
    public function getNumPages()
119
    {
120
        $total = 0;
121
        foreach ($this->getCounts() as $ns => $values) {
122
            $total += $values['count'];
123
        }
124
        return $total;
125
    }
126
127
    /**
128
     * Get the total number of pages we're showing data for.
129
     * @return int
130
     */
131 1
    public function getNumResults()
132
    {
133 1
        $total = 0;
134 1
        foreach ($this->getResults() as $ns => $pages) {
135 1
            $total += count($pages);
136
        }
137 1
        return $total;
138
    }
139
140
    /**
141
     * Get the total number of pages that are currently deleted.
142
     * @return int
143
     */
144 1
    public function getNumDeleted()
145
    {
146 1
        $total = 0;
147 1
        foreach ($this->getCounts() as $ns => $values) {
148 1
            $total += $values['deleted'];
149
        }
150 1
        return $total;
151
    }
152
153
    /**
154
     * Get the total number of pages that are currently redirects.
155
     * @return int
156
     */
157 1
    public function getNumRedirects()
158
    {
159 1
        $total = 0;
160 1
        foreach ($this->getCounts() as $ns => $values) {
161 1
            $total += $values['redirects'];
162
        }
163 1
        return $total;
164
    }
165
166
    /**
167
     * Get the namespaces in which this user has created pages.
168
     * @return string[] The IDs.
169
     */
170 1
    public function getNamespaces()
171
    {
172 1
        return array_keys($this->getCounts());
173
    }
174
175
    /**
176
     * Number of namespaces being reported.
177
     * @return int
178
     */
179
    public function getNumNamespaces()
180
    {
181
        return count(array_keys($this->getCounts()));
182
    }
183
184
    /**
185
     * Number of redirects/pages that were created/deleted, broken down by namespace.
186
     * @return array Namespace IDs as the keys, with values 'count', 'deleted' and 'redirects'.
187
     */
188 1
    public function getCounts()
189
    {
190 1
        if ($this->countsByNamespace !== null) {
191 1
            return $this->countsByNamespace;
192
        }
193
194 1
        $counts = [];
195
196 1
        foreach ($this->countPagesCreated() as $row) {
197 1
            $counts[$row['namespace']] = [
198 1
                'count' => (int)$row['count'],
199 1
                'deleted' => (int)$row['deleted'],
200 1
                'redirects' => (int)$row['redirects']
201
            ];
202
        }
203
204 1
        $this->countsByNamespace = $counts;
205 1
        return $this->countsByNamespace;
206
    }
207
208
    /**
209
     * Number of results to show, depending on the namespace.
210
     * @param bool $all Whether to get *all* results. This should only be used for
211
     *     export options. HTTP and JSON should paginate.
212
     * @return int
213
     */
214 1
    public function resultsPerPage($all = false)
215
    {
216 1
        if ($all === true) {
217
            return false;
218
        }
219 1
        if ($this->namespace === 'all') {
220
            return self::RESULTS_LIMIT_ALL_NAMESPACES;
221
        }
222 1
        return self::RESULTS_LIMIT_SINGLE_NAMESPACE;
223
    }
224
225
    /**
226
     * Run the query to get pages created by the user with options.
227
     * This is ran independently for each namespace if $this->namespace is 'all'.
228
     * @param string $namespace Namespace ID.
229
     * @param bool $all Whether to get *all* results. This should only be used for
230
     *     export options. HTTP and JSON should paginate.
231
     * @return array
232
     */
233 1
    private function fetchPagesCreated($namespace, $all = false)
234
    {
235 1
        return $this->getRepository()->getPagesCreated(
236 1
            $this->project,
237 1
            $this->user,
238 1
            $namespace,
239 1
            $this->redirects,
240 1
            $this->deleted,
241 1
            $this->resultsPerPage($all),
242 1
            $this->offset * $this->resultsPerPage()
243
        );
244
    }
245
246
    /**
247
     * Run the query to get the number of pages created by the user
248
     * with given options.
249
     * @return array
250
     */
251 1
    private function countPagesCreated()
252
    {
253 1
        return $this->getRepository()->countPagesCreated(
254 1
            $this->project,
255 1
            $this->user,
256 1
            $this->namespace,
257 1
            $this->redirects,
258 1
            $this->deleted
259
        );
260
    }
261
262
    /**
263
     * Format the data, adding humanized timestamps, page titles, assessment badges,
264
     * and sorting by namespace and then timestamp.
265
     * @param array $pages As returned by self::fetchPagesCreated()
266
     * @return array
267
     */
268 1
    private function formatPages($pages)
269
    {
270 1
        $results = [];
271
272 1
        foreach ($pages as $row) {
273 1
            $datetime = DateTime::createFromFormat('YmdHis', $row['rev_timestamp']);
274 1
            $datetimeHuman = $datetime->format('Y-m-d H:i');
275
276 1
            $pageData = array_merge($row, [
277 1
                'raw_time' => $row['rev_timestamp'],
278 1
                'human_time' => $datetimeHuman,
279 1
                'page_title' => str_replace('_', ' ', $row['page_title'])
280
            ]);
281
282 1
            if ($this->project->hasPageAssessments()) {
283
                $pageData['badge'] = $this->project
284
                    ->getPageAssessments()
285
                    ->getBadgeURL($pageData['pa_class']);
286
                $pageData['badgeFile'] = $this->project
287
                    ->getPageAssessments()
288
                    ->getBadgeURL($pageData['pa_class'], true);
289
            }
290
291 1
            $results[$row['namespace']][] = $pageData;
292
        }
293
294 1
        return $results;
295
    }
296
}
297