Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
RCold committed Oct 16, 2016
1 parent c666413 commit a553d95
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 115 deletions.
24 changes: 21 additions & 3 deletions includes/bootstrap.inc
Expand Up @@ -410,9 +410,27 @@ function conf_init() {
include_once './'. conf_path() .'/settings.php';
}

// Ignore the placeholder URL from default.settings.php.
if (isset($db_url) && $db_url == 'mysql://username:password@localhost/databasename') {
$db_url = '';
if (is_string($db_url)) {
if ($db_url == 'mysql://username:password@localhost/databasename') {
// Ignore the placeholder URL from default.settings.php.
$db_url = NULL;
}
else {
$db_url = array('default' => $db_url);
}
}
elseif (!is_array($db_url)) {
$db_url = NULL;
}

if (is_string($db_prefix)) {
$db_prefix = array('default' => $db_prefix);
}
elseif (!is_array($db_prefix)) {
$db_prefix = array('default' => '');
}
elseif (!isset($db_prefix['default'])) {
$db_prefix['default'] = '';
}

if (isset($base_url)) {
Expand Down
198 changes: 87 additions & 111 deletions includes/database.inc 100755 → 100644
Expand Up @@ -134,16 +134,6 @@ function update_sql() {
function db_prefix_tables($query) {
global $db_prefix;

if (is_string($db_prefix)) {
$db_prefix = array('default' => $db_prefix);
}
elseif (!is_array($db_prefix)) {
$db_prefix = array('default' => '');
}
elseif (!isset($db_prefix['default'])) {
$db_prefix['default'] = '';
}

$db_prefixes = $db_prefix;
unset($db_prefixes['default']);

Expand Down Expand Up @@ -177,19 +167,6 @@ function db_set_active($name = 'default') {
global $db_url, $db_type, $active_db;
static $db_conns, $active_name = FALSE;

if (is_string($db_url)) {
if ($db_url == 'mysql://username:password@localhost/databasename') {
// Ignore the placeholder URL
$db_url = NULL;
}
else {
$db_url = array('default' => $db_url);
}
}
elseif (!is_array($db_url)) {
$db_url = NULL;
}

if (empty($db_url)) {
include_once './includes/install.inc';
install_goto('install.php');
Expand Down Expand Up @@ -257,104 +234,103 @@ function db_is_active() {
}

function _db_process_sql() {
global $db_prefix;

if (is_string($db_prefix)) {
$db_prefix = array('default' => $db_prefix);
}
elseif (!is_array($db_prefix)) {
$db_prefix = array('default' => '');
}
elseif (!isset($db_prefix['default'])) {
$db_prefix['default'] = '';
}

$args = func_get_args();
$query = array_shift($args);

if (isset($args[0]) && is_array($args[0])) { // 'All arguments in one array' syntax
$args = $args[0];
}

$query = preg_replace_callback("/%d|'%s'|%s|%n|%%|%f|%b|%l|%v|\\{[^}]*\\}/", function ($matches) use (&$args, $db_prefix) {
switch ($matches[0]) {
case '%d':
$value = array_shift($args);
if (is_bool($value)) {
return (int) $value;
}
$value = trim($value);
// 64-bit integer values can't be treated as int in 32-bit systems.
// is_numeric() allows decimal part, exponential part (+0123.45e6) and
// hex values (0xf4c3b00c), but they are not valid.
return is_numeric($value) && empty(strpbrk($value, '.EeXx')) ? $value : 'NULL';
case "'%s'":
$value = array_shift($args);
if (is_null($value)) {
return 'NULL';
}
return "'". db_escape_string($value) ."'";
case '%s':
$value = array_shift($args);
return db_escape_string($value);
case '%n':
$value = trim(array_shift($args));
// Numeric values have arbitrary precision, so can't be treated as float.
// is_numeric() allows exponential part (+0123.45e6) and hex values
// (0xf4c3b00c), but they are not valid.
return is_numeric($value) && empty(strpbrk($value, 'EeXx')) ? $value : 'NULL';
case '%%':
return '%';
case '%f':
$value = array_shift($args);
if (is_null($value)) {
return 'NULL';
}
return (float) $value;
case '%b':
// Binary data.
$value = array_shift($args);
if (is_null($value)) {
return 'NULL';
}
return db_encode_blob($value);
case '%l':
// Dynamic table, column or constraint name.
$value = array_shift($args);
return db_escape_table($value);
case '%v':
// Mixed variable. Internal use only.
$value = array_shift($args);
if (is_bool($value)) {
return (int) $value;
}
$numeric = trim($value);
if (is_numeric($numeric) && empty(strpbrk($numeric, 'Xx'))) {
return $numeric;
}
if (is_string($value)) {
return "'". db_escape_string($value) ."'";
}
return 'NULL';
default:
// Prefix table name.
$table = trim(substr($matches[0], 1, -1));
if ($table == '%l') {
// Dynamic table name.
$table = array_shift($args);
}
if (array_key_exists($table, $db_prefix)) {
$table = $db_prefix[$table] . $table;
}
else {
$table = $db_prefix['default'] . $table;
}
return db_escape_table($table);
}
}, $query);
_db_process_sql_callback(NULL, $args);
$query = preg_replace_callback("/%d|'%s'|%s|%n|%%|%f|%b|%l|%v|\\{[^}]*\\}/", '_db_process_sql_callback', $query);
return $query;
}

function _db_process_sql_callback($matches, $init = NULL) {
global $db_prefix;
static $args;

if (isset($init)) {
$args = $init;
return NULL;
}

switch ($matches[0]) {
case '%d':
$value = array_shift($args);
if (is_bool($value)) {
return (int) $value;
}
$value = trim($value);
// 64-bit integer values can't be treated as int in 32-bit systems.
// is_numeric() allows decimal part, exponential part (+0123.45e6) and
// hex values (0xf4c3b00c), but they are not valid.
return is_numeric($value) && empty(strpbrk($value, '.EeXx')) ? $value : 'NULL';
case "'%s'":
$value = array_shift($args);
if (is_null($value)) {
return 'NULL';
}
return "'" . db_escape_string($value) . "'";
case '%s':
$value = array_shift($args);
return db_escape_string($value);
case '%n':
$value = trim(array_shift($args));
// Numeric values have arbitrary precision, so can't be treated as float.
// is_numeric() allows exponential part (+0123.45e6) and hex values
// (0xf4c3b00c), but they are not valid.
return is_numeric($value) && empty(strpbrk($value, 'EeXx')) ? $value : 'NULL';
case '%%':
return '%';
case '%f':
$value = array_shift($args);
if (is_null($value)) {
return 'NULL';
}
return (float) $value;
case '%b':
// Binary data.
$value = array_shift($args);
if (is_null($value)) {
return 'NULL';
}
return db_encode_blob($value);
case '%l':
// Dynamic table, column or constraint name.
$value = array_shift($args);
return db_escape_table($value);
case '%v':
// Mixed variable. Internal use only.
$value = array_shift($args);
if (is_bool($value)) {
return (int) $value;
}
$numeric = trim($value);
if (is_numeric($numeric) && empty(strpbrk($numeric, 'Xx'))) {
return $numeric;
}
if (is_string($value)) {
return "'" . db_escape_string($value) . "'";
}
return 'NULL';
default:
// Prefix table name.
$table = trim(substr($matches[0], 1, -1));
if ($table == '%l') {
// Dynamic table name.
$table = array_shift($args);
}
if (array_key_exists($table, $db_prefix)) {
$table = $db_prefix[$table] . $table;
}
else {
$table = $db_prefix['default'] . $table;
}
return db_escape_table($table);
}
}

/**
* Generate placeholders for an array of query arguments of a single type.
*
Expand Down
Empty file modified includes/database.mysql-common.inc 100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion includes/database.pgsql.inc 100755 → 100644
Expand Up @@ -596,7 +596,7 @@ function _db_create_field_sql($field, $spec) {
}

if (isset($spec['default'])) {
$sql .= _db_process_sql(' DEFAULT %v', $spec['default']);
$sql .= _db_process_sql(' DEFAULT '. db_type_placeholder($spec['type']), $spec['default']);
}

return $sql;
Expand Down
Empty file modified includes/pager.inc 100755 → 100644
Empty file.
Empty file modified modules/blogapi/blogapi.install 100755 → 100644
Empty file.
Empty file modified modules/taxonomy/taxonomy.module 100755 → 100644
Empty file.
Empty file modified scripts/drupal.sh 100755 → 100644
Empty file.

0 comments on commit a553d95

Please sign in to comment.