X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FDatabase%2FDBStructure.php;h=abbac4e781795a700b10c52c95cbecf0abc0aeb2;hb=ce3b1210640ce653ef91cbf033788474c1f88b0c;hp=5374e11d575d988b5333a90f79d79e8e34627880;hpb=48a356dba20237253d1180072ed3cef83ac2e5b1;p=friendica.git diff --git a/src/Database/DBStructure.php b/src/Database/DBStructure.php index 5374e11d57..abbac4e781 100644 --- a/src/Database/DBStructure.php +++ b/src/Database/DBStructure.php @@ -12,10 +12,7 @@ use Friendica\Core\L10n; use Friendica\Core\Logger; use Friendica\Util\DateTimeFormat; -require_once 'boot.php'; require_once 'include/dba.php'; -require_once 'include/enotify.php'; -require_once 'include/text.php'; /** * @brief This class contain functions for the database management @@ -77,9 +74,9 @@ class DBStructure return L10n::t('Errors encountered performing database changes: ') . $message . EOL; } - public static function printStructure() + public static function printStructure($basePath) { - $database = self::definition(false); + $database = self::definition($basePath, false); echo "-- ------------------------------------------\n"; echo "-- " . FRIENDICA_PLATFORM . " " . FRIENDICA_VERSION . " (" . FRIENDICA_CODENAME, ")\n"; @@ -101,15 +98,15 @@ class DBStructure * * @see config/dbstructure.config.php * @param boolean $with_addons_structure Whether to tack on addons additional tables + * @param string $basePath The base path of this application * @return array * @throws Exception */ - public static function definition($with_addons_structure = true) + public static function definition($basePath, $with_addons_structure = true) { if (!self::$definition) { - $a = \Friendica\BaseObject::getApp(); - $filename = $a->getBasePath() . '/config/dbstructure.config.php'; + $filename = $basePath . '/config/dbstructure.config.php'; if (!is_readable($filename)) { throw new Exception('Missing database structure config file config/dbstructure.config.php'); @@ -220,9 +217,9 @@ class DBStructure throw new Exception("Invalid parameter 'method' in self::createIndex(): '$method'"); } - if ($fieldnames[0] == "UNIQUE") { - array_shift($fieldnames); - $method .= ' UNIQUE'; + if (in_array($fieldnames[0], ["UNIQUE", "FULLTEXT"])) { + $index_type = array_shift($fieldnames); + $method .= " " . $index_type; } $names = ""; @@ -250,14 +247,16 @@ class DBStructure /** * Updates DB structure and returns eventual errors messages * - * @param bool $verbose - * @param bool $action Whether to actually apply the update - * @param bool $install Is this the initial update during the installation? - * @param array $tables An array of the database tables - * @param array $definition An array of the definition tables + * @param string $basePath The base path of this application + * @param bool $verbose + * @param bool $action Whether to actually apply the update + * @param bool $install Is this the initial update during the installation? + * @param array $tables An array of the database tables + * @param array $definition An array of the definition tables * @return string Empty string if the update is successful, error messages otherwise + * @throws Exception */ - public static function update($verbose, $action, $install = false, array $tables = null, array $definition = null) + public static function update($basePath, $verbose, $action, $install = false, array $tables = null, array $definition = null) { if ($action && !$install) { Config::set('system', 'maintenance', 1); @@ -286,7 +285,7 @@ class DBStructure // Get the definition if (is_null($definition)) { - $definition = self::definition(); + $definition = self::definition($basePath); } // MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements @@ -407,9 +406,7 @@ class DBStructure $sql2 = self::createIndex($indexname, $fieldnames); // Fetch the "group by" fields for unique indexes - if ($fieldnames[0] == "UNIQUE") { - $group_by = self::groupBy($indexname, $fieldnames); - } + $group_by = self::groupBy($fieldnames); if ($sql2 != "") { if ($sql3 == "") { $sql3 = "ALTER" . $ignore . " TABLE `" . $temp_name . "` " . $sql2; @@ -610,8 +607,12 @@ class DBStructure if (DBA::isResult($indexes)) { foreach ($indexes AS $index) { - if ($index['Key_name'] != 'PRIMARY' && $index['Non_unique'] == '0' && !isset($indexdata[$index["Key_name"]])) { - $indexdata[$index["Key_name"]] = ['UNIQUE']; + if ($index["Key_name"] != "PRIMARY" && $index["Non_unique"] == "0" && !isset($indexdata[$index["Key_name"]])) { + $indexdata[$index["Key_name"]] = ["UNIQUE"]; + } + + if ($index["Index_type"] == "FULLTEXT" && !isset($indexdata[$index["Key_name"]])) { + $indexdata[$index["Key_name"]] = ["FULLTEXT"]; } $column = $index["Column_name"]; @@ -676,7 +677,13 @@ class DBStructure return ($sql); } - private static function groupBy($indexname, $fieldnames) + /** + * Constructs a GROUP BY clause from a UNIQUE index definition. + * + * @param array $fieldnames + * @return string + */ + private static function groupBy(array $fieldnames) { if ($fieldnames[0] != "UNIQUE") { return ""; @@ -712,7 +719,7 @@ class DBStructure * @param int $type The type of renaming (Default is Column) * * @return boolean Was the renaming successful? - * + * @throws Exception */ public static function rename($table, $columns, $type = self::RENAME_COLUMN) { @@ -772,6 +779,7 @@ class DBStructure * @param array $columns Columns to check ( Syntax: [ $col1, $col2, .. ] ) * * @return boolean Does the table exist? + * @throws Exception */ public static function existsColumn($table, $columns = []) { @@ -812,6 +820,7 @@ class DBStructure * @param string $table Table name * * @return boolean Does the table exist? + * @throws Exception */ public static function existsTable($table) { @@ -835,4 +844,18 @@ class DBStructure return $retval; } + + /** + * Returns the columns of a table + * + * @param string $table Table name + * + * @return array An array of the table columns + * @throws Exception + */ + public static function getColumns($table) + { + $stmtColumns = DBA::p("SHOW COLUMNS FROM `" . $table . "`"); + return DBA::toArray($stmtColumns); + } }