From 0f3062cb7a6b719b1638cc1bb073c0ea8a50c078 Mon Sep 17 00:00:00 2001 From: Eric Rasmussen <ericrasmussen1@gmail.com> Date: Wed, 30 May 2012 13:59:56 -0500 Subject: [PATCH] [gh-390] Move the unl_sites db stuff out of cron No real reason to weigh cron down with updating site name and last access timestamp constantly. The update db queries for those two fields can just be run when the site list is accessed. --- sites/all/modules/unl/cron.php | 54 -------------------- sites/all/modules/unl/unl_site_creation.php | 56 +++++++++++++++++++++ 2 files changed, 56 insertions(+), 54 deletions(-) diff --git a/sites/all/modules/unl/cron.php b/sites/all/modules/unl/cron.php index 8670589b..9ec4f3df 100644 --- a/sites/all/modules/unl/cron.php +++ b/sites/all/modules/unl/cron.php @@ -28,7 +28,6 @@ unl_remove_page_aliases(); unl_add_sites(); unl_add_aliases(); unl_add_page_aliases(); -unl_update_unl_sites(); function unl_add_sites() { $query = db_query('SELECT * FROM {unl_sites} WHERE installed=0'); @@ -451,56 +450,3 @@ function unl_require_writable($path) { } } -/** - * Updates the name and access fields in the default site unl_sites table for display on admin/sites/unl - */ -function unl_update_unl_sites() { - // Get all sites in production - $query = db_query('SELECT * FROM {unl_sites} WHERE installed=2'); - - // Get all custom made roles (roles other than authenticated, anonymous, administrator) - $roles = user_roles(TRUE); - unset($roles[DRUPAL_AUTHENTICATED_RID]); - unset($roles[variable_get('user_admin_role')]); - - // Setup alternate db connection so we can query other sites' tables without a prefix being attached - $database_noprefix = array( - 'database' => $GLOBALS['databases']['default']['default']['database'], - 'username' => $GLOBALS['databases']['default']['default']['username'], - 'password' => $GLOBALS['databases']['default']['default']['password'], - 'host' => $GLOBALS['databases']['default']['default']['host'], - 'port' => $GLOBALS['databases']['default']['default']['port'], - 'driver' => $GLOBALS['databases']['default']['default']['driver'], - ); - Database::addConnectionInfo('UNLNoPrefix', 'default', $database_noprefix); - - // The master prefix that was specified during initial drupal install - $master_prefix = $GLOBALS['databases']['default']['default']['prefix']; - - while ($row = $query->fetchAssoc()) { - // Switch to alt db connection - db_set_active('UNLNoPrefix'); - - // Get site name - $table = $row['db_prefix'].'_'.$master_prefix.'variable'; - $name = db_query("SELECT value FROM ".$table." WHERE name = 'site_name'")->fetchField(); - - // Get last access timestamp (by a non-administrator) - $table_users = $row['db_prefix'].'_'.$master_prefix.'users u'; - $table_users_roles = $row['db_prefix'].'_'.$master_prefix.'users_roles r'; - if (!empty($roles)) { - $access = db_query('SELECT u.access FROM '.$table_users.', '.$table_users_roles.' WHERE u.uid = r.uid AND u.access > 0 AND r.rid IN (' . implode(',', array_keys($roles)) . ') ORDER BY u.access DESC')->fetchColumn(); - } else { - $access = 0; - } - - // Restore default db connection - db_set_active(); - - // Update unl_sites table of the default site - db_update('unl_sites') - ->fields(array('name' => @unserialize($name), 'access' => (int)$access)) - ->condition('site_id', $row['site_id']) - ->execute(); - } -} diff --git a/sites/all/modules/unl/unl_site_creation.php b/sites/all/modules/unl/unl_site_creation.php index 2e5aa139..5b5b2186 100644 --- a/sites/all/modules/unl/unl_site_creation.php +++ b/sites/all/modules/unl/unl_site_creation.php @@ -91,10 +91,66 @@ function unl_site_create_submit($form, &$form_state) { return; } +/** + * Updates the name and access fields in the default site unl_sites table. + */ +function unl_update_unl_sites() { + // Get all sites in production + $query = db_query('SELECT * FROM {unl_sites} WHERE installed=2'); + + // Get all custom made roles (roles other than authenticated, anonymous, administrator) + $roles = user_roles(TRUE); + unset($roles[DRUPAL_AUTHENTICATED_RID]); + unset($roles[variable_get('user_admin_role')]); + + // Setup alternate db connection so we can query other sites' tables without a prefix being attached + $database_noprefix = array( + 'database' => $GLOBALS['databases']['default']['default']['database'], + 'username' => $GLOBALS['databases']['default']['default']['username'], + 'password' => $GLOBALS['databases']['default']['default']['password'], + 'host' => $GLOBALS['databases']['default']['default']['host'], + 'port' => $GLOBALS['databases']['default']['default']['port'], + 'driver' => $GLOBALS['databases']['default']['default']['driver'], + ); + Database::addConnectionInfo('UNLNoPrefix', 'default', $database_noprefix); + + // The master prefix that was specified during initial drupal install + $master_prefix = $GLOBALS['databases']['default']['default']['prefix']; + + while ($row = $query->fetchAssoc()) { + // Switch to alt db connection + db_set_active('UNLNoPrefix'); + + // Get site name + $table = $row['db_prefix'].'_'.$master_prefix.'variable'; + $name = db_query("SELECT value FROM ".$table." WHERE name = 'site_name'")->fetchField(); + + // Get last access timestamp (by a non-administrator) + $table_users = $row['db_prefix'].'_'.$master_prefix.'users u'; + $table_users_roles = $row['db_prefix'].'_'.$master_prefix.'users_roles r'; + if (!empty($roles)) { + $access = db_query('SELECT u.access FROM '.$table_users.', '.$table_users_roles.' WHERE u.uid = r.uid AND u.access > 0 AND r.rid IN (' . implode(',', array_keys($roles)) . ') ORDER BY u.access DESC')->fetchColumn(); + } else { + $access = 0; + } + + // Restore default db connection + db_set_active(); + + // Update unl_sites table of the default site + db_update('unl_sites') + ->fields(array('name' => @unserialize($name), 'access' => (int)$access)) + ->condition('site_id', $row['site_id']) + ->execute(); + } +} + /** * Site List appears on admin/sites/unl, admin/sites/unl/sites */ function unl_site_list($form, &$form_state) { + unl_update_unl_sites(); + $header = array( 'uri' => array( 'data' => t('Default Path'), -- GitLab