Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
30.00% covered (danger)
30.00%
6 / 20
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ResourceLoaderGlobalModule
30.00% covered (danger)
30.00%
6 / 20
25.00% covered (danger)
25.00%
1 / 4
61.39
0.00% covered (danger)
0.00%
0 / 1
 __construct
41.67% covered (danger)
41.67%
5 / 12
0.00% covered (danger)
0.00%
0 / 1
16.73
 getSource
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 getCurrentWikiId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDB
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @author Szymon Ćšwierkosz
20 * @author Kunal Mehta
21 */
22
23namespace MediaWiki\GlobalCssJs;
24
25use InvalidArgumentException;
26use MediaWiki\MediaWikiServices;
27use MediaWiki\ResourceLoader\WikiModule;
28use MediaWiki\WikiMap\WikiMap;
29use Wikimedia\Rdbms\IDatabase;
30
31/**
32 * Base class for global modules.
33 *
34 * This module does not provide any resources directly.
35 * It instructs ResourceLoader to load a module from a remote site.
36 */
37abstract class ResourceLoaderGlobalModule extends WikiModule {
38
39    /**
40     * name of global wiki database
41     * @var string
42     */
43    protected $wiki;
44
45    /**
46     * name of a ResourceLoader source pointing to the global wiki
47     * @var string
48     */
49    protected $source;
50
51    /**
52     * Either 'style' or 'script'
53     *
54     * @var string
55     */
56    protected $type;
57
58    /** @inheritDoc */
59    public function __construct( $options ) {
60        foreach ( $options as $member => $option ) {
61            switch ( $member ) {
62                case 'wiki':
63                case 'source':
64                    $this->{$member} = (string)$option;
65                    break;
66                case 'type':
67                    if ( $option !== 'style' && $option !== 'script' ) {
68                        throw new InvalidArgumentException(
69                            "type must be either 'style' or 'script', not '$option'"
70                        );
71                    }
72                    $this->type = $option;
73                    break;
74            }
75        }
76    }
77
78    /**
79     * @return string
80     */
81    public function getSource() {
82        return $this->getCurrentWikiId() === $this->wiki ? 'local' : $this->source;
83    }
84
85    /**
86     * @return string
87     */
88    protected function getCurrentWikiId() {
89        return WikiMap::getCurrentWikiId();
90    }
91
92    /**
93     * @return IDatabase
94     */
95    protected function getDB() {
96        $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
97        if ( $this->wiki === $this->getCurrentWikiId() ) {
98            $lb = $lbFactory->getMainLB();
99            return $lb->getConnection( DB_REPLICA );
100        } else {
101            $lb = $lbFactory->getMainLB( $this->wiki );
102            return $lb->getConnection( DB_REPLICA, [], $this->wiki );
103        }
104    }
105}