Elgg  Version master
Users.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database\Seeds;
4 
8 
14 class Users extends Seed {
15 
19  public function seed() {
20  $count_friends = function ($user) {
21  return elgg_count_entities([
22  'types' => 'user',
23  'relationship' => 'friend',
24  'relationship_guid' => $user->guid,
25  'inverse_relationship' => false,
26  'metadata_names' => '__faker',
27  ]);
28  };
29 
30  $exclude = [];
31 
32  $this->advance($this->getCount());
33 
34  $profile_fields_config = _elgg_services()->fields->get('user', 'user');
35  $profile_fields = [];
36  foreach ($profile_fields_config as $field) {
37  $profile_fields[$field['name']] = $field['#type'];
38  }
39 
40  while ($this->getCount() < $this->limit) {
41  try {
42  $user = $this->createUser([], [
43  'profile_fields' => $profile_fields,
44  ]);
45  } catch (MaxAttemptsException $e) {
46  // unable to create a user with the given options
47  continue;
48  }
49 
50  $this->createIcon($user);
51 
52  $exclude[] = $user->guid;
53 
54  // Friend the user other members
55  // Create a friend access collection and add some random friends to it
56  if ($count_friends($user)) {
57  continue;
58  }
59 
60  $collection = elgg_create_access_collection('Best Fake Friends Collection', $user->guid, 'friends_collection');
61  if ($collection instanceof \ElggAccessCollection) {
62  $this->log("Created new friend collection for user {$user->getDisplayName()} [collection_id: {$collection->id}]");
63  }
64 
65  $friends_limit = $this->faker()->numberBetween(5, 10);
66 
67  $friends_exclude = [$user->guid];
68  while ($count_friends($user) < $friends_limit) {
69  $friend = $this->getRandomUser($friends_exclude);
70  if (!$friend) {
71  continue;
72  }
73 
74  $friends_exclude[] = $friend->guid;
75 
76  if ($user->addFriend($friend->guid, true)) {
77  $this->log("User {$user->getDisplayName()} [guid: {$user->guid}] friended user {$friend->getDisplayName()} [guid: {$friend->guid}]");
78 
79  if ($collection instanceof \ElggAccessCollection && $this->faker()->boolean()) {
80  $collection->addMember($friend->guid);
81  }
82 
83  // randomize the river activity
84  $since = $this->create_since;
85  $this->setCreateSince(max($user->time_created, $friend->time_created));
86 
87  // fix river item
88  $river = elgg_get_river([
89  'view' => 'river/relationship/friend/create',
90  'action_type' => 'friend',
91  'subject_guid' => $user->guid,
92  'object_guid' => $friend->guid,
93  ]);
94  /* @var $item \ElggRiverItem */
95  foreach ($river as $item) {
97  $update->set('posted', $update->param($this->getRandomCreationTimestamp(), ELGG_VALUE_TIMESTAMP))
98  ->where($update->compare('id', '=', $item->id, ELGG_VALUE_ID));
99 
100  _elgg_services()->db->updateData($update);
101  }
102 
103  $this->create_since = $since;
104  }
105  }
106 
107  $this->advance();
108  }
109  }
110 
114  public function unseed() {
115  /* @var $users \ElggBatch */
117  'type' => 'user',
118  'metadata_name' => '__faker',
119  'limit' => false,
120  'batch' => true,
121  'batch_inc_offset' => false,
122  ]);
123 
124  /* @var $user \ElggUser */
125  foreach ($users as $user) {
126  if ($user->delete(true, true)) {
127  $this->log("Deleted user {$user->guid}");
128  } else {
129  $this->log("Failed to delete user {$user->guid}");
130  $users->reportFailure();
131  continue;
132  }
133 
134  $this->advance();
135  }
136  }
137 
141  public static function getType() : string {
142  return 'user';
143  }
144 
148  protected function getCountOptions() : array {
149  return [
150  'type' => 'user',
151  ];
152  }
153 }
if(!$items) $item
Definition: delete.php:13
static table(string $table)
Returns a QueryBuilder for updating data in a given table.
Definition: Update.php:17
faker(string $locale= 'en_US')
Returns an instance of faker.
Definition: Seeding.php:48
Thrown when the seeding has exceeded the max attempts for trying to create an .
if(empty($user_guids)) $users
Definition: ban.php:12
if(elgg_extract('input_type', $vars)) if(elgg_extract('required', $vars)) if(elgg_extract('disabled', $vars)) $field
Definition: field.php:42
createUser(array $properties=[])
Create a new fake user.
const ELGG_VALUE_ID
Definition: constants.php:114
elgg_get_entities(array $options=[])
Fetches/counts entities or performs a calculation on their properties.
Definition: entities.php:507
advance(int $step=1)
Advance progressbar.
Definition: Seed.php:98
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:518
getRandomUser(array $exclude=[], bool $allow_create=true)
Returns random fake user.
Definition: Seeding.php:521
$user
Definition: ban.php:7
elgg_get_river(array $options=[])
Get river items.
Definition: river.php:101
createIcon(\ElggEntity $entity)
Create an icon for an entity.
Definition: Seeding.php:779
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:115
log($level, $message, array $context=[])
Log a message.
Definition: Loggable.php:58
setCreateSince($since= 'now')
Set a time for entities to be created after.
Definition: TimeHelpers.php:33
Abstract seed.
Definition: Seed.php:14
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
elgg_create_access_collection(string $name, int $owner_guid=0, string $subtype=null)
Creates a new access collection.
Definition: access.php:147
getCount()
Get the count of the seeded entities.
Definition: Seed.php:78