Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.89% covered (success)
91.89%
34 / 37
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
NamespaceList
91.89% covered (success)
91.89%
34 / 37
60.00% covered (warning)
60.00%
3 / 5
14.10
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 getReadable
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEditable
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getNamespacesByPermission
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
 skip
85.71% covered (warning)
85.71%
12 / 14
0.00% covered (danger)
0.00%
0 / 1
7.14
1<?php
2
3namespace NSFileRepo;
4
5use MediaWiki\MediaWikiServices;
6
7class NamespaceList {
8
9    /**
10     *
11     * @var \User
12     */
13    protected $user = null;
14
15    /**
16     *
17     * @var \Config
18     */
19    protected $config = null;
20
21    /**
22     *
23     * @var \Language
24     */
25    protected $lang = null;
26
27    /**
28     *
29     * @param \User $user
30     * @param \Config $config
31     * @param \Language $lang
32     */
33    public function __construct( \User $user, \Config $config, \Language $lang ) {
34        $this->user = $user;
35        $this->config = new \MultiConfig([
36            $config,
37            new \HashConfig( [
38                Config::CONFIG_SKIP_TALK => true,
39                Config::CONFIG_THRESHOLD => 0,
40                Config::CONFIG_BLACKLIST => []
41            ] )
42        ]);
43        $this->lang = $lang;
44    }
45
46    /**
47     * @return MWNamespace[] With namespace id as an index
48     */
49    public function getReadable() {
50        return $this->getNamespacesByPermission( 'read' );
51    }
52
53    /**
54     * @return MWNamespace[] With namespace id as an index
55     */
56    public function getEditable() {
57        return $this->getNamespacesByPermission( 'edit' );
58    }
59
60    protected function getNamespacesByPermission( $permission ) {
61        $availableNamespaces = $this->lang->getNamespaces();
62
63        $namespaces = [];
64        $namespaceInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
65        foreach( $availableNamespaces as $nsId => $nsText ) {
66
67            if( $this->skip( $nsId, $permission ) ) {
68                continue;
69            }
70
71            if( $nsId === NS_MAIN ) {
72                $nsText = wfMessage('nsfilerepo-nsmain')->plain();
73            }
74
75            $canonicalName = $namespaceInfo->getCanonicalName( $nsId );
76            $namespaces[$nsId] = new MWNamespace( $nsId, $canonicalName , $nsText );
77        }
78
79        return $namespaces;
80    }
81
82    protected function skip( $nsId, $permission = '' ) {
83
84        if( $nsId < $this->config->get( Config::CONFIG_THRESHOLD ) && $nsId !== NS_MAIN ) {
85            return true;
86        }
87
88        if( in_array( $nsId, $this->config->get( Config::CONFIG_BLACKLIST ) ) ) {
89            return true;
90        }
91
92        $services = MediaWikiServices::getInstance();
93        $namespaceInfo = $services->getNamespaceInfo();
94        if( $this->config->get( Config::CONFIG_SKIP_TALK )
95                && $namespaceInfo->isTalk( $nsId ) ) {
96            return true;
97        }
98
99        if( !empty( $permission ) ) {
100            $title = \Title::makeTitle( $nsId, 'Dummy' );
101            return !$services->getPermissionManager()
102                ->userCan( $permission, $this->user, $title );
103        }
104
105        return false;
106    }
107
108}