]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/DBStructure.php
Switch from dbstructure.json to dbstructure.php
[friendica.git] / src / Database / DBStructure.php
index db775b59c105bf841c604ee48f782ec5f592ff93..4e4c69b1d8bafe9ec0e6864e122271b51bf1ba70 100644 (file)
@@ -5,7 +5,7 @@
 namespace Friendica\Database;
 
 use Exception;
-use Friendica\Core\Addon;
+use Friendica\Core\Hook;
 use Friendica\Core\Config;
 use Friendica\Core\L10n;
 use Friendica\Util\DateTimeFormat;
@@ -22,6 +22,13 @@ require_once 'include/text.php';
  */
 class DBStructure
 {
+       /**
+        * Database structure definition loaded from config/dbstructure.php
+        *
+        * @var array
+        */
+       private static $definition = [];
+
        /*
         * Converts all tables from MyISAM to InnoDB
         */
@@ -77,10 +84,9 @@ class DBStructure
                                The friendica developers released update %s recently,
                                but when I tried to install it, something went terribly wrong.
                                This needs to be fixed soon and I can't do it alone. Please contact a
-                               friendica developer if you can not help me on your own. My database might be invalid."));
-                       $body = L10n::t("The error message is\n[pre]%s[/pre]");
-                       $preamble = sprintf($preamble, $update_id);
-                       $body = sprintf($body, $error_message);
+                               friendica developer if you can not help me on your own. My database might be invalid.",
+                               $update_id));
+                       $body = L10n::t("The error message is\n[pre]%s[/pre]", $error_message);
 
                        notification([
                                'uid'      => $admin['uid'],
@@ -823,43 +829,36 @@ class DBStructure
        }
 
        /**
-        * Loads the database structure definition from the /config/dbstructure.json file
-        *
-        * Expected format:
-        * "table_name": {
-        *   "comment": "meaningful table comment",
-        *   "fields": {
-        *     "field_name1": {"type": "int unsigned", "not null": "1", "extra": "auto_increment", "primary": "1", "comment": "meaningful field comment"},
-        *     "field_name2": {"type": "varchar(50)", "not null": "1", "default": "", "comment": "meaningful field comment"},
-        *   },
-        *   "indexes": {
-        *     "PRIMARY": ["field_name1"],
-        *     "name": ["UNIQUE", "field_name2"]
-        *   }
-        * }
+        * Loads the database structure definition from the config/dbstructure.php file.
         *
+        * @see config/dbstructure.php
         * @return array
         * @throws Exception
         */
-       public static function definition() {
-               $a = \Friendica\BaseObject::getApp();
+       public static function definition()
+       {
+               if (!self::$definition) {
+                       $a = \Friendica\BaseObject::getApp();
 
-               $filename = $a->getBasePath() . '/config/dbstructure.json';
+                       $filename = $a->getBasePath() . '/config/dbstructure.php';
 
-               if (!is_readable($filename)) {
-                       throw new Exception('Missing database structure config file config/dbstructure.json');
-               }
+                       if (!is_readable($filename)) {
+                               throw new Exception('Missing database structure config file config/dbstructure.php');
+                       }
 
-               $json = file_get_contents($filename);
+                       $definition = require $filename;
 
-               $database = json_decode($json, true);
+                       if (!$definition) {
+                               throw new Exception('Corrupted database structure config file config/dbstructure.php');
+                       }
 
-               if (!$database) {
-                       throw new Exception('Corrupted database structure config file config/dbstructure.json');
+                       self::$definition = $definition;
+               } else {
+                       $definition = self::$definition;
                }
 
-               Addon::callHooks('dbstructure_definition', $database);
+               Hook::callAll('dbstructure_definition', $definition);
 
-               return $database;
+               return $definition;
        }
 }