X-Git-Url: http://git.cyclocoop.org/?a=blobdiff_plain;f=includes%2FConfEditor.php;h=42a7173d5ebf3c516b330c7701a2383814feacde;hb=f50eab171e0db645483a0c36e0e00a0441fe8561;hp=9c8ebb6c5f08e208a79e956644791540057f2128;hpb=52eee0cfa9e86fedf7d414ae2a538e5d0c889dce;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/ConfEditor.php b/includes/ConfEditor.php index 9c8ebb6c5f..42a7173d5e 100644 --- a/includes/ConfEditor.php +++ b/includes/ConfEditor.php @@ -1,6 +1,6 @@ 'delete', 'path' => '$foo/bar/baz' ) * is equivalent to the runtime PHP code: * unset( $foo['bar']['baz'] ); * * set - * Sets the value of an array element. If the element doesn't exist, it - * is appended to the array. If it does exist, the value is set, with + * Sets the value of an array element. If the element doesn't exist, it + * is appended to the array. If it does exist, the value is set, with * comments and indenting preserved. * * append * Appends a new element to the end of the array. Adds a trailing comma. * e.g. - * array( 'type' => 'append', 'path', '$foo/bar', + * array( 'type' => 'append', 'path', '$foo/bar', * 'key' => 'baz', 'value' => "'x'" ) * is like the PHP code: * $foo['bar']['baz'] = 'x'; @@ -176,7 +180,7 @@ class ConfEditor { // Has it got a comma already? if ( strpos( $lastEltPath, '@extra' ) === false && !$lastEltInfo['hasComma'] ) { // No comma, insert one after the value region - list( $start, $end ) = $this->findValueRegion( $lastEltPath ); + list( , $end ) = $this->findValueRegion( $lastEltPath ); $this->replaceSourceRegion( $end - 1, $end - 1, ',' ); } @@ -184,10 +188,10 @@ class ConfEditor { list( $start, $end ) = $this->findDeletionRegion( $lastEltPath ); if ( $key === null ) { - list( $indent, $arrowIndent ) = $this->getIndent( $start ); + list( $indent, ) = $this->getIndent( $start ); $textToInsert = "$indent$value,"; } else { - list( $indent, $arrowIndent ) = + list( $indent, $arrowIndent ) = $this->getIndent( $start, $key, $lastEltInfo['arrowByte'] ); $textToInsert = "$indent$key$arrowIndent=> $value,"; } @@ -202,15 +206,15 @@ class ConfEditor { if ( $firstEltPath === false ) { throw new MWException( "Can't find array element of \"$path\"" ); } - list( $start, $end ) = $this->findDeletionRegion( $firstEltPath ); + list( $start, ) = $this->findDeletionRegion( $firstEltPath ); $info = $this->pathInfo[$firstEltPath]; // Make the text to insert if ( $key === null ) { - list( $indent, $arrowIndent ) = $this->getIndent( $start ); + list( $indent, ) = $this->getIndent( $start ); $textToInsert = "$indent$value,"; } else { - list( $indent, $arrowIndent ) = + list( $indent, $arrowIndent ) = $this->getIndent( $start, $key, $info['arrowByte'] ); $textToInsert = "$indent$key$arrowIndent=> $value,"; } @@ -239,13 +243,13 @@ class ConfEditor { try { $this->parse(); } catch ( ConfEditorParseError $e ) { - throw new MWException( + throw new MWException( "Sorry, ConfEditor broke the file during editing and it won't parse anymore: " . $e->getMessage() ); } return $out; } - + /** * Get the variables defined in the text * @return array( varname => value ) @@ -266,7 +270,7 @@ class ConfEditor { strlen( $trimmedPath ) - strlen( $name ) ); if( substr( $parentPath, -1 ) == '/' ) $parentPath = substr( $parentPath, 0, -1 ); - + $value = substr( $this->text, $data['valueStartByte'], $data['valueEndByte'] - $data['valueStartByte'] ); @@ -275,7 +279,7 @@ class ConfEditor { } return $vars; } - + /** * Set a value in an array, unless it's set already. For instance, * setVar( $arr, 'foo/bar', 'baz', 3 ); will set @@ -298,7 +302,7 @@ class ConfEditor { if ( !isset( $target[$key] ) ) $target[$key] = $value; } - + /** * Parse a scalar value in PHP * @return mixed Parsed value @@ -306,11 +310,17 @@ class ConfEditor { function parseScalar( $str ) { if ( $str !== '' && $str[0] == '\'' ) // Single-quoted string - return strtr( substr( $str, 1, -1 ), + // @todo FIXME: trim() call is due to mystery bug where whitespace gets + // appended to the token; without it we ended up reading in the + // extra quote on the end! + return strtr( substr( trim( $str ), 1, -1 ), array( '\\\'' => '\'', '\\\\' => '\\' ) ); - if ( $str !== '' && @$str[0] == '"' ) + if ( $str !== '' && $str[0] == '"' ) // Double-quoted string - return stripcslashes( substr( $str, 1, -1 ) ); + // @todo FIXME: trim() call is due to mystery bug where whitespace gets + // appended to the token; without it we ended up reading in the + // extra quote on the end! + return stripcslashes( substr( trim( $str ), 1, -1 ) ); if ( substr( $str, 0, 4 ) == 'true' ) return true; if ( substr( $str, 0, 5 ) == 'false' ) @@ -330,7 +340,7 @@ class ConfEditor { // Split all copy operations with a source corresponding to the region // in question. $newEdits = array(); - foreach ( $this->edits as $i => $edit ) { + foreach ( $this->edits as $edit ) { if ( $edit[0] !== 'copy' ) { $newEdits[] = $edit; continue; @@ -358,8 +368,8 @@ class ConfEditor { } /** - * Finds the source byte region which you would want to delete, if $pathName - * was to be deleted. Includes the leading spaces and tabs, the trailing line + * Finds the source byte region which you would want to delete, if $pathName + * was to be deleted. Includes the leading spaces and tabs, the trailing line * break, and any comments in between. */ function findDeletionRegion( $pathName ) { @@ -413,15 +423,15 @@ class ConfEditor { } /** - * Find the byte region in the source corresponding to the value part. - * This includes the quotes, but does not include the trailing comma - * or semicolon. + * Find the byte region in the source corresponding to the value part. + * This includes the quotes, but does not include the trailing comma + * or semicolon. * * The end position is the past-the-end (end + 1) value as per convention. */ function findValueRegion( $pathName ) { if ( !isset( $this->pathInfo[$pathName] ) ) { - throw new MWEXception( "Can't find path \"$pathName\"" ); + throw new MWException( "Can't find path \"$pathName\"" ); } $path = $this->pathInfo[$pathName]; if ( $path['valueStartByte'] === false || $path['valueEndByte'] === false ) { @@ -432,7 +442,7 @@ class ConfEditor { /** * Find the path name of the last element in the array. - * If the array is empty, this will return the @extra interstitial element. + * If the array is empty, this will return the \@extra interstitial element. * If the specified path is not found or is not an array, it will return false. */ function findLastArrayElement( $path ) { @@ -466,9 +476,9 @@ class ConfEditor { return $extraPath; } - /* + /** * Find the path name of first element in the array. - * If the array is empty, this will return the @extra interstitial element. + * If the array is empty, this will return the \@extra interstitial element. * If the specified path is not found or is not an array, it will return false. */ function findFirstArrayElement( $path ) { @@ -504,7 +514,6 @@ class ConfEditor { $indent = false; } if ( $indent !== false && $arrowPos !== false ) { - $textToInsert = "$indent$key "; $arrowIndentLength = $arrowPos - $pos - $indentLength - strlen( $key ); if ( $arrowIndentLength > 0 ) { $arrowIndent = str_repeat( ' ', $arrowIndentLength ); @@ -514,7 +523,7 @@ class ConfEditor { } /** - * Run the parser on the text. Throws an exception if the string does not + * Run the parser on the text. Throws an exception if the string does not * match our defined subset of PHP syntax. */ public function parse() { @@ -531,7 +540,7 @@ class ConfEditor { switch ( $state ) { case 'file': - $token = $this->expect( T_OPEN_TAG ); + $this->expect( T_OPEN_TAG ); $token = $this->skipSpace(); if ( $token->isEnd() ) { break 2; @@ -701,7 +710,7 @@ class ConfEditor { } /** - * Set the parse position. Do not call this except from firstToken() and + * Set the parse position. Do not call this except from firstToken() and * nextToken(), there is more to update than just the position. */ protected function setPos( $pos ) { @@ -795,7 +804,7 @@ class ConfEditor { if ( $this->currentToken && $this->currentToken->type == $type ) { return $this->nextToken(); } else { - $this->error( "expected " . $this->getTypeName( $type ) . + $this->error( "expected " . $this->getTypeName( $type ) . ", got " . $this->getTypeName( $this->currentToken->type ) ); } } @@ -830,7 +839,6 @@ class ConfEditor { * not call except from popPath() or nextPath(). */ function endPath() { - $i = count( $this->pathStack ) - 1; $key = ''; foreach ( $this->pathStack as $pathInfo ) { if ( $key !== '' ) { @@ -871,8 +879,8 @@ class ConfEditor { } /** - * Go to the next path on the same level. This ends the current path and - * starts a new one. If $path is @next, the new path is set to the next + * Go to the next path on the same level. This ends the current path and + * starts a new one. If $path is \@next, the new path is set to the next * numeric array element. */ function nextPath( $path ) { @@ -885,7 +893,7 @@ class ConfEditor { } else { $this->pathStack[$i]['name'] = $path; } - $this->pathStack[$i] = + $this->pathStack[$i] = array( 'startByte' => $this->byteNum, 'startToken' => $this->pos, @@ -951,8 +959,8 @@ class ConfEditor { } /** - * Looks ahead to see if the given type is the next token type, starting - * from the current position plus the given offset. Skips any intervening + * Looks ahead to see if the given type is the next token type, starting + * from the current position plus the given offset. Skips any intervening * whitespace. */ function isAhead( $type, $offset = 0 ) { @@ -988,8 +996,8 @@ class ConfEditor { $out = ''; foreach ( $this->tokens as $token ) { $obj = $this->newTokenObj( $token ); - $out .= sprintf( "%-28s %s\n", - $this->getTypeName( $obj->type ), + $out .= sprintf( "%-28s %s\n", + $this->getTypeName( $obj->type ), addcslashes( $obj->text, "\0..\37" ) ); } echo "
" . htmlspecialchars( $out ) . "
"; @@ -1004,7 +1012,7 @@ class ConfEditorParseError extends MWException { function __construct( $editor, $msg ) { $this->lineNum = $editor->lineNum; $this->colNum = $editor->colNum; - parent::__construct( "Parse error on line {$editor->lineNum} " . + parent::__construct( "Parse error on line {$editor->lineNum} " . "col {$editor->colNum}: $msg" ); } @@ -1024,7 +1032,7 @@ class ConfEditorParseError extends MWException { */ class ConfEditorToken { var $type, $text; - + static $scalarTypes = array( T_LNUMBER, T_DNUMBER, T_STRING, T_CONSTANT_ENCAPSED_STRING ); static $skipTypes = array( T_WHITESPACE, T_COMMENT, T_DOC_COMMENT );