1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482:
<?php
defined("ICMS_ROOT_PATH") or die("ImpressCMS root path not defined");
global $icmsConfigPersona;
icms_loadLanguageFile('core', 'databaseupdater');
class icms_db_legacy_updater_Handler {
var $_dbTypesArray;
var $_db;
var $db;
var $_messages = array();
function __construct() {
$this->_db = icms::$xoopsDB;
$this->db = icms::$xoopsDB;
$this->_dbTypesArray[XOBJ_DTYPE_TXTBOX] = 'varchar(255)';
$this->_dbTypesArray[XOBJ_DTYPE_TXTAREA] = 'text';
$this->_dbTypesArray[XOBJ_DTYPE_INT] = 'int(11)';
$this->_dbTypesArray[XOBJ_DTYPE_URL] = 'varchar(255)';
$this->_dbTypesArray[XOBJ_DTYPE_EMAIL] = 'varchar(255)';
$this->_dbTypesArray[XOBJ_DTYPE_ARRAY] = 'text';
$this->_dbTypesArray[XOBJ_DTYPE_OTHER] = 'text';
$this->_dbTypesArray[XOBJ_DTYPE_SOURCE] = 'text';
$this->_dbTypesArray[XOBJ_DTYPE_STIME] = 'int(11)';
$this->_dbTypesArray[XOBJ_DTYPE_MTIME] = 'int(11)';
$this->_dbTypesArray[XOBJ_DTYPE_LTIME] = 'int(11)';
$this->_dbTypesArray[XOBJ_DTYPE_SIMPLE_ARRAY] = 'text';
$this->_dbTypesArray[XOBJ_DTYPE_CURRENCY] = 'text';
$this->_dbTypesArray[XOBJ_DTYPE_FLOAT] = 'float';
$this->_dbTypesArray[XOBJ_DTYPE_TIME_ONLY] = 'int(11)';
$this->_dbTypesArray[XOBJ_DTYPE_URLLINK] = 'int(11)';
$this->_dbTypesArray[XOBJ_DTYPE_FILE] = 'int(11)';
$this->_dbTypesArray[XOBJ_DTYPE_IMAGE] = 'varchar(255)';
}
function runQuery($query, $goodmsg, $badmsg, $force=false) {
if ($force) {
$ret = $this->_db->queryF($query);
} else {
$ret = $this->_db->query($query);
}
if (!$ret) {
$this->_messages[] = " $badmsg";
return false;
} else {
$this->_messages[] = " $goodmsg";
return true;
}
}
function renameTable($from, $to, $force=false) {
$from = $this->_db->prefix($from);
$to = $this->_db->prefix($to);
$query = sprintf("ALTER TABLE %s RENAME %s", $from, $to);
if ($force) {
$ret = $this->_db->queryF($query);
} else {
$ret = $this->_db->query($query);
}
if (!$ret) {
$this->_messages[] = " " . sprintf(_DATABASEUPDATER_MSG_RENAME_TABLE_ERR, $from);
return false;
} else {
$this->_messages[] = " " . sprintf(_DATABASEUPDATER_MSG_RENAME_TABLE, $from, $to);
return true;
}
}
function updateTable($table, $force=false) {
$ret = true;
$table->force = $force;
if ($table->getStructure()) {
$ret = $table->createTable() && $ret;
}
if ($table->_flagForDrop) {
$ret = $table->dropTable() && $ret;
}
if ($table->getData()) {
$ret = $table->addData() && $ret;
}
if ($table->getNewFields()) {
$ret = $table->addNewFields() && $ret;
}
if ($table->getAlteredFields()) {
$ret = $table->alterTable() && $ret;
}
if ($table->getDropedFields()) {
$ret = $table->dropFields() && $ret;
}
if ($table->getUpdateAll()) {
$ret = $table->updateAll() && $ret;
}
return $ret;
}
function automaticUpgrade($dirname, $item) {
if (is_array($item)) {
foreach($item as $v) {
$this->upgradeObjectItem($dirname, $v);
}
} else {
$this->upgradeObjectItem($dirname, $item);
}
}
function getFieldTypeFromVar($var) {
$ret = isset($this->_dbTypesArray[$var['data_type']]) ? $this->_dbTypesArray[$var['data_type']] : 'text';
return $ret;
}
function getFieldDefaultFromVar($var, $key = false) {
if (in_array($var['data_type'], array(
XOBJ_DTYPE_TXTAREA,
XOBJ_DTYPE_SOURCE,
XOBJ_DTYPE_OTHER,
XOBJ_DTYPE_SIMPLE_ARRAY,
XOBJ_DTYPE_CURRENCY,
XOBJ_DTYPE_ARRAY,
))) {
return 'nodefault';
} elseif ($var['value']) {
return $var['value'];
} else {
if (in_array($var['data_type'], array(
XOBJ_DTYPE_INT,
XOBJ_DTYPE_STIME,
XOBJ_DTYPE_MTIME,
XOBJ_DTYPE_LTIME,
XOBJ_DTYPE_TIME_ONLY,
XOBJ_DTYPE_URLLINK,
XOBJ_DTYPE_FILE,
XOBJ_DTYPE_FLOAT,
))) {
return '0';
} else {
return '';
}
}
}
function upgradeObjectItem($dirname, $item) {
$module_handler = icms_getModuleHandler($item, $dirname);
if (!$module_handler) {
return false;
}
$table = new icms_db_legacy_updater_Table($dirname . '_' . $item);
$object = $module_handler->create();
$objectVars = $object->getVars();
if (!$table->exists()) {
$structure = "";
foreach($objectVars as $key=>$var) {
if ($var['persistent']) {
$type = $this->getFieldTypeFromVar($var);
if ($key == $module_handler->keyName) {
$extra = "auto_increment";
} else {
$default = $this->getFieldDefaultFromVar($var);
if ($default != 'nodefault') {
$extra = "default '$default'";
} else {
$extra = false;
}
}
if ($extra) {
$structure .= "`$key` $type not null $extra,";
} else {
$structure .= "`$key` $type not null,";
}
}
}
$ModKeyNames = $module_handler->keyName;
$structure .= "PRIMARY KEY (";
if (is_array($ModKeyNames)){
$structure .= "`".$ModKeyNames[0]."`";
foreach( $ModKeyNames as $ModKeyName){
$structure .= ($ModKeyName != $ModKeyNames[0])?", `".$ModKeyName."`":"";
}
}else{
$structure .= "`".$ModKeyNames."`";
}
$structure .= ")";
$table->setStructure($structure);
if (!$this->updateTable($table)) {
}
foreach ($table->_messages as $msg) {
$this->_messages[] = $msg;
}
} else {
$existingFieldsArray = $table->getExistingFieldsArray();
foreach($objectVars as $key=>$var) {
if ($var['persistent']) {
if (!isset($existingFieldsArray[$key])) {
$type = $this->getFieldTypeFromVar($var);
$default = $this->getFieldDefaultFromVar($var);
if ($default != 'nodefault') {
$extra = "default '$default'";
} else {
$extra = false;
}
$table->addNewField($key, "$type not null " . $extra);
} else {
$definition = strtolower($existingFieldsArray[$key]);
$type = $this->getFieldTypeFromVar($var);
if ($key == $module_handler->keyName) {
$extra = "auto_increment";
} else {
$default = $this->getFieldDefaultFromVar($var, $key);
if ($default != 'nodefault') {
$extra = "default '$default'";
} else {
$extra = false;
}
}
$actual_definition = "$type not null";
if ($extra) {
$actual_definition .= " $extra";
}
if ($definition != $actual_definition) {
$table->addAlteredField($key, $actual_definition);
}
}
}
}
foreach ($existingFieldsArray as $key=>$v) {
if (!isset($objectVars[$key]) || !$objectVars[$key]['persistent']) {
$table->addDropedField($key);
}
}
if (!$this->updateTable($table)) {
}
}
}
function insertConfig($conf_catid, $conf_name, $conf_title, $conf_value, $conf_desc, $conf_formtype, $conf_valuetype, $conf_order) {
global $dbVersion;
$configitem_handler = icms::handler('icms_config_item');
$configitemObj = $configitem_handler->create();
$configitemObj->setVar('conf_modid', 0);
$configitemObj->setVar('conf_catid', $conf_catid);
$configitemObj->setVar('conf_name', $conf_name);
$configitemObj->setVar('conf_title', $conf_title);
$configitemObj->setVar('conf_value', $conf_value);
$configitemObj->setVar('conf_desc', $conf_desc);
$configitemObj->setVar('conf_formtype', $conf_formtype);
$configitemObj->setVar('conf_valuetype', $conf_valuetype);
$configitemObj->setVar('conf_order', $conf_order);
if (!$configitem_handler->insert($configitemObj)) {
$querry_answer = sprintf(_DATABASEUPDATER_MSG_CONFIG_ERR, $conf_title);
} else{
$querry_answer = sprintf(_DATABASEUPDATER_MSG_CONFIG_SCC, $conf_title);
}
$this->_messages[] = $querry_answer;
}
function moduleUpgrade(&$module, $tables_first=false) {
$dirname = $module->getVar('dirname');
$dbVersion = $module->getDbversion();
$newDbVersion = constant(strtoupper($dirname . '_db_version')) ? constant(strtoupper($dirname . '_db_version')) : 0;
$textcurrentversion = sprintf(_DATABASEUPDATER_CURRENTVER, $dbVersion);
$textlatestversion = sprintf(_DATABASEUPDATER_LATESTVER, $newDbVersion);
$this->_messages[] = $textcurrentversion;
$this->_messages[] = $textlatestversion;
if(!$tables_first){
if ($newDbVersion > $dbVersion) {
for($i=$dbVersion+1;$i<=$newDbVersion; $i++) {
$upgrade_function = $dirname . '_db_upgrade_' . $i;
if (function_exists($upgrade_function)) {
$upgrade_function();
}
}
}
}
$this->_messages[] = _DATABASEUPDATER_UPDATE_UPDATING_DATABASE;
$this->automaticUpgrade($dirname, $module->modinfo['object_items']);
if($tables_first){
if ($newDbVersion > $dbVersion) {
for($i=$dbVersion+1;$i<=$newDbVersion; $i++) {
$upgrade_function = $dirname . '_db_upgrade_' . $i;
if (function_exists($upgrade_function)) {
$upgrade_function();
}
}
}
}
$this->updateModuleDBVersion($newDbVersion, $dirname);
return true;
}
function updateModuleDBVersion($newDBVersion, $dirname) {
if (!$dirname) {
$dirname = icms_getCurrentModuleName();
}
$module_handler = icms::handler('icms_module');
$module = $module_handler->getByDirname($dirname);
$module->setVar('dbversion', $newDBVersion);
if (!$module_handler->insert($module)) {
$module->setErrors(_DATABASEUPDATER_MSG_DB_VERSION_ERR);
return false;
}
return true;
}
}
?>