Ignore:
Timestamp:
Jun 3, 2006 7:47:48 PM (18 years ago)
Author:
scdev
Message:

Q - Merged branches/2.0singleton into trunk. Completed updating classes to use singleton methods. Implemented tests. Fixed some bugs. Changed some interfaces.

File:
1 edited

Legend:

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

    r111 r136  
    11<?php
    22/**
    3  * The RecordVersion:: class provides a system for saving, reviewing, and
     3 * RecordVersion.inc.php
     4 * code by strangecode :: www.strangecode.com :: this document contains copyrighted information
     5 *
     6 * The RecordVersion class provides a system for saving, reviewing, and
    47 * restoring versions of a record of any DB table. All the data in the record is
    58 * serialized, compressed, and saved in a blob in the version_tbl. Restoring a
     
    1417 * @version 2.1
    1518 */
    16 
    1719class RecordVersion {
    1820
     
    3335     * This method enforces the singleton pattern for this class.
    3436     *
    35      * @return  object  Reference to the global RecordVersion object.
     37     * @return  object  Reference to the global RecordLock object.
    3638     * @access  public
    3739     * @static
     
    3941    function &getInstance($auth_object)
    4042    {
    41         static $instances = array();
    42 
    43         if (!isset($instances[$auth_object->getVal('auth_name')])) {
    44             $instances[$auth_object->getVal('auth_name')] = new RecordVersion($auth_object);
    45         }
    46 
    47         return $instances[$auth_object->getVal('auth_name')];
     43        static $instance = null;
     44
     45        if ($instance === null) {
     46            $instance = new RecordVersion($auth_object);
     47        }
     48
     49        return $instance;
    4850    }
    4951
     
    5557    function RecordVersion($auth_object)
    5658    {
    57         if (!is_a($auth_object, 'Auth_SQL')) {
    58             trigger_error('Constructor not provided a valid Auth_SQL object.', E_USER_ERROR);
     59        $app =& App::getInstance();
     60
     61        if (!method_exists($auth_object, 'getVal') || !method_exists($auth_object, 'getUsername')) {
     62            trigger_error('Constructor not provided a valid Auth_* object.', E_USER_ERROR);
    5963        }
    6064
     
    6266
    6367        // Get create tables config from global context.
    64         if (!is_null(App::getParam('db_create_tables'))) {
    65             $this->setParam(array('create_table' => App::getParam('db_create_tables')));
     68        if (!is_null($app->getParam('db_create_tables'))) {
     69            $this->setParam(array('create_table' => $app->getParam('db_create_tables')));
    6670        }
    6771    }
     
    7680    function initDB($recreate_db=false)
    7781    {
     82        $app =& App::getInstance();
     83        $db =& DB::getInstance();
     84
    7885        static $_db_tested = false;
    7986
    8087        if ($recreate_db || !$_db_tested && $this->getParam('create_table')) {
    8188            if ($recreate_db) {
    82                 DB::query("DROP TABLE IF EXISTS " . $this->getParam('db_table'));
    83                 App::logMsg(sprintf('Dropping and recreating table %s.', $this->getParam('db_table')), LOG_DEBUG, __FILE__, __LINE__);
     89                $db->query("DROP TABLE IF EXISTS " . $this->getParam('db_table'));
     90                $app->logMsg(sprintf('Dropping and recreating table %s.', $this->getParam('db_table')), LOG_DEBUG, __FILE__, __LINE__);
    8491            }
    85             DB::query("CREATE TABLE IF NOT EXISTS " . $this->getParam('db_table') . " (
     92            $db->query("CREATE TABLE IF NOT EXISTS " . $this->getParam('db_table') . " (
    8693                version_id int NOT NULL auto_increment,
    8794                record_table varchar(255) NOT NULL default '',
     
    99106            )");
    100107
    101             if (!DB::columnExists($this->getParam('db_table'), array(
     108            if (!$db->columnExists($this->getParam('db_table'), array(
    102109                'version_id',
    103110                'record_table',
     
    110117                'version_datetime',
    111118            ), false, false)) {
    112                 App::logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), LOG_ALERT, __FILE__, __LINE__);
     119                $app->logMsg(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), LOG_ALERT, __FILE__, __LINE__);
    113120                trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), E_USER_ERROR);
    114121            }
     
    131138
    132139    /**
    133      * Return the value of a param setting.
    134      *
    135      * @access  public
    136      * @param   string  $params Which param to return.
    137      * @return  mixed   Configured param value.
     140     * Return the value of a parameter, if it exists.
     141     *
     142     * @access public
     143     * @param string $param        Which parameter to return.
     144     * @return mixed               Configured parameter value.
    138145     */
    139146    function getParam($param)
    140147    {
     148        $app =& App::getInstance();
     149   
    141150        if (isset($this->_params[$param])) {
    142151            return $this->_params[$param];
    143152        } else {
    144             App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
     153            $app->logMsg(sprintf('Parameter is not set: %s', $param), LOG_NOTICE, __FILE__, __LINE__);
    145154            return null;
    146155        }
     
    159168    function create($record_table, $record_key, $record_val, $title='', $notes='')
    160169    {
     170        $app =& App::getInstance();
     171        $db =& DB::getInstance();
     172
    161173        $this->initDB();
    162174
    163175        // Get current record.
    164176        if (!$record = $this->getCurrent($record_table, $record_key, $record_val)) {
    165             App::logMsg(sprintf('Could not create %s version, record not found: %s, %s, %s.', $title, $record_table, $record_key, $record_val), LOG_ERR, __FILE__, __LINE__);
     177            $app->logMsg(sprintf('Could not create %s version, record not found: %s, %s, %s.', $title, $record_table, $record_key, $record_val), LOG_ERR, __FILE__, __LINE__);
    166178            return false;
    167179        }
     
    171183
    172184        // Save as new version.
    173         DB::query("
     185        $db->query("
    174186            INSERT INTO " . $this->getParam('db_table') . " (
    175187                record_table,
     
    182194                version_datetime
    183195            ) VALUES (
    184                 '" . DB::escapeString($record_table) . "',
    185                 '" . DB::escapeString($record_key) . "',
    186                 '" . DB::escapeString($record_val) . "',
    187                 '" . DB::escapeString(gzcompress(serialize($record), 9)) . "',
    188                 '" . DB::escapeString($title) . "',
    189                 '" . DB::escapeString($notes) . "',
    190                 '" . DB::escapeString($this->_auth->getVal('user_id')) . "',
     196                '" . $db->escapeString($record_table) . "',
     197                '" . $db->escapeString($record_key) . "',
     198                '" . $db->escapeString($record_val) . "',
     199                '" . $db->escapeString(gzcompress(serialize($record), 9)) . "',
     200                '" . $db->escapeString($title) . "',
     201                '" . $db->escapeString($notes) . "',
     202                '" . $db->escapeString($this->_auth->getVal('user_id')) . "',
    191203                NOW()
    192204            )
    193205        ");
    194206
    195         return mysql_insert_id(DB::getDBH());
     207        return mysql_insert_id($db->getDBH());
    196208    }
    197209
     
    205217    function restore($version_id)
    206218    {
     219        $app =& App::getInstance();
     220        $db =& DB::getInstance();
     221
    207222        $this->initDB();
    208223
    209224        // Get version data.
    210         $qid = DB::query("
     225        $qid = $db->query("
    211226            SELECT * FROM " . $this->getParam('db_table') . "
    212             WHERE version_id = '" . DB::escapeString($version_id) . "'
     227            WHERE version_id = '" . $db->escapeString($version_id) . "'
    213228        ");
    214229        if (!$record = mysql_fetch_assoc($qid)) {
    215             App::raiseMsg(sprintf(_("Version ID %s%s not found."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')')), MSG_WARNING, __FILE__, __LINE__);
    216             App::logMsg(sprintf(_("Version ID %s%s not found."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')')), LOG_WARNING, __FILE__, __LINE__);
     230            $app->raiseMsg(sprintf(_("Version ID %s%s not found."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')')), MSG_WARNING, __FILE__, __LINE__);
     231            $app->logMsg(sprintf(_("Version ID %s%s not found."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')')), LOG_WARNING, __FILE__, __LINE__);
    217232            return false;
    218233        }
     
    220235
    221236        // Ensure saved db columns match current table schema.
    222         if (!DB::columnExists($record['record_table'], array_keys($data), $this->getParam('db_schema_strict'))) {
    223             App::raiseMsg(sprintf(_("Version ID %s%s is not compatible with the current database table."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')')), MSG_ERR, __FILE__, __LINE__);
    224             App::logMsg(sprintf(_("Version ID %s%s restoration failed, DB schema does not match for table %s."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')'), $record['record_table']), LOG_ALERT, __FILE__, __LINE__);
     237        if (!$db->columnExists($record['record_table'], array_keys($data), $this->getParam('db_schema_strict'))) {
     238            $app->raiseMsg(sprintf(_("Version ID %s%s is not compatible with the current database table."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')')), MSG_ERR, __FILE__, __LINE__);
     239            $app->logMsg(sprintf(_("Version ID %s%s restoration failed, DB schema does not match for table %s."), $version_id, (empty($record['version_title']) ? '' : ' (' . $record['version_title'] . ')'), $record['record_table']), LOG_ALERT, __FILE__, __LINE__);
    225240            return false;
    226241        }
    227242
    228243        // SQLize the keys of the specified versioned record.
    229         $replace_keys = join(",\n", array_map(array('DB', 'escapeString'), array_keys($data)));
     244        $replace_keys = join(",\n", array_map(array($db, 'escapeString'), array_keys($data)));
    230245
    231246        // SQLize the keys of the values of the specified versioned record. (These are more complex because we need to account for SQL null values.)
     
    233248        $comma = '';
    234249        foreach ($data as $v) {
    235             $replace_values .= is_null($v) ? "$comma\nNULL" : "$comma\n'" . DB::escapeString($v) . "'";
     250            $replace_values .= is_null($v) ? "$comma\nNULL" : "$comma\n'" . $db->escapeString($v) . "'";
    236251            $comma = ',';
    237252        }
    238253
    239254        // Replace current record with specified versioned record.
    240         DB::query("
     255        $db->query("
    241256            REPLACE INTO " . $record['record_table'] . " (
    242257                $replace_keys
     
    263278    function deleteOld($record_table, $record_key, $record_val)
    264279    {
     280        $db =& DB::getInstance();
     281   
    265282        $this->initDB();
    266283
    267284        // Get total number of versions for this record.
    268         $qid = DB::query("
     285        $qid = $db->query("
    269286            SELECT COUNT(*) FROM " . $this->getParam('db_table') . "
    270             WHERE record_table = '" . DB::escapeString($record_table) . "'
    271             AND record_key = '" . DB::escapeString($record_key) . "'
    272             AND record_val = '" . DB::escapeString($record_val) . "'
     287            WHERE record_table = '" . $db->escapeString($record_table) . "'
     288            AND record_key = '" . $db->escapeString($record_key) . "'
     289            AND record_val = '" . $db->escapeString($record_val) . "'
    273290        ");
    274291        list($v_count) = mysql_fetch_row($qid);
     
    278295                // To prevent a record bomb, limit max number of versions to max_qty.
    279296                // First query for oldest records, selecting enough to bring total number down to min_qty.
    280                 $qid = DB::query("
     297                $qid = $db->query("
    281298                    SELECT version_id FROM " . $this->getParam('db_table') . "
    282                     WHERE record_table = '" . DB::escapeString($record_table) . "'
    283                     AND record_key = '" . DB::escapeString($record_key) . "'
    284                     AND record_val = '" . DB::escapeString($record_val) . "'
     299                    WHERE record_table = '" . $db->escapeString($record_table) . "'
     300                    AND record_key = '" . $db->escapeString($record_key) . "'
     301                    AND record_val = '" . $db->escapeString($record_val) . "'
    285302                    ORDER BY version_datetime ASC
    286303                    LIMIT " . ($v_count - $this->getParam('min_qty')) . "
     
    289306                    $old_versions[] = $old_id;
    290307                }
    291                 DB::query("
     308                $db->query("
    292309                    DELETE FROM " . $this->getParam('db_table') . "
    293310                    WHERE version_id IN ('" . join("','", $old_versions) . "')
     
    295312            } else {
    296313                // Delete versions older than min_days, while still keeping min_qty.
    297                 $qid = DB::query("
     314                $qid = $db->query("
    298315                    SELECT version_id FROM " . $this->getParam('db_table') . "
    299                     WHERE record_table = '" . DB::escapeString($record_table) . "'
    300                     AND record_key = '" . DB::escapeString($record_key) . "'
    301                     AND record_val = '" . DB::escapeString($record_val) . "'
     316                    WHERE record_table = '" . $db->escapeString($record_table) . "'
     317                    AND record_key = '" . $db->escapeString($record_key) . "'
     318                    AND record_val = '" . $db->escapeString($record_val) . "'
    302319                    AND DATE_ADD(version_datetime, INTERVAL '" . $this->getParam('min_days') . "' DAY) < NOW()
    303320                    ORDER BY version_datetime ASC
     
    308325                }
    309326                if (sizeof($old_versions) > 0) {
    310                     DB::query("
     327                    $db->query("
    311328                        DELETE FROM " . $this->getParam('db_table') . "
    312329                        WHERE version_id IN ('" . join("','", $old_versions) . "')
     
    328345    function getList($record_table, $record_key, $record_val)
    329346    {
     347        $db =& DB::getInstance();
     348   
    330349        $this->initDB();
    331350
    332351        // Get versions of this record.
    333         $qid = DB::query("
     352        $qid = $db->query("
    334353            SELECT version_id, saved_by_admin_id, version_datetime, version_title
    335354            FROM " . $this->getParam('db_table') . "
    336             WHERE record_table = '" . DB::escapeString($record_table) . "'
    337             AND record_key = '" . DB::escapeString($record_key) . "'
    338             AND record_val = '" . DB::escapeString($record_val) . "'
     355            WHERE record_table = '" . $db->escapeString($record_table) . "'
     356            AND record_key = '" . $db->escapeString($record_key) . "'
     357            AND record_val = '" . $db->escapeString($record_val) . "'
    339358            ORDER BY version_datetime DESC
    340359        ");
     
    357376    function getVerson($version_id)
    358377    {
     378        $db =& DB::getInstance();
     379   
    359380        $this->initDB();
    360381
    361382        // Get version data.
    362         $qid = DB::query("
     383        $qid = $db->query("
    363384            SELECT * FROM " . $this->getParam('db_table') . "
    364             WHERE version_id = '" . DB::escapeString($version_id) . "'
     385            WHERE version_id = '" . $db->escapeString($version_id) . "'
    365386        ");
    366387        return mysql_fetch_assoc($qid);
     
    376397    function getData($version_id)
    377398    {
     399        $db =& DB::getInstance();
     400   
    378401        $this->initDB();
    379402
    380403        // Get version data.
    381         $qid = DB::query("
     404        $qid = $db->query("
    382405            SELECT * FROM " . $this->getParam('db_table') . "
    383             WHERE version_id = '" . DB::escapeString($version_id) . "'
     406            WHERE version_id = '" . $db->escapeString($version_id) . "'
    384407        ");
    385408        $record = mysql_fetch_assoc($qid);
     
    400423    function getCurrent($record_table, $record_key, $record_val)
    401424    {
    402         $this->initDB();
    403 
    404         $qid = DB::query("
    405             SELECT * FROM " . DB::escapeString($record_table) . "
    406             WHERE " . DB::escapeString($record_key) . " = '" . DB::escapeString($record_val) . "'
     425        $db =& DB::getInstance();
     426   
     427        $this->initDB();
     428
     429        $qid = $db->query("
     430            SELECT * FROM " . $db->escapeString($record_table) . "
     431            WHERE " . $db->escapeString($record_key) . " = '" . $db->escapeString($record_val) . "'
    407432        ");
    408433        if ($record = mysql_fetch_assoc($qid)) {
Note: See TracChangeset for help on using the changeset viewer.