Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.67% covered (warning)
86.67%
13 / 15
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ResourceLoaderGlobalUserModule
86.67% covered (warning)
86.67%
13 / 15
0.00% covered (danger)
0.00%
0 / 2
10.24
0.00% covered (danger)
0.00%
0 / 1
 getPages
92.86% covered (success)
92.86%
13 / 14
0.00% covered (danger)
0.00%
0 / 1
9.03
 getGroup
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * ResourceLoader module for global user customizations.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Szymon Ćšwierkosz
22 * @author Kunal Mehta
23 */
24
25namespace MediaWiki\GlobalCssJs;
26
27use MediaWiki\MediaWikiServices;
28use MediaWiki\ResourceLoader\Context;
29
30/**
31 * Module for user customizations - runs on all wikis
32 */
33class ResourceLoaderGlobalUserModule extends ResourceLoaderGlobalModule {
34
35    /** @inheritDoc */
36    protected $origin = self::ORIGIN_USER_INDIVIDUAL;
37
38    /**
39     * @param Context $context
40     * @return array
41     */
42    protected function getPages( Context $context ) {
43        // Note: When computing meta data on a local wiki (not the central wiki),
44        // this will produce a UserIdentity object based on the local database, not the
45        // foreign/central wiki. Use this object very carefully.
46        $user = $context->getUserIdentity();
47        $tempUserConfig = MediaWikiServices::getInstance()->getTempUserConfig();
48        if ( !$user || !$user->isRegistered() || $tempUserConfig->isTempName( $user->getName() ) ) {
49            return [];
50        }
51
52        if ( !Hooks::loadForUser( $user ) ) {
53            return [];
54        }
55
56        $userpage = strtr( $user->getName(), ' ', '_' );
57        $config = $this->getConfig();
58        $pages = [];
59
60        // Note: This uses the canonical namespace prefix to ensure the same array
61        // being returned on both the local and remote wiki. This matters because
62        // this method informs getVersionHash() which is used by the browser in the
63        // request URI for the central wiki, where it should match its version hash.
64        if ( $this->type === 'style' && $config->get( 'AllowUserCss' ) ) {
65            $pages["User:$userpage/global.css"] = [ 'type' => 'style' ];
66        } elseif ( $this->type === 'script' && $config->get( 'AllowUserJs' ) ) {
67            $pages["User:$userpage/global.js"] = [ 'type' => 'script' ];
68        }
69
70        return $pages;
71    }
72
73    /**
74     * @return string
75     */
76    public function getGroup() {
77        return 'user';
78    }
79}