Changeset 119 for trunk/lib/DB.inc.php


Ignore:
Timestamp:
May 20, 2006 2:12:12 AM (18 years ago)
Author:
scdev
Message:

Q - making codebase 2 work with php5. Rewrote ImageThumb? class to work with GD

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/DB.inc.php

    r71 r119  
    8585    function setParam($params)
    8686    {
    87         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    88             $this =& DB::getInstance();
     87        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
     88            $_this =& DB::getInstance();
    8989        }
    9090
    9191        if (isset($params) && is_array($params)) {
    9292            // Merge new parameters with old overriding only those passed.
    93             $this->_params = array_merge($this->_params, $params);
     93            $_this->_params = array_merge($_this->_params, $params);
    9494        } else {
    9595            App::logMsg(sprintf('Parameters are not an array: %s', $params), LOG_ERR, __FILE__, __LINE__);
     
    108108    function getParam($param)
    109109    {
    110         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    111             $this =& DB::getInstance();
    112         }
    113 
    114         if (isset($this->_params[$param])) {
    115             return $this->_params[$param];
     110        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
     111            $_this =& DB::getInstance();
     112        }
     113
     114        if (isset($_this->_params[$param])) {
     115            return $_this->_params[$param];
    116116        } else {
    117117            App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
     
    129129    function connect()
    130130    {
    131         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    132             $this =& DB::getInstance();
    133         }
    134 
    135         if (!$this->getParam('db_name') || !$this->getParam('db_user') || !$this->getParam('db_pass')) {
     131        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
     132            $_this =& DB::getInstance();
     133        }
     134
     135        if (!$_this->getParam('db_name') || !$_this->getParam('db_user') || !$_this->getParam('db_pass')) {
    136136            App::logMsg('Database credentials missing.', LOG_EMERG, __FILE__, __LINE__);
    137137            return false;
     
    139139
    140140        // Connect to database. Always create a new link to the server.
    141         if ($this->dbh = mysql_connect($this->getParam('db_server'), $this->getParam('db_user'), $this->getParam('db_pass'), true)) {
     141        if ($_this->dbh = mysql_connect($_this->getParam('db_server'), $_this->getParam('db_user'), $_this->getParam('db_pass'), true)) {
    142142            // Select database
    143             mysql_select_db($this->getParam('db_name'), $this->dbh);
     143            mysql_select_db($_this->getParam('db_name'), $_this->dbh);
    144144        }
    145145
    146146        // Test for connection errors.
    147         if (!$this->dbh || mysql_error($this->dbh)) {
    148             $mysql_error_msg = $this->dbh ? 'Codebase MySQL error: (' . mysql_errno($this->dbh) . ') ' . mysql_error($this->dbh) : 'Codebase MySQL error: Could not connect to server.';
     147        if (!$_this->dbh || mysql_error($_this->dbh)) {
     148            $mysql_error_msg = $_this->dbh ? 'Codebase MySQL error: (' . mysql_errno($_this->dbh) . ') ' . mysql_error($_this->dbh) : 'Codebase MySQL error: Could not connect to server.';
    149149            App::logMsg($mysql_error_msg, LOG_EMERG, __FILE__, __LINE__);
    150150
    151151            // Print helpful or pretty error?
    152             if ($this->getParam('db_debug')) {
     152            if ($_this->getParam('db_debug')) {
    153153                echo $mysql_error_msg . "\n";
    154154            } else {
     
    157157
    158158            // Die or continue without connection?
    159             if ($this->getParam('db_die_on_failure')) {
     159            if ($_this->getParam('db_die_on_failure')) {
    160160                echo "\n\n<!-- Script execution stopped out of embarrassment. -->";
    161161                die;
     
    166166
    167167        // DB connection success!
    168         $this->_connected = true;
     168        $_this->_connected = true;
    169169
    170170        // Tell MySQL what character set we're useing. Available only on MySQL verions > 4.01.01.
    171         $this->query("/*!40101 SET NAMES '" . $this->mysql_character_sets[strtolower(App::getParam('character_set'))] . "' */");
     171        $_this->query("/*!40101 SET NAMES '" . $_this->mysql_character_sets[strtolower(App::getParam('character_set'))] . "' */");
    172172
    173173        return true;
     
    183183    function close()
    184184    {
    185         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    186             $this =& DB::getInstance();
    187         }
    188 
    189         if (!$this->_connected) {
    190             return false;
    191         }
    192 
    193         mysql_close($this->dbh);
     185        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
     186            $_this =& DB::getInstance();
     187        }
     188
     189        if (!$_this->_connected) {
     190            return false;
     191        }
     192
     193        mysql_close($_this->dbh);
    194194    }
    195195
     
    204204    function getDBH()
    205205    {
    206         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    207             $this =& DB::getInstance();
    208         }
    209 
    210         if (!$this->_connected) {
    211             return false;
    212         }
    213 
    214         return $this->dbh;
     206        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
     207            $_this =& DB::getInstance();
     208        }
     209
     210        if (!$_this->_connected) {
     211            return false;
     212        }
     213
     214        return $_this->dbh;
    215215    }
    216216
     
    238238    function escapeString($string)
    239239    {
    240         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    241             $this =& DB::getInstance();
    242         }
    243         return mysql_real_escape_string($string, $this->dbh);
     240        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
     241            $_this =& DB::getInstance();
     242        }
     243        return mysql_real_escape_string($string, $_this->dbh);
    244244    }
    245245
     
    256256        static $_query_count = 0;
    257257
    258         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    259             $this =& DB::getInstance();
    260         }
    261 
    262         if (!$this->_connected) {
     258        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
     259            $_this =& DB::getInstance();
     260        }
     261
     262        if (!$_this->_connected) {
    263263           return false;
    264264        }
     
    266266        $_query_count++;
    267267        $debugqry = preg_replace("/\n[\t ]+/", "\n", $query);
    268         if ($this->getParam('db_always_debug') || $debug) {
     268        if ($_this->getParam('db_always_debug') || $debug) {
    269269            echo "<!-- ----------------- Query $_query_count ---------------------\n$debugqry\n-->\n";
    270270        }
    271271
    272272        // Execute!
    273         $qid = mysql_query($query, $this->dbh);
     273        $qid = mysql_query($query, $_this->dbh);
    274274
    275275        // Error checking.
    276         if (!$qid || mysql_error($this->dbh)) {
    277             if ($this->getParam('db_debug')) {
    278                 echo '<pre style="padding:2em; background:#ddd; font:9px monaco;">' . wordwrap(mysql_error($this->dbh)) . '<hr>' . htmlspecialchars($debugqry) . '</pre>';
     276        if (!$qid || mysql_error($_this->dbh)) {
     277            if ($_this->getParam('db_debug')) {
     278                echo '<pre style="padding:2em; background:#ddd; font:9px monaco;">' . wordwrap(mysql_error($_this->dbh)) . '<hr>' . htmlspecialchars($debugqry) . '</pre>';
    279279            } else {
    280280                echo _("This page is temporarily unavailable. It should be back up in a few minutes.");
    281281            }
    282             App::logMsg(sprintf('MySQL error %s: %s in query: %s', mysql_errno($this->dbh), mysql_error($this->dbh), $debugqry), LOG_EMERG, __FILE__, __LINE__);
    283             if ($this->getParam('db_die_on_failure')) {
     282            App::logMsg(sprintf('MySQL error %s: %s in query: %s', mysql_errno($_this->dbh), mysql_error($_this->dbh), $debugqry), LOG_EMERG, __FILE__, __LINE__);
     283            if ($_this->getParam('db_die_on_failure')) {
    284284                echo "\n\n<!-- Script execution stopped out of embarrassment. -->";
    285285                die;
     
    302302    function tableExists($table, $use_cached_results=true)
    303303    {
    304         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    305             $this =& DB::getInstance();
    306         }
    307 
    308         if (!$this->_connected) {
    309             return false;
    310         }
    311 
    312         if (!isset($this->existing_tables) || !$use_cached_results) {
    313             $this->existing_tables = array();
    314             $qid = $this->query("SHOW TABLES");
     304        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
     305            $_this =& DB::getInstance();
     306        }
     307
     308        if (!$_this->_connected) {
     309            return false;
     310        }
     311
     312        if (!isset($_this->existing_tables) || !$use_cached_results) {
     313            $_this->existing_tables = array();
     314            $qid = $_this->query("SHOW TABLES");
    315315            while (list($row) = mysql_fetch_row($qid)) {
    316                 $this->existing_tables[] = $row;
    317             }
    318         }
    319         if (in_array($table, $this->existing_tables)) {
     316                $_this->existing_tables[] = $row;
     317            }
     318        }
     319        if (in_array($table, $_this->existing_tables)) {
    320320            return true;
    321321        } else {
    322             App::logMsg(sprintf('nonexistent DB table: %s.%s', $this->getParam('db_name'), $table), LOG_ALERT, __FILE__, __LINE__);
     322            App::logMsg(sprintf('nonexistent DB table: %s.%s', $_this->getParam('db_name'), $table), LOG_ALERT, __FILE__, __LINE__);
    323323            return false;
    324324        }
     
    336336    function columnExists($table, $columns, $strict=true, $use_cached_results=true)
    337337    {
    338         if (!isset($this) || !is_a($this, 'DB') && !is_subclass_of($this, 'DB')) {
    339             $this =& DB::getInstance();
    340         }
    341 
    342         if (!$this->_connected) {
     338        if (!isset($_this) || !is_a($_this, 'DB') && !is_subclass_of($_this, 'DB')) {
     339            $_this =& DB::getInstance();
     340        }
     341
     342        if (!$_this->_connected) {
    343343            return false;
    344344        }
    345345
    346346        // Ensure the table exists.
    347         if (!$this->tableExists($table, $use_cached_results)) {
     347        if (!$_this->tableExists($table, $use_cached_results)) {
    348348            return false;
    349349        }
     
    354354        }
    355355
    356         if (!isset($this->table_columns[$table]) || !$use_cached_results) {
     356        if (!isset($_this->table_columns[$table]) || !$use_cached_results) {
    357357            // Populate and cache array of current columns for this table.
    358             $this->table_columns[$table] = array();
    359             $qid = $this->query("DESCRIBE $table");
     358            $_this->table_columns[$table] = array();
     359            $qid = $_this->query("DESCRIBE $table");
    360360            while ($row = mysql_fetch_row($qid)) {
    361                 $this->table_columns[$table][] = $row[0];
     361                $_this->table_columns[$table][] = $row[0];
    362362            }
    363363        }
     
    366366            // Do an exact comparison of table schemas.
    367367            sort($columns);
    368             sort($this->table_columns[$table]);
    369             return $this->table_columns[$table] == $columns;
     368            sort($_this->table_columns[$table]);
     369            return $_this->table_columns[$table] == $columns;
    370370        } else {
    371371            // Only check that the specified columns are available in the table.
    372             $match_columns = array_intersect($this->table_columns[$table], $columns);
     372            $match_columns = array_intersect($_this->table_columns[$table], $columns);
    373373            sort($columns);
    374374            sort($match_columns);
Note: See TracChangeset for help on using the changeset viewer.