You are here

function piwik_update_8200 in Piwik Web Analytics 8

Install Matomo module and migrate all settings.

File

./piwik.install, line 59
Installation file for Piwik - Web analytics module.

Code

function piwik_update_8200() {

  // Make the update hook failing so it can re-run later again.
  if (!module_load_include('module', 'matomo', 'matomo')) {
    throw new DrupalUpdateException(t('Matomo module is missing. Download it and re-run update.php'));
  }
  $module_enabled = \Drupal::service('module_installer')
    ->install(array(
    'matomo',
  ));
  if ($module_enabled) {
    $messages[] = t('Successfully installed matomo module.');
    $piwik_config = \Drupal::configFactory()
      ->getEditable('piwik.settings');
    $matomo_config = \Drupal::configFactory()
      ->getEditable('matomo.settings');
    $matomo_config
      ->set('site_id', $piwik_config
      ->get('site_id'))
      ->set('url_http', $piwik_config
      ->get('url_http'))
      ->set('url_https', $piwik_config
      ->get('url_https'))
      ->set('codesnippet.before', $piwik_config
      ->get('codesnippet.before'))
      ->set('codesnippet.after', $piwik_config
      ->get('codesnippet.after'))
      ->set('custom.variable', $piwik_config
      ->get('custom.variable'))
      ->set('domain_mode', $piwik_config
      ->get('domain_mode'))
      ->set('track.files', $piwik_config
      ->get('track.files'))
      ->set('track.files_extensions', $piwik_config
      ->get('track.files_extensions'))
      ->set('track.colorbox', $piwik_config
      ->get('track.colorbox'))
      ->set('track.userid', $piwik_config
      ->get('track.userid'))
      ->set('track.mailto', $piwik_config
      ->get('track.mailto'))
      ->set('track.messages', $piwik_config
      ->get('track.messages'))
      ->set('track.site_search', $piwik_config
      ->get('track.site_search'))
      ->set('translation_set', $piwik_config
      ->get('translation_set'))
      ->set('privacy.donottrack', $piwik_config
      ->get('privacy.donottrack'))
      ->set('cache', $piwik_config
      ->get('cache'))
      ->set('visibility.request_path_mode', $piwik_config
      ->get('visibility.request_path_mode'))
      ->set('visibility.request_path_pages', $piwik_config
      ->get('visibility.request_path_pages'))
      ->set('visibility.user_account_mode', $piwik_config
      ->get('visibility.user_account_mode'))
      ->set('visibility.user_role_mode', $piwik_config
      ->get('visibility.user_role_mode'))
      ->set('visibility.user_role_roles', $piwik_config
      ->get('visibility.user_role_roles'))
      ->save();
    $messages[] = t('Copied settings from piwik to matomo module.');

    // Migrate piwik permissions to matomo.
    $permissions = array(
      'administer piwik' => 'administer matomo',
      'opt-in or out of piwik tracking' => 'opt-in or out of matomo tracking',
      'use PHP for piwik tracking visibility' => 'use php for matomo tracking visibility',
      'add JS snippets for piwik' => 'add js snippets for matomo',
    );
    foreach (user_roles(FALSE) as $rid => $name) {
      $role = \Drupal\user\Entity\Role::load($rid);
      foreach ($permissions as $permission_old_key => $permission_new_key) {
        if ($role
          ->hasPermission($permission_old_key)) {
          $role
            ->grantPermission($permission_new_key);
          $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,
          ));
        }
      }
      $role
        ->save();
    }
    $messages[] = t('Copied piwik permissions to matomo module.');

    // Uninstall piwik module.
    $module_uninstalled = \Drupal::service('module_installer')
      ->uninstall(array(
      'piwik',
    ), FALSE);
    if ($module_uninstalled) {
      $messages[] = t('Successfully uninstalled piwik module. Please remove piwik from your system.');
      drupal_set_message('Piwik is now Matomo! Matomo has been installed. Please remove Piwik module from your system.', 'warning');
    }
    else {
      $messages[] = t('FAILED to uninstall piwik module.');

      // Unconfigure piwik to remove piwik tracking code; if piwik module may failed to disable.
      $piwik_config
        ->set('site_id', '');
      $messages[] = t('Unconfigured site_id in piwik module to disable double tracking.');
    }
  }
  return empty($messages) ? t('FAILED to migrate piwik to matomo module. Please uninstall piwik module and install matomo module manually!') : implode(' ', $messages);
}