You are here

og.install in Organic groups 5

File

og.install
View source
<?php

function og_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {og} (\n        nid int(11) NOT NULL,\n        selective int(11) NOT NULL default '0',\n        description varchar(255) NULL,\n        theme varchar(255) NULL,\n        register int(1) NOT NULL default 0,\n        directory int(1) NOT NULL default 0,\n        notification int(1) NOT NULL default 0,\n        language varchar(12) NOT NULL default '',\n        private int(1) NOT NULL default 0,\n        PRIMARY KEY  (nid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {og_uid} (\n        nid int(11) NOT NULL,\n        og_role int(1) NOT NULL DEFAULT 0,\n        is_active int(1) NOT NULL DEFAULT 0,\n        is_admin int(1) NOT NULL DEFAULT 0,\n        uid int(11) NOT NULL,\n        mail_type int(11) NULL,\n      \tcreated int(11) NULL DEFAULT 0,\n      \tchanged int(11) NULL DEFAULT 0,\n        PRIMARY KEY  (nid, uid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {og_uid_global} (\n        uid int(11) NOT NULL,\n        og_email int(11) NOT NULL DEFAULT 2,\n        PRIMARY KEY  (uid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {og_ancestry} (\n        nid int(11) NOT NULL,\n        group_nid int(11) NOT NULL,\n        is_public int(1) NOT NULL,\n        KEY  (nid),\n        KEY  (group_nid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {og} (\n        nid int NOT NULL,\n        selective int NOT NULL default 0,\n        description varchar(255) NULL,\n        theme varchar(255) NULL,\n        website varchar(255) NULL,\n        register smallint NOT NULL default 0,\n        directory smallint NOT NULL default 0,\n        notification smallint NOT NULL default 0,\n        language varchar(12) NOT NULL default '',\n        private smallint NOT NULL default 0,  \n        PRIMARY KEY  (nid)\n      );");
      db_query("CREATE TABLE {og_uid} (\n        nid int NOT NULL,\n        og_role smallint NOT NULL DEFAULT 0,\n        is_active smallint NOT NULL DEFAULT 0,\n        is_admin smallint NOT NULL DEFAULT 0,\n        uid int NOT NULL,\n        mail_type int NULL,\n\t    created int NULL DEFAULT 0,\n\t    changed int NULL DEFAULT 0,\n        PRIMARY KEY  (nid, uid)\n      );");
      db_query("CREATE TABLE {og_uid_global} (\n        uid int NOT NULL,\n        og_email int NOT NULL DEFAULT 2,\n        PRIMARY KEY (uid)\n      );");
      db_query("\n      CREATE TABLE {og_ancestry} (\n       nid int NOT NULL,\n       group_nid int NOT NULL,\n       is_public smallint NOT NULL\n      );");
      db_query("CREATE INDEX {og_ancestry}_nid_idx ON {og_ancestry} (nid);");
      db_query("CREATE INDEX {og_ancestry}_group_nid_idx ON {og_ancestry} (group_nid);");
      break;
  }

  // enable standard og blocks. for custom profiles, block may not be included yet.
  if (function_exists('block_rehash')) {
    _block_rehash();
  }

  // Need og_readme() function.
  drupal_load('module', 'og');
  drupal_set_message(t('Organic groups module enabled. Please see the included !README file for further installation instructions.', array(
    '!README' => og_readme(),
  )));
}

// 2006-05-31 PFM: deletes row if present in the table.  The first parameter
// is a table, the next two are arrays of fields and values
// respectively.  Two values are supported.
function og_deleteifpresent_2keys($sTable, $aField, $aReplace, $aValue) {
  $sqlExist = "select count(*) as nCount " . "from " . $sTable . " where " . $aField[0] . " = " . $aReplace[0] . " and " . $aField[1] . " = " . $aReplace[1];
  $resExist = db_query($sqlExist, $aValue[0], $aValue[1]);
  $objExist = db_fetch_object($resExist);
  if ($objExist->nCount > 0) {

    // found.  delete before inserting below.
    $sqlExist = "delete from " . $sTable . " where " . $aField[0] . " = " . $aReplace[0] . " and " . $aField[1] . " = " . $aReplace[1];
    db_query($sql, $aValue[0], $aValue[1]);
  }
}

// move subscriber data to own table from node_access
function og_update_1() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("ALTER TABLE {og_uid} ADD `og_role` int(1) NOT NULL default '0'");
      db_query("ALTER TABLE {og_uid} ADD `is_active` int(1) default '0'");
      db_query("ALTER TABLE {og_uid} ADD `is_admin` int(1) default '0'");
      break;
    case 'pgsql':
      db_query("ALTER TABLE {og_uid} ADD `og_role` smallint NOT NULL default 0");
      db_query("ALTER TABLE {og_uid} ADD `is_active` smallint default 0");
      db_query("ALTER TABLE {og_uid} ADD `is_admin` smallint default 0");
      break;
  }
  $result = db_query("SELECT * FROM {node_access} WHERE realm = 'og_uid'");
  while ($object = db_fetch_object($result)) {
    og_deleteifpresent_2keys('og_uid', array(
      'nid',
      'gid',
    ), array(
      '%d',
      '%d',
    ), array(
      $object->nid,
      $object->gid,
    ));

    // insert new row.
    $sql = "INSERT INTO {og_uid} (nid, uid, og_role, is_admin, is_active) VALUES (%d, %d, %d, %d, %d)";
    db_query($sql, $object->nid, $object->gid, $object->grant_view + $object->grant_update, $object->grant_update, $object->grant_view);
  }
  $sql = "DELETE FROM {node_access} WHERE realm = 'og_uid'";
  db_query($sql);
  return array();
}
function og_update_2() {
  return _system_update_utf8(array(
    'og',
    'og_uid',
  ));
}
function og_update_3() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
    case 'pgsql':
      $sql = "DELETE FROM {node_access} WHERE realm = 'og_uid'";
      db_query($sql);
      $sql = "SELECT DISTINCT(n.nid) FROM {node} n INNER JOIN {node_access} na ON n.nid = na.nid WHERE type != 'og' AND na.realm = 'og_group'";
      $result = db_query($sql);
      while ($row = db_fetch_object($result)) {
        $sql = "UPDATE {node_access} SET grant_view=1, grant_update=1, grant_delete=1 WHERE realm = 'og_group' AND nid = %d AND gid != 0";
        db_query($sql, $row->nid);
      }
      $sql = "SELECT nid FROM {node} WHERE type = 'og'";
      $result = db_query($sql);
      while ($row = db_fetch_object($result)) {
        og_deleteifpresent_2keys('node_access', array(
          'nid',
          'gid',
        ), array(
          '%d',
          '%d',
        ), array(
          $object->nid,
          $object->gid,
        ));
        $sql = "INSERT INTO {node_access} (nid, gid, realm, grant_view, grant_update, grant_delete) VALUES (%d, %d, 'og_group', 1, 1, 0)";
        db_query($sql, $row->nid, $row->nid);
      }
  }
  return array();
}

/**
 * mar 9,2006. 
 * unfortunately, we need duplicates so we change primary key to regular INDEX.
 */
function og_update_4() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('ALTER TABLE {node_access} DROP PRIMARY KEY, ADD INDEX nid_gid_realm (nid, gid, realm)');
  }
  return $ret ? $ret : array();
}

/**
 * feb 19, 2006
 * add a row for each combination of public node and group. needed to 
 * make public nodes show up in group homepage for non subscribers
 */
function og_update_5() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
    case 'pgsql':
      $sql = "SELECT DISTINCT(nid) as nid FROM {node_access} WHERE realm = 'og_group' AND gid = 0";
      $result = db_query($sql);
      while ($row = db_fetch_object($result)) {
        $sql = "SELECT gid FROM {node_access} WHERE nid = %d AND realm = 'og_group' AND gid != 0";
        $result2 = db_query($sql, $row->nid);
        while ($row2 = db_fetch_object($result2)) {
          og_deleteifpresent_2keys('node_access', array(
            'nid',
            'gid',
          ), array(
            '%d',
            '%d',
          ), array(
            $row->nid,
            $row->gid,
          ));
          $sql = "INSERT INTO {node_access} (nid, realm, gid, grant_view) VALUES (%d, 'og_public', 0, %d)";
          db_query($sql, $row->nid, $row2->gid);
        }
      }

      // change all former public node grants to 'og_all' realm
      $sql = "UPDATE {node_access} SET realm = 'og_all' WHERE realm = 'og_group' AND gid = 0 AND grant_view = 1";
      db_query($sql);

      // change all nodes in groups to new 'og_subscriber' realm
      $sql = "UPDATE {node_access} SET realm = 'og_subscriber' WHERE realm = 'og_group' AND gid != 0";
      db_query($sql);

      // these records are no longer used. we've migrated them to new grant scheme
      $sql = "DELETE FROM {node_access} WHERE realm = 'og_group'";
      db_query($sql);
  }
  return array();
}
function og_update_6() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('ALTER TABLE {og} ADD website varchar(255) NULL AFTER theme');
  }
  return $ret ? $ret : array();
}

// we store a nid in grant_view column in the og_public realm so we need high numbers
function og_update_7() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {node_access} CHANGE grant_view grant_view int(11) unsigned NOT NULL default '0'");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {node_access} " . "ALTER COLUMN grant_view TYPE int, " . "ALTER COLUMN grant_view SET NOT NULL, " . "ALTER COLUMN grant_view SET DEFAULT 0;");
      break;
  }
  return $ret ? $ret : array();
}

// Enable og_basic.module by default.
function og_update_8() {

  // do nothing. it turns out you can't easily enable a module here because its row does not exist yet in system table.
  return array();
}

// Group language
function og_update_9() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {og} ADD language varchar(12) NOT NULL default ''");
  }
  return $ret ? $ret : array();
}

// Created and Updated dates
function og_update_10() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {og_uid} ADD created int(11) NULL DEFAULT 0");
      $ret[] = update_sql("ALTER TABLE {og_uid} ADD changed int(11) NULL DEFAULT 0");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {og_uid} ADD COLUMN created int NULL DEFAULT 0");
      $ret[] = update_sql("ALTER TABLE {og_uid} ADD COLUMN changed int NULL DEFAULT 0");
      break;
  }
  return $ret ? $ret : array();
}

// notification flag for a group
function og_update_11() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {og} ADD notification int(1) NOT NULL default 0");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {og} ADD COLUMN notification smallint NOT NULL default 0");
      break;
  }
  return $ret ? $ret : array();
}

// remove crufty image field. we now use image_attach.module fom image.module package instead.
function og_update_12() {

  // works for mysql and postgres
  $ret[] = update_sql("ALTER TABLE {og} DROP image");
  return $ret;
}

// populate the og_uid_global table. we have to choose selective here for backward compatibility.
// to set everyone up for 'always' notifications, run this after the update: UPDATE og_uid_global SET og_email=1
function og_update_13() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {og_uid_global} (\n        uid int(11) NOT NULL,\n        og_email int(11) NOT NULL DEFAULT 2,\n        PRIMARY KEY  (uid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {og_uid_global} (\n        uid int NOT NULL,\n        og_email int NOT NULL DEFAULT 2,\n        PRIMARY KEY (uid)\n      );");
      break;
  }

  // works for mysql and postgres
  $sql = 'SELECT uid FROM {users} WHERE uid > 0';
  $result = db_query($sql);
  while ($row = db_fetch_object($result)) {
    $sql = "INSERT INTO {og_uid_global} (uid, og_email) VALUES (%d, %d)";
    db_query($sql, $row->uid, OG_NOTIFICATION_SELECTIVE);
  }
  return array();
}

// use the new na_arbitrator way of writing to node_access table
function og_update_14() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("CREATE TABLE {og_ancestry} (\n        nid int(11) NOT NULL,\n        group_nid int(11) NOT NULL,\n        is_public int(1) NULL,\n        KEY  (nid),\n        KEY  (group_nid)\n      ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {og_ancestry} (\n        nid int NOT NULL,\n        group_nid int NOT NULL,\n        is_public smallint NOT NULL\n      );");
      db_query("CREATE INDEX {og_ancestry}_nid_idx ON {og_ancestry} (nid);");
      db_query("CREATE INDEX {og_ancestry}_group_nid_idx ON {og_ancestry} (group_nid);");
      break;
  }
  og_migrate_type_basic_14();

  // populate og_ancestry.
  $result = db_query_temporary("SELECT na.nid, na.gid, IF(MIN(na.realm) = 'og_all', 1, 0) AS is_public \n     FROM {node_access} na INNER JOIN {node} n ON na.nid=n.nid \n     WHERE realm IN ('og_all', 'og_subscriber') AND n.type NOT IN ('%s') GROUP BY na.nid, na.gid ORDER BY nid ASC", implode(', ', variable_get('og_node_types', array(
    'og',
  ))), 'og_migrate');
  $sql = "INSERT INTO {og_ancestry} (nid, group_nid, is_public) SELECT nid, gid, is_public FROM {og_migrate}";
  db_query($sql);

  // rebuild takes care of writing new access records
  // too bad this part can't be performed over multiple updates
  node_access_rebuild();
  return array();
}

// Create a user managed node type which replaces the deprecated og_basic module
// helper function for og_update_14
function og_migrate_type_basic_14() {
  if (og_is_group_type('og')) {
    $info = array(
      'type' => 'og',
      'name' => 'group',
      'module' => 'node',
      'has_title' => 1,
      'title_label' => 'Group name',
      'has_body' => 1,
      'body_label' => 'Welcome message',
      'description' => 'A group provides a home page for like minded users. There they post articles about their shared interest.',
      'help' => '',
      'min_word_count' => 0,
      'custom' => 1,
      'modified' => 1,
      'locked' => 0,
      'orig_type' => 'og',
    );
    node_type_save((object) $info);
    module_disable(array(
      'og_basic',
    ));
    node_types_rebuild();
  }
}
function og_update_15() {
  variable_del('og_max_posts');
  variable_del('og_home_page_presentation');
  return array();
}
function og_update_16() {

  // we are no longer denying access to nodes without groups. see http://drupal.org/node/107289
  if (variable_get('og_enabled', 0)) {
    node_access_rebuild();
  }
  return array();
}
function og_update_17() {

  // we are once again putting group nodes into the node access system. see http://drupal.org/node/128306
  if (variable_get('og_enabled', 0)) {
    node_access_rebuild();
  }
  return array();
}
function og_update_18() {

  // woops. got it a bit wrong last time
  if (variable_get('og_enabled', 0)) {
    node_access_rebuild();
  }
  return array();
}
function og_update_19() {
  og_populate_uid_global_table();
  return array(
    'Populated og_uid_global table',
  );
}
function og_update_20() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("ALTER TABLE {og} ADD private int(1) NOT NULL default 0");
      break;
    case 'pgsql':
      $ret[] = update_sql("ALTER TABLE {og} ADD private smallint NOT NULL default 0");
      break;
  }
  return $ret;
}

/**
 * Changes '@body' to '@node_teaser' in existing e-mail templates.
 */
function og_update_5600() {
  $ret = array();
  $variables = array(
    'og_new_node_body',
    'og_new_node_subject',
  );
  foreach ($variables as $variable) {
    $value = variable_get($variable, '');
    if (!empty($value)) {
      variable_set($variable, str_replace('@body', '@node_teaser', $value));
      $ret[] = array(
        'success' => TRUE,
        'query' => "Replaced @body with @node_teaser in {$variable}",
      );
    }
  }
  return $ret;
}

/**
 * Enable og_access module if needed
 */
function og_update_5700() {
  $ret = array();
  if (variable_get('og_enabled', FALSE) && !module_exists('og_access')) {
    module_enable('og_access');
  }

  // variable_del('og_enabled'); is harmless to leave around, and will help if someone reruns the update.
  return $ret;
}

/**
 * Update variables to new content type system. Also rebuild node_access for new grants system.
 * 
 * @return void
 **/
function og_update_5701() {
  $ret = array();
  $types = node_get_types();
  foreach ($types as $type) {
    if (in_array($type->type, variable_get('og_node_types', array(
      'og',
    )))) {
      variable_set('og_content_type_usage_' . $type->type, 'group');
    }
    elseif (in_array($type->type, variable_get('og_omitted', array(
      'og',
    )))) {
      variable_set('og_content_type_usage_' . $type->type, 'omitted');
    }
    elseif (in_array($type->type, variable_get('og_omitted_email_node_types', array(
      'og',
    )))) {
      variable_set('og_content_type_usage_' . $type->type, 'group_post_standard_nomail');
    }
    else {
      variable_set('og_content_type_usage_' . $type->type, 'group_post_standard_mail');
    }
  }
  node_access_rebuild();
  return $ret;
}

/**
 * Force menu rebuild since subscribe/unsubscribe paths are better protected now.
 */
function og_update_5702() {
  $ret[] = array(
    'success' => TRUE,
    'query' => 'Menu has been rebuilt.',
  );
  menu_rebuild();
  return $ret;
}

// end updates //
function og_enable() {
  og_populate_uid_global_table();
}

// need to populate og_uid_global for existing users on enable
// this is called from update 13 & 19 & og_enable.
// TODO: conditionally use subquery
function og_populate_uid_global_table() {
  $sql = 'SELECT u.uid FROM {users} u LEFT JOIN {og_uid_global} oug ON u.uid=oug.uid WHERE u.uid > 0 AND oug.og_email IS NULL';
  $result = db_query($sql);
  while ($row = db_fetch_object($result)) {
    $sql = "INSERT INTO {og_uid_global} (uid, og_email) VALUES (%d, %d)";
    db_query($sql, $row->uid, OG_NOTIFICATION_SELECTIVE);
  }
}
function og_uninstall() {

  // Drop database table
  db_query('DROP TABLE {og}');
  db_query('DROP TABLE {og_uid}');
  db_query('DROP TABLE {og_uid_global}');
  db_query('DROP TABLE {og_ancestry}');

  // Delete variables
  $variables = array(
    'og_member_pics',
    'og_help',
    'og_block_cnt_2',
    'og_block_cnt_3',
    'og_audience_checkboxes',
    'og_omitted',
    'og_content_type_usage',
    'og_audience_required',
    'og_omitted_email_node_types',
    'og_visibility_directory',
    'og_visibility_registration',
    'og_home_page_view',
    'og_email_max',
    'og_node_types',
    'og_admin_email_body',
    'og_email_notification_pattern',
    'og_approve_user_body',
    'og_approve_user_subject',
    'og_deny_user_body',
    'og_deny_user_subject',
    'og_invite_user_body',
    'og_invite_user_subject',
    'og_new_admin_body',
    'og_new_admin_subject',
    'og_new_node_body',
    'og_new_node_subject',
    'og_notification',
    'og_request_user_body',
    'og_request_user_subject',
  );
  foreach ($variables as $variable) {
    variable_del($variable);
  }
}

Functions

Namesort descending Description
og_deleteifpresent_2keys
og_enable
og_install
og_migrate_type_basic_14
og_populate_uid_global_table
og_uninstall
og_update_1
og_update_10
og_update_11
og_update_12
og_update_13
og_update_14
og_update_15
og_update_16
og_update_17
og_update_18
og_update_19
og_update_2
og_update_20
og_update_3
og_update_4 mar 9,2006. unfortunately, we need duplicates so we change primary key to regular INDEX.
og_update_5 feb 19, 2006 add a row for each combination of public node and group. needed to make public nodes show up in group homepage for non subscribers
og_update_5600 Changes '@body' to '@node_teaser' in existing e-mail templates.
og_update_5700 Enable og_access module if needed
og_update_5701 Update variables to new content type system. Also rebuild node_access for new grants system.
og_update_5702 Force menu rebuild since subscribe/unsubscribe paths are better protected now.
og_update_6
og_update_7
og_update_8
og_update_9