Elgg  Version 5.1
Users.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database\Seeds;
4 
7 
13 class Users extends Seed {
14 
18  public function seed() {
19  $count_friends = function ($user) {
20  return elgg_count_entities([
21  'types' => 'user',
22  'relationship' => 'friend',
23  'relationship_guid' => $user->guid,
24  'inverse_relationship' => false,
25  'metadata_names' => '__faker',
26  ]);
27  };
28 
29  $exclude = [];
30 
31  $this->advance($this->getCount());
32 
33  $profile_fields_config = _elgg_services()->fields->get('user', 'user');
34  $profile_fields = [];
35  foreach ($profile_fields_config as $field) {
36  $profile_fields[$field['name']] = $field['#type'];
37  }
38 
39  while ($this->getCount() < $this->limit) {
40  try {
41  $user = $this->createUser([], [
42  'profile_fields' => $profile_fields,
43  ]);
44  } catch (MaxAttemptsException $e) {
45  // unable to create a user with the given options
46  continue;
47  }
48 
49  $this->createIcon($user);
50 
51  $exclude[] = $user->guid;
52 
53  // Friend the user other members
54  // Create a friend access collection and add some random friends to it
55  if ($count_friends($user)) {
56  continue;
57  }
58 
59  $collection = elgg_create_access_collection('Best Fake Friends Collection', $user->guid, 'friends_collection');
60  if ($collection instanceof \ElggAccessCollection) {
61  $this->log("Created new friend collection for user {$user->getDisplayName()} [collection_id: {$collection->id}]");
62  }
63 
64  $friends_limit = $this->faker()->numberBetween(5, 10);
65 
66  $friends_exclude = [$user->guid];
67  while ($count_friends($user) < $friends_limit) {
68  $friend = $this->getRandomUser($friends_exclude);
69  if (!$friend) {
70  continue;
71  }
72 
73  $friends_exclude[] = $friend->guid;
74 
75  if ($user->addFriend($friend->guid, true)) {
76  $this->log("User {$user->getDisplayName()} [guid: {$user->guid}] friended user {$friend->getDisplayName()} [guid: {$friend->guid}]");
77 
78  if ($collection instanceof \ElggAccessCollection && $this->faker()->boolean()) {
79  $collection->addMember($friend->guid);
80  }
81 
82  // randomize the river activity
83  $since = $this->create_since;
84  $this->setCreateSince(max($user->time_created, $friend->time_created));
85 
86  // fix river item
87  $river = elgg_get_river([
88  'view' => 'river/relationship/friend/create',
89  'action_type' => 'friend',
90  'subject_guid' => $user->guid,
91  'object_guid' => $friend->guid,
92  ]);
93  /* @var $item \ElggRiverItem */
94  foreach ($river as $item) {
95  $update = Update::table('river');
96  $update->set('posted', $update->param($this->getRandomCreationTimestamp(), ELGG_VALUE_TIMESTAMP))
97  ->where($update->compare('id', '=', $item->id, ELGG_VALUE_ID));
98 
99  _elgg_services()->db->updateData($update);
100  }
101 
102  $this->create_since = $since;
103  }
104  }
105 
106  $this->advance();
107  }
108  }
109 
113  public function unseed() {
114  /* @var $users \ElggBatch */
116  'type' => 'user',
117  'metadata_name' => '__faker',
118  'limit' => false,
119  'batch' => true,
120  'batch_inc_offset' => false,
121  ]);
122 
123  /* @var $user \ElggUser */
124  foreach ($users as $user) {
125  if ($user->delete()) {
126  $this->log("Deleted user {$user->guid}");
127  } else {
128  $this->log("Failed to delete user {$user->guid}");
129  $users->reportFailure();
130  continue;
131  }
132 
133  $this->advance();
134  }
135  }
136 
140  public static function getType() : string {
141  return 'user';
142  }
143 
147  protected function getCountOptions() : array {
148  return [
149  'type' => 'user',
150  ];
151  }
152 }
if(!$items) $item
Definition: delete.php:13
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
static table($table, $alias=null)
{}
Definition: Update.php:13
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:504
advance(int $step=1)
Advance progressbar.
Definition: Seed.php:98
elgg_count_entities(array $options=[])
Returns a count of entities.
Definition: entities.php:515
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
if(elgg_extract('required', $vars)) if(elgg_extract('disabled', $vars)) $field
Definition: field.php:38
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:346
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