[Xml/Html] new method Html::namespaceSelector
authorKrinkle <krinkle@users.mediawiki.org>
Wed, 25 Jan 2012 03:01:20 +0000 (03:01 +0000)
committerKrinkle <krinkle@users.mediawiki.org>
Wed, 25 Jan 2012 03:01:20 +0000 (03:01 +0000)
* Using params and option arrays instead of 4 random parameters like Xml::namespaceSelector did
* Right now it's passing $selectAttribs['name'] to Xml::label, this is done because that's what Xml::namespaceSelector did. However it's wrong since labels associate over ID not NAME. Will fix in the next commit, making sure unit tests stay functional first. This bug has been in Xml::namespaceSelector for a long time but usually unnoticed as people kept either defaults. Although it was easy to get wrong as the NAME was configurable but the ID was hardcoded in Xml::namespaceSelector.
* Deprecated Xml::namespaceSelector and made it cal Html::namespaceSelector

* Follows-up r109974, r109698
* XmlTest.php still runs successfully

RELEASE-NOTES-1.19
includes/Html.php
includes/Xml.php

index 77900ca..17f93f7 100644 (file)
@@ -315,6 +315,8 @@ changes to languages because of Bugzilla reports.
   using Title::isCssJsSubpage() or checking Title::isWrongCaseCssJsPage().
 * (bug 24430) Remove number of column for edit field in preference page.
 * Support for the deprecated hook MagicWordMagicWords was removed.
+* The Xml::namespaceSelector method has been deprecated, please use
+  Html::namespaceSelector instead (note that the parameters have changed also).
 
 == Compatibility ==
 
index fb3f167..eaa244f 100644 (file)
@@ -699,6 +699,60 @@ class Html {
                }
                return self::element( 'textarea', $attribs, $spacedValue );
        }
+       /**
+        * Build a drop-down box for selecting a namespace
+        *
+        * @param $params array:
+        * - selected: [optional] Id of namespace which should be pre-selected
+        * - all: [optional] Value of item for "all namespaces". If null or unset, <option> is omitted.
+        * - label: text for label to add before the field
+        * @param $selectAttribs array
+        * @return string
+        */
+       public static function namespaceSelector( Array $params = array(), Array $selectAttribs = array() ) {
+               global $wgContLang;
+
+               $selectAttribs = $selectAttribs + array(
+                       'id' => 'mw-namespaceselect',
+                       'name' => 'namespace',
+               );
+               ksort( $selectAttribs );
+
+               // If string only contains digits, convert to clean int. Selected could also
+               // be "all" or "" etc. which needs to be left untouched.
+               // PHP is_numeric() has issues with large strings, PHP ctype_digit has other issues
+               // and returns false for already clean ints. Use regex instead..
+               if ( preg_match( '/^\d+$/', $params['selected'] ) ) {
+                       $params['selected'] = intval( $params['selected'] );
+               }
+
+               $options = array();
+               if ( isset( $params['all'] ) ) {
+                       $options[$params['all']] = wfMsg( 'namespacesall' );
+               }
+               $options += $wgContLang->getFormattedNamespaces();
+
+               $optionsHtml = array();
+               foreach ( $options as $nsId => $nsName ) {
+                       if ( $nsId < NS_MAIN ) {
+                               continue;
+                       }
+                       if ( $nsId === 0 ) {
+                               $nsName = wfMsg( 'blanknamespace' );
+                       }
+                       $optionsHtml[] = Xml::option( $nsName, $nsId, $nsId === $params['selected'] );
+               }
+
+               $ret = Html::openElement( 'select', $selectAttribs )
+                       . "\n"
+                       . implode( "\n", $optionsHtml )
+                       . "\n"
+                       . Html::closeElement( 'select' );
+               if ( isset( $params['label'] ) ) {
+                       $ret = Xml::label( $params['label'], $selectAttribs['name'] ) . '&#160;' . $ret;
+               }
+               return $ret;
+       }
 
        /**
         * Constructs the opening html-tag with necessary doctypes depending on
index 863e872..f3ce546 100644 (file)
@@ -117,42 +117,19 @@ class Xml {
         * @param $element_name String: value of the "name" attribute of the select tag
         * @param $label String: optional label to add to the field
         * @return string
+        * @deprecated since 1.19
         */
        public static function namespaceSelector( $selected = '', $all = null, $element_name = 'namespace', $label = null ) {
-               global $wgContLang;
-               $namespaces = $wgContLang->getFormattedNamespaces();
-               $options = array();
-
-               // Godawful hack... we'll be frequently passed selected namespaces
-               // as strings since PHP is such a shithole.
-               // But we also don't want blanks and nulls and "all"s matching 0,
-               // so let's convert *just* string ints to clean ints.
-               if( preg_match( '/^\d+$/', $selected ) ) {
-                       $selected = intval( $selected );
-               }
-
-               if( !is_null( $all ) )
-                       $namespaces = array( $all => wfMsg( 'namespacesall' ) ) + $namespaces;
-               foreach( $namespaces as $index => $name ) {
-                       if( $index < NS_MAIN ) {
-                               continue;
-                       }
-                       if( $index === 0 ) {
-                               $name = wfMsg( 'blanknamespace' );
-                       }
-                       $options[] = self::option( $name, $index, $index === $selected );
-               }
-
-               $ret = Xml::openElement( 'select', array( 'class' => 'namespaceselector', 'id' => 'namespace',
-                       'name' => $element_name ) )
-                       . "\n"
-                       . implode( "\n", $options )
-                       . "\n"
-                       . Xml::closeElement( 'select' );
-               if ( !is_null( $label ) ) {
-                       $ret = Xml::label( $label, $element_name ) . '&#160;' . $ret;
-               }
-               return $ret;
+               wfDeprecated( __METHOD__, '1.19' );
+               return Html::namespaceSelector( array(
+                       'selected' => $selected,
+                       'all' => $all,
+                       'label' => $label,
+               ), array(
+                       'name' => $element_name,
+                       'id' => 'namespace',
+                       'class' => 'namespaceselector',
+               ) );
        }
 
        /**