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 157 158 159 160 161 162 163 164 165
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\DataTable;
use Exception;
use Piwik\Common;
use Piwik\DataTable;
/**
* The DataTable_Manager registers all the instantiated DataTable and provides an
* easy way to access them. This is used to store all the DataTable during the archiving process.
* At the end of archiving, the ArchiveProcessor will read the stored datatable and record them in the DB.
*/
class Manager extends \ArrayObject
{
/**
* Id of the next inserted table id in the Manager
* @var int
*/
protected $nextTableId = 0;
private static $instance;
public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new Manager();
}
return self::$instance;
}
/**
* Add a DataTable to the registry
*
* @param DataTable $table
* @return int Index of the table in the manager array
*/
public function addTable($table)
{
$this->nextTableId++;
$this[$this->nextTableId] = $table;
return $this->nextTableId;
}
/**
* Returns the DataTable associated to the ID $idTable.
* NB: The datatable has to have been instantiated before!
* This method will not fetch the DataTable from the DB.
*
* @param int $idTable
* @throws Exception If the table can't be found
* @return DataTable The table
*/
public function getTable($idTable)
{
if (!isset($this[$idTable])) {
throw new TableNotFoundException(sprintf("Error: table id %s not found in memory. (If this error is causing you problems in production, please report it in Matomo issue tracker.)", $idTable));
}
return $this[$idTable];
}
/**
* Returns the latest used table ID
*
* @return int
*/
public function getMostRecentTableId()
{
return $this->nextTableId;
}
/**
* Delete all the registered DataTables from the manager
*
* @param int $deleteWhenIdTableGreaterThan if supplied, only deletes tables whose id is greater than this value.
* @param null|int $deleteUntil if not null, only deletes tables that are <= this value.
*/
public function deleteAll($deleteWhenIdTableGreaterThan = 0, $deleteUntil = null)
{
foreach ($this as $id => $table) {
if (
$id > $deleteWhenIdTableGreaterThan
&& ($deleteUntil === null || $id <= $deleteUntil)
) {
$this->deleteTable($id);
}
}
if ($deleteWhenIdTableGreaterThan == 0) {
$this->exchangeArray(array());
$this->nextTableId = 0;
}
}
/**
* Deletes (unsets) the datatable given its id and removes it from the manager
* Subsequent get for this table will fail
*
* @param int $id
*/
public function deleteTable($id)
{
if (isset($this[$id])) {
Common::destroy($this[$id]);
$this->setTableDeleted($id);
}
}
/**
* Deletes all tables starting from the $firstTableId to the most recent table id except the ones that are
* supposed to be ignored.
*
* @param int[] $idsToBeIgnored
* @param int $firstTableId
*/
public function deleteTablesExceptIgnored($idsToBeIgnored, $firstTableId = 0)
{
$lastTableId = $this->getMostRecentTableId();
for ($index = $firstTableId; $index <= $lastTableId; $index++) {
if (!in_array($index, $idsToBeIgnored)) {
$this->deleteTable($index);
}
}
}
/**
* Remove the table from the manager (table has already been unset)
*
* @param int $id
*/
public function setTableDeleted($id)
{
$this[$id] = null;
}
/**
* Debug only. Dumps all tables currently registered in the Manager
*/
public function dumpAllTables()
{
echo "<hr />Manager->dumpAllTables()<br />";
foreach ($this as $id => $table) {
if (!($table instanceof DataTable)) {
echo "Error table $id is not instance of datatable<br />";
var_export($table);
} else {
echo "<hr />";
echo "Table (index=$id) TableId = " . $table->getId() . "<br />";
echo $table;
echo "<br />";
}
}
echo "<br />-- End Manager->dumpAllTables()<hr />";
}
}
|