* - selected: [optional] Id of namespace which should be pre-selected
* - all: [optional] Value of item for "all namespaces". If null or unset, no <option> is generated to select all namespaces
* - label: text for label to add before the field
+ * - exclude: [optional] Array of namespace ids to exclude
+ * - disable: [optional] Array of namespace ids for which the option should be disabled in the selector
* @param $selectAttribs array HTML attributes for the generated select element.
* - id: [optional], default: 'namespace'
* - name: [optional], default: 'namespace'
$params['selected'] = '';
}
+ if ( !isset( $params['exclude'] ) || !is_array( $params['exclude'] ) ) {
+ $params['exclude'] = array();
+ }
+ if ( !isset( $params['disable'] ) || !is_array( $params['disable'] ) ) {
+ $params['disable'] = array();
+ }
+
// Associative array between option-values and option-labels
$options = array();
// Convert $options to HTML and filter out namespaces below 0
$optionsHtml = array();
foreach ( $options as $nsId => $nsName ) {
- if ( $nsId < NS_MAIN ) {
+ if ( $nsId < NS_MAIN || in_array( $nsId, $params['exclude'] ) ) {
continue;
}
if ( $nsId === 0 ) {
// main we don't use "" but the user message descripting it (e.g. "(Main)" or "(Article)")
$nsName = wfMsg( 'blanknamespace' );
}
- $optionsHtml[] = Xml::option( $nsName, $nsId, $nsId === $params['selected'] );
+ $optionsHtml[] = Xml::option( $nsName, $nsId, $nsId === $params['selected'], array(
+ 'disabled' => in_array( $nsId, $params['disable'] ),
+ ) );
}
$ret = '';
}
function testNamespaceSelector() {
+ global $wgContLang;
+
$this->assertEquals(
'<select id="namespace" name="namespace">' . "\n" .
'<option value="0">(Main)</option>' . "\n" .
),
'Basic namespace selector with custom values'
);
+ $immovable = array();
+ $namespaces = $wgContLang->getNamespaces();
+ foreach ( $namespaces as $nsId => $nsName ) {
+ if ( !MWNamespace::isMovable( intval( $nsId ) ) ) {
+ $immovable[] = $nsId;
+ }
+ }
+ $this->assertEquals(
+ '<select id="namespace" name="namespace">' . "\n" .
+'<option value="0">(Main)</option>' . "\n" .
+'<option value="1">Talk</option>' . "\n" .
+'<option value="2">User</option>' . "\n" .
+'<option value="3">User talk</option>' . "\n" .
+'<option value="4">MyWiki</option>' . "\n" .
+'<option value="5">MyWiki Talk</option>' . "\n" .
+'<option value="6">File</option>' . "\n" .
+'<option value="7">File talk</option>' . "\n" .
+'<option value="8">MediaWiki</option>' . "\n" .
+'<option value="9">MediaWiki talk</option>' . "\n" .
+'<option value="10">Template</option>' . "\n" .
+'<option value="11">Template talk</option>' . "\n" .
+'<option disabled="" value="14">Category</option>' . "\n" .
+'<option value="15">Category talk</option>' . "\n" .
+'</select>',
+ Html::namespaceSelector(
+ array( 'exclude' => array( 100, 101 ), 'disable' => $immovable )
+ ),
+ 'Namespace selector without the custom namespace and immovable namespaces disabled.'
+ );
}
function testNamespaceSelectorIdAndNameDefaultsAttributes() {