From d12d5ea58cd5475295616d4ad12775ae9ce7b757 Mon Sep 17 00:00:00 2001
From: mattab <matthieu.aubry@gmail.com>
Date: Tue, 19 Nov 2013 16:24:43 +1300
Subject: [PATCH] Refs #4309 when --force-idsites is set, really force the site
 to be processed

---
 core/CronArchive.php  | 53 ++++++++++++++++++++++++++++++++++++++++---
 misc/cron/archive.php | 42 ++--------------------------------
 2 files changed, 52 insertions(+), 43 deletions(-)

diff --git a/core/CronArchive.php b/core/CronArchive.php
index df36a9df4f..8ce739f5d8 100644
--- a/core/CronArchive.php
+++ b/core/CronArchive.php
@@ -21,6 +21,47 @@ use Exception;
  */
 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 only 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 will 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-idsites=1,2,n
+			Restricts archiving to the specified website IDs, comma separated list.
+	--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 use Piwik to track dozens/hundreds of websites, please let the team know at hello@piwik.org
+	  it makes us happy to learn successful user stories :)
+	* Enjoy!
+";
+    }
+
     // the url can be set here before the init, and it will be used instead of --url=
     static public $url = false;
 
@@ -166,6 +207,11 @@ class CronArchive
                 $shouldArchivePeriods = true;
             }
 
+            $websiteIdIsForced = in_array($idsite, $this->shouldArchiveSpecifiedSites);
+            if($websiteIdIsForced) {
+                $shouldArchivePeriods = true;
+            }
+
             // Test if we should process this website at all
             $elapsedSinceLastArchiving = time() - $lastTimestampWebsiteProcessedDay;
 
@@ -178,7 +224,9 @@ class CronArchive
             $skipDayArchive = $skipDayArchive && !$websiteIsOldDataInvalidate;
 
             // Also reprocess when day has ended since last run
-            if($dayHasEndedMustReprocess && !$existingArchiveIsValid) {
+            if($dayHasEndedMustReprocess
+                && !$existingArchiveIsValid
+                && !$websiteIdIsForced) {
                 $skipDayArchive = false;
             }
 
@@ -611,8 +659,7 @@ class CronArchive
      */
     private function usage()
     {
-        global $USAGE;
-        echo $USAGE;
+        echo self::getUsage();
     }
 
     /**
diff --git a/misc/cron/archive.php b/misc/cron/archive.php
index f448ee5868..fab800af39 100644
--- a/misc/cron/archive.php
+++ b/misc/cron/archive.php
@@ -10,47 +10,9 @@
  */
 
 namespace Piwik;
-use Exception;
-
-$USAGE = "
-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 only 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 will 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-idsites=1,2,n
-			Restricts archiving to the specified website IDs, comma separated list.
-	--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 use Piwik to track dozens/hundreds of websites, please let the team know at hello@piwik.org
-	  it makes us happy to learn successful user stories :)
-	* Enjoy!
+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
@@ -77,7 +39,7 @@ if(!defined('PIWIK_MODE_ARCHIVE')) {
 
 require_once PIWIK_INCLUDE_PATH . "/index.php";
 
-$archiving = new CronArchive;
+$archiving = new CronArchive();
 try {
     $archiving->init();
     $archiving->run();
-- 
GitLab