]> git.mxchange.org Git - friendica.git/blobdiff - include/dbstructure.php
Create `database.sql` from `include\dbstructure.php`
[friendica.git] / include / dbstructure.php
index 66e67c0a9aaf977ca68f0086360fa955d26ca60e..6bcff294627776962147cdcd3fce6f0599b38e0c 100644 (file)
@@ -106,29 +106,17 @@ function table_structure($table) {
 }
 
 function print_structure($database) {
+       echo "-- ------------------------------------------\n";
+       echo "-- ".FRIENDICA_PLATFORM." ".FRIENDICA_VERSION." (".FRIENDICA_CODENAME,")\n";
+       echo "-- DB_UPDATE_VERSION ".DB_UPDATE_VERSION."\n";
+       echo "-- ------------------------------------------\n\n\n";
        foreach ($database AS $name => $structure) {
-               echo "\t".'$database["'.$name."\"] = array(\n";
+               echo "--\n";
+               echo "-- TABLE $name\n";
+               echo "--\n";
+               db_create_table($name, $structure['fields'], true, false, $structure["indexes"]);
 
-               echo "\t\t\t".'"fields" => array('."\n";
-               foreach ($structure["fields"] AS $fieldname => $parameters) {
-                       echo "\t\t\t\t\t".'"'.$fieldname.'" => array(';
-
-                       $data = "";
-                       foreach ($parameters AS $name => $value) {
-                               if ($data != "")
-                                       $data .= ", ";
-                               $data .= '"'.$name.'" => "'.$value.'"';
-                       }
-
-                       echo $data."),\n";
-               }
-               echo "\t\t\t\t\t),\n";
-               echo "\t\t\t".'"indexes" => array('."\n";
-               foreach ($structure["indexes"] AS $indexname => $fieldnames) {
-                       echo "\t\t\t\t\t".'"'.$indexname.'" => array("'.implode($fieldnames, '","').'"'."),\n";
-               }
-               echo "\t\t\t\t\t)\n";
-               echo "\t\t\t);\n";
+               echo "\n";
        }
 }
 
@@ -243,7 +231,7 @@ function db_field_command($parameters, $create = true) {
        return($fieldstruct);
 }
 
-function db_create_table($name, $fields, $verbose, $action) {
+function db_create_table($name, $fields, $verbose, $action, $indexes=null) {
        global $a, $db;
 
        $r = true;
@@ -253,7 +241,13 @@ function db_create_table($name, $fields, $verbose, $action) {
                if ($sql != "")
                        $sql .= ",\n";
 
-               $sql .= "`".dbesc($fieldname)."` ".db_field_command($field);
+               $sql .= "\t`".dbesc($fieldname)."` ".db_field_command($field);
+       }
+
+       if (!is_null($indexes)) {
+               foreach ($indexes AS $indexname => $fieldnames) {
+                       $sql .= "\t".db_create_index($indexname, $fieldnames, "")."\n";
+               }
        }
 
        $sql = sprintf("CREATE TABLE IF NOT EXISTS `%s` (\n", dbesc($name)).$sql."\n) DEFAULT CHARSET=utf8";
@@ -282,7 +276,7 @@ function db_drop_index($indexname) {
        return($sql);
 }
 
-function db_create_index($indexname, $fieldnames) {
+function db_create_index($indexname, $fieldnames, $method="ADD") {
 
        if ($indexname == "PRIMARY")
                return;
@@ -298,7 +292,13 @@ function db_create_index($indexname, $fieldnames) {
                        $names .= "`".dbesc($fieldname)."`";
        }
 
-       $sql = sprintf("ADD INDEX `%s` (%s)", dbesc($indexname), $names);
+       $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);
 }
 
@@ -413,6 +413,10 @@ function db_definition() {
                                        "network" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "name" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "nick" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+                                       "location" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+                                       "about" => array("type" => "text", "not null" => "1"),
+                                       "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" => ""),
                                        "photo" => array("type" => "text", "not null" => "1"),
                                        "thumb" => array("type" => "text", "not null" => "1"),
@@ -616,6 +620,13 @@ function db_definition() {
                                        "nurl" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "photo" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "connect" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+                                       "updated" => array("type" => "datetime", "default" => "0000-00-00 00:00:00"),
+                                       "location" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+                                       "about" => array("type" => "text", "not null" => "1"),
+                                       "keywords" => array("type" => "text", "not null" => "1"),
+                                       "gender" => array("type" => "varchar(32)", "not null" => "1", "default" => ""),
+                                       "network" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+                                       "generation" => array("type" => "tinyint(3)", "not null" => "1", "default" => "0"),
                                        ),
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
@@ -765,6 +776,9 @@ function db_definition() {
                                        "last-child" => array("type" => "tinyint(1) unsigned", "not null" => "1", "default" => "1"),
                                        "mention" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
                                        "network" => array("type" => "varchar(32)", "not null" => "1", "default" => ""),
+                                       "rendered-hash" => array("type" => "varchar(32)", "not null" => "1", "default" => ""),
+                                       "rendered-html" => array("type" => "mediumtext", "not null" => "1"),
+                                       "global" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
                                        ),
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
@@ -823,6 +837,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" => "0000-00-00 00:00:00"),
                                        ),
                        "indexes" => array(
                                        "PRIMARY" => array("id"),
@@ -1176,6 +1191,10 @@ function db_definition() {
                                        "type" => array("type" => "tinyint(3) unsigned", "not null" => "1", "default" => "0"),
                                        "term" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
                                        "url" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+                                       "guid" => array("type" => "varchar(255)", "not null" => "1", "default" => ""),
+                                       "created" => 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"),
+                                       "global" => array("type" => "tinyint(1)", "not null" => "1", "default" => "0"),
                                        "aid" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0"),
                                        "uid" => array("type" => "int(10) unsigned", "not null" => "1", "default" => "0"),
                                        ),
@@ -1184,8 +1203,9 @@ function db_definition() {
                                        "oid_otype_type_term" => array("oid","otype","type","term"),
                                        "uid_term_tid" => array("uid","term","tid"),
                                        "type_term" => array("type","term"),
-                                       "uid_otype_type_term_tid" => array("uid","otype","type","term","tid"),
+                                       "uid_otype_type_term_global_created" => array("uid","otype","type","term","global","created"),
                                        "otype_type_term_tid" => array("otype","type","term","tid"),
+                                       "guid" => array("guid"),
                                        )
                        );
        $database["thread"] = array(
@@ -1247,6 +1267,8 @@ function db_definition() {
                                        "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"),
@@ -1335,10 +1357,32 @@ function dbstructure_run(&$argv, &$argc) {
                        unset($db_host, $db_user, $db_pass, $db_data);
        }
 
-       update_structure(true, true);
+       if ($argc==2) {
+               switch ($argv[1]) {
+                       case "update":
+                               update_structure(true, true);
+                               return;
+                       case "dumpsql":
+                               print_structure(db_definition());
+                               return;
+               }
+       }
+
+
+       // print help
+       echo $argv[0]." <command>\n";
+       echo "\n";
+       echo "commands:\n";
+       echo "update            update database schema\n";
+       echo "dumpsql           dump database schema\n";
+       return;
+
+
+
+
 }
 
 if (array_search(__file__,get_included_files())===0){
-       dbstructure_run($argv,$argc);
+       dbstructure_run($_SERVER["argv"],$_SERVER["argc"]);
        killme();
 }