Skip to content

Commit

Permalink
reuse $row->getSubtable() as much as possible instead of callng Manag…
Browse files Browse the repository at this point in the history
…er::getInstance()->getTable which can throw exception refs matomo-org#3414
  • Loading branch information
mattab committed Dec 10, 2014
1 parent dab1ee7 commit c28f9d6
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 51 deletions.
43 changes: 20 additions & 23 deletions core/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,11 @@ public function sort($functionCallback, $columnSortedBy)

if ($this->enableRecursiveSort === true) {
foreach ($this->getRows() as $row) {
if (($idSubtable = $row->getIdSubDataTable()) !== null) {
$table = Manager::getInstance()->getTable($idSubtable);
$table->enableRecursiveSort();
$table->sort($functionCallback, $columnSortedBy);

$subTable = $row->getSubtable();
if ($subTable) {
$subTable->enableRecursiveSort();
$subTable->sort($functionCallback, $columnSortedBy);
}
}
}
Expand Down Expand Up @@ -868,8 +869,8 @@ public function getRowsCountRecursive()
{
$totalCount = 0;
foreach ($this->rows as $row) {
if (($idSubTable = $row->getIdSubDataTable()) !== null) {
$subTable = Manager::getInstance()->getTable($idSubTable);
$subTable = $row->getSubtable();
if ($subTable) {
$count = $subTable->getRowsCountRecursive();
$totalCount += $count;
}
Expand Down Expand Up @@ -907,8 +908,9 @@ public function renameColumn($oldName, $newName, $doRenameColumnsOfSubTables = t
$row->renameColumn($oldName, $newName);

if ($doRenameColumnsOfSubTables) {
if (($idSubDataTable = $row->getIdSubDataTable()) !== null) {
Manager::getInstance()->getTable($idSubDataTable)->renameColumn($oldName, $newName);
$subTable = $row->getSubtable();
if ($subTable) {
$subTable->renameColumn($oldName, $newName);
}
}
}
Expand All @@ -929,8 +931,9 @@ public function deleteColumns($names, $deleteRecursiveInSubtables = false)
foreach ($names as $name) {
$row->deleteColumn($name);
}
if (($idSubDataTable = $row->getIdSubDataTable()) !== null) {
Manager::getInstance()->getTable($idSubDataTable)->deleteColumns($names, $deleteRecursiveInSubtables);
$subTable = $row->getSubtable();
if ($subTable) {
$subTable->deleteColumns($names, $deleteRecursiveInSubtables);
}
}
if (!is_null($this->summaryRow)) {
Expand Down Expand Up @@ -1110,17 +1113,11 @@ public function getSerialized($maximumRowsInDataTable = null,
// but returns all serialized tables and subtable in an array of 1 dimension
$aSerializedDataTable = array();
foreach ($this->rows as $row) {
if (($idSubTable = $row->getIdSubDataTable()) !== null) {
$subTable = null;
try {
$subTable = Manager::getInstance()->getTable($idSubTable);
} catch(TableNotFoundException $e) {
// This occurs is an unknown & random data issue. Catch Exception and remove subtable from the row.
$row->removeSubtable();
// Go to next row
continue;
}

$subTable = $row->getSubtable();
if (!$subTable) {
// Not sure if this code is needed
$row->removeSubtable();
} else {
$depth++;
$aSerializedDataTable = $aSerializedDataTable + $subTable->getSerialized($maximumRowsInSubDataTable, $maximumRowsInSubDataTable, $columnToSortByBeforeTruncation);
$depth--;
Expand Down Expand Up @@ -1616,8 +1613,8 @@ protected function aggregateRowWithLabel(Row $row, $doAggregateSubTables = true)
// we simply add it (cloning the subtable)
// if the row has the subtable already
// then we have to recursively sum the subtables
if (($idSubTable = $row->getIdSubDataTable()) !== null) {
$subTable = Manager::getInstance()->getTable($idSubTable);
$subTable = $row->getSubtable();
if ($subTable) {
$subTable->metadata[self::COLUMN_AGGREGATION_OPS_METADATA_NAME]
= $this->getMetadata(self::COLUMN_AGGREGATION_OPS_METADATA_NAME);
$rowFound->sumSubtable($subTable);
Expand Down
4 changes: 2 additions & 2 deletions core/DataTable/BaseFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public function filterSubTable(Row $row)
if (!$this->enableRecursive) {
return;
}
if ($row->isSubtableLoaded()) {
$subTable = Manager::getInstance()->getTable($row->getIdSubDataTable());
$subTable = $row->getSubtable();
if ($subTable) {
$this->filter($subTable);
}
}
Expand Down
11 changes: 4 additions & 7 deletions core/DataTable/Filter/PatternRecursive.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,15 @@ public function filter($table)
// AND 2 - the label is not found in the children
$patternNotFoundInChildren = false;

try {
$idSubTable = $row->getIdSubDataTable();
$subTable = Manager::getInstance()->getTable($idSubTable);

$subTable = $row->getSubtable();
if(!$subTable) {
$patternNotFoundInChildren = true;
} else {
// we delete the row if we couldn't find the pattern in any row in the
// children hierarchy
if ($this->filter($subTable) == 0) {
$patternNotFoundInChildren = true;
}
} catch (Exception $e) {
// there is no subtable loaded for example
$patternNotFoundInChildren = true;
}

if ($patternNotFoundInChildren
Expand Down
4 changes: 2 additions & 2 deletions core/DataTable/Filter/ReplaceSummaryRowLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public function filter($table)

// recurse
foreach ($rows as $row) {
if ($row->isSubtableLoaded()) {
$subTable = Manager::getInstance()->getTable($row->getIdSubDataTable());
$subTable = $row->getSubtable();
if ($subTable) {
$this->filter($subTable);
}
}
Expand Down
10 changes: 3 additions & 7 deletions core/DataTable/Renderer/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,10 @@ protected function renderTable($table, $prefix = "")
. $row->getIdSubDataTable() . "]<br />\n";

if (!is_null($row->getIdSubDataTable())) {
if ($row->isSubtableLoaded()) {
$subTable = $row->getSubtable();
if ($subTable) {
$depth++;
$output .= $this->renderTable(
Manager::getInstance()->getTable(
$row->getIdSubDataTable()
),
$prefix . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
);
$output .= $this->renderTable($subTable, $prefix . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
$depth--;
} else {
$output .= "-- Sub DataTable not loaded<br />\n";
Expand Down
4 changes: 2 additions & 2 deletions core/DataTable/Renderer/Php.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ protected function renderTable($table)
$newRow['issummaryrow'] = true;
}

$subTable = $row->getSubtable();
if ($this->isRenderSubtables()
&& $row->isSubtableLoaded()
&& $subTable
) {
$subTable = $this->renderTable(Manager::getInstance()->getTable($row->getIdSubDataTable()));
$newRow['subtable'] = $subTable;
if ($this->hideIdSubDatatable === false
&& isset($newRow['metadata']['idsubdatatable_in_db'])
Expand Down
5 changes: 2 additions & 3 deletions core/DataTable/Row/DataTableSummaryRow.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ public function __construct($subTable = null)
*/
public function recalculate()
{
$id = $this->getIdSubDataTable();
if ($id !== null) {
$subTable = Manager::getInstance()->getTable($id);
$subTable = $this->getSubtable();
if ($subTable) {
$this->sumTable($subTable);
}
}
Expand Down
6 changes: 3 additions & 3 deletions plugins/Actions/ArchivingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ public static function deleteInvalidSummedColumnsFromDataTable($dataTable)
if (($idSubtable = $row->getIdSubDataTable()) !== null
|| $id === DataTable::ID_SUMMARY_ROW
) {
if ($idSubtable !== null) {
$subtable = Manager::getInstance()->getTable($idSubtable);
self::deleteInvalidSummedColumnsFromDataTable($subtable);
$subTable = $row->getSubtable();
if ($subTable) {
self::deleteInvalidSummedColumnsFromDataTable($subTable);
}

if ($row instanceof DataTableSummaryRow) {
Expand Down
4 changes: 2 additions & 2 deletions plugins/Transitions/API.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,8 @@ private function addExternalReferrers($logAggregator, &$report, $idaction, $acti
if ($visits) {
// load details (i.e. subtables)
$details = array();
if ($idSubTable = $row->getIdSubDataTable()) {
$subTable = Manager::getInstance()->getTable($idSubTable);
$subTable = $row->getSubtable();
if ($subTable) {
foreach ($subTable->getRows() as $subRow) {
$details[] = array(
'label' => $subRow->getColumn('label'),
Expand Down

0 comments on commit c28f9d6

Please sign in to comment.