Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Tasks.php « CoreAdminHome « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 803880b621af0ce9595af4c3406eb0464331f42f (plain)
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
<?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\Plugins\CoreAdminHome;

use Piwik\API\Request;
use Piwik\Archive;
use Piwik\Archive\ArchiveInvalidator;
use Piwik\ArchiveProcessor\Rules;
use Piwik\Archive\ArchivePurger;
use Piwik\Common;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\CronArchive;
use Piwik\DataAccess\ArchiveTableCreator;
use Piwik\DataAccess\Model as CoreModel;
use Piwik\Date;
use Piwik\Db;
use Piwik\Http;
use Piwik\Option;
use Piwik\Piwik;
use Piwik\Plugins\CoreAdminHome\Emails\JsTrackingCodeMissingEmail;
use Piwik\Plugins\CoreAdminHome\Emails\TrackingFailuresEmail;
use Piwik\Plugins\CoreAdminHome\Tasks\ArchivesToPurgeDistributedList;
use Piwik\Plugins\SegmentEditor\Model;
use Piwik\Plugins\SitesManager\SitesManager;
use Piwik\Scheduler\Schedule\SpecificTime;
use Piwik\Settings\Storage\Backend\MeasurableSettingsTable;
use Piwik\Tracker\Failures;
use Piwik\Site;
use Piwik\Tracker\FingerprintSalt;
use Piwik\Tracker\Visit\ReferrerSpamFilter;
use Psr\Log\LoggerInterface;
use Piwik\SettingsPiwik;

class Tasks extends \Piwik\Plugin\Tasks
{
    const TRACKING_CODE_CHECK_FLAG = 'trackingCodeExistsCheck';
    /**
     * @var ArchivePurger
     */
    private $archivePurger;

    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * @var Failures
     */
    private $trackingFailures;

    public function __construct(ArchivePurger $archivePurger, LoggerInterface $logger, Failures $failures)
    {
        $this->archivePurger = $archivePurger;
        $this->logger = $logger;
        $this->trackingFailures = $failures;
    }

    public function schedule()
    {
        // for browser triggered archiving, make sure we invalidate archives once a day just to make
        // sure all archives that need to be invalidated get invalidated
        $this->daily('invalidateOutdatedArchives', null, self::HIGH_PRIORITY);

        $this->daily('deleteOldFingerprintSalts', null, self::HIGH_PRIORITY);

        // general data purge on older archive tables, executed daily
        $this->daily('purgeOutdatedArchives', null, self::HIGH_PRIORITY);

        // general data purge on invalidated archive records, executed daily
        $this->daily('purgeInvalidatedArchives', null, self::LOW_PRIORITY);
        $this->daily('purgeInvalidationsForDeletedSites', null, self::LOW_PRIORITY);

        $this->weekly('purgeOrphanedArchives', null, self::NORMAL_PRIORITY);

        // lowest priority since tables should be optimized after they are modified
        $this->monthly('optimizeArchiveTable', null, self::LOWEST_PRIORITY);

        $this->daily('cleanupTrackingFailures', null, self::LOWEST_PRIORITY);
        $this->weekly('notifyTrackingFailures', null, self::LOWEST_PRIORITY);

        $generalConfig = Config::getInstance()->Tracker;
        if((SettingsPiwik::isInternetEnabled() === true) && $generalConfig['enable_spam_filter']){
            $this->weekly('updateSpammerList');
        }

        $this->scheduleTrackingCodeReminderChecks();
    }

    public function purgeInvalidationsForDeletedSites()
    {
        $coreModel = new CoreModel();
        $coreModel->deleteInvalidationsForDeletedSites();
    }

    public function deleteOldFingerprintSalts()
    {
        StaticContainer::get(FingerprintSalt::class)->deleteOldSalts();
    }

    public function invalidateOutdatedArchives()
    {
        if (!Rules::isBrowserTriggerEnabled()) {
            $this->logger->info("Browser triggered archiving disabled, archives will be invalidated during core:archive.");
            return;
        }

        $idSites = Request::processRequest('SitesManager.getAllSitesId');
        $cronArchive = new CronArchive();
        foreach ($idSites as $idSite) {
            $cronArchive->invalidateArchivedReportsForSitesThatNeedToBeArchivedAgain($idSite);
        }
    }

    private function scheduleTrackingCodeReminderChecks()
    {
        $daysToTrackedVisitsCheck = (int) Config::getInstance()->General['num_days_before_tracking_code_reminder'];
        if ($daysToTrackedVisitsCheck <= 0) {
            return;
        }

        // add check for a site's tracked visits
        $sites = Request::processRequest('SitesManager.getAllSites');

        foreach ($sites as $site) {
            $createdTime = Date::factory($site['ts_created']);
            $scheduledTime = $createdTime->addDay($daysToTrackedVisitsCheck)->setTime('02:00:00');

            // we don't want to run this check for every site in an install when this code is introduced,
            // so if the site is over 2 * $daysToTrackedVisitsCheck days old, assume the check has run.
            $isSiteOld = $createdTime->isEarlier(Date::today()->subDay($daysToTrackedVisitsCheck * 2));

            if ($isSiteOld || $this->hasTrackingCodeReminderRun($site['idsite'])) {
                continue;
            }

            $schedule = new SpecificTime($scheduledTime->getTimestamp());
            $this->custom($this, 'checkSiteHasTrackedVisits', $site['idsite'], $schedule);
        }
    }

    public function checkSiteHasTrackedVisits($idSite)
    {
        $this->rememberTrackingCodeReminderRan($idSite);

        if (!SitesManager::shouldPerormEmptySiteCheck($idSite)) {
            return;
        }

        if (SitesManager::hasTrackedAnyTraffic($idSite)) {
            return;
        }

        // site is still empty after N days, so send an email to the user that created the site
        $creatingUser = Site::getCreatorLoginFor($idSite);
        if (empty($creatingUser)) {
            return;
        }

        $user = Request::processRequest('UsersManager.getUser', [
            'userLogin' => $creatingUser,
        ]);
        if (empty($user['email'])) {
            return;
        }

        $container = StaticContainer::getContainer();
        $email = $container->make(JsTrackingCodeMissingEmail::class, array(
            'login' => $user['login'],
            'emailAddress' => $user['email'],
            'idSite' => $idSite
        ));
        $email->send();
    }

    private function hasTrackingCodeReminderRun($idSite)
    {
        $table = new MeasurableSettingsTable($idSite, 'CoreAdminHome');
        $settings = $table->load();
        return !empty($settings[self::TRACKING_CODE_CHECK_FLAG]);
    }

    private function rememberTrackingCodeReminderRan($idSite)
    {
        $table = new MeasurableSettingsTable($idSite, 'CoreAdminHome');
        $settings = $table->load();
        $settings[self::TRACKING_CODE_CHECK_FLAG] = 1;
        $table->save($settings);
    }

    /**
     * To test execute the following command:
     * `./console core:run-scheduled-tasks "Piwik\Plugins\CoreAdminHome\Tasks.cleanupTrackingFailures"`
     *
     * @throws \Exception
     */
    public function cleanupTrackingFailures()
    {
        // we remove possibly outdated/fixed tracking failures that have not occurred again recently
        $this->trackingFailures->removeFailuresOlderThanDays(Failures::CLEANUP_OLD_FAILURES_DAYS);
    }

    /**
     * To test execute the following command:
     * `./console core:run-scheduled-tasks "Piwik\Plugins\CoreAdminHome\Tasks.notifyTrackingFailures"`
     *
     * @throws \Exception
     */
    public function notifyTrackingFailures()
    {
        $this->cleanupTrackingFailures();
        $failures = $this->trackingFailures->getAllFailures();
        $general = Config::getInstance()->General;
        if (!empty($failures) && $general['enable_tracking_failures_notification']) {
            $superUsers = Piwik::getAllSuperUserAccessEmailAddresses();
            foreach ($superUsers as $login => $email) {
                $email = new TrackingFailuresEmail($login, $email, count($failures));
                $email->send();
            }
        }
    }

    /**
     * @return bool `true` if the purge was executed, `false` if it was skipped.
     * @throws \Exception
     */
    public function purgeOutdatedArchives()
    {
        if ($this->willPurgingCausePotentialProblemInUI() && !Rules::$disablePureOutdatedArchive) {
            $this->logger->info("Purging temporary archives: skipped (browser triggered archiving not enabled & not running after core:archive)");
            return false;
        }

        $archiveTables = ArchiveTableCreator::getTablesArchivesInstalled();

        $this->logger->info("Purging archives in {tableCount} archive tables.", array('tableCount' => count($archiveTables)));

        // keep track of dates we purge for, since getTablesArchivesInstalled() will return numeric & blob
        // tables (so dates will appear two times, and we should only purge once per date)
        $datesPurged = array();

        foreach ($archiveTables as $table) {
            $date = ArchiveTableCreator::getDateFromTableName($table);
            list($year, $month) = explode('_', $date);

            // Somehow we may have archive tables created with older dates, prevent exception from being thrown
            if ($year > 1990) {
                if (empty($datesPurged[$date])) {
                    $dateObj = Date::factory("$year-$month-15");

                    $this->archivePurger->purgeOutdatedArchives($dateObj);
                    $this->archivePurger->purgeArchivesWithPeriodRange($dateObj);

                    $datesPurged[$date] = true;
                } else {
                    $this->logger->debug("Date {date} already purged.", array('date' => $date));
                }
            } else {
                $this->logger->info("Skipping purging of archive tables *_{year}_{month}, year <= 1990.", array('year' => $year, 'month' => $month));
            }
        }

        return true;
    }

    public function purgeInvalidatedArchives()
    {
        $purgedDates = [];

        $archivesToPurge = new ArchivesToPurgeDistributedList();
        foreach ($archivesToPurge->getAllAsDates() as $date) {
            $this->archivePurger->purgeInvalidatedArchivesFrom($date);

            $archivesToPurge->removeDate($date);

            $purgedDates[$date->toString('Y-m')] = true;
        }

        // purge from today if not done already since we will have many archives to remove
        $today = Date::today();
        $todayStr = $today->toString('Y-m');
        if (empty($purgedDates[$todayStr])) {
            $this->archivePurger->purgeInvalidatedArchivesFrom($today);
            $purgedDates[$todayStr] = true;
        }

        // handle yesterday if it belongs to a different month
        $yesterday = Date::yesterday();
        $yesterdayStr = $yesterday->toString('Y-m');
        if (empty($purgedDates[$yesterdayStr])) {
            $this->archivePurger->purgeInvalidatedArchivesFrom($yesterday);
        }

        // handle year start table
        $yearStart = $today->toString('Y-01');
        if (empty($purgedDates[$yearStart])) {
            $this->archivePurger->purgeInvalidatedArchivesFrom(Date::factory($yearStart . '-01'));
        }
    }

    public function optimizeArchiveTable()
    {
        $archiveTables = ArchiveTableCreator::getTablesArchivesInstalled();
        Db::optimizeTables($archiveTables);
    }

    /**
     * Update the referrer spam blacklist
     *
     * @see https://github.com/matomo-org/referrer-spam-list
     */
    public function updateSpammerList()
    {
        $url = 'https://raw.githubusercontent.com/matomo-org/referrer-spam-list/master/spammers.txt';
        $list = Http::sendHttpRequest($url, 30);
        $list = preg_split("/\r\n|\n|\r/", $list);
        if (count($list) < 10) {
            throw new \Exception(sprintf(
                'The spammers list downloaded from %s contains less than 10 entries, considering it a fail',
                $url
            ));
        }

        Option::set(ReferrerSpamFilter::OPTION_STORAGE_NAME, serialize($list));
    }

    /**
     * To test execute the following command:
     * `./console core:run-scheduled-tasks "Piwik\Plugins\CoreAdminHome\Tasks.purgeOrphanedArchives"`
     *
     * @throws \Exception
     */
    public function purgeOrphanedArchives()
    {
        $eightDaysAgo = Date::factory('now')->subDay(8);
        $model = new Model();
        $deletedSegments = $model->getSegmentsDeletedSince($eightDaysAgo);

        $archiveTables = ArchiveTableCreator::getTablesArchivesInstalled('numeric');

        $datesPurged = array();
        foreach ($archiveTables as $table) {
            $date = ArchiveTableCreator::getDateFromTableName($table);
            list($year, $month) = explode('_', $date);

            $dateObj = Date::factory("$year-$month-15");

            $this->archivePurger->purgeDeletedSiteArchives($dateObj);
            if (count($deletedSegments)) {
                $this->archivePurger->purgeDeletedSegmentArchives($dateObj, $deletedSegments);
            }

            $datesPurged[$date] = true;
        }
    }

    /**
     * we should only purge outdated & custom range archives if we know cron archiving has just run,
     * or if browser triggered archiving is enabled. if cron archiving has run, then we know the latest
     * archives are in the database, and we can remove temporary ones. if browser triggered archiving is
     * enabled, then we know any archives that are wrongly purged, can be re-archived on demand.
     * this prevents some situations where "no data" is displayed for reports that should have data.
     *
     * @return bool
     */
    private function willPurgingCausePotentialProblemInUI()
    {
        return !Rules::isRequestAuthorizedToArchive();
    }
}