Passed
Push — master ( 95203a...4fa206 )
by MusikAnimal
04:54
created

SimpleEditCounterRepository::fetchData()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 46
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 25
nc 8
nop 5
dl 0
loc 46
rs 8.6315
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file contains only the SimpleEditCounterRepository class.
4
 */
5
6
namespace Xtools;
7
8
/**
9
 * SimpleEditCounterRepository is responsible for retrieving data
10
 * from the database for the Simple Edit Counter tool.
11
 * @codeCoverageIgnore
12
 */
13
class SimpleEditCounterRepository extends Repository
14
{
15
    /**
16
     * Execute and return results of the query used for the Simple Edit Counter.
17
     * @param  Project $project
18
     * @param  User $user
19
     * @param  int|string $namespace Namespace ID or 'all' for all namespaces.
20
     * @param  int $start Unix timestamp.
21
     * @param  int $end Unix timestamp.
22
     * @return string[] Counts, each row with keys 'source' and 'value'.
23
     */
24
    public function fetchData(Project $project, User $user, $namespace = 'all', $start = null, $end = null)
25
    {
26
        $userTable = $project->getTableName('user');
27
        $pageTable = $project->getTableName('page');
28
        $archiveTable = $project->getTableName('archive');
29
        $revisionTable = $project->getTableName('revision');
30
        $userGroupsTable = $project->getTableName('user_groups');
31
32
        $arDateConditions = $this->getDateConditions($start, $end, null, 'ar_timestamp');
33
        $revDateConditions = $this->getDateConditions($start, $end);
34
35
        $revNamespaceJoinSql = $namespace === 'all' ? '' : "JOIN $pageTable ON rev_page = page_id";
36
        $revNamespaceWhereSql = $namespace === 'all' ? '' : "AND page_namespace = $namespace";
37
        $arNamespaceWhereSql = $namespace === 'all' ? '' : "AND ar_namespace = $namespace";
38
39
        // Prepare the query and execute
40
        $resultQuery = $this->getProjectsConnection()->prepare("
41
            SELECT 'id' AS source, user_id as value
42
                FROM $userTable
43
                WHERE user_name = :username
44
            UNION
45
            SELECT 'arch' AS source, COUNT(*) AS value
46
                FROM $archiveTable
47
                WHERE ar_user_text = :username
48
                $arNamespaceWhereSql
49
                $arDateConditions
50
            UNION
51
            SELECT 'rev' AS source, COUNT(*) AS value
52
                FROM $revisionTable
53
                $revNamespaceJoinSql
54
                WHERE rev_user_text = :username
55
                $revNamespaceWhereSql
56
                $revDateConditions
57
            UNION
58
            SELECT 'groups' AS source, ug_group AS value
59
                FROM $userGroupsTable
60
                JOIN $userTable ON user_id = ug_user
61
                WHERE user_name = :username
62
        ");
63
64
        $username = $user->getUsername();
65
        $resultQuery->bindParam('username', $username);
66
        $resultQuery->execute();
67
68
        // Fetch the result data
69
        return $resultQuery->fetchAll();
70
    }
71
}
72