Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enh: Consolidate (is|set)(Database)Installed() to ApplicationTrait #6720

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ HumHub Changelog

1.16.0 (Unreleased)
-------------------
- Enh #6720: Consolidate `isInstalled()`, `setInstalled()`, and `setDatabaseInstalled`
- Fix #6693: `MigrateController::$migrationPathMap` stored rolling sum of migrations
- Enh #6697: Make state badge customizable
- Fix #6636: Module Manager test
Expand Down
2 changes: 2 additions & 0 deletions MIGRATE-DEV.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Version 1.16 (Unreleased)
- `\humhub\modules\content\components\ContentAddonActiveRecord::canWrite()`
- `\humhub\modules\file\models\File::canRead()` use `canView()` instead
- `\humhub\modules\content\components\ContentAddonActiveRecord::canRead()` use `canView()` instead
- `\humhub\models\Setting::isInstalled()` use `Yii::$app->isInstalled()` instead
- `\humhub\libs\BaseSettingsManager::isDatabaseInstalled()` use `Yii::$app->isDatabaseInstalled()` instead

### Type restrictions
- `\humhub\modules\comment\models\Comment` on `canDelete()`
Expand Down
66 changes: 66 additions & 0 deletions protected/humhub/components/ApplicationTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* @link https://www.humhub.org/
* @copyright Copyright (c) 2023 HumHub GmbH & Co. KG
Expand All @@ -7,7 +8,10 @@

namespace humhub\components;

use humhub\helpers\DatabaseHelper;
use humhub\interfaces\MailerInterface;
use humhub\libs\DynamicConfig;
use Yii;
use yii\helpers\Url;

trait ApplicationTrait
Expand Down Expand Up @@ -66,4 +70,66 @@ public function getMailer(): MailerInterface
{
return parent::getMailer();
}

/**
* Checks if Humhub is installed
*
* @return boolean
* @since 1.16
*/
public function isInstalled(): bool
{
return isset(Yii::$app->params['installed']) && Yii::$app->params['installed'];
}

/**
* Sets application in installed state (disables installer)
*
* @since 1.16
*/
public function setInstalled()
{
$config = DynamicConfig::load();
$config['params']['installed'] = true;
DynamicConfig::save($config);
}


/**
* Checks if settings table exists or application is not installed yet
*
* @since 1.16
*/
public function isDatabaseInstalled(bool $checkConnection = false): bool
{
$dieOnError = isset(Yii::$app->params['databaseInstalled']) && $this->params['databaseInstalled'];

if (!$checkConnection) {
return $dieOnError;
}

try {
$db = Yii::$app->db;
$db->open();
} catch (\Exception $ex) {
if ($dieOnError) {
DatabaseHelper::handleConnectionErrors($ex);
}
return false;
}

return Yii::$app->params['databaseInstalled'] = in_array('setting', $db->schema->getTableNames());
}

/**
* Sets the application database in installed state
*
* @since 1.16
*/
public function setDatabaseInstalled()
{
$config = DynamicConfig::load();
$config['params']['databaseInstalled'] = true;
DynamicConfig::save($config);
}
}
7 changes: 4 additions & 3 deletions protected/humhub/components/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use humhub\components\access\ControllerAccess;
use humhub\components\access\StrictAccess;
use humhub\components\behaviors\AccessControl;
use humhub\models\Setting;
use humhub\modules\user\services\IsOnlineService;
use Yii;
use yii\helpers\Html;
Expand All @@ -28,7 +27,6 @@
*/
class Controller extends \yii\web\Controller
{

/**
* @event \yii\base\Event an event raised on init a controller.
*/
Expand Down Expand Up @@ -121,6 +119,7 @@ public function renderAjaxContent($content)
* Renders a string as Ajax including assets without end page so it can be called several times.
*
* @param string $content
*
* @return string Rendered content
*/
public function renderAjaxPartial(string $content): string
Expand All @@ -132,6 +131,7 @@ public function renderAjaxPartial(string $content): string
* Renders a static string by applying the layouts (sublayout + layout.
*
* @param string $content the static string being rendered
*
* @return string the rendering result of the layout with the given static string as the `$content` variable.
* If the layout is disabled, the string will be returned back.
*
Expand Down Expand Up @@ -223,7 +223,7 @@ public function beforeAction($action)
if (!Yii::$app->request->isAjax || Yii::$app->request->isPjax) {
$this->setJsViewStatus();

if (Setting::isInstalled()) {
if (Yii::$app->isInstalled()) {
// Update "is online" status ony on full page loads
(new IsOnlineService(Yii::$app->user->identity))->updateStatus();
}
Expand Down Expand Up @@ -313,6 +313,7 @@ public function setJsViewStatus()
* Check if action cannot be intercepted
*
* @param string|null $actionId , NULL - to use current action
*
* @return bool
* @since 1.9
*/
Expand Down
30 changes: 26 additions & 4 deletions protected/humhub/components/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace humhub\components;

use humhub\models\Setting;
use humhub\modules\like\activities\Liked;
use humhub\modules\like\models\Like;
use Throwable;
Expand Down Expand Up @@ -76,6 +75,7 @@ public function down()
* Helper function for self::up() and self::down()
*
* @param array $action
*
* @return bool|null
* @since 1.15.0
*/
Expand Down Expand Up @@ -127,6 +127,7 @@ protected function saveUpDown(array $action): ?bool
* @param string $table
* @param $columns
* @param string|null $options
*
* @return bool indicates if the table has been created
* @see static::createTable()
* @noinspection PhpMissingReturnTypeInspection
Expand All @@ -147,6 +148,7 @@ protected function safeCreateTable(string $table, $columns, ?string $options = n

/**
* @param string $table
*
* @return bool indicates if the table has been dropped
* @see static::dropTable()
* @noinspection PhpUnused
Expand All @@ -171,6 +173,7 @@ protected function safeDropTable(string $table)
*
* @param string $column
* @param string $table
*
* @return bool
* @since 1.9.1
*/
Expand Down Expand Up @@ -226,6 +229,7 @@ protected function safeAddColumn(string $table, string $column, $type)
*
* @param string $index
* @param string $table
*
* @return bool
* @throws Exception
* @since 1.9.1
Expand All @@ -242,6 +246,7 @@ protected function indexExists(string $index, string $table): bool
*
* @param string $index
* @param string $table
*
* @return bool
* @throws Exception
* @since 1.9.1
Expand All @@ -263,6 +268,7 @@ protected function foreignIndexExists(string $index, string $table): bool
* @param string $table
* @param string|array $columns
* @param bool $unique
*
* @return bool indicates if the index has been created
* @throws Exception
* @since 1.9.1
Expand All @@ -288,6 +294,7 @@ protected function safeCreateIndex(string $index, string $table, $columns, bool
*
* @param string $index
* @param string $table
*
* @return bool indicates if the index has been dropped
* @throws Exception
* @since 1.9.1
Expand Down Expand Up @@ -315,6 +322,7 @@ protected function safeDropIndex(string $index, string $table)
* @param string $index
* @param string $table
* @param string|array $columns
*
* @return bool indicates if key has been added
* @throws Exception
* @since 1.9.1
Expand Down Expand Up @@ -372,6 +380,7 @@ protected function safeDropPrimaryKey(string $index, string $table)
* @param string|array $refColumns
* @param string|null $delete
* @param string|null $update
*
* @return bool indicates if key has been added
* @throws Exception
* @since 1.9.1
Expand All @@ -397,6 +406,7 @@ protected function safeAddForeignKey(string $index, string $table, $columns, str
*
* @param string $index
* @param string $table
*
* @return bool indicates if key has been dropped
* @throws Exception
* @since 1.9.1
Expand Down Expand Up @@ -483,15 +493,15 @@ public function safeAddForeignKeyCreatedBy()
public function integerReferenceKey(): ColumnSchemaBuilder
{
return $this->integer(11)
->notNull()
;
->notNull();
}

/**
* Returns the field configuration for a timestamp field that does not get automatically updated by mysql in case it
* being the first timestamp column in the table.
*
* @param $precision
*
* @return ColumnSchemaBuilder
* @since 1.15
* @see https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html
Expand All @@ -516,6 +526,7 @@ public function timestampWithoutAutoUpdate($precision = null): ColumnSchemaBuild
*
* @param string $oldClass
* @param string $newClass
*
* @throws Exception
*/
protected function renameClass(string $oldClass, string $newClass): void
Expand Down Expand Up @@ -561,6 +572,7 @@ protected function renameClass(string $oldClass, string $newClass): void
* @param array|string $condition the conditions that will be put in the WHERE part. Please
* refer to [[Query::where()]] on how to specify conditions.
* @param array|Traversable $params the parameters to be bound to the query.
*
* @throws Exception
*/
public function updateSilent(string $table, $columns, $condition = '', $params = []): void
Expand All @@ -571,8 +583,10 @@ public function updateSilent(string $table, $columns, $condition = '', $params =
/**
* Creates and executes an INSERT SQL statement without any output
* The method will properly escape the column names, and bind the values to be inserted.
*
* @param string $table the table that new rows will be inserted into.
* @param array|Traversable $columns the column data (name => value) to be inserted into the table.
*
* @throws Exception
*/
public function insertSilent(string $table, $columns): void
Expand All @@ -588,14 +602,16 @@ public function insertSilent(string $table, $columns): void
*/
protected function isInitialInstallation(): bool
{
return (!Setting::isInstalled());
return (!Yii::$app->isInstalled());
}

/**
* Get data from database dsn config
*
* @since 1.9.3
*
* @param string $name 'host', 'port', 'dbname'
*
* @return string|null
*/
private function getDsnAttribute(string $name): ?string
Expand All @@ -608,6 +624,7 @@ private function getDsnAttribute(string $name): ?string
/**
* @param string $message Message to be logged
* @param array $params Parameters to translate in $message
*
* @return void
* @since 1.15.0
*/
Expand All @@ -619,6 +636,7 @@ protected function logError(string $message, array $params = []): void
/**
* @param string $message Message to be logged
* @param array $params Parameters to translate in $message
*
* @return void
* @since 1.15.0
*/
Expand All @@ -630,6 +648,7 @@ protected function logWarning(string $message, array $params = []): void
/**
* @param string $message Message to be logged
* @param array $params Parameters to translate in $message
*
* @return void
* @since 1.15.0
* @noinspection PhpUnused
Expand All @@ -642,6 +661,7 @@ protected function logInfo(string $message, array $params = []): void
/**
* @param string $message Message to be logged
* @param array $params Parameters to translate in $message
*
* @return void
* @since 1.15.0
*/
Expand All @@ -656,6 +676,7 @@ protected function logDebug(string $message, array $params = []): void
*
* @param string $message Message to be logged
* @param array $params Parameters to translate in $message
*
* @return void
* @since 1.15.0
*/
Expand All @@ -680,6 +701,7 @@ protected function logTranslation(string $message, array $params = []): string
*
* @param Throwable $e The Throwable to be logged
* @param string $method The Method that was running
*
* @since 1.15.0
*/
protected function logException(Throwable $e, string $method): void
Expand Down