ActionDimension.php
1 month ago
ConversionDimension.php
1 month ago
DimensionMetadataProvider.php
1 year ago
VisitDimension.php
3 months ago
DimensionMetadataProvider.php
94 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\Plugin\Dimension; |
| 10 | |
| 11 | use Piwik\Piwik; |
| 12 | /** |
| 13 | * Provides metadata about dimensions for the LogDataPurger class. |
| 14 | */ |
| 15 | class DimensionMetadataProvider |
| 16 | { |
| 17 | /** |
| 18 | * Overrids for the result of the getActionReferenceColumnsByTable() method. Exists so Piwik |
| 19 | * instances can be monkey patched, in case there are idaction columns that this class does not |
| 20 | * naturally discover. |
| 21 | * |
| 22 | * @var array |
| 23 | */ |
| 24 | private $actionReferenceColumnsOverride; |
| 25 | public function __construct(array $actionReferenceColumnsOverride = array()) |
| 26 | { |
| 27 | $this->actionReferenceColumnsOverride = $actionReferenceColumnsOverride; |
| 28 | } |
| 29 | /** |
| 30 | * Returns a list of idaction column names organized by table name. Uses dimension metadata |
| 31 | * to find idaction columns dynamically. |
| 32 | * |
| 33 | * Note: It is not currently possible to use the Piwik platform to add idaction columns to tables |
| 34 | * other than log_link_visit_action (w/o doing something unsupported), so idaction columns in |
| 35 | * other tables are hard coded. |
| 36 | * |
| 37 | * @return array[] |
| 38 | */ |
| 39 | public function getActionReferenceColumnsByTable() |
| 40 | { |
| 41 | $result = array('log_link_visit_action' => array('idaction_url', 'idaction_url_ref', 'idaction_name_ref'), 'log_conversion' => array('idaction_url'), 'log_visit' => array('visit_exit_idaction_url', 'visit_exit_idaction_name', 'visit_entry_idaction_url', 'visit_entry_idaction_name'), 'log_conversion_item' => array('idaction_sku', 'idaction_name', 'idaction_category', 'idaction_category2', 'idaction_category3', 'idaction_category4', 'idaction_category5')); |
| 42 | $dimensionIdActionColumns = $this->getVisitActionTableActionReferences(); |
| 43 | $result['log_link_visit_action'] = array_unique(array_merge($result['log_link_visit_action'], $dimensionIdActionColumns)); |
| 44 | foreach ($this->actionReferenceColumnsOverride as $table => $columns) { |
| 45 | if (empty($result[$table])) { |
| 46 | $result[$table] = $columns; |
| 47 | } else { |
| 48 | $result[$table] = array_unique(array_merge($result[$table], $columns)); |
| 49 | } |
| 50 | } |
| 51 | /** |
| 52 | * Triggered when detecting which log_action entries to keep. Any log tables that use the log_action |
| 53 | * table to reference text via an ID should add their table info so no actions that are still in use |
| 54 | * will be accidentally deleted. |
| 55 | * |
| 56 | * **Example** |
| 57 | * |
| 58 | * Piwik::addAction('Db.getActionReferenceColumnsByTable', function(&$result) { |
| 59 | * $tableNameUnprefixed = 'log_example'; |
| 60 | * $columnNameThatReferencesIdActionInLogActionTable = 'idaction_example'; |
| 61 | * $result[$tableNameUnprefixed] = array($columnNameThatReferencesIdActionInLogActionTable); |
| 62 | * }); |
| 63 | * @param array $result |
| 64 | */ |
| 65 | Piwik::postEvent('Db.getActionReferenceColumnsByTable', array(&$result)); |
| 66 | return $result; |
| 67 | } |
| 68 | private function getVisitActionTableActionReferences() |
| 69 | { |
| 70 | $idactionColumns = array(); |
| 71 | foreach (\Piwik\Plugin\Dimension\ActionDimension::getAllDimensions() as $actionDimension) { |
| 72 | if ($this->isActionReference($actionDimension)) { |
| 73 | $idactionColumns[] = $actionDimension->getColumnName(); |
| 74 | } |
| 75 | } |
| 76 | return $idactionColumns; |
| 77 | } |
| 78 | /** |
| 79 | * Returns `true` if the column for this dimension is a reference to the `log_action` table (ie, an "idaction column"), |
| 80 | * `false` if otherwise. |
| 81 | * |
| 82 | * @return bool |
| 83 | */ |
| 84 | private function isActionReference(\Piwik\Plugin\Dimension\ActionDimension $dimension) |
| 85 | { |
| 86 | try { |
| 87 | $dimension->getActionId(); |
| 88 | return \true; |
| 89 | } catch (\Exception $ex) { |
| 90 | return \false; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 |