You are here

piwik.install in Piwik Web Analytics 6.2

Installation file for Piwik - Web analytics module.

File

piwik.install
View source
<?php

/**
 * @file
 * Installation file for Piwik - Web analytics module.
 */

/**
 * Implementation of hook_install().
 */
function piwik_install() {

  // Remove tracking from all administrative pages, see http://drupal.org/node/34970.
  variable_set('piwik_visibility_roles', 0);
  variable_set('piwik_visibility_pages', 0);
  $pages = array(
    'admin',
    'admin/*',
    'batch',
    'node/add*',
    'node/*/*',
    'user/*/*',
  );
  variable_set('piwik_pages', implode("\n", $pages));
}

/**
 * Implementation of hook_uninstall().
 */
function piwik_uninstall() {
  variable_del('piwik_cache');
  variable_del('piwik_custom');
  variable_del('piwik_codesnippet_before');
  variable_del('piwik_codesnippet_after');
  variable_del('piwik_domain_mode');
  variable_del('piwik_js_scope');
  variable_del('piwik_last_cache');
  variable_del('piwik_page_title_hierarchy');
  variable_del('piwik_page_title_hierarchy_exclude_home');
  variable_del('piwik_pages');
  variable_del('piwik_privacy_donottrack');
  variable_del('piwik_roles');
  variable_del('piwik_site_id');
  variable_del('piwik_site_search');
  variable_del('piwik_trackmailto');
  variable_del('piwik_track');

  // interrims solution
  variable_del('piwik_trackfiles_extensions');
  variable_del('piwik_trackuserid');
  variable_del('piwik_translation_set');
  variable_del('piwik_url_http');
  variable_del('piwik_url_https');
  variable_del('piwik_visibility_pages');
  variable_del('piwik_visibility_roles');

  // Remove backup variables if exits. Remove this code in D8.
  variable_del('piwik_codesnippet_before_backup_7202');
  variable_del('piwik_codesnippet_after_backup_7202');
}

/**
 * Implementation of hook_disable().
 */
function piwik_disable() {

  // Remove cache directory if module is disabled (or uninstalled).
  piwik_clear_js_cache();
}

/**
 * Implementation of hook_requirements().
 */
function piwik_requirements($phase) {
  $requirements = array();
  $t = get_t();
  switch ($phase) {
    case 'runtime':

      // Module cannot validate piwik URL without external HTTP requests.
      if (variable_get('drupal_http_request_fails', TRUE) && !system_check_http_request()) {
        $requirements['piwik_http_requests'] = array(
          'title' => $t('HTTP request status'),
          'value' => $t('Fails'),
          'severity' => REQUIREMENT_ERROR,
          '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.'),
        );
      }

      // Raise warning if Piwik user account has not been set yet.
      if (!preg_match('/^\\d{1,}$/', variable_get('piwik_site_id', ''))) {
        $requirements['piwik'] = array(
          '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>.', array(
            '@url' => url('admin/settings/piwik'),
          )),
          'severity' => REQUIREMENT_WARNING,
          'value' => $t('Not configured'),
        );
      }
      break;
  }
  return $requirements;
}

/**
 * Change visibility setting for paths.
 */
function piwik_update_6000() {
  $ret = array();

  // Orginal pages setting.
  $pages = array(
    'admin*',
    'user*',
    'node/add*',
    'node/*/*',
  );
  $diff = array_diff($pages, preg_split('/(\\r\\n?|\\n)/', variable_get('piwik_pages', implode("\n", $pages))));
  if (empty($diff)) {

    // No diff to original settings found. Update with new settings.
    $pages = array(
      'admin',
      'admin/*',
      'user/*/*',
      'node/add*',
      'node/*/*',
    );
    variable_set('piwik_pages', implode("\n", $pages));
    $ret[] = array(
      'success' => TRUE,
      'query' => 'Path visibility filter setting changed from "admin*" to "admin, admin/*" and "user*" changed to "user/*/*".',
    );
  }
  else {
    $ret[] = array(
      'success' => TRUE,
      'query' => 'Custom path visibility filter setting found. Update skipped!',
    );
  }
  return $ret;
}

/**
 * Upgrade custom javascript settings.
 */
function piwik_update_6001() {
  $ret = array();
  variable_set('piwik_codesnippet_before', variable_get('piwik_codesnippet', ''));
  variable_del('piwik_codesnippet');
  $ret[] = array(
    'success' => TRUE,
    'query' => 'Upgraded custom javascript codesnippet setting.',
  );
  return $ret;
}

/**
 * Remove obsolte token auth.
 */
function piwik_update_6002() {
  $ret = array();
  variable_del('piwik_auth');
  $ret[] = array(
    'success' => TRUE,
    'query' => 'The global token_auth setting has been deleted. Users need to configure their personal token_auth.',
  );
  return $ret;
}

/**
 * Update list of default extensions
 */
function piwik_update_6003() {
  $ret = array();
  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') {
    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');
  }
  $ret[] = array(
    'success' => TRUE,
    'query' => 'The default extensions for download tracking have been updated to piwik defaults.',
  );
  return $ret;
}

/**
 * Add trailing slash to piwik URLs.
 */
function piwik_update_7200() {
  $ret = array();
  $url_http = variable_get('piwik_url_http', '');
  $url_https = variable_get('piwik_url_https', '');
  if (!empty($url_http)) {
    variable_set('piwik_url_http', $url_http . '/');
    $ret[] = array(
      'success' => TRUE,
      'query' => 'Added trailing slash to piwik HTTP URL.',
    );
  }
  if (!empty($url_https)) {
    variable_set('piwik_url_https', $url_https . '/');
    $ret[] = array(
      'success' => TRUE,
      'query' => 'Added trailing slash to piwik HTTPS URLs.',
    );
  }
  return $ret;
}

/**
 * Move async tracking code to header.
 */
function piwik_update_7201() {
  $ret = array();
  variable_set('piwik_js_scope', 'header');
  $ret[] = array(
    'success' => TRUE,
    'query' => 'Async tracking code has been moved to header.',
  );
  return $ret;
}

/**
 * Backup synchronous code snippets.
 */
function piwik_update_7202() {
  $ret = array();

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

  // Upgrade of BEFORE code snippet.
  $code_before = variable_get('piwik_codesnippet_before', '');
  if (!empty($code_before)) {

    // No value, e.g. piwikTracker.enableLinkTracking()
    $code_before = preg_replace('/(.*)piwikTracker\\.(\\w+)\\(\\);(.*)/i', '$1_paq.push(["$2"]);$3', $code_before);

    // One value, e.g. piwikTracker.addDownloadExtensions("mp5|mp6"), piwikTracker.trackGoal(1)
    $code_before = preg_replace('/(.*)piwikTracker\\.(\\w+)\\(("|\'?)(\\w+)("|\'?)\\);(.*)/i', '$1_paq.push(["$2", $3$4$5]);$6', $code_before);

    // Multiple values e.g. piwikTracker.trackLink('/store', 'addTransaction', trans)
    $code_before = preg_replace('/(.*)piwikTracker\\.(\\w+)\\((.*)\\);(.*)/i', '$1_paq.push(["$2", $3]);$4', $code_before);
    variable_set('piwik_codesnippet_before', $code_before);
    $ret[] = array(
      'success' => TRUE,
      'query' => 'Upgraded custom "before" code snippet.',
    );
    drupal_set_message(db_prefix_tables("<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');
  }

  // Upgrade of AFTER code snippet.
  // We cannot update this code snippet automatically. Show message that the upgrade has been skipped.
  $code_after = variable_get('piwik_codesnippet_after', '');
  if (!empty($code_after)) {
    $ret[] = array(
      'success' => TRUE,
      'query' => 'Skipped custom "after" code snippet.',
    );
    drupal_set_message(db_prefix_tables("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');
  }
  return $ret;
}

/**
 * Path visibility filter setting should hide "batch" path.
 */
function piwik_update_7203() {
  $ret = array();

  // Current pages setting.
  $pages = array(
    'admin',
    'admin/*',
    'user/*/*',
    'node/add*',
    'node/*/*',
  );
  $diff = array_diff($pages, preg_split('/(\\r\\n?|\\n)/', variable_get('piwik_pages', implode("\n", $pages))));
  if (empty($diff)) {

    // No diff to previous settings found. Update with new settings.
    $pages = array(
      'admin',
      'admin/*',
      'batch',
      'node/add*',
      'node/*/*',
      'user/*/*',
    );
    variable_set('piwik_pages', implode("\n", $pages));
    $ret[] = array(
      'success' => TRUE,
      'query' => 'Added "batch" to path visibility filter setting.',
    );
  }
  else {
    $ret[] = array(
      'success' => TRUE,
      'query' => 'Custom path visibility filter setting found. Update skipped!',
    );
  }
  return $ret;
}

/**
 * Rename piwik_visibility variable to piwik_visibility_pages for consistency.
 */
function piwik_update_7204() {
  $ret = array();
  variable_set('piwik_visibility_pages', variable_get('piwik_visibility', 1));
  variable_del('piwik_visibility');
  $ret[] = array(
    'success' => TRUE,
    'query' => 'Renamed "piwik_visibility" settings variable to piwik_visibility_pages.',
  );
  return $ret;
}

/**
 * Update list of default file extensions.
 */
function piwik_update_7205() {
  $ret = array();
  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') {
    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');
    $ret[] = array(
      'success' => TRUE,
      'query' => 'The default extensions for download tracking have been updated.',
    );
  }
  else {
    $ret[] = array(
      'success' => TRUE,
      'query' => 'Custom extensions for download tracking setting found. Update skipped!',
    );
  }
  return $ret;
}

Functions

Namesort descending Description
piwik_disable Implementation of hook_disable().
piwik_install Implementation of hook_install().
piwik_requirements Implementation of hook_requirements().
piwik_uninstall Implementation of hook_uninstall().
piwik_update_6000 Change visibility setting for paths.
piwik_update_6001 Upgrade custom javascript settings.
piwik_update_6002 Remove obsolte token auth.
piwik_update_6003 Update list of default extensions
piwik_update_7200 Add trailing slash to piwik URLs.
piwik_update_7201 Move async tracking code to header.
piwik_update_7202 Backup synchronous code snippets.
piwik_update_7203 Path visibility filter setting should hide "batch" path.
piwik_update_7204 Rename piwik_visibility variable to piwik_visibility_pages for consistency.
piwik_update_7205 Update list of default file extensions.