piwik.install 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @file
  4. * Installation file for Piwik - Web analytics module.
  5. */
  6. use Drupal\Core\Url;
  7. /**
  8. * Implements hook_uninstall().
  9. *
  10. * Remove cache directory if module is uninstalled.
  11. */
  12. function piwik_uninstall() {
  13. piwik_clear_js_cache();
  14. }
  15. /**
  16. * Implements hook_requirements().
  17. */
  18. function piwik_requirements($phase) {
  19. $requirements = [];
  20. switch ($phase) {
  21. case 'install':
  22. if (\Drupal::moduleHandler()->moduleExists('matomo')) {
  23. // https://matomo.org/blog/2018/01/piwik-is-now-matomo/
  24. $requirements['piwik_matomo_is_installed'] = array(
  25. 'title' => t('Piwik is now Matomo!'),
  26. 'value' => t('Fails'),
  27. 'severity' => REQUIREMENT_ERROR,
  28. 'description' => t('Piwik has been renamed to Matomo Analytics. Piwik module is blocked now and cannot re-enabled! Please remove piwik module from your system. It is no longer used.'),
  29. );
  30. }
  31. break;
  32. case 'runtime':
  33. $config = \Drupal::config('piwik.settings');
  34. // Raise warning if Piwik user account has not been set yet.
  35. if (!preg_match('/^\d{1,}$/', $config->get('site_id'))) {
  36. $requirements['piwik_site_id'] = [
  37. 'title' => t('Piwik module'),
  38. 'description' => t('Piwik module has not been configured yet. Please configure its settings from the <a href=":url">Piwik settings page</a>.', [':url' => Url::fromRoute('piwik.admin_settings_form')->toString()]),
  39. 'severity' => REQUIREMENT_WARNING,
  40. 'value' => t('Not configured'),
  41. ];
  42. }
  43. break;
  44. }
  45. return $requirements;
  46. }
  47. /**
  48. * Install Matomo module and migrate all settings.
  49. */
  50. function piwik_update_8200() {
  51. // Make the update hook failing so it can re-run later again.
  52. if (!module_load_include('module', 'matomo', 'matomo')) {
  53. throw new DrupalUpdateException(t('Matomo module is missing. Download it and re-run update.php'));
  54. }
  55. $module_enabled = \Drupal::service('module_installer')->install(array('matomo'));
  56. if ($module_enabled) {
  57. $messages[] = t('Successfully installed matomo module.');
  58. $piwik_config = \Drupal::configFactory()->getEditable('piwik.settings');
  59. $matomo_config = \Drupal::configFactory()->getEditable('matomo.settings');
  60. $matomo_config
  61. ->set('site_id', $piwik_config->get('site_id'))
  62. ->set('url_http', $piwik_config->get('url_http'))
  63. ->set('url_https', $piwik_config->get('url_https'))
  64. ->set('codesnippet.before', $piwik_config->get('codesnippet.before'))
  65. ->set('codesnippet.after', $piwik_config->get('codesnippet.after'))
  66. ->set('custom.variable', $piwik_config->get('custom.variable'))
  67. ->set('domain_mode', $piwik_config->get('domain_mode'))
  68. ->set('track.files', $piwik_config->get('track.files'))
  69. ->set('track.files_extensions', $piwik_config->get('track.files_extensions'))
  70. ->set('track.colorbox', $piwik_config->get('track.colorbox'))
  71. ->set('track.userid', $piwik_config->get('track.userid'))
  72. ->set('track.mailto', $piwik_config->get('track.mailto'))
  73. ->set('track.messages', $piwik_config->get('track.messages'))
  74. ->set('track.site_search', $piwik_config->get('track.site_search'))
  75. ->set('translation_set', $piwik_config->get('translation_set'))
  76. ->set('privacy.donottrack', $piwik_config->get('privacy.donottrack'))
  77. ->set('cache', $piwik_config->get('cache'))
  78. ->set('visibility.request_path_mode', $piwik_config->get('visibility.request_path_mode'))
  79. ->set('visibility.request_path_pages', $piwik_config->get('visibility.request_path_pages'))
  80. ->set('visibility.user_account_mode', $piwik_config->get('visibility.user_account_mode'))
  81. ->set('visibility.user_role_mode', $piwik_config->get('visibility.user_role_mode'))
  82. ->set('visibility.user_role_roles', $piwik_config->get('visibility.user_role_roles'))
  83. ->save();
  84. $messages[] = t('Copied settings from piwik to matomo module.');
  85. // Migrate piwik permissions to matomo.
  86. $permissions = array(
  87. 'administer piwik' => 'administer matomo',
  88. 'opt-in or out of piwik tracking' => 'opt-in or out of matomo tracking',
  89. 'use PHP for piwik tracking visibility' => 'use php for matomo tracking visibility',
  90. 'add JS snippets for piwik' => 'add js snippets for matomo',
  91. );
  92. foreach (user_roles(FALSE) as $rid => $name) {
  93. $role = \Drupal\user\Entity\Role::load($rid);
  94. foreach ($permissions as $permission_old_key => $permission_new_key) {
  95. if ($role->hasPermission($permission_old_key)) {
  96. $role->grantPermission($permission_new_key);
  97. $messages[] = t('Role "@rid" has permission "@permission_old_key" and "@permission_new_key" granted.', array('@rid' => $rid, '@permission_old_key' => $permission_old_key, '@permission_new_key' => $permission_new_key));
  98. }
  99. }
  100. $role->save();
  101. }
  102. $messages[] = t('Copied piwik permissions to matomo module.');
  103. // Uninstall piwik module.
  104. $module_uninstalled = \Drupal::service('module_installer')->uninstall(array('piwik'), FALSE);
  105. if ($module_uninstalled) {
  106. $messages[] = t('Successfully uninstalled piwik module. Please remove piwik from your system.');
  107. drupal_set_message('Piwik is now Matomo! Matomo has been installed. Please remove Piwik module from your system.', 'warning');
  108. }
  109. else {
  110. $messages[] = t('FAILED to uninstall piwik module.');
  111. // Unconfigure piwik to remove piwik tracking code; if piwik module may failed to disable.
  112. $piwik_config->set('site_id', '');
  113. $messages[] = t('Unconfigured site_id in piwik module to disable double tracking.');
  114. }
  115. }
  116. return empty($messages) ? t('FAILED to migrate piwik to matomo module. Please uninstall piwik module and install matomo module manually!') : implode(' ', $messages);
  117. }