-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathAPI.php
More file actions
119 lines (95 loc) · 3.81 KB
/
API.php
File metadata and controls
119 lines (95 loc) · 3.81 KB
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
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
declare(strict_types=1);
namespace Piwik\Plugins\BotTracking;
use Piwik\Archive;
use Piwik\DataTable;
use Piwik\DataTable\DataTableInterface;
use Piwik\DataTable\Filter\ColumnDelete;
use Piwik\Piwik;
class API extends \Piwik\Plugin\API
{
/**
* @param string|int|int[] $idSite
* @param null|string|string[] $columns
*/
public function get($idSite, string $period, string $date, $columns = null): DataTableInterface
{
Piwik::checkUserHasViewAccess($idSite);
$archive = Archive::build($idSite, $period, $date, '');
$metrics = Metrics::getReportMetricColumns();
if ($period !== 'day') {
$metrics = array_filter($metrics, function ($metric) {
return !in_array($metric, [Metrics::METRIC_AI_ASSISTANTS_UNIQUE_DOCUMENT_URLS, Metrics::METRIC_AI_ASSISTANTS_UNIQUE_PAGE_URLS]);
});
}
$dataTable = $archive->getDataTableFromNumeric($metrics);
$this->filterColumns($dataTable, $columns);
return $dataTable;
}
/**
* Returns a report about AI assistants crawling your site and how many hits each one generates. Depending on the provided secondary dimension
* the subtable will either contain all requested page urls or document urls.
*
* @param string|int|int[] $idSite
* @param null|'pages'|'documents' $secondaryDimension can be either `pages` (default) or `documents`
* @return DataTable|DataTable\Map
*/
public function getAIAssistantRequests($idSite, string $period, string $date, bool $expanded = false, bool $flat = false, ?string $secondaryDimension = null): DataTableInterface
{
Piwik::checkUserHasViewAccess($idSite);
$archiveName = Archiver::AI_ASSISTANTS_PAGES_RECORD;
if ($secondaryDimension === 'documents') {
$archiveName = Archiver::AI_ASSISTANTS_DOCUMENTS_RECORD;
}
$dataTable = Archive::createDataTableFromArchive($archiveName, $idSite, $period, $date, '', $expanded, $flat);
// When flattening a report, remove all main table rows, where no subtable exists
if ($flat) {
$dataTable->filter(function (DataTable $table) {
foreach ($table->getRows() as $key => $row) {
if (!$row->getIdSubDataTable()) {
$table->deleteRow($key);
}
}
});
}
return $dataTable;
}
/**
* @param string|int|int[] $idSite
* @return DataTable|DataTable\Map
*/
public function getPageUrlsForAIAssistant($idSite, string $period, string $date, int $idSubtable): DataTableInterface
{
Piwik::checkUserHasViewAccess($idSite);
return Archive::createDataTableFromArchive(Archiver::AI_ASSISTANTS_PAGES_RECORD, $idSite, $period, $date, '', false, false, $idSubtable);
}
/**
* @param string|int|int[] $idSite
* @return DataTable|DataTable\Map
*/
public function getDocumentUrlsForAIAssistant($idSite, string $period, string $date, int $idSubtable): DataTableInterface
{
Piwik::checkUserHasViewAccess($idSite);
return Archive::createDataTableFromArchive(Archiver::AI_ASSISTANTS_DOCUMENTS_RECORD, $idSite, $period, $date, '', false, false, $idSubtable);
}
/**
* @param null|string|string[] $columns
*/
private function filterColumns(DataTableInterface $table, $columns): void
{
if (empty($columns)) {
return;
}
$columnsToKeep = Piwik::getArrayFromApiParameter($columns);
if (empty($columnsToKeep)) {
return;
}
$table->filter(ColumnDelete::class, [[], $columnsToKeep]);
}
}