]> git.mxchange.org Git - friendica.git/blobdiff - include/dba.php
Add Install Mode
[friendica.git] / include / dba.php
index 208cf5812140be6d4953f929a935140b34795fdb..1d3b432141d525e015539b5b3a6864f5153749ca 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 
+use Friendica\App;
 use Friendica\Core\L10n;
 use Friendica\Core\System;
 use Friendica\Database\DBM;
@@ -24,7 +25,7 @@ class dba {
        private static $in_transaction = false;
        private static $relation = [];
 
-       public static function connect($serveraddr, $user, $pass, $db, $install = false) {
+       public static function connect($serveraddr, $user, $pass, $db) {
                if (!is_null(self::$db)) {
                        return true;
                }
@@ -51,8 +52,9 @@ class dba {
                        return false;
                }
 
-               if ($install) {
-                       if (strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) {
+               if ($a->mode == App::MODE_INSTALL) {
+                       // server has to be a non-empty string that is not 'localhost' and not an IP
+                       if (strlen($server) && ($server !== 'localhost') && filter_var($server, FILTER_VALIDATE_IP) === false) {
                                if (! dns_get_record($server, DNS_A + DNS_CNAME)) {
                                        self::$error = L10n::t('Cannot locate DNS info for database server \'%s\'', $server);
                                        return false;
@@ -92,6 +94,7 @@ class dba {
 
                // No suitable SQL driver was found.
                if (!self::$connected) {
+                       self::$driver = null;
                        self::$db = null;
                }
                $a->save_timestamp($stamp1, "network");
@@ -857,12 +860,15 @@ class dba {
         *
         * @param string  $table       Table name
         * @param array   $conditions  Field condition(s)
+        * @param array   $options
+        *                - cascade: If true we delete records in other tables that depend on the one we're deleting through
+        *                           relations (default: true)
         * @param boolean $in_process  Internal use: Only do a commit after the last delete
         * @param array   $callstack   Internal use: prevent endless loops
         *
         * @return boolean|array was the delete successful? When $in_process is set: deletion data
         */
-       public static function delete($table, array $conditions, $in_process = false, array &$callstack = [])
+       public static function delete($table, array $conditions, array $options = [], $in_process = false, array &$callstack = [])
        {
                if (empty($table) || empty($conditions)) {
                        logger('Table and conditions have to be set');
@@ -885,13 +891,15 @@ class dba {
 
                $commands[$key] = ['table' => $table, 'conditions' => $conditions];
 
+               $cascade = defaults($options, 'cascade', true);
+
                // To speed up the whole process we cache the table relations
-               if (count(self::$relation) == 0) {
+               if ($cascade && count(self::$relation) == 0) {
                        self::buildRelationData();
                }
 
                // Is there a relation entry for the table?
-               if (isset(self::$relation[$table])) {
+               if ($cascade && isset(self::$relation[$table])) {
                        // We only allow a simple "one field" relation.
                        $field = array_keys(self::$relation[$table])[0];
                        $rel_def = array_values(self::$relation[$table])[0];
@@ -904,7 +912,7 @@ class dba {
                        if ((count($conditions) == 1) && ($field == array_keys($conditions)[0])) {
                                foreach ($rel_def AS $rel_table => $rel_fields) {
                                        foreach ($rel_fields AS $rel_field) {
-                                               $retval = self::delete($rel_table, [$rel_field => array_values($conditions)[0]], true, $callstack);
+                                               $retval = self::delete($rel_table, [$rel_field => array_values($conditions)[0]], $options, true, $callstack);
                                                $commands = array_merge($commands, $retval);
                                        }
                                }
@@ -918,7 +926,7 @@ class dba {
 
                                while ($row = self::fetch($data)) {
                                        // Now we accumulate the delete commands
-                                       $retval = self::delete($table, [$field => $row[$field]], true, $callstack);
+                                       $retval = self::delete($table, [$field => $row[$field]], $options, true, $callstack);
                                        $commands = array_merge($commands, $retval);
                                }
 
@@ -965,7 +973,7 @@ class dba {
                                        // Split the SQL queries in chunks of 100 values
                                        // We do the $i stuff here to make the code better readable
                                        $i = $counter[$key_table][$key_condition];
-                                       if (count($compacted[$key_table][$key_condition][$i]) > 100) {
+                                       if (isset($compacted[$key_table][$key_condition][$i]) && count($compacted[$key_table][$key_condition][$i]) > 100) {
                                                ++$i;
                                        }