Changeset 136 for trunk/lib/RecordVersion.inc.php
- Timestamp:
- Jun 3, 2006 7:47:48 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lib/RecordVersion.inc.php
r111 r136 1 1 <?php 2 2 /** 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 4 7 * restoring versions of a record of any DB table. All the data in the record is 5 8 * serialized, compressed, and saved in a blob in the version_tbl. Restoring a … … 14 17 * @version 2.1 15 18 */ 16 17 19 class RecordVersion { 18 20 … … 33 35 * This method enforces the singleton pattern for this class. 34 36 * 35 * @return object Reference to the global Record Versionobject.37 * @return object Reference to the global RecordLock object. 36 38 * @access public 37 39 * @static … … 39 41 function &getInstance($auth_object) 40 42 { 41 static $instance s = array();42 43 if ( !isset($instances[$auth_object->getVal('auth_name')])) {44 $instance s[$auth_object->getVal('auth_name')]= new RecordVersion($auth_object);45 } 46 47 return $instance s[$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; 48 50 } 49 51 … … 55 57 function RecordVersion($auth_object) 56 58 { 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); 59 63 } 60 64 … … 62 66 63 67 // 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'))); 66 70 } 67 71 } … … 76 80 function initDB($recreate_db=false) 77 81 { 82 $app =& App::getInstance(); 83 $db =& DB::getInstance(); 84 78 85 static $_db_tested = false; 79 86 80 87 if ($recreate_db || !$_db_tested && $this->getParam('create_table')) { 81 88 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__); 84 91 } 85 DB::query("CREATE TABLE IF NOT EXISTS " . $this->getParam('db_table') . " (92 $db->query("CREATE TABLE IF NOT EXISTS " . $this->getParam('db_table') . " ( 86 93 version_id int NOT NULL auto_increment, 87 94 record_table varchar(255) NOT NULL default '', … … 99 106 )"); 100 107 101 if (! DB::columnExists($this->getParam('db_table'), array(108 if (!$db->columnExists($this->getParam('db_table'), array( 102 109 'version_id', 103 110 'record_table', … … 110 117 'version_datetime', 111 118 ), 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__); 113 120 trigger_error(sprintf('Database table %s has invalid columns. Please update this table manually.', $this->getParam('db_table')), E_USER_ERROR); 114 121 } … … 131 138 132 139 /** 133 * Return the value of a param setting.134 * 135 * @access 136 * @param string $params Which paramto return.137 * @return mixed Configured paramvalue.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. 138 145 */ 139 146 function getParam($param) 140 147 { 148 $app =& App::getInstance(); 149 141 150 if (isset($this->_params[$param])) { 142 151 return $this->_params[$param]; 143 152 } 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__); 145 154 return null; 146 155 } … … 159 168 function create($record_table, $record_key, $record_val, $title='', $notes='') 160 169 { 170 $app =& App::getInstance(); 171 $db =& DB::getInstance(); 172 161 173 $this->initDB(); 162 174 163 175 // Get current record. 164 176 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__); 166 178 return false; 167 179 } … … 171 183 172 184 // Save as new version. 173 DB::query("185 $db->query(" 174 186 INSERT INTO " . $this->getParam('db_table') . " ( 175 187 record_table, … … 182 194 version_datetime 183 195 ) 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')) . "', 191 203 NOW() 192 204 ) 193 205 "); 194 206 195 return mysql_insert_id( DB::getDBH());207 return mysql_insert_id($db->getDBH()); 196 208 } 197 209 … … 205 217 function restore($version_id) 206 218 { 219 $app =& App::getInstance(); 220 $db =& DB::getInstance(); 221 207 222 $this->initDB(); 208 223 209 224 // Get version data. 210 $qid = DB::query("225 $qid = $db->query(" 211 226 SELECT * FROM " . $this->getParam('db_table') . " 212 WHERE version_id = '" . DB::escapeString($version_id) . "'227 WHERE version_id = '" . $db->escapeString($version_id) . "' 213 228 "); 214 229 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__); 217 232 return false; 218 233 } … … 220 235 221 236 // 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__); 225 240 return false; 226 241 } 227 242 228 243 // 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))); 230 245 231 246 // SQLize the keys of the values of the specified versioned record. (These are more complex because we need to account for SQL null values.) … … 233 248 $comma = ''; 234 249 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) . "'"; 236 251 $comma = ','; 237 252 } 238 253 239 254 // Replace current record with specified versioned record. 240 DB::query("255 $db->query(" 241 256 REPLACE INTO " . $record['record_table'] . " ( 242 257 $replace_keys … … 263 278 function deleteOld($record_table, $record_key, $record_val) 264 279 { 280 $db =& DB::getInstance(); 281 265 282 $this->initDB(); 266 283 267 284 // Get total number of versions for this record. 268 $qid = DB::query("285 $qid = $db->query(" 269 286 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) . "' 273 290 "); 274 291 list($v_count) = mysql_fetch_row($qid); … … 278 295 // To prevent a record bomb, limit max number of versions to max_qty. 279 296 // First query for oldest records, selecting enough to bring total number down to min_qty. 280 $qid = DB::query("297 $qid = $db->query(" 281 298 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) . "' 285 302 ORDER BY version_datetime ASC 286 303 LIMIT " . ($v_count - $this->getParam('min_qty')) . " … … 289 306 $old_versions[] = $old_id; 290 307 } 291 DB::query("308 $db->query(" 292 309 DELETE FROM " . $this->getParam('db_table') . " 293 310 WHERE version_id IN ('" . join("','", $old_versions) . "') … … 295 312 } else { 296 313 // Delete versions older than min_days, while still keeping min_qty. 297 $qid = DB::query("314 $qid = $db->query(" 298 315 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) . "' 302 319 AND DATE_ADD(version_datetime, INTERVAL '" . $this->getParam('min_days') . "' DAY) < NOW() 303 320 ORDER BY version_datetime ASC … … 308 325 } 309 326 if (sizeof($old_versions) > 0) { 310 DB::query("327 $db->query(" 311 328 DELETE FROM " . $this->getParam('db_table') . " 312 329 WHERE version_id IN ('" . join("','", $old_versions) . "') … … 328 345 function getList($record_table, $record_key, $record_val) 329 346 { 347 $db =& DB::getInstance(); 348 330 349 $this->initDB(); 331 350 332 351 // Get versions of this record. 333 $qid = DB::query("352 $qid = $db->query(" 334 353 SELECT version_id, saved_by_admin_id, version_datetime, version_title 335 354 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) . "' 339 358 ORDER BY version_datetime DESC 340 359 "); … … 357 376 function getVerson($version_id) 358 377 { 378 $db =& DB::getInstance(); 379 359 380 $this->initDB(); 360 381 361 382 // Get version data. 362 $qid = DB::query("383 $qid = $db->query(" 363 384 SELECT * FROM " . $this->getParam('db_table') . " 364 WHERE version_id = '" . DB::escapeString($version_id) . "'385 WHERE version_id = '" . $db->escapeString($version_id) . "' 365 386 "); 366 387 return mysql_fetch_assoc($qid); … … 376 397 function getData($version_id) 377 398 { 399 $db =& DB::getInstance(); 400 378 401 $this->initDB(); 379 402 380 403 // Get version data. 381 $qid = DB::query("404 $qid = $db->query(" 382 405 SELECT * FROM " . $this->getParam('db_table') . " 383 WHERE version_id = '" . DB::escapeString($version_id) . "'406 WHERE version_id = '" . $db->escapeString($version_id) . "' 384 407 "); 385 408 $record = mysql_fetch_assoc($qid); … … 400 423 function getCurrent($record_table, $record_key, $record_val) 401 424 { 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) . "' 407 432 "); 408 433 if ($record = mysql_fetch_assoc($qid)) {
Note: See TracChangeset
for help on using the changeset viewer.