[Html] Follow-up r109990: Add support for excluding and disabling options
authorKrinkle <krinkle@users.mediawiki.org>
Mon, 13 Feb 2012 15:08:26 +0000 (15:08 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Mon, 13 Feb 2012 15:08:26 +0000 (15:08 +0000)
includes/Html.php
tests/phpunit/includes/HtmlTest.php

index 4d8047e..26a40cd 100644 (file)
@@ -708,6 +708,8 @@ class Html {
         * - 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'
@@ -737,6 +739,13 @@ class Html {
                        $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();
 
@@ -751,7 +760,7 @@ class Html {
                // 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 ) {
@@ -759,7 +768,9 @@ class Html {
                                // 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 = '';
index 53ef678..566152e 100644 (file)
@@ -214,6 +214,8 @@ class HtmlTest extends MediaWikiTestCase {
        }
 
        function testNamespaceSelector() {
+               global $wgContLang;
+
                $this->assertEquals(
                        '<select id="namespace" name="namespace">' . "\n" .
 '<option value="0">(Main)</option>' . "\n" .
@@ -263,6 +265,35 @@ class HtmlTest extends MediaWikiTestCase {
                        ),
                        '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() {