[!!!][TASK] Migrate syslog lowlevel command to Symfony Console 56/48756/2
authorBenni Mack <benni@typo3.org>
Thu, 30 Jun 2016 15:40:25 +0000 (17:40 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Thu, 7 Jul 2016 16:24:46 +0000 (18:24 +0200)
The lowlevel cleaner syslog command is migrated to a Symfony
Command to show the latest sys_log entries on the command line.

The new command is called via

./typo3/sysext/core/bin/typo3 syslog:list

instead of the old way using

./typo3/cli_dispatch.phpsh lowlevel_cleaner syslog -r

Resolves: #76891
Releases: master
Change-Id: I594ecef186d5a2d10e6196c843b6e37207d0b8d3
Reviewed-on: https://review.typo3.org/48756
Tested-by: Bamboo TYPO3com <info@typo3.com>
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/core/Documentation/Changelog/master/Breaking-76891-SyslogLowlevelCommand.rst [new file with mode: 0644]
typo3/sysext/lowlevel/Classes/Command/ListSysLogCommand.php [new file with mode: 0644]
typo3/sysext/lowlevel/Classes/SyslogCommand.php [deleted file]
typo3/sysext/lowlevel/Configuration/Commands.php [new file with mode: 0644]
typo3/sysext/lowlevel/ext_localconf.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Breaking-76891-SyslogLowlevelCommand.rst b/typo3/sysext/core/Documentation/Changelog/master/Breaking-76891-SyslogLowlevelCommand.rst
new file mode 100644 (file)
index 0000000..eb15126
--- /dev/null
@@ -0,0 +1,22 @@
+==========================================
+Breaking: #76891 - syslog lowlevel command
+==========================================
+
+Description
+===========
+
+The lowlevel cleaner syslog command is migrated to a Symfony Command to show the latest sys_log entries on the command line.
+
+The new command is called via ``./typo3/sysext/core/bin/typo3 syslog:list``.
+
+
+Impact
+======
+
+Calling ``./typo3/cli_dispatch.phpsh lowlevel_cleaner syslog -r`` will not show the expected output anymore as before.
+
+
+Migration
+=========
+
+Use ``./typo3/sysext/core/bin/typo3 syslog:list`` with the optional verbose parameter instead.
\ No newline at end of file
diff --git a/typo3/sysext/lowlevel/Classes/Command/ListSysLogCommand.php b/typo3/sysext/lowlevel/Classes/Command/ListSysLogCommand.php
new file mode 100644 (file)
index 0000000..79db0bd
--- /dev/null
@@ -0,0 +1,115 @@
+<?php
+namespace TYPO3\CMS\Lowlevel\Command;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Symfony\Component\Console\Style\SymfonyStyle;
+use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Database\ConnectionPool;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+
+/**
+ * Lists all sys_log entries from the last 24 hours by default
+ * This is the most basic and can be useful for nightly check test reports.
+ */
+class ListSysLogCommand extends Command
+{
+
+    /**
+     * Configure the command by defining the name, options and arguments
+     */
+    public function configure()
+    {
+        $this->setDescription('Show entries from the sys_log database table of the last 24 hours.');
+        $this->setHelp('Prints a list of recent sys_log entries.' . LF . 'If you want to get more detailed information, use the --verbose option.');
+    }
+
+    /**
+     * Executes the command for showing sys_log entries
+     *
+     * @param InputInterface $input
+     * @param OutputInterface $output
+     *
+     * @return void
+     */
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $io = new SymfonyStyle($input, $output);
+        $io->title($this->getDescription());
+        $showDetails = $output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL;
+
+        $tableHeaders = [
+            'Log ID',
+            'Date & Time',
+            'User ID',
+            'Message'
+        ];
+        if ($showDetails) {
+            $tableHeaders[] = 'Details';
+        }
+
+        // Initialize result array
+        $content = [];
+
+        // Select DB relations from reference table
+        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('sys_log');
+        $rowIterator = $queryBuilder
+            ->select('*')
+            ->from('sys_log')
+            ->where(
+                $queryBuilder->expr()->gt('tstamp', $GLOBALS['EXEC_TIME'] - 24 * 3600)
+            )
+            ->orderBy('tstamp', 'DESC')
+            ->execute();
+
+        while ($row = $rowIterator->fetch()) {
+            $logData = unserialize($row['log_data']);
+            $userInformation = $row['userid'];
+            if (!empty($logData['originalUser'])) {
+                $userInformation .= ' via ' . $logData['originalUser'];
+            }
+
+            $result = [
+                $row['uid'],
+                BackendUtility::datetime($row['tstamp']),
+                $userInformation,
+                sprintf($row['details'], $logData[0], $logData[1], $logData[2], $logData[3], $logData[4], $logData[5])
+            ];
+
+            if ($showDetails) {
+                $result[] = str_replace('; ', LF, GeneralUtility::arrayToLogString($row, [
+                    'uid',
+                    'userid',
+                    'action',
+                    'recuid',
+                    'tablename',
+                    'recpid',
+                    'error',
+                    'tstamp',
+                    'type',
+                    'details_nr',
+                    'IP',
+                    'event_pid',
+                    'NEWid',
+                    'workspace'
+                ]));
+            }
+            $content[] = $result;
+        }
+        $io->table($tableHeaders, $content);
+    }
+}
diff --git a/typo3/sysext/lowlevel/Classes/SyslogCommand.php b/typo3/sysext/lowlevel/Classes/SyslogCommand.php
deleted file mode 100644 (file)
index f07871a..0000000
+++ /dev/null
@@ -1,76 +0,0 @@
-<?php
-namespace TYPO3\CMS\Lowlevel;
-
-/*
- * This file is part of the TYPO3 CMS project.
- *
- * It is free software; you can redistribute it and/or modify it under
- * the terms of the GNU General Public License, either version 2
- * of the License, or any later version.
- *
- * For the full copyright and license information, please read the
- * LICENSE.txt file that was distributed with this source code.
- *
- * The TYPO3 project - inspiring people to share!
- */
-
-/**
- * syslog
- */
-class SyslogCommand extends CleanerCommand
-{
-    /**
-     * Constructor
-     */
-    public function __construct()
-    {
-        parent::__construct();
-        $this->cli_help['name'] = 'syslog -- Show entries from syslog';
-        $this->cli_help['description'] = trim('
-Showing last 25 hour entries from the syslog. More features pending. This is the most basic and can be useful for nightly check test reports.
-');
-        $this->cli_help['examples'] = '';
-    }
-
-    /**
-     * Find syslog
-     *
-     * @return array
-     */
-    public function main()
-    {
-        // Initialize result array:
-        $resultArray = array(
-            'message' => $this->cli_help['name'] . LF . LF . $this->cli_help['description'],
-            'headers' => array(
-                'listing' => array('', '', 1),
-                'allDetails' => array('', '', 0)
-            ),
-            'listing' => array(),
-            'allDetails' => array()
-        );
-        $rows = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'sys_log', 'tstamp>' . ($GLOBALS['EXEC_TIME'] - 25 * 3600));
-        foreach ($rows as $r) {
-            $l = unserialize($r['log_data']);
-            $userInformation = $r['userid'];
-            if (!empty($l['originalUser'])) {
-                $userInformation .= ' via ' . $l['originalUser'];
-            }
-            $explained = '#' . $r['uid'] . ' ' . \TYPO3\CMS\Backend\Utility\BackendUtility::datetime($r['tstamp']) . ' USER[' . $userInformation . ']: ' . sprintf($r['details'], $l[0], $l[1], $l[2], $l[3], $l[4], $l[5]);
-            $resultArray['listing'][$r['uid']] = $explained;
-            $resultArray['allDetails'][$r['uid']] = array($explained, \TYPO3\CMS\Core\Utility\GeneralUtility::arrayToLogString($r, 'uid,userid,action,recuid,tablename,recpid,error,tstamp,type,details_nr,IP,event_pid,NEWid,workspace'));
-        }
-        return $resultArray;
-    }
-
-    /**
-     * Mandatory autofix function
-     * Will run auto-fix on the result array. Echos status during processing.
-     *
-     * @param array Result array from main() function
-     * @return void
-     */
-    public function main_autoFix($resultArray)
-    {
-    }
-}
diff --git a/typo3/sysext/lowlevel/Configuration/Commands.php b/typo3/sysext/lowlevel/Configuration/Commands.php
new file mode 100644 (file)
index 0000000..f65afa0
--- /dev/null
@@ -0,0 +1,13 @@
+<?php
+/**
+ * Commands to be executed by typo3, where the key of the array
+ * is the name of the command (to be called as the first argument after typo3).
+ * Required parameter is the "class" of the command which needs to be a subclass
+ * of Symfony/Console/Command. An optional parameter is "user" that logs in
+ * a Backend user via CLI.
+ */
+return [
+    'syslog:list' => [
+        'class' => \TYPO3\CMS\Lowlevel\Command\ListSysLogCommand::class
+    ]
+];
index 107a570..ff815e0 100644 (file)
@@ -19,5 +19,4 @@ if (TYPO3_MODE === 'BE') {
     $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lowlevel']['cleanerModules']['deleted'] = array(\TYPO3\CMS\Lowlevel\DeletedRecordsCommand::class);
     $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lowlevel']['cleanerModules']['versions'] = array(\TYPO3\CMS\Lowlevel\VersionsCommand::class);
     $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lowlevel']['cleanerModules']['cleanflexform'] = array(\TYPO3\CMS\Lowlevel\CleanFlexformCommand::class);
-    $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['lowlevel']['cleanerModules']['syslog'] = array(\TYPO3\CMS\Lowlevel\SyslogCommand::class);
 }