<?php
// $Id: versioncontrol_project.install,v 1.21 2009/02/14 14:19:51 jpetso Exp $
/**
 * @file
 * Version Control / Project Node Integration - Integrates project nodes
 * (provided by the Project module) with version control systems supported
 * by the Version Control API.
 *
 * Copyright 2006 by Karthik ("Zen", http://drupal.org/user/21209)
 * Copyright 2006, 2007 by Derek Wright ("dww", http://drupal.org/user/46549)
 * Copyright 2007 by Jakob Petsovits ("jpetso", http://drupal.org/user/56020)
 */

/**
 * Implementation of hook_schema().
 */
function versioncontrol_project_schema() {
  $schema['versioncontrol_project_projects'] = array(
    'description' => 'This table associates a project (given as node id) with a directory location in a version controlled repository.',
    'fields' => array(
      'nid' => array(
        'description' => 'The {node}.nid identifier of the project node.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'repo_id' => array(
        'description' => 'The {versioncontrol_repositories}.repo_id identifier of the repository where the project directory is located.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'directory' => array(
        'description' => 'The path of the project directory relative to the repository root, in the same format as {versioncontrol_item_revisions}.path (which means it starts with a slash and does not end with one).',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
    ),
    'indexes' => array(
      'repo_id_directory' => array('repo_id', 'directory'),
    ),
    'primary key' => array('nid'),
  );

  $schema['versioncontrol_project_comaintainers'] = array(
    'description' => 'The list of project co-maintainers. The actual maintainer is the node owner and not stored in here, (s)he will be retrieved from {node}.uid rather than from this table.',
    'fields' => array(
      'nid' => array(
        'description' => 'The {node}.nid identifier of the project node where the user in {versioncontrol_project_comaintainers}.uid is listed as co-maintainer.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'uid' => array(
        'description' => 'The Drupal user id (referring to {users}.uid) of the co-maintainer.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('nid', 'uid'),
  );

  $schema['versioncontrol_project_items'] = array(
    'description' => 'An association of repository items in {versioncontrol_item_revisions} to projects in {versioncontrol_project_projects}.',
    'fields' => array(
      'item_revision_id' => array(
        'description' => 'The {versioncontrol_item_revisions}.item_revision_id identifier that belongs to the project.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'description' => 'The {node}.nid identifier of the project node that the item belongs to. VERSIONCONTROL_PROJECT_NID_NONE (== 0) for items which do not belong to any project. VERSIONCONTROL_PROJECT_NID_NONE and actual (positive) nids are mutually exclusive for any given item_revision_id.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('item_revision_id', 'nid'),
  );

  $schema['versioncontrol_project_operations'] = array(
    'description' => 'An association of operations in {versioncontrol_operations} to projects in {versioncontrol_project_projects}. This is just a cache built out of {versioncontrol_project_items}, for more efficient joins in repository queries, and unlike {versioncontrol_project_items} does not contain items with nid == VERSIONCONTROL_PROJECT_NID_NONE.',
    'fields' => array(
      'vc_op_id' => array(
        'description' => 'The {versioncontrol_operations}.vc_op_id identifier that is related to the project.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'description' => 'The {node}.nid identifier of the project node that the operation took place in.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('vc_op_id', 'nid'),
  );

  return $schema;
}

/**
 * Implementation of hook_install().
 */
function versioncontrol_project_install() {
  // Create tables.
  drupal_install_schema('versioncontrol_project');
}

/**
 * Implementation of hook_enable().
 */
function versioncontrol_project_enable() {
  require_once(drupal_get_path('module', 'versioncontrol_project') .'/versioncontrol_project.module');

  // Check to make sure all existing projects are entered in the
  // {versioncontrol_project_projects} table, and add if not.
  $vc_projects = array();
  $projects = array();
  $current_vc_projects = db_query("SELECT nid FROM {versioncontrol_project_projects}");
  while ($vc_project = db_fetch_object($current_vc_projects)) {
    $vc_projects[] = $vc_project->nid;
  }
  $projects = db_query("SELECT nid FROM {node} WHERE type = 'project_project'");
  while ($project = db_fetch_array($projects)) {
    if (!in_array($project['nid'], $vc_projects)) {
      $project['repo_id'] = 0;
      versioncontrol_project_set_project($project);
    }
  }

  _versioncontrol_project_write_missing_item_associations();
}

/**
 * Implementation of hook_uninstall().
 */
function versioncontrol_project_uninstall() {
  // Remove variables.
  db_query("DELETE FROM {variable} WHERE name LIKE 'versioncontrol_project_directory_tid_%'");
  $variables = array(
    'versioncontrol_project_restrict_commits',
    'versioncontrol_project_restrict_creation',
    'versioncontrol_project_dir_validate_by_type',
    'versioncontrol_project_validate_by_short_name',
  );
  foreach ($variables as $variable) {
    variable_del($variable);
  }

  // Remove tables.
  drupal_uninstall_schema('versioncontrol_project');
}


// Update functions. To be named versioncontrol_project_update_xyzz(), where x
// is the major version of Drupal core, y is the major version of this module
// for this version of Drupal core, and zz is a consecutive number.

// versioncontrol_project_update_2() was the last update on Drupal 5.x (-2.x).

/**
 * Update 6100: Add the project/item association table and the related
 *   project/operation association cache built out of the former.
 */
function versioncontrol_project_update_6100() {
  $ret = array();

  $project_item_table = array(
    'fields' => array(
      'item_revision_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('item_revision_id', 'nid'),
  );
  $project_operations_table = array(
    'fields' => array(
      'vc_op_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'nid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('vc_op_id', 'nid'),
  );

  db_create_table($ret, 'versioncontrol_project_items', $project_item_table);
  db_create_table($ret, 'versioncontrol_project_operations', $project_operations_table);

  require_once(drupal_get_path('module', 'versioncontrol_project') .'/versioncontrol_project.module');
  _versioncontrol_project_write_missing_item_associations();
  $ret[] = array(
    'success' => TRUE,
    'query' => t('Countless queries (probably), inserting project associations into {versioncontrol_project_items} and {versioncontrol_project_operations}.'),
  );

  return $ret;
}
