ActionDimension.php
6 years ago
ConversionDimension.php
6 years ago
DimensionMetadataProvider.php
6 years ago
VisitDimension.php
6 years ago
DimensionMetadataProvider.php
125 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 | namespace Piwik\Plugin\Dimension; |
| 10 | use Piwik\Piwik; |
| 11 | |
| 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 | |
| 26 | public function __construct(array $actionReferenceColumnsOverride = array()) |
| 27 | { |
| 28 | $this->actionReferenceColumnsOverride = $actionReferenceColumnsOverride; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Returns a list of idaction column names organized by table name. Uses dimension metadata |
| 33 | * to find idaction columns dynamically. |
| 34 | * |
| 35 | * Note: It is not currently possible to use the Piwik platform to add idaction columns to tables |
| 36 | * other than log_link_visit_action (w/o doing something unsupported), so idaction columns in |
| 37 | * other tables are hard coded. |
| 38 | * |
| 39 | * @return array[] |
| 40 | */ |
| 41 | public function getActionReferenceColumnsByTable() |
| 42 | { |
| 43 | $result = array( |
| 44 | 'log_link_visit_action' => array('idaction_url', |
| 45 | 'idaction_url_ref', |
| 46 | 'idaction_name_ref' |
| 47 | ), |
| 48 | |
| 49 | 'log_conversion' => array('idaction_url'), |
| 50 | |
| 51 | 'log_visit' => array('visit_exit_idaction_url', |
| 52 | 'visit_exit_idaction_name', |
| 53 | 'visit_entry_idaction_url', |
| 54 | 'visit_entry_idaction_name'), |
| 55 | |
| 56 | 'log_conversion_item' => array('idaction_sku', |
| 57 | 'idaction_name', |
| 58 | 'idaction_category', |
| 59 | 'idaction_category2', |
| 60 | 'idaction_category3', |
| 61 | 'idaction_category4', |
| 62 | 'idaction_category5') |
| 63 | ); |
| 64 | |
| 65 | $dimensionIdActionColumns = $this->getVisitActionTableActionReferences(); |
| 66 | $result['log_link_visit_action'] = array_unique( |
| 67 | array_merge($result['log_link_visit_action'], $dimensionIdActionColumns)); |
| 68 | |
| 69 | foreach ($this->actionReferenceColumnsOverride as $table => $columns) { |
| 70 | if (empty($result[$table])) { |
| 71 | $result[$table] = $columns; |
| 72 | } else { |
| 73 | $result[$table] = array_unique(array_merge($result[$table], $columns)); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Triggered when detecting which log_action entries to keep. Any log tables that use the log_action |
| 79 | * table to reference text via an ID should add their table info so no actions that are still in use |
| 80 | * will be accidentally deleted. |
| 81 | * |
| 82 | * **Example** |
| 83 | * |
| 84 | * Piwik::addAction('Db.getActionReferenceColumnsByTable', function(&$result) { |
| 85 | * $tableNameUnprefixed = 'log_example'; |
| 86 | * $columnNameThatReferencesIdActionInLogActionTable = 'idaction_example'; |
| 87 | * $result[$tableNameUnprefixed] = array($columnNameThatReferencesIdActionInLogActionTable); |
| 88 | * }); |
| 89 | * @param array $result |
| 90 | */ |
| 91 | Piwik::postEvent('Db.getActionReferenceColumnsByTable', array(&$result)); |
| 92 | |
| 93 | return $result; |
| 94 | } |
| 95 | |
| 96 | private function getVisitActionTableActionReferences() |
| 97 | { |
| 98 | $idactionColumns = array(); |
| 99 | foreach (ActionDimension::getAllDimensions() as $actionDimension) { |
| 100 | if ($this->isActionReference($actionDimension)) { |
| 101 | $idactionColumns[] = $actionDimension->getColumnName(); |
| 102 | } |
| 103 | } |
| 104 | return $idactionColumns; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | /** |
| 109 | * Returns `true` if the column for this dimension is a reference to the `log_action` table (ie, an "idaction column"), |
| 110 | * `false` if otherwise. |
| 111 | * |
| 112 | * @return bool |
| 113 | */ |
| 114 | private function isActionReference(ActionDimension $dimension) |
| 115 | { |
| 116 | try { |
| 117 | $dimension->getActionId(); |
| 118 | |
| 119 | return true; |
| 120 | } catch (\Exception $ex) { |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 |