1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik;
use Exception;
use Piwik\ArchiveProcessor\Loader;
use Piwik\ArchiveProcessor\Parameters;
use Piwik\ArchiveProcessor\Rules;
use Piwik\CliMulti\Process;
use Piwik\Container\StaticContainer;
use Piwik\CronArchive\ArchiveFilter;
use Piwik\CronArchive\FixedSiteIds;
use Piwik\CronArchive\Performance\Logger;
use Piwik\Archive\ArchiveInvalidator;
use Piwik\CliMulti\RequestParser;
use Piwik\CronArchive\QueueConsumer;
use Piwik\CronArchive\SharedSiteIds;
use Piwik\CronArchive\StopArchiverException;
use Piwik\DataAccess\ArchiveSelector;
use Piwik\DataAccess\ArchiveTableCreator;
use Piwik\DataAccess\ArchiveWriter;
use Piwik\DataAccess\Model;
use Piwik\DataAccess\RawLogDao;
use Piwik\Exception\UnexpectedWebsiteFoundException;
use Piwik\Metrics\Formatter;
use Piwik\Period\Factory as PeriodFactory;
use Piwik\CronArchive\SegmentArchiving;
use Piwik\Period\Range;
use Piwik\Plugins\CoreAdminHome\API as CoreAdminHomeAPI;
use Piwik\Plugins\Monolog\Processor\ExceptionToTextProcessor;
use Piwik\Plugins\SitesManager\API as APISitesManager;
use Piwik\Plugins\UsersManager\API as APIUsersManager;
use Piwik\Plugins\UsersManager\UserPreferences;
use Psr\Log\LoggerInterface;
/**
* ./console core:archive runs as a cron and is a useful tool for general maintenance,
* and pre-process reports for a Fast dashboard rendering.
*/
class CronArchive
{
// the url can be set here before the init, and it will be used instead of --url=
const CRON_INVALIDATION_TIME_OPTION_NAME = 'CronArchive.lastInvalidationTime';
public static $url = false;
const TABLES_WITH_INVALIDATED_ARCHIVES = 'CronArchive.getTablesWithInvalidatedArchives';
const TABLES_WITH_INVALIDATED_ARCHIVES_TTL = 3600;
// Max parallel requests for a same site's segments
const MAX_CONCURRENT_API_REQUESTS = 3;
// force-timeout-for-periods default (1 hour)
const SECONDS_DELAY_BETWEEN_PERIOD_ARCHIVES = 3600;
// Flag to know when the archive cron is calling the API
const APPEND_TO_API_REQUEST = '&trigger=archivephp';
// Flag used to record timestamp in Option::
const OPTION_ARCHIVING_FINISHED_TS = "LastCompletedFullArchiving";
// Name of option used to store starting timestamp
const OPTION_ARCHIVING_STARTED_TS = "LastFullArchivingStartTime";
// Show only first N characters from Piwik API output in case of errors
const TRUNCATE_ERROR_MESSAGE_SUMMARY = 6000;
// By default, we only process the current week/month/year at most once an hour
private $todayArchiveTimeToLive;
private $allWebsites = array();
/**
* @var FixedSiteIds|SharedSiteIds
*/
private $websiteIdArchiveList;
private $requests = 0;
private $archiveAndRespectTTL = true;
public $shouldArchiveAllSites = false;
private $idSitesNotUsingTracker = [];
/**
* @var Model
*/
private $model;
private $lastSuccessRunTimestamp = false;
private $errors = array();
private $apiToInvalidateArchivedReport;
const NO_ERROR = "no error";
public $testmode = false;
/**
* The list of IDs for sites for whom archiving should be initiated. If supplied, only these
* sites will be archived.
*
* @var int[]
*/
public $shouldArchiveSpecifiedSites = array();
public $shouldSkipSpecifiedSites = [];
/**
* If true, xhprof will be initiated for the archiving run. Only for development/testing.
*
* @var bool
*/
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.
*
* @var bool
*/
public $acceptInvalidSSLCertificate = false;
/**
* If set to true, scheduled tasks will not be run.
*
* @var bool
*/
public $disableScheduledTasks = false;
/**
* Forces CronArchive to invalidate data for the last [$dateLastForced] years when it notices a segment that
* was recently created or updated. By default this is 7.
*
* @var int|false
*/
public $dateLastForced = SegmentArchiving::DEFAULT_BEGINNING_OF_TIME_LAST_N_YEARS;
/**
* The number of concurrent requests to issue per website. Defaults to {@link MAX_CONCURRENT_API_REQUESTS}.
*
* Used when archiving a site's segments concurrently.
*
* @var int|false
*/
public $concurrentRequestsPerWebsite = false;
/**
* The number of concurrent archivers to run at once max.
*
* @var int|false
*/
public $maxConcurrentArchivers = false;
private $archivingStartingTime;
private $formatter;
private $lastDbReset = false;
/**
* @var SegmentArchiving
*/
private $segmentArchiving;
/**
* @var LoggerInterface
*/
private $logger;
/**
* Only used when archiving using HTTP requests.
*
* @var string
*/
private $urlToPiwik = null;
/**
* @var ArchiveInvalidator
*/
private $invalidator;
/**
* @var bool
*/
private $isArchiveProfilingEnabled = false;
/**
* @var array
*/
private $periodIdsToLabels;
/**
* @var ArchiveFilter
*/
private $archiveFilter;
/**
* @var RequestParser
*/
private $cliMultiRequestParser;
/**
* @var bool|mixed
*/
private $supportsAsync;
/**
* Constructor.
*
* @param LoggerInterface|null $logger
*/
public function __construct(LoggerInterface $logger = null)
{
$this->logger = $logger ?: StaticContainer::get('Psr\Log\LoggerInterface');
$this->formatter = new Formatter();
$this->invalidator = StaticContainer::get('Piwik\Archive\ArchiveInvalidator');
$this->isArchiveProfilingEnabled = Config::getInstance()->Debug['archiving_profile'] == 1;
$this->model = StaticContainer::get(Model::class);
$this->periodIdsToLabels = array_flip(Piwik::$idPeriods);
$this->rawLogDao = new RawLogDao();
$this->supportsAsync = $this->makeCliMulti()->supportsAsync();
$this->cliMultiRequestParser = new RequestParser($this->supportsAsync);
$this->archiveFilter = new ArchiveFilter();
}
private function isMaintenanceModeEnabled()
{
return Config::getInstance()->General['maintenance_mode'] == 1;
}
/**
* Initializes and runs the cron archiver.
*/
public function main()
{
if ($this->isMaintenanceModeEnabled()) {
$this->logger->info("Archiving won't run because maintenance mode is enabled");
return;
}
$self = $this;
Access::doAsSuperUser(function () use ($self) {
try {
$self->init();
$self->run();
$self->runScheduledTasks();
$self->end();
} catch (StopArchiverException $e) {
$this->logger->info("Archiving stopped by stop archiver exception");
}
});
}
public function init()
{
$this->segmentArchiving = StaticContainer::get(SegmentArchiving::class);
/**
* This event is triggered during initializing archiving.
*
* @param CronArchive $this
*/
Piwik::postEvent('CronArchive.init.start', array($this));
SettingsServer::setMaxExecutionTime(0);
$this->archivingStartingTime = time();
// Note: the order of methods call matters here.
$this->initStateFromParameters();
$this->logInitInfo();
$this->logArchiveTimeoutInfo();
$idSitesNotUsingTracker = Loader::getSitesNotUsingTracker();
if (!empty($idSitesNotUsingTracker)) {
$this->logger->info("- The following websites do not use the tracker: " . implode(',', $this->idSitesNotUsingTracker));
}
// record archiving start time
Option::set(self::OPTION_ARCHIVING_STARTED_TS, time());
$allWebsites = APISitesManager::getInstance()->getAllSitesId();
$websitesIds = $this->initWebsiteIds($allWebsites);
$this->filterWebsiteIds($websitesIds, $allWebsites);
$this->allWebsites = $websitesIds;
$this->websiteIdArchiveList = $this->makeWebsiteIdArchiveList($websitesIds);
if (method_exists($this->websiteIdArchiveList, 'isContinuingPreviousRun') &&
$this->websiteIdArchiveList->isContinuingPreviousRun()
) {
$this->logger->info("- Continuing ongoing archiving run by pulling from shared idSite queue.");
}
if ($this->archiveFilter) {
$this->archiveFilter->logFilterInfo($this->logger);
}
/**
* This event is triggered after a CronArchive instance is initialized.
*
* @param array $websiteIds The list of website IDs this CronArchive instance is processing.
* This will be the entire list of IDs regardless of whether some have
* already been processed.
*/
Piwik::postEvent('CronArchive.init.finish', [$this->allWebsites]);
}
/**
* Main function, runs archiving on all websites with new activity
*/
public function run()
{
$pid = Common::getProcessId();
$timer = new Timer;
$this->logSection("START");
$this->logger->info("Starting Matomo reports archiving...");
$numArchivesFinished = 0;
if ($this->hasReachedMaxConcurrentArchivers()) {
$this->logger->info("Reached maximum concurrent archivers allowed ({$this->maxConcurrentArchivers}), aborting run.");
return;
}
$this->logger->debug("Applying queued rearchiving...");
\Piwik\Tracker\Cache::withDelegatedCacheClears(function () {
$this->invalidator->applyScheduledReArchiving();
});
$failedJobs = $this->model->resetFailedArchivingJobs();
if ($failedJobs) {
$this->logger->info("Found {failed} failed jobs (ts_invalidated older than 1 day), resetting status to try them again.", [
'failed' => $failedJobs,
]);
}
$countOfProcesses = $this->getMaxConcurrentApiRequests();
$queueConsumer = new QueueConsumer($this->logger, $this->websiteIdArchiveList, $countOfProcesses, $pid,
$this->model, $this->segmentArchiving, $this, $this->cliMultiRequestParser, $this->archiveFilter);
while (true) {
if ($this->isMaintenanceModeEnabled()) {
$this->logger->info("Archiving will stop now because maintenance mode is enabled");
return;
}
if (!Process::isMethodDisabled('getmypid') && !Process::isMethodDisabled('ignore_user_abort')) {
// see https://github.com/matomo-org/wp-matomo/issues/163
flush();
}
try {
$archivesToProcess = $queueConsumer->getNextArchivesToProcess();
} catch (UnexpectedWebsiteFoundException $ex) {
$this->logger->debug("Site {$queueConsumer->getIdSite()} was deleted, skipping to next...");
$queueConsumer->skipToNextSite();
continue;
}
if ($archivesToProcess === null) {
break;
}
if (empty($archivesToProcess)) {
continue;
}
$successCount = $this->launchArchivingFor($archivesToProcess, $queueConsumer);
$numArchivesFinished += $successCount;
}
$this->disconnectDb();
$this->logger->info("Done archiving!");
$this->logSection("SUMMARY");
$this->logger->info("Processed $numArchivesFinished archives.");
$this->logger->info("Total API requests: {$this->requests}");
$this->logger->info("done: " .
$this->requests . " req, " . round($timer->getTimeMs()) . " ms, " .
(empty($this->errors)
? self::NO_ERROR
: (count($this->errors) . " errors."))
);
$this->logger->info($timer->__toString());
}
private function launchArchivingFor($archives, QueueConsumer $queueConsumer)
{
$urls = [];
$archivesBeingQueried = [];
foreach ($archives as $index => $archive) {
list($url, $segment, $plugin) = $this->generateUrlToArchiveFromArchiveInfo($archive);
if (empty($url)) {
// can happen if, for example, a segment was deleted after an archive was invalidated
// in this case, we can just delete the archive entirely.
$this->deleteInvalidatedArchives($archive);
continue;
}
$idSite = $archive['idsite'];
if (!$this->siteExists($idSite)) {
$this->logger->debug("Site $idSite no longer exists, no longer launching archiving.");
$this->deleteInvalidatedArchives($archive);
continue;
}
$dateStr = $archive['period'] == Range::PERIOD_ID ? ($archive['date1'] . ',' . $archive['date2']) : $archive['date1'];
$period = PeriodFactory::build($this->periodIdsToLabels[$archive['period']], $dateStr);
$params = new Parameters(new Site($idSite), $period, new Segment($segment, [$idSite], $period->getDateStart(), $period->getDateEnd()));
if (!empty($plugin)) {
$params->setRequestedPlugin($plugin);
$params->onlyArchiveRequestedPlugin();
}
$loader = new Loader($params);
if ($loader->canSkipThisArchive()) {
$this->logger->info("Found no visits for site ID = {idSite}, {period} ({date1},{date2}), site is using the tracker so skipping archiving...", [
'idSite' => $idSite,
'period' => $this->periodIdsToLabels[$archive['period']],
'date1' => $archive['date1'],
'date2' => $archive['date2'],
]);
// site is using the tracker, but there are no visits for this period, so just delete the archive and move on
$this->deleteInvalidatedArchives($archive);
continue;
}
$this->logger->debug("Starting archiving for {url}", ['url' => $url]);
$urls[] = $url;
$archivesBeingQueried[] = $archive;
}
if (empty($urls)) {
return 0; // all URLs had no visits and were using the tracker
}
$cliMulti = $this->makeCliMulti();
$cliMulti->timeRequests();
$responses = $cliMulti->request($urls);
$this->disconnectDb();
$timers = $cliMulti->getTimers();
$successCount = 0;
foreach ($urls as $index => $url) {
$content = array_key_exists($index, $responses) ? $responses[$index] : null;
$this->checkResponse($content, $url);
$stats = json_decode($content, $assoc = true);
if (!is_array($stats)) {
$this->logger->info(var_export($content, true));
$idinvalidation = $archivesBeingQueried[$index]['idinvalidation'];
$this->model->releaseInProgressInvalidation($idinvalidation);
$queueConsumer->ignoreIdInvalidation($idinvalidation);
$this->logError("Error unserializing the following response from $url: '" . $content . "'");
continue;
}
$visitsForPeriod = $this->getVisitsFromApiResponse($stats);
$this->logArchiveJobFinished($url, $timers[$index], $visitsForPeriod, $archivesBeingQueried[$index]['plugin'], $archivesBeingQueried[$index]['report']);
$this->deleteInvalidatedArchives($archivesBeingQueried[$index]);
$this->repairInvalidationsIfNeeded($archivesBeingQueried[$index]);
++$successCount;
}
$this->requests += count($urls);
return $successCount;
}
private function deleteInvalidatedArchives($archive)
{
$this->model->deleteInvalidations([$archive]);
}
private function generateUrlToArchiveFromArchiveInfo($archive)
{
$plugin = $archive['plugin'];
$report = $archive['report'];
$period = $this->periodIdsToLabels[$archive['period']];
if ($period == 'range') {
$date = $archive['date1'] . ',' . $archive['date2'];
} else {
$date = $archive['date1'];
}
$idSite = $archive['idsite'];
$segment = isset($archive['segment']) ? $archive['segment'] : '';
$url = $this->getVisitsRequestUrl($idSite, $period, $date, $segment, $plugin);
$url = $this->makeRequestUrl($url);
if (!empty($segment)) {
$shouldSkipToday = $this->archiveFilter->isSkipSegmentsForToday() && !$this->wasSegmentChangedRecently($segment,
$this->segmentArchiving->getAllSegments());
if ($shouldSkipToday) {
$url .= '&skipArchiveSegmentToday=1';
}
}
if (!empty($plugin)) {
$url .= "&pluginOnly=1";
}
if (!empty($report)) {
$url .= "&requestedReport=" . urlencode($report);
}
return [$url, $segment, $plugin];
}
private function logArchiveJobFinished($url, $timer, $visits, $plugin = null, $report = null)
{
$params = UrlHelper::getArrayFromQueryString($url);
$visits = (int) $visits;
$this->logger->info("Archived website id {$params['idSite']}, period = {$params['period']}, date = "
. "{$params['date']}, segment = '" . (isset($params['segment']) ? urldecode(urldecode($params['segment'])) : '') . "', "
. ($plugin ? "plugin = $plugin, " : "") . ($report ? "report = $report, " : "") . "$visits visits found. $timer");
}
public function getErrors()
{
return $this->errors;
}
/**
* End of the script
*/
public function end()
{
/**
* This event is triggered after archiving.
*
* @param CronArchive $this
*/
Piwik::postEvent('CronArchive.end', array($this));
if (empty($this->errors)) {
// No error -> Logs the successful script execution until completion
Option::set(self::OPTION_ARCHIVING_FINISHED_TS, time());
return;
}
$this->logSection("SUMMARY OF ERRORS");
foreach ($this->errors as $error) {
// do not logError since errors are already in stderr
$this->logger->info("Error: " . $error);
}
$summary = count($this->errors) . " total errors during this script execution, please investigate and try and fix these errors.";
$this->logFatalError($summary);
}
public function logFatalError($m)
{
$this->logError($m);
throw new Exception($m);
}
public function runScheduledTasks()
{
$this->logSection("SCHEDULED TASKS");
if ($this->disableScheduledTasks) {
$this->logger->info("Scheduled tasks are disabled with --disable-scheduled-tasks");
return;
}
$this->disconnectDb();
// TODO: this is a HACK to get the purgeOutdatedArchives task to work when run below. without
// it, the task will not run because we no longer run the tasks through CliMulti.
// harder to implement alternatives include:
// - moving CronArchive logic to DI and setting a flag in the class when the whole process
// runs
// - setting a new DI environment for core:archive which CoreAdminHome can use to conditionally
// enable/disable the task
$_GET['trigger'] = 'archivephp';
CoreAdminHomeAPI::getInstance()->runScheduledTasks();
$this->logSection("");
}
private function disconnectDb()
{
$twoHoursInSeconds = 60 * 60 * 2;
if (time() > ($this->lastDbReset + $twoHoursInSeconds)) {
// we aim to through DB connections away only after 2 hours
$this->lastDbReset = time();
Db::destroyDatabaseObject();
Tracker::disconnectCachedDbConnection();
}
}
/**
* Returns base URL to process reports for the $idSite on a given $period
*
* @param string $idSite
* @param string $period
* @param string $date
* @param bool|false $segment
* @return string
*/
private function getVisitsRequestUrl($idSite, $period, $date, $segment = false, $plugin = null)
{
$request = "?module=API&method=CoreAdminHome.archiveReports&idSite=$idSite&period=$period&date=" . $date . "&format=json";
if ($segment) {
$request .= '&segment=' . urlencode($segment);
}
if (!empty($plugin)) {
$request .= "&plugin=" . $plugin;
}
return $request;
}
/**
* Logs a section in the output
*
* @param string $title
*/
private function logSection($title = "")
{
$this->logger->info("---------------------------");
if (!empty($title)) {
$this->logger->info($title);
}
}
public function logError($m)
{
if (!defined('PIWIK_ARCHIVE_NO_TRUNCATE')) {
$m = str_replace(array("\n", "\t"), " ", $m);
if (Common::mb_strlen($m) > self::TRUNCATE_ERROR_MESSAGE_SUMMARY) {
$numCharactersKeepFromEnd = 100;
$m = Common::mb_substr($m, 0, self::TRUNCATE_ERROR_MESSAGE_SUMMARY - $numCharactersKeepFromEnd)
. ' ... ' .
Common::mb_substr($m, -1 * $numCharactersKeepFromEnd);
}
}
$this->errors[] = $m;
$this->logger->error($m);
}
private function logNetworkError($url, $response)
{
$message = "Got invalid response from API request: $url. ";
if (empty($response)) {
$message .= "The response was empty. This usually means a server error. A solution to this error is generally to increase the value of 'memory_limit' in your php.ini file. ";
if($this->supportsAsync) {
$message .= " For more information and the error message please check in your PHP CLI error log file. As this core:archive command triggers PHP processes over the CLI, you can find where PHP CLI logs are stored by running this command: php -i | grep error_log";
} else {
$message .= " For more information and the error message please check your web server's error Log file. As this core:archive command triggers PHP processes over HTTP, you can find the error message in your Matomo's web server error logs. ";
}
} else {
$message .= "Response was '$response'";
}
$this->logError($message);
return false;
}
private function checkResponse($response, $url)
{
if (empty($response)
|| stripos($response, 'error') !== false
) {
return $this->logNetworkError($url, $response);
}
return true;
}
/**
* Initializes the various parameters to the script, based on input parameters.
*
*/
private function initStateFromParameters()
{
$this->todayArchiveTimeToLive = Rules::getTodayArchiveTimeToLive();
$this->lastSuccessRunTimestamp = $this->getLastSuccessRunTimestamp();
}
public function filterWebsiteIds(&$websiteIds, $allWebsites)
{
// Keep only the websites that do exist
$websiteIds = array_intersect($websiteIds, $allWebsites);
if (!empty($this->shouldSkipSpecifiedSites)) {
$websiteIds = array_diff($websiteIds, $this->shouldSkipSpecifiedSites);
}
/**
* Triggered by the **core:archive** console command so plugins can modify the priority of
* websites that the archiving process will be launched for.
*
* Plugins can use this hook to add websites to archive, remove websites to archive, or change
* the order in which websites will be archived.
*
* @param array $websiteIds The list of website IDs to launch the archiving process for.
*/
Piwik::postEvent('CronArchive.filterWebsiteIds', array(&$websiteIds));
}
/**
* @internal
* @param $api
*/
public function setApiToInvalidateArchivedReport($api)
{
$this->apiToInvalidateArchivedReport = $api;
}
private function getApiToInvalidateArchivedReport()
{
if ($this->apiToInvalidateArchivedReport) {
return $this->apiToInvalidateArchivedReport;
}
return CoreAdminHomeAPI::getInstance();
}
public function invalidateArchivedReportsForSitesThatNeedToBeArchivedAgain($idSiteToInvalidate)
{
\Piwik\Tracker\Cache::withDelegatedCacheClears(function () use ($idSiteToInvalidate) {
$this->invalidateArchivedReportsForSitesThatNeedToBeArchivedAgainImpl($idSiteToInvalidate);
});
}
private function invalidateArchivedReportsForSitesThatNeedToBeArchivedAgainImpl($idSiteToInvalidate)
{
if (empty($this->segmentArchiving)) {
// might not be initialised if init is not called
$this->segmentArchiving = StaticContainer::get(SegmentArchiving::class);
}
$this->logger->debug("Checking for queued invalidations...");
// invalidate remembered site/day pairs
$sitesPerDays = $this->invalidator->getRememberedArchivedReportsThatShouldBeInvalidated();
krsort($sitesPerDays); // for tests
foreach ($sitesPerDays as $date => $siteIds) {
//Concurrent transaction logic will end up with duplicates set. Adding array_unique to the siteIds.
$siteIds = array_unique($siteIds);
$siteIdsToInvalidate = [];
foreach ($siteIds as $idSite) {
if ($idSite != $idSiteToInvalidate) {
continue;
}
$siteIdsToInvalidate[] = $idSite;
}
if (empty($siteIdsToInvalidate)) {
continue;
}
$listSiteIds = implode(',', $siteIdsToInvalidate);
try {
$this->logger->debug(' Will invalidate archived reports for ' . $date . ' for following websites ids: ' . $listSiteIds);
$this->invalidateWithSegments($siteIdsToInvalidate, $date, $period = 'day');
} catch (Exception $e) {
$message = ExceptionToTextProcessor::getMessageAndWholeBacktrace($e);
$this->logger->info(' Failed to invalidate archived reports: ' . $message);
}
}
// invalidate today if needed for all websites
$this->invalidateRecentDate('today', $idSiteToInvalidate);
// invalidate yesterday archive if the time of the latest valid archive is earlier than today
// (means the day has changed and there might be more visits that weren't processed)
$this->invalidateRecentDate('yesterday', $idSiteToInvalidate);
// invalidate range archives
$dates = $this->getCustomDateRangeToPreProcess($idSiteToInvalidate);
foreach ($dates as $date) {
try {
PeriodFactory::build('range', $date);
} catch (\Exception $ex) {
$this->logger->debug(" Found invalid range date in [General] archiving_custom_ranges: {date}", ['date' => $date]);
continue;
}
$this->logger->debug(' Invalidating custom date range ({date}) for site {idSite}', ['idSite' => $idSiteToInvalidate, 'date' => $date]);
$this->invalidateWithSegments($idSiteToInvalidate, $date, 'range', $_forceInvalidateNonexistant = true);
}
$this->setInvalidationTime();
$this->logger->debug("Done invalidating");
}
public function invalidateRecentDate($dateStr, $idSite)
{
$timezone = Site::getTimezoneFor($idSite);
$date = Date::factoryInTimezone($dateStr, $timezone);
$period = PeriodFactory::build('day', $date);
$params = new Parameters(new Site($idSite), $period, new Segment('', [$idSite], $period->getDateStart(), $period->getDateEnd()));
$loader = new Loader($params);
if ($loader->canSkipThisArchive()) {
$this->logger->debug(" " . ucfirst($dateStr) . " archive can be skipped due to no visits for idSite = $idSite, skipping invalidation...");
return;
}
$this->logger->info(" Will invalidate archived reports for $dateStr in site ID = {idSite}'s timezone ({date}).", [
'idSite' => $idSite,
'date' => $date->getDatetime(),
]);
// if we are invalidating yesterday here, we are only interested in checking if there is no archive for yesterday, or the day has changed since
// the last archive was archived (in which there may have been more visits before midnight). so we disable the ttl check, since any archive
// will be good enough, if the date hasn't changed.
$isYesterday = $dateStr == 'yesterday';
$this->invalidateWithSegments([$idSite], $date->toString(), 'day', false, $doNotIncludeTtlInExistingArchiveCheck = $isYesterday);
}
private function invalidateWithSegments($idSites, $date, $period, $_forceInvalidateNonexistant = false, $doNotIncludeTtlInExistingArchiveCheck = false)
{
if ($date instanceof Date) {
$date = $date->toString();
}
$periodObj = PeriodFactory::build($period, $date);
if ($period == 'range') {
$date = [$date]; // so we don't split on the ',' in invalidateArchivedReports
}
if (!is_array($idSites)) {
$idSites = [$idSites];
}
foreach ($idSites as $idSite) {
$params = new Parameters(new Site($idSite), $periodObj, new Segment('', [$idSite], $periodObj->getDateStart(), $periodObj->getDateEnd()));
if ($this->canWeSkipInvalidatingBecauseThereIsAUsablePeriod($params, $doNotIncludeTtlInExistingArchiveCheck)) {
$this->logger->debug(' Found usable archive for {archive}, skipping invalidation.', ['archive' => $params]);
} else {
$this->getApiToInvalidateArchivedReport()->invalidateArchivedReports($idSite, $date, $period, $segment = false, $cascadeDown = false,
$_forceInvalidateNonexistant);
}
foreach ($this->segmentArchiving->getAllSegmentsToArchive($idSite) as $segmentDefinition) {
$params = new Parameters(new Site($idSite), $periodObj, new Segment($segmentDefinition, [$idSite], $periodObj->getDateStart(), $periodObj->getDateEnd()));
if ($this->canWeSkipInvalidatingBecauseThereIsAUsablePeriod($params, $doNotIncludeTtlInExistingArchiveCheck)) {
$this->logger->debug(' Found usable archive for {archive}, skipping invalidation.', ['archive' => $params]);
} else {
if (empty($this->segmentArchiving)) {
// might not be initialised if init is not called
$this->segmentArchiving = StaticContainer::get(SegmentArchiving::class);
}
$segmentInfo = $this->segmentArchiving->findSegmentForHash($params->getSegment()->getHash(), $idSite);
if ($segmentInfo) {
$segmentArchiveStartDate = $this->segmentArchiving->getReArchiveSegmentStartDate($segmentInfo);
if ($segmentArchiveStartDate !== null && $segmentArchiveStartDate->isLater($params->getPeriod()->getDateEnd()->getEndOfDay())) {
// the system is not allowed to invalidate reports for this period
// automatically, only a user can specifically invalidate
continue;
}
}
$this->getApiToInvalidateArchivedReport()->invalidateArchivedReports($idSite, $date, $period, $segmentDefinition,
$cascadeDown = false, $_forceInvalidateNonexistant);
}
}
}
}
/**
* Returns true if there is an existing valid period we can use, or false if there isn't and the invalidation should go through.
*
* Note: this method should only be used in the context of invalidation.
*
* @params Parameters $params The parameters for the archive we want to invalidate.
*/
public function canWeSkipInvalidatingBecauseThereIsAUsablePeriod(Parameters $params, $doNotIncludeTtlInExistingArchiveCheck = false)
{
$today = Date::factoryInTimezone('today', Site::getTimezoneFor($params->getSite()->getId()));
$isYesterday = $params->getPeriod()->getLabel() == 'day' && $params->getPeriod()->getDateStart()->toString() == Date::factory('yesterday')->toString();
$isPeriodIncludesToday = $params->getPeriod()->isDateInPeriod($today);
$minArchiveProcessedTime = $doNotIncludeTtlInExistingArchiveCheck ? null :
Date::now()->subSeconds(Rules::getPeriodArchiveTimeToLiveDefault($params->getPeriod()->getLabel()));
// empty plugins param since we only check for an 'all' archive
list($idArchive, $visits, $visitsConverted, $ignore, $tsArchived) = ArchiveSelector::getArchiveIdAndVisits($params, $minArchiveProcessedTime, $includeInvalidated = $isPeriodIncludesToday);
// day has changed since the archive was created, we need to reprocess it
if ($isYesterday
&& !empty($idArchive)
&& Date::factory($tsArchived)->toString() != $today->toString()
) {
return false;
}
return !empty($idArchive);
}
// public for tests
public function repairInvalidationsIfNeeded($archiveToProcess)
{
$table = Common::prefixTable('archive_invalidations');
$bind = [
$archiveToProcess['idsite'],
$archiveToProcess['name'],
$archiveToProcess['period'],
$archiveToProcess['date1'],
$archiveToProcess['date2'],
];
$reportClause = '';
if (!empty($archiveToProcess['report'])) {
$reportClause = " AND report = ?";
$bind[] = $archiveToProcess['report'];
}
$sql = "SELECT DISTINCT period FROM `$table`
WHERE idsite = ? AND name = ? AND period > ? AND ? >= date1 AND date2 >= ? AND status = " . ArchiveInvalidator::INVALIDATION_STATUS_QUEUED . " $reportClause";
$higherPeriods = Db::fetchAll($sql, $bind);
$higherPeriods = array_column($higherPeriods, 'period');
$invalidationsToInsert = [];
foreach (Piwik::$idPeriods as $label => $id) {
// lower period than the one we're processing or range, don't care
if ($id <= $archiveToProcess['period'] || $label == 'range') {
continue;
}
if (in_array($id, $higherPeriods)) { // period exists in table
continue;
}
// archive is for week that is over two months, we don't need to care about the month
if ($label == 'month'
&& Date::factory($archiveToProcess['date1'])->toString('m') != Date::factory($archiveToProcess['date2'])->toString('m')
) {
continue;
}
$period = Period\Factory::build($label, $archiveToProcess['date1']);
$invalidationToInsert = [
'idarchive' => null,
'name' => $archiveToProcess['name'],
'report' => $archiveToProcess['report'],
'idsite' => $archiveToProcess['idsite'],
'date1' => $period->getDateStart()->getDatetime(),
'date2' => $period->getDateEnd()->getDatetime(),
'period' => $id,
'ts_invalidated' => $archiveToProcess['ts_invalidated'],
];
$this->logger->debug("Found dangling invalidation, inserting {invalidationToInsert}", [
'invalidationToInsert' => json_encode($invalidationToInsert),
]);
$invalidationsToInsert[] = $invalidationToInsert;
}
if (empty($invalidationsToInsert)) {
return;
}
$fields = ['idarchive', 'name', 'report', 'idsite', 'date1', 'date2', 'period', 'ts_invalidated'];
Db\BatchInsert::tableInsertBatch(Common::prefixTable('archive_invalidations'), $fields, $invalidationsToInsert);
}
private function setInvalidationTime()
{
$cache = Cache::getTransientCache();
Option::set(self::CRON_INVALIDATION_TIME_OPTION_NAME, time());
$cacheKey = 'CronArchive.getLastInvalidationTime';
$cache->delete($cacheKey);
}
public static function getLastInvalidationTime()
{
$cache = Cache::getTransientCache();
$cacheKey = 'CronArchive.getLastInvalidationTime';
$result = $cache->fetch($cacheKey);
if ($result !== false) {
return $result;
}
Option::clearCachedOption(self::CRON_INVALIDATION_TIME_OPTION_NAME);
$result = Option::get(self::CRON_INVALIDATION_TIME_OPTION_NAME);
if (empty($result)) {
Option::clearCachedOption(self::OPTION_ARCHIVING_FINISHED_TS);
$result = Option::get(self::OPTION_ARCHIVING_FINISHED_TS);
}
$cache->save($cacheKey, $result, self::TABLES_WITH_INVALIDATED_ARCHIVES_TTL);
return $result;
}
/**
* Returns the list of sites to loop over and archive.
* @return array
*/
private function initWebsiteIds($allWebsites)
{
if (count($this->shouldArchiveSpecifiedSites) > 0) {
$this->logger->info("- Will process " . count($this->shouldArchiveSpecifiedSites) . " websites (--force-idsites)");
return $this->shouldArchiveSpecifiedSites;
}
return $allWebsites;
}
private function logInitInfo()
{
$this->logSection("INIT");
$this->logger->info("Running Matomo " . Version::VERSION . " as Super User");
}
private function logArchiveTimeoutInfo()
{
$this->logSection("NOTES");
// Recommend to disable browser archiving when using this script
if (Rules::isBrowserTriggerEnabled()) {
$this->logger->info("- If you execute this script at least once per hour (or more often) in a crontab, you may disable 'Browser trigger archiving' in Matomo UI > Settings > General Settings.");
$this->logger->info(" See the doc at: https://matomo.org/docs/setup-auto-archiving/");
}
$cliMulti = new CliMulti($this->logger);
$supportsAsync = $cliMulti->supportsAsync();
$this->logger->info("- " . ($supportsAsync ? 'Async process archiving supported, using CliMulti.' : 'Async process archiving not supported, using curl requests.'));
$this->logger->info("- Reports for today will be processed at most every " . $this->todayArchiveTimeToLive
. " seconds. You can change this value in Matomo UI > Settings > General Settings.");
foreach (array('week', 'month', 'year', 'range') as $period) {
$ttl = Rules::getPeriodArchiveTimeToLiveDefault($period);
if (!empty($ttl) && $ttl !== $this->todayArchiveTimeToLive) {
$this->logger->info("- Reports for the current $period will be processed at most every " . $ttl
. " seconds. You can change this value in config/config.ini.php by editing 'time_before_" . $period . "_archive_considered_outdated' in the '[General]' section.");
}
}
// Try and not request older data we know is already archived
if ($this->lastSuccessRunTimestamp !== false) {
$dateLast = time() - $this->lastSuccessRunTimestamp;
$this->logger->info("- Archiving was last executed without error "
. $this->formatter->getPrettyTimeFromSeconds($dateLast, true) . " ago");
}
}
private function getVisitsFromApiResponse($stats)
{
if (empty($stats['nb_visits'])) {
return 0;
}
return (int) $stats['nb_visits'];
}
/**
* @return int
*/
private function getMaxConcurrentApiRequests()
{
if (false !== $this->concurrentRequestsPerWebsite) {
return $this->concurrentRequestsPerWebsite;
}
return self::MAX_CONCURRENT_API_REQUESTS;
}
/**
* @return false|string
*/
private function getLastSuccessRunTimestamp()
{
$timestamp = Option::get(self::OPTION_ARCHIVING_FINISHED_TS);
return $this->sanitiseTimestamp($timestamp);
}
private function sanitiseTimestamp($timestamp)
{
$now = time();
return ($timestamp < $now) ? $timestamp : $now;
}
/**
* @param $idSite
* @return array of date strings
*/
private function getCustomDateRangeToPreProcess($idSite)
{
static $cache = null;
if (is_null($cache)) {
$cache = $this->loadCustomDateRangeToPreProcess();
}
if (empty($cache[$idSite])) {
$cache[$idSite] = array();
}
$customRanges = array_filter(Config::getInstance()->General['archiving_custom_ranges']);
if (!empty($customRanges)) {
$cache[$idSite] = array_merge($cache[$idSite], $customRanges);
}
$dates = array_unique($cache[$idSite]);
return $dates;
}
/**
* @return array
*/
private function loadCustomDateRangeToPreProcess()
{
$customDateRangesToProcessForSites = array();
// For all users who have selected this website to load by default,
// we load the default period/date that will be loaded for this user
// and make sure it's pre-archived
$allUsersPreferences = APIUsersManager::getInstance()->getAllUsersPreferences(array(
APIUsersManager::PREFERENCE_DEFAULT_REPORT_DATE,
APIUsersManager::PREFERENCE_DEFAULT_REPORT
));
foreach ($allUsersPreferences as $userLogin => $userPreferences) {
if (!isset($userPreferences[APIUsersManager::PREFERENCE_DEFAULT_REPORT_DATE])) {
continue;
}
$defaultDate = $userPreferences[APIUsersManager::PREFERENCE_DEFAULT_REPORT_DATE];
$preference = new UserPreferences();
$period = $preference->getDefaultPeriod($defaultDate);
if ($period != 'range') {
continue;
}
if (isset($userPreferences[APIUsersManager::PREFERENCE_DEFAULT_REPORT])
&& is_numeric($userPreferences[APIUsersManager::PREFERENCE_DEFAULT_REPORT])) {
// If user selected one particular website ID
$idSites = array($userPreferences[APIUsersManager::PREFERENCE_DEFAULT_REPORT]);
} else {
// If user selected "All websites" or some other random value, we pre-process all websites that they have access to
$idSites = APISitesManager::getInstance()->getSitesIdWithAtLeastViewAccess($userLogin);
}
foreach ($idSites as $idSite) {
$customDateRangesToProcessForSites[$idSite][] = $defaultDate;
}
}
return $customDateRangesToProcessForSites;
}
/**
* @param $url
* @return string
*/
private function makeRequestUrl($url)
{
$url = $url . self::APPEND_TO_API_REQUEST;
if ($this->shouldStartProfiler) {
$url .= "&xhprof=2";
}
if ($this->testmode) {
$url .= "&testmode=1";
}
/**
* @ignore
*/
Piwik::postEvent('CronArchive.alterArchivingRequestUrl', [&$url]);
return $url;
}
/**
* @return CliMulti
*/
private function makeCliMulti()
{
/** @var CliMulti $cliMulti */
$cliMulti = new CliMulti($this->logger);
$cliMulti->setUrlToPiwik($this->urlToPiwik);
$cliMulti->setPhpCliConfigurationOptions($this->phpCliConfigurationOptions);
$cliMulti->setAcceptInvalidSSLCertificate($this->acceptInvalidSSLCertificate);
$cliMulti->setConcurrentProcessesLimit($this->getMaxConcurrentApiRequests());
$cliMulti->runAsSuperUser();
$cliMulti->onProcessFinish(function ($pid) {
$this->printPerformanceStatsForProcess($pid);
});
return $cliMulti;
}
public function setUrlToPiwik($url)
{
$this->urlToPiwik = $url;
}
private function printPerformanceStatsForProcess($childPid)
{
if (!$this->isArchiveProfilingEnabled) {
return;
}
$data = Logger::getMeasurementsFor(getmypid(), $childPid);
if (empty($data)) {
return;
}
$message = "";
foreach ($data as $request => $measurements) {
$message .= "PERFORMANCE FOR " . $request . "\n ";
$message .= implode("\n ", $measurements) . "\n";
}
$this->logger->info($message);
}
private function hasReachedMaxConcurrentArchivers()
{
$cliMulti = $this->makeCliMulti();
if ($this->maxConcurrentArchivers && $cliMulti->supportsAsync()) {
$numRunning = 0;
$processes = Process::getListOfRunningProcesses();
$instanceId = SettingsPiwik::getPiwikInstanceId();
foreach ($processes as $process) {
if (strpos($process, ' core:archive') !== false &&
strpos($process, 'console ') !== false &&
(!$instanceId
|| strpos($process, '--matomo-domain=' . $instanceId) !== false
|| strpos($process, '--matomo-domain="' . $instanceId . '"') !== false
|| strpos($process, '--matomo-domain=\'' . $instanceId . "'") !== false
|| strpos($process, '--piwik-domain=' . $instanceId) !== false
|| strpos($process, '--piwik-domain="' . $instanceId . '"') !== false
|| strpos($process, '--piwik-domain=\'' . $instanceId . "'") !== false)) {
$numRunning++;
}
}
if ($this->maxConcurrentArchivers < $numRunning) {
$this->logger->info(sprintf("Archiving will stop now because %s archivers are already running and max %s are supposed to run at once.", $numRunning, $this->maxConcurrentArchivers));
return true;
} else {
$this->logger->info(sprintf("%s out of %s archivers running currently", $numRunning, $this->maxConcurrentArchivers));
}
}
return false;
}
protected function wasSegmentChangedRecently($definition, $allSegments)
{
foreach ($allSegments as $segment) {
if ($segment['definition'] === $definition) {
$twentyFourHoursAgo = Date::now()->subHour(24);
$segmentDate = $segment['ts_created'];
if (!empty($segment['ts_last_edit'])) {
$segmentDate = $segment['ts_last_edit'];
}
return Date::factory($segmentDate)->isLater($twentyFourHoursAgo);
}
}
return false;
}
/**
* @param ArchiveFilter $archiveFilter
*/
public function setArchiveFilter(ArchiveFilter $archiveFilter): void
{
$this->archiveFilter = $archiveFilter;
}
private function makeWebsiteIdArchiveList(array $websitesIds)
{
if ($this->shouldArchiveAllSites) {
$this->logger->info("- Will process all " . count($websitesIds) . " websites");
return new FixedSiteIds($websitesIds);
}
if (!empty($this->shouldArchiveSpecifiedSites)) {
$this->logger->info("- Will process specified sites: " . implode(', ', $websitesIds));
return new FixedSiteIds($websitesIds);
}
return new SharedSiteIds($websitesIds, SharedSiteIds::OPTION_ALL_WEBSITES);
}
private function siteExists($idSite)
{
try {
new Site($idSite);
return true;
} catch (\UnexpectedValueException $ex) {
return false;
}
}
}
|