Commit 12960541 authored by Henning Leutz's avatar Henning Leutz 🥋

feat: #1009

parent 872bb1aa
<?php
/**
* cache purging
*/
QUI::$Ajax->registerFunction(
'ajax_system_cache_mongoAvailable',
function () {
try {
QUI::getPackage('mongodb/mongodb');
} catch (QUI\Exception $Exception) {
return false;
}
return \class_exists('\MongoDB\Client');
},
false,
'Permission::checkSU'
);
<?php
/**
* mongoDB data check
*/
use QUI\Cache\QuiqqerMongoDriver;
QUI::$Ajax->registerFunction(
'ajax_system_cache_mongoCheck',
function ($host, $database, $collection, $username, $password) {
try {
QUI::getPackage('mongodb/mongodb');
} catch (QUI\Exception $Exception) {
throw new QUI\Exception('MongoDB Client not installed');
}
if (!\class_exists('\MongoDB\Client')) {
throw new QUI\Exception('MongoDB Client not installed');
}
// database server
if (empty($host)) {
$host = 'localhost';
}
if (empty($database)) {
$database = 'local';
}
if (empty($collection)) {
$collection = 'quiqqer.longterm';
}
if (\strpos($host, 'mongodb://') === false) {
$host = 'mongodb://'.$host;
}
if (!empty($username) && !empty($password)) {
$Client = new \MongoDB\Client($host, [
"username" => $username,
"password" => $password
]);
} else {
$Client = new \MongoDB\Client($host);
}
$CacheDriver = new QuiqqerMongoDriver([
'mongo' => $Client,
'database' => $database,
'collection' => $collection
]);
$CacheDriver->storeData(['db-test'], 1, false);
$result = $CacheDriver->getData(['db-test']);
if ($result['data'] === 1) {
QUI::getMessagesHandler()->addSuccess(
QUI::getLocale()->get('quiqqer/quiqqer', 'message.quiqqer.mongo.success')
);
} else {
QUI::getMessagesHandler()->addError(
QUI::getLocale()->get('quiqqer/quiqqer', 'message.quiqqer.mongo.error')
);
}
},
['host', 'database', 'collection', 'username', 'password'],
'Permission::checkSU'
);
......@@ -94,7 +94,8 @@ define('controls/cache/General', [
* event: on type change
*/
$onTypeChange: function () {
var Elm = this.getElm(),
var self = this,
Elm = this.getElm(),
CacheType = Elm.querySelector('[name="general.cacheType"]'),
RedisTable = Elm.querySelector('[name="general.redis"]').getParent('table'),
APCTable = Elm.querySelector('[name="apc.namespace"]').getParent('table'),
......@@ -123,6 +124,54 @@ define('controls/cache/General', [
case 'mongo':
MongoTable.setStyle('display', null);
CacheType.disabled = true;
// availability check
this.checkMongoAvailability().then(function (availability) {
CacheType.disabled = false;
MongoTable.getElements('.mongo-error-message').destroy();
MongoTable.getElements('.mongo-check-button').destroy();
if (!availability) {
var RowMessage = new Element('tr', {
'class': 'mongo-error-message',
html : '<td>' +
'<div class="messages-message message-error">' +
QUILocale.get('quiqqer/quiqqer', 'message.quiqqer.mongo.missing') +
'</div>' +
'</td>'
});
RowMessage.inject(
MongoTable.getElement('tbody'),
'top'
);
return;
}
new Element('tr', {
'class': 'mongo-check-button',
html : '<td>' +
'<button class="qui-button" style="float: right">' +
QUILocale.get('quiqqer/quiqqer', 'message.quiqqer.mongo.button') +
'</button>' +
'</td>'
}).inject(MongoTable.getElement('tbody'));
var Button = MongoTable.getElement('button');
Button.addEvent('click', function () {
Button.disabled = true;
Button.set('html', '<span class="fa fa-spinner fa-spin"></span>');
self.checkMongoDB().then(function () {
Button.disabled = false;
Button.set('html', QUILocale.get('quiqqer/quiqqer', 'message.quiqqer.mongo.button'));
});
});
});
break;
default:
......@@ -165,6 +214,37 @@ define('controls/cache/General', [
}, {
server: this.getElm().querySelector('[name="general.redis"]').value
});
},
/**
* Checks, if mongoDB can be used
*
* @return {Promise}
*/
checkMongoAvailability: function () {
return new Promise(function (resolve) {
QUIAjax.get('ajax_system_cache_mongoAvailable', resolve);
});
},
/**
* Checks, if mongoDB can be used
*
* @return {Promise}
*/
checkMongoDB: function () {
var Elm = this.getElm(),
Form = Elm.querySelector('[name="mongo.host"]').getParent('form');
return new Promise(function (resolve) {
QUIAjax.get('ajax_system_cache_mongoCheck', resolve, {
'host' : Form.elements['mongo.host'].value,
'database' : Form.elements['mongo.database'].value,
'collection': Form.elements['mongo.collection'].value,
'username' : Form.elements['mongo.username'].value,
'password' : Form.elements['mongo.password'].value
});
});
}
});
});
......@@ -93,7 +93,8 @@ define('controls/cache/LongTime', [
* event: on type change
*/
$onTypeChange: function () {
var Elm = this.getElm(),
var self = this,
Elm = this.getElm(),
CacheType = Elm.querySelector('[name="longtime.type"]'),
RedisTable = Elm.querySelector('[name="longtime.redis_server"]').getParent('table'),
FileTable = Elm.querySelector('[name="longtime.file_path"]').getParent('table'),
......@@ -106,6 +107,54 @@ define('controls/cache/LongTime', [
switch (CacheType.value) {
case 'mongo':
MongoTable.setStyle('display', null);
CacheType.disabled = true;
// availability check
this.checkMongoAvailability().then(function (availability) {
CacheType.disabled = false;
MongoTable.getElements('.mongo-error-message').destroy();
MongoTable.getElements('.mongo-check-button').destroy();
if (!availability) {
var RowMessage = new Element('tr', {
'class': 'mongo-error-message',
html : '<td>' +
'<div class="messages-message message-error">' +
QUILocale.get('quiqqer/quiqqer', 'message.quiqqer.mongo.missing') +
'</div>' +
'</td>'
});
RowMessage.inject(
MongoTable.getElement('tbody'),
'top'
);
return;
}
new Element('tr', {
'class': 'mongo-check-button',
html : '<td>' +
'<button class="qui-button" style="float: right">' +
QUILocale.get('quiqqer/quiqqer', 'message.quiqqer.mongo.button') +
'</button>' +
'</td>'
}).inject(MongoTable.getElement('tbody'));
var Button = MongoTable.getElement('button');
Button.addEvent('click', function () {
Button.disabled = true;
Button.set('html', '<span class="fa fa-spinner fa-spin"></span>');
self.checkMongoDB().then(function () {
Button.disabled = false;
Button.set('html', QUILocale.get('quiqqer/quiqqer', 'message.quiqqer.mongo.button'));
});
});
});
break;
case 'redis':
......@@ -185,6 +234,43 @@ define('controls/cache/LongTime', [
}
}
}).open();
},
/**
* Checks, if mongoDB can be used
*
* @return {Promise}
*/
checkMongoAvailability: function () {
return new Promise(function (resolve) {
QUIAjax.get('ajax_system_cache_mongoAvailable', resolve);
});
},
/**
* Checks, if mongoDB can be used
*
* @return {Promise}
*/
checkMongoDB: function () {
var Elm = this.getElm(),
Form = Elm.querySelector('[name="longtime.mongo_host"]').getParent('form');
var collection = 'quiqqer.store';
if (Form.elements['longtime.mongo_collection'].value !== '') {
collection = Form.elements['longtime.mongo_collection'].value;
}
return new Promise(function (resolve) {
QUIAjax.get('ajax_system_cache_mongoCheck', resolve, {
'host' : Form.elements['longtime.mongo_host'].value,
'database' : Form.elements['longtime.mongo_database'].value,
'collection': collection,
'username' : Form.elements['longtime.mongo_username'].value,
'password' : Form.elements['longtime.mongo_password'].value
});
});
}
});
});
......@@ -205,7 +205,8 @@ class LongTermCache
if (!class_exists('\MongoDB\Client')) {
QUI\System\Log::write(
'Mongo DB Driver not found.
Please install MongoDB\Client (php MongoDB extension) or don\'t use MongoDB as long term cache',
Please install MongoDB\Client (php MongoDB extension) and the mongodb/mongodb package.
Otherwise don\'t use MongoDB as long term cache',
QUI\System\Log::LEVEL_ALERT
);
} else {
......
......@@ -356,10 +356,11 @@ class Manager
break;
case 'mongo':
if (!class_exists('\MongoDB\Client')) {
if (!\class_exists('\MongoDB\Client')) {
QUI\System\Log::write(
'Mongo DB Driver not found.
Please install MongoDB\Client (php MongoDB extension) or don\'t use MongoDB as long term cache',
Please install MongoDB\Client (php MongoDB extension) and the mongodb/mongodb package.
Otherwise don\'t use MongoDB as caching method',
QUI\System\Log::LEVEL_ALERT
);
} else {
......
......@@ -3271,6 +3271,21 @@
<locale name="quiqqer.settings.mongo.password">
<de><![CDATA[Passwort]]></de>
</locale>
<locale name="message.quiqqer.mongo.missing">
<de><![CDATA[
MongoDB ist nicht verfügbar für dein System. Bitte installiere das mongodb/mongodb Paket und
zusätzlich die PHP MongoDB Erweiterung (https://www.php.net/manual/de/mongodb.setup.php).
]]></de>
</locale>
<locale name="message.quiqqer.mongo.button">
<de><![CDATA[MongoDB Daten testen]]></de>
</locale>
<locale name="message.quiqqer.mongo.success">
<de><![CDATA[MongoDB konnte erfolgreich genutzt werden]]></de>
</locale>
<locale name="message.quiqqer.mongo.error">
<de><![CDATA[Ein Fehler ist aufgetreten. Daten konnten nicht in die MongoDB gespeichert werden.]]></de>
</locale>
<locale name="quiqqer.settings.gui">
<de><![CDATA[Oberfläche (GUI)]]></de>
......
......@@ -2687,7 +2687,21 @@ You can also ignore the console user permissions check with '--ignore-file-permi
<locale name="quiqqer.settings.mongo.password">
<en><![CDATA[Password]]></en>
</locale>
<locale name="message.quiqqer.mongo.missing">
<en><![CDATA[
MongoDB is not available for your system. Please install the mongodb/mongodb package
and additionally the PHP MongoDB extension (https://www.php.net/manual/de/mongodb.setup.php).
]]></en>
</locale>
<locale name="message.quiqqer.mongo.button">
<en><![CDATA[Testing MongoDB data]]></en>
</locale>
<locale name="message.quiqqer.mongo.success">
<en><![CDATA[MongoDB could be used successfully]]></en>
</locale>
<locale name="message.quiqqer.mongo.error">
<en><![CDATA[An error has occurred. Data could not be saved to the MongoDB.]]></en>
</locale>
<locale name="quiqqer.settings.cache.quiqqer.title">
<en><![CDATA[QUIQQER System Cache]]></en>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment