Elgg  Version 6.1
20171010095648_drop_users_entity_table.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class DropUsersEntityTable extends AbstractMigration {
9  public function up() {
10 
11  if (!$this->hasTable('users_entity') || !$this->hasTable('metadata')) {
12  return;
13  }
14 
15  $prefix = $this->getAdapter()->getOption('table_prefix');
16  $cols = [
17  'name' => 'text',
18  'username' => 'text',
19  'password_hash' => 'text',
20  'email' => 'text',
21  'language' => 'text',
22  'banned' => 'text',
23  'admin' => 'text',
24  'prev_last_action' => 'integer',
25  'last_login' => 'integer',
26  'prev_last_login' => 'integer',
27  ];
28  $col_names = "'" . implode("', '", array_keys($cols)) . "'";
29 
30  $users_query = "SELECT * FROM {$prefix}users_entity LIMIT 100";
31  while ($rows = $this->fetchAll($users_query)) {
32  $guids = [];
33  foreach ($rows as $row) {
34  $guids[] = $row['guid'];
35  }
36 
37  $guids = implode(',', $guids);
38 
39  // remove existing metadata... attributes are more important
40  $this->execute("
41  DELETE FROM {$prefix}metadata
42  WHERE entity_guid IN ({$guids}) AND
43  name IN ({$col_names})
44  ");
45 
46  $new_metadata_rows = [];
47 
48  foreach ($rows as $row) {
49  // special column last_action goes to last_action in entities table
50  $this->execute("
51  UPDATE {$prefix}entities SET last_action = {$row['last_action']}
52  WHERE guid = {$row['guid']}
53  ");
54 
55  foreach ($cols as $col => $type) {
56  $value = $row[$col];
57  if (is_null($value) || $value === '') {
58  continue;
59  }
60 
61  $new_metadata_rows[] = [
62  'entity_guid' => $row['guid'],
63  'name' => $col,
64  'value' => $value,
65  'value_type' => $type,
66  'owner_guid' => 0,
67  'access_id' => 2,
68  'time_created' => time(),
69  'enabled' => 'yes',
70  ];
71  }
72  }
73 
74  if (!empty($new_metadata_rows)) {
75  $this->table('metadata')->insert($new_metadata_rows)->saveData();
76  }
77 
78  // remove from users so it does not get processed again in the next while loop
79  $this->execute("
80  DELETE FROM {$prefix}users_entity
81  WHERE guid IN ({$guids})
82  ");
83  }
84 
85  // all data migrated, so drop the table
86  $this->table('users_entity')->drop()->save();
87  }
88 }
$rows
Definition: redis.php:25
$type
Definition: delete.php:21
$value
Definition: generic.php:51
up()
Move users_entity attributes to metadata.
$guids
Activates all specified installed and inactive plugins.
Definition: activate_all.php:9