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