]> git.mxchange.org Git - friendica.git/blobdiff - include/dbstructure.php
Merge pull request #2758 from annando/1609-sql-charset
[friendica.git] / include / dbstructure.php
index 2078c208a2a03183234d08c0763eac002b37506b..dedd916a8eb1ad0fc3a1487aad8857ad4d8558c4 100644 (file)
@@ -142,24 +142,36 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
        if (is_null($definition))
                $definition = db_definition();
 
+
        // Compare it
        foreach ($definition AS $name => $structure) {
+               $is_new_table = False;
                $sql3="";
                if (!isset($database[$name])) {
-                       $r = db_create_table($name, $structure["fields"], $verbose, $action);
-                        if(false === $r)
+                       $r = db_create_table($name, $structure["fields"], $verbose, $action, $structure['indexes']);
+                       if(false === $r) {
                                $errors .=  t('Errors encountered creating database tables.').$name.EOL;
+                       }
+                       $is_new_table = True;
                } else {
-                       // Drop the index if it isn't present in the definition and index name doesn't start with "local_"
-                       foreach ($database[$name]["indexes"] AS $indexname => $fieldnames)
-                               if (!isset($structure["indexes"][$indexname]) && substr($indexname, 0, 6) != 'local_') {
+                       // Drop the index if it isn't present in the definition
+                       // or the definition differ from current status
+                       // and index name doesn't start with "local_"
+                       foreach ($database[$name]["indexes"] AS $indexname => $fieldnames) {
+                               $current_index_definition = implode(",",$fieldnames);
+                               if (isset($structure["indexes"][$indexname])) {
+                                       $new_index_definition = implode(",",$structure["indexes"][$indexname]);
+                               } else {
+                                       $new_index_definition = "__NOT_SET__";
+                               }
+                               if ($current_index_definition != $new_index_definition && substr($indexname, 0, 6) != 'local_') {
                                        $sql2=db_drop_index($indexname);
                                        if ($sql3 == "")
                                                $sql3 = "ALTER TABLE `".$name."` ".$sql2;
                                        else
                                                $sql3 .= ", ".$sql2;
                                }
-
+                       }
                        // Compare the field structure field by field
                        foreach ($structure["fields"] AS $fieldname => $parameters) {
                                if (!isset($database[$name]["fields"][$fieldname])) {
@@ -184,19 +196,28 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
                        }
                }
 
-               // Create the index
-               foreach ($structure["indexes"] AS $indexname => $fieldnames) {
-                       if (!isset($database[$name]["indexes"][$indexname])) {
-                               $sql2=db_create_index($indexname, $fieldnames);
-                               if ($sql2 != "") {
-                                       if ($sql3 == "")
-                                               $sql3 = "ALTER TABLE `".$name."` ".$sql2;
-                                       else
-                                               $sql3 .= ", ".$sql2;
+               // Create the index if the index don't exists in database
+               // or the definition differ from the current status.
+               // Don't create keys if table is new
+               if (!$is_new_table) {
+                       foreach ($structure["indexes"] AS $indexname => $fieldnames) {
+                               if (isset($database[$name]["indexes"][$indexname])) {
+                                       $current_index_definition = implode(",",$database[$name]["indexes"][$indexname]);
+                               } else {
+                                       $current_index_definition = "__NOT_SET__";
+                               }
+                               $new_index_definition = implode(",",$fieldnames);
+                               if ($current_index_definition != $new_index_definition) {
+                                       $sql2=db_create_index($indexname, $fieldnames);
+                                       if ($sql2 != "") {
+                                               if ($sql3 == "")
+                                                       $sql3 = "ALTER TABLE `".$name."` ".$sql2;
+                                               else
+                                                       $sql3 .= ", ".$sql2;
+                                       }
                                }
                        }
                }
-
                if ($sql3 != "") {
                        $sql3 .= ";";
 
@@ -230,8 +251,8 @@ function db_field_command($parameters, $create = true) {
        if ($parameters["extra"] != "")
                $fieldstruct .= " ".$parameters["extra"];
 
-       if (($parameters["primary"] != "") AND $create)
-               $fieldstruct .= " PRIMARY KEY";
+       /*if (($parameters["primary"] != "") AND $create)
+               $fieldstruct .= " PRIMARY KEY";*/
 
        return($fieldstruct);
 }
@@ -239,16 +260,27 @@ function db_field_command($parameters, $create = true) {
 function db_create_table($name, $fields, $verbose, $action, $indexes=null) {
        global $a, $db;
 
+       if (isset($a->config["system"]["db_charset"]))
+               $charset = $a->config["system"]["db_charset"];
+       elseif ($verbose)
+               $charset = "utf8mb4";
+       else
+               $charset = "utf8";
+
        $r = true;
 
        $sql = "";
+
        $sql_rows = array();
+       $primary_keys = array();
        foreach($fields AS $fieldname => $field) {
                $sql_rows[] = "`".dbesc($fieldname)."` ".db_field_command($field);
+               if (x($field,'primary') and $field['primary']!=''){
+                       $primary_keys[] = $fieldname;
+               }
        }
 
        if (!is_null($indexes)) {
-
                foreach ($indexes AS $indexname => $fieldnames) {
                        $sql_index = db_create_index($indexname, $fieldnames, "");
                        if (!is_null($sql_index)) $sql_rows[] = $sql_index;
@@ -257,8 +289,7 @@ function db_create_table($name, $fields, $verbose, $action, $indexes=null) {
 
        $sql = implode(",\n\t", $sql_rows);
 
-       $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", dbesc($name)).$sql."\n) DEFAULT CHARSET=utf8";
-
+       $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n\t", dbesc($name)).$sql."\n) DEFAULT CHARSET=".$charset;
        if ($verbose)
                echo $sql.";\n";
 
@@ -285,8 +316,16 @@ function db_drop_index($indexname) {
 
 function db_create_index($indexname, $fieldnames, $method="ADD") {
 
-       if ($indexname == "PRIMARY")
-               return;
+       $method = strtoupper(trim($method));
+       if ($method!="" && $method!="ADD") {
+               throw new Exception("Invalid parameter 'method' in db_create_index(): '$method'");
+               killme();
+       }
+
+
+       if ($indexname == "PRIMARY") {
+               return sprintf("%s PRIMARY KEY(`%s`)", $method, implode("`,`", $fieldnames));
+       }
 
        $names = "";
        foreach ($fieldnames AS $fieldname) {
@@ -299,11 +338,6 @@ function db_create_index($indexname, $fieldnames, $method="ADD") {
                        $names .= "`".dbesc($fieldname)."`";
        }
 
-       $method = strtoupper(trim($method));
-       if ($method!="" && $method!="ADD") {
-               throw new Exception("Invalid parameter 'method' in db_create_index(): '$method'");
-               killme();
-       }
 
        $sql = sprintf("%s INDEX `%s` (%s)", $method, dbesc($indexname), $names);
        return($sql);
@@ -426,6 +460,7 @@ function db_definition() {
                                        "keywords" => array("type" => "text", "not null" => "1"),
                                        "gender" => array("type" => "varchar(32)", "not null" => "1", "default" => ""),
                                        "attag" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+                                       "avatar" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "photo" => array("type" => "text", "not null" => "1"),
                                        "thumb" => array("type" => "text", "not null" => "1"),
                                        "micro" => array("type" => "text", "not null" => "1"),
@@ -480,6 +515,7 @@ function db_definition() {
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
                                        "uid" => array("uid"),
+                                       "nurl" => array("nurl"),
                                        )
                        );
        $database["conv"] = array(
@@ -509,17 +545,6 @@ function db_definition() {
                                        "PRIMARY" => array("id"),
                                        )
                        );
-       $database["dsprphotoq"] = array(
-                       "fields" => array(
-                                       "id" => array("type" => "int(10) unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
-                                       "uid" => array("type" => "int(11)", "not null" => "1", "default" => "0"),
-                                       "msg" => array("type" => "mediumtext", "not null" => "1"),
-                                       "attempt" => array("type" => "tinyint(4)", "not null" => "1", "default" => "0"),
-                                       ),
-                       "indexes" => array(
-                                       "PRIMARY" => array("id"),
-                                       )
-                       );
        $database["event"] = array(
                        "fields" => array(
                                        "id" => array("type" => "int(11)", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
@@ -550,6 +575,7 @@ function db_definition() {
        $database["fcontact"] = array(
                        "fields" => array(
                                        "id" => array("type" => "int(10) unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
+                                       "guid" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "url" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "name" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "photo" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
@@ -639,15 +665,23 @@ function db_definition() {
                                        "about" => array("type" => "text", "not null" => "1"),
                                        "keywords" => array("type" => "text", "not null" => "1"),
                                        "gender" => array("type" => "varchar(32)", "not null" => "1", "default" => ""),
+                                       "birthday" => array("type" => "varchar(32)", "not null" => "1", "default" => "0000-00-00"),
                                        "community" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
+                                       "hide" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
+                                       "nsfw" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
                                        "network" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "addr" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+                                       "notify" => array("type" => "text", "not null" => "1"),
+                                       "alias" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "generation" => array("type" => "tinyint(3)", "not null" => "1", "default" => "0"),
                                        "server_url" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        ),
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
                                        "nurl" => array("nurl"),
+                                       "name" => array("name"),
+                                       "nick" => array("nick"),
+                                       "addr" => array("addr"),
                                        "updated" => array("updated"),
                                        )
                        );
@@ -715,21 +749,6 @@ function db_definition() {
                                        "nurl" => array("nurl"),
                                        )
                        );
-       $database["guid"] = array(
-                       "fields" => array(
-                                       "id" => array("type" => "int(10) unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
-                                       "guid" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
-                                       "plink" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
-                                       "uri" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
-                                       "network" => array("type" => "varchar(32)", "not null" => "1", "default" => ""),
-                                       ),
-                       "indexes" => array(
-                                       "PRIMARY" => array("id"),
-                                       "guid" => array("guid"),
-                                       "plink" => array("plink"),
-                                       "uri" => array("uri"),
-                                       )
-                       );
        $database["hook"] = array(
                        "fields" => array(
                                        "id" => array("type" => "int(11)", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
@@ -768,6 +787,7 @@ function db_definition() {
                                        "uri" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "uid" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0"),
                                        "contact-id" => array("type" => "int(11)", "not null" => "1", "default" => "0"),
+                                       "gcontact-id" => array("type" => "int(11) unsigned", "not null" => "1", "default" => "0"),
                                        "type" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "wall" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
                                        "gravity" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
@@ -780,9 +800,11 @@ function db_definition() {
                                        "commented" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
                                        "received" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
                                        "changed" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
+                                       "owner-id" => array("type" => "int(11)", "not null" => "1", "default" => "0"),
                                        "owner-name" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "owner-link" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "owner-avatar" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+                                       "author-id" => array("type" => "int(11)", "not null" => "1", "default" => "0"),
                                        "author-name" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "author-link" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "author-avatar" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
@@ -835,7 +857,7 @@ function db_definition() {
                                        "extid" => array("extid"),
                                        "uid_id" => array("uid","id"),
                                        "uid_created" => array("uid","created"),
-                                       "uid_unseen" => array("uid","unseen"),
+                                       "uid_unseen_contactid" => array("uid","unseen","contact-id"),
                                        "uid_network_received" => array("uid","network","received"),
                                        "uid_received" => array("uid","received"),
                                        "uid_network_commented" => array("uid","network","commented"),
@@ -843,14 +865,18 @@ function db_definition() {
                                        "uid_title" => array("uid","title"),
                                        "uid_thrparent" => array("uid","thr-parent"),
                                        "uid_parenturi" => array("uid","parent-uri"),
+                                       "uid_contactid_id" => array("uid","contact-id","id"),
                                        "uid_contactid_created" => array("uid","contact-id","created"),
+                                       "gcontactid_uid_created" => array("gcontact-id","uid","created"),
+                                       "authorid_created" => array("author-id","created"),
+                                       "ownerid_created" => array("owner-id","created"),
                                        "wall_body" => array("wall","body(6)"),
                                        "uid_visible_moderated_created" => array("uid","visible","moderated","created"),
                                        "uid_uri" => array("uid","uri"),
                                        "uid_wall_created" => array("uid","wall","created"),
                                        "resource-id" => array("resource-id"),
                                        "uid_type" => array("uid","type"),
-                                       "uid_starred" => array("uid","starred"),
+                                       "uid_starred_id" => array("uid","starred", "id"),
                                        "contactid_allowcid_allowpid_denycid_denygid" => array("contact-id","allow_cid(10)","allow_gid(10)","deny_cid(10)","deny_gid(10)"),
                                        "uid_wall_parent_created" => array("uid","wall","parent","created"),
                                        "uid_type_changed" => array("uid","type","changed"),
@@ -987,6 +1013,30 @@ function db_definition() {
                                        "receiver-uid" => array("receiver-uid"),
                                        )
                        );
+       $database["oembed"] = array(
+                       "fields" => array(
+                                       "url" => array("type" => "varchar(255)", "not null" => "1", "primary" => "1"),
+                                       "content" => array("type" => "text", "not null" => "1"),
+                                       "created" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
+                                       ),
+                       "indexes" => array(
+                                       "PRIMARY" => array("url"),
+                                       "created" => array("created"),
+                                       )
+                       );
+       $database["parsed_url"] = array(
+                       "fields" => array(
+                                       "url" => array("type" => "varchar(255)", "not null" => "1", "primary" => "1"),
+                                       "guessing" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0", "primary" => "1"),
+                                       "oembed" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0", "primary" => "1"),
+                                       "content" => array("type" => "text", "not null" => "1"),
+                                       "created" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
+                                       ),
+                       "indexes" => array(
+                                       "PRIMARY" => array("url", "guessing", "oembed"),
+                                       "created" => array("created"),
+                                       )
+                       );
        $database["pconfig"] = array(
                        "fields" => array(
                                        "id" => array("type" => "int(11)", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
@@ -1202,7 +1252,6 @@ function db_definition() {
                        "fields" => array(
                                        "id" => array("type" => "int(10) unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
                                        "iid" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0"),
-                                       "retract_iid" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0"),
                                        "signed_text" => array("type" => "mediumtext", "not null" => "1"),
                                        "signature" => array("type" => "text", "not null" => "1"),
                                        "signer" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
@@ -1210,7 +1259,6 @@ function db_definition() {
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
                                        "iid" => array("iid"),
-                                       "retract_iid" => array("retract_iid"),
                                        )
                        );
        $database["spam"] = array(
@@ -1260,6 +1308,9 @@ function db_definition() {
                                        "iid" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0", "primary" => "1"),
                                        "uid" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0"),
                                        "contact-id" => array("type" => "int(11) unsigned", "not null" => "1", "default" => "0"),
+                                       "gcontact-id" => array("type" => "int(11) unsigned", "not null" => "1", "default" => "0"),
+                                       "owner-id" => array("type" => "int(11) unsigned", "not null" => "1", "default" => "0"),
+                                       "author-id" => array("type" => "int(11) unsigned", "not null" => "1", "default" => "0"),
                                        "created" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
                                        "edited" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
                                        "commented" => array("type" => "datetime", "not null" => "1", "default" => "0000-00-00 00:00:00"),
@@ -1289,6 +1340,8 @@ 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"),
+                                       "uid_gcontactid_commented" => array("uid","gcontact-id","commented"),
+                                       "uid_gcontactid_created" => array("uid","gcontact-id","created"),
                                        "wall_private_received" => array("wall","private","received"),
                                        "uid_created" => array("uid","created"),
                                        "uid_commented" => array("uid","commented"),
@@ -1307,21 +1360,6 @@ function db_definition() {
                                        "PRIMARY" => array("id"),
                                        )
                        );
-       $database["unique_contacts"] = array(
-                       "fields" => array(
-                                       "id" => array("type" => "int(11)", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
-                                       "url" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
-                                       "nick" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
-                                       "name" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
-                                       "avatar" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
-                                       "location" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
-                                       "about" => array("type" => "text", "not null" => "1"),
-                                       ),
-                       "indexes" => array(
-                                       "PRIMARY" => array("id"),
-                                       "url" => array("url"),
-                                       )
-                       );
        $database["user"] = array(
                        "fields" => array(
                                        "uid" => array("type" => "int(11)", "not null" => "1", "extra" => "auto_increment", "primary" => "1"),
@@ -1422,6 +1460,7 @@ function dbstructure_run(&$argv, &$argc) {
                switch ($argv[1]) {
                        case "update":
                                update_structure(true, true);
+                               set_config('system','build',DB_UPDATE_VERSION);
                                return;
                        case "dumpsql":
                                print_structure(db_definition());