r109990 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r109989‎ | r109990 | r109991 >
Date:03:01, 25 January 2012
Author:krinkle
Status:ok (Comments)
Tags:core 
Comment:
[Xml/Html] new method Html::namespaceSelector
* 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
Modified paths:
  • /trunk/phase3/RELEASE-NOTES-1.19 (modified) (history)
  • /trunk/phase3/includes/Html.php (modified) (history)
  • /trunk/phase3/includes/Xml.php (modified) (history)

Diff [purge]

Index: trunk/phase3/RELEASE-NOTES-1.19
@@ -315,6 +315,8 @@
316316 using Title::isCssJsSubpage() or checking Title::isWrongCaseCssJsPage().
317317 * (bug 24430) Remove number of column for edit field in preference page.
318318 * Support for the deprecated hook MagicWordMagicWords was removed.
 319+* The Xml::namespaceSelector method has been deprecated, please use
 320+ Html::namespaceSelector instead (note that the parameters have changed also).
319321
320322 == Compatibility ==
321323
Index: trunk/phase3/includes/Xml.php
@@ -117,42 +117,19 @@
118118 * @param $element_name String: value of the "name" attribute of the select tag
119119 * @param $label String: optional label to add to the field
120120 * @return string
 121+ * @deprecated since 1.19
121122 */
122123 public static function namespaceSelector( $selected = '', $all = null, $element_name = 'namespace', $label = null ) {
123 - global $wgContLang;
124 - $namespaces = $wgContLang->getFormattedNamespaces();
125 - $options = array();
126 -
127 - // Godawful hack... we'll be frequently passed selected namespaces
128 - // as strings since PHP is such a shithole.
129 - // But we also don't want blanks and nulls and "all"s matching 0,
130 - // so let's convert *just* string ints to clean ints.
131 - if( preg_match( '/^\d+$/', $selected ) ) {
132 - $selected = intval( $selected );
133 - }
134 -
135 - if( !is_null( $all ) )
136 - $namespaces = array( $all => wfMsg( 'namespacesall' ) ) + $namespaces;
137 - foreach( $namespaces as $index => $name ) {
138 - if( $index < NS_MAIN ) {
139 - continue;
140 - }
141 - if( $index === 0 ) {
142 - $name = wfMsg( 'blanknamespace' );
143 - }
144 - $options[] = self::option( $name, $index, $index === $selected );
145 - }
146 -
147 - $ret = Xml::openElement( 'select', array( 'class' => 'namespaceselector', 'id' => 'namespace',
148 - 'name' => $element_name ) )
149 - . "\n"
150 - . implode( "\n", $options )
151 - . "\n"
152 - . Xml::closeElement( 'select' );
153 - if ( !is_null( $label ) ) {
154 - $ret = Xml::label( $label, $element_name ) . '&#160;' . $ret;
155 - }
156 - return $ret;
 124+ wfDeprecated( __METHOD__, '1.19' );
 125+ return Html::namespaceSelector( array(
 126+ 'selected' => $selected,
 127+ 'all' => $all,
 128+ 'label' => $label,
 129+ ), array(
 130+ 'name' => $element_name,
 131+ 'id' => 'namespace',
 132+ 'class' => 'namespaceselector',
 133+ ) );
157134 }
158135
159136 /**
Index: trunk/phase3/includes/Html.php
@@ -699,7 +699,61 @@
700700 }
701701 return self::element( 'textarea', $attribs, $spacedValue );
702702 }
 703+ /**
 704+ * Build a drop-down box for selecting a namespace
 705+ *
 706+ * @param $params array:
 707+ * - selected: [optional] Id of namespace which should be pre-selected
 708+ * - all: [optional] Value of item for "all namespaces". If null or unset, <option> is omitted.
 709+ * - label: text for label to add before the field
 710+ * @param $selectAttribs array
 711+ * @return string
 712+ */
 713+ public static function namespaceSelector( Array $params = array(), Array $selectAttribs = array() ) {
 714+ global $wgContLang;
703715
 716+ $selectAttribs = $selectAttribs + array(
 717+ 'id' => 'mw-namespaceselect',
 718+ 'name' => 'namespace',
 719+ );
 720+ ksort( $selectAttribs );
 721+
 722+ // If string only contains digits, convert to clean int. Selected could also
 723+ // be "all" or "" etc. which needs to be left untouched.
 724+ // PHP is_numeric() has issues with large strings, PHP ctype_digit has other issues
 725+ // and returns false for already clean ints. Use regex instead..
 726+ if ( preg_match( '/^\d+$/', $params['selected'] ) ) {
 727+ $params['selected'] = intval( $params['selected'] );
 728+ }
 729+
 730+ $options = array();
 731+ if ( isset( $params['all'] ) ) {
 732+ $options[$params['all']] = wfMsg( 'namespacesall' );
 733+ }
 734+ $options += $wgContLang->getFormattedNamespaces();
 735+
 736+ $optionsHtml = array();
 737+ foreach ( $options as $nsId => $nsName ) {
 738+ if ( $nsId < NS_MAIN ) {
 739+ continue;
 740+ }
 741+ if ( $nsId === 0 ) {
 742+ $nsName = wfMsg( 'blanknamespace' );
 743+ }
 744+ $optionsHtml[] = Xml::option( $nsName, $nsId, $nsId === $params['selected'] );
 745+ }
 746+
 747+ $ret = Html::openElement( 'select', $selectAttribs )
 748+ . "\n"
 749+ . implode( "\n", $optionsHtml )
 750+ . "\n"
 751+ . Html::closeElement( 'select' );
 752+ if ( isset( $params['label'] ) ) {
 753+ $ret = Xml::label( $params['label'], $selectAttribs['name'] ) . '&#160;' . $ret;
 754+ }
 755+ return $ret;
 756+ }
 757+
704758 /**
705759 * Constructs the opening html-tag with necessary doctypes depending on
706760 * global variables.

Follow-up revisions

RevisionCommit summaryAuthorDate
r109993[Html] Unit test + bugfix Html::namespaceSelector...krinkle03:25, 25 January 2012
r110209[Special:MovePage] Split new title input, fix bug 29454 (byteLimit), namespac...krinkle16:26, 28 January 2012
r110252[specials] Xml::namespaceSelector > Html::namespaceSelector...krinkle19:05, 29 January 2012
r110274test Html::namespaceSelector() id & name attributes generation...hashar10:39, 30 January 2012
r110275comment / style for Html::namespaceSelector()...hashar11:02, 30 January 2012
r111375[Html] Follow-up r109990: Add category to example namespaces...krinkle14:53, 13 February 2012
r111376[Html] Follow-up r109990: Add support for excluding and disabling optionskrinkle15:08, 13 February 2012
r113272[Html::namespaceSelector] Remove default id/name attributes...krinkle19:14, 7 March 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r109698[Unit testing] Add unit tests for Xml::namespaceSelector...krinkle22:26, 21 January 2012
r109974[Unit testing] Re-order attribs to a-z to make testing more reliable...krinkle00:52, 25 January 2012

Comments

#Comment by Umherirrender (talk | contribs)   14:09, 11 February 2012

Please add the warning for deprecated method only, when the method is not used in core. Thanks.

Status & tagging log