From cba1a60e0c16ab12da77efdc91ee783bc02b8b52 Mon Sep 17 00:00:00 2001
From: mattab <matthieu.aubry@gmail.com>
Date: Tue, 18 Feb 2014 12:16:17 +1300
Subject: [PATCH] Add new piwik-domain generic option to the console.

---
 core/CliMulti.php                | 10 ++++++----
 core/CliMulti/RequestCommand.php | 11 +++++++++--
 core/Console.php                 |  5 +++++
 core/Url.php                     | 16 +++++++++++++---
 misc/cron/archive.php            |  9 ---------
 5 files changed, 33 insertions(+), 18 deletions(-)

diff --git a/core/CliMulti.php b/core/CliMulti.php
index 3239061661..42285fd028 100644
--- a/core/CliMulti.php
+++ b/core/CliMulti.php
@@ -90,12 +90,12 @@ class CliMulti {
         }
     }
 
-    private function buildCommand($query, $outputFile)
+    private function buildCommand($hostname, $query, $outputFile)
     {
         $bin = $this->findPhpBinary();
 
-        return sprintf('%s -q %s/console climulti:request %s > %s 2>&1 &',
-                       $bin, PIWIK_INCLUDE_PATH, escapeshellarg($query), $outputFile);
+        return sprintf('%s -q %s/console climulti:request --piwik-domain=%s %s > %s 2>&1 &',
+                       $bin, PIWIK_INCLUDE_PATH, escapeshellarg($hostname), escapeshellarg($query), $outputFile);
     }
 
     private function getResponse()
@@ -223,8 +223,10 @@ class CliMulti {
 
         $url     = $this->appendTestmodeParamToUrlIfNeeded($url);
         $query   = Url::getQueryFromUrl($url, array('pid' => $cmdId));
-        $command = $this->buildCommand($query, $output->getPathToFile());
+        $hostname = parse_url($url, PHP_URL_HOST);
+        $command = $this->buildCommand($hostname, $query, $output->getPathToFile());
 
+        Log::debug($command);
         shell_exec($command);
     }
 
diff --git a/core/CliMulti/RequestCommand.php b/core/CliMulti/RequestCommand.php
index 2a5a567bf4..a9ddd51a3c 100644
--- a/core/CliMulti/RequestCommand.php
+++ b/core/CliMulti/RequestCommand.php
@@ -9,6 +9,7 @@
 namespace Piwik\CliMulti;
 
 use Piwik\Plugin\ConsoleCommand;
+use Piwik\Url;
 use Symfony\Component\Console\Input\InputArgument;
 use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Input\InputOption;
@@ -26,13 +27,19 @@ class RequestCommand extends ConsoleCommand
     {
         $this->setName('climulti:request');
         $this->setDescription('Parses and executes the given query. See Piwik\CliMulti. Intended only for system usage.');
-        $this->addArgument('url', InputArgument::OPTIONAL, 'Piwik URL, for instance "module=API&method=API.getPiwikVersion&token_auth=123456789"', '');
+        $this->addArgument('url', null, InputOption::VALUE_REQUIRED, 'Piwik URL query string, for instance: "module=API&method=API.getPiwikVersion&token_auth=123456789"');
     }
 
     protected function execute(InputInterface $input, OutputInterface $output)
     {
         $_GET = array();
-        FrontController::assignCliParametersToRequest();
+
+        $hostname = $input->getOption('piwik-domain');
+        Url::setHost($hostname);
+
+        $url = $input->getArgument('url');
+        $query = Url::getQueryStringFromUrl($url);
+        parse_str($query, $_GET);
 
         if ($this->isTestModeEnabled()) {
             Config::getInstance()->setTestEnvironment();
diff --git a/core/Console.php b/core/Console.php
index af0a62bc9b..54bfcc0938 100644
--- a/core/Console.php
+++ b/core/Console.php
@@ -9,12 +9,17 @@
 namespace Piwik;
 
 use Symfony\Component\Console\Application;
+use Symfony\Component\Console\Input\InputOption;
 
 class Console
 {
     public function run()
     {
         $console  = new Application();
+        $option = new InputOption('piwik-domain', null, InputOption::VALUE_OPTIONAL, 'Piwik URL (protocol and domain) eg. "http://piwik.example.org"');
+
+        $console->getDefinition()->addOption($option);
+
         $commands = $this->getAvailableCommands();
 
         foreach ($commands as $command) {
diff --git a/core/Url.php b/core/Url.php
index a5f90140ce..0aa5e88590 100644
--- a/core/Url.php
+++ b/core/Url.php
@@ -434,18 +434,28 @@ class Url
         return $query;
     }
 
+    static public function getQueryStringFromUrl($url)
+    {
+        return parse_url($url, PHP_URL_QUERY);
+    }
+
+    static public function getHostFromUrl($url)
+    {
+        return parse_url($url, PHP_URL_HOST);
+    }
+
     /**
      * Returns the query part from any valid url and adds additional parameters to the query part if needed.
      *
-     * @param string $aUrl    Any url eg `"http://example.com/piwik/?foo=bar"`
+     * @param string $url    Any url eg `"http://example.com/piwik/?foo=bar"`
      * @param array $additionalParamsToAdd    If not empty the given parameters will be added to the query.
      *
      * @return string eg. `"foo=bar&foo2=bar2"`
      * @api
      */
-    static public function getQueryFromUrl($aUrl, array $additionalParamsToAdd)
+    static public function getQueryFromUrl($url, array $additionalParamsToAdd)
     {
-        $url   = @parse_url($aUrl);
+        $url   = @parse_url($url);
         $query = '';
 
         if (!empty($url['query'])) {
diff --git a/misc/cron/archive.php b/misc/cron/archive.php
index fab800af39..dcc5fdaa4d 100644
--- a/misc/cron/archive.php
+++ b/misc/cron/archive.php
@@ -13,15 +13,6 @@ namespace Piwik;
 
 use Exception;
 
-/*
-Ideas for improvements:
-	- Known limitation: when adding new segments to preprocess, script will assume that data was processed for this segment in the past
-      Workaround: run --force-all-websites --force-all-periods=10000000 to archive everything.
-	- Possible performance improvement
-      - Run first websites which are faster to process (weighted by visits and/or time to generate the last daily report)
-	    This would make sure that huge websites do not 'block' processing of smaller websites' reports.
-*/
-
 if (!defined('PIWIK_INCLUDE_PATH')) {
     define('PIWIK_INCLUDE_PATH', realpath(dirname(__FILE__) . "/../.."));
 }
-- 
GitLab