From a10eb83bfc85221b0afaedccfae881a55639d782 Mon Sep 17 00:00:00 2001
From: diosmosis <benakamoorthi@fastmail.fm>
Date: Tue, 22 Oct 2013 00:01:08 -0400
Subject: [PATCH] Refs #4200, make DataTable:: private & add public methods
 necessary, tweaking some filter docs, and remove AddConstantMetadata filter.

---
 core/API/DataTableManipulator.php             |  2 +-
 core/Archive/DataTableFactory.php             | 10 ++--
 core/ArchiveProcessor/Period.php              |  2 +-
 core/DataTable.php                            | 26 +++++++++-
 .../Filter/AddColumnsProcessedMetrics.php     |  4 ++
 .../Filter/AddColumnsProcessedMetricsGoal.php |  5 ++
 core/DataTable/Filter/AddConstantMetadata.php | 52 -------------------
 core/DataTable/Filter/Limit.php               |  2 +-
 core/DataTable/Map.php                        |  2 +-
 core/DataTable/Renderer/Console.php           |  5 +-
 core/DataTable/Row.php                        |  4 +-
 plugins/API/ProcessedReport.php               |  4 +-
 plugins/Actions/API.php                       | 10 ++--
 plugins/Actions/Archiver.php                  |  2 +-
 plugins/Goals/API.php                         |  6 ++-
 plugins/ImageGraph/API.php                    |  2 +-
 plugins/UserCountry/API.php                   |  5 +-
 plugins/UserSettings/API.php                  |  4 +-
 18 files changed, 67 insertions(+), 80 deletions(-)
 delete mode 100644 core/DataTable/Filter/AddConstantMetadata.php

diff --git a/core/API/DataTableManipulator.php b/core/API/DataTableManipulator.php
index cfaa49e63d..774863f539 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 c21fcf7dd5..ab6cd5e568 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 a885bfefde..eb6199c9bf 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 792ccbf6f5..cc367d98cd 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 8c16313d75..1303519fdc 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 cf2f5b4429..60f9083950 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 f3682f5b10..0000000000
--- 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 501d5fbcb3..d54edd472d 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 d0dec41026..f016cec912 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 667ea97a78..0fab067dc0 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 104744b88b..ecfb8ef82f 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 389666b4e3..4888f1639c 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 84ceca80ac..5e15d1b770 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 e9e298ceb9..8335553c1e 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 410af4d26f..5ad1bc28dc 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 ce0b600f6b..f97f71c744 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 79ac0cb6ec..0c70cbc8ba 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 d01b50793f..ddd7edb231 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)
-- 
GitLab