r47247 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r47246‎ | r47247 | r47248 >
Date:01:52, 14 February 2009
Author:tparscal
Status:deferred (Comments)
Tags:
Comment:
Initial import.
Modified paths:
  • /trunk/extensions/ClientSide (modified) (history)
  • /trunk/extensions/ClientSide/ClientSide.php (added) (history)

Diff [purge]

Index: trunk/extensions/ClientSide/ClientSide.php
@@ -0,0 +1,435 @@
 2+<?php
 3+
 4+abstract class Html {
 5+
 6+ /* Static Functions */
 7+
 8+ /**
 9+ * Builds an HTML string for a complete table tag
 10+ * @param attributes Optional Array of HTML attributes
 11+ * @param content... Any number of Strings of HTML content
 12+ */
 13+ public static function table() {
 14+ $arguments = func_get_args();
 15+ $attributes = array(
 16+ 'cellpadding' => 0, 'cellspacing' => 0, 'border' => 0,
 17+ );
 18+ if ( count( $arguments ) > 0 && is_array( $arguments[0] ) ) {
 19+ $attributes = array_merge( $attributes, $arguments[0] );
 20+ array_shift( $arguments );
 21+ }
 22+ if ( count( $arguments ) > 0 ) {
 23+ return self::tag( 'table', $attributes, implode( $arguments ) );
 24+ }
 25+ return null;
 26+ }
 27+
 28+ /**
 29+ * Builds an HTML string for a complete table row tag
 30+ * @param attributes Optional Array of HTML attributes
 31+ * @param content... Any number of Strings of HTML content
 32+ */
 33+
 34+ public static function row() {
 35+ $arguments = func_get_args();
 36+ $attributes = array();
 37+ if ( count( $arguments ) > 0 && is_array( $arguments[0] ) ) {
 38+ $attributes = array_merge( $attributes, $arguments[0] );
 39+ array_shift( $arguments );
 40+ }
 41+ if ( count( $arguments ) > 0 ) {
 42+ return self::tag( 'tr', $attributes, implode( $arguments ) );
 43+ }
 44+ return null;
 45+ }
 46+
 47+ /**
 48+ * Builds an HTML string for a complete table cell tag
 49+ * @param attributes Optional Array of HTML attributes
 50+ * @param content String of HTML content
 51+ */
 52+ public static function cell() {
 53+ $arguments = func_get_args();
 54+ $attributes = array();
 55+ if ( count( $arguments ) > 0 && is_array( $arguments[0] ) ) {
 56+ $attributes = array_merge( $attributes, $arguments[0] );
 57+ array_shift( $arguments );
 58+ }
 59+ if ( count( $arguments ) > 0 ) {
 60+ return self::tag( 'td', $attributes, $arguments[0] );
 61+ } else {
 62+ return self::tag( 'td', $attributes );
 63+ }
 64+ }
 65+
 66+ /**
 67+ * Builds an HTML string for a complete div
 68+ * @param attributes Array of HTML attributes
 69+ * @param content
 70+ */
 71+ public static function div() {
 72+ $arguments = func_get_args();
 73+ $attributes = array();
 74+ if ( count( $arguments ) > 0 && is_array( $arguments[0] ) ) {
 75+ $attributes = array_merge( $attributes, $arguments[0] );
 76+ array_shift( $arguments );
 77+ }
 78+ if ( count( $arguments ) > 0 ) {
 79+ return self::tag( 'div', $attributes, $arguments[0] );
 80+ } else {
 81+ return self::tag( 'div', $attributes );
 82+ }
 83+ }
 84+
 85+ /**
 86+ * Builds an HTML string for a complete span
 87+ * @param attributes Array of HTML attributes
 88+ * @param content
 89+ */
 90+ public static function span() {
 91+ $arguments = func_get_args();
 92+ $attributes = array();
 93+ if ( count( $arguments ) > 0 && is_array( $arguments[0] ) ) {
 94+ $attributes = array_merge( $attributes, $arguments[0] );
 95+ array_shift( $arguments );
 96+ }
 97+ if ( count( $arguments ) > 0 ) {
 98+ return self::tag( 'span', $attributes, $arguments[0] );
 99+ } else {
 100+ return self::tag( 'span', $attributes );
 101+ }
 102+ }
 103+
 104+ /**
 105+ * Builds an HTML string for an input
 106+ * @param attributes Array of HTML attributes
 107+ */
 108+ public static function input(
 109+ array $attributes = array()
 110+ ) {
 111+ return self::tag( 'input', $attributes );
 112+ }
 113+
 114+ /**
 115+ * Builds an HTML string for a stand-alone block of javascript
 116+ * @param script String of raw text to use as script contents or
 117+ * Array of attributes such as src
 118+ */
 119+ public static function script(
 120+ $script
 121+ ) {
 122+ global $wgJsMimeType;
 123+ if ( is_array( $script ) ) {
 124+ return Xml::element(
 125+ 'script',
 126+ array_merge(
 127+ $script,
 128+ array(
 129+ 'type' => $wgJsMimeType, 'language' => 'javascript'
 130+ )
 131+ ),
 132+ '//'
 133+ );
 134+ } else {
 135+ return Xml::tags(
 136+ 'script',
 137+ array( 'type' => $wgJsMimeType, 'language' => 'javascript' ),
 138+ $script
 139+ );
 140+ }
 141+ }
 142+
 143+ /**
 144+ * Builds an HTML string for a complete tag
 145+ * @param tag Name of tag
 146+ * @param attributes Array of HTML attributes
 147+ * @param contents Array of Strings or String of HTML or null to
 148+ * make tag self-closing
 149+ */
 150+ public static function tag(
 151+ $tag,
 152+ array $attributes = array(),
 153+ $contents = null
 154+ ) {
 155+ if ( is_array( $contents ) && count( $contents ) > 1 ) {
 156+ return Xml::tags( $tag, $attributes, implode( $contents ) );
 157+ } else {
 158+ return Xml::tags(
 159+ $tag, $attributes, $contents, ( $contents !== null )
 160+ );
 161+ }
 162+ }
 163+
 164+ /**
 165+ * Builds an HTML string for a tag opening
 166+ * @param tag Name of tag
 167+ * @param attributes Array of HTML attributes
 168+ */
 169+ public static function open(
 170+ $tag,
 171+ array $attributes = array()
 172+ ) {
 173+ return Xml::openElement( $tag, $attributes );
 174+ }
 175+
 176+ /**
 177+ * Builds an HTML string for a tag closing
 178+ * @param tag Name of tag
 179+ */
 180+ public static function close(
 181+ $tag
 182+ ) {
 183+ return Xml::closeElement( $tag );
 184+ }
 185+
 186+ /**
 187+ * Sanitizes a string to be used as an HTML ID
 188+ * @param id String to sanitize
 189+ */
 190+ public static function toId(
 191+ $id
 192+ ) {
 193+ return preg_replace(
 194+ '`\ +`', ' ', preg_replace( '`[^a-z0-9\_]`i', '', $id )
 195+ );
 196+ }
 197+}
 198+
 199+abstract class Css {
 200+
 201+ /* Static Functions */
 202+
 203+ public static function toAttributes(
 204+ array $attributes
 205+ ) {
 206+ $cssOutput = '';
 207+ foreach( $attributes as $name => $value ) {
 208+ if ( !is_int( $name ) ) {
 209+ $cssOutput .= $name . ':' . $value . ';';
 210+ }
 211+ }
 212+ return $cssOutput;
 213+ }
 214+}
 215+
 216+abstract class Js {
 217+
 218+ /* Static Functions */
 219+
 220+ public static function chain(
 221+ $functions,
 222+ $end = true
 223+ ) {
 224+ $jsFunctions = array();
 225+ foreach( $functions as $name => $arguments ) {
 226+ if ( is_int( $name ) ) {
 227+ $jsFunctions[] = sprintf( '%s()', $arguments );
 228+ } else if ( is_array( $arguments ) ) {
 229+ $jsFunctions[] = sprintf(
 230+ '%s(%s)', $name, implode( ',', $arguments )
 231+ );
 232+ } else {
 233+ $jsFunctions[] = sprintf( '%s(%s)', $name, $arguments );
 234+ }
 235+ }
 236+ return implode( '.', $jsFunctions ) . ( $end ? ';' : '' );
 237+ }
 238+
 239+ /**
 240+ * Escapes a javascript string to make it safe to use anywhere
 241+ * @param string String to escape
 242+ */
 243+ public static function escape(
 244+ $string
 245+ ) {
 246+ return Xml::escapeJSString( $string );
 247+ }
 248+
 249+ /**
 250+ * Converts a PHP value to a javascript object
 251+ * @param value Associative Array to convert
 252+ */
 253+ public static function toObject(
 254+ $values
 255+ ) {
 256+ // Arrays
 257+ if ( is_array( $values ) ) {
 258+ $jsValues = array();
 259+ foreach( $values as $key => $value ) {
 260+ if ( is_array( $value ) ) {
 261+ $jsValues[] = $key . ':' . self::toObject( $value );
 262+ } else {
 263+ $jsValues[] = $key . ':' . self::toScalar( $value );
 264+ }
 265+ }
 266+ return '{' . implode( ',', $jsValues ) . '}';
 267+ }
 268+ return 'null';
 269+ }
 270+
 271+ /**
 272+ * Converts a PHP value to a javascript array
 273+ * @param value Array or Scalar to convert
 274+ */
 275+ public static function toArray(
 276+ $values
 277+ ) {
 278+ // Arrays
 279+ if ( is_array( $values ) ) {
 280+ $jsValues = array();
 281+ foreach( $values as $value ) {
 282+ $jsValues[] = self::toScalar( $value );
 283+ }
 284+ return '[' . implode( ',', $jsValues ) . ']';
 285+ }
 286+ // Scalars
 287+ if ( is_scalar( $values ) ) {
 288+ return '[' . self::toScalar( $values ) . ']';
 289+ }
 290+ return 'null';
 291+ }
 292+
 293+ /**
 294+ * Converts a PHP value to a javascript value
 295+ * @param value Array or Scalar to convert, if value is string
 296+ * it will be escaped and surrounded by quotes
 297+ * unless it is already surrounded by ' quotes,
 298+ * in which case the ' quotes will be removed and
 299+ * the value will be used as a statement
 300+ */
 301+ public static function toScalar(
 302+ $value
 303+ ) {
 304+ // Arrays
 305+ if ( is_array( $value ) ) {
 306+ return "'" . self::escape( implode( $value ) ) . "'";
 307+ }
 308+ // Scalars
 309+ if ( is_scalar( $value ) ) {
 310+ // Numbers
 311+ if ( is_numeric( $value ) ) {
 312+ return $value;
 313+ }
 314+ // Strings
 315+ if ( is_string( $value ) ) {
 316+ // Checks if string is surrouded with ' quotes
 317+ if (
 318+ substr( $value, 0, 1 ) == "'" &&
 319+ substr( $value, -1, 1 ) == "'"
 320+ ) {
 321+ // Formats as statement
 322+ return trim( $value, "'" );
 323+ } else {
 324+ // Formats as string
 325+ return "'" . self::escape( $value ) . "'";
 326+ }
 327+ }
 328+ // Booleans
 329+ if ( is_bool( $value ) ) {
 330+ return $value ? 'true' : 'false';
 331+ }
 332+ // Nulls
 333+ if ( is_null( $value ) ) {
 334+ return 'null';
 335+ }
 336+ }
 337+ return 'null';
 338+ }
 339+
 340+ /**
 341+ * Creates code to call a javascript function
 342+ * @param function String of name of function
 343+ * @param arguments Array of argument values to pass
 344+ */
 345+ public static function callFunction(
 346+ $function,
 347+ $arguments = array(),
 348+ $end = true
 349+ ) {
 350+ if ( !is_array( $arguments ) ) {
 351+ $arguments = array( $arguments );
 352+ }
 353+ return sprintf(
 354+ '%s(%s)', $function, implode( ',', $arguments )
 355+ ) . ( $end ? ';' : '' );
 356+ }
 357+
 358+ /**
 359+ * Builds an annonomous javascript function declaration
 360+ * @param arguments Array of argument names to accept
 361+ * @param body String of code for body
 362+ */
 363+ public static function buildFunction(
 364+ $arguments,
 365+ $body
 366+ ) {
 367+ if ( is_array( $arguments ) ) {
 368+ return sprintf(
 369+ 'function(%s){%s}', implode( ',', $arguments ), $body
 370+ );
 371+ } else if ( $arguments !== null ) {
 372+ return sprintf( 'function(%s){%s}', $arguments, $body );
 373+ } else {
 374+ return sprintf( 'function(){%s}', $body );
 375+ }
 376+ }
 377+
 378+ /**
 379+ * Builds an annonomous javascript function declaration
 380+ * @param arguments Array of argument names to accept
 381+ * @param body String of code for body
 382+ */
 383+ public static function buildInstance(
 384+ $prototype,
 385+ $arguments = array()
 386+ ) {
 387+ if ( !is_array( $arguments ) ) {
 388+ $arguments = array( $arguments );
 389+ }
 390+ return sprintf(
 391+ 'new %s(%s)', $prototype, implode( ',', $arguments )
 392+ );
 393+ }
 394+
 395+ public static function declareVar(
 396+ $name,
 397+ $value = 'null',
 398+ $end = true
 399+ ) {
 400+ return sprintf( 'var %s=%s', $name, $value ) . ( $end ? ';' : '' );
 401+ }
 402+
 403+ public static function declareVars(
 404+ array $vars,
 405+ $end = true
 406+ ) {
 407+ $jsOutput = '';
 408+ foreach( $vars as $name => $value ) {
 409+ if ( is_int( $name ) ) {
 410+ $name = $value;
 411+ $value = 'null';
 412+ }
 413+ $jsOutput .= sprintf(
 414+ 'var %s=%s', $name, $value
 415+ ) . ( $end ? ';' : '' );
 416+ }
 417+ return $jsOutput;
 418+ }
 419+
 420+ public static function declareInstance(
 421+ $name,
 422+ $prototype,
 423+ $arguments = array(),
 424+ $end = true
 425+ ) {
 426+ if ( !is_array( $arguments ) ) {
 427+ $arguments = array( $arguments );
 428+ }
 429+ return sprintf(
 430+ '%s=new %s(%s)',
 431+ $name,
 432+ $prototype,
 433+ implode( ',', $arguments )
 434+ ) . ( $end ? ';' : '' );
 435+ }
 436+}
Property changes on: trunk/extensions/ClientSide
___________________________________________________________________
Name: svn:ignore
1437 + .project

Comments

#Comment by Siebrand (talk | contribs)   12:19, 20 February 2009

'deferred' here is a little uninterested IMO. A single file was checked in with 400 lines of code, little to no context, and apparently it is a dependency for CommunityVoice which is active on usability.wikimedia.org. Bad stuff. I do hope WMF developers will try and lead by better example. I added minimal extension credits in r47542.

Status & tagging log