Skip to content
Extraits de code Groupes Projets
Valider ff8fa3f3 rédigé par diosmosis's avatar diosmosis
Parcourir les fichiers

Add cron archiving API method only visible to superusers, use this in...

Add cron archiving API method only visible to superusers, use this in archive.php script and re-add php-cgi support to Console, which did not get merged in the past for some reason.
parent e2d14cd5
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -28,7 +28,7 @@ class Console extends Application
public function __construct()
{
$this->checkCompatibility();
$this->setServerArgsIfPhpCgi();
parent::__construct();
......@@ -130,13 +130,22 @@ class Console extends Application
return $commands;
}
private function checkCompatibility()
private function setServerArgsIfPhpCgi()
{
if (Common::isPhpCgiType()) {
echo 'Piwik Console is known to be not compatible with PHP-CGI (you are using '.php_sapi_name().'). ' .
'Please execute console using PHP-CLI. For instance "/usr/bin/php-cli console ..."';
echo "\n";
exit(1);
$_SERVER['argv'] = array();
foreach ($_GET as $name => $value) {
$argument = $name;
if (!empty($value)) {
$argument .= '=' . $value;
}
$_SERVER['argv'][] = $argument;
}
if (!defined('STDIN')) {
define('STDIN', fopen('php://stdin','r'));
}
}
}
......
......@@ -27,10 +27,10 @@ if (!defined('PIWIK_USER_PATH')) {
define('PIWIK_USER_PATH', PIWIK_INCLUDE_PATH);
}
define('PIWIK_ENABLE_DISPATCH', false);
define('PIWIK_ENABLE_ERROR_HANDLER', false);
define('PIWIK_ENABLE_SESSION_START', false);
require_once PIWIK_INCLUDE_PATH . "/index.php";
require_once PIWIK_INCLUDE_PATH . "/core/bootstrap.php";
if (!empty($_SERVER['argv'][0])) {
$callee = $_SERVER['argv'][0];
......@@ -52,8 +52,9 @@ try 'php archive.php --url=http://your.piwik/path'
-------------------------------------------------------
\n\n";
}
if (isset($_SERVER['argv']) && Piwik\Console::isSupported()) {
// TODO: manual test against php-cgi + ArchiveWebTest
// => test w/ token auth & w/o token auth
if (Piwik\Common::isPhpCliMode()) {
$console = new Piwik\Console();
// manipulate command line arguments so CoreArchiver command will be executed
......@@ -64,41 +65,15 @@ if (isset($_SERVER['argv']) && Piwik\Console::isSupported()) {
$console->run();
} else { // if running via web request, use CronArchive directly
if (Piwik\Common::isPhpCliMode()) {
// We can run the archive in CLI with `php-cgi` so we have to configure the container/logger
// just like for CLI
$environment = new \Piwik\Application\Environment('cli');
$environment->init();
/** @var ConsoleHandler $consoleLogHandler */
$consoleLogHandler = StaticContainer::get('Symfony\Bridge\Monolog\Handler\ConsoleHandler');
$consoleLogHandler->setOutput(new ConsoleOutput(OutputInterface::VERBOSITY_VERBOSE));
} else {
// HTTP request: logs needs to be dumped in the HTTP response (on top of existing log destinations)
$environment = new \Piwik\Application\Environment(null);
$environment->init();
/** @var \Monolog\Logger $logger */
$logger = StaticContainer::get('Psr\Log\LoggerInterface');
$handler = new StreamHandler('php://output', Logger::INFO);
$handler->setFormatter(StaticContainer::get('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter'));
$logger->pushHandler($handler);
}
\Piwik\FrontController::getInstance()->init();
$archiver = new Piwik\CronArchive();
if (!Piwik\Common::isPhpCliMode()) {
$token_auth = Piwik\Common::getRequestVar('token_auth', '', 'string');
// HTTP request: logs needs to be dumped in the HTTP response (on top of existing log destinations)
/** @var \Monolog\Logger $logger */
$logger = StaticContainer::get('Psr\Log\LoggerInterface');
$handler = new StreamHandler('php://output', Logger::INFO);
$handler->setFormatter(StaticContainer::get('Piwik\Plugins\Monolog\Formatter\LineMessageFormatter'));
$logger->pushHandler($handler);
if (!$archiver->isTokenAuthSuperUserToken($token_auth)) {
die('<b>You must specify the Super User token_auth as a parameter to this script, eg. <code>?token_auth=XYZ</code> if you wish to run this script through the browser. </b><br>
However it is recommended to run it <a href="http://piwik.org/docs/setup-auto-archiving/">via cron in the command line</a>, since it can take a long time to run.<br/>
In a shell, execute for example the following to trigger archiving on the local Piwik server:<br/>
<code>$ /path/to/php /path/to/piwik/console core:archive --url=http://your-website.org/path/to/piwik/</code>');
}
}
$_GET['module'] = 'API';
$_GET['method'] = 'CoreAdminHome.runCronArchiving';
$archiver->main();
require_once PIWIK_INCLUDE_PATH . "/index.php";
}
\ No newline at end of file
......@@ -11,6 +11,7 @@ namespace Piwik\Plugins\CoreAdminHome;
use Exception;
use Piwik\Container\StaticContainer;
use Piwik\Archive\ArchiveInvalidator;
use Piwik\CronArchive;
use Piwik\Db;
use Piwik\Piwik;
use Piwik\Scheduler\Scheduler;
......@@ -85,5 +86,16 @@ class API extends \Piwik\Plugin\API
return $output;
}
/**
* Initiates cron archiving via web request.
*
* @hideExceptForSuperUser
*/
public function runCronArchiving()
{
Piwik::checkUserHasSuperUserAccess();
}
$archiver = new CronArchive();
$archiver->main();
}
}
\ No newline at end of file
......@@ -25,24 +25,14 @@ class CoreArchiver extends ConsoleCommand
protected function execute(InputInterface $input, OutputInterface $output)
{
$url = $input->getOption('url') ?: $input->getOption('piwik-domain');
if (empty($url)) {
throw new \InvalidArgumentException("The --url argument is not set. It should be set to your Piwik URL, for example: --url=http://example.org/piwik/.");
}
if (is_string($url) && $url && in_array($url, array('http://', 'https://'))) {
// see https://github.com/piwik/piwik/issues/5180 and http://forum.piwik.org/read.php?2,115274
throw new \InvalidArgumentException('No valid URL given. If you have specified a valid URL try --piwik-domain instead of --url');
}
$archiver = self::makeArchiver($url, $input);
$archiver = self::makeArchiver("", $input);
$archiver->main();
}
// also used by another console command
public static function makeArchiver($url, InputInterface $input)
public static function makeArchiver($url, InputInterface $input) // TODO: remove url from this function
{
$archiver = new CronArchive($url);
$archiver = new CronArchive();
$archiver->disableScheduledTasks = $input->getOption('disable-scheduled-tasks');
$archiver->acceptInvalidSSLCertificate = $input->getOption("accept-invalid-ssl-certificate");
......@@ -81,7 +71,7 @@ class CoreArchiver extends ConsoleCommand
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 feedback@piwik.org
* Enjoy!");
$command->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/");
$command->addOption('url', null, InputOption::VALUE_REQUIRED, "Deprecated.");
$command->addOption('force-all-websites', null, InputOption::VALUE_NONE, "If specified, the script will trigger archiving on all websites.\nUse with --force-all-periods=[seconds] to also process those websites that had visits in the last [seconds] seconds.\nLaunching several processes with this option will make them share the list of sites to process.");
$command->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 . " seconds (" . round(CronArchive::ARCHIVE_SITES_WITH_TRAFFIC_SINCE / 86400) . " days) will be archived.");
$command->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 . ".");
......
......@@ -58,7 +58,7 @@ class ArchiveWebTest extends SystemTestCase
{
list($returnCode, $output) = $this->runArchivePhpScriptWithPhpCgi();
$this->assertEquals(0, $returnCode);
$this->assertEquals(0, $returnCode, "Output: " . $output);
$this->assertWebArchivingDone($output, $checkArchivedSite = false);
}
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter