From fc12e85f407653ecf9d1c66d32baf360c3dc8f27 Mon Sep 17 00:00:00 2001
From: Thomas Steur <thomas.steur@gmail.com>
Date: Mon, 11 Jan 2016 22:56:51 +0000
Subject: [PATCH] refs #9258 forward php cli options to the archiver commands

---
 CHANGELOG.md                                  |  3 ++-
 core/CliMulti.php                             | 15 +++++++++++++--
 core/CronArchive.php                          | 13 +++++++++----
 plugins/CoreConsole/Commands/CoreArchiver.php |  2 ++
 tests/PHPUnit/Integration/CronArchiveTest.php |  1 -
 5 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 211e0a3169..f76c728b5a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,11 +2,12 @@
 
 This is a changelog for Piwik platform developers. All changes for our HTTP API's, Plugins, Themes, etc will be listed here.
 
-## Piwik 2.15.1
+## Piwik 2.16.0
 
 ### New features
  * New segment `actionType` lets you segment all actions of a given type, eg. `actionType==events` or `actionType==downloads`. Action types values are: `pageviews`, `contents`, `sitesearches`, `events`, `outlinks`, `downloads`
  * The JavaScript Tracker method `PiwikTracker.setDomains()` can now handle paths. This means when setting eg `_paq.push(['setDomains, '*.piwik.org/website1'])` all link that goes to the same domain `piwik.org` but to any other path than `website1/*` will be treated as outlink.
+ * It is now possible to pass an option `php-cli-options` to the `core:archive` command. The given cli options will be forwarded to the actual PHP command. This allows to for example specifiy a different memory limit for the archiving process like this: `./console core:archive --php-cli-options="-d memory_limit=8G"`
 
 ### Internal change
  * When generating a new plugin skeleton via `generate:plugin` command, plugin name must now contain only letters and numbers.
diff --git a/core/CliMulti.php b/core/CliMulti.php
index 4921236f11..ef360f0214 100644
--- a/core/CliMulti.php
+++ b/core/CliMulti.php
@@ -56,6 +56,8 @@ class CliMulti
      */
     private $urlToPiwik = null;
 
+    private $phpCliOptions = '';
+
     public function __construct()
     {
         $this->supportsAsync = $this->supportsAsync();
@@ -89,6 +91,15 @@ class CliMulti
         return $results;
     }
 
+    /**
+     * Forwards the given configuration options to the PHP cli command.
+     * @param string $phpCliOptions  eg "-d memory_limit=8G -c=path/to/php.ini"
+     */
+    public function setPhpCliConfigurationOptions($phpCliOptions)
+    {
+        $this->phpCliOptions = (string) $phpCliOptions;
+    }
+
     /**
      * Ok, this sounds weird. Why should we care about ssl certificates when we are in CLI mode? It is needed for
      * our simple fallback mode for Windows where we initiate HTTP requests instead of CLI.
@@ -142,8 +153,8 @@ class CliMulti
         $bin = $this->findPhpBinary();
         $superuserCommand = $this->runAsSuperUser ? "--superuser" : "";
 
-        return sprintf('%s %s/console climulti:request -q --piwik-domain=%s %s %s > %s 2>&1 &',
-                       $bin, PIWIK_INCLUDE_PATH, escapeshellarg($hostname), $superuserCommand, escapeshellarg($query), $outputFile);
+        return sprintf('%s %s %s/console climulti:request -q --piwik-domain=%s %s %s > %s 2>&1 &',
+                       $bin, $this->phpCliOptions, PIWIK_INCLUDE_PATH, escapeshellarg($hostname), $superuserCommand, escapeshellarg($query), $outputFile);
     }
 
     private function getResponse()
diff --git a/core/CronArchive.php b/core/CronArchive.php
index c929f88809..1c5a5154e9 100644
--- a/core/CronArchive.php
+++ b/core/CronArchive.php
@@ -128,6 +128,12 @@ class CronArchive
      */
     public $shouldStartProfiler = false;
 
+    /**
+     * Given options will be forwarded to the PHP command if the archiver is executed via CLI.
+     * @var string
+     */
+    public $phpCliConfigurationOptions = '';
+
     /**
      * If HTTP requests are used to initiate archiving, this controls whether invalid SSL certificates should
      * be accepted or not by each request.
@@ -896,9 +902,7 @@ class CronArchive
         $this->requests += count($urls);
 
         $cliMulti = $this->makeCliMulti();
-        $cliMulti->setAcceptInvalidSSLCertificate($this->acceptInvalidSSLCertificate);
         $cliMulti->setConcurrentProcessesLimit($this->getConcurrentRequestsPerWebsite());
-        $cliMulti->runAsSuperUser();
         $response = $cliMulti->request($urls);
 
         foreach ($urls as $index => $url) {
@@ -979,8 +983,6 @@ class CronArchive
 
         try {
             $cliMulti  = $this->makeCliMulti();
-            $cliMulti->setAcceptInvalidSSLCertificate($this->acceptInvalidSSLCertificate);
-            $cliMulti->runAsSuperUser();
             $responses = $cliMulti->request(array($url));
 
             $response  = !empty($responses) ? array_shift($responses) : null;
@@ -1712,6 +1714,9 @@ class CronArchive
     {
         $cliMulti = StaticContainer::get('Piwik\CliMulti');
         $cliMulti->setUrlToPiwik($this->urlToPiwik);
+        $cliMulti->setPhpCliConfigurationOptions($this->phpCliConfigurationOptions);
+        $cliMulti->setAcceptInvalidSSLCertificate($this->acceptInvalidSSLCertificate);
+        $cliMulti->runAsSuperUser();
         return $cliMulti;
     }
 
diff --git a/plugins/CoreConsole/Commands/CoreArchiver.php b/plugins/CoreConsole/Commands/CoreArchiver.php
index 7f1eb6107d..32f1ba1116 100644
--- a/plugins/CoreConsole/Commands/CoreArchiver.php
+++ b/plugins/CoreConsole/Commands/CoreArchiver.php
@@ -41,6 +41,7 @@ class CoreArchiver extends ConsoleCommand
         $archiver->forceTimeoutPeriod = $input->getOption("force-timeout-for-periods");
         $archiver->shouldArchiveAllPeriodsSince = $input->getOption("force-all-periods");
         $archiver->restrictToDateRange = $input->getOption("force-date-range");
+        $archiver->phpCliConfigurationOptions = $input->getOption("php-cli-options");
 
         $restrictToPeriods = $input->getOption("force-periods");
         $restrictToPeriods = explode(',', $restrictToPeriods);
@@ -116,5 +117,6 @@ class CoreArchiver extends ConsoleCommand
         $command->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");
+        $command->addOption('php-cli-options', null, InputOption::VALUE_OPTIONAL, 'Forwards the PHP configuration options to the PHP CLI command. For example "-d memory_limit=8G". Note: These options are only applied if the archiver actually uses CLI and not HTTP.', $default = '');
     }
 }
diff --git a/tests/PHPUnit/Integration/CronArchiveTest.php b/tests/PHPUnit/Integration/CronArchiveTest.php
index c64f0c3e62..c8f97754fc 100644
--- a/tests/PHPUnit/Integration/CronArchiveTest.php
+++ b/tests/PHPUnit/Integration/CronArchiveTest.php
@@ -8,7 +8,6 @@
 
 namespace Piwik\Tests\Integration;
 
-use Piwik\Archiver\Request;
 use Piwik\CliMulti;
 use Piwik\Container\StaticContainer;
 use Piwik\CronArchive;
-- 
GitLab