-
Notifications
You must be signed in to change notification settings - Fork 0
/
patterns_server.install
356 lines (346 loc) · 10.6 KB
/
patterns_server.install
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
/**
* @file
* patterns server install file.
*/
/**
* Implements hook_install().
* create the public://patterns_server folder.
*/
function patterns_server_install() {
//create public://patterns_server folder.
$public_dir_path = variable_get('file_public_path', conf_path() . '/files');
if (!file_exists($public_dir_path . '/patterns_server')) {
if (!drupal_mkdir($public_dir_path . '/patterns_server')) {
drupal_set_message(st('cannot create folder in public directory, this should be caused by permission of public directory. for example "sites/default/files".'), 'error');
}
}
// Check if role patterns_server_user exists
$result = db_select('role', 'r')
->fields('r', array('name'))
->condition('name', 'patterns_server_user', '=')
->execute()
->fetchAssoc();
if (!empty($result['name'])) {
return;
}
// Otherwise creates the role
$role = new stdClass();
$role->name = 'patterns_server_user';
user_role_save($role);
$role = user_role_load_by_name('patterns_server_user');
$role_rid = $role->rid;
// Define our 'editor' role permissions
$role_permissions = array(
'administer patterns server' => TRUE,
'view patterns server' => TRUE,
'upload patterns server' => TRUE,
);
// Grant permissions to our 'editor' role
user_role_change_permissions($role_rid, $role_permissions);
}
/**
* Implements hook_schema().
*/
function patterns_server_schema() {
$schema['patterns_server'] = array(
'description' => 'Patterns server for Patterns module',
'fields' => array(
'pid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Unique pattern id',
),
'title' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Human readable name.',
),
'description' => array(
'type' => 'text',
'description' => 'Description of the pattern.',
),
'category' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Category of the pattern.',
),
'pattern' => array(
'type' => 'text',
'size' => 'medium',
'not null' => TRUE,
'serialize' => TRUE,
'description' => 'A serialized array containing pattern code.',
),
'author' => array(
'type' => 'varchar',
'length' => 50,
'description' => 'pattern\'s author',
),
'uploader' => array(
'type' => 'int',
'length' => 50,
'default' => 0,
'nut null' => TRUE,
'description' => 'pattern\'s uploader',
),
'file_name' => array(
'type' => 'varchar',
'length' => 155,
'not null' => TRUE,
'description' => 'Machine readable name of this pattern.',
),
'file_format' => array(
'type' => 'varchar',
'length' => 10,
'not null' => TRUE,
'description' => 'Format of the patterns (XML, YAML, etc.).',
),
'file_path' => array(
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'description' => 'Path of the pattern file relative to Drupal root.',
),
'file_downloads' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Download Number.',
),
'created' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'unsigned' => TRUE,
'description' => 'The Unix timestamp indicating when the pattern file was created.',
),
'uuuid' => array(
'type' => 'varchar',
'length' => 50,
'default' => '',
'description' => 'Identify patterns and their parent',
),
'host' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'description' => 'The host which the pattern comes from',
),
'd2did' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'description' => 'The d2did of the host which the pattern comes from',
),
),
'primary key' => array('pid'),
'unique keys' => array('file_name' => array('file_name')),
);
$schema['patterns_server_relation_pairs'] = array(
'description' => 'Patterns server relation pairs',
'fields' => array(
'pair_id' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Unique relation pair id',
),
'parent_uuuid' => array(
'type' => 'varchar',
'not null' => TRUE,
'length' => 50,
'description' => 'the uuuid of the parent',
),
'child_uuuid' => array(
'type' => 'varchar',
'not null' => TRUE,
'length' => 50,
'description' => 'the uuuid of the child',
),
//for now, only two, one is pull. the other is merge.
//pull will have only one parent.
//merge will have more than one.
'action' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'description' => 'The action generating the pattern.',
),
//actully, because you can merge or pull in the same drupal instance,
//the host means where you pull the pattern, or where you merge some other
//pattern files to generate this pattern.
'host' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'description' => 'The host of the parents.',
),
//the d2did and host give the same information. all show where you get the
//pattern.
'd2did' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'description' => 'The d2did of the d2d instance which the parents host on',
),
'user' => array(
'type' => 'int',
'length' => 50,
'default' => 0,
'nut null' => TRUE,
'description' => 'the uid of the user which uploads or pushes the pattern file',
),
'date' => array(
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
'description' => 'The Unix timestamp indicating when the pattern file was generated(pull or merge).',
),
'title' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'description' => 'The title of the parent pattern',
),
'category' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'description' => 'The category of the parent pattern',
),
'author' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'description' => 'The author of the parent pattern',
),
'created' => array(
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
'description' => 'The Unix timestamp indicating when the pattern file was uploaded or pushed).',
),
),
'primary key' => array('pair_id'),
);
$schema['patterns_server_relation_index'] = array(
'description' => 'Patterns server relation index',
'fields' => array(
'iid' => array(
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Unique index id',
),
'pid' => array(
'type' => 'int',
'length' => 50,
'not null' => TRUE,
'description' => 'the pid of the pattern in patterns_server table',
),
'uuuid' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'description' => 'the uuuid of the pattern.',
),
'parents' => array(
'type' => 'text',
'size' => 'medium',
'not null' => TRUE,
'serialize' => TRUE,
'description' => 'A serialized array containing the parents net of this pattern.',
),
'children' => array(
'type' => 'text',
'size' => 'medium',
'not null' => TRUE,
'serialize' => TRUE,
'description' => 'A serialized array containing the children net of this pattern.',
),
'category' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'description' => 'The category of the parent pattern',
),
'author' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'description' => 'The author of the parent pattern',
),
'title' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'description' => 'The title of the parent pattern',
),
'user' => array(
'type' => 'int',
'length' => 50,
'not null' => TRUE,
'description' => 'the uid of the user which upload the pattern',
),
'host' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'description' => 'The host which the pattern comes from',
),
'd2did' => array(
'type' => 'varchar',
'length' => 50,
'not null' => TRUE,
'description' => 'The d2did of the host which the pattern comes from',
),
'updated' => array(
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
'description' => 'The Unix timestamp indicating when the parents and children net are updated.',
),
),
'primary key' => array('iid'),
'unique keys' => array('uuuid' => array('uuuid')),
);
return $schema;
}
/**
* Implements hook_uninstall().
*
* 1. clean file_managed table.
* 2. clean votingapi_vote table.
* 3. clean all attached field.
*/
function patterns_server_uninstall() {
//delete uploaded file, clean file_managed table.
$query = db_select('file_managed', 'fm')
->fields('fm', array('fid'))
->condition('uri', '%' . db_like('patterns_server') . '%', 'LIKE')
->execute()
->fetchAll();
$file_fids = array();
foreach ($query as $fid) {
$file_fids[] = $fid->fid;
}
$files = file_load_multiple($file_fids);
foreach ($files as $file) {
file_delete($file);
}
//clean votingapi_vote table.
$num_deleted = db_delete('votingapi_vote')
->condition('entity_type', 'patterns_server')
->execute();
$num_deleted = db_delete('votingapi_cache')
->condition('entity_type', 'patterns_server')
->execute();
//clean all attached field.
field_attach_delete_bundle('patterns_server', 'patterns_server');
//delete the role.
user_role_delete('patterns_server_user');
}