diff --git a/sites/all/modules/unl/unl.install b/sites/all/modules/unl/unl.install index 338438c246bf582d2aae54fdcb2c9c58cd70f7d2..a683699352cbc82e7d6235e95ae490e5b86f978e 100644 --- a/sites/all/modules/unl/unl.install +++ b/sites/all/modules/unl/unl.install @@ -66,7 +66,9 @@ function unl_schema() { ), 'primary key' => array('site_id'), 'unique keys' => array( - 'sub_site' => array('site_path'), + 'sub_site' => array('site_path'), + 'uri' => array('uri'), + 'db_prefix' => array('db_prefix'), ), ); @@ -369,3 +371,8 @@ function unl_update_7110() db_query("UPDATE {unl_sites_aliases} SET base_uri = CONCAT(base_uri, '/') WHERE SUBSTRING(base_uri, -1) != '/'"); db_query("UPDATE {unl_sites_aliases} SET path = CONCAT(path, '/') WHERE SUBSTRING(path, -1) != '/' AND path != ''"); } + +function unl_update_7111() { + db_add_unique_key('unl_sites', 'uri', array('uri')); + db_add_unique_key('unl_sites', 'db_prefix', array('db_prefix')); +} diff --git a/sites/all/modules/unl/unl.module b/sites/all/modules/unl/unl.module index 19f749866292c6b049c3265d9158c9e97080c6a6..e36a11a69c5f5dec551eaac72f57177732635150 100644 --- a/sites/all/modules/unl/unl.module +++ b/sites/all/modules/unl/unl.module @@ -560,6 +560,15 @@ function unl_user_admin_role_validate($form, &$form_state) { } function unl_cron() { + _unl_cron_migration_step(); + _unl_cron_import_wdn_registry_sites(); +} + +/** + * If a site is being migrated via cron jobs, do some work towards that migration. + */ +function _unl_cron_migration_step() { + // We don't want this running as system user, only the web user. if (PHP_SAPI == 'cli') { return; } @@ -573,76 +582,88 @@ function unl_cron() { unl_send_site_created_email(); } } +} - if (conf_path() == 'sites/default') { - - $wdn_registry_info = array( - 'database' => variable_get('unl_wdn_registry_database'), - 'username' => variable_get('unl_wdn_registry_username'), - 'password' => variable_get('unl_wdn_registry_password'), - 'host' => variable_get('unl_wdn_registry_host'), - 'driver' => 'mysql', - ); +/** + * Checks the wdn registry for any sites that need to be imported and does so. + */ +function _unl_cron_import_wdn_registry_sites() { + // We don't want this running as system user, only the web user. + if (PHP_SAPI == 'cli') { + return; + } + + // We don't want this running on sub-sites. + if (conf_path() != 'sites/default') { + return; + } - if (!$wdn_registry_info['database']) { - return; - } + $wdn_registry_info = array( + 'database' => variable_get('unl_wdn_registry_database'), + 'username' => variable_get('unl_wdn_registry_username'), + 'password' => variable_get('unl_wdn_registry_password'), + 'host' => variable_get('unl_wdn_registry_host'), + 'driver' => 'mysql', + ); - Database::addConnectionInfo('wdn_registry', 'default', $wdn_registry_info); + if (!$wdn_registry_info['database']) { + return; + } - try { - db_set_active('wdn_registry'); - $data = db_select('site_request', 'r') - ->fields('r') - ->where('`url` IS NULL') - ->execute() - ->fetchAll(); - db_set_active(); - } - catch (Exception $e) { - db_set_active(); - return; - } + Database::addConnectionInfo('wdn_registry', 'default', $wdn_registry_info); - $sites_to_create = array(); - foreach ($data as $site) { - $path = unl_sanitize_url_part($site->department) . '/' . unl_sanitize_url_part($site->site_name); - $db_prefix = unl_create_db_prefix($site->site_name); - - $sites_to_create[] = array( - 'site_path' => $path, - 'uri' => url($path), - 'clean_url' => TRUE, - 'db_prefix' => $db_prefix, - 'site_admin' => $site->site_admin ? $site->site_admin : '', - 'migration_url' => $site->migration_url ? $site->migration_url : '', - 'migration_path' => $site->migration_path ? $site->migration_path : '', - 'department' => $site->department, - ); + try { + db_set_active('wdn_registry'); + $data = db_select('site_request', 'r') + ->fields('r') + ->where('`url` IS NULL') + ->execute() + ->fetchAll(); + db_set_active(); + } + catch (Exception $e) { + db_set_active(); + return; + } - db_set_active('wdn_registry'); - if (variable_get('unl_wdn_registry_production')) { - db_update('site_request') - ->fields(array('url' => url($path))) - ->condition('id', $site->id) - ->execute(); - } - db_set_active(); - } + $sites_to_create = array(); + foreach ($data as $site) { + $path = unl_sanitize_url_part($site->department) . '/' . unl_sanitize_url_part($site->site_name); + $db_prefix = unl_create_db_prefix($site->department . '/' . $site->site_name); + + $sites_to_create[$site->id] = array( + 'site_path' => $path, + 'uri' => url($path), + 'clean_url' => TRUE, + 'db_prefix' => $db_prefix, + 'site_admin' => $site->site_admin ? $site->site_admin : '', + 'migration_url' => $site->migration_url ? $site->migration_url : '', + 'migration_path' => $site->migration_path ? $site->migration_path : '', + 'department' => $site->department, + ); + } - foreach ($sites_to_create as $site_to_create) { - try { - db_insert('unl_sites')->fields($site_to_create)->execute(); - } - catch (PDOException $e) { - // Ignore duplicate records. - if ($e->getCode() != 23000) { - throw $e; - } + foreach ($sites_to_create as $wdn_site_id => $site_to_create) { + try { + db_insert('unl_sites')->fields($site_to_create)->execute(); + } + catch (PDOException $e) { + // Ignore duplicate records. + if ($e->getCode() != 23000) { + throw $e; } + continue; } - + + db_set_active('wdn_registry'); + if (variable_get('unl_wdn_registry_production')) { + db_update('site_request') + ->fields(array('url' => $site_to_create['uri'])) + ->condition('id', $wdn_site_id) + ->execute(); + } + db_set_active(); } } @@ -678,6 +699,7 @@ function unl_create_db_prefix($site_name) { * Custom function to sanitize user created URLs */ function unl_sanitize_url_part($url_part) { + $url_part = strtolower($url_part); $url_part = preg_replace('/[^a-z0-9]/', '-', $url_part); $url_part = preg_replace('/-+/', '-', $url_part); $url_part = preg_replace('/(^-)|(-$)/', '', $url_part);