You are here

statspro.install in Statistics Pro 6

Same filename and directory in other branches
  1. 6.2 statspro.install

Install file for statistics pro module

File

statspro.install
View source
<?php

/**
 * @file
 *   Install file for statistics pro module
 *
 */

/**
 * Implementation of hook_install().
 */
function statspro_install() {
  variable_set('statspro_last_run', 0);
  variable_set('statspro_start', 0);
  variable_set('statspro_end', 0);
  variable_set('statspro_term_rebuild', TRUE);
  drupal_install_schema('statspro');
  drupal_set_message(st('You have to run cron to recreate statistics. !url', array(
    '!url' => l(st('You can run cron manually.'), 'admin/reports/status/run-cron'),
  )));
}

/**
 * Implementation of hook_uninstall().
 */
function statspro_uninstall() {
  drupal_uninstall_schema('statspro');
  db_query("DELETE FROM {variable} WHERE name LIKE 'statspro_%%'");
}

/**
 * Implementation of hook_schema().
 */
function statspro_schema() {
  $schema['statspro'] = array(
    'fields' => array(
      'day' => array(
        'type' => 'varchar',
        'pgsql_type' => 'date',
        'mysql_type' => 'DATE',
        'sqlite_type' => 'DATE',
        'not null' => TRUE,
        'disp-width' => '10',
      ),
      'nuser' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'auser' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'nnode' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'cnode' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'comment' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'pi' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'upi' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'error' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'uerror' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'warning' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'uwarning' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
    ),
    'primary key' => array(
      'day',
    ),
  );
  $schema['statspro_term'] = array(
    'fields' => array(
      'tid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'ncount' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
    ),
    'primary key' => array(
      'tid',
    ),
    'indexes' => array(
      'ncount' => array(
        'ncount',
      ),
    ),
  );
  return $schema;
}

/**
 * Implementation of hook_requirements().
 */
function statspro_requirements($phase) {
  $requirements = array();

  // Ensure translations don't break at install time
  $t = get_t();
  if ($phase == 'runtime') {
    $last_run = variable_get('statspro_last_run', 0);
    if (empty($last_run)) {
      $requirements['statspro'] = array(
        'title' => $t('Statistics Pro'),
        'description' => $t('Statistics Pro requires to run cron. !url', array(
          '!url' => l('You can run cron manually.', 'admin/reports/status/run-cron'),
        )),
        'severity' => REQUIREMENT_ERROR,
        'value' => $t('No data generated'),
      );
    }

    /**
     * This check is important to catch updates from previous versions when the
     * Statistics Advanced module wasn't yet a dependency.
     */
    $req_id = 'statspro_statistics_advanced';
    $requirements[$req_id] = array(
      'title' => $t('@module module', array(
        '@module' => $t('Statistics Advanced'),
      )),
    );
    if (module_exists('statistics_advanced')) {
      $requirements[$req_id]['value'] = $t('Enabled');
      $requirements[$req_id]['severity'] = REQUIREMENT_OK;
    }
    else {
      $requirements[$req_id]['value'] = $t('Disabled');
      $requirements[$req_id]['severity'] = REQUIREMENT_WARNING;
      $requirements[$req_id]['description'] = $t('All users of @this_module module should enable the !statistics_advanced module for proper statistcs recording. Please consider installing and enabling it.', array(
        '!statistics_advanced' => l($t('Statistics Advanced'), 'http://drupal.org/project/statistics_advanced'),
        '@this_module' => $t('Statistics Pro'),
      ));
    }
  }
  return $requirements;
}
function statspro_update_1() {
  statspro_reset_stats();
  return array();
}
function statspro_update_2() {
  $ret = array();
  $schema = statspro_schema();
  db_create_table($ret, 'statspro_term', $schema['statspro_term']);
  variable_set('statspro_term_rebuild', TRUE);
  return $ret;
}

/**
 * Creates a temporary statspro table with the day column already as DATE.
 *
 * @return array
 */
function statspro_update_6100() {
  $ret = array();
  $new_table = array(
    'fields' => array(
      'day' => array(
        'type' => 'varchar',
        'pgsql_type' => 'date',
        'mysql_type' => 'DATE',
        'sqlite_type' => 'DATE',
        'not null' => TRUE,
        'disp-width' => '10',
      ),
      'nuser' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'auser' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'nnode' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'cnode' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'comment' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'pi' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'upi' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'error' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'uerror' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'warning' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
      'uwarning' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'disp-width' => '10',
      ),
    ),
    'primary key' => array(
      'day',
    ),
  );
  db_create_table($ret, 'statspro_day_to_date_temp', $new_table);
  return $ret;
}

/**
 * Moves the data from the old statspro table which has the column 'day' as
 * TIMESTAMP to the temporary statspro_temp table which already has the 'day'
 * column as DATE.
 *
 * @return array
 */
function statspro_update_6101() {
  $qt_per_call = 20;

  // See if we are being called for the first time
  if (!isset($_SESSION['statspro_update_6101_current'])) {

    // These variables keep track of our progress
    $_SESSION['statspro_update_6101_current'] = 0;
    $_SESSION['statspro_update_6101_max'] = db_result(db_query('SELECT COUNT(*) FROM {statspro}')) - 1;
  }

  // Fetch the next $qt_per_call nodes
  $result = db_query_range('SELECT *, DATE(FROM_UNIXTIME(day)) AS day_date FROM {statspro} ORDER BY day', $_SESSION['statspro_update_6101_current'], $_SESSION['statspro_update_6101_current'] + $qt_per_call);
  while ($row = db_fetch_array($result)) {
    $found = db_result(db_query("SELECT * FROM {statspro_day_to_date_temp} WHERE day = '%s'", $row['day_date']));
    $_SESSION['statspro_update_6101_current']++;
    if ($found) {
      db_query("UPDATE {statspro_day_to_date_temp}\n          SET nuser = nuser + %d,\n            auser = auser + %d,\n            nnode = nnode + %d,\n            cnode = cnode + %d,\n            comment = comment + %d,\n            pi = pi + %d,\n            upi = upi + %d,\n            error = error + %d,\n            uerror = uerror + %d,\n            warning = warning + %d,\n            uwarning = uwarning + %d\n          WHERE day = '%s'", $row['nuser'], $row['auser'], $row['nnode'], $row['cnode'], $row['comment'], $row['pi'], $row['upi'], $row['error'], $row['uerror'], $row['warning'], $row['uwarning'], $row['day_date']);
    }
    else {
      db_query("INSERT INTO {statspro_day_to_date_temp}\n          (nuser, auser, nnode, cnode, comment, pi, upi, error, uerror, warning,\n            uwarning, day)\n          VALUES (%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, '%s')", $row['nuser'], $row['auser'], $row['nnode'], $row['cnode'], $row['comment'], $row['pi'], $row['upi'], $row['error'], $row['uerror'], $row['warning'], $row['uwarning'], $row['day_date']);
    }
  }

  // See if we are done
  if ($_SESSION['statspro_update_6101_current'] < $_SESSION['statspro_update_6101_max']) {

    // Not done yet. Return the progress.
    return array(
      '#finished' => $_SESSION['statspro_update_6101_current'] / $_SESSION['statspro_update_6101_max'],
    );
  }
  else {

    // Done. Clean up and indicate we're finished.
    unset($_SESSION['statspro_update_6101_current']);
    unset($_SESSION['statspro_update_6101_max']);
    return array(
      '#finished' => 1,
    );
  }
}

/**
 * Drops the old statspro table.
 *
 * @return array
 */
function statspro_update_6102() {
  $ret = array();
  db_drop_table($ret, 'statspro');
  db_rename_table($ret, 'statspro_day_to_date_temp', 'statspro');
  return $ret;
}

/**
 * Should not be used.
 *
 * Jump straight to statspro_update_6104().
 *
 * @return array
 */
function statspro_update_6103() {

  //  $ret = array();
  //  db_rename_table(&$ret, 'statspro_day_to_date_temp', 'statspro_day_to_date_temp2');
  //  if($ret['success']) {
  //    $ret = array();
  //    db_rename_table(&$ret, 'statspro_day_to_date_temp2', 'statspro');
  //  }
  //  else {
  //    $ret = array('success' => TRUE, 'query' => check_plain('SELECT COUNT(*) FROM {statspro}'));
  //  }
  //  return $ret;
  $ret = array();
  $ret[] = array(
    'success' => TRUE,
    'query' => check_plain('SELECT COUNT(*) FROM {statspro}'),
  );
  return $ret;
}

Functions

Namesort descending Description
statspro_install Implementation of hook_install().
statspro_requirements Implementation of hook_requirements().
statspro_schema Implementation of hook_schema().
statspro_uninstall Implementation of hook_uninstall().
statspro_update_1
statspro_update_2
statspro_update_6100 Creates a temporary statspro table with the day column already as DATE.
statspro_update_6101 Moves the data from the old statspro table which has the column 'day' as TIMESTAMP to the temporary statspro_temp table which already has the 'day' column as DATE.
statspro_update_6102 Drops the old statspro table.
statspro_update_6103 Should not be used.