Completed
Push — master ( 1ced49...3e4594 )
by Sam
03:18
created

ProjectRepository::getDefaultProject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Xtools;
4
5
use Mediawiki\Api\MediawikiApi;
6
use Mediawiki\Api\SimpleRequest;
7
use Symfony\Component\DependencyInjection\Container;
8
9
class ProjectRepository extends Repository
10
{
11
12
    /** @var array Project metadata. */
13
    protected $metadata;
14
15
    /** @var string[] */
16
    protected $singleMetadata;
17
18
    /**
19
     * Convenience method to get a new Project object based on a given identification string.
20
     * @param string $projectIdent The domain name, database name, or URL of a project.
21
     * @param Container $container Symfony's container.
22
     * @return Project
23
     */
24
    public static function getProject($projectIdent, Container $container)
25
    {
26
        $project = new Project($projectIdent);
27
        $projectRepo = new ProjectRepository();
28
        $projectRepo->setContainer($container);
29
        if ($container->getParameter('app.single_wiki')) {
30
            $projectRepo->setSingleMetadata([
31
                'url' => $container->getParameter('wiki_url'),
32
                'dbname' => $container->getParameter('database_replica_name'),
33
            ]);
34
        }
35
        $project->setRepository($projectRepo);
36
        return $project;
37
    }
38
39
    /**
40
     * Get the XTools default project.
41
     * @param Container $container
42
     * @return Project
43
     */
44
    public static function getDefaultProject(Container $container)
45
    {
46
        $defaultProjectName = $container->getParameter('default_project');
47
        return self::getProject($defaultProjectName, $container);
48
    }
49
50
    /**
51
     * For single-wiki installations, you must manually set the wiki URL and database name
52
     * (because there's no meta.wiki database to query).
53
     * @param $metadata
54
     * @throws \Exception
55
     */
56
    public function setSingleMetadata($metadata)
57
    {
58
        if (!array_key_exists('url', $metadata) || !array_key_exists('dbname', $metadata)) {
59
            $error = "Single-wiki metadata should contain 'url' and 'dbname' keys.";
60
            throw new \Exception($error);
61
        }
62
        $this->singleMetadata = array_intersect_key($metadata, ['url' => '', 'dbname' => '']);
63
    }
64
65
    /**
66
     * Get metadata about all projects.
67
     * @return string[] Each item has 'dbname' and 'url' keys.
68
     */
69
    public function getAll()
70
    {
71
        if ($this->singleMetadata) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->singleMetadata of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
72
            return [$this->getOne('')];
0 ignored issues
show
Bug Best Practice introduced by
The return type of return array($this->getOne('')); (string[][]) is incompatible with the return type documented by Xtools\ProjectRepository::getAll of type string[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
73
        }
74
        $wikiQuery = $this->getMetaConnection()->createQueryBuilder();
75
        $wikiQuery->select(['dbname', 'url'])->from('wiki');
76
        return $wikiQuery->execute()->fetchAll();
77
    }
78
79
    /**
80
     * Get metadata about one project.
81
     * @param string $project A project URL, domain name, or database name.
82
     * @return string[] With 'dbname' and 'url' keys.
83
     */
84
    public function getOne($project)
85
    {
86
        // For single-wiki setups, every project is the same.
87
        if ($this->singleMetadata) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->singleMetadata of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
88
            return $this->singleMetadata;
89
        }
90
91
        // For muli-wiki setups, first check the cache.
92
        $cacheKey = "project.$project";
93
        if ($this->cache->hasItem($cacheKey)) {
94
            return $this->cache->getItem($cacheKey)->get();
95
        }
96
97
        // Otherwise, fetch the project's metadata from the meta.wiki table.
98
        $wikiQuery = $this->getMetaConnection()->createQueryBuilder();
99
        $wikiQuery->select(['dbname', 'url'])
100
            ->from('wiki')
101
            ->where($wikiQuery->expr()->eq('dbname', ':project'))
102
            // The meta database will have the project's URL stored as https://en.wikipedia.org
103
            // so we need to query for it accordingly, trying different variations the user
104
            // might have inputted.
105
            ->orwhere($wikiQuery->expr()->like('url', ':projectUrl'))
106
            ->orwhere($wikiQuery->expr()
107
                ->like('url', ':projectUrl2'))
108
            ->setParameter('project', $project)
109
            ->setParameter('projectUrl', "https://$project")
110
            ->setParameter('projectUrl2', "https://$project.org");
111
        $wikiStatement = $wikiQuery->execute();
112
113
        // Fetch and cache the wiki data.
114
        $projectMetadata = $wikiStatement->fetch();
115
        $cacheItem = $this->cache->getItem($cacheKey);
116
        $cacheItem->set($projectMetadata)
117
            ->expiresAfter(new \DateInterval('PT1H'));
118
        $this->cache->save($cacheItem);
119
120
        return $projectMetadata;
121
    }
122
123
    /**
124
     * Get metadata about a project.
125
     *
126
     * @param string $projectUrl The project's URL.
127
     * @return array With 'general' and 'namespaces' keys: the former contains 'wikiName',
128
     * 'wikiId', 'url', 'lang', 'articlePath', 'scriptPath', 'script', 'timezone', and
129
     * 'timezoneOffset'; the latter contains all namespace names, keyed by their IDs.
130
     */
131
    public function getMetadata($projectUrl)
132
    {
133
        if ($this->metadata) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->metadata of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
134
            return $this->metadata;
135
        }
136
        
137
        $api = MediawikiApi::newFromPage($projectUrl);
138
139
        $params = ['meta' => 'siteinfo', 'siprop' => 'general|namespaces'];
140
        $query = new SimpleRequest('query', $params);
141
142
        $this->metadata = [
143
            'general' => [],
144
            'namespaces' => [],
145
        ];
146
147
        $res = $api->getRequest($query);
148
149
        if (isset($res['query']['general'])) {
150
            $info = $res['query']['general'];
151
            $this->metadata['general'] = [
152
                'wikiName' => $info['sitename'],
153
                'wikiId' => $info['wikiid'],
154
                'url' => $info['server'],
155
                'lang' => $info['lang'],
156
                'articlePath' => $info['articlepath'],
157
                'scriptPath' => $info['scriptpath'],
158
                'script' => $info['script'],
159
                'timezone' => $info['timezone'],
160
                'timeOffset' => $info['timeoffset'],
161
            ];
162
163
//            if ($this->container->getParameter('app.is_labs') &&
1 ignored issue
show
Unused Code Comprehensibility introduced by
59% 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...
164
//                substr($result['general']['dbName'], -2) != '_p'
165
//            ) {
166
//                $result['general']['dbName'] .= '_p';
167
//            }
168
        }
169
170 View Code Duplication
        if (isset($res['query']['namespaces'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
171
            foreach ($res['query']['namespaces'] as $namespace) {
172
                if ($namespace['id'] < 0) {
173
                    continue;
174
                }
175
176
                if (isset($namespace['name'])) {
177
                    $name = $namespace['name'];
178
                } elseif (isset($namespace['*'])) {
179
                    $name = $namespace['*'];
180
                } else {
181
                    continue;
182
                }
183
184
                // FIXME: Figure out a way to i18n-ize this
185
                if ($name === '') {
186
                    $name = 'Article';
187
                }
188
189
                $this->metadata['namespaces'][$namespace['id']] = $name;
190
            }
191
        }
192
193
        return $this->metadata;
194
    }
195
}
196