Skip to content

Commit

Permalink
Fixes #4711 Adding new console command to trigger Piwik core and Plug…
Browse files Browse the repository at this point in the history
…ins upgrades, if any.

Run it via: ./console core:update --dry-run
  • Loading branch information
mattab committed Feb 20, 2014
1 parent 55d3afa commit 8082e28
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 29 deletions.
12 changes: 12 additions & 0 deletions core/Common.php
Expand Up @@ -128,6 +128,18 @@ public static function isPhpCliMode()
(!strncmp(PHP_SAPI, 'cgi', 3) && empty($remoteAddr));
}

/**
* Returns true if the current request is a console command, eg. ./console xx:yy
* @return bool
*/
public static function isRunningConsoleCommand()
{
$searched = '/console';
$consolePos = strpos($_SERVER['SCRIPT_NAME'], $searched);
$expectedConsolePos = strlen($_SERVER['SCRIPT_NAME']) - strlen($searched);
$isScriptIsConsole = $consolePos == $expectedConsolePos;
return self::isPhpCliMode() && $isScriptIsConsole;
}

/*
* String operations
Expand Down
6 changes: 3 additions & 3 deletions core/Updater.php
Expand Up @@ -126,9 +126,9 @@ public function getSqlQueriesToExecute()
$this->hasMajorDbUpdate = $this->hasMajorDbUpdate || call_user_func(array($className, 'isMajorUpdate'));
}
// unfortunately had to extract this query from the Option class
$queries[] = 'UPDATE `' . Common::prefixTable('option') . '`
SET option_value = \'' . $fileVersion . '\'
WHERE option_name = \'' . self::getNameInOptionTable($componentName) . '\';';
$queries[] = 'UPDATE `' . Common::prefixTable('option') . '` '.
'SET option_value = \'' . $fileVersion . '\' '.
'WHERE option_name = \'' . self::getNameInOptionTable($componentName) . '\';';
}
return $queries;
}
Expand Down
4 changes: 4 additions & 0 deletions core/Url.php
Expand Up @@ -469,6 +469,10 @@ static public function redirectToUrl($url)
} else {
echo "Invalid URL to redirect to.";
}

if(Common::isPhpCliMode()) {
die("If you were using a browser, Piwik would redirect you to this URL: $url \n\n");
}
exit;
}

Expand Down
2 changes: 1 addition & 1 deletion core/Version.php
Expand Up @@ -21,5 +21,5 @@ final class Version
* The current Piwik version.
* @var string
*/
const VERSION = '2.1-rc5';
const VERSION = '2.1-rc6';
}
61 changes: 61 additions & 0 deletions plugins/CoreUpdater/Commands/Update.php
@@ -0,0 +1,61 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\CoreUpdater\Commands;

use Piwik\Plugin\ConsoleCommand;
use Piwik\Plugins\CoreUpdater\Controller;
use Piwik\Plugins\CoreUpdater\NoUpdatesFoundException;
use Piwik\Plugins\UserCountry\LocationProvider;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @package CloudAdmin
*/
class Update extends ConsoleCommand
{
protected function configure()
{
$this->setName('core:update');
$this->setDescription('Triggers the upgrades for Piwik core and plugins. Useful after Piwik core files or some plugins were updated to latest files.');

$this->addOption('dry-run', null, InputOption::VALUE_NONE, 'Only prints out the SQL requests that would be executed during the upgrade');
}

/**
* Execute command like: ./console core:update --dry-run
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$doDryRun = $input->getOption('dry-run');

try {
$this->makeUpdate($input, $output, $doDryRun);
} catch(NoUpdatesFoundException $e) {
// Do not fail if no updates were found
$output->writeln("<info>".$e->getMessage()."</info>");
} catch (\Exception $e) {
// Fail in case of any other error during upgrade
$output->writeln("<error>" . $e->getMessage() . "</error>");
throw $e;
}
}

protected function makeUpdate(InputInterface $input, OutputInterface $output, $doDryRun)
{
$this->checkAllRequiredOptionsAreNotEmpty($input);

$updateController = new Controller();
echo $updateController->runUpdaterAndExit($doDryRun);
}
}
50 changes: 33 additions & 17 deletions plugins/CoreUpdater/Controller.php
Expand Up @@ -13,6 +13,7 @@
use Piwik\ArchiveProcessor\Rules;
use Piwik\Common;
use Piwik\Config;
use Piwik\DataTable\Renderer\Console;
use Piwik\DbHelper;
use Piwik\Filechecks;
use Piwik\Filesystem;
Expand Down Expand Up @@ -265,15 +266,19 @@ public function index()
LanguagesManager::setLanguageForSession($language);
}

return $this->runUpdaterAndExit();
try {
return $this->runUpdaterAndExit();
} catch(NoUpdatesFoundException $e) {
Piwik::redirectToModule('CoreHome');
}
}

protected function runUpdaterAndExit()
public function runUpdaterAndExit($doDryRun = null)
{
$updater = new Updater();
$componentsWithUpdateFile = CoreUpdater::getComponentUpdates($updater);
if (empty($componentsWithUpdateFile)) {
Piwik::redirectToModule('CoreHome');
throw new NoUpdatesFoundException("Everything is already up to date.");
}

SettingsServer::setMaxExecutionTime(0);
Expand All @@ -283,33 +288,44 @@ protected function runUpdaterAndExit()
$doneTemplate = '@CoreUpdater/runUpdaterAndExit_done' . $cli;
$viewWelcome = new View($welcomeTemplate);
$viewDone = new View($doneTemplate);
$doExecuteUpdates = Common::getRequestVar('updateCorePlugins', 0, 'integer') == 1;

if(is_null($doDryRun)) {
$doDryRun = !$doExecuteUpdates;
}

if($doDryRun) {
$viewWelcome->queries = $updater->getSqlQueriesToExecute();
$viewWelcome->isMajor = $updater->hasMajorDbUpdate();
$this->doWelcomeUpdates($viewWelcome, $componentsWithUpdateFile);
return $viewWelcome->render();
}

// CLI
if (Common::isPhpCliMode()) {
$this->doWelcomeUpdates($viewWelcome, $componentsWithUpdateFile);
$output = $viewWelcome->render();

if (!$this->coreError && Piwik::getModule() == 'CoreUpdater') {
// Proceed with upgrade in CLI only if user specifically asked for it, or if running console command
$isUpdateRequested = Common::isRunningConsoleCommand() || Piwik::getModule() == 'CoreUpdater';

if (!$this->coreError && $isUpdateRequested) {
$this->doExecuteUpdates($viewDone, $updater, $componentsWithUpdateFile);
$output .= $viewDone->render();
}

return $output;
}

} else {
if (Common::getRequestVar('updateCorePlugins', 0, 'integer') == 1) {
$this->warningMessages = array();
$this->doExecuteUpdates($viewDone, $updater, $componentsWithUpdateFile);
// Web
if ($doExecuteUpdates) {
$this->warningMessages = array();
$this->doExecuteUpdates($viewDone, $updater, $componentsWithUpdateFile);

$this->redirectToDashboardWhenNoError($updater);
$this->redirectToDashboardWhenNoError($updater);

return $viewDone->render();
} else {
$viewWelcome->queries = $updater->getSqlQueriesToExecute();
$viewWelcome->isMajor = $updater->hasMajorDbUpdate();
$this->doWelcomeUpdates($viewWelcome, $componentsWithUpdateFile);
return $viewWelcome->render();
}
return $viewDone->render();
}

exit;
}

Expand Down
6 changes: 6 additions & 0 deletions plugins/CoreUpdater/CoreUpdater.php
Expand Up @@ -32,10 +32,16 @@ public function getListHooksRegistered()
$hooks = array(
'Request.dispatchCoreAndPluginUpdatesScreen' => 'dispatch',
'Updater.checkForUpdates' => 'updateCheck',
'Console.addCommands' => 'addConsoleCommands',
);
return $hooks;
}

public function addConsoleCommands(&$commands)
{
$commands[] = 'Piwik\Plugins\CoreUpdater\Commands\Update';
}

public static function updateComponents(Updater $updater, $componentsWithUpdateFile)
{
$warnings = array();
Expand Down
13 changes: 13 additions & 0 deletions plugins/CoreUpdater/NoUpdatesFoundException.php
@@ -0,0 +1,13 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\CoreUpdater;

class NoUpdatesFoundException extends \Exception {

}
Expand Up @@ -47,7 +47,7 @@

* {{ helpMessage }}
{% else %}
{{ 'CoreUpdater_PiwikHasBeenSuccessfullyUpgraded'|translate }}
*** {{ 'CoreUpdater_PiwikHasBeenSuccessfullyUpgraded'|translate }} ***
{% endif %}

{% endif %}
Expand Down
25 changes: 18 additions & 7 deletions plugins/CoreUpdater/templates/runUpdaterAndExit_welcome_cli.twig
Expand Up @@ -22,16 +22,27 @@

{{ 'CoreUpdater_YourDatabaseIsOutOfDate'|translate }}

{% if coreToUpdate %}
{{ 'CoreUpdater_PiwikWillBeUpgradedFromVersionXToVersionY'|translate(current_piwik_version, new_piwik_version) }}
{% endif %}
{% if coreToUpdate %}
{{ 'CoreUpdater_PiwikWillBeUpgradedFromVersionXToVersionY'|translate(current_piwik_version, new_piwik_version) }}
{% endif %}

{% if pluginNamesToUpdate|length > 0 %}
{%- set listOfPlugins %}{{ pluginNamesToUpdate|implode(', ') }}{% endset %}
{{ 'CoreUpdater_TheFollowingPluginsWillBeUpgradedX'|translate( listOfPlugins) }}
{% endif %}
{%- if pluginNamesToUpdate|length > 0 %}
{%- set listOfPlugins %}{{ pluginNamesToUpdate|implode(', ') }}{% endset %}
{{ 'CoreUpdater_TheFollowingPluginsWillBeUpgradedX'|translate( listOfPlugins) }}
{% endif %}

{# dry run #}
{% if queries is defined and queries is not empty %}
*** Note: this is a Dry Run ***

{% for query in queries %}{{ query|trim }}
{% endfor %}

*** End of Dry Run ***
{% else %}
{{ 'CoreUpdater_TheUpgradeProcessMayTakeAWhilePleaseBePatient'|translate }}
{% endif %}

{% endif %}
{% endautoescape %}

0 comments on commit 8082e28

Please sign in to comment.