Skip to content

Commit

Permalink
improved topics script
Browse files Browse the repository at this point in the history
topics script alert fix

improved adding oauth clientin upgrade script
  • Loading branch information
takeit committed Mar 16, 2015
1 parent 4c9e39e commit 2ea50ba
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,65 @@
<?php
exec(__DIR__.'/../../../../../../application/console oauth:create-client newscoop '.$_SERVER['HTTP_HOST'].' '.$_SERVER['HTTP_HOST'].' --default', $output, $code);

$newscoopDir = realpath(dirname(__FILE__).'/../../../../../../');

require_once $newscoopDir.'/vendor/autoload.php';
require $newscoopDir.'/conf/database_conf.php';

use Monolog\Logger;
use Newscoop\Installer\Services;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\PhpExecutableFinder;

$upgradeErrors = array();
$app = new Silex\Application();
$app->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));
}
20 changes: 17 additions & 3 deletions newscoop/install/Resources/sql/upgrade/4.4.x/2015.03.11/topics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
15 changes: 8 additions & 7 deletions newscoop/library/Newscoop/Installer/Services/UpgradeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}
}
}
12 changes: 0 additions & 12 deletions newscoop/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 2ea50ba

Please sign in to comment.