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

Single.php « Archive « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2f4d5e283e842147bf856c137abf65a272d40b5d (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
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
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 *
 * @category Piwik
 * @package Piwik
 */

/**
 * Piwik_Archive_Single is used to store the data of a single archive,
 * for example the statistics for the 'day' '2008-02-21' for the website idSite '2'
 *
 * @package Piwik
 * @subpackage Piwik_Archive
 */
class Piwik_Archive_Single extends Piwik_Archive
{
    /**
     * The Piwik_ArchiveProcessing object used to check that the archive is available
     * and launch the processing if the archive was not yet processed
     *
     * @var Piwik_ArchiveProcessing
     */
    public $archiveProcessing = null;

    /**
     * @var bool Set to true if the archive has at least 1 visit
     */
    public $isThereSomeVisits = null;

    /**
     * Period of this Archive
     *
     * @var Piwik_Period
     */
    protected $period = null;

    /**
     * Set to true will activate numeric value caching for this archive.
     *
     * @var bool
     */
    protected $cacheEnabledForNumeric = true;

    /**
     * Array of cached numeric values, used to make requests faster
     * when requesting the same value again and again
     *
     * @var array of numeric
     */
    protected $numericCached = array();

    /**
     * Array of cached blob, used to make requests faster when requesting the same blob again and again
     *
     * @var array of mixed
     */
    protected $blobCached = array();

    /**
     * idarchive of this Archive in the database
     *
     * @var int
     */
    protected $idArchive = null;

    /**
     * name of requested report
     *
     * @var string
     */
    protected $requestedReport = null;

    /**
     * Flag set to true once the archive has been checked (when we make sure it is archived)
     *
     * @var bool
     */
    protected $alreadyChecked = array();

    protected function clearCache()
    {
        foreach ($this->blobCached as $name => $blob) {
            $this->freeBlob($name);
        }
        $this->blobCached = array();
    }

    public function __destruct()
    {
        $this->clearCache();
    }

    /**
     * Returns the blob cache. For testing.
     *
     * @return array
     */
    public function getBlobCache()
    {
        return $this->blobCached;
    }

    /**
     * Returns the pretty date of this Archive, eg. 'Thursday 20th March 2008'
     *
     * @return string
     */
    public function getPrettyDate()
    {
        return $this->period->getPrettyString();
    }

    /**
     * Returns the idarchive of this Archive used to index this archive in the DB
     *
     * @throws Exception
     * @return int
     */
    public function getIdArchive()
    {
        if (is_null($this->idArchive)) {
            throw new Exception("idArchive is null");
        }
        return $this->idArchive;
    }

    /**
     * Set the period
     *
     * @param Piwik_Period $period
     */
    public function setPeriod(Piwik_Period $period)
    {
        $this->period = $period;
    }

    public function getPeriod()
    {
        return $this->period;
    }

    /**
     * Returns the timestamp of the first date in the period for this Archive.
     * This is used to sort archives by date when working on a Archive_Array
     *
     * @return int Unix timestamp
     */
    public function getTimestampStartDate()
    {
        if (!is_null($this->archiveProcessing)) {
            $timestamp = $this->archiveProcessing->getTimestampStartDate();
            if (!empty($timestamp)) {
                return $timestamp;
            }
        }
        return $this->period->getDateStart()->getTimestamp();
    }

    /**
     * Prepares the archive. Gets the idarchive from the ArchiveProcessing.
     *
     * This will possibly launch the archiving process if the archive was not available.
     * @return bool
     */
    public function prepareArchive()
    {
        $archiveJustProcessed = false;


        $periodString = $this->period->getLabel();
        $plugin = Piwik_ArchiveProcessing::getPluginBeingProcessed($this->getRequestedReport());

        $cacheKey = 'all';
        if ($periodString == 'range') {
            $cacheKey = $plugin;
        }
        if (!isset($this->alreadyChecked[$cacheKey])) {
            $this->isThereSomeVisits = false;
            $this->alreadyChecked[$cacheKey] = true;
            $dayString = $this->period->getPrettyString();
            $logMessage = sprintf("%s (%s), plugin %s", $periodString, $dayString, $plugin);
            // if the END of the period is BEFORE the website creation date
            // we already know there are no stats for this period
            // we add one day to make sure we don't miss the day of the website creation
            if ($this->period->getDateEnd()->addDay(2)->isEarlier($this->site->getCreationDate())) {
                Piwik::log(sprintf("Archive %s skipped, archive is before the website was created.", $logMessage));
                return;
            }

            // if the starting date is in the future we know there is no visit
            if ($this->period->getDateStart()->subDay(2)->isLater(Piwik_Date::today())) {
                Piwik::log(sprintf("Archive %s skipped, archive is after today.", $logMessage));
                return;
            }

            // we make sure the archive is available for the given date
            $periodLabel = $this->period->getLabel();
            $this->archiveProcessing = Piwik_ArchiveProcessing::factory($periodLabel);
            $this->archiveProcessing->setSite($this->site);
            $this->archiveProcessing->setPeriod($this->period);
            $this->archiveProcessing->setSegment($this->segment);

            $this->archiveProcessing->init();

            $this->archiveProcessing->setRequestedReport($this->getRequestedReport());

            $archivingDisabledArchiveNotProcessed = false;
            $idArchive = $this->archiveProcessing->loadArchive();
            if (empty($idArchive)) {
                if ($this->archiveProcessing->isArchivingDisabled()) {
                    $archivingDisabledArchiveNotProcessed = true;
                    $logMessage = sprintf("Archiving disabled, for %s", $logMessage);
                } else {
                    Piwik::log(sprintf("Processing %s, not archived yet...", $logMessage));
                    $archiveJustProcessed = true;

                    // Process the reports
                    $this->archiveProcessing->launchArchiving();

                    $idArchive = $this->archiveProcessing->getIdArchive();
                    $logMessage = sprintf("Processed %d, for %s", $idArchive, $logMessage);
                }
            } else {
                $logMessage = sprintf("Already processed, fetching idArchive = %d (idSite=%d), for %s", $idArchive, $this->site->getId(), $logMessage);
            }
            Piwik::log(sprintf("%s, Visits = %d", $logMessage, $this->archiveProcessing->getNumberOfVisits()));
            $this->isThereSomeVisits = !$archivingDisabledArchiveNotProcessed
                && $this->archiveProcessing->isThereSomeVisits();
            $this->idArchive = $idArchive;
        }
        return $archiveJustProcessed;
    }

    /**
     * Returns a value from the current archive with the name = $name
     * Method used by getNumeric or getBlob
     *
     * @param string $name
     * @param string $typeValue     numeric|blob
     * @param string|bool $archivedDate  Value to store date of archive info in. If false, not stored.
     * @return mixed|bool  false if no result
     */
    protected function get($name, $typeValue = 'numeric', &$archivedDate = false)
    {
        $this->setRequestedReport($name);
        $this->prepareArchive();

        // values previously "get" and now cached
        if ($typeValue == 'numeric'
            && $this->cacheEnabledForNumeric
            && isset($this->numericCached[$name])
        ) {
            return $this->numericCached[$name];
        }

        // During archiving we prefetch the blobs recursively
        // and we get them faster from memory after
        if ($typeValue == 'blob'
            && isset($this->blobCached[$name])
        ) {
            return $this->blobCached[$name];
        }

        if ($name == 'idarchive') {
            return $this->idArchive;
        }

        if (!$this->isThereSomeVisits) {
            return false;
        }

        // select the table to use depending on the type of the data requested
        switch ($typeValue) {
            case 'blob':
                $table = $this->archiveProcessing->getTableArchiveBlobName();
                break;

            case 'numeric':
            default:
                $table = $this->archiveProcessing->getTableArchiveNumericName();
                break;
        }

        $db = Zend_Registry::get('db');
        $row = $db->fetchRow("SELECT value, ts_archived
								FROM $table
								WHERE idarchive = ? AND name = ?",
            array($this->idArchive, $name)
        );

        $value = $tsArchived = false;
        if (is_array($row)) {
            $value = $row['value'];
            $tsArchived = $row['ts_archived'];
        }

        if ($archivedDate !== false) {
            $archivedDate = $tsArchived;
        }

        if ($value === false) {
            if ($typeValue == 'numeric'
                && $this->cacheEnabledForNumeric
            ) {
                $this->numericCached[$name] = false;
            }
            return $value;
        }

        // uncompress when selecting from the BLOB table
        if ($typeValue == 'blob' && $db->hasBlobDataType()) {
            $value = $this->uncompress($value);
        }

        if ($typeValue == 'numeric'
            && $this->cacheEnabledForNumeric
        ) {
            $this->numericCached[$name] = $value;
        }
        return $value;
    }


    /**
     * This method loads in memory all the subtables for the main table called $name.
     * You have to give it the parent table $dataTableToLoad so we can lookup the sub tables ids to load.
     *
     * If $addMetadataSubtableId set to true, it will add for each row a 'metadata' called 'databaseSubtableId'
     *  containing the child ID of the subtable  associated to this row.
     *
     * @param string $name
     * @param Piwik_DataTable $dataTableToLoad
     * @param bool $addMetadataSubtableId
     */
    public function loadSubDataTables($name, Piwik_DataTable $dataTableToLoad, $addMetadataSubtableId = false)
    {
        // we have to recursively load all the subtables associated to this table's rows
        // and update the subtableID so that it matches the newly instanciated table
        foreach ($dataTableToLoad->getRows() as $row) {
            $subTableID = $row->getIdSubDataTable();

            if ($subTableID !== null) {
                $subDataTableLoaded = $this->getDataTable($name, $subTableID);

                $row->setSubtable($subDataTableLoaded);

                $this->loadSubDataTables($name, $subDataTableLoaded, $addMetadataSubtableId);

                // we edit the subtable ID so that it matches the newly table created in memory
                // NB: we dont overwrite the datatableid in the case we are displaying the table expanded.
                if ($addMetadataSubtableId) {
                    // this will be written back to the column 'idsubdatatable' just before rendering, see Renderer/Php.php
                    $row->addMetadata('idsubdatatable_in_db', $row->getIdSubDataTable());
                }
            }
        }
    }


    /**
     * Free the blob cache memory array
     * @param $name
     */
    public function freeBlob($name)
    {
        unset($this->blobCached[$name]);
        $this->blobCached[$name] = null;
    }

    protected function uncompress($data)
    {
        return @gzuncompress($data);
    }

    /**
     * Fetches all blob fields name_* at once for the current archive for performance reasons.
     *
     * @param $name
     * @return
     */
    public function preFetchBlob($name)
    {
        $this->setRequestedReport($name);
        $this->prepareArchive();
        if (!$this->isThereSomeVisits) {
            return;
        }

        $tableBlob = $this->archiveProcessing->getTableArchiveBlobName();

        $db = Zend_Registry::get('db');
        $hasBlobs = $db->hasBlobDataType();

        // select blobs w/ name like "$name_[0-9]+" w/o using RLIKE
        $nameEnd = strlen($name) + 2;
        $query = $db->query("SELECT value, name
								FROM $tableBlob
								WHERE idarchive = ?
									AND (name = ? OR
											(name LIKE ? AND SUBSTRING(name, $nameEnd, 1) >= '0'
														 AND SUBSTRING(name, $nameEnd, 1) <= '9') )",
            array($this->idArchive, $name, $name . '%')
        );

        while ($row = $query->fetch()) {
            $value = $row['value'];
            $name = $row['name'];

            if ($hasBlobs) {
                $this->blobCached[$name] = $this->uncompress($value);
                if ($this->blobCached[$name] === false) {
                    //throw new Exception("Error gzuncompress $name ");
                }
            } else {
                $this->blobCached[$name] = $value;
            }
        }
    }

    /**
     * Returns a numeric value from this Archive, with the name '$name'
     *
     * @param string $name
     * @return int|float
     */
    public function getNumeric($name)
    {
        return $this->formatNumericValue($this->get($name, 'numeric'));
    }


    /**
     * Returns a blob value from this Archive, with the name '$name'
     * Blob values are all values except int and float.
     *
     * @param string $name
     * @return mixed
     */
    public function getBlob($name)
    {
        return $this->get($name, 'blob');
    }

    /**
     * Given a list of fields defining numeric values, it will return a Piwik_DataTable_Simple
     * containing one row per field name.
     *
     * For example $fields = array(    'max_actions',
     *                        'nb_uniq_visitors',
     *                        'nb_visits',
     *                        'nb_actions',
     *                        'sum_visit_length',
     *                        'bounce_count',
     *                        'nb_visits_converted'
     *                    );
     *
     * @param string|array $fields Name or array of names of Archive fields
     *
     * @return Piwik_DataTable_Simple
     */
    public function getDataTableFromNumeric($fields)
    {
        if (!is_array($fields)) {
            $fields = array($fields);
        }

        $values = array();
        foreach ($fields as $field) {
            $values[$field] = $this->getNumeric($field);
        }

        $table = new Piwik_DataTable_Simple();
        $table->addRowsFromArray($values);
        return $table;
    }

    /**
     * Returns a DataTable that has the name '$name' from the current Archive.
     * If $idSubTable is specified, returns the subDataTable called '$name_$idSubTable'
     *
     * @param string $name
     * @param int $idSubTable  optional id SubDataTable
     * @return Piwik_DataTable
     */
    public function getDataTable($name, $idSubTable = null)
    {
        if (!is_null($idSubTable)) {
            $name .= sprintf("_%s", $idSubTable);
        }

        $this->setRequestedReport($name);

        $data = $this->get($name, 'blob', $tsArchived);

        $table = $this->makeDataTable();

        if ($data !== false) {
            $table->addRowsFromSerializedArray($data);
            $table->setMetadata(Piwik_DataTable::ARCHIVED_DATE_METADATA_NAME, $tsArchived);
        }
        if ($data === false
            && $idSubTable !== null
        ) {
            // This is not expected, but somehow happens in some unknown cases and very rarely.
            // Do not throw error in this case
            //throw new Exception("not expected");
            return new Piwik_DataTable();
        }

        return $table;
    }

    /**
     * Creates a new DataTable with some metadata set. Sets the following metadata:
     * - 'site' => Piwik_Site instance for this archive
     * - 'period' => Piwik_Period instance for this archive
     * - 'timestamp' => the timestamp of the first date in this archive
     *
     * @param bool $isSimple Whether the DataTable should be a DataTable_Simple
     *                       instance or not.
     * @return Piwik_DataTable
     */
    public function makeDataTable($isSimple = false)
    {
        if ($isSimple) {
            $result = new Piwik_DataTable_Simple();
        } else {
            $result = new Piwik_DataTable();
        }

        $result->setMetadata('site', $this->getSite());
        $result->setMetadata('period', $this->getPeriod());
        $result->setMetadata('timestamp', $this->getTimestampStartDate());

        return $result;
    }

    public function setRequestedReport($requestedReport)
    {
        $this->requestedReport = $requestedReport;
    }

    /**
     * Returns the report (the named collection of metrics) this Archive instance is
     * currently going to query/process.
     *
     * @return string
     */
    protected function getRequestedReport()
    {
        return self::getRequestedReportFor($this->requestedReport);
    }

    /**
     * Returns the name of the report (the named collection of metrics) that contains the
     * specified metric.
     *
     * @param string $metric The metric whose report is being requested. If this does
     *                       not belong to a known report, its assumed to be the report
     *                       itself.
     * @return string
     */
    public static function getRequestedReportFor($metric)
    {
        // Core metrics are always processed in Core, for the requested date/period/segment
        if (in_array($metric, Piwik_ArchiveProcessing::getCoreMetrics())
            || $metric == 'max_actions'
        ) {
            return 'VisitsSummary_CoreMetrics';
        }
        // VisitFrequency metrics don't follow the same naming convention (HACK)
        if (strpos($metric, '_returning') > 0
            // ignore Goal_visitor_returning_1_1_nb_conversions
            && strpos($metric, 'Goal_') === false
        ) {
            return 'VisitFrequency_Metrics';
        }
        // Goal_* metrics are processed by the Goals plugin (HACK)
        if (strpos($metric, 'Goal_') === 0) {
            return 'Goals_Metrics';
        }
        // Actions metrics are processed by the Actions plugin (HACK) (3RD HACK IN FACT) (YES, THIS IS TOO MUCH HACKING)
        // (FIXME PLEASE).
        if (in_array($metric, Piwik_Archive::$actionsMetrics)) {
            return 'Actions_Metrics';
        }
        return $metric;
    }

    /**
     * Returns a DataTable that has the name '$name' from the current Archive.
     * Also loads in memory all subDataTable for this DataTable.
     *
     * For example, if $name = 'Referers_keywordBySearchEngine' it will load all DataTable
     *  named 'Referers_keywordBySearchEngine_*' and they will be set as subDataTable to the
     *  rows. You can then go through the rows
     *        $rows = DataTable->getRows();
     *  and for each row request the subDataTable (in this case the DataTable of the keywords for each search engines)
     *        $idSubTable = $row->getIdSubDataTable();
     *        $subTable = Piwik_DataTable_Manager::getInstance()->getTable($idSubTable);
     *
     * @param string $name
     * @param int $idSubTable  Optional subDataTable to load instead of loading the parent DataTable
     * @return Piwik_DataTable
     */
    public function getDataTableExpanded($name, $idSubTable = null)
    {
        $this->preFetchBlob($name);
        $dataTableToLoad = $this->getDataTable($name, $idSubTable);
        $this->loadSubDataTables($name, $dataTableToLoad, $addMetadataSubtableId = true);
        $dataTableToLoad->enableRecursiveFilters();
        $this->freeBlob($name);
        return $dataTableToLoad;
    }

    /**
     * Returns true if Piwik can launch the archiving process for this archive,
     * false if otherwise.
     *
     * @return bool
     */
    public function isArchivingDisabled()
    {
        return Piwik_ArchiveProcessing::isArchivingDisabledFor($this->segment, $this->period);
    }
}