Piwik\DataTable\
A DataTable where every row has two columns: label and value.
Simple DataTables are only used to slightly alter the output of some renderers (notably the XML renderer).
This class defines the following constants:
COLUMN_AGGREGATION_OPS_METADATA_NAME
— Name for metadata that describes how individual columns should be aggregated when addDataTable()
or Row::sumRow() is called. Inherited from DataTable
- ID_ARCHIVED_METADATA_ROW
— The ID of the special metadata row. This row only exists in the serialized row data and stores the datatable metadata. Inherited from DataTable
- EXTRA_PROCESSED_METRICS_METADATA_NAME
— Name for metadata that contains extra ProcessedMetrics for a DataTable. Inherited from DataTable
COLUMN_AGGREGATION_OPS_METADATA_NAME
This metadata value must be an array that maps column names with valid operations. Valid aggregation operations are:
'skip'
: do nothing'max'
: does max($column1, $column2)
'min'
: does min($column1, $column2)
'sum'
: does $column1 + $column2
See addDataTable() and DataTable\Row::sumRow() for more information.
ID_ARCHIVED_METADATA_ROW
This allows us to save datatable metadata in archive data.
EXTRA_PROCESSED_METRICS_METADATA_NAME
These metrics will be added in addition to the ones specified in the table's associated Report class.
The class defines the following methods:
__construct()
ash; Constructor. Inherited from DataTable
__destruct()
ash; Destructor. Inherited from DataTable
__clone()
ash; Clone. Inherited from DataTable
setLabelsHaveChanged()
Inherited from DataTable
sort()
ash; Sorts the DataTable rows using the supplied callback function. Inherited from DataTable
setTotalsRow()
Inherited from DataTable
getTotalsRow()
Inherited from DataTable
getSummaryRow()
Inherited from DataTable
getSortedByColumnName()
ash; Returns the name of the column this table was sorted by (if any). Inherited from DataTable
enableRecursiveSort()
ash; Enables recursive sorting. Inherited from DataTable
enableRecursiveFilters()
ash; Enables recursive filtering. Inherited from DataTable
filter()
ash; Applies a filter to this datatable. Inherited from DataTable
multiFilter()
ash; Invokes $filter
with this table and every table in $otherTables
. Inherited from DataTable
filterSubtables()
ash; Applies a filter to all subtables but not to this datatable. Inherited from DataTable
queueFilterSubtables()
ash; Adds a filter and a list of parameters to the list of queued filters of all subtables. Inherited from DataTable
queueFilter()
ash; Adds a filter and a list of parameters to the list of queued filters. Inherited from DataTable
applyQueuedFilters()
ash; Applies all filters that were previously queued to the table. Inherited from DataTable
addDataTable()
ash; Sums a DataTable to this one. Inherited from DataTable
getRowFromLabel()
ash; Returns the Row whose 'label'
column is equal to $label
. Inherited from DataTable
getRowIdFromLabel()
ash; Returns the row id for the row whose 'label'
column is equal to $label
. Inherited from DataTable
getEmptyClone()
ash; Returns an empty DataTable with the same metadata and queued filters as $this
one. Inherited from DataTable
getRowFromId()
ash; Returns a row by ID. Inherited from DataTable
getRowFromIdSubDataTable()
ash; Returns the row that has a subtable with ID matching $idSubtable
. Inherited from DataTable
addRow()
ash; Adds a row to this table. Inherited from DataTable
addSummaryRow()
ash; Sets the summary row. Inherited from DataTable
getId()
ash; Returns the DataTable ID. Inherited from DataTable
addRowFromArray()
ash; Adds a new row from an array. Inherited from DataTable
addRowFromSimpleArray()
ash; Adds a new row a from an array of column values. Inherited from DataTable
getRows()
ash; Returns the array of Rows. Inherited from DataTable
getColumn()
ash; Returns an array containing all column values for the requested column. Inherited from DataTable
getColumnsStartingWith()
ash; Returns an array containing all column values of columns whose name starts with $name
. Inherited from DataTable
getColumns()
ash; Returns the names of every column this DataTable contains. Inherited from DataTable
getRowsMetadata()
ash; Returns an array containing the requested metadata value of each row. Inherited from DataTable
deleteRowsMetadata()
ash; Delete row metadata by name in every row. Inherited from DataTable
getRowsCount()
ash; Returns the number of rows in the table including the summary row. Inherited from DataTable
getFirstRow()
ash; Returns the first row of the DataTable. Inherited from DataTable
getLastRow()
ash; Returns the last row of the DataTable. Inherited from DataTable
getRowsCountRecursive()
ash; Returns the number of rows in the entire DataTable hierarchy. Inherited from DataTable
deleteColumn()
ash; Delete a column by name in every row. Inherited from DataTable
__sleep()
Inherited from DataTable
renameColumn()
ash; Rename a column in every row. Inherited from DataTable
deleteColumns()
ash; Deletes several columns by name in every row. Inherited from DataTable
deleteRow()
ash; Deletes a row by ID. Inherited from DataTable
deleteRowsOffset()
ash; Deletes rows from $offset
to $offset + $limit
. Inherited from DataTable
deleteRows()
ash; Deletes a set of rows by ID. Inherited from DataTable
__toString()
ash; Returns a string representation of this DataTable for convenient viewing. Inherited from DataTable
isEqual()
ash; Returns true if both DataTable instances are exactly the same. Inherited from DataTable
getSerialized()
ash; Serializes an entire DataTable hierarchy and returns the array of serialized DataTables. Inherited from DataTable
addRowsFromSerializedArray()
ash; Adds a set of rows from a serialized DataTable string. Inherited from DataTable
addRowsFromArray()
— Adds rows based on an array mapping label column values to value column values.addRowsFromSimpleArray()
ash; Adds multiple rows from an array containing arrays of column values. Inherited from DataTable
makeFromIndexedArray()
ash; Rewrites the input $array
Inherited from DataTable
setMaximumDepthLevelAllowedAtLeast()
— Sets the maximum depth level to at least a certain value. Inherited from DataTable
getMetadata()
ash; Returns metadata by name. Inherited from DataTable
setMetadata()
ash; Sets a metadata value by name. Inherited from DataTable
deleteMetadata()
ash; Deletes a metadata property by name. Inherited from DataTable
getAllTableMetadata()
ash; Returns all table metadata. Inherited from DataTable
setMetadataValues()
ash; Sets several metadata values by name. Inherited from DataTable
setAllTableMetadata()
ash; Sets metadata, erasing existing values. Inherited from DataTable
setMaximumAllowedRows()
ash; Sets the maximum number of rows allowed in this datatable (including the summary row). Inherited from DataTable
walkPath()
ash; Traverses a DataTable tree using an array of labels and returns the row it finds or false
if it cannot find one. Inherited from DataTable
mergeSubtables()
ash; Returns a new DataTable in which the rows of this table are replaced with the aggregatated rows of all its subtables. Inherited from DataTable
makeFromSimpleArray()
ash; Returns a new DataTable created with data from a 'simple' array. Inherited from DataTable
fromSerializedArray()
ash; Creates a new DataTable instance from a serialized DataTable string. Inherited from DataTable
clearQueuedFilters()
ash; Unsets all queued filters. Inherited from DataTable
getQueuedFilters()
Inherited from DataTable
getIterator()
Inherited from DataTable
offsetExists()
Inherited from DataTable
offsetGet()
Inherited from DataTable
offsetSet()
Inherited from DataTable
offsetUnset()
Inherited from DataTable
sumRowWithLabel()
Inherited from DataTable
__construct()
Constructor. Creates an empty DataTable.
__destruct()
Destructor. Makes sure DataTable memory will be cleaned up.
__clone()
Clone. Called when cloning the datatable. We need to make sure to create a new datatableId.
If we do not increase tableId it can result in segmentation faults when destructing a datatable.
setLabelsHaveChanged()
sort()
Sorts the DataTable rows using the supplied callback function.
$functionCallback
(string
) —
A comparison callback compatible with usort.$columnSortedBy
(string
) —
The column name $functionCallback
sorts by. This is stored so we can determine how the DataTable was sorted in the future.setTotalsRow()
It accepts the following parameter(s):
$totalsRow
(Row
) —It does not return anything or a mixed result.
getTotalsRow()
getSummaryRow()
getSortedByColumnName()
Returns the name of the column this table was sorted by (if any).
See sort().
false
|string
—
The sorted column name or false if none.enableRecursiveSort()
Enables recursive sorting. If this method is called sort() will also sort all subtables.
enableRecursiveFilters()
Enables recursive filtering. If this method is called then the filter() method will apply filters to every subtable in addition to this instance.
filter()
Applies a filter to this datatable.
If enableRecursiveFilters() was called, the filter will be applied to all subtables as well.
$className
(string
|Closure
) —
Class name, eg. "Sort"
or "Piwik\DataTable\Filters\Sort". If no namespace is supplied,
Piwik\DataTable\BaseFilter` is assumed. This parameter can also be a closure that takes a DataTable as its first parameter.$parameters
(array
) —
Array of extra parameters to pass to the filter.multiFilter()
Invokes $filter
with this table and every table in $otherTables
. The result of $filter()
is returned.
This method is used to iterate over multiple DataTable\Map's concurrently.
See Map::multiFilter() for more information.
It accepts the following parameter(s):
$otherTables
(DataTable[]
) —
$filter
(callable
) —
filter A function like function (DataTable $thisTable, $otherTable1, $otherTable2) {}
.
Returns: mixed
—
The result of $filter.
filterSubtables()
Applies a filter to all subtables but not to this datatable.
$className
(string
|Closure
) —
Class name, eg. "Sort"
or "Piwik\DataTable\Filters\Sort". If no namespace is supplied,
Piwik\DataTable\BaseFilter` is assumed. This parameter can also be a closure that takes a DataTable as its first parameter.$parameters
(array
) —
Array of extra parameters to pass to the filter.queueFilterSubtables()
Adds a filter and a list of parameters to the list of queued filters of all subtables. These filters will be executed when applyQueuedFilters() is called.
Filters that prettify the column values or don't need the full set of rows should be queued. This way they will be run after the table is truncated which will result in better performance.
$className
(string
|Closure
) —
The class name of the filter, eg. 'Limit'
.$parameters
(array
) —
The parameters to give to the filter, eg. array($offset, $limit)
for the Limit filter.queueFilter()
Adds a filter and a list of parameters to the list of queued filters. These filters will be executed when applyQueuedFilters() is called.
Filters that prettify the column values or don't need the full set of rows should be queued. This way they will be run after the table is truncated which will result in better performance.
$className
(string
|Closure
) —
The class name of the filter, eg. 'Limit'
.$parameters
(array
) —
The parameters to give to the filter, eg. array($offset, $limit)
for the Limit filter.applyQueuedFilters()
Applies all filters that were previously queued to the table. See queueFilter() for more information.
addDataTable()
Sums a DataTable to this one.
This method will sum rows that have the same label. If a row is found in $tableToSum
whose
label is not found in $this
, the row will be added to $this
.
If the subtables for this table are loaded, they will be summed as well.
Rows are summed together by summing individual columns. By default columns are summed by adding one column value to another. Some columns cannot be aggregated this way. In these cases, the COLUMN_AGGREGATION_OPS_METADATA_NAME metadata can be used to specify a different type of operation.
It accepts the following parameter(s):
$tableToSum
(DataTable
) —It does not return anything or a mixed result.
getRowFromLabel()
Returns the Row whose 'label'
column is equal to $label
.
This method executes in constant time except for the first call which caches row label => row ID mappings.
It accepts the following parameter(s):
$label
(string
) —
'label'
column value to look for.Returns: Row
|false
—
The row if found, false
if otherwise.
getRowIdFromLabel()
Returns the row id for the row whose 'label'
column is equal to $label
.
This method executes in constant time except for the first call which caches row label => row ID mappings.
It accepts the following parameter(s):
$label
(string
) —
'label'
column value to look for.Returns: int
—
The row ID.
getEmptyClone()
Returns an empty DataTable with the same metadata and queued filters as $this
one.
$keepFilters
(bool
) —
Whether to pass the queued filter list to the new DataTable or not.DataTable
value.getRowFromId()
Returns a row by ID. The ID is either the index of the row or ID_SUMMARY_ROW.
It accepts the following parameter(s):
$id
(int
) —
The row ID.Returns: Row
|false
—
The Row or false if not found.
getRowFromIdSubDataTable()
Returns the row that has a subtable with ID matching $idSubtable
.
It accepts the following parameter(s):
$idSubTable
(int
) —
The subtable ID.Returns: Row
|false
—
The row or false if not found
addRow()
Adds a row to this table.
If setMaximumAllowedRows() was called and the current row count is at the maximum, the new row will be summed to the summary row. If there is no summary row, this row is set as the summary row.
It accepts the following parameter(s):
$row
(Row
) —Returns: Row
—
$row
or the summary row if we're at the maximum number of rows.
addSummaryRow()
Sets the summary row.
Note: A DataTable can have only one summary row.
getId()
Returns the DataTable ID.
int
value.addRowFromArray()
Adds a new row from an array.
You can add row metadata with this method.
$row
(array
) —
eg. array(Row::COLUMNS => array('visits' => 13, 'test' => 'toto'), Row::METADATA => array('mymetadata' => 'myvalue'))
addRowFromSimpleArray()
Adds a new row a from an array of column values.
Row metadata cannot be added with this method.
$row
(array
) —
eg. array('name' => 'google analytics', 'license' => 'commercial')
getRows()
Returns the array of Rows.
Internal logic in Matomo core should avoid using this method as it is time and memory consuming when being executed thousands of times. The alternative is to use getRowsWithoutSummaryRow() + get the summary row manually.
Row[]
value.getColumn()
Returns an array containing all column values for the requested column.
It accepts the following parameter(s):
$name
(string
) —
The column name.Returns: array
—
The array of column values.
getColumnsStartingWith()
Returns an array containing all column values of columns whose name starts with $name
.
It accepts the following parameter(s):
$namePrefix
(string
) —
The column name prefix.Returns: array
—
The array of column values.
getColumns()
Returns the names of every column this DataTable contains. This method will return the columns of the first row with data and will assume they occur in every other row as well.
_ Note: If column names still use their in-database INDEX values (@see Metrics), they will be converted to their string name in the array result._
array
—
Array of string column names.getRowsMetadata()
Returns an array containing the requested metadata value of each row.
$name
(string
) —
The metadata column to return.array
value.deleteRowsMetadata()
Delete row metadata by name in every row.
It accepts the following parameter(s):
$name
$deleteRecursiveInSubtables
(bool
) —
It does not return anything or a mixed result.
getRowsCount()
Returns the number of rows in the table including the summary row.
int
value.getFirstRow()
Returns the first row of the DataTable.
Row
|false
—
The first row or false
if it cannot be found.getLastRow()
Returns the last row of the DataTable. If there is a summary row, it will always be considered the last row.
Row
|false
—
The last row or false
if it cannot be found.getRowsCountRecursive()
Returns the number of rows in the entire DataTable hierarchy. This is the number of rows in this DataTable summed with the row count of each descendant subtable.
int
value.deleteColumn()
Delete a column by name in every row. This change is NOT applied recursively to all subtables.
$name
(string
) —
Column name to delete.__sleep()
renameColumn()
Rename a column in every row. This change is applied recursively to all subtables.
$oldName
(string
) —
Old column name.$newName
(string
) —
New column name.deleteColumns()
Deletes several columns by name in every row.
$names
(array
) —
List of column names to delete.$deleteRecursiveInSubtables
(bool
) —
Whether to apply this change to all subtables or not.deleteRow()
Deletes a row by ID.
$id
(int
) —
The row ID.Exception
— If the row $id
cannot be found.deleteRowsOffset()
Deletes rows from $offset
to $offset + $limit
.
It accepts the following parameter(s):
$offset
(int
) —
The offset to start deleting rows from.$limit
(int
|null
) —
The number of rows to delete. If null
all rows after the offset will be removed.Returns: int
—
The number of rows deleted.
deleteRows()
Deletes a set of rows by ID.
$rowIds
(array
) —
The list of row IDs to delete.Exception
— If a row ID cannot be found.__toString()
Returns a string representation of this DataTable for convenient viewing.
Note: This uses the html DataTable renderer.
string
value.isEqual()
Returns true if both DataTable instances are exactly the same.
DataTables are equal if they have the same number of rows, if each row has a label that exists in the other table, and if each row is equal to the row in the other table with the same label. The order of rows is not important.
It accepts the following parameter(s):
It returns a bool
value.
getSerialized()
Serializes an entire DataTable hierarchy and returns the array of serialized DataTables.
The first element in the returned array will be the serialized representation of this DataTable. Every subsequent element will be a serialized subtable.
This DataTable and subtables can optionally be truncated before being serialized. In most cases where DataTables can become quite large, they should be truncated before being persisted in an archive.
The result of this method is intended for use with the ArchiveProcessor::insertBlobRecord() method.
It accepts the following parameter(s):
$maximumRowsInDataTable
(int
) —
If not null, defines the maximum number of rows allowed in the serialized DataTable.$maximumRowsInSubDataTable
(int
) —
If not null, defines the maximum number of rows allowed in serialized subtables.$columnToSortByBeforeTruncation
(string
) —
The column to sort by before truncating, eg, Metrics::INDEX_NB_VISITS
.$aSerializedDataTable
(array
) —
Will contain all the output arraysReturns: array
—
The array of serialized DataTables:
array(
// this DataTable (the root)
0 => 'eghuighahgaueytae78yaet7yaetae',
// a subtable
1 => 'gaegae gh gwrh guiwh uigwhuige',
// another subtable
2 => 'gqegJHUIGHEQjkgneqjgnqeugUGEQHGUHQE',
// etc.
);
Exception
— If infinite recursion detected. This will occur if a table's subtable is one of its parent tables.addRowsFromSerializedArray()
Adds a set of rows from a serialized DataTable string.
See serialize().
Note: This function will successfully load DataTables serialized by Matomo (formerly Piwik) 1.X.
$serialized
(string
) —
A string with the format of a string in the array returned by serialize().Exception
— if $serialized
is invalid.addRowsFromArray()
Adds rows based on an array mapping label column values to value column values.
$array
(array
) —
Array with the following structure array( // row1 array( Row::COLUMNS => array( col1_name => value1, col2_name => value2, ...), Row::METADATA => array( metadata1_name => value1, ...), // see Row ), // row2 array( ... ), )addRowsFromSimpleArray()
Adds multiple rows from an array containing arrays of column values.
Row metadata cannot be added with this method.
$array
(array
) —
Array with the following structure: array( array( col1_name => valueA, col2_name => valueC, ...), array( col1_name => valueB, col2_name => valueD, ...), )Exception
— if $array
is in an incorrect format.makeFromIndexedArray()
Rewrites the input $array
array ( LABEL => array(col1 => X, col2 => Y), LABEL2 => array(col1 => X, col2 => Y), )
to a DataTable with rows that look like:
array (
array( Row::COLUMNS => array('label' => LABEL, col1 => X, col2 => Y)),
array( Row::COLUMNS => array('label' => LABEL2, col1 => X, col2 => Y)),
)
Will also convert arrays like:
array (
LABEL => X,
LABEL2 => Y,
)
to:
array (
array( Row::COLUMNS => array('label' => LABEL, 'value' => X)),
array( Row::COLUMNS => array('label' => LABEL2, 'value' => Y)),
)
$array
(array
) —
Indexed array, two formats supported, see above.$subtablePerLabel
(array
|null
) —
An array mapping label values with DataTable instances to associate as a subtable.DataTable
value.setMaximumDepthLevelAllowedAtLeast()
Sets the maximum depth level to at least a certain value. If the current value is
greater than $atLeastLevel
, the maximum nesting level is not changed.
The maximum depth level determines the maximum number of subtable levels in the
DataTable tree. For example, if it is set to 2
, this DataTable is allowed to
have subtables, but the subtables are not.
It accepts the following parameter(s):
$atLeastLevel
(int
) —It does not return anything or a mixed result.
getMetadata()
Returns metadata by name.
It accepts the following parameter(s):
$name
(string
) —
The metadata name.Returns: mixed
|false
—
The metadata value or false
if it cannot be found.
setMetadata()
Sets a metadata value by name.
It accepts the following parameter(s):
$name
(string
) —
The metadata name.$value
(mixed
) —It does not return anything or a mixed result.
deleteMetadata()
Deletes a metadata property by name.
It accepts the following parameter(s):
$name
(bool
|string
) —
The metadata name (omit to delete all metadata)Returns: bool
—
True if the requested metadata was deleted
getAllTableMetadata()
Returns all table metadata.
array
value.setMetadataValues()
Sets several metadata values by name.
$values
(array
) —
Array mapping metadata names with metadata values.setAllTableMetadata()
Sets metadata, erasing existing values.
It accepts the following parameter(s):
$metadata
It does not return anything or a mixed result.
setMaximumAllowedRows()
Sets the maximum number of rows allowed in this datatable (including the summary row). If adding more then the allowed number of rows is attempted, the extra rows are summed to the summary row.
$maximumAllowedRows
(int
) —
If 0
, the maximum number of rows is unset.walkPath()
Traverses a DataTable tree using an array of labels and returns the row
it finds or false
if it cannot find one. The number of path segments that
were successfully walked is also returned.
If $missingRowColumns
is supplied, the specified path is created. When
a subtable is encountered w/o the required label, a new row is created
with the label, and a new subtable is added to the row.
Read http://en.wikipedia.org/wiki/Tree_(data_structure)#Traversal_methods for more information about tree walking.
It accepts the following parameter(s):
$path
(array
) —
The path to walk. An array of label values. The first element refers to a row in this DataTable, the second in a subtable of the first row, the third a subtable of the second row, etc.$missingRowColumns
(array
|bool
) —
The default columns to use when creating new rows. If this parameter is supplied, new rows will be created for path labels that cannot be found.$maxSubtableRows
(int
) —
The maximum number of allowed rows in new subtables. New subtables are only created if $missingRowColumns
is provided.Returns: array
—
First element is the found row or false
. Second element is
the number of path segments walked. If a row is found, this
will be == to count($path)
. Otherwise, it will be the index
of the path segment that we could not find.
mergeSubtables()
Returns a new DataTable in which the rows of this table are replaced with the aggregatated rows of all its subtables.
$labelColumn
(string
|bool
) —
If supplied the label of the parent row will be added to a new column in each subtable row. If set to, 'label'
each subtable row's label will be prepended w/ the parent row's label. So 'child_label'
becomes 'parent_label - child_label'
.$useMetadataColumn
(bool
) —
If true and if $labelColumn
is supplied, the parent row's label will be added as metadata and not a new column.DataTable
value.makeFromSimpleArray()
Returns a new DataTable created with data from a 'simple' array.
It accepts the following parameter(s):
$array
(array
) —It returns a DataTable
value.
fromSerializedArray()
Creates a new DataTable instance from a serialized DataTable string.
See getSerialized() and addRowsFromSerializedArray() for more information on DataTable serialization.
It accepts the following parameter(s):
$data
(string
) —It returns a DataTable
value.
clearQueuedFilters()
Unsets all queued filters.
getQueuedFilters()
getIterator()
ArrayIterator
value.offsetExists()
It accepts the following parameter(s):
$offset
It returns a bool
value.
offsetGet()
It accepts the following parameter(s):
$offset
It returns a Row
value.
offsetSet()
It accepts the following parameter(s):
$offset
$value
It returns a void
value.
offsetUnset()
It accepts the following parameter(s):
$offset
It returns a void
value.
sumRowWithLabel()
It accepts the following parameter(s):
$label
$columns
(array
) —
$aggregationOps
(array
|null
) —
It returns a Row
value.