Example #1
0
 /**
  * Execute command
  *
  * @param  InputInterface  $input  Input instance
  * @param  OutputInterface $output Output instance
  *
  * @return int|null|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $this->elevateProcess($input, $output);
     $debugLogLocation = $this->getApplication()->getConfigValue('db', 'debug_log_dir');
     $debugLogDir = dirname($debugLogLocation);
     $output->writeln('<h2>Starting MySQL general query log</h2>');
     // Create directory if not exists
     if (!is_dir($debugLogDir)) {
         if (!mkdir($debugLogDir, 0777, true)) {
             $output->writeln('<p-error>Could not create "' . $debugLogDir . '" directory</p-error>');
             throw new \CliTools\Exception\StopException(1);
         }
     }
     if (!empty($debugLogLocation)) {
         $debugLogLocation .= 'mysql_' . getmypid() . '.log';
         $query = 'SET GLOBAL general_log_file = ' . DatabaseConnection::quote($debugLogLocation);
         DatabaseConnection::exec($query);
     }
     // Fetch log file
     $query = 'SHOW VARIABLES LIKE \'general_log_file\'';
     $logFileRow = DatabaseConnection::getRow($query);
     if (!empty($logFileRow['Value'])) {
         // Enable general log
         $output->writeln('<p>Enabling general log</p>');
         $query = 'SET GLOBAL general_log = \'ON\'';
         DatabaseConnection::exec($query);
         // Setup teardown cleanup
         $tearDownFunc = function () use($output) {
             // Disable general log
             $output->writeln('<p>Disabling general log</p>');
             $query = 'SET GLOBAL general_log = \'OFF\'';
             DatabaseConnection::exec($query);
         };
         $this->getApplication()->registerTearDown($tearDownFunc);
         // Read grep value
         $grep = null;
         if ($input->hasArgument('grep')) {
             $grep = $input->getArgument('grep');
         }
         // Tail logfile
         $logList = array($logFileRow['Value']);
         $optionList = array('-n 0');
         $this->showLog($logList, $input, $output, $grep, $optionList);
         return 0;
     } else {
         $output->writeln('<p-error>MySQL general_log_file not set</p-error>');
         return 1;
     }
 }
Example #2
0
 /**
  * Execute command
  *
  * @param  InputInterface  $input  Input instance
  * @param  OutputInterface $output Output instance
  *
  * @return int|null|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $charset = 'utf8';
     $collation = 'utf8_general_ci';
     $stdout = false;
     $database = $input->getArgument('database');
     if ($input->getOption('charset')) {
         $charset = (string) $input->getOption('charset');
     }
     if ($input->getOption('collation')) {
         $collation = (string) $input->getOption('collation');
     }
     if ($input->getOption('stdout')) {
         $stdout = true;
     }
     // ##################
     // Alter database
     // ##################
     $query = 'ALTER DATABASE %s CHARACTER SET %s COLLATE %s';
     $query = sprintf($query, DatabaseConnection::sanitizeSqlDatabase($database), DatabaseConnection::quote($charset), DatabaseConnection::quote($collation));
     if (!$stdout) {
         // Execute
         $output->writeln('<h2>Converting database ' . $database . '</h2>');
         DatabaseConnection::exec($query);
     } else {
         // Show only
         $output->writeln($query . ';');
     }
     // ##################
     // Alter tables
     // ##################
     $tableList = DatabaseConnection::tableList($database);
     foreach ($tableList as $table) {
         // Build statement
         $query = 'ALTER TABLE %s.%s CONVERT TO CHARACTER SET %s COLLATE %s';
         $query = sprintf($query, DatabaseConnection::sanitizeSqlDatabase($database), DatabaseConnection::sanitizeSqlTable($table), DatabaseConnection::quote($charset), DatabaseConnection::quote($collation));
         if (!$stdout) {
             // Execute
             $output->writeln('<p>Converting table ' . $table . '</p>');
             DatabaseConnection::exec($query);
         } else {
             // Show only
             $output->writeln($query . ';');
         }
     }
     return 0;
 }
Example #3
0
 /**
  * Execute command
  *
  * @param  InputInterface  $input  Input instance
  * @param  OutputInterface $output Output instance
  *
  * @return int|null|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     // ##################
     // Init
     // ##################
     $dbName = $input->getArgument('db');
     $output->writeln('<h2>Cleanup TYPO3 database</h2>');
     // ##############
     // Loop through databases
     // ##############
     if (empty($dbName)) {
         // ##############
         // All databases
         // ##############
         // Get list of databases
         $query = 'SELECT SCHEMA_NAME
                 FROM information_schema.SCHEMATA';
         $databaseList = DatabaseConnection::getCol($query);
         foreach ($databaseList as $dbName) {
             // Skip internal mysql databases
             if (in_array(strtolower($dbName), array('mysql', 'information_schema', 'performance_schema'))) {
                 continue;
             }
             // Check if database is TYPO3 instance
             $query = 'SELECT COUNT(*) as count
                         FROM information_schema.tables
                        WHERE table_schema = ' . DatabaseConnection::quote($dbName) . '
                          AND table_name = \'be_users\'';
             $isTypo3Database = DatabaseConnection::getOne($query);
             if ($isTypo3Database) {
                 $this->cleanupTypo3Database($dbName);
             }
         }
     } else {
         // ##############
         // One databases
         // ##############
         $this->cleanupTypo3Database($dbName);
     }
     return 0;
 }
Example #4
0
 /**
  * Set development domains for TYPO3 database
  *
  * @return void
  */
 protected function manipulateDomains()
 {
     $devDomain = '.' . $this->getApplication()->getConfigValue('config', 'domain_dev');
     $domainLength = strlen($devDomain);
     // ##################
     // Fix domains
     // ##################
     $query = 'UPDATE sys_domain
                  SET domainName = CONCAT(domainName, ' . DatabaseConnection::quote($devDomain) . ')
                WHERE RIGHT(domainName, ' . $domainLength . ') <> ' . DatabaseConnection::quote($devDomain);
     DatabaseConnection::exec($query);
 }
Example #5
0
 /**
  * Execute command
  *
  * @param  InputInterface  $input  Input instance
  * @param  OutputInterface $output Output instance
  *
  * @return int|null|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     // Get list of databases
     $databaseList = DatabaseConnection::databaseList();
     if (!empty($databaseList)) {
         // ########################
         // Fetch statistics
         // ########################
         $databaseRowList = array();
         foreach ($databaseList as $database) {
             // Get all tables
             $query = 'SELECT COUNT(*) AS count
                         FROM information_schema.tables
                        WHERE TABLE_SCHEMA = ' . DatabaseConnection::quote($database) . '
                          AND TABLE_TYPE = \'BASE TABLE\'';
             $tableCount = DatabaseConnection::getOne($query);
             // Get all views
             $query = 'SELECT COUNT(*) AS count
                         FROM information_schema.tables
                        WHERE TABLE_SCHEMA = ' . DatabaseConnection::quote($database) . '
                          AND TABLE_TYPE LIKE \'%VIEW\'';
             $viewCount = DatabaseConnection::getOne($query);
             // Get size of database
             $query = 'SELECT SUM(data_length) AS data_size,
                              SUM(index_length) AS index_size,
                              SUM(data_length + index_length) AS total_size
                         FROM information_schema.tables
                        WHERE TABLE_SCHEMA = ' . DatabaseConnection::quote($database);
             $statsRow = DatabaseConnection::getRow($query);
             $databaseRowList[$database] = array('name' => $database, 'table_count' => $tableCount, 'view_count' => $viewCount, 'data_size' => $statsRow['data_size'], 'index_size' => $statsRow['index_size'], 'total_size' => $statsRow['total_size']);
         }
         // ########################
         // Sorting
         // ########################
         // Sort: default by name (natural sort)
         uasort($databaseRowList, function ($a, $b) {
             return strnatcmp($a['name'], $b['name']);
         });
         // Sort: by table names
         if ($input->getOption('sort-name')) {
             uasort($databaseRowList, function ($a, $b) {
                 return $a['table_count'] < $b['table_count'];
             });
         }
         // Sort: by data size
         if ($input->getOption('sort-data')) {
             uasort($databaseRowList, function ($a, $b) {
                 return $a['data_size'] < $b['data_size'];
             });
         }
         // Sort: by index size
         if ($input->getOption('sort-index')) {
             uasort($databaseRowList, function ($a, $b) {
                 return $a['index_size'] < $b['index_size'];
             });
         }
         // Sort: by total size
         if ($input->getOption('sort-total')) {
             uasort($databaseRowList, function ($a, $b) {
                 return $a['total_size'] < $b['total_size'];
             });
         }
         // ########################
         // Stats
         // ########################
         $statsRow = array('name' => '', 'table_count' => 0, 'view_count' => 0, 'data_size' => 0, 'index_size' => 0, 'total_size' => 0);
         $databaseCount = count($databaseRowList);
         foreach ($databaseRowList as $databaseRow) {
             $statsRow['table_count'] += $databaseRow['table_count'];
             $statsRow['view_count'] += $databaseRow['view_count'];
             $statsRow['data_size'] += $databaseRow['data_size'];
             $statsRow['index_size'] += $databaseRow['index_size'];
             $statsRow['total_size'] += $databaseRow['total_size'];
         }
         // ########################
         // Output
         // ########################
         /** @var \Symfony\Component\Console\Helper\Table $table */
         $table = new Table($output);
         $table->setHeaders(array('Database', 'Tables', 'Views', 'Data', 'Index', 'Total'));
         foreach ($databaseRowList as $databaseRow) {
             $databaseRow['table_count'] = FormatUtility::number($databaseRow['table_count']);
             $databaseRow['view_count'] = FormatUtility::number($databaseRow['view_count']);
             $databaseRow['data_size'] = FormatUtility::bytes($databaseRow['data_size']);
             $databaseRow['index_size'] = FormatUtility::bytes($databaseRow['index_size']);
             $databaseRow['total_size'] = FormatUtility::bytes($databaseRow['total_size']);
             $table->addRow(array_values($databaseRow));
         }
         // Stats: average
         if ($databaseCount >= 1) {
             $table->addRow(new TableSeparator());
             $statsAvgRow = array();
             $statsAvgRow['name'] = 'Average';
             $statsAvgRow['table_count'] = FormatUtility::number($statsRow['table_count'] / $databaseCount);
             $statsAvgRow['view_count'] = FormatUtility::number($statsRow['view_count'] / $databaseCount);
             $statsAvgRow['data_size'] = FormatUtility::bytes($statsRow['data_size'] / $databaseCount);
             $statsAvgRow['index_size'] = FormatUtility::bytes($statsRow['index_size'] / $databaseCount);
             $statsAvgRow['total_size'] = FormatUtility::bytes($statsRow['total_size'] / $databaseCount);
             $table->addRow(array_values($statsAvgRow));
         }
         // Stats: total
         $statsTotalRow['name'] = 'Total';
         $statsTotalRow['table_count'] = FormatUtility::number($statsRow['table_count']);
         $statsTotalRow['view_count'] = FormatUtility::number($statsRow['view_count']);
         $statsTotalRow['data_size'] = FormatUtility::bytes($statsRow['data_size']);
         $statsTotalRow['index_size'] = FormatUtility::bytes($statsRow['index_size']);
         $statsTotalRow['total_size'] = FormatUtility::bytes($statsRow['total_size']);
         $table->addRow(array_values($statsTotalRow));
         $table->render();
     } else {
         $output->writeln('<p-error>No databases found</p-error>');
     }
     return 0;
 }
Example #6
0
 /**
  * Set TYPO3 user for database
  *
  * @param string $database Database
  * @param string $username Username
  * @param string $password Password (salted/hashed)
  */
 protected function setTypo3UserForDatabase($database, $username, $password)
 {
     // ##################
     // Update/insert user
     // ##################
     // Default UserTS
     $tsConfig = array('options.clearCache.system = 1', 'options.clearCache.all = 1', 'options.enableShowPalettes = 1', 'options.alertPopups = 254', 'options.pageTree.showPageIdWithTitle = 1', 'options.pageTree.showPathAboveMounts = 1', 'options.pageTree.showDomainNameWithTitle = 1', 'admPanel.enable.edit = 1', 'admPanel.module.edit.forceDisplayFieldIcons = 1', 'admPanel.hide = 0', 'setup.default.thumbnailsByDefault = 1', 'setup.default.enableFlashUploader = 0', 'setup.default.recursiveDelete = 1', 'setup.default.showHiddenFilesAndFolders = 1', 'setup.default.resizeTextareas_Flexible = 1', 'setup.default.copyLevels = 99', 'setup.default.rteResize = 99', 'setup.default.moduleData.web_list.bigControlPanel = 1', 'setup.default.moduleData.web_list.clipBoard = 1', 'setup.default.moduleData.web_list.localization = 1', 'setup.default.moduleData.web_list.showPalettes = 1', 'setup.default.moduleData.file_list.bigControlPanel = 1', 'setup.default.moduleData.file_list.clipBoard = 1', 'setup.default.moduleData.file_list.localization = 1', 'setup.default.moduleData.file_list.showPalettes = 1');
     $tsConfig = implode("\n", $tsConfig);
     try {
         // Get uid from current dev user (if already existing)
         $query = 'SELECT uid
                     FROM ' . DatabaseConnection::sanitizeSqlDatabase($database) . '.be_users
                    WHERE username = '******'
                      AND deleted = 0';
         $beUserId = DatabaseConnection::getOne($query);
         // Insert or update user in TYPO3 database
         $query = 'INSERT INTO ' . DatabaseConnection::sanitizeSqlDatabase($database) . '.be_users
                               (uid, tstamp, crdate, realName, username, password, TSconfig, admin, disable, starttime, endtime)
                    VALUES(
                       ' . DatabaseConnection::quote($beUserId) . ',
                       UNIX_TIMESTAMP(),
                       UNIX_TIMESTAMP(),
                       ' . DatabaseConnection::quote('DEVELOPMENT') . ',
                       ' . DatabaseConnection::quote($username) . ',
                       ' . DatabaseConnection::quote($password) . ',
                       ' . DatabaseConnection::quote($tsConfig) . ',
                       1,
                       0,
                       0,
                       0
                    ) ON DUPLICATE KEY UPDATE
                         realName  = VALUES(realName),
                         password  = VALUES(password),
                         TSconfig  = VALUES(TSconfig),
                         disable   = VALUES(disable),
                         starttime = VALUES(starttime),
                         endtime   = VALUES(endtime)';
         DatabaseConnection::exec($query);
         if ($beUserId) {
             $this->output->writeln('<p>User successfully updated to "' . $database . '"</p>');
         } else {
             $this->output->writeln('<p>User successfully added to "' . $database . '"</p>');
         }
     } catch (\Exception $e) {
         $this->output->writeln('<p-error>User adding failed</p-error>');
     }
 }
Example #7
0
 /**
  * Execute command
  *
  * @param  InputInterface  $input  Input instance
  * @param  OutputInterface $output Output instance
  *
  * @return int|null|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $this->elevateProcess($input, $output);
     $slowLogQueryTime = 1;
     $logNonIndexedQueries = false;
     // Slow log threshold
     if ($input->getOption('time')) {
         $slowLogQueryTime = $input->getOption('time');
     }
     // Also show not using indexes queries
     if ($input->getOption('no-index')) {
         $logNonIndexedQueries = true;
     }
     $debugLogLocation = $this->getApplication()->getConfigValue('db', 'debug_log_dir');
     $debugLogDir = dirname($debugLogLocation);
     $output->writeln('<h2>Starting MySQL slow query log</h2>');
     // Create directory if not exists
     if (!is_dir($debugLogDir)) {
         if (!mkdir($debugLogDir, 0777, true)) {
             $output->writeln('<p-error>Could not create "' . $debugLogDir . '" directory</p-error>');
             throw new \CliTools\Exception\StopException(1);
         }
     }
     if (!empty($debugLogLocation)) {
         $debugLogLocation .= 'mysql_' . getmypid() . '.log';
         $query = 'SET GLOBAL slow_query_log_file = ' . DatabaseConnection::quote($debugLogLocation);
         DatabaseConnection::exec($query);
     }
     // Fetch log file
     $query = 'SHOW VARIABLES LIKE \'slow_query_log_file\'';
     $logFileRow = DatabaseConnection::getRow($query);
     if (!empty($logFileRow['Value'])) {
         // Enable slow log
         $output->writeln('<p>Enabling slow log</p>');
         $query = 'SET GLOBAL slow_query_log = \'ON\'';
         DatabaseConnection::exec($query);
         // Enable slow log
         $output->writeln('<p>Set long_query_time to ' . (int) abs($slowLogQueryTime) . ' seconds</p>');
         $query = 'SET GLOBAL long_query_time = ' . (int) abs($slowLogQueryTime);
         DatabaseConnection::exec($query);
         // Enable log queries without indexes log
         if ($logNonIndexedQueries) {
             $output->writeln('<p>Enabling logging of queries without using indexes</p>');
             $query = 'SET GLOBAL log_queries_not_using_indexes = \'ON\'';
             DatabaseConnection::exec($query);
         } else {
             $output->writeln('<p>Disabling logging of queries without using indexes</p>');
             $query = 'SET GLOBAL log_queries_not_using_indexes = \'OFF\'';
             DatabaseConnection::exec($query);
         }
         // Setup teardown cleanup
         $tearDownFunc = function () use($output, $logNonIndexedQueries) {
             // Disable general log
             $output->writeln('<p>Disable slow log</p>');
             $query = 'SET GLOBAL slow_query_log = \'OFF\'';
             DatabaseConnection::exec($query);
             if ($logNonIndexedQueries) {
                 // Disable log queries without indexes log
                 $query = 'SET GLOBAL log_queries_not_using_indexes = \'OFF\'';
                 DatabaseConnection::exec($query);
             }
         };
         $this->getApplication()->registerTearDown($tearDownFunc);
         // Read grep value
         $grep = null;
         if ($input->hasArgument('grep')) {
             $grep = $input->getArgument('grep');
         }
         // Tail logfile
         $logList = array($logFileRow['Value']);
         $optionList = array('-n 0');
         $this->showLog($logList, $input, $output, $grep, $optionList);
         return 0;
     } else {
         $output->writeln('<p-error>MySQL general_log_file not set</p-error>');
         return 1;
     }
 }