diff --git a/core/API/DataTableManipulator.php b/core/API/DataTableManipulator.php index cfaa49e63d33045bb996bc2d4b228d6bc87221fb..774863f53966f0462eaa332c93543ca4ffb9f66f 100644 --- a/core/API/DataTableManipulator.php +++ b/core/API/DataTableManipulator.php @@ -119,7 +119,7 @@ abstract class DataTableManipulator $request['idSubtable'] = $idSubTable; if ($dataTable) { - $period = $dataTable->metadata['period']; + $period = $dataTable->getMetadata('period'); if ($period instanceof Range) { $request['date'] = $period->getDateStart() . ',' . $period->getDateEnd(); } else { diff --git a/core/Archive/DataTableFactory.php b/core/Archive/DataTableFactory.php index c21fcf7dd5510f95ec49ec22957fcce25362195b..ab6cd5e56889193e4c7ba4df98084b455a49032e 100644 --- a/core/Archive/DataTableFactory.php +++ b/core/Archive/DataTableFactory.php @@ -205,7 +205,7 @@ class DataTableFactory } // set table metadata - $table->metadata = DataCollection::getDataRowMetadata($blobRow); + $table->setMetadataValues(DataCollection::getDataRowMetadata($blobRow)); if ($this->expandDataTable) { $table->enableRecursiveFilters(); @@ -231,7 +231,7 @@ class DataTableFactory foreach ($blobRow as $name => $blob) { $newTable = DataTable::fromSerializedArray($blob); - $newTable->metadata = $tableMetadata; + $newTable->setAllTableMetadata($tableMetadata); $table->addTable($newTable, $name); } @@ -287,7 +287,7 @@ class DataTableFactory $table = new DataTable\Simple(); if (!empty($data)) { - $table->metadata = DataCollection::getDataRowMetadata($data); + $table->setAllTableMetadata(DataCollection::getDataRowMetadata($data)); DataCollection::removeMetadataFromDataRow($data); @@ -383,8 +383,8 @@ class DataTableFactory { $periods = $this->periods; $table->filter(function ($table) use ($periods) { - $table->metadata['site'] = new Site($table->metadata['site']); - $table->metadata['period'] = $periods[$table->metadata['period']]; + $table->setMetadata('site', new Site($table->getMetadata('site'))); + $table->setMetadata('period', $periods[$table->getMetadata('period')]); }); } diff --git a/core/ArchiveProcessor/Period.php b/core/ArchiveProcessor/Period.php index a885bfefde9533a856d1392830199876cdaece29..eb6199c9bf2cf6fe8e6907d3a3e8ca10a11f5d2e 100644 --- a/core/ArchiveProcessor/Period.php +++ b/core/ArchiveProcessor/Period.php @@ -164,7 +164,7 @@ class Period extends ArchiveProcessor { $table = new DataTable(); if (!empty($columnAggregationOperations)) { - $table->metadata[DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME] = $columnAggregationOperations; + $table->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, $columnAggregationOperations); } $data = $this->archiver->getDataTableExpanded($name, $idSubTable = null, $depth = null, $addMetadataSubtableId = false); diff --git a/core/DataTable.php b/core/DataTable.php index 792ccbf6f543e12dbf3a803b90ed23b111d500d6..cc367d98cd03b877b9f56f977a044a9633435367 100644 --- a/core/DataTable.php +++ b/core/DataTable.php @@ -38,7 +38,7 @@ require_once PIWIK_INCLUDE_PATH . '/core/Common.php'; * Every row has an ID. The ID is either the index of the row or [ID_SUMMARY_ROW](#ID_SUMMARY_ROW). * * DataTables are hierarchical data structures. Each row can also contain an additional - * nested sub-DataTable. + * nested sub-DataTable (commonly referred to as a 'subtable'). * * Both DataTables and DataTable rows can hold **metadata**. _DataTable metadata_ is information * regarding all the data, such as the site or period that the data is for. _Row metadata_ @@ -298,7 +298,7 @@ class DataTable implements DataTableInterface * * @var array */ - public $metadata = array(); + private $metadata = array(); /** * Maximum number of rows allowed in this datatable (including the summary row). @@ -1368,6 +1368,28 @@ class DataTable implements DataTableInterface return $this->metadata; } + /** + * Sets several metadata values by name. + * + * @param array $values Array mapping metadata names with metadata values. + */ + public function setMetadataValues($values) + { + foreach ($values as $name => $value) { + $this->metadata[$name] = $value; + } + } + + /** + * Sets metadata erasing existing values. + * + * @param array $values Array mapping metadata names with metadata values. + */ + public function setAllTableMetadata($metadata) + { + $this->metadata = $metadata; + } + /** * 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 diff --git a/core/DataTable/Filter/AddColumnsProcessedMetrics.php b/core/DataTable/Filter/AddColumnsProcessedMetrics.php index 8c16313d756cf86e6513e0d95da6633bc3ccff23..1303519fdc6301be865a6ca27ac4b9b9f14dc7b4 100644 --- a/core/DataTable/Filter/AddColumnsProcessedMetrics.php +++ b/core/DataTable/Filter/AddColumnsProcessedMetrics.php @@ -29,6 +29,10 @@ use Piwik\Metrics; * * Note: This filter must be called before [ReplaceColumnNames](#) is called. * + * **Basic usage example** + * + * $dataTable->filter('AddColumnsProcessedMetrics'); + * * @package Piwik * @subpackage DataTable * @api diff --git a/core/DataTable/Filter/AddColumnsProcessedMetricsGoal.php b/core/DataTable/Filter/AddColumnsProcessedMetricsGoal.php index cf2f5b4429501ec5171bf6cd1acb5ec9e9595b62..60f9083950d6afc93e7a1477094cc6d3e76b0f23 100644 --- a/core/DataTable/Filter/AddColumnsProcessedMetricsGoal.php +++ b/core/DataTable/Filter/AddColumnsProcessedMetricsGoal.php @@ -43,6 +43,11 @@ use Piwik\Tracker\GoalManager; * * Note: This filter must be called before [ReplaceColumnNames](#) is called. * + * **Basic usage example** + * + * $dataTable->filter('AddColumnsProcessedMetricsGoal', + * array($enable = true, $idGoal = Piwik::LABEL_ID_GOAL_IS_ECOMMERCE_ORDER)); + * * @package Piwik * @subpackage DataTable * @api diff --git a/core/DataTable/Filter/AddConstantMetadata.php b/core/DataTable/Filter/AddConstantMetadata.php deleted file mode 100644 index f3682f5b10659dd6e8617f5f2da710b5cbcd62e4..0000000000000000000000000000000000000000 --- a/core/DataTable/Filter/AddConstantMetadata.php +++ /dev/null @@ -1,52 +0,0 @@ -<?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 - */ -namespace Piwik\DataTable\Filter; - -use Piwik\DataTable; -use Piwik\DataTable\Filter; - -/** - * Add a new metadata column to the table. - * - * This is used to add a column containing the logo width and height of the countries flag icons. - * This value is fixed for all icons so we simply add the same value for all rows. - * - * @package Piwik - * @subpackage DataTable - */ -class AddConstantMetadata extends Filter -{ - /** - * Creates a new filter and sets all required parameters - * - * @param DataTable $table - * @param string $metadataName - * @param mixed $metadataValue - */ - public function __construct($table, $metadataName, $metadataValue) - { - parent::__construct($table); - $this->name = $metadataName; - $this->value = $metadataValue; - } - - /** - * Filters the given data table - * - * @param DataTable $table - */ - public function filter($table) - { - foreach ($table->getRows() as $row) { - $row->addMetadata($this->name, $this->value); - } - } -} diff --git a/core/DataTable/Filter/Limit.php b/core/DataTable/Filter/Limit.php index 501d5fbcb30c55138fdf5d1b8d5b882e97fe8b34..d54edd472df784c4537922e7bd39cd8cab6f7eb4 100644 --- a/core/DataTable/Filter/Limit.php +++ b/core/DataTable/Filter/Limit.php @@ -45,7 +45,7 @@ class Limit extends Filter */ public function filter($table) { - $table->metadata[DataTable::TOTAL_ROWS_BEFORE_LIMIT_METADATA_NAME] = $table->getRowsCount(); + $table->setMetadata(DataTable::TOTAL_ROWS_BEFORE_LIMIT_METADATA_NAME, $table->getRowsCount()); if ($this->keepSummaryRow) { $summaryRow = $table->getRowFromId(DataTable::ID_SUMMARY_ROW); diff --git a/core/DataTable/Map.php b/core/DataTable/Map.php index d0dec41026851c0a311dc92f9e13aee410c09b08..f016cec9128f6a30a3f76320e2556de6565a2a22 100644 --- a/core/DataTable/Map.php +++ b/core/DataTable/Map.php @@ -308,7 +308,7 @@ class Map implements DataTableInterface foreach ($subDataTableMap->getDataTables() as $innerLabel => $subTable) { if (!isset($result->array[$innerLabel])) { $dataTable = new DataTable(); - $dataTable->metadata = $subTable->metadata; + $dataTable->setMetadataValues($subTable->getAllTableMetadata()); $result->addTable($dataTable, $innerLabel); } diff --git a/core/DataTable/Renderer/Console.php b/core/DataTable/Renderer/Console.php index 667ea97a78ac265841e80219965db308a9fa31fc..0fab067dc0fb5a0a03d141dffc2431cc142ba206 100644 --- a/core/DataTable/Renderer/Console.php +++ b/core/DataTable/Renderer/Console.php @@ -154,9 +154,10 @@ class Console extends Renderer $i++; } - if (!empty($table->metadata)) { + $metadata = $table->getAllTableMetadata(); + if (!empty($metadata)) { $output .= "<hr />Metadata<br />"; - foreach ($table->metadata as $id => $metadata) { + foreach ($metadata as $id => $metadata) { $output .= "<br />"; $output .= $prefix . " <b>$id</b><br />"; foreach ($metadata as $name => $value) { diff --git a/core/DataTable/Row.php b/core/DataTable/Row.php index 104744b88bb86c60bb3219a8cc5b6c9a632a0915..ecfb8ef82f2c86c6998ffa43a8ac070e91110e22 100644 --- a/core/DataTable/Row.php +++ b/core/DataTable/Row.php @@ -283,8 +283,8 @@ class Row $thisSubTable = new DataTable(); $this->addSubtable($thisSubTable); } - $thisSubTable->metadata[DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME] - = $subTable->getMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME); + $columnOps = $subTable->getMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME); + $thisSubTable->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, $columnOps); $thisSubTable->addDataTable($subTable); } diff --git a/plugins/API/ProcessedReport.php b/plugins/API/ProcessedReport.php index 389666b4e32610f62a838e47c0a12d8d87545824..4888f1639ca6a9b12574a5c3e8b5dabe735d980d 100644 --- a/plugins/API/ProcessedReport.php +++ b/plugins/API/ProcessedReport.php @@ -386,9 +386,9 @@ class ProcessedReport $this->removeEmptyColumns($columns, $reportMetadata, $simpleDataTable); list($enhancedSimpleDataTable, $rowMetadata) = $this->handleSimpleDataTable($idSite, $simpleDataTable, $columns, $hasDimension, $showRawMetrics); - $enhancedSimpleDataTable->metadata = $simpleDataTable->metadata; + $enhancedSimpleDataTable->setAllTableMetadata($simpleDataTable->getAllTableMetadata()); - $period = $simpleDataTable->metadata['period']->getLocalizedLongString(); + $period = $simpleDataTable->getMetadata('period')->getLocalizedLongString(); $newReport->addTable($enhancedSimpleDataTable, $period); $rowsMetadata->addTable($rowMetadata, $period); } diff --git a/plugins/Actions/API.php b/plugins/Actions/API.php index 84ceca80acbe790bcbc9a055f03ececbe1c827fd..5e15d1b7702b8caef13aa1a6470242a6f79ab0dd 100644 --- a/plugins/Actions/API.php +++ b/plugins/Actions/API.php @@ -347,10 +347,12 @@ class API extends \Piwik\Plugin\API $dataTables = $dataTable->getDataTables(); foreach ($customVariableDatatables as $key => $customVariableTableForDate) { // we do not enter the IF, in the case idSite=1,3 AND period=day&date=datefrom,dateto, - if (isset($customVariableTableForDate->metadata['period'])) { + if ($customVariableTableForDate instanceof DataTable + && $customVariableTableForDate->getMetadata('period') + ) { $row = $customVariableTableForDate->getRowFromLabel($customVarNameToLookFor); if ($row) { - $dateRewrite = $customVariableTableForDate->metadata['period']->getDateStart()->toString(); + $dateRewrite = $customVariableTableForDate->getMetadata('period')->getDateStart()->toString(); $idSubtable = $row->getIdSubDataTable(); $categories = APICustomVariables::getInstance()->getCustomVariablesValuesFromNameId($idSite, $period, $dateRewrite, $idSubtable, $segment); $dataTable->addTable($categories, $key); @@ -443,7 +445,7 @@ class API extends \Piwik\Plugin\API if ($row === false) { // not found $result = new DataTable; - $result->metadata = $table->metadata; + $result->setAllTableMetadata($table->getAllTableMetadata()); return $result; } @@ -451,7 +453,7 @@ class API extends \Piwik\Plugin\API if (count($searchTree) == 0) { $result = new DataTable(); $result->addRow($row); - $result->metadata = $table->metadata; + $result->setAllTableMetadata($table->getAllTableMetadata()); return $result; } diff --git a/plugins/Actions/Archiver.php b/plugins/Actions/Archiver.php index e9e298ceb919a6021bc035f07df5645f88e8aa95..8335553c1e1639856cbe780910d1a4fdfec738cd 100644 --- a/plugins/Actions/Archiver.php +++ b/plugins/Actions/Archiver.php @@ -166,7 +166,7 @@ class Archiver extends \Piwik\Plugin\Archiver || $type == Action::TYPE_ACTION_NAME ) { // for page urls and page titles, performance metrics exist and have to be aggregated correctly - $dataTable->metadata[DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME] = self::$actionColumnAggregationOperations; + $dataTable->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, self::$actionColumnAggregationOperations); } $this->actionsTablesByType[$type] = $dataTable; diff --git a/plugins/Goals/API.php b/plugins/Goals/API.php index 410af4d26f357fe9345a77882a38915b7dfeec74..5ad1bc28dcb9f950c2c491ea6744cc9c0454e18d 100644 --- a/plugins/Goals/API.php +++ b/plugins/Goals/API.php @@ -253,8 +253,10 @@ class API extends \Piwik\Plugin\API // we do not enter the IF // if case idSite=1,3 AND period=day&date=datefrom,dateto, - if (isset($customVariableTableForDate->metadata['period'])) { - $dateRewrite = $customVariableTableForDate->metadata['period']->getDateStart()->toString(); + if ($customVariableTableForDate instanceof DataTable + && $customVariableTableForDate->getMetadata('period') + ) { + $dateRewrite = $customVariableTableForDate->getMetadata('period')->getDateStart()->toString(); $row = $customVariableTableForDate->getRowFromLabel($customVarNameToLookFor); if ($row) { $idSubtable = $row->getIdSubDataTable(); diff --git a/plugins/ImageGraph/API.php b/plugins/ImageGraph/API.php index ce0b600f6b43c66f64f11e50a1f4d54e3bfec6fb..f97f71c74458de855f2ee804094755a99444a094 100644 --- a/plugins/ImageGraph/API.php +++ b/plugins/ImageGraph/API.php @@ -438,7 +438,7 @@ class API extends \Piwik\Plugin\API } } - $rowId = $periodsData[$i]->metadata['period']->getLocalizedShortString(); + $rowId = $periodsData[$i]->getMetadata('period')->getLocalizedShortString(); $abscissaSeries[] = Common::unsanitizeInputValue($rowId); } } diff --git a/plugins/UserCountry/API.php b/plugins/UserCountry/API.php index 79ac0cb6ec3113b21a0a24833fc6d043145a10fe..0c70cbc8bab41923455ac93eb571717b948b35a9 100644 --- a/plugins/UserCountry/API.php +++ b/plugins/UserCountry/API.php @@ -38,8 +38,9 @@ class API extends \Piwik\Plugin\API $dataTable->filter('ColumnCallbackAddMetadata', array('label', 'code')); $dataTable->filter('ColumnCallbackAddMetadata', array('label', 'logo', __NAMESPACE__ . '\getFlagFromCode')); $dataTable->filter('ColumnCallbackReplace', array('label', __NAMESPACE__ . '\countryTranslate')); - $dataTable->queueFilter('AddConstantMetadata', array('logoWidth', 16)); - $dataTable->queueFilter('AddConstantMetadata', array('logoHeight', 11)); + + $dataTable->queueFilter('ColumnCallbackAddMetadata', array(array(), 'logoWidth', function () { return 16; })); + $dataTable->queueFilter('ColumnCallbackAddMetadata', array(array(), 'logoHeight', function () { return 11; })); return $dataTable; } diff --git a/plugins/UserSettings/API.php b/plugins/UserSettings/API.php index d01b50793f2c89b597d0817f8cada22a2c38de7a..ddd7edb23192a6305afb33fcbb7889e1a40a5712 100644 --- a/plugins/UserSettings/API.php +++ b/plugins/UserSettings/API.php @@ -219,7 +219,9 @@ class API extends \Piwik\Plugin\API // When Truncate filter is applied, it will call AddSummaryRow which tries to sum all rows. // We tell the object to skip the column nb_visits_percentage when aggregating (since it's not correct to sum % values) - $table->metadata[DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME]['nb_visits_percentage'] = 'skip'; + $columnAggregationOps = $table->getMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME); + $columnAggregationOps['nb_visits_percentage'] = 'skip'; + $table->setMetadata(DataTable::COLUMN_AGGREGATION_OPS_METADATA_NAME, $columnAggregationOps); // The filter must be applied now so that the new column can // be sorted by the generic filters (applied right after this loop exits)