forked from easyting/bpi
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bpi.install
217 lines (196 loc) · 6.84 KB
/
bpi.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
<?php
/**
* @file
* Install/uninstall, database related stuff here.
*/
/**
* Implements hook_schema().
*/
function bpi_schema() {
$schema = array();
$schema['bpi_syndicated'] = array(
'description' => 'Holds the correspondence between bpi id and node id.',
'fields' => array(
'id' => array(
'type' => 'serial',
),
'nid' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'bid' => array(
'type' => 'varchar',
'length' => '128',
'not null' => TRUE,
'default' => '',
),
'status' => array(
'type' => 'int',
'not null' => TRUE,
'default' => 0,
),
'timestamp' => array(
'type' => 'varchar',
'length' => '128',
'not null' => TRUE,
'default' => '',
),
'data' => array(
'type' => 'blob',
'description' => 'Data blob for values related to syndication',
),
),
'primary key' => array('id'),
'indexes' => array(
'node_id' => array('nid'),
'bpi_id' => array('bid'),
),
);
return $schema;
}
/**
* Add BPI data blob to db table.
*/
function bpi_update_7001() {
$field = array(
'type' => 'blob',
'description' => 'Data blob for values related to syndication',
);
db_add_field('bpi_syndicated', 'data', $field);
}
/**
* Enable bpi features module.
*/
function bpi_update_7002() {
module_enable(array('bpi_features'), TRUE);
}
/**
* Disable admin/content view from admin_views to avoid conflicts with our view.
*/
function bpi_update_7003() {
$views_defaults = variable_get('views_defaults', array());
if (!isset($views_defaults['admin_views_node']) || !$views_defaults['admin_views_node']) {
$views_defaults['admin_views_node'] = TRUE;
}
variable_set('views_defaults', $views_defaults);
}
/**
* Convert BPI Syndication data to workflow states.
*/
function bpi_update_7004(&$sandbox) {
$limit = 10;
$field_name = 'field_bpi_workflow';
// Initialize sandbox object.
if (!isset($sandbox['max'])) {
$query = db_select('bpi_syndicated', 'bpi');
$query->fields('bpi', array('id'));
$query->isNull('bpi.data');
$count = $query->countQuery()->execute()->fetchField();
$sandbox['max'] = $count;
$sandbox['current'] = 0;
if ($count == 0) {
$sandbox['#finished'] = 1;
return;
}
// Get workflows by machine name.
$workflow_query = db_select('workflows', 'w');
$workflow_query->fields('w', array('wid'));
$workflow_query->condition('w.name', 'bpi');
$res = $workflow_query->execute();
$wid = $res->fetchField();
// Get workflow states with machine names.
$workflow_states_query = db_select('workflow_states', 'w');
$workflow_states_query->fields('w', array('sid', 'name'));
$workflow_states_query->condition('w.wid', $wid);
$res = $workflow_states_query->execute();
$states = $res->fetchAllAssoc('name');
// Save states for easy access later.
$sandbox['states'] = array(
'creation' => $states['(creation)']->sid,
BPI_PUSHED => $states['sent_to_bpi']->sid,
BPI_SYNDICATED => $states['created_bpi']->sid,
);
}
// Get the next $limit records from db. We only need to search for records
// where data is null, because already processed records get data set to
// something.
$query = db_select('bpi_syndicated', 'bpi');
$query->fields('bpi');
$query->isNull('bpi.data');
$query->range(0, $limit);
$res = $query->execute();
if (drupal_is_cli()) {
$args = array(
'@index' => $limit > 1 ? ($sandbox['current'] + 1) . " - " . min($sandbox['current'] + $limit, $sandbox['max']) : $sandbox['current'],
'@max' => $sandbox['max'],
);
if ($sandbox['current'] + 1 == $sandbox['max']) {
$args['@index'] = $sandbox['max'];
}
drupal_set_message(t('Updating @index of @max', $args));
}
// For each record in bpi_syndicated not processed yet we create a Workflow
// transition with either "Sent to BPI" or "Created from BPI" as the new sid
// and set the workflow field to the same id.
while ($record = $res->fetchAssoc()) {
$nid = $record['nid'];
$bid = $record['bid'];
$status = $record['status'];
$timestamp = $record['timestamp'];
$node = node_load($nid);
if (!$node) {
// The bpi_syndicated table may contain references to nodes which no
// longer exist. Skip processing those.
watchdog('bpi', 'Unable to migrate syndication information for %nid. The node does not exist.', array('%nid' => $nid), WATCHDOG_WARNING);
continue;
}
if (!isset($node->$field_name)) {
// The bpi_syndicated table may contain references to nodes of types
// which do not contain the new workflow field. Skip those.
watchdog('bpi', 'Unable to migrate syndication information for %type #%nid. The content type does not have the workflow field %field_name', array('%type' => $node->type, '%nid' => $nid, '%field_name' => $field_name), WATCHDOG_WARNING);
continue;
}
// Create new transition and save it.
$uid = $node->uid;
$old_sid = $sandbox['states']['creation'];
$new_sid = $sandbox['states'][$status];
$transition = new WorkflowTransition();
$transition->setValues('node', $node, $field_name, $old_sid, $new_sid, $uid, $timestamp, '');
$transition->save();
$hid = $transition->hid;
// The timestamp for the transition is set to current time and not the time
// of creation, so we need to change it to the correct timestamp.
db_update('workflow_node_history')->condition('hid', $hid)->fields(array('stamp' => $timestamp))->execute();
// Set workflow state for node to new_sid from the newly created transition.
$wrapper = entity_metadata_wrapper('node', $node);
$wrapper->field_bpi_workflow->set($new_sid);
$wrapper->save();
// Get information from BPI server to create data object in db.
$bpi = bpi_client_instance();
$bpi_node = $bpi->getNode($bid);
$bpi_content = $bpi_node->getProperties();
$params = array(
'nid' => $node->nid,
'category' => $bpi_content['category'],
'audience' => $bpi_content['audience'],
'editable' => $bpi_content['editable'],
);
bpi_update_syndicated($node->nid, $node->bpi_id, $params, $status, FALSE);
}
$sandbox['current'] += $limit;
$sandbox['#finished'] = min($sandbox['current'], $sandbox['max']) / $sandbox['max'];
}
/**
* Disable content view that overrides admin_views_node and enable this instead.
*/
function bpi_update_7005() {
$views_defaults = variable_get('views_defaults', array());
if (isset($views_defaults['admin_views_node']) && $views_defaults['admin_views_node']) {
$views_defaults['admin_views_node'] = FALSE;
}
if (!isset($views_defaults['content']) || !$views_defaults['content']) {
$views_defaults['content'] = TRUE;
}
variable_set('views_defaults', $views_defaults);
}