Projet

Général

Profil

Paste
Télécharger (8,93 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / piwik / piwik.install @ 503b3f7b

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_track'); // interrims solution
40
  variable_del('piwik_trackfiles_extensions');
41
  variable_del('piwik_translation_set');
42
  variable_del('piwik_url_http');
43
  variable_del('piwik_url_https');
44
  variable_del('piwik_visibility_pages');
45
  variable_del('piwik_visibility_roles');
46

    
47
  // Remove backup variables if exits. Remove this code in D8.
48
  variable_del('piwik_codesnippet_before_backup_7202');
49
  variable_del('piwik_codesnippet_after_backup_7202');
50
}
51

    
52
/**
53
 * Remove cache directory if module is disabled (or uninstalled).
54
 */
55
function piwik_disable() {
56
  piwik_clear_js_cache();
57
}
58

    
59
/**
60
 * Implementation of hook_requirements().
61
 */
62
function piwik_requirements($phase) {
63
  $requirements = array();
64
  $t = get_t();
65

    
66
  switch ($phase) {
67
    case 'runtime' :
68
      // Module cannot validate piwik URL without external HTTP requests.
69
      if (variable_get('drupal_http_request_fails', TRUE) && !system_check_http_request()) {
70
        $requirements['http requests'] = array(
71
          'title' => $t('HTTP request status'),
72
          'value' => $t('Fails'),
73
          'severity' => REQUIREMENT_ERROR,
74
          '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.'),
75
        );
76
      }
77

    
78
      // Raise warning if Piwik user account has not been set yet.
79
      if (!preg_match('/^\d{1,}$/', variable_get('piwik_site_id', ''))) {
80
        $requirements['piwik'] = array(
81
          'title' => $t('Piwik module'),
82
          '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'))),
83
          'severity' => REQUIREMENT_WARNING,
84
          'value' => $t('Not configured'),
85
        );
86
      }
87
      break;
88
  }
89
  return $requirements;
90
}
91

    
92

    
93
/**
94
 * Change visibility setting for paths.
95
 */
96
function piwik_update_6000() {
97
  // Orginal pages setting.
98
  $pages = array(
99
    'admin*',
100
    'user*',
101
    'node/add*',
102
    'node/*/*',
103
  );
104

    
105
  $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('piwik_pages', implode("\n", $pages))));
106
  if (empty($diff)) {
107
    // No diff to original settings found. Update with new settings.
108
    $pages = array(
109
      'admin',
110
      'admin/*',
111
      'user/*/*',
112
      'node/add*',
113
      'node/*/*',
114
    );
115
    variable_set('piwik_pages', implode("\n", $pages));
116
    return t('Path visibility filter setting changed from "admin*" to "admin, admin/*" and "user*" changed to "user/*/*".');
117
  }
118
  else {
119
    return t('Custom path visibility filter setting found. Update skipped!');
120
  }
121
}
122

    
123
/**
124
 * Upgrade custom javascript settings.
125
 */
126
function piwik_update_6001() {
127
  variable_set('piwik_codesnippet_before', variable_get('piwik_codesnippet', ''));
128
  variable_del('piwik_codesnippet');
129
  return t('Upgraded custom javascript codesnippet setting.');
130
}
131

    
132
/**
133
 * Remove obsolte token auth.
134
 */
135
function piwik_update_6002() {
136
  variable_del('piwik_auth');
137
  return t('The global token_auth setting has been deleted. Users need to configure their personal token_auth.');
138
}
139

    
140
/**
141
 * Update list of default extensions
142
 */
143
function piwik_update_6003() {
144
  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') {
145
    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');
146
  }
147
  return t('The default extensions for download tracking have been updated to piwik defaults.');
148
}
149

    
150
/**
151
 * Add trailing slash to piwik URLs.
152
 */
153
function piwik_update_7200() {
154
  $url_http = variable_get('piwik_url_http', '');
155
  $url_https = variable_get('piwik_url_https', '');
156

    
157
  if (!empty($url_http)) {
158
    variable_set('piwik_url_http', $url_http . '/');
159
  }
160
  if (!empty($url_https)) {
161
    variable_set('piwik_url_https', $url_https . '/');
162
  }
163

    
164
  return t('Added trailing slash to piwik URLs.');
165
}
166

    
167
/**
168
 * Move async tracking code to header.
169
 */
170
function piwik_update_7201() {
171
  variable_set('piwik_js_scope', 'header');
172

    
173
  return t('Async tracking code has been moved to header.');
174
}
175

    
176
/**
177
 * Backup synchronous code snippets.
178
 */
179
function piwik_update_7202() {
180
  $messages = array();
181

    
182
  // TODO: Backup synchronous code snippets. Remove variables in D8.
183
  variable_set('piwik_codesnippet_before_backup_7202', variable_get('piwik_codesnippet_before', ''));
184
  variable_set('piwik_codesnippet_after_backup_7202', variable_get('piwik_codesnippet_after', ''));
185

    
186
  // Upgrade of BEFORE code snippet.
187
  $code_before = variable_get('piwik_codesnippet_before', '');
188
  if (!empty($code_before)) {
189
    // No value, e.g. piwikTracker.enableLinkTracking()
190
    $code_before = preg_replace('/(.*)piwikTracker\.(\w+)\(\);(.*)/i', '$1_paq.push(["$2"]);$3', $code_before);
191
    // One value, e.g. piwikTracker.addDownloadExtensions("mp5|mp6"), piwikTracker.trackGoal(1)
192
    $code_before = preg_replace('/(.*)piwikTracker\.(\w+)\(("|\'?)(\w+)("|\'?)\);(.*)/i', '$1_paq.push(["$2", $3$4$5]);$6', $code_before);
193
    // Multiple values e.g. piwikTracker.trackLink('/store', 'addTransaction', trans)
194
    $code_before = preg_replace('/(.*)piwikTracker\.(\w+)\((.*)\);(.*)/i', '$1_paq.push(["$2", $3]);$4', $code_before);
195

    
196
    variable_set('piwik_codesnippet_before', $code_before);
197

    
198
    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');
199
    $messages[] = t('Upgraded custom "before" code snippet.');
200
  }
201

    
202
  // Upgrade of AFTER code snippet.
203
  // We cannot update this code snippet automatically. Show message that the upgrade has been skipped.
204
  $code_after = variable_get('piwik_codesnippet_after', '');
205
  if (!empty($code_after)) {
206
    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');
207
    $messages[] = t('Skipped custom "after" code snippet.');
208
  }
209

    
210
  return empty($messages) ? t('No custom code snipped found. Nothing to do.') : implode(' ', $messages);
211
}
212

    
213
/**
214
 * Path visibility filter setting should hide "batch" path.
215
 */
216
function piwik_update_7203() {
217
  // Latest pages setting.
218
  $pages = array(
219
    'admin',
220
    'admin/*',
221
    'user/*/*',
222
    'node/add*',
223
    'node/*/*',
224
  );
225

    
226
  $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('piwik_pages', implode("\n", $pages))));
227
  if (empty($diff)) {
228
    // No diff to latest settings found. Update with new settings.
229
    $pages = array(
230
      'admin',
231
      'admin/*',
232
      'batch',
233
      'node/add*',
234
      'node/*/*',
235
      'user/*/*',
236
    );
237
    variable_set('piwik_pages', implode("\n", $pages));
238
    return t('Added "batch" to path visibility filter setting.');
239
  }
240
  else {
241
    return t('Custom path visibility filter setting found. Update skipped!');
242
  }
243
}
244

    
245
/**
246
 * Rename piwik_visibility variable to piwik_visibility_pages for consistency.
247
 */
248
function piwik_update_7204() {
249
  variable_set('piwik_visibility_pages', variable_get('piwik_visibility', 1));
250
  variable_del('piwik_visibility');
251

    
252
  return t('Renamed "piwik_visibility" settings variable to piwik_visibility_pages.');
253
}