User.php 3.72 KB
Newer Older
1
2
<?php

3
namespace Drupal\user\Plugin\migrate\source\d6;
4
5
6
7
8
9
10

use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;

/**
 * Drupal 6 user source from database.
 *
11
12
13
14
 * For available configuration keys, refer to the parent classes:
 * @see \Drupal\migrate\Plugin\migrate\source\SqlBase
 * @see \Drupal\migrate\Plugin\migrate\source\SourcePluginBase
 *
15
 * @MigrateSource(
16
17
 *   id = "d6_user",
 *   source_module = "user"
18
19
 * )
 */
20
class User extends DrupalSqlBase {
21
22
23
24
25
26
27

  /**
   * {@inheritdoc}
   */
  public function query() {
    return $this->select('users', 'u')
      ->fields('u', array_keys($this->baseFields()))
28
      ->condition('u.uid', 0, '>');
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    $fields = $this->baseFields();

    // Add roles field.
    $fields['roles'] = $this->t('Roles');

    return $fields;
  }

  /**
   * {@inheritdoc}
   */
46
  public function prepareRow(Row $row) {
47
48
    // User roles.
    $roles = $this->select('users_roles', 'ur')
49
      ->fields('ur', ['rid'])
50
51
52
53
54
55
56
57
58
59
      ->condition('ur.uid', $row->getSourceProperty('uid'))
      ->execute()
      ->fetchCol();
    $row->setSourceProperty('roles', $roles);

    // We are adding here the Event contributed module column.
    // @see https://api.drupal.org/api/drupal/modules%21user%21user.install/function/user_update_7002/7
    if ($row->hasSourceProperty('timezone_id') && $row->getSourceProperty('timezone_id')) {
      if ($this->getDatabase()->schema()->tableExists('event_timezones')) {
        $event_timezone = $this->select('event_timezones', 'e')
60
          ->fields('e', ['name'])
61
62
63
64
65
66
67
68
69
          ->condition('e.timezone', $row->getSourceProperty('timezone_id'))
          ->execute()
          ->fetchField();
        if ($event_timezone) {
          $row->setSourceProperty('event_timezone', $event_timezone);
        }
      }
    }

70
    // Unserialize Data.
larowlan's avatar
larowlan committed
71
72
73
74
    $data = $row->getSourceProperty('data');
    if ($data !== NULL) {
      $row->setSourceProperty('data', unserialize($row->getSourceProperty('data')));
    }
75

76
77
78
79
80
81
82
    return parent::prepareRow($row);
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
83
84
    return [
      'uid' => [
85
86
        'type' => 'integer',
        'alias' => 'u',
87
88
      ],
    ];
89
90
91
92
93
94
95
96
97
  }

  /**
   * Returns the user base fields to be migrated.
   *
   * @return array
   *   Associative array having field name as key and description as value.
   */
  protected function baseFields() {
98
    $fields = [
99
100
101
102
      'uid' => $this->t('User ID'),
      'name' => $this->t('Username'),
      'pass' => $this->t('Password'),
      'mail' => $this->t('Email address'),
103
      'theme' => $this->t('Theme'),
104
105
106
107
108
109
110
111
112
113
      'signature' => $this->t('Signature'),
      'signature_format' => $this->t('Signature format'),
      'created' => $this->t('Registered timestamp'),
      'access' => $this->t('Last access timestamp'),
      'login' => $this->t('Last login timestamp'),
      'status' => $this->t('Status'),
      'timezone' => $this->t('Timezone'),
      'language' => $this->t('Language'),
      'picture' => $this->t('Picture'),
      'init' => $this->t('Init'),
114
      'data' => $this->t('User data'),
115
    ];
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

    // Possible field added by Date contributed module.
    // @see https://api.drupal.org/api/drupal/modules%21user%21user.install/function/user_update_7002/7
    if ($this->getDatabase()->schema()->fieldExists('users', 'timezone_name')) {
      $fields['timezone_name'] = $this->t('Timezone (Date)');
    }

    // Possible field added by Event contributed module.
    // @see https://api.drupal.org/api/drupal/modules%21user%21user.install/function/user_update_7002/7
    if ($this->getDatabase()->schema()->fieldExists('users', 'timezone_id')) {
      $fields['timezone_id'] = $this->t('Timezone (Event)');
    }

    return $fields;
  }

}