Completed
Push — master ( 61818c...b240d3 )
by Sam
03:00
created

PagesRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 0
cbo 3
dl 0
loc 54
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPageInfo() 0 5 1
B getPagesInfo() 0 28 4
1
<?php
2
3
namespace Xtools;
4
5
use Mediawiki\Api\SimpleRequest;
6
7
/**
8
 * A PagesRepository fetches data about Pages, either singularly or for multiple.
9
 */
10
class PagesRepository extends Repository
11
{
12
13
    /**
14
     * Get metadata about a single page from the API.
15
     * @param Project $project The project to which the page belongs.
16
     * @param string $pageTitle Page title.
17
     * @param boolean $followRedirects Whether or not to resolve redirects
18
     * @return string[] Array with some of the following keys: pageid, title, missing, displaytitle,
19
     * url.
20
     */
21
    public function getPageInfo(Project $project, $pageTitle, $followRedirects = true)
22
    {
23
        $info = $this->getPagesInfo($project, [$pageTitle], $followRedirects);
24
        return array_shift($info);
25
    }
26
27
    /**
28
     * Get metadata about a set of pages from the API.
29
     * @param Project $project The project to which the pages belong.
30
     * @param string[] $pageTitles Array of page titles.
31
     * @param boolean $followRedirects Whether or not to resolve redirects
32
     * @return string[] Array keyed by the page names, each element with some of the
33
     * following keys: pageid, title, missing, displaytitle, url.
34
     */
35
    public function getPagesInfo(Project $project, $pageTitles, $followRedirects = true)
36
    {
37
        // @TODO: Also include 'extlinks' prop when we start checking for dead external links.
38
        $params = [
39
            'prop' => 'info|pageprops',
40
            'inprop' => 'protection|talkid|watched|watchers|notificationtimestamp|subjectid|url|readable|displaytitle',
41
            'converttitles' => '',
42
            // '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...
43
            // '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...
44
            'titles' => join('|', $pageTitles),
45
            'formatversion' => 2
46
            // '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...
47
        ];
48
        if ($followRedirects) {
49
            $params['redirects'] = '';
50
        }
51
52
        $query = new SimpleRequest('query', $params);
53
        $api = $this->getMediawikiApi($project);
54
        $res = $api->getRequest($query);
55
        $result = [];
56
        if (isset($res['query']['pages'])) {
57
            foreach ($res['query']['pages'] as $pageInfo) {
58
                $result[$pageInfo['title']] = $pageInfo;
59
            }
60
        }
61
        return $result;
62
    }
63
}
64