Passed
Push — master ( f43d54...b6518a )
by MusikAnimal
01:39
created

ArticleInfoRepository::getTransclusionData()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 16
nc 2
nop 1
dl 0
loc 26
ccs 0
cts 16
cp 0
crap 6
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file contains only the ArticleInfoRepository class.
4
 */
5
6
namespace Xtools;
7
8
use GuzzleHttp;
9
10
/**
11
 * ArticleInfoRepository is responsible for retrieving data about a single
12
 * article on a given wiki.
13
 */
14
class ArticleInfoRepository extends Repository
15
{
16
    /**
17
     * Get the number of edits made to the page by bots or former bots.
18
     * @param  Page $page
19
     * @param  false|int $start
20
     * @param  false|int $end
21
     * @return \Doctrine\DBAL\Driver\Statement resolving with keys 'count', 'username' and 'current'.
22
     */
23
    public function getBotData(Page $page, $start, $end)
24
    {
25
        $project = $page->getProject();
26
        $userGroupsTable = $project->getTableName('user_groups');
27
        $userFormerGroupsTable = $project->getTableName('user_former_groups');
28
29
        $datesConditions = $this->createDatesConditions($start, $end);
30
31
        $sql = "SELECT COUNT(rev_user_text) AS count, rev_user_text AS username, ug_group AS current
32
                FROM " . $project->getTableName('revision') . "
33
                LEFT JOIN $userGroupsTable ON rev_user = ug_user
34
                LEFT JOIN $userFormerGroupsTable ON rev_user = ufg_user
35
                WHERE rev_page = :pageId AND (ug_group = 'bot' OR ufg_group = 'bot') $datesConditions
36
                GROUP BY rev_user_text";
37
        $pageId = $page->getId();
38
        $resultQuery = $this->getProjectsConnection()->prepare($sql);
39
        $resultQuery->bindParam('pageId', $pageId);
40
        $resultQuery->execute();
41
        return $resultQuery;
42
    }
43
44
    /**
45
     * Get prior deletions, page moves, and protections to the page.
46
     * @param Page $page
47
     * @param false|int $start
48
     * @param false|int $end
49
     * @return string[] each entry with keys 'log_action', 'log_type' and 'timestamp'.
50
     */
51
    public function getLogEvents(Page $page, $start, $end)
52
    {
53
        $loggingTable = $page->getProject()->getTableName('logging', 'logindex');
54
55
        $datesConditions = $this->createDatesConditions($start, $end, '', 'log_timestamp');
56
57
        $sql = "SELECT log_action, log_type, log_timestamp AS 'timestamp'
58
                FROM $loggingTable
59
                WHERE log_namespace = '" . $page->getNamespace() . "'
60
                AND log_title = :title AND log_timestamp > 1 $datesConditions
61
                AND log_type IN ('delete', 'move', 'protect', 'stable')";
62
        $title = str_replace(' ', '_', $page->getTitle());
63
        $resultQuery = $this->getProjectsConnection()->prepare($sql);
64
        $resultQuery->bindParam(':title', $title);
65
        $resultQuery->execute();
66
        return $resultQuery->fetchAll();
67
    }
68
69
    /**
70
     * Query the WikiWho service to get authorship percentages.
71
     * @see https://api.wikiwho.net/
72
     * @param Page $page
73
     * @return array[] Response from WikiWho.
74
     */
75
    public function getTextshares(Page $page)
76
    {
77
        $title = rawurlencode(str_replace(' ', '_', $page->getTitle()));
78
        $client = new GuzzleHttp\Client();
79
80
        $projectLang = $page->getProject()->getLang();
81
82
        $url = "https://api.wikiwho.net/$projectLang/api/v1.0.0-beta/rev_content/" .
83
            "$title/?o_rev_id=false&editor=true&token_id=false&out=false&in=false";
84
85
        $res = $client->request('GET', $url, ['http_errors' => false]);
86
        return json_decode($res->getBody()->getContents(), true);
87
    }
88
89
    /**
90
     * Get a map of user IDs/usernames given the user IDs.
91
     * @param  Project $project
92
     * @param  int[]   $userIds
93
     * @return array
94
     */
95
    public function getUsernamesFromIds(Project $project, $userIds)
96
    {
97
        $userTable = $project->getTableName('user');
98
        $userIds = implode(',', $userIds);
99
        $sql = "SELECT user_id, user_name
100
                FROM $userTable
101
                WHERE user_id IN ($userIds)";
102
        $resultQuery = $this->getProjectsConnection()->prepare($sql);
103
        $resultQuery->execute();
104
        return $resultQuery->fetchAll();
105
    }
106
107
    /**
108
     * Get the number of categories, templates, and files that are on the page.
109
     * @param  Page $page
110
     * @return array With keys 'categories', 'templates' and 'files'.
111
     */
112
    public function getTransclusionData(Page $page)
113
    {
114
        $categorylinksTable = $page->getProject()->getTableName('categorylinks');
115
        $templatelinksTable = $page->getProject()->getTableName('templatelinks');
116
        $imagelinksTable = $page->getProject()->getTableName('imagelinks');
117
        $pageId = $page->getId();
118
        $sql = "(
119
                    SELECT 'categories' AS `key`, COUNT(*) AS val
120
                    FROM $categorylinksTable
121
                    WHERE cl_from = $pageId
122
                ) UNION (
123
                    SELECT 'templates' AS `key`, COUNT(*) AS val
124
                    FROM $templatelinksTable
125
                    WHERE tl_from = $pageId
126
                ) UNION (
127
                    SELECT 'files' AS `key`, COUNT(*) AS val
128
                    FROM $imagelinksTable
129
                    WHERE il_from = $pageId
130
                )";
131
        $resultQuery = $this->getProjectsConnection()->query($sql);
132
        $transclusionCounts = [];
133
        while ($result = $resultQuery->fetch()) {
134
            $transclusionCounts[$result['key']] = $result['val'];
135
        }
136
137
        return $transclusionCounts;
138
    }
139
}
140