]> git.mxchange.org Git - friendica.git/blobdiff - include/dbstructure.php
Merge remote-tracking branch 'friendica/develop' into develop
[friendica.git] / include / dbstructure.php
index fdf09d90def8abd034d0801540fa02ca2ed744b4..50c2856ab6d4a26e8b820f619e431289c793ea2a 100644 (file)
@@ -17,8 +17,15 @@ function update_fail($update_id, $error_message){
                $admin_mail_list
        );
 
-       // every admin could had different language
+       // 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);
+
+               // Don't continue
+               return;
+       }
 
+       // every admin could had different language
        foreach ($adminlist as $admin) {
                $lang = (($admin['language'])?$admin['language']:'en');
                push_lang($lang);
@@ -48,11 +55,11 @@ function update_fail($update_id, $error_message){
        $email_tpl = get_intltext_template("update_fail_eml.tpl");
        $email_msg = replace_macros($email_tpl, array(
                '$sitename' => $a->config['sitename'],
-               '$siteurl' =>  $a->get_baseurl(),
+               '$siteurl' =>  App::get_baseurl(),
                '$update' => DB_UPDATE_VERSION,
                '$error' => sprintf(t('Update %s failed. See error logs.'), DB_UPDATE_VERSION)
        ));
-       $subject=sprintf(t('Update Error at %s'), $a->get_baseurl());
+       $subject=sprintf(t('Update Error at %s'), App::get_baseurl());
        require_once('include/email.php');
        $subject = email_header_encode($subject,'UTF-8');
        mail($a->config['admin_email'], $subject, $email_msg,
@@ -73,10 +80,11 @@ function table_structure($table) {
        $fielddata = array();
        $indexdata = array();
 
-       if (is_array($indexes))
+       if (dbm::is_result($indexes))
                foreach ($indexes AS $index) {
-                       if ($index["Index_type"] == "FULLTEXT")
+                       if ($index["Index_type"] == "FULLTEXT") {
                                continue;
+                       }
 
                        if ($index['Key_name'] != 'PRIMARY' && $index['Non_unique'] == '0' && !isset($indexdata[$index["Key_name"]])) {
                                $indexdata[$index["Key_name"]] = array('UNIQUE');
@@ -87,26 +95,31 @@ function table_structure($table) {
                        // To avoid the need to add this to every index definition we just ignore it here.
                        // Exception are primary indexes
                        // Since there are some combindex primary indexes we use the limit of 180 here.
-                       if (($index["Sub_part"] != "") AND (($index["Sub_part"] < 180) OR ($index["Key_name"] == "PRIMARY")))
+                       if (($index["Sub_part"] != "") AND (($index["Sub_part"] < 180) OR ($index["Key_name"] == "PRIMARY"))) {
                                $column .= "(".$index["Sub_part"].")";
+                       }
 
                        $indexdata[$index["Key_name"]][] = $column;
                }
 
-       if (is_array($structures)) {
-               foreach($structures AS $field) {
+       if (dbm::is_result($structures)) {
+               foreach ($structures AS $field) {
                        $fielddata[$field["Field"]]["type"] = $field["Type"];
-                       if ($field["Null"] == "NO")
+                       if ($field["Null"] == "NO") {
                                $fielddata[$field["Field"]]["not null"] = true;
+                       }
 
-                       if (isset($field["Default"]))
+                       if (isset($field["Default"])) {
                                $fielddata[$field["Field"]]["default"] = $field["Default"];
+                       }
 
-                       if ($field["Extra"] != "")
+                       if ($field["Extra"] != "") {
                                $fielddata[$field["Field"]]["extra"] = $field["Extra"];
+                       }
 
-                       if ($field["Key"] == "PRI")
+                       if ($field["Key"] == "PRI") {
                                $fielddata[$field["Field"]]["primary"] = true;
+                       }
                }
        }
        return(array("fields"=>$fielddata, "indexes"=>$indexdata));
@@ -130,13 +143,15 @@ function print_structure($database, $charset) {
 function update_structure($verbose, $action, $tables=null, $definition=null) {
        global $a, $db;
 
-       if ($action)
+       if ($action) {
                set_config('system', 'maintenance', 1);
+       }
 
-       if (isset($a->config["system"]["db_charset"]))
+       if (isset($a->config["system"]["db_charset"])) {
                $charset = $a->config["system"]["db_charset"];
-       else
+       } else {
                $charset = "utf8";
+       }
 
        $errors = false;
 
@@ -145,31 +160,36 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
        // Get the current structure
        $database = array();
 
-       if (is_null($tables))
-               $tables = q("show tables");
+       if (is_null($tables)) {
+               $tables = q("SHOW TABLES");
+       }
 
        foreach ($tables AS $table) {
                $table = current($table);
 
+               logger(sprintf('updating structure for table %s ...', $table), LOGGER_DEBUG);
                $database[$table] = table_structure($table);
        }
 
        // Get the definition
-       if (is_null($definition))
+       if (is_null($definition)) {
                $definition = db_definition($charset);
+       }
 
        // Ensure index conversion to unique removes duplicates
        $sql_config = "SET session old_alter_table=1;";
-       if ($verbose)
+       if ($verbose) {
                echo $sql_config."\n";
-       if ($action)
-               @$db->q($sql_config);
+       }
+       if ($action) {
+               $db->q($sql_config);
+       }
 
        // 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)) {
                $ignore = '';
-       }else {
+       } else {
                $ignore = ' IGNORE';
        }
 
@@ -179,15 +199,17 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
                $sql3="";
                if (!isset($database[$name])) {
                        $r = db_create_table($name, $structure["fields"], $charset, $verbose, $action, $structure['indexes']);
-                       if(false === $r) {
+                       if (!dbm::is_result($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
-                       // or the definition differ from current status
-                       // and index name doesn't start with "local_"
-                       foreach ($database[$name]["indexes"] AS $indexname => $fieldnames) {
+                       /*
+                        * 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]);
@@ -196,39 +218,44 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
                                }
                                if ($current_index_definition != $new_index_definition && substr($indexname, 0, 6) != 'local_') {
                                        $sql2=db_drop_index($indexname);
-                                       if ($sql3 == "")
+                                       if ($sql3 == "") {
                                                $sql3 = "ALTER".$ignore." TABLE `".$name."` ".$sql2;
-                                       else
+                                       } else {
                                                $sql3 .= ", ".$sql2;
+                                       }
                                }
                        }
                        // Compare the field structure field by field
                        foreach ($structure["fields"] AS $fieldname => $parameters) {
                                if (!isset($database[$name]["fields"][$fieldname])) {
                                        $sql2=db_add_table_field($fieldname, $parameters);
-                                       if ($sql3 == "")
+                                       if ($sql3 == "") {
                                                $sql3 = "ALTER TABLE `".$name."` ".$sql2;
-                                       else
+                                       } else {
                                                $sql3 .= ", ".$sql2;
+                                       }
                                } else {
                                        // Compare the field definition
                                        $current_field_definition = implode(",",$database[$name]["fields"][$fieldname]);
                                        $new_field_definition = implode(",",$parameters);
                                        if ($current_field_definition != $new_field_definition) {
                                                $sql2=db_modify_table_field($fieldname, $parameters);
-                                               if ($sql3 == "")
+                                               if ($sql3 == "") {
                                                        $sql3 = "ALTER TABLE `".$name."` ".$sql2;
-                                               else
+                                               } 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
+               /*
+                * 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])) {
@@ -256,7 +283,7 @@ function update_structure($verbose, $action, $tables=null, $definition=null) {
 
                        if ($action) {
                                $r = @$db->q($sql3);
-                               if(false === $r)
+                               if (dbm::is_result($r))
                                        $errors .= t('Errors encountered performing database changes.').$sql3.EOL;
                        }
                }
@@ -358,10 +385,11 @@ function db_create_index($indexname, $fieldnames, $method="ADD") {
                if ($names != "")
                        $names .= ",";
 
-               if (preg_match('|(.+)\((\d+)\)|', $fieldname, $matches))
+               if (preg_match('|(.+)\((\d+)\)|', $fieldname, $matches)) {
                        $names .= "`".dbesc($matches[1])."`(".intval($matches[2]).")";
-               else
+               } else {
                        $names .= "`".dbesc($fieldname)."`";
+               }
        }
 
        if ($indexname == "PRIMARY") {
@@ -374,8 +402,9 @@ function db_create_index($indexname, $fieldnames, $method="ADD") {
 }
 
 function db_index_suffix($charset, $reduce = 0) {
-       if ($charset != "utf8mb4")
+       if ($charset != "utf8mb4") {
                return "";
+       }
 
        // On utf8mb4 indexes can only have a length of 191
        $indexlength = 191 - $reduce;
@@ -443,6 +472,7 @@ function db_definition($charset) {
                        "indexes" => array(
                                        "PRIMARY" => array("k".db_index_suffix($charset)),
                                        "updated" => array("updated"),
+                                       "expire_mode_updated" => array("expire_mode", "updated"),
                                        )
                        );
        $database["challenge"] = array(
@@ -557,6 +587,7 @@ function db_definition($charset) {
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
                                        "uid" => array("uid"),
+                                       "addr_uid" => array("addr", "uid"),
                                        "nurl" => array("nurl"),
                                        )
                        );
@@ -585,6 +616,7 @@ function db_definition($charset) {
                                        ),
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
+                                       "cmd_item_contact" => array("UNIQUE", "cmd", "item", "contact"),
                                        )
                        );
        $database["event"] = array(
@@ -915,7 +947,7 @@ function db_definition($charset) {
                                        "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_uri" => array("uid", "uri"),
                                        "uid_wall_created" => array("uid","wall","created"),
                                        "resource-id" => array("resource-id"),
                                        "uid_type" => array("uid","type"),
@@ -1036,6 +1068,8 @@ function db_definition($charset) {
                                        "seen" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
                                        "verb" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "otype" => array("type" => "varchar(16)", "not null" => "1", "default" => ""),
+                                       "name_cache" => array("type" => "tinytext"),
+                                       "msg_cache" => array("type" => "mediumtext")
                                        ),
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
@@ -1120,7 +1154,9 @@ function db_definition($charset) {
                                        ),
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
-                                       "uid" => array("uid"),
+                                       "uid_contactid" => array("uid", "contact-id"),
+                                       "uid_profile" => array("uid", "profile"),
+                                       "uid_album_created" => array("uid", "album", "created"),
                                        "resource-id" => array("resource-id"),
                                        "guid" => array("guid"),
                                        )
@@ -1273,6 +1309,7 @@ function db_definition($charset) {
                                        "uid" => array("type" => "int(11) unsigned", "not null" => "1", "default" => "0"),
                                        "password" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "language" => array("type" => "varchar(16)", "not null" => "1", "default" => ""),
+                                       "note" => array("type" => "text"),
                                        ),
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
@@ -1355,6 +1392,7 @@ function db_definition($charset) {
                                        "type_term" => array("type","term"),
                                        "uid_otype_type_term_global_created" => array("uid","otype","type","term","global","created"),
                                        "otype_type_term_tid" => array("otype","type","term","tid"),
+                                       "uid_otype_type_url" => array("uid","otype","type","url"),
                                        "guid" => array("guid"),
                                        )
                        );
@@ -1555,9 +1593,6 @@ function dbstructure_run(&$argv, &$argc) {
        echo "dumpsql           dump database schema\n";
        return;
 
-
-
-
 }
 
 if (array_search(__file__,get_included_files())===0){