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