You are here

radioactivity.install in Radioactivity 5

File

radioactivity.install
View source
<?php

// Advanced node popularity install
function radioactivity_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {radioactivity} (\n                 id int NOT NULL,\n                 class varchar(7) NOT NULL,\n\t\t decay_profile int NOT NULL,\n                 energy double unsigned NOT NULL DEFAULT 0,\n                 last_emission_timestamp int NOT NULL,\n                 PRIMARY KEY (id, class, decay_profile)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE INDEX {radioactivity_ix_energy}\n                 ON {radioactivity} (energy);");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {radioactivity} (\n                 id int NOT NULL,\n                 class varchar(7) NOT NULL,\n\t\t decay_profile int NOT NULL,\n                 energy double precision NOT NULL DEFAULT 0,\n                 last_emission_timestamp int NOT NULL,\n                 PRIMARY KEY (id, class, decay_profile)\n        );");
      db_query("CREATE INDEX {radioactivity}_ix_energy\n                 ON {radioactivity} (energy);");
      break;
  }
}
function radioactivity_uninstall() {
  db_query("DROP TABLE {radioactivity}");
  variable_del("radioactivity_profiles");
  variable_del("radioactivity_decay_granularity");
  variable_del("radioactivity_last_cron_timestamp");
}
function radioactivity_update_1() {
  $ret = array();
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {radioactivity} DROP PRIMARY KEY");
      $ret[] = update_sql("ALTER TABLE {radioactivity} CHANGE COLUMN nid id int NOT NULL");
      $ret[] = update_sql("ALTER TABLE {radioactivity} ADD COLUMN class varchar(7) NULL AFTER id");
      $ret[] = update_sql("UPDATE {radioactivity} SET class='node'");
      $ret[] = update_sql("ALTER TABLE {radioactivity} MODIFY COLUMN class varchar(7) NOT NULL");
      $ret[] = update_sql("ALTER TABLE {radioactivity} ADD PRIMARY KEY (id, class, decay_profile)");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {radioactivity} DROP CONSTRAINT radioactivity_pkey");
      $ret[] = update_sql("ALTER TABLE {radioactivity} RENAME COLUMN nid TO id");
      $ret[] = update_sql("ALTER TABLE {radioactivity} ADD COLUMN class varchar(7) NULL");
      $ret[] = update_sql("UPDATE {radioactivity} SET class='node'");
      $ret[] = update_sql("ALTER TABLE {radioactivity} ALTER COLUMN class SET NOT NULL");
      $ret[] = update_sql("ALTER TABLE {radioactivity} ADD PRIMARY KEY (id, class, decay_profile)");
      break;
  }

  // update decay profiles
  $profiles = variable_get('radioactivity_profiles', NULL);
  $new_profiles = array();
  if (is_array($profiles)) {
    foreach ($profiles as $dpid => $profile) {
      $new_profiles[$dpid] = _radioactivity_upgrade_profile($profile);
      $ret[] = array(
        'success' => TRUE,
        'query' => t('Profile %label (id=%id) upgraded', array(
          '%label' => $new_profiles[$dpid]['label'],
          '%id' => $dpid,
        )),
      );
    }
    variable_set('radioactivity_profiles', $new_profiles);
  }

  // enable radioactivity_node
  module_rebuild_cache();
  module_list(TRUE, FALSE);
  drupal_install_modules(array(
    'radioactivity_node',
  ));
  $ret[] = array(
    'success' => TRUE,
    'query' => t('Installed module %m', array(
      '%m' => 'radioactivity_node',
    )),
  );
  $ret[] = update_sql("INSERT INTO {radioactivity_node_clicks} (nid,uid,remote_address,click_timestamp) " . "SELECT nid,uid,remote_address,click_timestamp FROM {radioactivity_clicks}");
  $ret[] = update_sql("DROP TABLE {radioactivity_clicks}");
  return $ret;
}
function _radioactivity_upgrade_profile($profile) {
  $ret = array();
  if (isset($profile['label'])) {
    $ret['label'] = $profile['label'];
  }
  if (isset($profile['description'])) {
    $ret['description'] = $profile['description'];
  }
  if (isset($profile['half_life'])) {
    $ret['half_life'] = $profile['half_life'];
  }
  if (isset($profile['cut_off_energy'])) {
    $ret['cut_off_energy'] = $profile['cut_off_energy'];
  }
  $ret['energy']['node'] = array(
    'view' => $profile['energy_view'],
    'comment_insert' => $profile['energy_comment_insert'],
    'comment_publish' => $profile['energy_comment_publish'],
  );
  return $ret;
}
function _radioactivity_upgrade_view_get_legacy_dpid_from_field($field) {
  $matches = array();
  if (preg_match('@^radioactivity_(\\d+)\\.energy$@', $field, $matches)) {
    return $matches[1];
  }
  else {
    return FALSE;
  }
}
function _radioactivity_upgrade_view_get_legacy_dpid_from_tablename($tablename) {
  $matches = array();
  if (preg_match('@^radioactivity_(\\d+)$@', $tablename, $matches)) {
    return $matches[1];
  }
  else {
    return FALSE;
  }
}
function _radioactivity_upgrade_view($view) {
  $upgraded = FALSE;

  // upgrade sorts when necessary
  foreach (array_keys($view->sort) as $sortkey) {
    if ($dpid = _radioactivity_upgrade_view_get_legacy_dpid_from_field($view->sort[$sortkey]['id'])) {
      $view->sort[$sortkey]['field'] = 'radioactivity_node_' . $dpid . '_s.energy';
      $view->sort[$sortkey]['options'] = 'radioactive';
      $upgraded = TRUE;
    }
  }

  // upgrade tablefields when necessary
  foreach (array_keys($view->field) as $fieldkey) {
    if ($dpid = _radioactivity_upgrade_view_get_legacy_dpid_from_tablename($view->field[$fieldkey]['tablename'])) {
      $view->field[$fieldkey]['tablename'] = 'radioactivity_node_' . $dpid . '_f';
      $upgraded = TRUE;
    }
  }

  // upgrade filters when necessary
  foreach (array_keys($view->filter) as $filterkey) {
    if ($dpid = _radioactivity_upgrade_view_get_legacy_dpid_from_field($view->filter[$filterkey]['field'])) {
      $view->filter[$filterkey]['field'] = 'radioactivity_node_' . $dpid . '_c.energy';
      $upgraded = TRUE;
    }
  }
  if ($upgraded) {
    return $view;
  }
  else {
    return FALSE;
  }
}
function radioactivity_update_2() {
  $ret = array();

  // views update
  if (module_exists('views')) {
    views_invalidate_cache();
    $ret[] = array(
      'success' => TRUE,
      'query' => t('Views cache cleared'),
    );
    $view_upgraded = FALSE;

    // list views
    $result = db_query("SELECT vid, name FROM {view_view} ORDER BY NAME");
    while ($row = db_fetch_object($result)) {

      // get the view object
      $view = views_load_view($row->vid);
      $view = _radioactivity_upgrade_view($view);
      if ($view) {
        $success = _views_save_view($view) == $row->vid;
        $ret[] = array(
          'success' => $success,
          'query' => t('Upgraded view %name(%vid)', array(
            '%name' => $row->name,
            '%vid' => $row->vid,
          )),
        );
        $view_upgraded = TRUE;
      }
    }
    if (!$view_upgraded) {
      $ret[] = array(
        'success' => TRUE,
        'query' => t('No views needed upgrading'),
      );
    }
  }
  else {
    $ret[] = array(
      'success' => TRUE,
      'query' => t('Views not enabled, so skipping views upgrade'),
    );
  }
  return $ret;
}