piwik.install in Piwik Web Analytics 8
Same filename and directory in other branches
Installation file for Piwik - Web analytics module.
File
piwik.installView source
<?php
/**
* @file
* Installation file for Piwik - Web analytics module.
*/
use Drupal\Core\Url;
/**
* Implements hook_uninstall().
*
* Remove cache directory if module is uninstalled.
*/
function piwik_uninstall() {
piwik_clear_js_cache();
}
/**
* Implements hook_requirements().
*/
function piwik_requirements($phase) {
$requirements = [];
switch ($phase) {
case 'install':
if (\Drupal::moduleHandler()
->moduleExists('matomo')) {
// https://matomo.org/blog/2018/01/piwik-is-now-matomo/
$requirements['piwik_matomo_is_installed'] = array(
'title' => t('Piwik is now Matomo!'),
'value' => t('Fails'),
'severity' => REQUIREMENT_ERROR,
'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.'),
);
}
break;
case 'runtime':
$config = \Drupal::config('piwik.settings');
// Raise warning if Piwik user account has not been set yet.
if (!preg_match('/^\\d{1,}$/', $config
->get('site_id'))) {
$requirements['piwik_site_id'] = [
'title' => t('Piwik module'),
'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(),
]),
'severity' => REQUIREMENT_WARNING,
'value' => t('Not configured'),
];
}
break;
}
return $requirements;
}
/**
* Install Matomo module and migrate all settings.
*/
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);
}
Functions
Name | Description |
---|---|
piwik_requirements | Implements hook_requirements(). |
piwik_uninstall | Implements hook_uninstall(). |
piwik_update_8200 | Install Matomo module and migrate all settings. |