X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=include%2Fdbstructure.php;h=b8cf8e57ac4881f846cc03a4ec9f94c5c5cc1edf;hb=4584670af446553b6b7f7870e7081620ebaf44ee;hp=cedef40d98221870556430becd23a94fbb970231;hpb=c2820c452fbb9b2d26594fb504fe0dfc8206c5a2;p=friendica.git diff --git a/include/dbstructure.php b/include/dbstructure.php index cedef40d98..b8cf8e57ac 100644 --- a/include/dbstructure.php +++ b/include/dbstructure.php @@ -1,20 +1,24 @@ database_name())); + dbesc(dba::database_name())); if (!dbm::is_result($r)) { echo t('There are no tables on MyISAM.')."\n"; @@ -25,9 +29,9 @@ function convert_to_innodb() { $sql = sprintf("ALTER TABLE `%s` engine=InnoDB;", dbesc($table['TABLE_NAME'])); echo $sql."\n"; - $result = @$db->q($sql); + $result = dba::e($sql); if (!dbm::is_result($result)) { - print_update_error($db, $sql); + print_update_error($sql); } } } @@ -47,7 +51,7 @@ function update_fail($update_id, $error_message) { // No valid result? if (!dbm::is_result($adminlist)) { - logger(sprintf('Cannot notify administrators about update_id=%d, error_message=%s', $update_id, $error_message), LOGGER_WARNING); + logger(sprintf('Cannot notify administrators about update_id=%d, error_message=%s', $update_id, $error_message), LOGGER_NORMAL); // Don't continue return; @@ -68,7 +72,7 @@ function update_fail($update_id, $error_message) { $body = sprintf($body, $error_message); notification(array( - 'type' => "SYSTEM_EMAIL", + 'type' => SYSTEM_EMAIL, 'to_email' => $admin['email'], 'preamble' => $preamble, 'body' => $body, @@ -80,14 +84,15 @@ function update_fail($update_id, $error_message) { /* + @TODO deprecated code? $email_tpl = get_intltext_template("update_fail_eml.tpl"); $email_msg = replace_macros($email_tpl, array( '$sitename' => $a->config['sitename'], - '$siteurl' => App::get_baseurl(), + '$siteurl' => System::baseUrl(), '$update' => DB_UPDATE_VERSION, '$error' => sprintf(t('Update %s failed. See error logs.'), DB_UPDATE_VERSION) )); - $subject=sprintf(t('Update Error at %s'), App::get_baseurl()); + $subject=sprintf(t('Update Error at %s'), System::baseUrl()); require_once('include/email.php'); $subject = email_header_encode($subject,'UTF-8'); mail($a->config['admin_email'], $subject, $email_msg, @@ -128,7 +133,7 @@ function table_structure($table) { // On utf8mb4 a varchar index can only have a length of 191 // The "show index" command sometimes returns this value although this value wasn't added manually. // Because we don't want to add this number to every index, we ignore bigger numbers - if (($index["Sub_part"] != "") AND (($index["Sub_part"] < 191) OR ($index["Key_name"] == "PRIMARY"))) { + if (($index["Sub_part"] != "") && (($index["Sub_part"] < 191) || ($index["Key_name"] == "PRIMARY"))) { $column .= "(".$index["Sub_part"].")"; } @@ -181,20 +186,19 @@ function print_structure($database) { /** * @brief Print out database error messages * - * @param object $db Database object * @param string $message Message to be added to the error message * * @return string Error message */ -function print_update_error($db, $message) { +function print_update_error($message) { echo sprintf(t("\nError %d occurred during database update:\n%s\n"), - $db->errorno, $db->error); + dba::errorNo(), dba::errorMessage()); return t('Errors encountered performing database changes: ').$message.EOL; } function update_structure($verbose, $action, $tables=null, $definition=null) { - global $a, $db; + global $a; if ($action) { Config::set('system', 'maintenance', 1); @@ -227,8 +231,8 @@ function update_structure($verbose, $action, $tables=null, $definition=null) { } // MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements - if ((version_compare($db->server_info(), '5.7.4') >= 0) AND - !(strpos($db->server_info(), 'MariaDB') !== false)) { + if ((version_compare(dba::server_info(), '5.7.4') >= 0) && + !(strpos(dba::server_info(), 'MariaDB') !== false)) { $ignore = ''; } else { $ignore = ' IGNORE'; @@ -242,7 +246,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) { if (!isset($database[$name])) { $r = db_create_table($name, $structure["fields"], $verbose, $action, $structure['indexes']); if (!dbm::is_result($r)) { - $errors .= print_update_error($db, $name); + $errors .= print_update_error($name); } $is_new_table = True; } else { @@ -376,7 +380,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) { $field_definition = $database[$name]["fields"][$fieldname]; // Define the default collation if not given - if (!isset($parameters['Collation']) AND !is_null($field_definition['Collation'])) { + if (!isset($parameters['Collation']) && !is_null($field_definition['Collation'])) { $parameters['Collation'] = 'utf8mb4_general_ci'; } else { $parameters['Collation'] = null; @@ -384,7 +388,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) { if ($field_definition['Collation'] != $parameters['Collation']) { $sql2 = db_modify_table_field($fieldname, $parameters); - if (($sql3 == "") OR (substr($sql3, -2, 2) == "; ")) { + if (($sql3 == "") || (substr($sql3, -2, 2) == "; ")) { $sql3 .= "ALTER" . $ignore . " TABLE `".$temp_name."` ".$sql2; } else { $sql3 .= ", ".$sql2; @@ -398,6 +402,14 @@ function update_structure($verbose, $action, $tables=null, $definition=null) { $sql3 .= ";"; } + $field_list = ''; + if ($is_unique && $ignore == '') { + foreach ($structure['fields'] AS $fieldname => $parameters) { + $field_list .= 'ANY_VALUE(`' . $fieldname . '`),'; + } + $field_list = rtrim($field_list, ','); + } + if ($verbose) { // Ensure index conversion to unique removes duplicates if ($is_unique) { @@ -414,7 +426,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) { if ($ignore != "") { echo "SET session old_alter_table=0;\n"; } else { - echo "INSERT INTO `".$temp_name."` SELECT * FROM `".$name."`".$group_by.";\n"; + echo "INSERT INTO `".$temp_name."` SELECT ".$field_list." FROM `".$name."`".$group_by.";\n"; echo "DROP TABLE `".$name."`;\n"; echo "RENAME TABLE `".$temp_name."` TO `".$name."`;\n"; } @@ -427,37 +439,37 @@ function update_structure($verbose, $action, $tables=null, $definition=null) { // Ensure index conversion to unique removes duplicates if ($is_unique) { if ($ignore != "") { - $db->q("SET session old_alter_table=1;"); + dba::e("SET session old_alter_table=1;"); } else { - $r = $db->q("CREATE TABLE `".$temp_name."` LIKE `".$name."`;"); + $r = dba::e("CREATE TABLE `".$temp_name."` LIKE `".$name."`;"); if (!dbm::is_result($r)) { - $errors .= print_update_error($db, $sql3); + $errors .= print_update_error($sql3); return $errors; } } } - $r = @$db->q($sql3); + $r = @dba::e($sql3); if (!dbm::is_result($r)) { - $errors .= print_update_error($db, $sql3); + $errors .= print_update_error($sql3); } if ($is_unique) { if ($ignore != "") { - $db->q("SET session old_alter_table=0;"); + dba::e("SET session old_alter_table=0;"); } else { - $r = $db->q("INSERT INTO `".$temp_name."` SELECT * FROM `".$name."`".$group_by.";"); + $r = dba::e("INSERT INTO `".$temp_name."` SELECT ".$field_list." FROM `".$name."`".$group_by.";"); if (!dbm::is_result($r)) { - $errors .= print_update_error($db, $sql3); + $errors .= print_update_error($sql3); return $errors; } - $r = $db->q("DROP TABLE `".$name."`;"); + $r = dba::e("DROP TABLE `".$name."`;"); if (!dbm::is_result($r)) { - $errors .= print_update_error($db, $sql3); + $errors .= print_update_error($sql3); return $errors; } - $r = $db->q("RENAME TABLE `".$temp_name."` TO `".$name."`;"); + $r = dba::e("RENAME TABLE `".$temp_name."` TO `".$name."`;"); if (!dbm::is_result($r)) { - $errors .= print_update_error($db, $sql3); + $errors .= print_update_error($sql3); return $errors; } } @@ -471,6 +483,12 @@ function update_structure($verbose, $action, $tables=null, $definition=null) { Config::set('system', 'maintenance_reason', ''); } + if ($errors) { + Config::set('system', 'dbupdate', DB_UPDATE_FAILED); + } else { + Config::set('system', 'dbupdate', DB_UPDATE_SUCCESSFUL); + } + return $errors; } @@ -494,14 +512,14 @@ function db_field_command($parameters, $create = true) { if ($parameters["extra"] != "") $fieldstruct .= " ".$parameters["extra"]; - /*if (($parameters["primary"] != "") AND $create) + /*if (($parameters["primary"] != "") && $create) $fieldstruct .= " PRIMARY KEY";*/ return($fieldstruct); } function db_create_table($name, $fields, $verbose, $action, $indexes=null) { - global $a, $db; + global $a; $r = true; @@ -511,7 +529,7 @@ function db_create_table($name, $fields, $verbose, $action, $indexes=null) { $primary_keys = array(); foreach ($fields AS $fieldname => $field) { $sql_rows[] = "`".dbesc($fieldname)."` ".db_field_command($field); - if (x($field,'primary') and $field['primary']!='') { + if (x($field,'primary') && $field['primary']!='') { $primary_keys[] = $fieldname; } } @@ -530,7 +548,7 @@ function db_create_table($name, $fields, $verbose, $action, $indexes=null) { echo $sql.";\n"; if ($action) - $r = @$db->q($sql); + $r = @dba::e($sql); return $r; } @@ -799,7 +817,7 @@ function db_definition() { $database["conv"] = array( "fields" => array( "id" => array("type" => "int(10) unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1"), - "guid" => array("type" => "varchar(64)", "not null" => "1", "default" => ""), + "guid" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), "recips" => array("type" => "text"), "uid" => array("type" => "int(11)", "not null" => "1", "default" => "0", "relation" => array("user" => "uid")), "creator" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), @@ -878,7 +896,7 @@ function db_definition() { "indexes" => array( "PRIMARY" => array("id"), "addr" => array("addr(32)"), - "url" => array("url"), + "url" => array("UNIQUE", "url(190)"), ) ); $database["ffinder"] = array( @@ -963,7 +981,7 @@ function db_definition() { ), "indexes" => array( "PRIMARY" => array("id"), - "nurl" => array("nurl(64)"), + "nurl" => array("UNIQUE", "nurl(190)"), "name" => array("name(64)"), "nick" => array("nick(32)"), "addr" => array("addr(64)"), @@ -1033,7 +1051,7 @@ function db_definition() { ), "indexes" => array( "PRIMARY" => array("id"), - "nurl" => array("nurl(32)"), + "nurl" => array("UNIQUE", "nurl(190)"), ) ); $database["hook"] = array( @@ -1069,7 +1087,7 @@ function db_definition() { ); $database["item"] = array( "fields" => array( - "id" => array("type" => "int(10) unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1"), + "id" => array("type" => "int(10) unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "relation" => array("thread" => "iid")), "guid" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), "uri" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), "uid" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0", "relation" => array("user" => "uid")), @@ -1152,6 +1170,7 @@ function db_definition() { "uid_parenturi" => array("uid","parent-uri(190)"), "uid_contactid_created" => array("uid","contact-id","created"), "authorid_created" => array("author-id","created"), + "ownerid" => array("owner-id"), "uid_uri" => array("uid", "uri(190)"), "resource-id" => array("resource-id"), "contactid_allowcid_allowpid_denycid_denygid" => array("contact-id","allow_cid(10)","allow_gid(10)","deny_cid(10)","deny_gid(10)"), // @@ -1185,7 +1204,7 @@ function db_definition() { "id" => array("type" => "int(11)", "not null" => "1", "extra" => "auto_increment", "primary" => "1"), "name" => array("type" => "varchar(128)", "not null" => "1", "default" => ""), "locked" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"), - "created" => array("type" => "datetime", "default" => NULL_DATE), + "pid" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0"), ), "indexes" => array( "PRIMARY" => array("id"), @@ -1195,7 +1214,7 @@ function db_definition() { "fields" => array( "id" => array("type" => "int(10) unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1"), "uid" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0", "relation" => array("user" => "uid")), - "guid" => array("type" => "varchar(64)", "not null" => "1", "default" => ""), + "guid" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), "from-name" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), "from-photo" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), "from-url" => array("type" => "varchar(255)", "not null" => "1", "default" => ""), @@ -1217,6 +1236,7 @@ function db_definition() { "convid" => array("convid"), "uri" => array("uri(64)"), "parent-uri" => array("parent-uri(64)"), + "contactid" => array("contact-id"), ) ); $database["mailacct"] = array( @@ -1354,6 +1374,7 @@ function db_definition() { ), "indexes" => array( "PRIMARY" => array("id"), + "contactid" => array("contact-id"), "uid_contactid" => array("uid", "contact-id"), "uid_profile" => array("uid", "profile"), "uid_album_scale_created" => array("uid", "album(32)", "scale", "created"), @@ -1549,7 +1570,7 @@ function db_definition() { ), "indexes" => array( "PRIMARY" => array("id"), - "iid" => array("iid"), + "iid" => array("UNIQUE", "iid"), ) ); $database["spam"] = array( @@ -1627,9 +1648,13 @@ function db_definition() { "uid_network_created" => array("uid","network","created"), "uid_contactid_commented" => array("uid","contact-id","commented"), "uid_contactid_created" => array("uid","contact-id","created"), + "contactid" => array("contact-id"), + "ownerid" => array("owner-id"), + "authorid" => array("author-id"), "uid_created" => array("uid","created"), "uid_commented" => array("uid","commented"), "uid_wall_created" => array("uid","wall","created"), + "private_wall_received" => array("private","wall","received"), ) ); $database["tokens"] = array( @@ -1714,9 +1739,14 @@ function db_definition() { "created" => array("type" => "datetime", "not null" => "1", "default" => NULL_DATE), "pid" => array("type" => "int(11)", "not null" => "1", "default" => "0"), "executed" => array("type" => "datetime", "not null" => "1", "default" => NULL_DATE), + "done" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"), ), "indexes" => array( "PRIMARY" => array("id"), + "pid" => array("pid"), + "parameter" => array("parameter(64)"), + "priority_created" => array("priority", "created"), + "executed" => array("executed"), ) ); @@ -1728,20 +1758,18 @@ function db_definition() { * run from command line */ function dbstructure_run(&$argv, &$argc) { - global $a, $db; + global $a; - if (is_null($a)) { - $a = new App; + if (empty($a)) { + $a = new App(dirname(__DIR__)); } - if (is_null($db)) { - @include(".htconfig.php"); - require_once("include/dba.php"); - $db = new dba($db_host, $db_user, $db_pass, $db_data); - unset($db_host, $db_user, $db_pass, $db_data); - } + @include ".htconfig.php"; + require_once "include/dba.php"; + dba::connect($db_host, $db_user, $db_pass, $db_data); + unset($db_host, $db_user, $db_pass, $db_data); - if ($argc==2) { + if ($argc == 2) { switch ($argv[1]) { case "dryrun": update_structure(true, false); @@ -1751,7 +1779,7 @@ function dbstructure_run(&$argv, &$argc) { $build = get_config('system','build'); if (!x($build)) { - set_config('system','build',DB_UPDATE_VERSION); + set_config('system', 'build', DB_UPDATE_VERSION); $build = DB_UPDATE_VERSION; } @@ -1761,7 +1789,9 @@ function dbstructure_run(&$argv, &$argc) { // run any left update_nnnn functions in update.php for ($x = $stored; $x < $current; $x ++) { $r = run_update_function($x); - if (!$r) break; + if (!$r) { + break; + } } set_config('system','build',DB_UPDATE_VERSION); @@ -1788,7 +1818,7 @@ function dbstructure_run(&$argv, &$argc) { } -if (array_search(__file__,get_included_files())===0) { +if (array_search(__FILE__,get_included_files())===0) { dbstructure_run($_SERVER["argv"],$_SERVER["argc"]); killme(); }