remove global db variable

This commit is contained in:
redmatrix 2016-04-25 16:55:33 -07:00
parent 6291c08e01
commit c3b0c0f32a
6 changed files with 85 additions and 96 deletions

View File

@ -57,7 +57,7 @@ class Setup extends \Zotlabs\Web\Controller {
* @param[in,out] App &$a * @param[in,out] App &$a
*/ */
function post() { function post() {
global $db;
switch($this->install_wizard_pass) { switch($this->install_wizard_pass) {
case 1: case 1:
@ -82,37 +82,14 @@ class Setup extends \Zotlabs\Web\Controller {
$siteurl = rtrim($siteurl,'/'); $siteurl = rtrim($siteurl,'/');
require_once('include/dba/dba_driver.php'); require_once('include/dba/dba_driver.php');
unset($db); $db = null;
$db = dba_factory($dbhost, $dbport, $dbuser, $dbpass, $dbdata, $dbtype, true); $db = \DBA::dba_factory($dbhost, $dbport, $dbuser, $dbpass, $dbdata, $dbtype, true);
if(! $db->connected) { if(! $db->connected) {
echo 'Database Connect failed: ' . $db->error; echo 'Database Connect failed: ' . $db->error;
killme(); killme();
\App::$data['db_conn_failed']=true; \App::$data['db_conn_failed']=true;
} }
/*if(get_db_errno()) {
unset($db);
$db = dba_factory($dbhost, $dbport, $dbuser, $dbpass, '', true);
if(! get_db_errno()) {
$r = q("CREATE DATABASE '%s'",
dbesc($dbdata)
);
if($r) {
unset($db);
$db = new dba($dbhost, $dbport, $dbuser, $dbpass, $dbdata, true);
} else {
\App::$data['db_create_failed']=true;
}
} else {
\App::$data['db_conn_failed']=true;
return;
}
}*/
//if(get_db_errno()) {
//}
return; return;
break; break;
case 4: case 4:
@ -139,7 +116,7 @@ class Setup extends \Zotlabs\Web\Controller {
} }
// connect to db // connect to db
$db = dba_factory($dbhost, $dbport, $dbuser, $dbpass, $dbdata, $dbtype, true); $db = \DBA::dba_factory($dbhost, $dbport, $dbuser, $dbpass, $dbdata, $dbtype, true);
if(! $db->connected) { if(! $db->connected) {
echo 'CRITICAL: DB not connected.'; echo 'CRITICAL: DB not connected.';
@ -195,7 +172,6 @@ class Setup extends \Zotlabs\Web\Controller {
* @return string parsed HTML output * @return string parsed HTML output
*/ */
function get() { function get() {
global $db;
$o = ''; $o = '';
$wizard_status = ''; $wizard_status = '';
@ -228,7 +204,7 @@ class Setup extends \Zotlabs\Web\Controller {
$txt .= "<pre>".\App::$data['db_failed'] . "</pre>". EOL ; $txt .= "<pre>".\App::$data['db_failed'] . "</pre>". EOL ;
$db_return_text .= $txt; $db_return_text .= $txt;
} }
if($db && $db->connected) { if(\DBA::$dba && \DBA::$dba->connected) {
$r = q("SELECT COUNT(*) as `total` FROM `account`"); $r = q("SELECT COUNT(*) as `total` FROM `account`");
if($r && count($r) && $r[0]['total']) { if($r && count($r) && $r[0]['total']) {
$tpl = get_markup_template('install.tpl'); $tpl = get_markup_template('install.tpl');
@ -693,12 +669,12 @@ class Setup extends \Zotlabs\Web\Controller {
function load_database($db) { function load_database($db) {
$str = file_get_contents($db->get_install_script()); $str = file_get_contents(\DBA::$dba->get_install_script());
$arr = explode(';',$str); $arr = explode(';',$str);
$errors = false; $errors = false;
foreach($arr as $a) { foreach($arr as $a) {
if(strlen(trim($a))) { if(strlen(trim($a))) {
$r = @$db->q(trim($a)); $r = @\DBA::$dba->q(trim($a));
if(! $r) { if(! $r) {
$errors .= t('Errors encountered creating database tables.') . $a . EOL; $errors .= t('Errors encountered creating database tables.') . $a . EOL;
} }

View File

@ -1,6 +1,7 @@
<?php /** @file */ <?php /** @file */
require_once('boot.php'); require_once('boot.php');
require_once('include/dba/dba_driver.php');
// Everything we need to boot standalone 'background' processes // Everything we need to boot standalone 'background' processes
@ -14,7 +15,7 @@ function cli_startup() {
App::init(); App::init();
if(is_null($db)) { if(! DBA::$dba) {
@include(".htconfig.php"); @include(".htconfig.php");
$a->convert(); $a->convert();
@ -25,8 +26,7 @@ function cli_startup() {
App::$timezone = ((x($default_timezone)) ? $default_timezone : 'UTC'); App::$timezone = ((x($default_timezone)) ? $default_timezone : 'UTC');
date_default_timezone_set(App::$timezone); date_default_timezone_set(App::$timezone);
require_once('include/dba/dba_driver.php'); $db = DBA::dba_factory($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type);
$db = dba_factory($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type);
unset($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type); unset($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type);
}; };

View File

@ -7,44 +7,57 @@
* functions for working with databases. * functions for working with databases.
*/ */
/**
* @brief Returns the database driver object.
*
* If available it will use PHP's mysqli otherwise mysql driver.
*
* @param string $server DB server name
* @param string $port DB port
* @param string $user DB username
* @param string $pass DB password
* @param string $db database name
* @param string $dbtype 0 for mysql, 1 for postgres
* @param bool $install Defaults to false
* @return null|dba_driver A database driver object (dba_mysql|dba_mysqli) or null if no driver found.
*/
function dba_factory($server, $port,$user,$pass,$db,$dbtype,$install = false) {
$dba = null;
$dbtype = intval($dbtype); class DBA {
if($dbtype == DBTYPE_POSTGRES) { static public $dba = null;
require_once('include/dba/dba_postgres.php'); static public $dbtype = null;
if(is_null($port)) $port = 5432;
$dba = new dba_postgres($server, $port, $user, $pass, $db, $install);
} else { /**
if(class_exists('mysqli')) { * @brief Returns the database driver object.
if (is_null($port)) $port = ini_get("mysqli.default_port"); *
require_once('include/dba/dba_mysqli.php'); * If available it will use PHP's mysqli otherwise mysql driver.
$dba = new dba_mysqli($server, $port,$user,$pass,$db,$install); *
} else { * @param string $server DB server name
if (is_null($port)) $port = "3306"; * @param string $port DB port
require_once('include/dba/dba_mysql.php'); * @param string $user DB username
$dba = new dba_mysql($server, $port,$user,$pass,$db,$install); * @param string $pass DB password
* @param string $db database name
* @param string $dbtype 0 for mysql, 1 for postgres
* @param bool $install Defaults to false
* @return null|dba_driver A database driver object (dba_mysql|dba_mysqli) or null if no driver found.
*/
function dba_factory($server, $port,$user,$pass,$db,$dbtype,$install = false) {
self::$dba = null;
self::$dbtype = intval($dbtype);
if(self::$dbtype == DBTYPE_POSTGRES) {
require_once('include/dba/dba_postgres.php');
if(is_null($port)) $port = 5432;
self::$dba = new dba_postgres($server, $port, $user, $pass, $db, $install);
}
else {
if(class_exists('mysqli')) {
if (is_null($port)) $port = ini_get("mysqli.default_port");
require_once('include/dba/dba_mysqli.php');
self::$dba = new dba_mysqli($server, $port,$user,$pass,$db,$install);
}
else {
// UNSUPPORTED, OBSOLETE
if (is_null($port)) $port = "3306";
require_once('include/dba/dba_mysql.php');
self::$dba = new dba_mysql($server, $port,$user,$pass,$db,$install);
}
} }
}
define('NULL_DATE', $dba->get_null_date()); define('NULL_DATE', self::$dba->get_null_date());
define('ACTIVE_DBTYPE', $dbtype); define('ACTIVE_DBTYPE', self::$dbtype);
return $dba; return self::$dba;
}
} }
/** /**
@ -53,6 +66,7 @@ function dba_factory($server, $port,$user,$pass,$db,$dbtype,$install = false) {
* This class gets extended by the real database driver classes, e.g. dba_mysql, * This class gets extended by the real database driver classes, e.g. dba_mysql,
* dba_mysqli. * dba_mysqli.
*/ */
abstract class dba_driver { abstract class dba_driver {
// legacy behavior // legacy behavior
const INSTALL_SCRIPT='install/schema_mysql.sql'; const INSTALL_SCRIPT='install/schema_mysql.sql';
@ -203,10 +217,10 @@ function printable($s) {
* @param int $state 0 to disable debugging * @param int $state 0 to disable debugging
*/ */
function dbg($state) { function dbg($state) {
global $db; // global $db;
if($db) if(DBA::$dba)
$db->dbg($state); DBA::$dba->dbg($state);
} }
/** /**
@ -220,21 +234,20 @@ function dbg($state) {
* @return Return an escaped string of the value to pass to a DB query. * @return Return an escaped string of the value to pass to a DB query.
*/ */
function dbesc($str) { function dbesc($str) {
global $db; if(DBA::$dba && DBA::$dba->connected)
return(DBA::$dba->escape($str));
if($db && $db->connected)
return($db->escape($str));
else else
return(str_replace("'", "\\'", $str)); return(str_replace("'", "\\'", $str));
} }
function dbescbin($str) { function dbescbin($str) {
global $db; global $db;
return $db->escapebin($str); return DBA::$dba->escapebin($str);
} }
function dbunescbin($str) { function dbunescbin($str) {
global $db; global $db;
return $db->unescapebin($str); return DBA::$dba->unescapebin($str);
} }
function dbescdate($date) { function dbescdate($date) {
@ -248,27 +261,27 @@ function dbescdate($date) {
function db_quoteinterval($txt) { function db_quoteinterval($txt) {
global $db; global $db;
return $db->quote_interval($txt); return DBA::$dba->quote_interval($txt);
} }
function dbesc_identifier($str) { function dbesc_identifier($str) {
global $db; global $db;
return $db->escape_identifier($str); return DBA::$dba->escape_identifier($str);
} }
function db_utcnow() { function db_utcnow() {
global $db; global $db;
return $db->utcnow(); return DBA::$dba->utcnow();
} }
function db_optimizetable($table) { function db_optimizetable($table) {
global $db; global $db;
$db->optimize_table($table); DBA::$dba->optimize_table($table);
} }
function db_concat($fld, $sep) { function db_concat($fld, $sep) {
global $db; global $db;
return $db->concat($fld, $sep); return DBA::$dba->concat($fld, $sep);
} }
// Function: q($sql,$args); // Function: q($sql,$args);
@ -298,7 +311,7 @@ function q($sql) {
$args = func_get_args(); $args = func_get_args();
unset($args[0]); unset($args[0]);
if($db && $db->connected) { if(DBA::$dba && DBA::$dba->connected) {
$stmt = vsprintf($sql, $args); $stmt = vsprintf($sql, $args);
if($stmt === false) { if($stmt === false) {
if(version_compare(PHP_VERSION, '5.4.0') >= 0) if(version_compare(PHP_VERSION, '5.4.0') >= 0)
@ -307,7 +320,7 @@ function q($sql) {
else else
logger('dba: vsprintf error: ' . print_r(debug_backtrace(), true),LOGGER_NORMAL,LOG_CRIT); logger('dba: vsprintf error: ' . print_r(debug_backtrace(), true),LOGGER_NORMAL,LOG_CRIT);
} }
return $db->q($stmt); return DBA::$dba->q($stmt);
} }
/* /*
@ -329,8 +342,8 @@ function q($sql) {
function dbq($sql) { function dbq($sql) {
global $db; global $db;
if($db && $db->connected) if(DBA::$dba && DBA::$dba->connected)
$ret = $db->q($sql); $ret = DBA::$dba->q($sql);
else else
$ret = false; $ret = false;

View File

@ -2035,7 +2035,7 @@ function get_site_info() {
'admin' => $admin, 'admin' => $admin,
'site_name' => (($site_name) ? $site_name : ''), 'site_name' => (($site_name) ? $site_name : ''),
'platform' => Zotlabs\Project\System::get_platform_name(), 'platform' => Zotlabs\Project\System::get_platform_name(),
'dbdriver' => $db->getdriver(), 'dbdriver' => \DBA::$dba->getdriver(),
'lastpoll' => get_config('system','lastpoll'), 'lastpoll' => get_config('system','lastpoll'),
'info' => (($site_info) ? $site_info : ''), 'info' => (($site_info) ? $site_info : ''),
'channels_total' => $channels_total_stat, 'channels_total' => $channels_total_stat,

View File

@ -540,11 +540,11 @@ function attribute_contains($attr, $s) {
*/ */
function logger($msg, $level = LOGGER_NORMAL, $priority = LOG_INFO) { function logger($msg, $level = LOGGER_NORMAL, $priority = LOG_INFO) {
// turn off logger in install mode
global $a;
global $db;
if((App::$module == 'install') || (! ($db && $db->connected))) require_once('include/dba/dba_driver.php');
// turn off logger in install mode
if((App::$module == 'install') || (! (DBA::$dba && DBA::$dba->connected)))
return; return;
$debugging = get_config('system', 'debugging'); $debugging = get_config('system', 'debugging');
@ -621,11 +621,11 @@ function log_priority_str($priority) {
* @param int $level A log level. * @param int $level A log level.
*/ */
function dlogger($msg, $level = 0) { function dlogger($msg, $level = 0) {
// turn off logger in install mode
global $a;
global $db;
if((App::$module == 'install') || (! ($db && $db->connected))) require_once('include/dba/dba_driver.php');
// turn off logger in install mode
if((App::$module == 'install') || (! (DBA::$dba && DBA::$dba->connected)))
return; return;
$debugging = get_config('system','debugging'); $debugging = get_config('system','debugging');

View File

@ -47,7 +47,7 @@ date_default_timezone_set(App::$timezone);
require_once('include/dba/dba_driver.php'); require_once('include/dba/dba_driver.php');
if(! App::$install) { if(! App::$install) {
$db = dba_factory($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type, App::$install); $db = DBA::dba_factory($db_host, $db_port, $db_user, $db_pass, $db_data, $db_type, App::$install);
if(! $db->connected) { if(! $db->connected) {
system_unavailable(); system_unavailable();
} }