Filter
3 years ago
Renderer
3 years ago
Row
5 years ago
BaseFilter.php
5 years ago
Bridges.php
5 years ago
DataTableInterface.php
4 years ago
Manager.php
4 years ago
Map.php
3 years ago
Renderer.php
4 years ago
Row.php
4 years ago
Simple.php
5 years ago
TableNotFoundException.php
5 years ago
Manager.php
165 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | namespace Piwik\DataTable; |
| 11 | |
| 12 | use Exception; |
| 13 | use Piwik\Common; |
| 14 | use Piwik\DataTable; |
| 15 | |
| 16 | /** |
| 17 | * The DataTable_Manager registers all the instantiated DataTable and provides an |
| 18 | * easy way to access them. This is used to store all the DataTable during the archiving process. |
| 19 | * At the end of archiving, the ArchiveProcessor will read the stored datatable and record them in the DB. |
| 20 | */ |
| 21 | class Manager extends \ArrayObject |
| 22 | { |
| 23 | /** |
| 24 | * Id of the next inserted table id in the Manager |
| 25 | * @var int |
| 26 | */ |
| 27 | protected $nextTableId = 0; |
| 28 | |
| 29 | private static $instance; |
| 30 | |
| 31 | public static function getInstance() |
| 32 | { |
| 33 | if (!isset(self::$instance)) { |
| 34 | self::$instance = new Manager(); |
| 35 | } |
| 36 | |
| 37 | return self::$instance; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Add a DataTable to the registry |
| 42 | * |
| 43 | * @param DataTable $table |
| 44 | * @return int Index of the table in the manager array |
| 45 | */ |
| 46 | public function addTable($table) |
| 47 | { |
| 48 | $this->nextTableId++; |
| 49 | $this[$this->nextTableId] = $table; |
| 50 | return $this->nextTableId; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Returns the DataTable associated to the ID $idTable. |
| 55 | * NB: The datatable has to have been instantiated before! |
| 56 | * This method will not fetch the DataTable from the DB. |
| 57 | * |
| 58 | * @param int $idTable |
| 59 | * @throws Exception If the table can't be found |
| 60 | * @return DataTable The table |
| 61 | */ |
| 62 | public function getTable($idTable) |
| 63 | { |
| 64 | if (!isset($this[$idTable])) { |
| 65 | 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)); |
| 66 | } |
| 67 | |
| 68 | return $this[$idTable]; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns the latest used table ID |
| 73 | * |
| 74 | * @return int |
| 75 | */ |
| 76 | public function getMostRecentTableId() |
| 77 | { |
| 78 | return $this->nextTableId; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Delete all the registered DataTables from the manager |
| 83 | * |
| 84 | * @param int $deleteWhenIdTableGreaterThan if supplied, only deletes tables whose id is greater than this value. |
| 85 | * @param null|int $deleteUntil if not null, only deletes tables that are <= this value. |
| 86 | */ |
| 87 | public function deleteAll($deleteWhenIdTableGreaterThan = 0, $deleteUntil = null) |
| 88 | { |
| 89 | foreach ($this as $id => $table) { |
| 90 | if ($id > $deleteWhenIdTableGreaterThan |
| 91 | && ($deleteUntil === null || $id <= $deleteUntil) |
| 92 | ) { |
| 93 | $this->deleteTable($id); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | if ($deleteWhenIdTableGreaterThan == 0) { |
| 98 | $this->exchangeArray(array()); |
| 99 | $this->nextTableId = 0; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Deletes (unsets) the datatable given its id and removes it from the manager |
| 105 | * Subsequent get for this table will fail |
| 106 | * |
| 107 | * @param int $id |
| 108 | */ |
| 109 | public function deleteTable($id) |
| 110 | { |
| 111 | if (isset($this[$id])) { |
| 112 | Common::destroy($this[$id]); |
| 113 | $this->setTableDeleted($id); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Deletes all tables starting from the $firstTableId to the most recent table id except the ones that are |
| 119 | * supposed to be ignored. |
| 120 | * |
| 121 | * @param int[] $idsToBeIgnored |
| 122 | * @param int $firstTableId |
| 123 | */ |
| 124 | public function deleteTablesExceptIgnored($idsToBeIgnored, $firstTableId = 0) |
| 125 | { |
| 126 | $lastTableId = $this->getMostRecentTableId(); |
| 127 | |
| 128 | for ($index = $firstTableId; $index <= $lastTableId; $index++) { |
| 129 | if (!in_array($index, $idsToBeIgnored)) { |
| 130 | $this->deleteTable($index); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Remove the table from the manager (table has already been unset) |
| 137 | * |
| 138 | * @param int $id |
| 139 | */ |
| 140 | public function setTableDeleted($id) |
| 141 | { |
| 142 | $this[$id] = null; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Debug only. Dumps all tables currently registered in the Manager |
| 147 | */ |
| 148 | public function dumpAllTables() |
| 149 | { |
| 150 | echo "<hr />Manager->dumpAllTables()<br />"; |
| 151 | foreach ($this as $id => $table) { |
| 152 | if (!($table instanceof DataTable)) { |
| 153 | echo "Error table $id is not instance of datatable<br />"; |
| 154 | var_export($table); |
| 155 | } else { |
| 156 | echo "<hr />"; |
| 157 | echo "Table (index=$id) TableId = " . $table->getId() . "<br />"; |
| 158 | echo $table; |
| 159 | echo "<br />"; |
| 160 | } |
| 161 | } |
| 162 | echo "<br />-- End Manager->dumpAllTables()<hr />"; |
| 163 | } |
| 164 | } |
| 165 |