From 9ec7e47d8dabd38efd94e86ddc4a01da687d2582 Mon Sep 17 00:00:00 2001 From: Colin Finck Date: Mon, 18 Jun 2018 22:43:23 +0200 Subject: [PATCH] [ROSLOGIN] Add a first (unfinished) version of my script to migrate the user database from Drupal to RosLogin. I want to use this opportunity to clean the user database from inactive users. A user is considered inactive if it is older than a month and has never been active in either Drupal, MediaWiki, phpBB, or JIRA. Consequently, deleting it would not cause any loss of information. However, it keeps our future LDAP directory small and may fix possible name clashes due to the new stricter username rules. --- resources/roslogin/migrate.php | 133 +++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 resources/roslogin/migrate.php diff --git a/resources/roslogin/migrate.php b/resources/roslogin/migrate.php new file mode 100644 index 00000000..8b8f4d86 --- /dev/null +++ b/resources/roslogin/migrate.php @@ -0,0 +1,133 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + + $stmt = $dbh->query('SELECT COUNT(*) FROM drupal.users'); + $total_count = (int)$stmt->fetchColumn(); + printf("Total Users in the Drupal database: %u\n\n", $total_count); + + $one_month_ago = time() - 60 * 60 * 24 * 30; + $drupal_activity_stmt = $dbh->prepare('SELECT (SELECT COUNT(*) FROM drupal.history WHERE uid = :uid) + (SELECT COUNT(*) FROM drupal.node_revision WHERE uid = :uid)'); + $phpbb_uid_stmt = $dbh->prepare('SELECT user_id FROM forum.phpbb_users WHERE username_clean = :phpbb_name'); + $phpbb_activity_stmt = $dbh->prepare('SELECT (SELECT COUNT(*) FROM forum.phpbb_posts WHERE poster_id = :phpbb_uid) + (SELECT COUNT(*) FROM forum.phpbb_privmsgs WHERE author_id = :phpbb_uid)'); + $wiki_activity_stmt = $dbh->prepare('SELECT COUNT(*) FROM wiki.revision WHERE LOWER(rev_user_text) = :wiki_name'); + $jira_activity_stmt = $dbh->prepare('SELECT COUNT(*) FROM jira.jiraaction WHERE AUTHOR = :jira_name'); + + $stmt = $dbh->query('SELECT uid, name, mail, created, password_ldap FROM drupal.users'); + while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) + { + printf('%u: %s ... ', $row['uid'], $row['name']); + $active_user = false; + + for (;;) + { + // Do we have a user name? + if (trim($row['name'] == '')) + { + echo "Not adding (empty user name)\n"; + break; + } + + // Is this user less than a month old? + if ((int)$row['created'] > $one_month_ago) + { + echo "Adding (created less than a month ago)\n"; + $active_user = true; + break; + } + + // Did this user ever change a Drupal node? + $drupal_activity_stmt->bindValue(':uid', $row['uid']); + $drupal_activity_stmt->execute(); + $drupal_activity_count = (int)$drupal_activity_stmt->fetchColumn(); + if ($drupal_activity_count > 0) + { + echo "Adding (Drupal activity)\n"; + $active_user = true; + break; + } + + // Did this user ever post in the forums? + $phpbb_name = utf8_clean_string($row['name']); + $phpbb_uid_stmt->bindValue(':phpbb_name', $phpbb_name); + $phpbb_uid_stmt->execute(); + $phpbb_uid = (int)$phpbb_uid_stmt->fetchColumn(); + if ($phpbb_uid > 0) + { + $phpbb_activity_stmt->bindValue(':phpbb_uid', $phpbb_uid); + $phpbb_activity_stmt->execute(); + $phpbb_activity_count = (int)$stmt->fetchColumn(); + if ($phpbb_activity_count > 0) + { + echo "Adding (phpBB activity)\n"; + $active_user = true; + break; + } + } + + // Did this user ever change anything in the Wiki? + $wiki_name = strtolower(str_replace('_', ' ', $row['name'])); + $wiki_activity_stmt->bindValue(':wiki_name', $wiki_name); + $wiki_activity_stmt->execute(); + $wiki_activity_count = (int)$wiki_activity_stmt->fetchColumn(); + if ($wiki_activity_count > 0) + { + echo "Adding (Wiki activity)\n"; + $active_user = true; + break; + } + + // Did this user ever participate on JIRA? + $jira_name = strtolower($row['name']); + $jira_activity_stmt->bindValue(':jira_name', $jira_name); + $jira_activity_stmt->execute(); + $jira_activity_count = (int)$jira_activity_stmt->fetchColumn(); + if ($jira_activity_count > 0) + { + echo "Adding (JIRA activity)\n"; + $active_user = true; + break; + } + + // No activity determined. + echo "Not adding (no activity)\n"; + break; + } + + if ($active_user) + { + // TODO: Add to LDAP directory and forbidden_usernames table. + // TODO: Count migrated users. + // TODO: Remove an inactive user from Drupal, MediaWiki, phpBB, JIRA databases as well. + } + } + } + catch (Exception $e) + { + die($e->getFile() . ':' . $e->getLine() . ' - ' . $e->getMessage()); + }