Revision 2104
Added by darkviper almost 11 years ago
| WbDatabase.php | ||
|---|---|---|
| 33 | 33 |
|
| 34 | 34 |
/* -------------------------------------------------------- */ |
| 35 | 35 |
@define('DATABASE_CLASS_LOADED', true);
|
| 36 |
/* define the old mysql consts for Backward compatibility */ |
|
| 37 |
if (!defined('MYSQL_ASSOC'))
|
|
| 38 |
{
|
|
| 39 |
define('MYSQL_SEEK_LAST', -1);
|
|
| 40 |
define('MYSQL_SEEK_FIRST', 0);
|
|
| 41 |
define('MYSQL_ASSOC', 1);
|
|
| 42 |
define('MYSQL_NUM', 2);
|
|
| 43 |
define('MYSQL_BOTH', 3);
|
|
| 44 |
define('MYSQL_CLIENT_COMPRESS', 32);
|
|
| 45 |
define('MYSQL_CLIENT_IGNORE_SPACE', 256);
|
|
| 46 |
define('MYSQL_CLIENT_INTERACTIVE', 1024);
|
|
| 47 |
define('MYSQL_CLIENT_SSL', 2048);
|
|
| 48 |
} |
|
| 36 | 49 |
|
| 37 | 50 |
class WbDatabase {
|
| 38 | 51 |
|
| ... | ... | |
| 52 | 65 |
* __constructor |
| 53 | 66 |
* prevent from public instancing |
| 54 | 67 |
*/ |
| 55 |
private function __construct() {}
|
|
| 68 |
final private function __construct() {}
|
|
| 56 | 69 |
/** |
| 57 | 70 |
* prevent from cloning |
| 58 | 71 |
*/ |
| 59 |
private function __clone() {}
|
|
| 72 |
final private function __clone() {}
|
|
| 60 | 73 |
/** |
| 61 | 74 |
* get a valid instance of this class |
| 62 | 75 |
* @param string $sIdentifier selector for several different instances |
| 63 | 76 |
* @return WbDatabase object |
| 64 | 77 |
*/ |
| 65 |
public static function getInstance($sIdentifier = 'core') |
|
| 78 |
final public static function getInstance($sIdentifier = 'core')
|
|
| 66 | 79 |
{
|
| 67 | 80 |
if( !isset(self::$_oInstances[$sIdentifier])) {
|
| 68 | 81 |
$c = __CLASS__; |
| ... | ... | |
| 76 | 89 |
* disconnect and kills an existing instance |
| 77 | 90 |
* @param string $sIdentifier selector for instance to kill |
| 78 | 91 |
*/ |
| 79 |
public static function killInstance($sIdentifier) |
|
| 92 |
final public static function killInstance($sIdentifier)
|
|
| 80 | 93 |
{
|
| 81 | 94 |
if($sIdentifier != 'core') {
|
| 82 | 95 |
if( isset(self::$_oInstances[$sIdentifier])) {
|
| ... | ... | |
| 105 | 118 |
$username = isset($aIni['user']) ? $aIni['user'] : ''; |
| 106 | 119 |
$password = isset($aIni['pass']) ? $aIni['pass'] : ''; |
| 107 | 120 |
$hostport = isset($aIni['port']) ? $aIni['port'] : '3306'; |
| 108 |
$hostport = $hostport == '3306' ? '' : ':'.$hostport;
|
|
| 121 |
$hostport = $hostport == '3306' ? null : $hostport;
|
|
| 109 | 122 |
$db_name = ltrim(isset($aIni['path']) ? $aIni['path'] : '', '/\\'); |
| 110 | 123 |
$sTmp = isset($aIni['query']) ? $aIni['query'] : ''; |
| 111 | 124 |
$aQuery = explode('&', $sTmp);
|
| ... | ... | |
| 126 | 139 |
} else {
|
| 127 | 140 |
throw new WbDatabaseException('Missing parameter: unable to connect database');
|
| 128 | 141 |
} |
| 129 |
$this->oDbHandle = @mysql_connect($hostname.$hostport, $username, $password, true);
|
|
| 142 |
$this->oDbHandle = @mysqli_connect($hostname, $username, $password, $db_name, $hostport);
|
|
| 130 | 143 |
if (!$this->oDbHandle) {
|
| 131 |
throw new WbDatabaseException('unable to connect \''.$scheme.'://'.$hostname.$hostport.'\'');
|
|
| 144 |
throw new WbDatabaseException('unable to connect \''.$scheme.'://'.$hostname.':'.$hostport.'\'');
|
|
| 132 | 145 |
} else {
|
| 133 |
if (!@mysql_select_db($db_name, $this->oDbHandle)) {
|
|
| 134 |
throw new WbDatabaseException('unable to select database \''.$db_name.
|
|
| 135 |
'\' on \''.$scheme.'://'. |
|
| 136 |
$hostname.$hostport.'\'' |
|
| 137 |
); |
|
| 138 |
} else {
|
|
| 139 |
if ($this->sCharset) {
|
|
| 140 |
@mysql_query('SET NAMES \''.$this->sCharset.'\'', $this->oDbHandle);
|
|
| 141 |
} |
|
| 142 |
$this->connected = true; |
|
| 143 |
} |
|
| 146 |
if ($this->sCharset) {
|
|
| 147 |
@mysqli_query($this->oDbHandle, 'SET NAMES \''.$this->sCharset.'\''); |
|
| 148 |
} |
|
| 149 |
$this->connected = true; |
|
| 144 | 150 |
} |
| 145 | 151 |
return $this->connected; |
| 146 | 152 |
} |
| ... | ... | |
| 153 | 159 |
public function disconnect() |
| 154 | 160 |
{
|
| 155 | 161 |
if ($this->connected == true && $oInstance->sInstanceIdentifier != 'core') {
|
| 156 |
mysql_close($this->oDbHandle); |
|
| 162 |
mysqli_close($this->oDbHandle);
|
|
| 157 | 163 |
$this->connected = false; |
| 158 | 164 |
return true; |
| 159 | 165 |
} |
| ... | ... | |
| 161 | 167 |
} |
| 162 | 168 |
/** |
| 163 | 169 |
* Alias for doQuery() |
| 170 |
* @deprecated from WB-2.8.5 and higher |
|
| 164 | 171 |
*/ |
| 165 | 172 |
public function query($statement) |
| 166 | 173 |
{
|
| ... | ... | |
| 173 | 180 |
*/ |
| 174 | 181 |
public function doQuery($statement) {
|
| 175 | 182 |
$this->iQueryCount++; |
| 176 |
$mysql = new mysql(); |
|
| 177 |
$mysql->query($statement, $this->oDbHandle);
|
|
| 183 |
$mysql = new mysql($this->oDbHandle);
|
|
| 184 |
$mysql->query($statement); |
|
| 178 | 185 |
$this->set_error($mysql->error($this->oDbHandle)); |
| 179 |
if ($mysql->error($this->oDbHandle)) {
|
|
| 186 |
if ($mysql->error()) {
|
|
| 180 | 187 |
return null; |
| 181 | 188 |
} else {
|
| 182 | 189 |
return $mysql; |
| ... | ... | |
| 184 | 191 |
} |
| 185 | 192 |
/** |
| 186 | 193 |
* Alias for getOne() |
| 194 |
* @deprecated from WB-2.8.5 and higher |
|
| 187 | 195 |
*/ |
| 188 | 196 |
public function get_one( $statement ) |
| 189 | 197 |
{
|
| ... | ... | |
| 198 | 206 |
public function getOne( $statement ) |
| 199 | 207 |
{
|
| 200 | 208 |
$this->iQueryCount++; |
| 201 |
$fetch_row = mysql_fetch_array(mysql_query($statement, $this->oDbHandle));
|
|
| 209 |
$fetch_row = mysqli_fetch_array(mysqli_query($this->oDbHandle, $statement));
|
|
| 202 | 210 |
$result = $fetch_row[0]; |
| 203 |
$this->set_error(mysql_error($this->oDbHandle)); |
|
| 204 |
if (mysql_error($this->oDbHandle)) {
|
|
| 211 |
$this->set_error(null); |
|
| 212 |
if (mysqli_error($this->oDbHandle)) {
|
|
| 213 |
$this->set_error(mysqli_error($this->oDbHandle)); |
|
| 205 | 214 |
return null; |
| 206 | 215 |
} else {
|
| 207 | 216 |
return $result; |
| ... | ... | |
| 209 | 218 |
} |
| 210 | 219 |
/** |
| 211 | 220 |
* Alias for setError() |
| 221 |
* @deprecated from WB-2.8.5 and higher |
|
| 212 | 222 |
*/ |
| 213 | 223 |
public function set_error($message = null) |
| 214 | 224 |
{
|
| ... | ... | |
| 225 | 235 |
} |
| 226 | 236 |
/** |
| 227 | 237 |
* Alias for isError |
| 238 |
* @deprecated from WB-2.8.5 and higher |
|
| 228 | 239 |
*/ |
| 229 | 240 |
public function is_error() |
| 230 | 241 |
{
|
| ... | ... | |
| 240 | 251 |
} |
| 241 | 252 |
/** |
| 242 | 253 |
* Alias for getError |
| 254 |
* @deprecated from WB-2.8.5 and higher |
|
| 243 | 255 |
*/ |
| 244 | 256 |
public function get_error() |
| 245 | 257 |
{
|
| ... | ... | |
| 278 | 290 |
break; |
| 279 | 291 |
case 'LastInsertId': |
| 280 | 292 |
case 'getLastInsertId': // << set deprecated |
| 281 |
$retval = mysql_insert_id($this->oDbHandle);
|
|
| 293 |
$retval = $this->getLastInsertId();
|
|
| 282 | 294 |
break; |
| 283 | 295 |
case 'DbName': |
| 284 | 296 |
case 'getDbName': // << set deprecated |
| ... | ... | |
| 306 | 318 |
*/ |
| 307 | 319 |
public function escapeString($unescaped_string) |
| 308 | 320 |
{
|
| 309 |
return mysql_real_escape_string($unescaped_string, $this->oDbHandle);
|
|
| 321 |
return mysqli_real_escape_string($this->oDbHandle, $unescaped_string);
|
|
| 310 | 322 |
} |
| 311 | 323 |
/** |
| 312 | 324 |
* Last inserted Id |
| ... | ... | |
| 314 | 326 |
*/ |
| 315 | 327 |
public function getLastInsertId() |
| 316 | 328 |
{
|
| 317 |
return mysql_insert_id($this->oDbHandle); |
|
| 329 |
return mysqli_insert_id($this->oDbHandle);
|
|
| 318 | 330 |
} |
| 319 | 331 |
/** |
| 320 | 332 |
* Alias for isField() |
| 333 |
* @deprecated from WB-2.8.5 and higher |
|
| 321 | 334 |
*/ |
| 322 | 335 |
public function field_exists($table_name, $field_name) |
| 323 | 336 |
{
|
| ... | ... | |
| 331 | 344 |
public function isField($table_name, $field_name) |
| 332 | 345 |
{
|
| 333 | 346 |
$sql = 'DESCRIBE `'.$table_name.'` `'.$field_name.'` '; |
| 334 |
$query = $this->query($sql, $this->oDbHandle);
|
|
| 347 |
$query = $this->doQuery($sql);
|
|
| 335 | 348 |
return ($query->numRows() != 0); |
| 336 | 349 |
} |
| 337 | 350 |
/** |
| 338 | 351 |
* Alias for isIndex() |
| 352 |
* @deprecated from WB-2.8.5 and higher |
|
| 339 | 353 |
*/ |
| 340 | 354 |
public function index_exists($table_name, $index_name, $number_fields = 0) |
| 341 | 355 |
{
|
| ... | ... | |
| 352 | 366 |
$number_fields = intval($number_fields); |
| 353 | 367 |
$keys = 0; |
| 354 | 368 |
$sql = 'SHOW INDEX FROM `'.$table_name.'`'; |
| 355 |
if (($res_keys = $this->doQuery($sql, $this->oDbHandle))) {
|
|
| 369 |
if (($res_keys = $this->doQuery($sql))) {
|
|
| 356 | 370 |
while (($rec_key = $res_keys->fetchRow(MYSQL_ASSOC))) {
|
| 357 | 371 |
if ( $rec_key['Key_name'] == $index_name ) {
|
| 358 | 372 |
$keys++; |
| ... | ... | |
| 368 | 382 |
} |
| 369 | 383 |
/** |
| 370 | 384 |
* Alias for addField() |
| 385 |
* @deprecated from WB-2.8.5 and higher |
|
| 371 | 386 |
*/ |
| 372 | 387 |
public function field_add($table_name, $field_name, $description) |
| 373 | 388 |
{
|
| ... | ... | |
| 384 | 399 |
if (!$this->isField($table_name, $field_name)) {
|
| 385 | 400 |
// add new field into a table |
| 386 | 401 |
$sql = 'ALTER TABLE `'.$table_name.'` ADD '.$field_name.' '.$description.' '; |
| 387 |
$query = $this->doQuery($sql, $this->oDbHandle);
|
|
| 388 |
$this->set_error(mysql_error($this->oDbHandle)); |
|
| 402 |
$query = $this->doQuery($sql); |
|
| 403 |
$this->set_error(mysqli_error($this->oDbHandle));
|
|
| 389 | 404 |
if (!$this->isError()) {
|
| 390 | 405 |
return ( $this->isField($table_name, $field_name) ) ? true : false; |
| 391 | 406 |
} |
| ... | ... | |
| 396 | 411 |
} |
| 397 | 412 |
/** |
| 398 | 413 |
* Alias for modifyField() |
| 414 |
* @deprecated from WB-2.8.5 and higher |
|
| 399 | 415 |
*/ |
| 400 | 416 |
public function field_modify($table_name, $field_name, $description) |
| 401 | 417 |
{
|
| ... | ... | |
| 413 | 429 |
if ($this->isField($table_name, $field_name)) {
|
| 414 | 430 |
// modify a existing field in a table |
| 415 | 431 |
$sql = 'ALTER TABLE `'.$table_name.'` MODIFY `'.$field_name.'` '.$description; |
| 416 |
$retval = ( $this->doQuery($sql, $this->oDbHandle) ? true : false);
|
|
| 417 |
$this->setError(mysql_error());
|
|
| 432 |
$retval = ( $this->doQuery($sql) ? true : false); |
|
| 433 |
$this->setError(mysqli_error($this->oDbHandle));
|
|
| 418 | 434 |
} |
| 419 | 435 |
return $retval; |
| 420 | 436 |
} |
| 421 | 437 |
/** |
| 422 | 438 |
* Alias for removeField() |
| 439 |
* @deprecated from WB-2.8.5 and higher |
|
| 423 | 440 |
*/ |
| 424 | 441 |
public function field_remove($table_name, $field_name) |
| 425 | 442 |
{
|
| ... | ... | |
| 442 | 459 |
} |
| 443 | 460 |
/** |
| 444 | 461 |
* Alias for addIndex() |
| 462 |
* @deprecated from WB-2.8.5 and higher |
|
| 445 | 463 |
*/ |
| 446 | 464 |
public function index_add($table_name, $index_name, $field_list, $index_type = 'KEY') |
| 447 | 465 |
{
|
| ... | ... | |
| 466 | 484 |
{
|
| 467 | 485 |
$sql = 'ALTER TABLE `'.$table_name.'` '; |
| 468 | 486 |
$sql .= 'DROP INDEX `'.$index_name.'`'; |
| 469 |
if (!$this->doQuery($sql, $this->oDbHandle)) { return false; }
|
|
| 487 |
if (!$this->doQuery($sql)) { return false; }
|
|
| 470 | 488 |
} |
| 471 | 489 |
$sql = 'ALTER TABLE `'.$table_name.'` '; |
| 472 | 490 |
$sql .= 'ADD '.$index_type.' '; |
| 473 | 491 |
$sql .= $index_type == 'PRIMARY' ? 'KEY ' : '`'.$index_name.'` '; |
| 474 | 492 |
$sql .= '( '.$field_list.' ); '; |
| 475 |
if ($this->doQuery($sql, $this->oDbHandle)) { $retval = true; }
|
|
| 493 |
if ($this->doQuery($sql)) { $retval = true; }
|
|
| 476 | 494 |
return $retval; |
| 477 | 495 |
} |
| 478 | 496 |
/** |
| 479 | 497 |
* Alias for removeIndex() |
| 498 |
* @deprecated from WB-2.8.5 and higher |
|
| 480 | 499 |
*/ |
| 481 | 500 |
public function index_remove($table_name, $index_name) |
| 482 | 501 |
{
|
| ... | ... | |
| 493 | 512 |
if ($this->isIndex($table_name, $index_name)) {
|
| 494 | 513 |
// modify a existing field in a table |
| 495 | 514 |
$sql = 'ALTER TABLE `'.$table_name.'` DROP INDEX `'.$index_name.'`'; |
| 496 |
$retval = ( $this->doQuery($sql, $this->oDbHandle) ? true : false );
|
|
| 515 |
$retval = ( $this->doQuery($sql) ? true : false ); |
|
| 497 | 516 |
} |
| 498 | 517 |
return $retval; |
| 499 | 518 |
} |
| 500 | 519 |
/** |
| 501 | 520 |
* Alias for importSql() |
| 521 |
* @deprecated from WB-2.8.5 and higher |
|
| 502 | 522 |
*/ |
| 503 | 523 |
public function SqlImport($sSqlDump, |
| 504 | 524 |
$sTablePrefix = '', |
| 505 |
$bPreserve = true,
|
|
| 525 |
$sAction = 'install',
|
|
| 506 | 526 |
$sEngine = 'MyISAM', |
| 507 | 527 |
$sCollation = 'utf8_unicode_ci') |
| 508 | 528 |
{
|
| 509 | 529 |
return $this->importSql($sSqlDump, $sTablePrefix, $bPreserve, $sEngine, $sCollation); |
| 510 | 530 |
} |
| 511 | 531 |
/** |
| 512 |
* Import a standard *.sql dump file |
|
| 513 |
* @param string $sSqlDump link to the sql-dumpfile |
|
| 514 |
* @param string $sTablePrefix |
|
| 515 |
* @param bool $bPreserve set to true will ignore all DROP TABLE statements |
|
| 516 |
* @param string $sEngine can be 'MyISAM' or 'InnoDB' |
|
| 517 |
* @param string $sCollation one of the list of available collations |
|
| 518 |
* @return boolean true if import successful |
|
| 519 |
* @description Import a standard *.sql dump file<br /> |
|
| 520 |
* The file can include placeholders TABLE_PREFIX, TABLE_COLLATION and TABLE_ENGINE |
|
| 532 |
* Import an SQl-Dumpfile witch can include unlimited placeholders for values |
|
| 533 |
* @param mixed $mSqlDump can be string with filename or array with additional vars |
|
| 534 |
* @param string $sTablePrefix can be used to override settings from WbDatabase object |
|
| 535 |
* @param string $sAction 'install', 'uninstall', 'upgrade', 'repair |
|
| 536 |
* @param string $sEngine kind of table engine: MyIsam(default) |
|
| 537 |
* @param string $sCollation utf8_unicode_ci(default) |
|
| 538 |
* @return bool false on error |
|
| 521 | 539 |
*/ |
| 522 |
public function importSql($sSqlDump, |
|
| 523 |
$sTablePrefix = '', /* unused argument, for backward compatibility only! */ |
|
| 524 |
$bPreserve = true, |
|
| 525 |
$sEngine = 'MyISAM', |
|
| 526 |
$sCollation = 'utf8_unicode_ci') |
|
| 527 |
{
|
|
| 528 |
$sCollation = ($sCollation != '' ? $sCollation : 'utf8_unicode_ci'); |
|
| 529 |
$aCharset = preg_split('/_/', $sCollation, null, PREG_SPLIT_NO_EMPTY);
|
|
| 530 |
$sEngine = 'ENGINE='.$sEngine.' DEFAULT CHARSET='.$aCharset[0].' COLLATE='.$sCollation; |
|
| 531 |
$sCollation = ' collate '.$sCollation; |
|
| 540 |
public function importSql( |
|
| 541 |
$mSqlDump, |
|
| 542 |
$sTablePrefix = '', // can override settings from WbDatabase object |
|
| 543 |
$sAction = 'install', // skip 'DROP TABLE' statements |
|
| 544 |
$sEngine = 'MyISAM', // the default table engine |
|
| 545 |
$sCollation = 'utf8_unicode_ci' // the default collation to use |
|
| 546 |
) |
|
| 547 |
{
|
|
| 532 | 548 |
$retval = true; |
| 533 | 549 |
$this->error = ''; |
| 534 |
$aSearch = array('{TABLE_PREFIX}','{TABLE_ENGINE}', '{TABLE_COLLATION}');
|
|
| 535 |
$aReplace = array($this->sTablePrefix, $sEngine, $sCollation); |
|
| 550 |
// sanitize arguments |
|
| 551 |
if (! is_string($sAction)) {
|
|
| 552 |
$sAction = $sAction ? 'repair' : 'install'; |
|
| 553 |
} |
|
| 554 |
$aAllowedActions = array('install', 'uninstall', 'upgrade', 'repair');
|
|
| 555 |
$sAction = strtolower(preg_replace('/^.*?('.implode('|', $aAllowedActions).')(\.php)?$/iU', '$1', $sAction));
|
|
| 556 |
$sAction = (in_array($sAction, $aAllowedActions) ? $sAction : 'install'); |
|
| 557 |
$sTablePrefix = trim($sTablePrefix); |
|
| 558 |
$aEngineTypes = array( |
|
| 559 |
'csv' => 'CSV', |
|
| 560 |
'blackhole' => 'BLACKHOLE', |
|
| 561 |
'memory' => 'MEMORY', |
|
| 562 |
'myisam' => 'MyISAM', |
|
| 563 |
'innodb' => 'InnoDB', |
|
| 564 |
'archive' => 'ARCHIVE', |
|
| 565 |
'mrg_myisam' => 'MRG_MYISAM' |
|
| 566 |
); |
|
| 567 |
if (isset($aEngineTypes[strtolower($sEngine)])) {
|
|
| 568 |
$sEngine = $aEngineTypes[strtolower($sEngine)]; |
|
| 569 |
} else {
|
|
| 570 |
$sEngine = 'MyISAM'; |
|
| 571 |
} |
|
| 572 |
// test if selected collation is available. Otherwise select 'utf8_unicode_ci' |
|
| 573 |
$sql = 'SELECT COUNT(*) FROM `COLLATIONS` ' |
|
| 574 |
. 'WHERE `COLLATION_NAME`=\''.$sCollation.'\''; |
|
| 575 |
$sCollation = ($this->get_one($sql) ? $sCollation : 'utf8_unicode_ci'); |
|
| 576 |
$aTmp = preg_split('/_/', $sCollation, null, PREG_SPLIT_NO_EMPTY);
|
|
| 577 |
$sCharset = $aTmp[0]; |
|
| 578 |
// define array of searches |
|
| 579 |
$aSearch = array( |
|
| 580 |
'/\{TABLE_PREFIX\}/',
|
|
| 581 |
'/\{TABLE_COLLATION\}/', // deprecated from 2.8.4
|
|
| 582 |
'/\{FIELD_COLLATION\}/', // deprecated from 2.8.4
|
|
| 583 |
'/\{TABLE_ENGINE\}/',
|
|
| 584 |
'/\{TABLE_ENGINE=([a-zA-Z_0-9]*)\}/',
|
|
| 585 |
'/\{CHARSET\}/',
|
|
| 586 |
'/\{COLLATION\}/'
|
|
| 587 |
); |
|
| 588 |
// define array of replacements |
|
| 589 |
$aReplace = array( |
|
| 590 |
$sTablePrefix, |
|
| 591 |
' COLLATE {COLLATION}', // deprecated from 2.8.4
|
|
| 592 |
' COLLATE {COLLATION}', // deprecated from 2.8.4
|
|
| 593 |
' {ENGINE='.$sEngine.'}',
|
|
| 594 |
' ENGINE=$1 DEFAULT CHARSET={CHARSET} COLLATION={COLLATION}',
|
|
| 595 |
$sCharset, |
|
| 596 |
$sCollation |
|
| 597 |
); |
|
| 598 |
|
|
| 599 |
if (is_array($mSqlDump)) {
|
|
| 600 |
// try to get dumpfile name |
|
| 601 |
if (!isset($mSqlDump['sSqlDump'])) {
|
|
| 602 |
$this->error = 'missing index \'sSqlDump\' in $mSqlDump'; |
|
| 603 |
return false; |
|
| 604 |
} else {
|
|
| 605 |
// get dumpfile name from array and then remove entry |
|
| 606 |
$sDumpFile = (string)$mSqlDump['sSqlDump']; |
|
| 607 |
unset($mSqlDump['sSqlDump']); |
|
| 608 |
// import all vars and it's values from array |
|
| 609 |
foreach ($mSqlDump as $sIndex => $sValue) {
|
|
| 610 |
// transform varname into placeholder name ('sPageTitle' => 'PAGE_TITLE')
|
|
| 611 |
$sIndex = strtoupper(preg_replace('/([a-z0-9])([A-Z])/', '\1_\2', ltrim($sIndex, 'a..z')));
|
|
| 612 |
// fill search/replace arrays |
|
| 613 |
$aSearch[] = '/\{'.$sIndex.'\}/';
|
|
| 614 |
$aReplace[] = $sValue ; |
|
| 615 |
} |
|
| 616 |
} |
|
| 617 |
} elseif (is_string($mSqlDump)) {
|
|
| 618 |
$sDumpFile = (string)$mSqlDump; |
|
| 619 |
} else {
|
|
| 620 |
$this->error = 'invalid argument $mSqlDump'; |
|
| 621 |
return false; |
|
| 622 |
} |
|
| 623 |
if (!is_readable($sDumpFile)) {
|
|
| 624 |
$this->Error = 'unable to open \''.$sDumpFile.'\''; |
|
| 625 |
return false; |
|
| 626 |
} |
|
| 536 | 627 |
$sql = ''; |
| 537 |
$aSql = file($sSqlDump);
|
|
| 538 |
// $aSql[0] = preg_replace('/^\xEF\xBB\xBF/', '', $aSql[0]);
|
|
| 628 |
$aSql = file($sDumpFile, FILE_SKIP_EMPTY_LINES);
|
|
| 629 |
// remove possible ByteOrderMark
|
|
| 539 | 630 |
$aSql[0] = preg_replace('/^[\xAA-\xFF]{3}/', '', $aSql[0]);
|
| 540 | 631 |
while (sizeof($aSql) > 0) {
|
| 541 | 632 |
$sSqlLine = trim(array_shift($aSql)); |
| 542 | 633 |
if (!preg_match('/^[-\/]+.*/', $sSqlLine)) {
|
| 543 | 634 |
$sql = $sql.' '.$sSqlLine; |
| 544 | 635 |
if ((substr($sql,-1,1) == ';')) {
|
| 545 |
$sql = trim(str_replace( $aSearch, $aReplace, $sql)); |
|
| 546 |
if (!($bPreserve && preg_match('/^\s*DROP TABLE IF EXISTS/siU', $sql))) {
|
|
| 547 |
if (!mysql_query($sql, $this->oDbHandle)) {
|
|
| 548 |
$retval = false; |
|
| 549 |
$this->error = mysql_error($this->oDbHandle); |
|
| 550 |
unset($aSql); |
|
| 551 |
break; |
|
| 552 |
} |
|
| 553 |
} |
|
| 636 |
$sql = trim(preg_replace($aSearch, $aReplace, $sql)); |
|
| 637 |
$sAvailSqlObjects = 'TABLE|VIEW|INDEX|PROCEDURE|FUNCTION|TRIGGER|EVENT'; |
|
| 638 |
switch ($sAction) {
|
|
| 639 |
case 'uninstall': // skip CREATE; execute DROP |
|
| 640 |
if (preg_match('/^\s*CREATE ('.$sAvailSqlObjects.') /siU', $sql)) {
|
|
| 641 |
$sql = ''; |
|
| 642 |
continue; // read next statement |
|
| 643 |
} |
|
| 644 |
break; |
|
| 645 |
case 'upgrade': // skip DROP; execute CREATE |
|
| 646 |
case 'repair': // skip DROP; execute CREATE |
|
| 647 |
if (preg_match('/^\s*DROP ('.$sAvailSqlObjects.') /siU', $sql)) {
|
|
| 648 |
$sql = ''; |
|
| 649 |
continue; // read next statement |
|
| 650 |
} |
|
| 651 |
break; |
|
| 652 |
default: // install: execute DROP; execute CREATE |
|
| 653 |
break; |
|
| 654 |
} |
|
| 655 |
if (!$this->doQuery($sql)) {
|
|
| 656 |
$retval = false; |
|
| 657 |
$this->error = $this->getError(); |
|
| 658 |
unset($aSql); |
|
| 659 |
break; |
|
| 660 |
} |
|
| 554 | 661 |
$sql = ''; |
| 555 | 662 |
} |
| 556 | 663 |
} |
| 557 | 664 |
} |
| 558 | 665 |
return $retval; |
| 559 |
}
|
|
| 666 |
} // end of function importSql()
|
|
| 560 | 667 |
/** |
| 561 | 668 |
* retuns the type of the engine used for requested table |
| 562 | 669 |
* @param string $table name of the table, including prefix |
| ... | ... | |
| 565 | 672 |
public function getTableEngine($table) |
| 566 | 673 |
{
|
| 567 | 674 |
$retVal = false; |
| 568 |
$mysqlVersion = mysql_get_server_info($this->oDbHandle); |
|
| 675 |
$mysqlVersion = mysqli_get_server_info($this->oDbHandle);
|
|
| 569 | 676 |
$engineValue = (version_compare($mysqlVersion, '5.0') < 0) ? 'Type' : 'Engine'; |
| 570 | 677 |
$sql = 'SHOW TABLE STATUS FROM `' . $this->sDbName . '` LIKE \'' . $table . '\''; |
| 571 |
if (($result = $this->doQuery($sql, $this->oDbHandle))) {
|
|
| 678 |
if (($result = $this->doQuery($sql))) {
|
|
| 572 | 679 |
if (($row = $result->fetchRow(MYSQL_ASSOC))) {
|
| 573 | 680 |
$retVal = $row[$engineValue]; |
| 574 | 681 |
} |
| ... | ... | |
| 614 | 721 |
*/ |
| 615 | 722 |
class mysql {
|
| 616 | 723 |
|
| 617 |
private $result = null; |
|
| 724 |
private $result = null;
|
|
| 618 | 725 |
private $oDbHandle = null; |
| 726 |
private $error = ''; |
|
| 619 | 727 |
|
| 728 |
public function __construct($oHandle) |
|
| 729 |
{
|
|
| 730 |
$this->oDbHandle = $oHandle; |
|
| 731 |
} |
|
| 620 | 732 |
/** |
| 621 | 733 |
* query sql statement |
| 622 | 734 |
* @param string $statement |
| 623 |
* @param object $dbHandle |
|
| 624 | 735 |
* @return object |
| 625 | 736 |
* @throws WbDatabaseException |
| 626 | 737 |
*/ |
| 627 |
function query($statement, $dbHandle)
|
|
| 738 |
function query($sStatement)
|
|
| 628 | 739 |
{
|
| 629 |
$this->oDbHandle = $dbHandle; |
|
| 630 |
$this->result = @mysql_query($statement, $this->oDbHandle); |
|
| 740 |
$this->result = @mysqli_query($this->oDbHandle, $sStatement); |
|
| 631 | 741 |
if ($this->result === false) {
|
| 632 | 742 |
if (DEBUG) {
|
| 633 |
throw new WbDatabaseException(mysql_error($this->oDbHandle)); |
|
| 743 |
throw new WbDatabaseException(mysqli_error($this->oDbHandle));
|
|
| 634 | 744 |
} else {
|
| 635 | 745 |
throw new WbDatabaseException('Error in SQL-Statement');
|
| 636 | 746 |
} |
| 637 | 747 |
} |
| 638 |
$this->error = mysql_error($this->oDbHandle); |
|
| 748 |
$this->error = mysqli_error($this->oDbHandle);
|
|
| 639 | 749 |
return $this->result; |
| 640 | 750 |
} |
| 641 | 751 |
/** |
| ... | ... | |
| 645 | 755 |
*/ |
| 646 | 756 |
function numRows() |
| 647 | 757 |
{
|
| 648 |
return mysql_num_rows($this->result); |
|
| 758 |
return mysqli_num_rows($this->result);
|
|
| 649 | 759 |
} |
| 650 | 760 |
/** |
| 651 | 761 |
* fetchRow |
| ... | ... | |
| 655 | 765 |
*/ |
| 656 | 766 |
function fetchRow($typ = MYSQL_BOTH) |
| 657 | 767 |
{
|
| 658 |
return mysql_fetch_array($this->result, $typ); |
|
| 768 |
return mysqli_fetch_array($this->result, $typ);
|
|
| 659 | 769 |
} |
| 660 | 770 |
/** |
| 661 | 771 |
* fetchObject |
| ... | ... | |
| 667 | 777 |
function fetchObject($sClassName = null, array $aParams = null) |
| 668 | 778 |
{
|
| 669 | 779 |
if ($sClassName === null || class_exists($sClassName)) {
|
| 670 |
return mysql_fetch_object($this->result, $sClassName, $aParams); |
|
| 780 |
return mysqli_fetch_object($this->result, $sClassName, $aParams);
|
|
| 671 | 781 |
} else {
|
| 672 | 782 |
throw new WbDatabaseException('Class <'.$sClassName.'> not available on request of mysql_fetch_object()');
|
| 673 | 783 |
} |
| ... | ... | |
| 691 | 801 |
{
|
| 692 | 802 |
$pmax = $this->numRows() - 1; |
| 693 | 803 |
$p = (($position < 0 || $position > $pmax) ? $pmax : $position); |
| 694 |
return mysql_data_seek($this->result, $p); |
|
| 804 |
return mysqli_data_seek($this->result, $p);
|
|
| 695 | 805 |
} |
| 696 | 806 |
/** |
| 697 | 807 |
* freeResult |
| ... | ... | |
| 700 | 810 |
*/ |
| 701 | 811 |
function freeResult() |
| 702 | 812 |
{
|
| 703 |
return mysql_free_result($this->result); |
|
| 813 |
return mysqli_free_result($this->result);
|
|
| 704 | 814 |
} |
| 705 | 815 |
/** |
| 706 | 816 |
* Get error |
| ... | ... | |
| 737 | 847 |
} |
| 738 | 848 |
} |
| 739 | 849 |
$retval = true; |
| 740 |
foreach( $key as $index=>$val) |
|
| 741 |
{
|
|
| 742 |
$index = strtolower($index); |
|
| 743 |
$sql = 'SELECT COUNT(`setting_id`) ' |
|
| 744 |
. 'FROM `'.$oDb->TablePrefix.$table.'` ' |
|
| 745 |
. 'WHERE `name` = \''.$index.'\' '; |
|
| 746 |
if ($oDb->getOne($sql)) {
|
|
| 747 |
$sql = 'UPDATE '; |
|
| 748 |
$sql_where = 'WHERE `name` = \''.$index.'\''; |
|
| 749 |
} else {
|
|
| 750 |
$sql = 'INSERT INTO '; |
|
| 751 |
$sql_where = ''; |
|
| 752 |
} |
|
| 753 |
$sql .= '`'.$oDb->TablePrefix.$table.'` '; |
|
| 754 |
$sql .= 'SET `name` = \''.$index.'\', '; |
|
| 755 |
$sql .= '`value` = \''.$val.'\' '.$sql_where; |
|
| 850 |
$sNameValPairs = ''; |
|
| 851 |
foreach ($key as $index => $val) {
|
|
| 852 |
$sNameValPairs .= ', (\''.$index.'\', \''.$val.'\')'; |
|
| 853 |
} |
|
| 854 |
$sValues = ltrim($sNameValPairs, ', '); |
|
| 855 |
if ($sValues != '') {
|
|
| 856 |
$sql = 'REPLACE INTO `'.$oDb->TablePrefix.$table.'` (`name`, `value`) ' |
|
| 857 |
. 'VALUES '.$sValues; |
|
| 756 | 858 |
if (!$oDb->doQuery($sql)) {
|
| 757 | 859 |
$retval = false; |
| 758 | 860 |
} |
| 759 |
}
|
|
| 861 |
}
|
|
| 760 | 862 |
return $retval; |
| 761 | 863 |
} |
Also available in: Unified diff
! complete rebuild of wb/install/ - changed to use import-struct.sql
! change class WbDatabase from mysql to msqli
! rework of WbDatabase::importSql()
! function db_update_key_value() optimized for speed
! field `settings_id`removed from table `settings` and new primary key set to `name`
! update-script extended to modify table `settings`