* @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__ );
}
}
}
/**
- * Create a namespace selector
+ * Build a drop-down box for selecting a namespace
*
- * @param $selected Mixed: the namespace which should be selected, default ''
- * @param $allnamespaces String: value of a special item denoting all namespaces. Null to not include (default)
- * @param $includehidden Bool: include hidden namespaces?
- * @return String: Html string containing the namespace selector
+ * @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]
+ * @return string
*/
- public static function namespaceSelector($selected = '', $allnamespaces = null, $includehidden=false) {
+ public static function namespaceSelector( $selected = '', $all = null, $hidden = false, $element_name = 'namespace' ) {
global $wgContLang;
- if( is_null( $selected ) )
- $selected = '';
- $s = "\n<select id='namespace' name='namespace' class='namespaceselector'>\n";
- $arr = $wgContLang->getFormattedNamespaces();
- if( !is_null($allnamespaces) ) {
- $arr = array($allnamespaces => wfMsg('namespacesall')) + $arr;
+ $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 );
}
- foreach ($arr as $index => $name) {
- if ($index < NS_MAIN) continue;
-
- $name = $index !== 0 ? $name : wfMsg('blanknamespace');
-
- if ($index === $selected) {
- $s .= "\t" . self::element("option",
- array("value" => $index, "selected" => "selected"),
- $name) . "\n";
- } else {
- $s .= "\t" . self::element("option", array("value" => $index), $name) . "\n";
- }
+
+ 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 );
}
- $s .= "</select>\n";
- return $s;
+
+ return Xml::openElement( 'select', array( 'id' => 'namespace', 'name' => $element_name,
+ 'class' => 'namespaceselector' ) )
+ . "\n"
+ . implode( "\n", $options )
+ . "\n"
+ . Xml::closeElement( 'select' );
}
-
- /**
- * Create a date selector
- *
- * @param $selected Mixed: the month which should be selected, default ''
- * @param $allmonths String: value of a special item denoting all month. Null to not include (default)
- * @return String: Html string containing the month selector
- */
- public static function monthSelector($selected = '', $allmonths = null) {
- if( is_null( $selected ) )
- $selected = '';
- $s = "\n<select id='month' name='month' class='monthselector'>\n";
- $arr = Language::$mMonthMsgs;
- if( !is_null($allmonths) ) {
- $arr = array($allmonths => 'monthsall') + $arr;
- }
- foreach ($arr as $index => $name) {
- $message = wfMsgHtml($name);
- $index++; // Let January be 1
-
- if ($index === $selected) {
- $s .= "\t" . self::element("option",
- array("value" => $index, "selected" => "selected"), $message) . "\n";
- } else {
- $s .= "\t" . self::element("option", array("value" => $index), $message) . "\n";
- }
- }
- $s .= "</select>\n";
- return $s;
+ /**
+ * Create a date selector
+ *
+ * @param $selected Mixed: the month which should be selected, default ''
+ * @param $allmonths String: value of a special item denoting all month. Null to not include (default)
+ * @param string $id Element identifier
+ * @return String: Html string containing the month selector
+ */
+ public static function monthSelector( $selected = '', $allmonths = null, $id = 'month' ) {
+ global $wgLang;
+ $options = array();
+ if( is_null( $selected ) )
+ $selected = '';
+ if( !is_null( $allmonths ) )
+ $options[] = self::option( wfMsg( 'monthsall' ), $allmonths, $selected === $allmonths );
+ for( $i = 1; $i < 13; $i++ )
+ $options[] = self::option( $wgLang->getMonthName( $i ), $i, $selected === $i );
+ return self::openElement( 'select', array( 'id' => $id, 'name' => 'month' ) )
+ . implode( "\n", $options )
+ . self::closeElement( 'select' );
}
/**
# To avoid any complaints about bad entity refs
"&" => "\\x26",
+
+ # Work around https://bugzilla.mozilla.org/show_bug.cgi?id=274152
+ # Encode certain Unicode formatting chars so affected
+ # versions of Gecko don't misinterpret our strings;
+ # this is a common problem with Farsi text.
+ "\xe2\x80\x8c" => "\\u200c", // ZERO WIDTH NON-JOINER
+ "\xe2\x80\x8d" => "\\u200d", // ZERO WIDTH JOINER
);
return strtr( $string, $pairs );
}
/**
* Encode a variable of unknown type to JavaScript.
- * Doesn't support hashtables just yet.
+ * Arrays are converted to JS arrays, objects are converted to JS associative
+ * arrays (objects). So cast your PHP associative arrays to objects before
+ * passing them to here.
*/
public static function encodeJsVar( $value ) {
if ( is_bool( $value ) ) {
$s = $value;
} elseif ( is_array( $value ) ) {
$s = '[';
- foreach ( $value as $name => $elt ) {
+ foreach ( $value as $elt ) {
if ( $s != '[' ) {
$s .= ', ';
}
$s .= self::encodeJsVar( $elt );
}
$s .= ']';
+ } elseif ( is_object( $value ) ) {
+ $s = '{';
+ foreach ( (array)$value as $name => $elt ) {
+ if ( $s != '{' ) {
+ $s .= ', ';
+ }
+ $s .= '"' . self::escapeJsString( $name ) . '": ' .
+ self::encodeJsVar( $elt );
+ }
+ $s .= '}';
} else {
$s = '"' . self::escapeJsString( $value ) . '"';
}
$in );
}
}
-?>
+