You are here

yandex_metrics.install in Yandex.Metrics 6.2

Install, uninstall and update the module.

File

yandex_metrics.install
View source
<?php

/**
 * @file
 *   Install, uninstall and update the module.
 */

/**
 * Implementation of hook_uninstall().
 */
function yandex_metrics_uninstall() {

  // Delete module variables and clear variables cache.
  db_query("DELETE FROM {variable} WHERE name LIKE 'yandex_metrics_%'");
  cache_clear_all('variables', 'cache');

  // Remove block.
  db_query("DELETE FROM {blocks} WHERE module = 'yandex_metrics'");

  // Uninstall schema.
  drupal_uninstall_schema('yandex_metrics');
}

/**
 * Implementation of hook_update_N().
 */
function yandex_metrics_update_6100() {
  $schema['yandex_metrics_popular_content'] = array(
    'description' => 'Stores the popular content.',
    'fields' => array(
      'url' => array(
        'description' => 'The url obtained from Yandex.metrika.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'page_title' => array(
        'description' => 'The page title of the url.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'page_views' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Page views of this url.',
      ),
    ),
    'unique keys' => array(
      'url' => array(
        'url',
      ),
    ),
  );
  $ret = array();
  if (!db_table_exists('yandex_metrics_popular_content')) {
    db_create_table($ret, 'yandex_metrics_popular_content', $schema['yandex_metrics_popular_content']);
  }

  // Clear CSS and JS caches.
  drupal_clear_css_cache();
  drupal_clear_js_cache();

  // Rebuild the menu to add new item admin/yandex_metrics_ajax/%/%/%.
  menu_rebuild();

  // Flush content caches.
  cache_clear_all();
  $ret[] = array(
    'success' => TRUE,
    'query' => 'Caches have been flushed',
  );
  return $ret;
}

/**
 * Implementation of hook_schema().
 */
function yandex_metrics_schema() {
  $schema['yandex_metrics_popular_content'] = array(
    'description' => 'Stores the popular content.',
    'fields' => array(
      'url' => array(
        'description' => 'The url obtained from Yandex.metrika.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'page_title' => array(
        'description' => 'The page title of the url.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'page_views' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Page views of this url.',
      ),
    ),
    'unique keys' => array(
      'url' => array(
        'url',
      ),
    ),
  );
  return $schema;
}

/**
 * Upgrade from 6.x-1.x to 6.x-2.x.
 */
function yandex_metrics_update_6200() {

  // Enable yandex_metrics_reports.
  module_enable(array(
    'yandex_metrics_reports',
  ));

  // I don't know why but module_enable does not call hook_install.
  // I should call it by myself.
  module_invoke('yandex_metrics_reports', 'install');
  $ret = array();

  // Rename old popular content table to the new table.
  if (db_table_exists('yandex_metrics_popular_content')) {
    db_drop_table($ret, 'yandex_metrics_reports_popular_content');
    db_rename_table($ret, 'yandex_metrics_popular_content', 'yandex_metrics_reports_popular_content');
  }

  // Remap variables.
  variable_set('yandex_metrics_reports_popular_content_date_period', variable_get('yandex_metrics_popular_content_date_period', 'week'));
  variable_set('yandex_metrics_reports_popular_content_links_count', variable_get('yandex_metrics_popular_content_links_count', 10));
  variable_set('yandex_metrics_reports_client_id', variable_get('yandex_metrics_client_id', ''));
  variable_set('yandex_metrics_reports_client_secret', variable_get('yandex_metrics_client_secret', ''));
  variable_set('yandex_metrics_reports_auth_token', variable_get('yandex_metrics_auth_token', ''));
  variable_set('yandex_metrics_reports_counter_id', variable_get('yandex_metrics_counter_id', ''));
  variable_set('yandex_metrics_reports_use_ajax', variable_get('yandex_metrics_use_ajax', TRUE));
  variable_set('yandex_metrics_reports_visits_chart_visible', variable_get('yandex_metrics_visits_chart_visible', TRUE));
  variable_set('yandex_metrics_reports_sources_chart_visible', variable_get('yandex_metrics_sources_chart_visible', TRUE));
  variable_set('yandex_metrics_reports_search_phrases_visible', variable_get('yandex_metrics_search_phrases_visible', TRUE));
  variable_set('yandex_metrics_reports_popular_content_visible', variable_get('yandex_metrics_popular_content_visible', TRUE));
  variable_set('yandex_metrics_reports_geo_chart_visible', variable_get('yandex_metrics_geo_chart_visible', TRUE));

  // Remove unnecessary variables.
  variable_del('yandex_metrics_popular_content_date_period');
  variable_del('yandex_metrics_popular_content_links_count');
  variable_del('yandex_metrics_client_id');
  variable_del('yandex_metrics_client_secret');
  variable_del('yandex_metrics_auth_token');
  variable_del('yandex_metrics_counter_id');
  variable_del('yandex_metrics_use_ajax');
  variable_del('yandex_metrics_visits_chart_visible');
  variable_del('yandex_metrics_sources_chart_visible');
  variable_del('yandex_metrics_search_phrases_visible');
  variable_del('yandex_metrics_popular_content_visible');
  variable_del('yandex_metrics_geo_chart_visible');

  // Clear variables cache.
  cache_clear_all('variables', 'cache');
  if (db_result(db_query("SELECT count(*) FROM {blocks} WHERE module = 'yandex_metrics'")) > 0) {

    // Remove new block records.
    $ret[] = update_sql("DELETE FROM {blocks} WHERE module = 'yandex_metrics_reports'");

    // Assign old block records.
    $ret[] = update_sql("UPDATE {blocks} SET module = 'yandex_metrics_reports' WHERE module = 'yandex_metrics'");
  }

  // Clear CSS and JS caches.
  drupal_clear_css_cache();
  drupal_clear_js_cache();

  // Rebuild the menu.
  menu_rebuild();
  $ret[] = array(
    'success' => TRUE,
    'query' => 'The module has been upgraded to 6.x-2.x',
  );
  return $ret;
}

Functions

Namesort descending Description
yandex_metrics_schema Implementation of hook_schema().
yandex_metrics_uninstall Implementation of hook_uninstall().
yandex_metrics_update_6100 Implementation of hook_update_N().
yandex_metrics_update_6200 Upgrade from 6.x-1.x to 6.x-2.x.