Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
JanJakes committed Jan 9, 2025
1 parent 68eb288 commit a8a1a93
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 33 deletions.
3 changes: 2 additions & 1 deletion tests/WP_SQLite_Driver_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -3151,7 +3151,8 @@ public function testTranslateLikeBinary() {

// Test escaping - "\\\%" is "\" and a wildcard
$result = $this->assertQuery( "SELECT * FROM _tmp_table WHERE name LIKE BINARY 'special\\\\\\%chars'" );
$this->assertCount( 0, $result );
$this->assertCount( 1, $result );
$this->assertEquals( 'special\\chars', $result[0]->name );

// Test LIKE without BINARY
$result = $this->assertQuery( "SELECT * FROM _tmp_table WHERE name LIKE 'FIRST'" );
Expand Down
40 changes: 8 additions & 32 deletions wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -794,41 +794,17 @@ public function _helper_like_to_glob_pattern( $pattern ) {
$pattern = str_replace( '*', '[*]', $pattern );
$pattern = str_replace( '?', '[?]', $pattern );

$glob_pattern = '';
for ( $i = 0; $i < strlen( $pattern ); $i += 1 ) {
$byte1 = $pattern[ $i ];
if ( '\\' === $byte1 ) {
// Add the escape character.
$glob_pattern .= $byte1;

// Special case: "\\%" and "\\_" are equivalent to "\%" and "\_".
// In such case, we need to skip the extra backslash.
$byte2 = $pattern[ $i + 1 ] ?? null;
$byte3 = $pattern[ $i + 2 ] ?? null;
if ( '\\' === $byte2 && ( '%' === $byte3 || '_' === $byte3 ) ) {
$glob_pattern .= $byte3;
$i += 2;
continue;
}

// We're in an escape sequence. Add the next character as it is.
$glob_pattern .= $byte2;
$i += 1;
} elseif ( '%' === $byte1 ) {
$glob_pattern .= '*';
} elseif ( '_' === $byte1 ) {
$glob_pattern .= '?';
} else {
$glob_pattern .= $byte1;
}
}
$pattern = preg_replace('/(^|[^\\\\](?:\\\\{2}))*(\\\\[%_])/', '$1\\\\$2', $pattern);

Check failure on line 797 in wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php

View workflow job for this annotation

GitHub Actions / Check code style

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 797 in wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php

View workflow job for this annotation

GitHub Actions / Check code style

Expected 1 spaces before closing parenthesis; 0 found

// 1. Unescape C-style escape sequences.
$glob_pattern = stripcslashes($glob_pattern);
$pattern = stripcslashes($pattern);

Check failure on line 800 in wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php

View workflow job for this annotation

GitHub Actions / Check code style

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 800 in wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php

View workflow job for this annotation

GitHub Actions / Check code style

Expected 1 spaces before closing parenthesis; 0 found

// 2. Unescape LIKE escape sequences.
$glob_pattern = preg_replace('/\\\\(.)/', '$1', $glob_pattern);
$pattern = preg_replace('/(^|[^\\\\](?:\\\\{2})*)%/', '$1*', $pattern);

Check failure on line 802 in wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php

View workflow job for this annotation

GitHub Actions / Check code style

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 802 in wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php

View workflow job for this annotation

GitHub Actions / Check code style

Expected 1 spaces before closing parenthesis; 0 found
$pattern = preg_replace('/(^|[^\\\\](?:\\\\{2})*)_/', '$1?', $pattern);

Check failure on line 803 in wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php

View workflow job for this annotation

GitHub Actions / Check code style

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 803 in wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php

View workflow job for this annotation

GitHub Actions / Check code style

Expected 1 spaces before closing parenthesis; 0 found

return $glob_pattern;
// 2. Unescape LIKE escape sequences.
$pattern = preg_replace('/\\\\(.)/', '$1', $pattern);

Check failure on line 806 in wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php

View workflow job for this annotation

GitHub Actions / Check code style

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 806 in wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php

View workflow job for this annotation

GitHub Actions / Check code style

Expected 1 spaces before closing parenthesis; 0 found

return $pattern;
}
}

0 comments on commit a8a1a93

Please sign in to comment.