Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
21 / 28
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
NamespaceInputWidget
75.00% covered (warning)
75.00%
21 / 28
50.00% covered (danger)
50.00%
2 / 4
5.39
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getNamespaceDropdownOptions
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
 getJavaScriptClassName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getConfig
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Widget;
4
5use MediaWiki\Html\Html;
6use OOUI\DropdownInputWidget;
7use OOUI\InputWidget;
8
9/**
10 * Namespace input widget. Displays a dropdown box with the choice of available namespaces.
11 *
12 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
13 * @license MIT
14 */
15class NamespaceInputWidget extends DropdownInputWidget {
16    /** @var string */
17    protected $includeAllValue;
18    /** @var bool */
19    protected $userLang;
20    /** @var int[] */
21    protected $exclude;
22    /** @var int[]|null */
23    protected $include;
24
25    /**
26     * @param array $config Configuration options
27     *   - string $config['includeAllValue'] If specified, add a "all namespaces" option to the
28     *     namespace dropdown, and use this as the input value for it
29     *   - bool $config['userLang'] Display namespaces in user language
30     *   - int[] $config['exclude'] List of namespace numbers to exclude from the selector
31     *   - int[]|null $config['include'] List of namespace numbers to only include in the selector, or null
32     *     to not apply this filter.
33     */
34    public function __construct( array $config = [] ) {
35        // Configuration initialization
36        $config['options'] = $this->getNamespaceDropdownOptions( $config );
37
38        parent::__construct( $config );
39
40        // Properties
41        $this->includeAllValue = $config['includeAllValue'] ?? null;
42        $this->userLang = $config['userLang'] ?? false;
43        $this->exclude = $config['exclude'] ?? [];
44        $this->include = $config['include'] ?? null;
45
46        // Initialization
47        $this->addClasses( [ 'mw-widget-namespaceInputWidget' ] );
48    }
49
50    protected function getNamespaceDropdownOptions( array $config ) {
51        $namespaceOptionsParams = [
52            'all' => $config['includeAllValue'] ?? null,
53            'in-user-lang' => $config['userLang'] ?? false,
54            'exclude' => $config['exclude'] ?? null,
55            'include' => $config['include'] ?? null,
56        ];
57        $namespaceOptions = Html::namespaceSelectorOptions( $namespaceOptionsParams );
58
59        $options = [];
60        foreach ( $namespaceOptions as $id => $name ) {
61            $options[] = [
62                'data' => (string)$id,
63                'label' => $name,
64            ];
65        }
66
67        return $options;
68    }
69
70    protected function getJavaScriptClassName() {
71        return 'mw.widgets.NamespaceInputWidget';
72    }
73
74    public function getConfig( &$config ) {
75        $config['includeAllValue'] = $this->includeAllValue;
76        $config['userLang'] = $this->userLang;
77        $config['exclude'] = $this->exclude;
78        $config['include'] = $this->include;
79        $config['dropdown']['$overlay'] = true;
80        // Skip DropdownInputWidget's getConfig(), we don't need 'options' config
81        return InputWidget::getConfig( $config );
82    }
83}