From 7e6687132c1c56a5fa44d7523af199a8ae3a707a Mon Sep 17 00:00:00 2001 From: Tobias Matthaiou Date: Wed, 6 Nov 2019 14:44:58 +0100 Subject: [PATCH] Status display of the DB how many queries were processed. (SHOW STATUS) --- src/AutoInstallSmaryPlugin.php | 57 +++++++++++++++++++++++++++ src/OxidUtilsView.php | 24 +++++++++++ src/Smarty/function.tm_sql_status.php | 55 ++++++++++++++++++++++++++ src/functions.php | 2 + 4 files changed, 138 insertions(+) create mode 100644 src/AutoInstallSmaryPlugin.php create mode 100644 src/OxidUtilsView.php create mode 100644 src/Smarty/function.tm_sql_status.php diff --git a/src/AutoInstallSmaryPlugin.php b/src/AutoInstallSmaryPlugin.php new file mode 100644 index 0000000..d19a782 --- /dev/null +++ b/src/AutoInstallSmaryPlugin.php @@ -0,0 +1,57 @@ + + * Date: 2019-08-20 + * Time: 21:33 + */ + +namespace tm\oxid\sql\logger; + +/** + * Class AutoInstallSmaryPlugin + */ +class AutoInstallSmaryPlugin +{ + public function runInstall() + { + $oxideshop_ce = new \SplFileInfo(__DIR__ . '/../../../oxid-esales/oxideshop-ce/source/Core/Smarty/Plugin'); + $smartyPlugin = new \SplFileInfo(__DIR__ . '/Smarty/function.tm_sql_status.php'); + + if ($oxideshop_ce->isDir()) { + + $target = new \SplFileInfo($oxideshop_ce->getRealPath() . '/' . $smartyPlugin->getBasename()); + + if ($target->isFile() && $this->isSameFile($target, $smartyPlugin)) { + return; + } + + $this->createHardLink($smartyPlugin, $target); + + OxidUtilsView::clearSmarty(); + } + } + + /** + * @param \SplFileInfo $target + * @param \SplFileInfo $ + * @return bool + */ + protected function isSameFile(\SplFileInfo $target, \SplFileInfo $smartyPlugin) + { + return @md5_file($target->getPathname()) == @md5_file($smartyPlugin->getRealPath()); + } + + /** + * @param \SplFileInfo $smarty_func_tm_sql_status + * @param \SplFileInfo $target + */ + protected function createHardLink(\SplFileInfo $smartyPlugin, \SplFileInfo $target) + { + if ($target->isFile()) { + @unlink($target->getPathname()); + } + + link($smartyPlugin->getPathname(), $target->getPathname()); + } +} + diff --git a/src/OxidUtilsView.php b/src/OxidUtilsView.php new file mode 100644 index 0000000..2d50f6e --- /dev/null +++ b/src/OxidUtilsView.php @@ -0,0 +1,24 @@ + + * Date: 2019-08-20 + * Time: 21:33 + */ + +namespace tm\oxid\sql\logger; + +/** + * Class OxidUtilsView + * @package tm\oxid\sql\logger + */ +class OxidUtilsView extends \OxidEsales\Eshop\Core\UtilsView +{ + + /** + * Removes existing Smarty instance + */ + public static function clearSmarty() + { + \OxidEsales\Eshop\Core\UtilsView::$_oSmarty = null; + } +} diff --git a/src/Smarty/function.tm_sql_status.php b/src/Smarty/function.tm_sql_status.php new file mode 100644 index 0000000..433bc98 --- /dev/null +++ b/src/Smarty/function.tm_sql_status.php @@ -0,0 +1,55 @@ +blSQLStatusBox = true +* ------------------------------------------------------------- +*/ +function smarty_function_tm_sql_status($aParams, &$smarty) +{ + $myConfig = \OxidEsales\Eshop\Core\Registry::getConfig(); + + // muss in config.inc.php gesetzt sein + + $box = $myConfig->getConfigParam('blSQLStatusBox'); + + if ($box == false) { + return ''; + } + + $db = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC); + $query = "SHOW STATUS WHERE Variable_name IN ( 'Com_select', 'Com_update', 'Com_insert', 'Com_delete' )"; + + $iSelects = $iDeletes = $iInserts = $iUpdates = 0; + $rows = $db->getAll($query); + foreach ($rows as $row) { + switch ($row['Variable_name']) { + case 'Com_select': + $iSelects = (int)$row['Value']; + break; + case 'Com_update': + $iUpdates = (int)$row['Value']; + break; + case 'Com_insert': + $iInserts = (int)$row['Value']; + break; + case 'Com_delete': + $iDeletes = (int)$row['Value']; + break; + default: + break; + } + } + + $iSum = $iSelects + $iDeletes + $iInserts + $iUpdates; + $sTable = ''; + $sTable .= ''; + $sTable .= ""; + $sTable .= '
All: $iSum SELECT: $iSelects UPDATE: $iUpdates INSERT: $iInserts DELETE: $iDeletes
'; + + return $sTable; +} diff --git a/src/functions.php b/src/functions.php index 5c2dec9..a5b4166 100644 --- a/src/functions.php +++ b/src/functions.php @@ -12,3 +12,5 @@ function StartSQLLog() { function StopSQLLog() { \tm\oxid\sql\logger\OxidEsalesDatabase::disableLogger(); } + +(new tm\oxid\sql\logger\AutoInstallSmaryPlugin())->runInstall();