Skip to content

Commit

Permalink
refs matomo-org#4903 forward archive.php to new console command
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed Mar 26, 2014
1 parent 2af200d commit c2b6c25
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 91 deletions.
20 changes: 13 additions & 7 deletions console
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#!/usr/bin/env php
<?php
namespace Piwik;
define('PIWIK_DOCUMENT_ROOT', dirname(__FILE__) == '/' ? '' : dirname(__FILE__));
define('PIWIK_INCLUDE_PATH', PIWIK_DOCUMENT_ROOT);
define('PIWIK_USER_PATH', PIWIK_DOCUMENT_ROOT);
if (!defined('PIWIK_DOCUMENT_ROOT')) {
define('PIWIK_DOCUMENT_ROOT', dirname(__FILE__) == '/' ? '' : dirname(__FILE__));
}
if (!defined('PIWIK_INCLUDE_PATH')) {
define('PIWIK_INCLUDE_PATH', PIWIK_DOCUMENT_ROOT);
}
if (!defined('PIWIK_USER_PATH')) {
define('PIWIK_USER_PATH', PIWIK_DOCUMENT_ROOT);
}

require_once PIWIK_INCLUDE_PATH . '/core/testMinimumPhpVersion.php';
require_once file_exists(PIWIK_INCLUDE_PATH . '/vendor/autoload.php')
Expand All @@ -12,11 +17,12 @@ require_once file_exists(PIWIK_INCLUDE_PATH . '/vendor/autoload.php')
require_once PIWIK_INCLUDE_PATH . '/core/Loader.php';
require_once PIWIK_INCLUDE_PATH . '/libs/upgradephp/upgrade.php';

Translate::loadEnglishTranslation();
Piwik\Translate::loadEnglishTranslation();

if (!Common::isPhpCliMode()) {
if (!Piwik\Common::isPhpCliMode()) {
exit;
}
$console = new Console();

$console = new Piwik\Console();
$console->init();
$console->run();
60 changes: 1 addition & 59 deletions core/CronArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,6 @@
*/
class CronArchive
{
static public function getUsage()
{
return "Usage:
/path/to/cli/php \"" . @$_SERVER['argv'][0] . "\" --url=http://your-website.org/path/to/piwik/ [arguments]
Arguments:
--url=[piwik-server-url]
Mandatory argument. Must be set to the Piwik base URL.
For example: --url=http://analytics.example.org/ or --url=https://example.org/piwik/
--force-all-websites
If specified, the script will trigger archiving on all websites and all past dates.
You may use --force-all-periods=[seconds] to trigger archiving on those websites that had visits in the last [seconds] seconds.
--force-all-periods[=seconds]
Limits archiving to websites with some traffic in the last [seconds] seconds.
For example --force-all-periods=86400 will archive websites that had visits in the last 24 hours.
If [seconds] is not specified, all websites with visits in the last ". CronArchive::ARCHIVE_SITES_WITH_TRAFFIC_SINCE
. " seconds (" . round( CronArchive::ARCHIVE_SITES_WITH_TRAFFIC_SINCE/86400 ) ." days) will be archived.
--force-timeout-for-periods=[seconds]
The current week/ current month/ current year will be processed at most every [seconds].
If not specified, defaults to ". CronArchive::SECONDS_DELAY_BETWEEN_PERIOD_ARCHIVES.".
--force-date-last-n=M
This script calls the API with period=lastN. You can force the N in lastN by specifying this value.
--force-idsites=1,2,n
Restricts archiving to the specified website IDs, comma separated list.
--skip-idsites=1,2,n
If the specified websites IDs were to be archived, skip them instead.
--disable-scheduled-tasks
Skips executing Scheduled tasks (sending scheduled reports, db optimization, etc.).
--xhprof
Enables XHProf profiler for this archive.php run. Requires XHPRof (see tests/README.xhprof.md).
--accept-invalid-ssl-certificate
It is _NOT_ recommended to use this argument. Instead, you should use a valid SSL certificate!
It can be useful if you specified --url=https://... or if you are using Piwik with force_ssl=1
--help
Displays usage
Notes:
* It is recommended to run the script with the argument --url=[piwik-server-url] only. Other arguments are not required.
* This script should be executed every hour via crontab, or as a deamon.
* You can also run it via http:// by specifying the Super User &token_auth=XYZ as a parameter ('Web Cron'),
but it is recommended to run it via command line/CLI instead.
* If you have any suggestion about this script, please let the team know at [email protected]
* Enjoy!
";
}

// the url can be set here before the init, and it will be used instead of --url=
static public $url = false;

Expand Down Expand Up @@ -141,7 +95,6 @@ static public function lastRunKey($idsite, $period)
public function init()
{
// Note: the order of methods call matters here.
$this->displayHelp();
$this->initPiwikHost();
$this->initLog();
$this->initCore();
Expand Down Expand Up @@ -725,7 +678,6 @@ private function initLog()

if (!function_exists("curl_multi_init")) {
$this->log("ERROR: this script requires curl extension php_curl enabled in your CLI php.ini");
$this->usage();
exit;
}
}
Expand Down Expand Up @@ -763,16 +715,6 @@ private function initCore()
}
}

private function displayHelp()
{
$displayHelp = $this->getParameterFromCli('help') || $this->getParameterFromCli('h');

if ($displayHelp) {
$this->usage();
exit;
}
}

/**
* Initializes the various parameters to the script, based on input parameters.
*
Expand Down Expand Up @@ -1150,7 +1092,7 @@ protected function setSiteIsArchived($idsite)

private function logFatalErrorUrlExpected()
{
$this->logFatalError("archive.php expects the argument 'url' to be set to your Piwik URL, for example: url=http://example.org/piwik/ "
$this->logFatalError("archive.php expects the argument 'url' to be set to your Piwik URL, for example: --url=http://example.org/piwik/ "
. "\n--help for more information", $backtrace = false);
}

Expand Down
44 changes: 22 additions & 22 deletions misc/cron/archive.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@
* @package Piwik
*/

namespace Piwik;

use Exception;

if (!defined('PIWIK_INCLUDE_PATH')) {
define('PIWIK_INCLUDE_PATH', realpath(dirname(__FILE__) . "/../.."));
$callee = array_shift($_SERVER['argv']);
$args = array($callee);
$args[] = 'core:archive';
foreach ($_SERVER['argv'] as $arg) {
if (0 === strpos($arg, '--')) {
$args[] = $arg;
} elseif (0 === strpos($arg, '-')) {
$args[] = '-' . $arg;
} else {
$args[] = '--' . $arg;
}
}

if (!defined('PIWIK_USER_PATH')) {
define('PIWIK_USER_PATH', PIWIK_INCLUDE_PATH);
}
$_SERVER['argv'] = $args;

define('PIWIK_ENABLE_DISPATCH', false);
define('PIWIK_ENABLE_ERROR_HANDLER', false);
define('PIWIK_ENABLE_SESSION_START', false);
$piwikHome = realpath(dirname(__FILE__) . "/../..");

require_once PIWIK_INCLUDE_PATH . "/index.php";

$archiving = new CronArchive();
try {
$archiving->init();
$archiving->run();
$archiving->runScheduledTasks();
$archiving->end();
} catch (Exception $e) {
$archiving->logFatalError($e->getMessage());
if (false !== strpos($callee, 'archive.php')) {
echo "
-------------------------------------------------------
Using this 'archive.php' script is no longer recommended.
Please use '/path/to/php $piwikHome/console core:archive " . implode(' ', array_slice($args, 2)) . "' instead.
-------------------------------------------------------
\n\n";
}

include $piwikHome . '/console';
24 changes: 21 additions & 3 deletions plugins/CoreConsole/Commands/CoreArchiver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/
namespace Piwik\Plugins\CoreConsole\Commands;

use Piwik\CliMulti;
use Piwik\CronArchive;
use Piwik\Plugin\ConsoleCommand;
use Symfony\Component\Console\Input\InputArgument;
Expand All @@ -20,12 +19,31 @@ class CoreArchiver extends ConsoleCommand
protected function configure()
{
$this->setName('core:archive');
$this->setDescription('Runs the CLI archiver');
$this->addArgument('config', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'Any parameters supported by the CronArchiver. Eg ./console core:archive url=http://example.org/piwik', array());
$this->setDescription("Runs the CLI archiver. It usually runs as a cron and is a useful tool for general maintenance, and pre-process reports for a Fast dashboard rendering.");
$this->setHelp("* It is recommended to run the script with the option --piwik-domain=[piwik-server-url] only. Other options are not required.
* This script should be executed every hour via crontab, or as a daemon.
* You can also run it via http:// by specifying the Super User &token_auth=XYZ as a parameter ('Web Cron'),
but it is recommended to run it via command line/CLI instead.
* If you have any suggestion about this script, please let the team know at [email protected]
* Enjoy!");
$this->addOption('url', null, InputOption::VALUE_REQUIRED, "Mandatory option as an alternative to '--piwik-domain'. Must be set to the Piwik base URL.\nFor example: --url=http://analytics.example.org/ or --url=https://example.org/piwik/");
$this->addOption('force-all-websites', null, InputOption::VALUE_NONE, "If specified, the script will trigger archiving on all websites and all past dates.\nYou may use --force-all-periods=[seconds] to trigger archiving on those websites\nthat had visits in the last [seconds] seconds.");
$this->addOption('force-all-periods', null, InputOption::VALUE_OPTIONAL, "Limits archiving to websites with some traffic in the last [seconds] seconds. \nFor example --force-all-periods=86400 will archive websites that had visits in the last 24 hours. \nIf [seconds] is not specified, all websites with visits in the last " . CronArchive::ARCHIVE_SITES_WITH_TRAFFIC_SINCE . "\n seconds (" . round( CronArchive::ARCHIVE_SITES_WITH_TRAFFIC_SINCE/86400 ) ." days) will be archived.");
$this->addOption('force-timeout-for-periods', null, InputOption::VALUE_OPTIONAL, "The current week/ current month/ current year will be processed at most every [seconds].\nIf not specified, defaults to ". CronArchive::SECONDS_DELAY_BETWEEN_PERIOD_ARCHIVES.".");
$this->addOption('force-date-last-n', null, InputOption::VALUE_REQUIRED, "This script calls the API with period=lastN. You can force the N in lastN by specifying this value.");
$this->addOption('force-idsites', null,InputOption::VALUE_REQUIRED, "Restricts archiving to the specified website IDs, comma separated list.");
$this->addOption('skip-idsites', null, InputOption::VALUE_REQUIRED, "If the specified websites IDs were to be archived, skip them instead.");
$this->addOption('disable-scheduled-tasks', null, InputOption::VALUE_NONE, "Skips executing Scheduled tasks (sending scheduled reports, db optimization, etc.).");
$this->addOption('xhprof', null, InputOption::VALUE_NONE, "Enables XHProf profiler for this archive.php run. Requires XHPRof (see tests/README.xhprof.md).");
$this->addOption('accept-invalid-ssl-certificate', null, InputOption::VALUE_NONE, "It is _NOT_ recommended to use this argument. Instead, you should use a valid SSL certificate!\nIt can be useful if you specified --url=https://... or if you are using Piwik with force_ssl=1");
}

protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getOption('piwik-domain') && !$input->getOption('url')) {
$_SERVER['argv'][] = '--url=' . $input->getOption('piwik-domain');
}

$this->initEnv();

$archiving = new CronArchive();
Expand Down

0 comments on commit c2b6c25

Please sign in to comment.