diff --git a/sites/all/modules/unl/cron.php b/sites/all/modules/unl/cron.php index 956b5436ae71c26dfa14e69486970a35b7337fcd..a4e417593a42cc36bd52f246570eb4657bf31f5b 100644 --- a/sites/all/modules/unl/cron.php +++ b/sites/all/modules/unl/cron.php @@ -12,44 +12,125 @@ require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; drupal_override_server_variables(); drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); -$query = db_query('SELECT * FROM {unl_sites} WHERE installed=0'); -while ($row = $query->fetchAssoc()) { - db_update('unl_sites') - ->fields(array('installed' => 1)) - ->condition('site_id', $row['site_id']) - ->execute(); - if (unl_add_site($row['site_path'], $row['uri'], $row['clean_url'], $row['db_prefix'], $row['site_id'])) { +unl_remove_aliases(); +unl_remove_sites(); +unl_add_sites(); +unl_add_aliases(); + +function unl_add_sites() { + $query = db_query('SELECT * FROM {unl_sites} WHERE installed=0'); + + while ($row = $query->fetchAssoc()) { db_update('unl_sites') - ->fields(array('installed' => 2)) + ->fields(array('installed' => 1)) ->condition('site_id', $row['site_id']) ->execute(); + if (unl_add_site($row['site_path'], $row['uri'], $row['clean_url'], $row['db_prefix'], $row['site_id'])) { + db_update('unl_sites') + ->fields(array('installed' => 2)) + ->condition('site_id', $row['site_id']) + ->execute(); + } + else { + db_update('unl_sites') + ->fields(array('installed' => 5)) + ->condition('site_id', $row['site_id']) + ->execute(); + } } - else { +} + +function unl_remove_sites() { + $query = db_query('SELECT * FROM {unl_sites} WHERE installed=3'); + while ($row = $query->fetchAssoc()) { db_update('unl_sites') - ->fields(array('installed' => 5)) + ->fields(array('installed' => 4)) ->condition('site_id', $row['site_id']) ->execute(); + if (unl_remove_site($row['site_path'], $row['uri'], $row['db_prefix'], $row['site_id'])) { + db_delete('unl_sites') + ->condition('site_id', $row['site_id']) + ->execute(); + } + else { + db_update('unl_sites') + ->fields(array('installed' => 5)) + ->condition('site_id', $row['site_id']) + ->execute(); + } } } -$query = db_query('SELECT * FROM {unl_sites} WHERE installed=3'); -while ($row = $query->fetchAssoc()) { - db_update('unl_sites') - ->fields(array('installed' => 4)) - ->condition('site_id', $row['site_id']) - ->execute(); - if (unl_remove_site($row['site_path'], $row['uri'], $row['db_prefix'], $row['site_id'])) { - db_delete('unl_sites') - ->condition('site_id', $row['site_id']) +function unl_add_aliases() { + $query = db_select('unl_sites_aliases', 'a'); + $query->join('unl_sites', 's', 's.site_id = a.site_id'); + $query->fields('s', array('uri')); + $query->fields('a', array('site_alias_id', 'uri')); + $query->condition('a.installed', 0); + $results = $query->execute()->fetchAll(); + + foreach ($results as $row) { + db_update('unl_sites_aliases') + ->fields(array('installed' => 1)) + ->condition('site_alias_id', $row->site_alias_id) ->execute(); + if (unl_add_alias($row->uri, $row->a_uri)) { + db_update('unl_sites_aliases') + ->fields(array('installed' => 2)) + ->condition('site_alias_id', $row->site_alias_id) + ->execute(); + } + else { + db_update('unl_sites_aliases') + ->fields(array('installed' => 5)) + ->condition('site_alias_id', $row->site_alias_id) + ->execute(); + } } - else { - db_update('unl_sites') - ->fields(array('installed' => 5)) - ->condition('site_id', $row['site_id']) +} + +function unl_remove_aliases() { + $query = db_select('unl_sites_aliases', 'a'); + $query->fields('a', array('site_alias_id', 'uri')); + $query->condition('a.installed', 3); + $results = $query->execute()->fetchAll(); + + foreach ($results as $row) { + db_update('unl_sites_aliases') + ->fields(array('installed' => 4)) + ->condition('site_alias_id', $row->site_alias_id) ->execute(); + if (unl_remove_alias($row->uri)) { + db_delete('unl_sites_aliases') + ->condition('site_alias_id', $row->site_alias_id) + ->execute(); + } + else { + db_update('unl_sites_aliases') + ->fields(array('installed' => 5)) + ->condition('site_alias_id', $row->site_alias_id) + ->execute(); + } + } +} + + +function _unl_get_sites_subdir($uri, $trim_subdomain = TRUE) { + $path_parts = parse_url($uri); + if ($trim_subdomain && substr($path_parts['host'], -7) == 'unl.edu') { + $path_parts['host'] = 'unl.edu'; + } + $sites_subdir = $path_parts['host'] . $path_parts['path']; + $sites_subdir = strtr($sites_subdir, array('/' => '.')); + + while (substr($sites_subdir, 0, 1) == '.') { + $sites_subdir = substr($sites_subdir, 1); + } + while (substr($sites_subdir, -1) == '.') { + $sites_subdir = substr($sites_subdir, 0, -1); } + return $sites_subdir; } @@ -155,18 +236,16 @@ function unl_remove_site($site_path, $uri, $db_prefix, $site_id) { return TRUE; } -function _unl_get_sites_subdir($uri) { - $path_parts = parse_url($uri); - if (substr($path_parts['host'], -7) == 'unl.edu') { - $path_parts['host'] = 'unl.edu'; - } - $sites_subdir = $path_parts['host'] . $path_parts['path']; - $sites_subdir = strtr($sites_subdir, array('/' => '.')); - - return $sites_subdir; +function unl_add_alias($site_uri, $alias_uri) { + $real_config_dir = _unl_get_sites_subdir($site_uri); + $alias_config_dir = _unl_get_sites_subdir($alias_uri, FALSE); + return symlink($real_config_dir, DRUPAL_ROOT . '/sites/' . $alias_config_dir); } - +function unl_remove_alias($alias_uri) { + $alias_config_dir = _unl_get_sites_subdir($alias_uri, FALSE); + return unlink(DRUPAL_ROOT . '/sites/' . $alias_config_dir); +} diff --git a/sites/all/modules/unl/unl.install b/sites/all/modules/unl/unl.install index 5ca132fe6ae1202721dab5531969436c803a835a..ca5a547999ba26bdfe8d56031756f441018d6cf0 100644 --- a/sites/all/modules/unl/unl.install +++ b/sites/all/modules/unl/unl.install @@ -42,7 +42,43 @@ function unl_schema() { 'primary key' => array('site_id'), 'unique keys' => array( 'sub_site' => array('site_path'), - ) + ), + ); + + $schema['unl_sites_aliases'] = array( + 'description' => 'Table of URL aliases for UNL sites.', + 'fields' => array( + 'site_alias_id' => array( + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'site_id' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'uri' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + ), + 'installed' => array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), + ), + 'primary key' => array('site_alias_id'), + 'unique keys' => array( + 'alias_uri' => array('uri'), + ), + 'foreign keys' => array( + 'aliased_site' => array( + 'table' => 'unl_sites', + 'columns' => array('site_id' => 'site_id'), + ), + ), ); return $schema; @@ -112,3 +148,43 @@ function unl_update_7102() { db_query("UPDATE {unl_sites} SET site_path = CONCAT(site_path_prefix, '/', site_path) WHERE site_path_prefix != ''"); db_drop_field('unl_sites', 'site_path_prefix'); } + +function unl_update_7103() { + $table = array( + 'description' => 'Table of URL aliases for UNL sites.', + 'fields' => array( + 'site_alias_id' => array( + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'site_id' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + ), + 'uri' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + ), + 'installed' => array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + ), + ), + 'primary key' => array('site_alias_id'), + 'unique keys' => array( + 'alias_uri' => array('uri'), + ), + 'foreign keys' => array( + 'aliased_site' => array( + 'table' => 'unl_sites', + 'columns' => array('site_id' => 'site_id'), + ), + ), + ); + + db_create_table('unl_sites_aliases', $table); +} diff --git a/sites/all/modules/unl/unl.module b/sites/all/modules/unl/unl.module index 6eea4d43f23ec5c6854a094e5f708fadb020abc5..f1e0b4d78a1620fc72cc151aabb9fa37e70ce16b 100644 --- a/sites/all/modules/unl/unl.module +++ b/sites/all/modules/unl/unl.module @@ -105,27 +105,25 @@ function unl_menu() { if (conf_path() == 'sites/default') { $items['admin/sites/unl'] = array( 'title' => 'UNL Site Creation Tool', - 'description' => 'Create a new UNL Drupal site', + 'description' => 'Create and manage UNL Drupal sites and aliases.', 'access arguments' => array('unl site creation'), - 'page callback' => 'drupal_get_form', - 'page arguments' => array('unl_site_creation'), + 'page callback' => 'unl_sites_page', 'type' => MENU_LOCAL_TASK, 'file' => 'unl_site_creation.php', ); - $items['admin/sites/unl/add'] = array( - 'title' => 'Add', - 'description' => 'Determine access to features by selecting permissions for roles.', + $items['admin/sites/unl/sites'] = array( + 'title' => 'Sites', + 'description' => 'Create and manage UNL Drupal sites.', 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -8, ); - $items['admin/sites/unl/list'] = array( - 'title' => 'List', - 'description' => 'List UNL Drupal sites', + $items['admin/sites/unl/aliases'] = array( + 'title' => 'Aliases', + 'description' => 'Manage aliases of UNL Drupal sites.', 'access arguments' => array('unl site creation'), - 'page callback' => 'drupal_get_form', - 'page arguments' => array('unl_site_list'), + 'page callback' => 'unl_aliases_page', 'type' => MENU_LOCAL_TASK, 'file' => 'unl_site_creation.php', ); @@ -224,7 +222,10 @@ function unl_theme() { return array( 'unl_site_list_table' => array( 'render element' => 'form' - ) + ), + 'unl_alias_list_table' => array( + 'render element' => 'form' + ), ); } diff --git a/sites/all/modules/unl/unl_site_creation.php b/sites/all/modules/unl/unl_site_creation.php index 91986d22e142b3b389e87a5024460d8cb37eab88..3bb3319dc95be85d8178ac1e0f2c51fd63d9481d 100644 --- a/sites/all/modules/unl/unl_site_creation.php +++ b/sites/all/modules/unl/unl_site_creation.php @@ -2,10 +2,21 @@ require_once DRUPAL_ROOT . '/includes/install.core.inc'; -function unl_site_creation($form, &$form_state) { + + +function unl_sites_page() { + $page = array(); + $page[] = drupal_get_form('unl_site_create'); + $page[] = drupal_get_form('unl_site_list'); + + return $page; +} + + +function unl_site_create($form, &$form_state) { $form['root'] = array( '#type' => 'fieldset', - '#title' => 'Site Creation Tool', + '#title' => 'Create New Site', ); $form['root']['site_path'] = array( @@ -23,7 +34,7 @@ function unl_site_creation($form, &$form_state) { '#default_value' => 1, ); - $form['submit'] = array( + $form['root']['submit'] = array( '#type' => 'submit', '#value' => 'Create Site', ); @@ -31,7 +42,7 @@ function unl_site_creation($form, &$form_state) { return $form; } -function unl_site_creation_validate($form, &$form_state) { +function unl_site_create_validate($form, &$form_state) { $site_path = trim($form_state['values']['site_path']); if (substr($site_path, 0, 1) == '/') { @@ -50,7 +61,7 @@ function unl_site_creation_validate($form, &$form_state) { $form_state['values']['site_path'] = $site_path; } -function unl_site_creation_submit($form, &$form_state) { +function unl_site_create_submit($form, &$form_state) { $site_path = $form_state['values']['site_path']; $clean_url = $form_state['values']['clean_url']; @@ -77,8 +88,11 @@ function unl_site_creation_submit($form, &$form_state) { function unl_site_list($form, &$form_state) { $form['root'] = array( '#type' => 'fieldset', - '#title' => 'UNL Site List', - '#theme' => 'unl_site_list_table' + '#title' => 'Existing Sites', + ); + + $form['root']['site_list'] = array( + '#theme' => 'unl_site_list_table', ); $sites = db_select('unl_sites', 's') @@ -87,7 +101,7 @@ function unl_site_list($form, &$form_state) { ->fetchAll(); foreach ($sites as $site) { - $form['root'][$site->site_id] = array( + $form['root']['site_list'][$site->site_id] = array( 'site_path' => array('#value' => $site->site_path), 'db_prefix' => array('#value' => $site->db_prefix . '_' . $GLOBALS['databases']['default']['default']['prefix']), 'installed' => array('#value' => $site->installed), @@ -100,12 +114,11 @@ function unl_site_list($form, &$form_state) { ); } - $form['submit'] = array( + $form['root']['submit'] = array( '#type' => 'submit', '#value' => 'Delete Selected Sites', ); - //print_r($form); exit; return $form; } @@ -115,32 +128,7 @@ function theme_unl_site_list_table($variables) { $headers = array('Site Path', 'Datbase Prefix', 'Status', 'Link', 'Remove (can not undo!)'); $rows = array(); foreach (element_children($form) as $key) { - $installed = $form[$key]['installed']['#value']; - switch ($installed) { - case 0: - $installed = 'Scheduled for creation.'; - break; - - case 1: - $installed = 'Curently being created.'; - break; - - case 2: - $installed = 'In production.'; - break; - - case 3: - $installed = 'Scheduled for removal.'; - break; - - case 4: - $installed = 'Currently being removed.'; - break; - - default: - $installed = 'Unknown'; - break; - } + $installed = _unl_get_install_status_text($form[$key]['installed']['#value']); $rows[] = array( 'data' => array( $form[$key]['site_path']['#value'], @@ -152,7 +140,7 @@ function theme_unl_site_list_table($variables) { ); } - return theme('table', array('header' => $headers, 'rows' => $rows, 'caption' => $form['#title'])); + return theme('table', array('header' => $headers, 'rows' => $rows)); } function unl_site_list_submit($form, &$form_state) { @@ -167,6 +155,7 @@ function unl_site_list_submit($form, &$form_state) { } } + function unl_site_remove($site_id) { $uri = db_select('unl_sites', 's') ->fields('s', array('uri')) @@ -202,6 +191,10 @@ function unl_site_remove($site_id) { ->fields(array('installed' => 3)) ->condition('site_id', $site_id) ->execute(); + db_update('unl_sites_aliases') + ->fields(array('installed' => 3)) + ->condition('site_id', $site_id) + ->execute(); drupal_set_message('The site has been scheduled for removal.'); } @@ -218,12 +211,163 @@ function _unl_get_sites_subdir($uri) { +function unl_aliases_page() { + $page = array(); + $page[] = drupal_get_form('unl_alias_create'); + $page[] = drupal_get_form('unl_alias_list'); + + return $page; +} +function unl_alias_create($form, &$form_state) { + + $sites = db_select('unl_sites', 's') + ->fields('s', array('site_id', 'uri')) + ->execute() + ->fetchAll(); + foreach ($sites as $site) { + $site_list[$site->site_id] = $site->uri . '/'; + } + + $form['root'] = array( + '#type' => 'fieldset', + '#title' => 'Create New Alias', + ); + + $form['root']['site'] = array( + '#type' => 'select', + '#title' => 'Aliased Site', + '#description' => 'The site the alias will point to.', + '#options' => $site_list, + '#required' => TRUE, + ); + + $form['root']['alias_uri'] = array( + '#type' => 'textfield', + '#title' => t('Alias URL'), + '#description' => t('Full URL for the new alias.'), + '#default_value' => t('http://newsite.example.com/'), + '#required' => TRUE, + ); + + $form['root']['submit'] = array( + '#type' => 'submit', + '#value' => 'Create Alias', + ); + + return $form; +} +function unl_alias_create_submit($form, &$form_state) { + db_insert('unl_sites_aliases')->fields(array( + 'site_id' => $form_state['values']['site'], + 'uri' => $form_state['values']['alias_uri'], + ))->execute(); +} +function unl_alias_list($form, &$form_state) { + $query = db_select('unl_sites_aliases', 'a'); + $query->join('unl_sites', 's', 's.site_id = a.site_id'); + $query->fields('s', array('uri')); + $query->fields('a', array('site_alias_id', 'uri', 'installed')); + $sites = $query->execute()->fetchAll(); + $form['root'] = array( + '#type' => 'fieldset', + '#title' => 'Existing Aliases', + ); + + $form['root']['alias_list'] = array( + '#theme' => 'unl_alias_list_table', + ); + + foreach ($sites as $site) { + $form['root']['alias_list'][$site->site_alias_id] = array( + 'site_uri' => array('#value' => $site->uri), + 'alias_uri' => array('#value' => $site->a_uri), + 'installed' => array('#value' => $site->installed), + 'remove' => array( + '#type' => 'checkbox', + '#parents' => array('aliases', $site->site_alias_id, 'remove'), + '#default_value' => 0 + ), + ); + } + + $form['root']['submit'] = array( + '#type' => 'submit', + '#value' => 'Delete Selected Aliases', + ); + + return $form; +} + +function theme_unl_alias_list_table($variables) { + $form = $variables['form']; + + $headers = array('Site URI', 'Alias URI', 'Status', 'Remove (can not undo!)'); + $rows = array(); + foreach (element_children($form) as $key) { + $installed = _unl_get_install_status_text($form[$key]['installed']['#value']); + $rows[] = array( + 'data' => array( + $form[$key]['site_uri']['#value'], + $form[$key]['alias_uri']['#value'], + $installed, + drupal_render($form[$key]['remove']), + ) + ); + } + + return theme('table', array('header' => $headers, 'rows' => $rows)); +} + +function unl_alias_list_submit($form, &$form_state) { + $site_alias_ids = array(); + foreach ($form_state['values']['aliases'] as $site_alias_id => $alias) { + if ($alias['remove']) { + $site_alias_ids[] = $site_alias_id; + } + } + + db_update('unl_sites_aliases') + ->fields(array('installed' => 3)) + ->condition('site_alias_id', $site_alias_ids, 'IN') + ->execute(); +} + + +function _unl_get_install_status_text($id) { + switch ($id) { + case 0: + $installed = 'Scheduled for creation.'; + break; + + case 1: + $installed = 'Curently being created.'; + break; + + case 2: + $installed = 'In production.'; + break; + + case 3: + $installed = 'Scheduled for removal.'; + break; + + case 4: + $installed = 'Currently being removed.'; + break; + + default: + $installed = 'Unknown'; + break; + } + + return $installed; +}