Completed
Push — master ( 684e0f...bce5b6 )
by
unknown
02:51
created

PagesRepository::getAssessments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
1
<?php
2
/**
3
 * This file contains only the PagesRepository class.
4
 */
5
6
namespace Xtools;
7
8
use Mediawiki\Api\SimpleRequest;
9
10
/**
11
 * A PagesRepository fetches data about Pages, either singularly or for multiple.
12
 */
13
class PagesRepository extends Repository
14
{
15
16
    /**
17
     * Get metadata about a single page from the API.
18
     * @param Project $project The project to which the page belongs.
19
     * @param string $pageTitle Page title.
20
     * @return string[] Array with some of the following keys: pageid, title, missing, displaytitle,
21
     * url.
22
     */
23
    public function getPageInfo(Project $project, $pageTitle)
24
    {
25
        $info = $this->getPagesInfo($project, [$pageTitle]);
26
        return array_shift($info);
27
    }
28
29
    /**
30
     * Get metadata about a set of pages from the API.
31
     * @param Project $project The project to which the pages belong.
32
     * @param string[] $pageTitles Array of page titles.
33
     * @return string[] Array keyed by the page names, each element with some of the
34
     * following keys: pageid, title, missing, displaytitle, url.
35
     */
36
    public function getPagesInfo(Project $project, $pageTitles)
37
    {
38
        // @TODO: Also include 'extlinks' prop when we start checking for dead external links.
39
        $params = [
40
            'prop' => 'info|pageprops',
41
            'inprop' => 'protection|talkid|watched|watchers|notificationtimestamp|subjectid|url|readable|displaytitle',
42
            'converttitles' => '',
43
            // 'ellimit' => 20,
1 ignored issue
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
            // 'elexpandurl' => '',
1 ignored issue
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
45
            'titles' => join('|', $pageTitles),
46
            'formatversion' => 2
47
            // 'pageids' => $pageIds // FIXME: allow page IDs
1 ignored issue
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
48
        ];
49
50
        $query = new SimpleRequest('query', $params);
51
        $api = $this->getMediawikiApi($project);
52
        $res = $api->getRequest($query);
53
        $result = [];
54
        if (isset($res['query']['pages'])) {
55
            foreach ($res['query']['pages'] as $pageInfo) {
56
                $result[$pageInfo['title']] = $pageInfo;
57
            }
58
        }
59
        return $result;
60
    }
61
62
    /**
63
     * Get revisions of a single page.
64
     * @param Page $page The page.
65
     * @param User|null $user Specify to get only revisions by the given user.
66
     * @return string[] Each member with keys: id, timestamp, length-
67
     */
68 View Code Duplication
    public function getRevisions(Page $page, User $user = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        $revTable = $this->getTableName($page->getProject()->getDatabaseName(), 'revision');
71
        $userClause = $user ? "revs.rev_user_text in (:username) AND " : "";
72
73
        $query = "SELECT
74
                    revs.rev_id AS id,
75
                    revs.rev_timestamp AS timestamp,
76
                    revs.rev_minor_edit AS minor,
77
                    revs.rev_len AS length,
78
                    (CAST(revs.rev_len AS SIGNED) - IFNULL(parentrevs.rev_len, 0)) AS length_change,
79
                    revs.rev_user AS user_id,
80
                    revs.rev_user_text AS username,
81
                    revs.rev_comment AS comment
82
                FROM $revTable AS revs
83
                LEFT JOIN $revTable AS parentrevs ON (revs.rev_parent_id = parentrevs.rev_id)
84
                WHERE $userClause revs.rev_page = :pageid
85
                ORDER BY revs.rev_timestamp ASC
86
            ";
87
        $params = ['pageid' => $page->getId()];
88
        if ($user) {
89
            $params['username'] = $user->getUsername();
90
        }
91
        $conn = $this->getProjectsConnection();
92
        return $conn->executeQuery($query, $params)->fetchAll();
93
    }
94
95
    /**
96
     * Get a count of the number of revisions of a single page
97
     * @param Page $page The page.
98
     * @param User|null $user Specify to only count revisions by the given user.
99
     * @return int
100
     */
101 View Code Duplication
    public function getNumRevisions(Page $page, User $user = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
    {
103
        $revTable = $this->getTableName($page->getProject()->getDatabaseName(), 'revision');
104
        $userClause = $user ? "rev_user_text in (:username) AND " : "";
105
106
        $query = "SELECT COUNT(*)
107
                FROM $revTable
108
                WHERE $userClause rev_page = :pageid
109
            ";
110
        $params = ['pageid' => $page->getId()];
111
        if ($user) {
112
            $params['username'] = $user->getUsername();
113
        }
114
        $conn = $this->getProjectsConnection();
115
        return $conn->executeQuery($query, $params)->fetchColumn(0);
116
    }
117
118
    /**
119
     * Get assessment data for the given pages
120
     * @param Project   $project The project to which the pages belong.
121
     * @param  int[]    $pageIds Page IDs
122
     * @return string[] Assessment data as retrieved from the database.
123
     */
124
    public function getAssessments(Project $project, $pageIds)
125
    {
126
        if (!$project->hasPageAssessments()) {
127
            return [];
128
        }
129
        $pageAssessmentsTable = $this->getTableName($project->getDatabaseName(), 'page_assessments');
0 ignored issues
show
Unused Code introduced by
$pageAssessmentsTable is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
130
        $pageIds = implode($pageIds, ',');
131
132
        $query = "SELECT pap_project_title AS wikiproject, pa_class AS class, pa_importance AS importance
133
                  FROM page_assessments
134
                  LEFT JOIN page_assessments_projects ON pa_project_id = pap_project_id
135
                  WHERE pa_page_id IN ($pageIds)";
136
137
        $conn = $this->getProjectsConnection();
138
        return $conn->executeQuery($query)->fetchAll();
139
    }
140
}
141