} } } } $foldersToBeRemovedCompletely = array($newscoopDir . 'admin-files/lang'); foreach ($foldersToBeRemovedCompletely as $folder) { if (is_dir($folder)) { try { // Remove parent directory $filesystem->remove(array($folder)); } catch (\Exception $e) { $msg = 'Could not remove folder ' . str_replace($newscoopDir, '', $folder) . ', please remove it and it\'s contents manually.'; $logger->addError($msg); $upgradeErrors[] = $msg; } } } try { $configFile = realpath(__DIR__ . '/../../../../../../conf/configuration.php'); $databaseService = new DatabaseService($logger); $databaseService->renderFile('_configuration.twig', $configFile, array()); } catch (\Exception $e) { $msg = "Could not update '" . $configFile . "', please update it manually." . " Copy content of '" . realpath(__DIR__ . '/../../../../../Resources/templates/_configuration.twig') . "' file to '" . $configFile . "' and save.\n"; $logger->addError($msg); array_splice($upgradeErrors, 0, 0, array($msg)); } if (count($upgradeErrors) > 0) { $msg = "Some files or directories could not automatically be removed. This is " . "most likely caused by permissions. \n" . "You can either remove the files manually (see {$newscoopDir}install/Resources/sql/upgrade/4.3.x/2014.09.22/delete_diff.txt) " . "or execute this file with root permissions, e.g.: \n" . "sudo php {$newscoopDir}install/Resources/sql/upgrade/4.3.x/2014.09.22/upgrade.php"; $logger->addError($msg); array_splice($upgradeErrors, 0, 0, array($msg)); }
/** * Upgrade database * * @param array $versionsArray * @param boolean $silent * @param boolean $showRolls * * @return boolean */ public function upgradeDatabase($versionsArray, $silent = false, $showRolls = false) { $databaseService = new Services\DatabaseService($this->monolog); $lockFileName = __FILE__; $lockFile = fopen($lockFileName, "r"); if ($lockFile === false) { return "Unable to create single process lock control!"; } if (!flock($lockFile, LOCK_EX | LOCK_NB)) { // do an exclusive lock return "The upgrade process is already running."; } // keeping the last imported version throughout the upgrade process $last_db_version = $versionsArray['version']; // keeping the last imported roll throughout the upgrade process $last_db_roll = $versionsArray['roll']; $first = true; $errorsCount = 0; $temp = 0; $skipped = array(); $sqlVersions = array_map('basename', glob($this->newscoopDir . '/install/Resources/sql/upgrade/[2-9].[0-9]*')); usort($sqlVersions, array($databaseService, 'versionCompare')); foreach ($sqlVersions as $index => $db_version) { if (-1 == $databaseService->versionCompare($db_version, $last_db_version)) { continue; } $last_db_version = $db_version; $last_db_roll = ''; $cur_old_roll = ''; // the roll of the running version that was imported before the upgrade ($old_roll or '') if ($first) { $last_db_roll = $versionsArray['roll']; $cur_old_roll = $last_db_roll; if (!$silent) { $db_ver_roll_info = "{$db_version}"; if (!in_array($last_db_roll, array('', '.'))) { $db_ver_roll_info .= ", roll {$last_db_roll}"; } $this->logger->addNotice('* Upgrading the database from version ' . $db_ver_roll_info . '...'); } $first = false; } $output = array(); $upgrade_base_dir = $this->newscoopDir . "/install/Resources/sql/upgrade/{$db_version}/"; $rolls = $databaseService->searchDbRolls($upgrade_base_dir, $cur_old_roll); // run upgrade scripts $sql_scripts = array("tables.sql", "data-required.sql", "data-optional.sql", "tables-post.sql"); foreach ($rolls as $upgrade_dir_roll => $upgrade_dir_path) { $upgrade_dir = $upgrade_dir_path . DIRECTORY_SEPARATOR; $last_db_roll = $upgrade_dir_roll; if ($showRolls || !$silent) { $this->logger->addNotice('* importing database roll ' . $last_db_version . ' / ' . $last_db_roll); } foreach ($sql_scripts as $index => $script) { if (!is_file($upgrade_dir . $script)) { continue; } $error_queries = array(); $errorsCount = $databaseService->importDB($upgrade_dir . $script, $this->connection, $this->logger); $temp = $temp + $errorsCount; if ($errorsCount) { $this->logger->addError('* ' . $script . ' (' . $db_version . ') errors'); } } $saveResult = $databaseService->saveDatabaseVersion($this->connection, $last_db_version, $last_db_roll); if ($saveResult) { $this->logger->addNotice('* version is updated to ' . $last_db_version . '/' . $last_db_roll); } } } if (!$silent) { $this->logger->addNotice('* importing database is done'); } flock($lockFile, LOCK_UN); // release the lock $errorsCount = $temp; if ($errorsCount) { return $databaseService->errorQueries; } return true; }
/** * @see Console\Command\Command */ protected function execute(InputInterface $input, OutputInterface $output) { $container = $this->getApplication()->getKernel()->getContainer(); $output->writeln('<info>Welcome to Newscoop Installer.<info>'); $symfonyRequirements = new \SymfonyRequirements(); $requirements = $symfonyRequirements->getRequirements(); $missingReq = array(); foreach ($requirements as $req) { if (!$req->isFulfilled()) { $missingReq[] = $req->getTestMessage() . ' - ' . $req->getHelpText(); } } $fixCommonIssues = $input->getOption('fix'); if (count($missingReq) > 0 && !$fixCommonIssues) { $output->writeln('<info>Before we start we need to fix some requirements.<info>'); $output->writeln('<info>Please read all messages and try to fix them:<info>'); foreach ($missingReq as $value) { $output->writeln('<error>' . $value . '<error>'); } $output->writeln('<error>Use --fix param to fix those errors<error>'); return; } elseif (count($missingReq) > 0 && $fixCommonIssues) { $newscoopDir = realpath(__DIR__ . '/../../../../../'); // set chmods for directories exec('chmod -R 777 ' . $newscoopDir . '/cache/'); exec('chmod -R 777 ' . $newscoopDir . '/log/'); exec('chmod -R 777 ' . $newscoopDir . '/conf/'); exec('chmod -R 777 ' . $newscoopDir . '/library/Proxy/'); exec('chmod -R 777 ' . $newscoopDir . '/themes/'); exec('chmod -R 777 ' . $newscoopDir . '/plugins/'); exec('chmod -R 777 ' . $newscoopDir . '/public/'); exec('chmod -R 777 ' . $newscoopDir . '/images/'); } $dbParams = array('driver' => 'pdo_mysql', 'charset' => 'utf8', 'host' => $input->getOption('database_server_name'), 'dbname' => $input->getOption('database_name'), 'port' => $input->getOption('database_server_port')); if ($input->getOption('database_user')) { $dbParams['user'] = $input->getOption('database_user'); } if ($input->getOption('database_password')) { $dbParams['password'] = $input->getOption('database_password'); } $databaseService = new Services\DatabaseService($container->get('logger')); $finishService = new Services\FinishService(); $demositeService = new Services\DemositeService($container->get('logger')); $connection = DriverManager::getConnection($dbParams); try { $connection->connect(); if ($connection->getDatabase() === null) { $databaseService->createNewscoopDatabase($connection); } } catch (\Exception $e) { if ($e->getCode() == '1049') { $databaseService->createNewscoopDatabase($connection); } elseif (strpos($e->getMessage(), 'database exists') === false) { throw $e; } } $output->writeln('<info>Successfully connected to database.<info>'); $tables = $connection->fetchAll('SHOW TABLES', array()); if (count($tables) == 0 || $input->getOption('database_override')) { $databaseService->fillNewscoopDatabase($connection); $databaseService->loadGeoData($connection); $databaseService->saveDatabaseConfiguration($connection); } else { throw new \Exception('There is already a database named ' . $connection->getDatabase() . '. If you are sure to overwrite it, use option --database_override. If not, just change the Database Name and continue.', 1); } $command = $this->getApplication()->find('cache:clear'); $arguments = array('command' => 'cache:clear', '--no-warmup' => true); $inputCache = new ArrayInput($arguments); $command->run($inputCache, $output); $databaseService->installDatabaseSchema($connection, $input->getArgument('alias'), $input->getArgument('site_title')); $output->writeln('<info>Database schema has been processed successfully.<info>'); $demositeService->installEmptyTheme(); $output->writeln('<info>Empty theme has been installed successfully.<info>'); $clearEm = \Doctrine\ORM\EntityManager::create($connection, $container->get('em')->getConfiguration(), $connection->getEventManager()); $finishService->saveCronjobs(new \Newscoop\Services\SchedulerService($clearEm)); $output->writeln('<info>Cronjobs have been saved successfully<info>'); $finishService->generateProxies(); $output->writeln('<info>Proxies have been generated successfully<info>'); $finishService->installAssets(); $output->writeln('<info>Assets have been installed successfully<info>'); $finishService->saveInstanceConfig(array('site_title' => $input->getArgument('site_title'), 'user_email' => $input->getArgument('user_email'), 'recheck_user_password' => $input->getArgument('user_password')), $connection); $output->writeln('<info>Config have been saved successfully.<info>'); if (!$input->getOption('no-client')) { $finishService->createDefaultOauthClient($input->getArgument('alias')); $output->writeln('<info>Default OAuth client has been created successfully.<info>'); } $output->writeln('<info>Newscoop is installed.<info>'); }