r52041 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r52040‎ | r52041 | r52042 >
Date:10:51, 17 June 2009
Author:catrope
Status:ok
Tags:
Comment:
switch-master: Add getVars() method and fix up r52034
Modified paths:
  • /trunk/tools/switch-master/ConfEditor.php (modified) (history)

Diff [purge]

Index: trunk/tools/switch-master/ConfEditor.php
@@ -245,6 +245,84 @@
246246 }
247247 return $out;
248248 }
 249+
 250+ /**
 251+ * Get the variables defined in the text
 252+ * @return array( varname => value )
 253+ */
 254+ function getVars() {
 255+ $vars = array();
 256+ $this->parse();
 257+ foreach( $this->pathInfo as $path => $data ) {
 258+ if ( $path[0] != '$' )
 259+ continue;
 260+ $trimmedPath = substr( $path, 1 );
 261+ $name = $data['name'];
 262+ if ( $name[0] == '@' )
 263+ continue;
 264+ if ( $name[0] == '$' )
 265+ $name = substr( $name, 1 );
 266+ $parentPath = substr( $trimmedPath, 0,
 267+ strlen( $trimmedPath ) - strlen( $name ) );
 268+ if( substr( $parentPath, -1 ) == '/' )
 269+ $parentPath = substr( $parentPath, 0, -1 );
 270+
 271+ $value = substr( $this->text, $data['valueStartByte'],
 272+ $data['valueEndByte'] - $data['valueStartByte']
 273+ );
 274+ $this->setVar( $vars, $parentPath, $name,
 275+ $this->parseScalar( $value ) );
 276+ }
 277+ return $vars;
 278+ }
 279+
 280+ /**
 281+ * Set a value in an array, unless it's set already. For instance,
 282+ * setVar( $arr, 'foo/bar', 'baz', 3 ); will set
 283+ * $arr['foo']['bar']['baz'] = 3;
 284+ * @param $array array
 285+ * @param $path string slash-delimited path
 286+ * @param $key mixed Key
 287+ * @param $value mixed Value
 288+ */
 289+ function setVar( &$array, $path, $key, $value ) {
 290+ echo "$path [$key] = $value\n";
 291+ $pathArr = explode( '/', $path );
 292+ $target =& $array;
 293+ if ( $path !== '' ) {
 294+ foreach ( $pathArr as $p ) {
 295+ if( !isset( $target[$p] ) )
 296+ $target[$p] = array();
 297+ $target =& $target[$p];
 298+ }
 299+ }
 300+ if ( !isset( $target[$key] ) )
 301+ $target[$key] = $value;
 302+ }
 303+
 304+ /**
 305+ * Parse a scalar value in PHP
 306+ * @return mixed Parsed value
 307+ */
 308+ function parseScalar( $str ) {
 309+ if ( @$str[0] == '\'' )
 310+ // Single-quoted string
 311+ return strtr( substr( $str, 1, -1 ),
 312+ array( '\\\'' => '\'', '\\\\' => '\\' ) );
 313+ if ( @$str[0] == '"' )
 314+ // Double-quoted string
 315+ return strtr( stripcslashes( substr( $str, 1, -1 ) ),
 316+ array( '\'' => '\\\'' ) );
 317+ if ( substr( $str, 0, 4 ) == 'true' )
 318+ return true;
 319+ if ( substr( $str, 0, 5 ) == 'false' )
 320+ return false;
 321+ if ( substr( $str, 0, 4 ) == 'null' )
 322+ return null;
 323+ // Must be some kind of numeric value, so let PHP's weak typing
 324+ // be useful for a change
 325+ return $str;
 326+ }
249327
250328 /**
251329 * Replace the byte offset region of the source with $newText.
@@ -560,7 +638,7 @@
561639 $this->error( "expected a string or number for the array key" );
562640 }
563641 if ( $token->type == T_CONSTANT_ENCAPSED_STRING ) {
564 - $text = stripslashes( substr( $token->text, 1, -1 ) );
 642+ $text = $thsi->parseScalar( $token->text );
565643 } else {
566644 $text = $token->text;
567645 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r52042Fix stupid typo in r52041catrope10:52, 17 June 2009

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r52034switch-master: Avoid use of eval() in ConfEditorcatrope09:39, 17 June 2009

Status & tagging log