* Polishing and documentation
[lhc/web/wiklou.git] / includes / Xml.php
index 509ed9a..1d38c34 100644 (file)
@@ -50,7 +50,9 @@ class Xml {
                        $attribs = array_map( array( 'UtfNormal', 'cleanUp' ), $attribs );
                }
                if( $contents ) {
+                       wfProfileIn( __METHOD__ . '-norm' );
                        $contents = UtfNormal::cleanUp( $contents );
+                       wfProfileOut( __METHOD__ . '-norm' );
                }
                return self::element( $element, $attribs, $contents );
        }
@@ -59,6 +61,14 @@ class Xml {
        public static function openElement( $element, $attribs = null ) { return self::element( $element, $attribs, null ); }
        public static function closeElement( $element ) { return "</$element>"; }
 
+       /**
+        * Same as <link>element</link>, but does not escape contents. Handy when the
+        * content you have is already valid xml.
+        */
+       public static function tags( $element, $attribs = null, $contents ) {
+               return self::element( $element, $attribs, null ) . $contents . "</$element>";
+       }
+
        /**
         * Create a namespace selector
         *
@@ -67,7 +77,7 @@ class Xml {
         * @param $includehidden Bool: include hidden namespaces?
         * @return String: Html string containing the namespace selector
         */
-       public static function &namespaceSelector($selected = '', $allnamespaces = null, $includehidden=false) {
+       public static function namespaceSelector($selected = '', $allnamespaces = null, $includehidden=false) {
                global $wgContLang;
                if( $selected !== '' ) {
                        if( is_null( $selected ) ) {
@@ -100,6 +110,45 @@ class Xml {
                return $s;
        }
 
+       /**
+        *
+        * @param $language The language code of the selected language
+        * @param $customisedOnly If true only languages which have some content are listed
+        * @return array of label and select
+        */
+       public static function languageSelector( $selected, $customisedOnly = true ) {
+               global $wgContLanguageCode;
+               /**
+                * Make sure the site language is in the list; a custom language code
+                * might not have a defined name...
+                */
+               $languages = Language::getLanguageNames( $customisedOnly );
+               if( !array_key_exists( $wgContLanguageCode, $languages ) ) {
+                       $languages[$wgContLanguageCode] = $wgContLanguageCode;
+               }
+               ksort( $languages );
+
+               /**
+                * If a bogus value is set, default to the content language.
+                * Otherwise, no default is selected and the user ends up
+                * with an Afrikaans interface since it's first in the list.
+                */
+               $selected = isset( $languages[$selected] ) ? $selected : $wgContLanguageCode;
+               $options = "\n";
+               foreach( $languages as $code => $name ) {
+                       $options .= Xml::option( "$code - $name", $code, ($code == $selected) ) . "\n";
+               }
+
+               return array(
+                       Xml::label( wfMsg('yourlanguage'), 'wpUserLanguage' ),
+                       Xml::tags( 'select',
+                               array( 'id' => 'wpUserLanguage', 'name' => 'wpUserLanguage' ),
+                               $options
+                       )
+               );
+
+       }
+
        public static function span( $text, $class, $attribs=array() ) {
                return self::element( 'span', array( 'class' => $class ) + $attribs, $text );
        }
@@ -128,10 +177,13 @@ class Xml {
         * @return string HTML
         */
        public static function check( $name, $checked=false, $attribs=array() ) {
-               return self::element( 'input', array(
-                       'name' => $name,
-                       'type' => 'checkbox',
-                       'value' => 1 ) + self::attrib( 'checked', $checked ) +  $attribs );
+               return self::element( 'input', array_merge(
+                       array(
+                               'name' => $name,
+                               'type' => 'checkbox',
+                               'value' => 1 ),
+                       self::attrib( 'checked', $checked ),
+                       $attribs ) );
        }
 
        /**
@@ -326,13 +378,13 @@ class Xml {
        }
 
        /**
-        * Escape html tags
-        * Basically replacing " > and < with HTML entities ( &quot;, &gt;, &lt;)
+        * Replace " > and < with their respective HTML entities ( &quot;,
+        * &gt;, &lt;)
         *
         * @param $in String: text that might contain HTML tags.
         * @return string Escaped string
         */
-       function escapeTagsOnly( $in ) {
+       public static function escapeTagsOnly( $in ) {
                return str_replace(
                        array( '"', '>', '<' ),
                        array( '&quot;', '&gt;', '&lt;' ),