Merge "Skip adding wikitext test pages in PrefixSearchTest if NS_MAIN is not wikitext"
[lhc/web/wiklou.git] / includes / widget / NamespaceInputWidget.php
1 <?php
2 /**
3 * MediaWiki Widgets – NamespaceInputWidget class.
4 *
5 * @copyright 2011-2015 MediaWiki Widgets Team and others; see AUTHORS.txt
6 * @license The MIT License (MIT); see LICENSE.txt
7 */
8 namespace MediaWiki\Widget;
9
10 /**
11 * Namespace input widget. Displays a dropdown box with the choice of available namespaces, plus two
12 * checkboxes to include associated namespace or to invert selection.
13 */
14 class NamespaceInputWidget extends \OOUI\Widget {
15
16 protected $namespace = null;
17 protected $associated = null;
18 protected $invert = null;
19 protected $allValue = null;
20
21 /**
22 * @param array $config Configuration options
23 * @param string $config['nameNamespace'] HTML input name for the namespace dropdown box (default:
24 * 'namespace')
25 * @param string $config['nameInvert'] HTML input name for the "invert selection" checkbox. If
26 * null, the checkbox will not be generated. (default: 'invert')
27 * @param string $config['nameAssociated'] HTML input name for the "include associated namespace"
28 * checkbox. If null, the checkbox will not be generated. (default: 'associated')
29 * @param string $config['includeAllValue'] If specified, add a "all namespaces" option to the
30 * namespace dropdown, and use this as the input value for it
31 * @param int|string $config['valueNamespace'] Input value of the namespace dropdown box. May be a
32 * string only if 'includeAllValue' is set.
33 * @param boolean $config['valueInvert'] Input value of the "invert selection" checkbox (default:
34 * false)
35 * @param boolean $config['valueAssociated'] Input value of the "include associated namespace"
36 * checkbox (default: false)
37 * @param string $config['labelInvert'] Text of label to use for "invert selection" checkbox
38 * @param string $config['labelAssociated'] Text of label to use for "include associated
39 * namespace" checkbox
40 */
41 public function __construct( array $config = array() ) {
42 // Configuration initialization
43 $config = array_merge(
44 array(
45 'nameNamespace' => 'namespace',
46 'nameInvert' => 'invert',
47 'nameAssociated' => 'associated',
48 // Choose first available: either main namespace or the "all namespaces" option
49 'valueNamespace' => null,
50 'valueInvert' => false,
51 'valueAssociated' => false,
52 ),
53 $config
54 );
55
56 // Parent constructor
57 parent::__construct( $config );
58
59 // Properties
60 $this->allValue = isset( $config['includeAllValue'] ) ? $config['includeAllValue'] : null;
61 $this->namespace = new \OOUI\DropdownInputWidget( array(
62 'name' => $config['nameNamespace'],
63 'value' => $config['valueNamespace'],
64 'options' => $this->getNamespaceDropdownOptions( $config ),
65 ) );
66 if ( $config['nameAssociated'] !== null ) {
67 // FIXME Should use a LabelWidget? But they don't work like HTML <label>s yet
68 $this->associated = new \OOUI\FieldLayout(
69 new \OOUI\CheckboxInputWidget( array(
70 'name' => $config['nameAssociated'],
71 'selected' => $config['valueAssociated'],
72 'value' => '1',
73 ) ),
74 array(
75 'align' => 'inline',
76 'label' => $config['labelAssociated'],
77 )
78 );
79 }
80 if ( $config['nameInvert'] !== null ) {
81 $this->invert = new \OOUI\FieldLayout(
82 new \OOUI\CheckboxInputWidget( array(
83 'name' => $config['nameInvert'],
84 'selected' => $config['valueInvert'],
85 'value' => '1',
86 ) ),
87 array(
88 'align' => 'inline',
89 'label' => $config['labelInvert'],
90 )
91 );
92 }
93
94 // Initialization
95 $this
96 ->addClasses( array( 'mw-widget-namespaceInputWidget' ) )
97 ->appendContent( $this->namespace, $this->associated, $this->invert );
98 }
99
100 protected function getNamespaceDropdownOptions( array $config ) {
101 $namespaceOptionsParams = isset( $config['includeAllValue'] ) ?
102 array( 'all' => $config['includeAllValue'] ) : array();
103 $namespaceOptions = \Html::namespaceSelectorOptions( $namespaceOptionsParams );
104
105 $options = array();
106 foreach ( $namespaceOptions as $id => $name ) {
107 $options[] = array(
108 'data' => (string)$id,
109 'label' => $name,
110 );
111 }
112
113 return $options;
114 }
115
116 protected function getJavaScriptClassName() {
117 return 'mw.widgets.NamespaceInputWidget';
118 }
119
120 public function getConfig( &$config ) {
121 $config['namespace'] = $this->namespace;
122 $config['associated'] = $this->associated;
123 $config['invert'] = $this->invert;
124 $config['allValue'] = $this->allValue;
125 return parent::getConfig( $config );
126 }
127 }