Passed
Push — master ( 9b6f1b...510c7f )
by MusikAnimal
04:53
created

EditSummaryRepository   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRevisions() 0 21 4
A prepareData() 0 16 3
1
<?php
2
/**
3
 * This file contains only the EditSummaryRepository class.
4
 */
5
6
namespace Xtools;
7
8
/**
9
 * An EditSummaryRepository is responsible for retrieving information from the
10
 * databases for the Edit Summary tool. It does not do any post-processing
11
 * of that data.
12
 * @codeCoverageIgnore
13
 */
14
class EditSummaryRepository extends Repository
15
{
16
    /**
17
     * Build and execute SQL to get edit summary usage.
18
     * @param Project $project The project we're working with.
19
     * @param User $user The user to process.
20
     * @param string|int $namespace Namespace ID or 'all' for all namespaces.
21
     * @return \Doctrine\DBAL\Statement
22
     */
23
    public function getRevisions(Project $project, User $user, $namespace)
24
    {
25
        $revisionTable = $project->getTableName('revision');
26
        $pageTable = $project->getTableName('page');
27
28
        $condNamespace = $namespace === 'all' ? '' : 'AND page_namespace = :namespace';
29
        $pageJoin = $namespace === 'all' ? '' : "JOIN $pageTable ON rev_page = page_id";
30
31
        $sql = "SELECT rev_comment, rev_timestamp, rev_minor_edit
32
                FROM  $revisionTable
33
    ​            $pageJoin
34
                WHERE rev_user_text = :username
35
                $condNamespace
36
                ORDER BY rev_timestamp DESC";
37
38
        $params = ['username' => $user->getUsername()];
39
        if ($namespace !== 'all') {
40
            $params['namespace'] = $namespace;
41
        }
42
43
        return $this->executeProjectsQuery($sql, $params);
44
    }
45
46
    /**
47
     * Loop through the revisions and tally up totals, based on callback
48
     * that lives in the EditSummary model.
49
     * @param Project $project
50
     * @param User $user
51
     * @param int $namespace
52
     * @param array $processRow [EditSummary instance, 'method name']
53
     * @return array The final results.
54
     */
55
    public function prepareData(Project $project, User $user, $namespace, array $processRow)
56
    {
57
        $cacheKey = $this->getCacheKey([$project, $user, $namespace], 'edit_summary_usage');
58
        if ($this->cache->hasItem($cacheKey)) {
59
            return $this->cache->getItem($cacheKey)->get();
60
        }
61
62
        $resultQuery = $this->getRevisions($project, $user, $namespace);
63
        $data = [];
64
65
        while ($row = $resultQuery->fetch()) {
66
            $data = call_user_func($processRow, $row);
67
        }
68
69
        // Cache and return.
70
        return $this->setCache($cacheKey, $data);
71
    }
72
}
73