Filter
6 years ago
Renderer
6 years ago
Row
6 years ago
BaseFilter.php
6 years ago
Bridges.php
6 years ago
DataTableInterface.php
6 years ago
Manager.php
6 years ago
Map.php
6 years ago
Renderer.php
6 years ago
Row.php
6 years ago
Simple.php
6 years ago
TableNotFoundException.php
6 years ago
Manager.php
160 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - 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 instanciated 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 instanciated 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 | public function deleteAll($deleteWhenIdTableGreaterThan = 0) |
| 85 | { |
| 86 | foreach ($this as $id => $table) { |
| 87 | if ($id > $deleteWhenIdTableGreaterThan) { |
| 88 | $this->deleteTable($id); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if ($deleteWhenIdTableGreaterThan == 0) { |
| 93 | $this->exchangeArray(array()); |
| 94 | $this->nextTableId = 0; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Deletes (unsets) the datatable given its id and removes it from the manager |
| 100 | * Subsequent get for this table will fail |
| 101 | * |
| 102 | * @param int $id |
| 103 | */ |
| 104 | public function deleteTable($id) |
| 105 | { |
| 106 | if (isset($this[$id])) { |
| 107 | Common::destroy($this[$id]); |
| 108 | $this->setTableDeleted($id); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Deletes all tables starting from the $firstTableId to the most recent table id except the ones that are |
| 114 | * supposed to be ignored. |
| 115 | * |
| 116 | * @param int[] $idsToBeIgnored |
| 117 | * @param int $firstTableId |
| 118 | */ |
| 119 | public function deleteTablesExceptIgnored($idsToBeIgnored, $firstTableId = 0) |
| 120 | { |
| 121 | $lastTableId = $this->getMostRecentTableId(); |
| 122 | |
| 123 | for ($index = $firstTableId; $index <= $lastTableId; $index++) { |
| 124 | if (!in_array($index, $idsToBeIgnored)) { |
| 125 | $this->deleteTable($index); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Remove the table from the manager (table has already been unset) |
| 132 | * |
| 133 | * @param int $id |
| 134 | */ |
| 135 | public function setTableDeleted($id) |
| 136 | { |
| 137 | $this[$id] = null; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Debug only. Dumps all tables currently registered in the Manager |
| 142 | */ |
| 143 | public function dumpAllTables() |
| 144 | { |
| 145 | echo "<hr />Manager->dumpAllTables()<br />"; |
| 146 | foreach ($this as $id => $table) { |
| 147 | if (!($table instanceof DataTable)) { |
| 148 | echo "Error table $id is not instance of datatable<br />"; |
| 149 | var_export($table); |
| 150 | } else { |
| 151 | echo "<hr />"; |
| 152 | echo "Table (index=$id) TableId = " . $table->getId() . "<br />"; |
| 153 | echo $table; |
| 154 | echo "<br />"; |
| 155 | } |
| 156 | } |
| 157 | echo "<br />-- End Manager->dumpAllTables()<hr />"; |
| 158 | } |
| 159 | } |
| 160 |