Index: trunk/phase3/includes/db/DatabaseOracle.php |
— | — | @@ -1126,7 +1126,7 @@ |
1127 | 1127 | } |
1128 | 1128 | |
1129 | 1129 | function quote_ident( $s ) { |
1130 | | - return $s; |
| 1130 | + return '"' . str_replace( '"', '""', $s ) . '"'; |
1131 | 1131 | } |
1132 | 1132 | |
1133 | 1133 | function selectRow( $table, $vars, $conds, $fname = 'DatabaseOracle::selectRow', $options = array(), $join_conds = array() ) { |
— | — | @@ -1345,15 +1345,7 @@ |
1346 | 1346 | $varnames[] = '_OracleTempTS'; |
1347 | 1347 | } |
1348 | 1348 | |
1349 | | - // Ordinary variables |
1350 | | - foreach ( $varnames as $var ) { |
1351 | | - if ( isset( $GLOBALS[$var] ) ) { |
1352 | | - $val = $this->addQuotes( $GLOBALS[$var] ); // FIXME: safety check? |
1353 | | - $ins = str_replace( '{$' . $var . '}', $val, $ins ); |
1354 | | - $ins = str_replace( '/*$' . $var . '*/`', '`' . $val, $ins ); |
1355 | | - $ins = str_replace( '/*$' . $var . '*/', $val, $ins ); |
1356 | | - } |
1357 | | - } |
| 1349 | + $ins = $this->replaceGlobalVars( $ins, $varnames ); |
1358 | 1350 | |
1359 | 1351 | return parent::replaceVars( $ins ); |
1360 | 1352 | } |
Index: trunk/phase3/includes/db/DatabasePostgres.php |
— | — | @@ -973,7 +973,7 @@ |
974 | 974 | * Return the next in a sequence, save the value for retrieval via insertId() |
975 | 975 | */ |
976 | 976 | function nextSequenceValue( $seqName ) { |
977 | | - $safeseq = preg_replace( "/'/", "''", $seqName ); |
| 977 | + $safeseq = str_replace( "'", "''", $seqName ); |
978 | 978 | $res = $this->query( "SELECT nextval('$safeseq')" ); |
979 | 979 | $row = $this->fetchRow( $res ); |
980 | 980 | $this->mInsertId = $row[0]; |
— | — | @@ -984,7 +984,7 @@ |
985 | 985 | * Return the current value of a sequence. Assumes it has been nextval'ed in this session. |
986 | 986 | */ |
987 | 987 | function currentSequenceValue( $seqName ) { |
988 | | - $safeseq = preg_replace( "/'/", "''", $seqName ); |
| 988 | + $safeseq = str_replace( "'", "''", $seqName ); |
989 | 989 | $res = $this->query( "SELECT currval('$safeseq')" ); |
990 | 990 | $row = $this->fetchRow( $res ); |
991 | 991 | $currval = $row[0]; |
— | — | @@ -1242,7 +1242,7 @@ |
1243 | 1243 | * Query whether a given schema exists. Returns the name of the owner |
1244 | 1244 | */ |
1245 | 1245 | function schemaExists( $schema ) { |
1246 | | - $eschema = preg_replace( "/'/", "''", $schema ); |
| 1246 | + $eschema = str_replace( "'", "''", $schema ); |
1247 | 1247 | $SQL = "SELECT rolname FROM pg_catalog.pg_namespace n, pg_catalog.pg_roles r " |
1248 | 1248 | ."WHERE n.nspowner=r.oid AND n.nspname = '$eschema'"; |
1249 | 1249 | $res = $this->query( $SQL ); |
— | — | @@ -1301,7 +1301,7 @@ |
1302 | 1302 | } |
1303 | 1303 | |
1304 | 1304 | function quote_ident( $s ) { |
1305 | | - return '"' . preg_replace( '/"/', '""', $s ) . '"'; |
| 1305 | + return '"' . str_replace( '"', '""', $s ) . '"'; |
1306 | 1306 | } |
1307 | 1307 | |
1308 | 1308 | /** |
Index: trunk/phase3/includes/db/Database.php |
— | — | @@ -1695,6 +1695,15 @@ |
1696 | 1696 | } |
1697 | 1697 | |
1698 | 1698 | /** |
| 1699 | + * Quotes a string using `backticks` for things like database, table, and field |
| 1700 | + * names, other databases which use something other than backticks can replace |
| 1701 | + * this with something else |
| 1702 | + */ |
| 1703 | + function quote_ident( $s ) { |
| 1704 | + return "`" . $this->strencode( $s ) . "`"; |
| 1705 | + } |
| 1706 | + |
| 1707 | + /** |
1699 | 1708 | * Escape string for safe LIKE usage. |
1700 | 1709 | * WARNING: you should almost never use this function directly, |
1701 | 1710 | * instead use buildLike() that escapes everything automatically |
— | — | @@ -2501,6 +2510,32 @@ |
2502 | 2511 | } |
2503 | 2512 | |
2504 | 2513 | /** |
| 2514 | + * Database independent variable replacement, replaces a set of named variables |
| 2515 | + * in a sql statement with the contents of their global variables. |
| 2516 | + * Supports '{$var}' `{$var}` and / *$var* / (without the spaces) style variables |
| 2517 | + * |
| 2518 | + * '{$var}' should be used for text and is passed through the database's addQuotes method |
| 2519 | + * `{$var}` should be used for identifiers (eg: table and database names), it is passed through |
| 2520 | + * the database's quote_ident method which can be overridden if the database |
| 2521 | + * uses something other than backticks. |
| 2522 | + * / *$var* / is just encoded, besides traditional dbprefix and tableoptions it's use should be avoided |
| 2523 | + * |
| 2524 | + * @param $ins String: SQL statement to replace variables in |
| 2525 | + * @param $varnames Array: Array of global variable names to replace |
| 2526 | + * @return String The new SQL statement with variables replaced |
| 2527 | + */ |
| 2528 | + protected function replaceGlobalVars( $ins, $varnames ) { |
| 2529 | + foreach ( $varnames as $var ) { |
| 2530 | + if ( isset( $GLOBALS[$var] ) ) { |
| 2531 | + $ins = str_replace( '\'{$' . $var . '}\'', $this->addQuotes( $GLOBALS[$var] ), $ins ); // replace '{$var}' |
| 2532 | + $ins = str_replace( '`{$' . $var . '}`', $this->quote_ident( $GLOBALS[$var] ), $ins ); // replace `{$var}` |
| 2533 | + $ins = str_replace( '/*$' . $var . '*/', $this->strencode( $GLOBALS[$var] ) , $ins ); // replace /*$var*/ |
| 2534 | + } |
| 2535 | + } |
| 2536 | + return $ins; |
| 2537 | + } |
| 2538 | + |
| 2539 | + /** |
2505 | 2540 | * Replace variables in sourced SQL |
2506 | 2541 | */ |
2507 | 2542 | protected function replaceVars( $ins ) { |
— | — | @@ -2510,15 +2545,7 @@ |
2511 | 2546 | 'wgDBadminuser', 'wgDBadminpassword', 'wgDBTableOptions', |
2512 | 2547 | ); |
2513 | 2548 | |
2514 | | - // Ordinary variables |
2515 | | - foreach ( $varnames as $var ) { |
2516 | | - if ( isset( $GLOBALS[$var] ) ) { |
2517 | | - $val = $this->addQuotes( $GLOBALS[$var] ); // FIXME: safety check? |
2518 | | - $ins = str_replace( '{$' . $var . '}', $val, $ins ); |
2519 | | - $ins = str_replace( '/*$' . $var . '*/`', '`' . $val, $ins ); |
2520 | | - $ins = str_replace( '/*$' . $var . '*/', $val, $ins ); |
2521 | | - } |
2522 | | - } |
| 2549 | + $ins = $this->replaceGlobalVars( $ins, $varnames ); |
2523 | 2550 | |
2524 | 2551 | // Table prefixes |
2525 | 2552 | $ins = preg_replace_callback( '!/\*(?:\$wgDBprefix|_)\*/([a-zA-Z_0-9]*)!', |
Index: trunk/phase3/includes/db/DatabaseSqlite.php |
— | — | @@ -531,7 +531,7 @@ |
532 | 532 | } |
533 | 533 | |
534 | 534 | function quote_ident( $s ) { |
535 | | - return $s; |
| 535 | + return '"' . str_replace( '"', '""', $s ) . '"'; |
536 | 536 | } |
537 | 537 | |
538 | 538 | function buildLike() { |