This repository has been archived by the owner on May 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathse_migrate.migrate.inc
339 lines (264 loc) · 10.8 KB
/
se_migrate.migrate.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
/*
* You must implement hook_migrate_api(), setting the API level to 2, for
* your migration classes to be recognized by the Migrate module.
*/
function se_migrate_migrate_api() {
$api = array(
'api' => 2,
'migrations' => array(
'MigrateSeUser' => array('class_name' => 'MigrateSeUser'),
'MigrateSeQuestions' => array('class_name' => 'MigrateSeQuestions'),
'MigrateSeAnswers' => array('class_name' => 'MigrateSeAnswers'),
'MigrateSeComments' => array('class_name' => 'MigrateSeComments'),
)
);
return $api;
}
class MigrateSeUser extends XMLMigration {
/* construct method */
public function __construct() {
parent::__construct(MigrateGroup::getInstance('se_migrate'));
// Source definition
$path = drupal_get_path('module','se_migrate') . '/data/users.xml';
$path_url = url($path,array('absolute' => TRUE));
// Xpath query to get items
$query = '/users/row';
// Xpath query withn the item query to get a unique id
$id_query = '@Id';
/**
* Sample line of the xml, indented for clarity
* <row Id="182"
* Reputation="4557"
* CreationDate="2011-03-03T07:59:42.010"
* DisplayName="Jeremy French"
* EmailHash="15ddf4d0e44ae1d9181a49a3aa836f86"
* LastAccessDate="2012-07-26T08:57:45.133"
* WebsiteUrl="http://www.jeremyfrench.co.uk"
* Location="United Kingdom"
* Age="36"
* AboutMe="<SNIP>"
* Views="0"
* UpVotes="517"
* DownVotes="33" />
*/
// Fields, need to be defined for XML types
$fields = array(
'reputation' => t('Reputation'),
'username' => t('Username'),
'created' => t('Registration date'),
'access' => t('Last Access'),
'webiste_url' => t('Website Url'),
'location' => t('Geographic location'),
'age' => t('Age (as of Sep 2011)'),
'up_votes' => t('Up votes'),
'down_votes' => t('Down votes'),
);
$this->source = new MigrateSourceXML($path_url, $query, $id_query, $fields);
// Destination definition
$this->destination = new MigrateDestinationUser();
// We instantiate the MigrateMap
$this->map = new MigrateSQLMap($this->machineName,
array(
'sourceid' => array('type' => 'int',
'unsigned' => FALSE,
'not null' => TRUE,
)
),
MigrateDestinationUser::getKeySchema()
);
// Add mappings
$this->addFieldMapping('name', 'username')
->xpath('@DisplayName');
$this->addFieldMapping('created', 'created')
->xpath('@CreationDate');
/* $this->addFieldMapping('field_website','webiste_url')
->xpath('@WebsiteUrl');*/
// Some Fields can have a default without a mapping, will be the same for all records.
$this->addFieldMapping('pass')->defaultValue('password');
$this->addFieldMapping('mail')->defaultValue('test@example.com');
$this->addFieldMapping('status')->defaultValue(1);
$this->description = t('Migrate Stack Exchange users to Drupal.');
}
}
/**
* Questions and answers are rather similar so define a base class that they can each extend.
*/
abstract class MigrateSeContent extends XMLMigration {
public function __construct() {
parent::__construct(MigrateGroup::getInstance('se_migrate'));
$this->dependencies = array('MigrateSeUser');
// Source definition
$path = drupal_get_path('module','se_migrate') . '/data/posts.xml';
$this->path_url = url($path,array('absolute' => TRUE));
// Xpath query withn the item query to get a unique id
$this->id_query = '@Id';
$this->fields = array(
'CreationDate' => t('creation date'),
'Score' => t('score'),
'ViewCount' => t('view count'),
'Body' => t('body'),
'OwnerUserId' => t('owner id'),
'LastEditorUserId' => t('last editor id'),
'LastEditorDisplayName' => t('last editor name'),
'LastEditDate' => t('last edit date'),
'LastActivityDate' => t('Last activity'),
'Title' => t('title'),
'Tags' => t('tags'),
'CommentCount' => t('Comment count'),
);
$this->map = new MigrateSQLMap($this->machineName,
array(
'sourceid' => array('type' => 'int',
'unsigned' => FALSE,
'not null' => TRUE,
)
),
MigrateDestinationUser::getKeySchema()
);
$this->addFieldMapping('body', 'Body')
->xpath('@Body');
$this->addFieldMapping('created', 'CreationDate')
->xpath('@CreationDate');
$this->addFieldMapping('changed', 'LastEditDate')
->xpath('@LastEditDate');
$this->addFieldMapping('uid','OwnerUserId')
->xpath('@OwnerUserId')
->sourceMigration('MigrateSeUser')
->defaultValue(1);
$this->addFieldMapping('revision_uid','LastEditorUserId')
->xpath('@OwnerUserId')
->sourceMigration('MigrateSeUser');
// Static values
$this->addFieldMapping('language')->defaultValue('en');
$this->addFieldMapping('body:format')->defaultValue('filtered_html');
// Add migration meta data
$this->description = t('Migrate Stack Exchange users to Drupal.');
$this->team = array(new MigrateTeamMember('Jeremy French','jeremy@jeremyfrench.co.uk','admin'));
}
}
class MigrateSeQuestions extends MigrateSeContent {
/** Post format
* <row Id="1"
* PostTypeId="1"
* AcceptedAnswerId="393"
* CreationDate="2011-03-02T20:49:08.213"
* Score="9"
* ViewCount="2140"
* Body="<p>How can I change the default Apache Solr URL path from "search/apachesolr_search/term" to something else?</p>
"
* OwnerUserId="14"
* LastEditorUserId="199"
* LastEditorDisplayName=""
* LastEditDate="2011-08-02T16:50:51.567"
* LastActivityDate="2011-11-09T17:11:18.020"
* Title="How can I change the Apache Solr search URL in Drupal?"
* Tags="<7><search><apache-solr>"
* AnswerCount="8"
* CommentCount="1"
* FavoriteCount="4" />
*/
/* construct method */
public function __construct() {
parent::__construct(MigrateGroup::getInstance('se_migrate'));
// Extend items not set up in the base class.
$this->description = t('Migrate stack exchange questions into se_question content type');
// Xpath query to get items PostTypeId = 1 is questions.
$query = '/posts/row[@PostTypeId="1"]';
// Add fields which are specific to questions.
$fields = $this->fields;
$fields['Title'] = t('title');
$fields['Tags'] = t('tags');
$fields['AnswerCount'] = t('Answer Count');
$fields['FavoriteCount'] = t('Fav count');
// Sorce needs to be defiend with complete fields
$this->source = new MigrateSourceXML($this->path_url, $query, $this->id_query, $fields);
// Destination definition
$this->destination = new MigrateDestinationNode('sequestion');
// Add mappings for distinct fields.
$this->addFieldMapping('title', 'Title')
->xpath('@Title');
/* Tags */
$this->addFieldMapping('field_tags', 'Tags')
->xpath('@Tags')
->separator(',');
$this->addFieldMapping('field_tags:create_term')->defaultValue(TRUE);
$this->addFieldMapping('field_tags:ignore_case')->defaultValue(TRUE);
// TODO: add view count.
}
/**
* Prepare row gives a chance to alter the data in a source row before
* it is passed into the source.
*/
function prepareRow($row) {
//Turn XML list into comma seperated list
$attributes = $row->xml->attributes();
$tags = $attributes['Tags'];
$tags = str_replace('>',',',$tags);
$tags = str_replace('<', '',$tags);
$row->xml['Tags'] = $tags;
}
}
class MigrateSeAnswers extends MigrateSeContent {
public function __construct() {
parent::__construct(MigrateGroup::getInstance('se_migrate'));
// Extend items not set up in the base class.
$this->description = t('Migrate stack exchange answers into se_answer content type');
// Xpath query to get items PostTypeId = 1 is questions.
$query = '/posts/row[@PostTypeId="2"]';
// Sorce needs to be defiend with complete fields
$this->source = new MigrateSourceXML($this->path_url, $query, $this->id_query, $this->fields);
// Destination definition
$this->destination = new MigrateDestinationNode('seanswer');
}
}
// Migration for comments
class MigrateSeComments extends XMLMigration {
public function __construct() {
parent::__construct(MigrateGroup::getInstance('se_migrate'));
$this->description = array('Migrate comments for se_question and se_answer content types');
$this->dependencies = array('MigrateSeAnswers','MigrateSeQuestions');
// Source definition
$path = drupal_get_path('module','se_migrate') . '/data/comments.xml';
$path_url = url($path,array('absolute' => TRUE));
$query = '/comments/row';
// Xpath query withn the item query to get a unique id
$id_query = '@Id';
$fields = array(
'PostId' => t('Post Id'),
'Score' => t('score'),
'Text' => t('comment text'),
'CreationDate' => t('creation date'),
'UserId' => t('User Id'),
);
$this->source = new MigrateSourceXML($path_url, $query, $id_query, $fields);
// Destination definition
$this->destination = new MigrateDestinationComment('comment_node_sequestion');
/**
* Note that above is a little bit of a hack. sequestion and seanswer node
* comments are the same format and the destination only looks at the structure
* so this migrates ok.
*/
$this->map = new MigrateSQLMap($this->machineName,
array(
'sourceid' => array('type' => 'int',
'unsigned' => FALSE,
'not null' => TRUE,
)
),
MigrateDestinationComment::getKeySchema()
);
$this->addFieldMapping('nid', 'PostId')
->sourceMigration(array('MigrateSeQuestions','MigrateSeAnswers'))
->xpath('@PostId');
$this->addFieldMapping('uid','UserId')
->xpath('@UserId')
->sourceMigration('MigrateSeUser')
->defaultValue(1);
$this->addFieldMapping('comment_body','Text')
->xpath('@Text');
$this->addFieldMapping('created','CreationDate')
->xpath('@CreationDate');
$this->addFieldMapping('status')->defaultValue(TRUE);
$this->addFieldMapping('language')->defaultValue('en');
}
}