Don't require commandLine.inc when not using the command line; instead, move wfWaitFo...
[lhc/web/wiklou.git] / includes / Xml.php
index a35d21b..6689a4a 100644 (file)
@@ -41,14 +41,15 @@ class Xml {
         * @param $attribs Array of attributes for an XML element
         */
        private static function expandAttributes( $attribs ) {
+               $out = '';
                if( is_null( $attribs ) ) {
                        return null;
-               } else {
-                       $out = '';
-                       foreach( $attribs as $name => $val ) {
-                               $out .= ' ' . $name . '="' . Sanitizer::encodeAttribute( $val ) . '"';
-                       }
+               } elseif( is_array( $attribs ) ) {
+                       foreach( $attribs as $name => $val )
+                               $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
                        return $out;
+               } else {
+                       throw new MWException( 'Expected attribute array, got something else in ' . __METHOD__ );
                }
        }
 
@@ -96,25 +97,32 @@ class Xml {
         * @param mixed $selected Namespace which should be pre-selected
         * @param mixed $all Value of an item denoting all namespaces, or null to omit
         * @param bool $hidden Include hidden namespaces? [WTF? --RC]
-        * @param array $exclude Array of indexes to exclude
         * @return string
         */
-       public static function namespaceSelector( $selected = '', $all = null, $hidden = false, $exclude = array() ) {
+       public static function namespaceSelector( $selected = '', $all = null, $hidden = false, $element_name = 'namespace' ) {
                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 || in_array( $index, $exclude ) )
+                       if( $index < NS_MAIN )
                                continue;
                        if( $index === 0 )
                                $name = wfMsg( 'blanknamespace' );
                        $options[] = self::option( $name, $index, $index === $selected );
                }
                
-               return Xml::openElement( 'select', array( 'id' => 'namespace', 'name' => 'namespace',
+               return Xml::openElement( 'select', array( 'id' => 'namespace', 'name' => $element_name,
                        'class' => 'namespaceselector' ) )
                        . "\n"
                        . implode( "\n", $options )
@@ -301,7 +309,7 @@ class Xml {
                        'type' => 'hidden',
                        'value' => $value ) + $attribs );
        }
-       
+
        /**
         * Convenience function to build an HTML drop-down list item.
         * @param $text String: text for this item
@@ -321,6 +329,63 @@ class Xml {
                return self::element( 'option', $attribs, $text );
        }
 
+       /**
+        * Build a drop-down box from a textual list.
+        * 
+        * @param mixed $name Name and id for the drop-down
+        * @param mixed $class CSS classes for the drop-down
+        * @param mixed $other Text for the "Other reasons" option
+        * @param mixed $list Correctly formatted text to be used to generate the options
+        * @param mixed $selected Option which should be pre-selected
+        * @return string
+        */
+       public static function listDropDown( $name= '', $list = '', $other = '', $selected = '', $class = '', $tabindex = Null ) {
+               $options = '';
+               $optgroup = false;
+               
+               $options = self::option( $other, 'other', $selected === 'other' );
+               
+               foreach ( explode( "\n", $list ) as $option) {
+                               $value = trim( $option );
+                               if ( $value == '' ) {
+                                       continue;
+                               } elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) {
+                                       // A new group is starting ...
+                                       $value = trim( substr( $value, 1 ) );
+                                       if( $optgroup ) $options .= self::closeElement('optgroup');
+                                       $options .= self::openElement( 'optgroup', array( 'label' => $value ) );
+                                       $optgroup = true;
+                               } elseif ( substr( $value, 0, 2) == '**' ) {
+                                       // groupmember
+                                       $value = trim( substr( $value, 2 ) );
+                                       $options .= self::option( $value, $value, $selected === $value );
+                               } else {
+                                       // groupless reason list
+                                       if( $optgroup ) $options .= self::closeElement('optgroup');
+                                       $options .= self::option( $value, $value, $selected === $value );
+                                       $optgroup = false;
+                               }
+                       }
+                       if( $optgroup ) $options .= self::closeElement('optgroup');
+               
+               $attribs = array();
+               if( $name ) {
+                       $attribs['id'] = $name;
+                       $attribs['name'] = $name;
+               }
+               if( $class ) {
+                       $attribs['class'] = $class;
+               }
+               if( $tabindex ) {
+                       $attribs['tabindex'] = $tabindex;
+               }
+               return Xml::openElement( 'select', $attribs )
+                       . "\n"
+                       . $options
+                       . "\n"
+                       . Xml::closeElement( 'select' );
+       }
+
        /**
         * Returns an escaped string suitable for inclusion in a string literal
         * for JavaScript source code.