GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — DataExplorer/Query/PreviewTree.php at 6.x-dev · openmost/DataExplorer

Expand file tree

/

PreviewTree.php

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
156 lines (136 loc) · 4.65 KB
/

PreviewTree.php

Copy path

File metadata and controls

156 lines (136 loc) · 4.65 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?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\DataExplorer\Query;
use Piwik\DataTable;
use Piwik\DataTable\Row;
use Piwik\Plugins\DataExplorer\Query\SavedReportReader;
/**
* The result of a live exploration as a plain array, so it can be cached between the requests of the native
* data table of the canvas (sort, limit, page, opening a row), and read back as a DataTable.
*
* The rows of the hierarchical view are opened one level at a time by subtable id. The ids of a live result
* only live for one request, so the rows that have children are numbered in tree order instead (1, 2, ...):
* the same cached result always gives the same numbers.
*/
class PreviewTree
{
/**
* @return array{metadata: array, rows: array[]}
*/
public static function export(DataTable $table): array
{
$metadata = [];
foreach ($table->getAllTableMetadata() as $name => $value) {
if (is_scalar($value) || is_array($value)) {
$metadata[$name] = $value;
}
}
$rows = [];
foreach ($table->getRows() as $row) {
$subtable = $row->getSubtable();
$rows[] = [
'columns' => $row->getColumns(),
'metadata' => $row->getMetadata(),
'children' => $subtable instanceof DataTable ? self::export($subtable) : null,
];
}
return ['metadata' => $metadata, 'rows' => $rows];
}
/**
* The whole result with its subtables in memory (flat view, export).
*/
public static function toTable(array $tree): DataTable
{
$table = self::emptyTable($tree);
foreach ($tree['rows'] as $node) {
$row = self::row($node);
if (is_array($node['children'])) {
$row->setSubtable(self::toTable($node['children']));
}
$table->addRow($row);
}
return $table;
}
/**
* One level of the hierarchical view: the first level, or the children of the row numbered $idSubtable.
* Each row with children carries its number as subtable id, its children are not loaded.
*
* @return DataTable|null null when no row has this number
*/
public static function level(array $tree, int $idSubtable = 0): ?DataTable
{
$next = 1;
$numbered = self::number($tree, $next);
[$node, $depth] = $idSubtable > 0 ? self::find($numbered, $idSubtable, 1) : [$numbered, 0];
if ($node === null) {
return null;
}
$table = self::emptyTable($node);
$table->setMetadata(SavedReportReader::DEPTH_METADATA, $depth);
foreach ($node['rows'] as $child) {
$row = self::row($child);
if (isset($child['id'])) {
$row->setNonLoadedSubtableId($child['id']);
}
$table->addRow($row);
}
return $table;
}
/**
* Numbers the rows that have children, parents before their children.
*/
private static function number(array $tree, int &$next): array
{
foreach ($tree['rows'] as $index => $node) {
if (is_array($node['children'])) {
$tree['rows'][$index]['id'] = $next++;
}
}
foreach ($tree['rows'] as $index => $node) {
if (is_array($node['children'])) {
$tree['rows'][$index]['children'] = self::number($node['children'], $next);
}
}
return $tree;
}
/**
* @return array{0: array|null, 1: int} the children of the row numbered $id and their level
*/
private static function find(array $tree, int $id, int $depth): array
{
foreach ($tree['rows'] as $node) {
if (!is_array($node['children'])) {
continue;
}
if (($node['id'] ?? 0) === $id) {
return [$node['children'], $depth];
}
$found = self::find($node['children'], $id, $depth + 1);
if ($found[0] !== null) {
return $found;
}
}
return [null, $depth];
}
private static function emptyTable(array $tree): DataTable
{
$table = new DataTable();
foreach ($tree['metadata'] as $name => $value) {
$table->setMetadata($name, $value);
}
return $table;
}
private static function row(array $node): Row
{
return new Row([
Row::COLUMNS => $node['columns'],
Row::METADATA => $node['metadata'],
]);
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.