Projet

Général

Profil

Paste
Télécharger (9,01 ko) Statistiques
| Branche: | Révision:

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

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

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

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

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

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

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

    
94

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

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

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

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

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

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

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

    
166
  return t('Added trailing slash to piwik URLs.');
167
}
168

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

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

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

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

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

    
198
    variable_set('piwik_codesnippet_before', $code_before);
199

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

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

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

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

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

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

    
254
  return t('Renamed "piwik_visibility" settings variable to piwik_visibility_pages.');
255
}