Projet

Général

Profil

Paste
Télécharger (17,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / piwik / piwik.install @ 60283730

1
<?php
2

    
3
/**
4
 * @file
5
 * Installation file for Piwik - Web analytics module.
6
 */
7

    
8
function piwik_install() {
9
  // Remove tracking from all administrative pages, see http://drupal.org/node/34970.
10
  variable_set('piwik_visibility_roles', 0);
11
  variable_set('piwik_visibility_pages', 0);
12
  $pages = array(
13
    'admin',
14
    'admin/*',
15
    'batch',
16
    'node/add*',
17
    'node/*/*',
18
    'user/*/*',
19
  );
20
  variable_set('piwik_pages', implode("\n", $pages));
21
}
22

    
23
function piwik_uninstall() {
24
  variable_del('piwik_cache');
25
  variable_del('piwik_codesnippet_before');
26
  variable_del('piwik_codesnippet_after');
27
  variable_del('piwik_custom');
28
  variable_del('piwik_custom_var');
29
  variable_del('piwik_domain_mode');
30
  variable_del('piwik_js_scope');
31
  variable_del('piwik_last_cache');
32
  variable_del('piwik_page_title_hierarchy');
33
  variable_del('piwik_page_title_hierarchy_exclude_home');
34
  variable_del('piwik_pages');
35
  variable_del('piwik_privacy_donottrack');
36
  variable_del('piwik_roles');
37
  variable_del('piwik_site_id');
38
  variable_del('piwik_site_search');
39
  variable_del('piwik_trackcolorbox');
40
  variable_del('piwik_trackmailto');
41
  variable_del('piwik_track'); // interrims solution
42
  variable_del('piwik_trackfiles_extensions');
43
  variable_del('piwik_trackmessages');
44
  variable_del('piwik_trackuserid');
45
  variable_del('piwik_translation_set');
46
  variable_del('piwik_url_http');
47
  variable_del('piwik_url_https');
48
  variable_del('piwik_visibility_pages');
49
  variable_del('piwik_visibility_roles');
50

    
51
  // Remove backup variables if exits. Remove this code in D8.
52
  variable_del('piwik_codesnippet_before_backup_7202');
53
  variable_del('piwik_codesnippet_after_backup_7202');
54
}
55

    
56
/**
57
 * Remove cache directory if module is disabled (or uninstalled).
58
 */
59
function piwik_disable() {
60
  piwik_clear_js_cache();
61
}
62

    
63
/**
64
 * Implementation of hook_requirements().
65
 */
66
function piwik_requirements($phase) {
67
  $requirements = array();
68
  $t = get_t();
69

    
70
  switch ($phase) {
71
    case 'install':
72
      if (module_exists('matomo')) {
73
        // https://matomo.org/blog/2018/01/piwik-is-now-matomo/
74
        $requirements['piwik_matomo_is_installed'] = array(
75
          'title' => $t('Piwik is now Matomo!'),
76
          'value' => $t('Fails'),
77
          'severity' => REQUIREMENT_ERROR,
78
          '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.'),
79
        );
80
      }
81
      break;
82

    
83
    case 'runtime':
84
      // Module cannot validate piwik URL without external HTTP requests.
85
      if (variable_get('drupal_http_request_fails', TRUE) && !system_check_http_request()) {
86
        $requirements['piwik_http_requests'] = array(
87
          'title' => $t('HTTP request status'),
88
          'value' => $t('Fails'),
89
          'severity' => REQUIREMENT_ERROR,
90
          'description' => $t('Your system or network configuration does not allow Drupal to access web pages, resulting in reduced functionality. This could be due to your webserver configuration or PHP settings, and should be resolved in order to download information about available updates, fetch aggregator feeds, sign in via OpenID, or use other network-dependent services.'),
91
        );
92
      }
93

    
94
      // Raise warning if Piwik user account has not been set yet.
95
      if (!preg_match('/^\d{1,}$/', variable_get('piwik_site_id', ''))) {
96
        $requirements['piwik'] = array(
97
          'title' => $t('Piwik module'),
98
          'description' => $t('Piwik module has not been configured yet. Please configure its settings from the <a href="@url">Piwik settings page</a>.', array('@url' => url('admin/config/system/piwik'))),
99
          'severity' => REQUIREMENT_WARNING,
100
          'value' => $t('Not configured'),
101
        );
102
      }
103
      break;
104
  }
105
  return $requirements;
106
}
107

    
108

    
109
/**
110
 * Change visibility setting for paths.
111
 */
112
function piwik_update_6000() {
113
  // Orginal pages setting.
114
  $pages = array(
115
    'admin*',
116
    'user*',
117
    'node/add*',
118
    'node/*/*',
119
  );
120

    
121
  $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('piwik_pages', implode("\n", $pages))));
122
  if (empty($diff)) {
123
    // No diff to original settings found. Update with new settings.
124
    $pages = array(
125
      'admin',
126
      'admin/*',
127
      'user/*/*',
128
      'node/add*',
129
      'node/*/*',
130
    );
131
    variable_set('piwik_pages', implode("\n", $pages));
132
    return t('Path visibility filter setting changed from "admin*" to "admin, admin/*" and "user*" changed to "user/*/*".');
133
  }
134
  else {
135
    return t('Custom path visibility filter setting found. Update skipped!');
136
  }
137
}
138

    
139
/**
140
 * Upgrade custom javascript settings.
141
 */
142
function piwik_update_6001() {
143
  variable_set('piwik_codesnippet_before', variable_get('piwik_codesnippet', ''));
144
  variable_del('piwik_codesnippet');
145
  return t('Upgraded custom javascript codesnippet setting.');
146
}
147

    
148
/**
149
 * Remove obsolte token auth.
150
 */
151
function piwik_update_6002() {
152
  variable_del('piwik_auth');
153
  return t('The global token_auth setting has been deleted. Users need to configure their personal token_auth.');
154
}
155

    
156
/**
157
 * Update list of default extensions
158
 */
159
function piwik_update_6003() {
160
  if (variable_get('piwik_trackfiles_extensions', '') == '7z|aac|avi|csv|doc|exe|flv|gif|gz|jpe?g|js|mp(3|4|e?g)|mov|pdf|phps|png|ppt|rar|sit|tar|torrent|txt|wma|wmv|xls|xml|zip') {
161
    variable_set('piwik_trackfiles_extensions', '7z|aac|arc|arj|asf|asx|avi|bin|csv|doc|exe|flv|gif|gz|gzip|hqx|jar|jpe?g|js|mp(2|3|4|e?g)|mov(ie)?|msi|msp|pdf|phps|png|ppt|qtm?|ra(m|r)?|sea|sit|tar|tgz|torrent|txt|wav|wma|wmv|wpd|xls|xml|z|zip');
162
  }
163
  return t('The default extensions for download tracking have been updated to piwik defaults.');
164
}
165

    
166
/**
167
 * Add trailing slash to piwik URLs.
168
 */
169
function piwik_update_7200() {
170
  $url_http = variable_get('piwik_url_http', '');
171
  $url_https = variable_get('piwik_url_https', '');
172

    
173
  if (!empty($url_http)) {
174
    variable_set('piwik_url_http', $url_http . '/');
175
  }
176
  if (!empty($url_https)) {
177
    variable_set('piwik_url_https', $url_https . '/');
178
  }
179

    
180
  return t('Added trailing slash to piwik URLs.');
181
}
182

    
183
/**
184
 * Move async tracking code to header.
185
 */
186
function piwik_update_7201() {
187
  variable_set('piwik_js_scope', 'header');
188

    
189
  return t('Async tracking code has been moved to header.');
190
}
191

    
192
/**
193
 * Backup synchronous code snippets.
194
 */
195
function piwik_update_7202() {
196
  $messages = array();
197

    
198
  // TODO: Backup synchronous code snippets. Remove variables in D8.
199
  variable_set('piwik_codesnippet_before_backup_7202', variable_get('piwik_codesnippet_before', ''));
200
  variable_set('piwik_codesnippet_after_backup_7202', variable_get('piwik_codesnippet_after', ''));
201

    
202
  // Upgrade of BEFORE code snippet.
203
  $code_before = variable_get('piwik_codesnippet_before', '');
204
  if (!empty($code_before)) {
205
    // No value, e.g. piwikTracker.enableLinkTracking()
206
    $code_before = preg_replace('/(.*)piwikTracker\.(\w+)\(\);(.*)/i', '$1_paq.push(["$2"]);$3', $code_before);
207
    // One value, e.g. piwikTracker.addDownloadExtensions("mp5|mp6"), piwikTracker.trackGoal(1)
208
    $code_before = preg_replace('/(.*)piwikTracker\.(\w+)\(("|\'?)(\w+)("|\'?)\);(.*)/i', '$1_paq.push(["$2", $3$4$5]);$6', $code_before);
209
    // Multiple values e.g. piwikTracker.trackLink('/store', 'addTransaction', trans)
210
    $code_before = preg_replace('/(.*)piwikTracker\.(\w+)\((.*)\);(.*)/i', '$1_paq.push(["$2", $3]);$4', $code_before);
211

    
212
    variable_set('piwik_codesnippet_before', $code_before);
213

    
214
    drupal_set_message(Database::getConnection()->prefixTables("<strong>Attempted</strong> to upgrade Piwik custom 'before' code snippet. Backup of previous code snippet has been saved in database table '{variable}' as 'piwik_codesnippet_before_backup_7202'. Please consult Piwik's <a href='http://piwik.org/docs/javascript-tracking/#toc-asynchronous-tracking'>Asynchronous tracking Guide</a> if the upgrade was successfully."), 'warning');
215
    $messages[] = t('Upgraded custom "before" code snippet.');
216
  }
217

    
218
  // Upgrade of AFTER code snippet.
219
  // We cannot update this code snippet automatically. Show message that the upgrade has been skipped.
220
  $code_after = variable_get('piwik_codesnippet_after', '');
221
  if (!empty($code_after)) {
222
    drupal_set_message(Database::getConnection()->prefixTables("Automatic upgrade of Google Analytics custom 'after' code snippet has been skipped. Backup of previous code snippet has been saved in database table '{variable}' as 'piwik_codesnippet_after_backup_7202'. You need to manually upgrade the custom 'after' code snippet."), 'error');
223
    $messages[] = t('Skipped custom "after" code snippet.');
224
  }
225

    
226
  return empty($messages) ? t('No custom code snipped found. Nothing to do.') : implode(' ', $messages);
227
}
228

    
229
/**
230
 * Path visibility filter setting should hide "batch" path.
231
 */
232
function piwik_update_7203() {
233
  // Latest pages setting.
234
  $pages = array(
235
    'admin',
236
    'admin/*',
237
    'user/*/*',
238
    'node/add*',
239
    'node/*/*',
240
  );
241

    
242
  $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('piwik_pages', implode("\n", $pages))));
243
  if (empty($diff)) {
244
    // No diff to latest settings found. Update with new settings.
245
    $pages = array(
246
      'admin',
247
      'admin/*',
248
      'batch',
249
      'node/add*',
250
      'node/*/*',
251
      'user/*/*',
252
    );
253
    variable_set('piwik_pages', implode("\n", $pages));
254
    return t('Added "batch" to path visibility filter setting.');
255
  }
256
  else {
257
    return t('Custom path visibility filter setting found. Update skipped!');
258
  }
259
}
260

    
261
/**
262
 * Rename piwik_visibility variable to piwik_visibility_pages for consistency.
263
 */
264
function piwik_update_7204() {
265
  variable_set('piwik_visibility_pages', variable_get('piwik_visibility', 1));
266
  variable_del('piwik_visibility');
267

    
268
  return t('Renamed "piwik_visibility" settings variable to piwik_visibility_pages.');
269
}
270

    
271
/**
272
 * Update list of default file extensions.
273
 */
274
function piwik_update_7205() {
275
  if (variable_get('piwik_trackfiles_extensions', '') == '7z|aac|arc|arj|asf|asx|avi|bin|csv|doc|exe|flv|gif|gz|gzip|hqx|jar|jpe?g|js|mp(2|3|4|e?g)|mov(ie)?|msi|msp|pdf|phps|png|ppt|qtm?|ra(m|r)?|sea|sit|tar|tgz|torrent|txt|wav|wma|wmv|wpd|xls|xml|z|zip') {
276
    variable_set('piwik_trackfiles_extensions', '7z|aac|arc|arj|asf|asx|avi|bin|csv|doc(x|m)?|dot(x|m)?|exe|flv|gif|gz|gzip|hqx|jar|jpe?g|js|mp(2|3|4|e?g)|mov(ie)?|msi|msp|pdf|phps|png|ppt(x|m)?|pot(x|m)?|pps(x|m)?|ppam|sld(x|m)?|thmx|qtm?|ra(m|r)?|sea|sit|tar|tgz|torrent|txt|wav|wma|wmv|wpd|xls(x|m|b)?|xlt(x|m)|xlam|xml|z|zip');
277
    return t('The default extensions for download tracking have been updated.');
278
  }
279
  else {
280
    return t('Custom extensions for download tracking setting found. Update skipped!');
281
  }
282
}
283

    
284
/**
285
 * Install Matomo module and migrate all settings.
286
 */
287
function piwik_update_7206(&$sandbox) {
288
  // Make the update hook failing so it can re-run later again.
289
  if (!module_load_include('module', 'matomo', 'matomo')) {
290
    throw new DrupalUpdateException(t('Matomo module is missing. Download it and re-run update.php'));
291
  }
292

    
293
  $module_enabled = module_enable(array('matomo'));
294
  if ($module_enabled) {
295
    $messages[] = t('Successfully installed matomo module.');
296

    
297
    $piwik_cache = variable_get('piwik_cache', 0);
298
    $piwik_codesnippet_before = variable_get('piwik_codesnippet_before', '');
299
    $piwik_codesnippet_after = variable_get('piwik_codesnippet_after', '');
300
    $piwik_custom = variable_get('piwik_custom', 0);
301
    $piwik_custom_var = variable_get('piwik_custom_var', array());
302
    $piwik_domain_mode = variable_get('piwik_domain_mode', 0);
303
    $piwik_js_scope = variable_get('piwik_js_scope', 'header');
304
    $piwik_last_cache = variable_get('piwik_last_cache', 0);
305
    $piwik_page_title_hierarchy = variable_get('piwik_page_title_hierarchy', FALSE);
306
    $piwik_page_title_hierarchy_exclude_home = variable_get('piwik_page_title_hierarchy_exclude_home', TRUE);
307
    $piwik_pages = variable_get('piwik_pages', "admin\nadmin/*\nbatch\nnode/add*\nnode/*/*\nuser/*/*");
308
    $piwik_privacy_donottrack = variable_get('piwik_privacy_donottrack', 1);
309
    $piwik_roles = variable_get('piwik_roles', array());
310
    $piwik_site_id = variable_get('piwik_site_id', '');
311
    $piwik_site_search = variable_get('piwik_site_search', FALSE);
312
    $piwik_trackcolorbox = variable_get('piwik_trackcolorbox', 1);
313
    $piwik_trackmailto = variable_get('piwik_trackmailto', 1);
314
    $piwik_track = variable_get('piwik_track', 1);
315
    $piwik_trackfiles_extensions = variable_get('piwik_trackfiles_extensions', '7z|aac|arc|arj|asf|asx|avi|bin|csv|doc(x|m)?|dot(x|m)?|exe|flv|gif|gz|gzip|hqx|jar|jpe?g|js|mp(2|3|4|e?g)|mov(ie)?|msi|msp|pdf|phps|png|ppt(x|m)?|pot(x|m)?|pps(x|m)?|ppam|sld(x|m)?|thmx|qtm?|ra(m|r)?|sea|sit|tar|tgz|torrent|txt|wav|wma|wmv|wpd|xls(x|m|b)?|xlt(x|m)|xlam|xml|z|zip');
316
    $piwik_trackmessages = variable_get('piwik_trackmessages', array());
317
    $piwik_trackuserid = variable_get('piwik_trackuserid', 0);
318
    $piwik_translation_set = variable_get('piwik_translation_set', 0);
319
    $piwik_url_http = variable_get('piwik_url_http', '');
320
    $piwik_url_https = variable_get('piwik_url_https', '');
321
    $piwik_visibility_pages = variable_get('piwik_visibility_pages', 0);
322
    $piwik_visibility_roles = variable_get('piwik_visibility_roles', 0);
323

    
324
    variable_set('matomo_cache', $piwik_cache);
325
    variable_set('matomo_codesnippet_before', $piwik_codesnippet_before);
326
    variable_set('matomo_codesnippet_after', $piwik_codesnippet_after);
327
    variable_set('matomo_custom', $piwik_custom);
328
    variable_set('matomo_custom_var', $piwik_custom_var);
329
    variable_set('matomo_domain_mode', $piwik_domain_mode);
330
    variable_set('matomo_js_scope', $piwik_js_scope);
331
    variable_set('matomo_last_cache', $piwik_last_cache);
332
    variable_set('matomo_page_title_hierarchy', $piwik_page_title_hierarchy);
333
    variable_set('matomo_page_title_hierarchy_exclude_home', $piwik_page_title_hierarchy_exclude_home);
334
    variable_set('matomo_pages', $piwik_pages);
335
    variable_set('matomo_privacy_donottrack', $piwik_privacy_donottrack);
336
    variable_set('matomo_roles', $piwik_roles);
337
    variable_set('matomo_site_id', $piwik_site_id);
338
    variable_set('matomo_site_search', $piwik_site_search);
339
    variable_set('matomo_trackcolorbox', $piwik_trackcolorbox);
340
    variable_set('matomo_trackmailto', $piwik_trackmailto);
341
    variable_set('matomo_track', $piwik_track);
342
    variable_set('matomo_trackfiles_extensions', $piwik_trackfiles_extensions);
343
    variable_set('matomo_trackmessages', $piwik_trackmessages);
344
    variable_set('matomo_trackuserid', $piwik_trackuserid);
345
    variable_set('matomo_translation_set', $piwik_translation_set);
346
    variable_set('matomo_url_http', $piwik_url_http);
347
    variable_set('matomo_url_https', $piwik_url_https);
348
    variable_set('matomo_visibility_pages', $piwik_visibility_pages);
349
    variable_set('matomo_visibility_roles', $piwik_visibility_roles);
350

    
351
    $messages[] = t('Copied settings from piwik to matomo module.');
352

    
353
    // Remove matomo default permissions or duplicate 'rid' error occurs.
354
    db_delete('role_permission')
355
      ->condition('module', 'matomo')
356
      ->execute();
357

    
358
    // Migrate piwik permissions to matomo.
359
    $permissions = array(
360
      'administer piwik' => 'administer matomo',
361
      'opt-in or out of tracking' => 'opt-in or out of matomo tracking',
362
      'use PHP for tracking visibility' => 'use php for matomo tracking visibility',
363
      'add JS snippets for piwik' => 'add js snippets for matomo',
364
    );
365
    foreach ($permissions as $permission_old_key => $permission_new_key) {
366
      db_update('role_permission')
367
        ->fields(array(
368
          'permission' => $permission_new_key,
369
          'module' => 'matomo',
370
        ))
371
        ->condition(db_and()
372
          ->condition('permission', $permission_old_key)
373
          ->condition('module', 'piwik')
374
        )
375
        ->execute();
376
    }
377

    
378
    // Clear the user access cache.
379
    drupal_static_reset('user_access');
380
    drupal_static_reset('user_role_permissions');
381
    $messages[] = t('Moved piwik permissions to matomo module.');
382

    
383
    // Disable piwik modules depending on piwik or the migration will fail.
384
    $module_disable = module_disable(array('piwik'), FALSE);
385
    if (!module_exists('piwik')) {
386
      $messages[] = t('Successfully disabled piwik module.');
387
    }
388
    else {
389
      $messages[] = t('FAILED to disable piwik module.');
390

    
391
      // Unconfigure piwik to remove piwik tracking code; if piwik module may failed to disable.
392
      variable_set('piwik_site_id', '');
393
      $messages[] = t('Unconfigured site id in piwik module to disable double tracking.');
394
    }
395

    
396
    // Uninstall piwik module.
397
    $module_uninstalled = drupal_uninstall_modules(array('piwik'), FALSE);
398
    if ($module_uninstalled) {
399
      // Drupal does not remove all entries as the piwik hook is currently running.
400
      db_delete('system')
401
        ->condition('name', 'piwik')
402
        ->execute();
403
      db_delete('cache_bootstrap')
404
        ->condition('cid', 'system_list')
405
        ->execute();
406

    
407
      $messages[] = t('Successfully uninstalled piwik module. Please remove piwik from your system.');
408
      drupal_set_message('Piwik is now Matomo! Matomo has been installed. Please remove Piwik module from your system.', 'warning');
409
    }
410
    else {
411
      $messages[] = t('FAILED to uninstall piwik module.');
412
    }
413
  }
414

    
415
  return empty($messages) ? t('FAILED to migrate piwik to matomo module. Please uninstall piwik module and install matomo module manually!') : implode(' ', $messages);
416
}