regene-usernames.script 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. //
  3. // This example demonstrates how to write a drush
  4. // script. These scripts are run with the php-script command.
  5. //
  6. use Drush\Drush;
  7. $this->output()->writeln("User names regenerating");
  8. $this->output()->writeln("The extra options/arguments to this command were:");
  9. $this->output()->writeln(print_r($extra, true));
  10. // if ($extra[0] === "delete") {
  11. // $delete = true;
  12. // } else {
  13. // $delete = false;
  14. // }
  15. $database = \Drupal::database();
  16. // __ __ _ _
  17. // | \ \ ___ <_>| | ___
  18. // | |<_> || || |<_-<
  19. // |_|_|_|<___||_||_|/__/
  20. $this->output()->writeln("");
  21. $result = $database->query("
  22. SELECT mail
  23. FROM {users_field_data}
  24. GROUP BY mail
  25. HAVING COUNT(*) > 1");
  26. $dupmail = [];
  27. foreach ($result as $user) {
  28. // $this->output()->writeln($user->mail);
  29. $dupmail[] = $user->mail;
  30. }
  31. $this->output()->writeln(count($dupmail) . ' dup emails');
  32. $this->output()->writeln("");
  33. $this->output()->writeln("All users Name <- Mail");
  34. $ufd_query = $database->select('users_field_data', 'ufd')
  35. ->fields('ufd', ['uid', 'name', 'mail'])
  36. ->condition('ufd.uid', [0,1], 'NOT IN')
  37. ->condition('mail', $dupmail, 'NOT IN');
  38. $ufd_result = $ufd_query->execute();
  39. foreach ($ufd_result as $ufd) {
  40. $user_updated = $database->update('users_field_data')
  41. ->fields([
  42. 'name' => $ufd->mail
  43. ])
  44. ->condition('uid', $ufd->uid)
  45. ->execute();
  46. }
  47. // __ __ _
  48. // | \ \ ___ ._ _ _ | |_ ___ _ _ ___
  49. // | |/ ._>| ' ' || . \/ ._>| '_><_-<
  50. // |_|_|_|\___.|_|_|_||___/\___.|_| /__/
  51. $this->output()->writeln("");
  52. $this->output()->writeln("All users Name <- Profile member");
  53. $p_query = $database->select('profile', 'p');
  54. $p_query->join('profile__field_name', 'pfn', 'pfn.entity_id = p.profile_id');
  55. $p_query->join('profile__field_first_name', 'pffn', 'pffn.entity_id = p.profile_id');
  56. $p_query->join('user__roles', 'ur', 'ur.entity_id = p.uid');
  57. $p_query->join('users_field_data', 'ufd', 'ufd.uid = p.uid');
  58. $p_query
  59. ->fields('p', ['profile_id', 'uid'])
  60. ->fields('pfn', ['field_name_value'])
  61. ->fields('pffn', ['field_first_name_value'])
  62. ->fields('ur', ['entity_id', 'roles_target_id'])
  63. ->fields('ufd', ['mail'])
  64. ->condition('p.type', 'member')
  65. ->condition('ur.roles_target_id', 'adherent');
  66. $p_result = $p_query->execute();
  67. // avoid duplicates
  68. $members = [];
  69. foreach ($p_result as $p) {
  70. // $this->output()->writeln("profile name ". $p->field_first_name_value . ' ' . $p->field_name_value);
  71. $name = $p->field_first_name_value . ' ' . $p->field_name_value;
  72. $name = str_replace(' Col.', '', $name);
  73. if (!in_array($name, array_values($members), true)) {
  74. $members[] = [
  75. "name" => $name,
  76. "mail" => $p->mail,
  77. "uid" => $p->uid
  78. ];
  79. }
  80. }
  81. // $members = array_unique($members);
  82. // array_multisort($members);
  83. // $this->output()->writeln(print_r($members, true));
  84. // $this->output()->writeln(print_r(array_count_values($members)));
  85. $this->output()->writeln("members count ". count($members));
  86. $prev_name = "";
  87. foreach ($members as $member) {
  88. $this->output()->writeln("updating " . $member['uid'] . " " . $member['name'] . " | " . $member['mail']);
  89. $user_updated = $database->update('users_field_data')
  90. ->fields([
  91. 'name' => $member['name']
  92. ])
  93. ->condition('uid', $member['uid'])
  94. ->execute();
  95. }
  96. // ___ _
  97. // | _> _ _ ___ _| |_ ___ ._ _ _ ___ _ _ ___
  98. // | <__| | |<_-< | | / . \| ' ' |/ ._>| '_><_-<
  99. // `___/`___|/__/ |_| \___/|_|_|_|\___.|_| /__/
  100. $this->output()->writeln("All users Name <- Profile customer");
  101. $p_query = $database->select('profile', 'p');
  102. $p_query->join('profile__address', 'pa', 'pa.entity_id = p.profile_id');
  103. $p_query->join('user__roles', 'ur', 'ur.entity_id = p.uid');
  104. $p_query->join('users_field_data', 'ufd', 'ufd.uid = p.uid');
  105. $p_query
  106. ->fields('p', ['profile_id', 'uid'])
  107. ->fields('pa', ['address_given_name', 'address_family_name'])
  108. ->fields('ur', ['entity_id', 'roles_target_id'])
  109. ->fields('ufd', ['mail'])
  110. ->condition('p.type', 'customer')
  111. ->condition('ufd.mail', ['info@materio.com'], 'NOT IN')
  112. ->condition('ur.roles_target_id', 'adherent');
  113. $p_result = $p_query->execute();
  114. // avoid duplicates
  115. $customers = [];
  116. foreach ($p_result as $p) {
  117. // $this->output()->writeln("profile name ". $p->field_first_name_value . ' ' . $p->field_name_value);
  118. $name = $p->address_given_name . ' ' . $p->address_family_name;
  119. $name = str_replace(' Col.', '', $name);
  120. if (!in_array($name, array_values($customers), true)) {
  121. $customers[] = [
  122. "name" => $name,
  123. "mail" => $p->mail,
  124. "uid" => $p->uid
  125. ];
  126. }
  127. }
  128. // $customers = array_unique($customers);
  129. // array_multisort($customers);
  130. // $this->output()->writeln(print_r($customers, true));
  131. // $this->output()->writeln(print_r(array_count_values($customers)));
  132. $this->output()->writeln("customers count ". count($customers));
  133. $prev_name = "";
  134. foreach ($customers as $customer) {
  135. $this->output()->writeln("updating " . $customer['uid'] . " " . $customer['name'] . " | " . $customer['mail']);
  136. $user_updated = $database->update('users_field_data')
  137. ->fields([
  138. 'name' => $customer['name']
  139. ])
  140. ->condition('uid', $customer['uid'])
  141. ->execute();
  142. }