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)
 {
     $database = $input->getArgument('db');
     $dumpFile = $input->getArgument('file');
     if (!is_file($dumpFile) || !is_readable($dumpFile)) {
         $output->writeln('<p-error>File is not readable</p-error>');
         return 1;
     }
     $dumpFileType = PhpUtility::getMimeType($dumpFile);
     $output->writeln('<h2>Restoring dump "' . $dumpFile . '" into database "' . $database . '"</h2>');
     if (DatabaseConnection::databaseExists($database)) {
         // Dropping
         $output->writeln('<p>Dropping database</p>');
         $query = 'DROP DATABASE IF EXISTS ' . DatabaseConnection::sanitizeSqlDatabase($database);
         DatabaseConnection::exec($query);
     }
     // Creating
     $output->writeln('<p>Creating database</p>');
     $query = 'CREATE DATABASE ' . DatabaseConnection::sanitizeSqlDatabase($database);
     DatabaseConnection::exec($query);
     // Inserting
     putenv('USER='******'MYSQL_PWD=' . DatabaseConnection::getDbPassword());
     $commandMysql = new CommandBuilder('mysql', '--user=%s %s --one-database', array(DatabaseConnection::getDbUsername(), $database));
     // Set server connection details
     if ($input->getOption('host')) {
         $commandMysql->addArgumentTemplate('-h %s', $input->getOption('host'));
     }
     if ($input->getOption('port')) {
         $commandMysql->addArgumentTemplate('-P %s', $input->getOption('port'));
     }
     $commandFile = new CommandBuilder();
     $commandFile->addArgument($dumpFile);
     $commandFile->addPipeCommand($commandMysql);
     switch ($dumpFileType) {
         case 'application/x-bzip2':
             $output->writeln('<p>Using BZIP2 decompression</p>');
             $commandFile->setCommand('bzcat');
             break;
         case 'application/gzip':
         case 'application/x-gzip':
             $output->writeln('<p>Using GZIP decompression</p>');
             $commandFile->setCommand('gzcat');
             break;
         case 'application/x-lzma':
         case 'application/x-xz':
             $output->writeln('<p>Using LZMA decompression</p>');
             $commandFile->setCommand('xzcat');
             break;
         default:
             $output->writeln('<p>Using plaintext (no decompression)</p>');
             $commandFile->setCommand('cat');
             break;
     }
     $output->writeln('<p>Reading dump</p>');
     $commandFile->executeInteractive();
     $output->writeln('<h2>Database "' . $database . '" restored</h2>');
     return 0;
 }
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)
 {
     $database = $input->getArgument('db');
     $output->writeln('<h2>Dropping Database "' . $database . '"...</h2>');
     $query = 'DROP DATABASE IF EXISTS ' . DatabaseConnection::sanitizeSqlDatabase($database);
     DatabaseConnection::exec($query);
     $output->writeln('<p>Database dropped</p>');
     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)
 {
     $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 #4
0
 /**
  * Execute command
  *
  * @param  InputInterface  $input  Input instance
  * @param  OutputInterface $output Output instance
  *
  * @return int|null|void
  */
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $database = $input->getArgument('db');
     $output->writeln('<h2>Clearing database "' . $database . '"</h2>');
     if (DatabaseConnection::databaseExists($database)) {
         $output->writeln('<p>Dropping database</p>');
         $query = 'DROP DATABASE ' . DatabaseConnection::sanitizeSqlDatabase($database);
         DatabaseConnection::exec($query);
     }
     $output->writeln('<p>Creating database</p>');
     $query = 'CREATE DATABASE ' . DatabaseConnection::sanitizeSqlDatabase($database);
     DatabaseConnection::exec($query);
     $output->writeln('<h2>Database "' . $database . '" recreated</h2>');
     return 0;
 }
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)
 {
     $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 #6
0
 /**
  * Cleanup MySQL
  *
  * @return string
  */
 protected function cleanupMysql()
 {
     try {
         // ############################
         // Clear general log
         // ############################
         // Disable general log
         $query = 'SET GLOBAL general_log = \'OFF\'';
         DatabaseConnection::exec($query);
         // Fetch log file
         $query = 'SHOW VARIABLES LIKE \'general_log_file\'';
         $logFileRow = DatabaseConnection::getRow($query);
         if (!empty($logFileRow['Value'])) {
             $command = new CommandBuilder('rm');
             $command->addArgument('-f')->addArgumentSeparator()->addArgument($logFileRow['Value'])->executeInteractive();
         }
     } catch (\Exception $e) {
         // do nothing if no mysql is running
     }
 }
Example #7
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 #8
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 #9
0
 /**
  * Cleanup TYPO3 database
  *
  * @param string $database Database
  */
 protected function cleanupTypo3Database($database)
 {
     $cleanupTableList = array();
     // Check if database is TYPO3 instance
     $tableList = DatabaseConnection::tableList($database);
     foreach ($tableList as $table) {
         $clearTable = false;
         // Caching und indexing tables
         switch (true) {
             case strpos($table, 'cache_') === 0:
             case strpos($table, 'cachingframework_') === 0:
             case strpos($table, 'cf_') === 0:
                 // Caching framework
                 $clearTable = true;
                 break;
             case strpos($table, 'index_') === 0:
                 // EXT:indexed_search
                 $clearTable = true;
                 break;
         }
         switch ($table) {
             case 'sys_history':
             case 'sys_log':
                 // History/Log
                 $clearTable = true;
                 break;
             case 'sys_dmain':
                 // EXT:direct_mail
                 $clearTable = true;
                 break;
             case 'tx_devlog':
                 // EXT:devlog
                 $clearTable = true;
                 break;
             case 'tx_realurl_errorlog':
             case 'tx_realurl_pathcache':
             case 'tx_realurl_urldecodecache':
             case 'tx_realurl_urlencodecache':
                 // EXT: realurl
                 $clearTable = true;
                 break;
             case 'tx_solr_cache':
             case 'tx_solr_cache_tags':
                 // EXT:solr
                 $clearTable = true;
                 break;
         }
         if ($clearTable) {
             $cleanupTableList[] = $table;
         }
     }
     $this->output->writeln('<p>Starting cleanup of database "' . $database . '"</p>');
     DatabaseConnection::switchDatabase(DatabaseConnection::sanitizeSqlDatabase($database));
     foreach ($cleanupTableList as $table) {
         $query = 'TRUNCATE ' . DatabaseConnection::sanitizeSqlTable($table);
         DatabaseConnection::exec($query);
         if ($this->output->isVerbose()) {
             $this->output->writeln('<p>Truncating table ' . $table . '</p>');
         }
     }
     $this->output->writeln('<p>finished</p>');
 }
Example #10
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;
     }
 }