From 2ea50ba0e2b636a381071ab31286a58022a98d30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Muszy=C5=84ski?= Date: Mon, 16 Mar 2015 16:25:21 +0100 Subject: [PATCH] improved topics script topics script alert fix improved adding oauth clientin upgrade script --- .../upgrade/4.4.x/2015.02.24/add_client.php | 65 ++++++++++++++++++- .../sql/upgrade/4.4.x/2015.03.11/topics.php | 20 +++++- .../Installer/Services/UpgradeService.php | 15 +++-- newscoop/upgrade.php | 12 ---- 4 files changed, 89 insertions(+), 23 deletions(-) diff --git a/newscoop/install/Resources/sql/upgrade/4.4.x/2015.02.24/add_client.php b/newscoop/install/Resources/sql/upgrade/4.4.x/2015.02.24/add_client.php index 7f520d6e0b..3277f988b4 100644 --- a/newscoop/install/Resources/sql/upgrade/4.4.x/2015.02.24/add_client.php +++ b/newscoop/install/Resources/sql/upgrade/4.4.x/2015.02.24/add_client.php @@ -1,2 +1,65 @@ register(new Silex\Provider\MonologServiceProvider(), array( + 'monolog.logfile' => $newscoopDir.'/log/upgrade.log', + 'monolog.level' => Logger::NOTICE, + 'monolog.name' => 'upgrade', +)); + +$app->register(new Silex\Provider\DoctrineServiceProvider(), array( + 'db.options' => array( + 'driver' => 'pdo_mysql', + 'host' => $Campsite['db']['host'], + 'dbname' => $Campsite['db']['name'], + 'user' => $Campsite['db']['user'], + 'password' => $Campsite['db']['pass'], + 'port' => $Campsite['db']['port'], + 'charset' => 'utf8', + ), +)); + +$app['upgrade_service'] = $app->share(function () use ($app) { + return new Services\UpgradeService($app['db'], $app['monolog']); +}); + +$logger = $app['monolog']; + +$newscoopConsole = escapeshellarg($newscoopDir.'/application/console'); +$phpFinder = new PhpExecutableFinder(); +$phpPath = $phpFinder->find(); +if (!$phpPath) { + throw new \RuntimeException('The php executable could not be found, add it to your PATH environment variable and try again'); +} + +try { + $alias = $app['upgrade_service']->getDefaultAlias(); + if (!$alias) { + $msg = "Could not find default alias! Aborting..."; + $upgradeErrors[] = $msg; + $logger->addError($msg); + } else { + $php = escapeshellarg($phpPath); + $process = new Process("$php $newscoopConsole oauth:create-client newscoop ".$alias." ".$alias." --default"); + $process->run(); + if (!$process->isSuccessful()) { + throw new \RuntimeException($process->getErrorOutput()); + } + } +} catch (\Exception $e) { + $msg = $e->getMessage(); + $upgradeErrors[] = $msg; + $logger->addError($msg); + array_splice($upgradeErrors, 0, 0, array($msg)); +} diff --git a/newscoop/install/Resources/sql/upgrade/4.4.x/2015.03.11/topics.php b/newscoop/install/Resources/sql/upgrade/4.4.x/2015.03.11/topics.php index d278b4e870..4357d84ee6 100644 --- a/newscoop/install/Resources/sql/upgrade/4.4.x/2015.03.11/topics.php +++ b/newscoop/install/Resources/sql/upgrade/4.4.x/2015.03.11/topics.php @@ -65,6 +65,13 @@ $app['orm.em']->getEventManager()->addEventSubscriber($treeListener); $app['orm.em']->getEventManager()->addEventSubscriber($translatableListener); + try { + $tablesCount = "SELECT COUNT(*) AS count FROM Topics"; + $app['db']->fetchAll($tablesCount); + } catch (\Exception $e) { + return; + } + $sqlTopics = "SELECT node.id, (COUNT( parent.id) -1) AS depth FROM Topics AS node, Topics AS parent WHERE node.node_left @@ -131,8 +138,8 @@ $app['orm.em']->flush(); } - } catch (\ResourcesConflictException $e) { - $logger->addInfo('Topic '.$topicDetails[0]['name'].'already exists. Skipping this topic...!\n'); + } catch (ResourcesConflictException $e) { + $logger->addInfo('Topic '.$topicDetails[0]['name'].' already exists. Skipping this topic...!\n'); } continue; @@ -159,7 +166,14 @@ $topic->setId($topicToInsertDetails[0]['id']); $topic->setTitle($topicToInsertDetails[0]['name']); $topic->setTranslatableLocale($locale); - $app['topics_service']->saveTopicPosition($topic, $params); + try { + $app['topics_service']->saveTopicPosition($topic, $params); + } catch (\Exception $e) { + $logger->addInfo('Topic '.$topicToInsertDetails[0]['name'].' already exists. Skipping this topic...!\n'); + + continue; + } + if (count($topicToInsertDetails) > 1) { unset($topicToInsertDetails[0]); foreach ($topicToInsertDetails as $key => $translation) { diff --git a/newscoop/library/Newscoop/Installer/Services/UpgradeService.php b/newscoop/library/Newscoop/Installer/Services/UpgradeService.php index 52d8d82660..e1799e60a0 100644 --- a/newscoop/library/Newscoop/Installer/Services/UpgradeService.php +++ b/newscoop/library/Newscoop/Installer/Services/UpgradeService.php @@ -167,18 +167,19 @@ public function upgradeDatabase($versionsArray, $silent = false, $showRolls = fa /** * Gets default alias for the first publication * - * @return string alias + * @return string|null alias */ public function getDefaultAlias() { - try { - $result = $this->connection->fetchAll('SELECT IdDefaultAlias as aliasId FROM Publications LIMIT 1'); + $alias = null; + $result = $this->connection->fetchAll('SELECT IdDefaultAlias as aliasId FROM Publications LIMIT 1'); + if ($result && isset($result[0])) { $aliasId = $result[0]['aliasId']; - $result = $this->connection->fetchAll('SELECT Name FROM Aliases WHERE Id = '.$aliasId); + $alias = $this->connection->fetchAll('SELECT Name FROM Aliases WHERE Id = '.$aliasId); + } - return $result[0]['Name']; - } catch (\Exception $e) { - throw new \Exception('Could not find default alias! Aborting...'); + if ($alias && isset($alias[0])) { + return $alias[0]['Name']; } } } diff --git a/newscoop/upgrade.php b/newscoop/upgrade.php index 256ac10d9b..35d9edad98 100644 --- a/newscoop/upgrade.php +++ b/newscoop/upgrade.php @@ -68,18 +68,6 @@ return new Services\UpgradeService($app['db'], $app['monolog']); }); -try { - // set default alias so it can be read by upgrade php scripts - // (e.g. by the script creating oauth default client) - if (php_sapi_name() == "cli") { - $_SERVER['HTTP_HOST'] = $app['upgrade_service']->getDefaultAlias(); - } -} catch (\Exception $e) { - echo $e->getMessage()."\n"; - - return; -} - $app->get('/', function (Silex\Application $app) { $oldVersions = $app['upgrade_service']->getDBVersion(); $response = $app['upgrade_service']->upgradeDatabase($oldVersions);