/[drupal]/contributions/modules/workflow/workflow.install
ViewVC logotype

Contents of /contributions/modules/workflow/workflow.install

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.14 - (show annotations) (download) (as text)
Thu Sep 24 23:02:50 2009 UTC (2 months ago) by eaton
Branch: MAIN
CVS Tags: HEAD
Changes since 1.13: +4 -4 lines
File MIME type: text/x-php
Migrating changes from the DRUPAL-6--1 branch to HEAD.
1 <?php
2 // $Id: workflow.install,v 1.13 2008/12/31 21:25:05 jvandyk Exp $
3
4 /**
5 * Implementation of hook_install().
6 */
7 function workflow_install() {
8 drupal_install_schema('workflow');
9 }
10
11 /**
12 * Implementation of hook_uninstall().
13 */
14 function workflow_uninstall() {
15 variable_del('workflow_states_per_page');
16 // Delete type-workflow mapping variables.
17 foreach (node_get_types() as $type => $name) {
18 variable_del('workflow_'. $type);
19 }
20 drupal_uninstall_schema('workflow');
21 }
22
23 /**
24 * Implementation of hook_schema().
25 */
26 function workflow_schema() {
27 $schema['workflows'] = array(
28 'fields' => array(
29 'wid' => array('type' => 'serial', 'not null' => TRUE),
30 'name' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
31 'tab_roles' => array('type' => 'varchar', 'length' => '60', 'not null' => TRUE, 'default' => ''),
32 'options' => array('type' => 'text', 'size' => 'big', 'not null' => FALSE)),
33 'primary key' => array('wid'),
34 );
35 $schema['workflow_type_map'] = array(
36 'fields' => array(
37 'type' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
38 'wid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10')),
39 'indexes' => array(
40 'type' => array('type', 'wid')),
41 );
42 $schema['workflow_transitions'] = array(
43 'fields' => array(
44 'tid' => array('type' => 'serial', 'not null' => TRUE),
45 'sid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
46 'target_sid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
47 'roles' => array('type' => 'varchar', 'length' => '255', 'not null' => FALSE)),
48 'primary key' => array('tid'),
49 'indexes' => array(
50 'sid' => array('sid'),
51 'target_sid' => array('target_sid')),
52 );
53 $schema['workflow_states'] = array(
54 'fields' => array(
55 'sid' => array('type' => 'serial', 'not null' => TRUE),
56 'wid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
57 'state' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
58 'weight' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0, 'disp-width' => '4'),
59 'sysid' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0, 'disp-width' => '4'),
60 'status' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 1, 'disp-width' => '4')),
61 'primary key' => array('sid'),
62 'indexes' => array(
63 'sysid' => array('sysid'),
64 'wid' => array('wid')),
65 );
66 $schema['workflow_scheduled_transition'] = array(
67 'fields' => array(
68 'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
69 'old_sid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
70 'sid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
71 'scheduled' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
72 'comment' => array('type' => 'text', 'size' => 'big', 'not null' => FALSE)),
73 'indexes' => array(
74 'nid' => array('nid')),
75 );
76 $schema['workflow_node_history'] = array(
77 'fields' => array(
78 'hid' => array('type' => 'serial', 'not null' => TRUE),
79 'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
80 'old_sid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
81 'sid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
82 'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
83 'stamp' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
84 'comment' => array('type' => 'text', 'size' => 'big', 'not null' => FALSE)),
85 'primary key' => array('hid'),
86 'indexes' => array(
87 'nid' => array('nid', 'sid')),
88 );
89 $schema['workflow_node'] = array(
90 'fields' => array(
91 'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
92 'sid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
93 'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
94 'stamp' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '11')),
95 'primary key' => array('nid'),
96 'indexes' => array(
97 'nid' => array('nid', 'sid')),
98 );
99
100 return $schema;
101 }
102
103
104 // Introduce workflow_node_history table so workflow_node is joinable for views.module.
105 function workflow_update_1() {
106 $ret = array();
107
108 switch ($GLOBALS['db_type']) {
109 case 'mysqli':
110 case 'mysql':
111 // Create new workflow_node_history table.
112 $ret[] = update_sql("CREATE TABLE {workflow_node_history} (
113 nid int(10) unsigned NOT NULL default '0',
114 sid int(10) unsigned NOT NULL default '0',
115 uid int(10) unsigned NOT NULL default '0',
116 stamp int(10) unsigned NOT NULL default '0',
117 KEY nid (nid,sid)
118 ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
119
120 // Copy data from the current workflow_node table.
121 $ret[] = update_sql("INSERT INTO {workflow_node_history} SELECT * FROM {workflow_node}");
122
123 // Delete older entries.
124 $result = db_query("SELECT w1.* FROM {workflow_node} w1 LEFT JOIN {workflow_node} AS w2 ON w1.nid = w2.nid AND w1.start < w2.start WHERE w2.start is NULL");
125 while ($record = db_fetch_array($result)) {
126 db_query("DELETE FROM {workflow_node} WHERE nid = %d", $record['nid']);
127 db_query("INSERT INTO {workflow_node} (nid, sid, uid) VALUES (%d, %d, %d)", $record['nid'], $record['sid'], $record['uid']);
128 }
129
130 $ret[] = update_sql("ALTER TABLE {workflow_node} DROP PRIMARY KEY");
131 $ret[] = update_sql("ALTER TABLE {workflow_node} DROP start");
132
133 // We can now use a unique primary key.
134 $ret[] = update_sql("ALTER TABLE {workflow_node} ADD PRIMARY KEY (nid)");
135 break;
136
137 case 'pgsql':
138 // Create new workflow_node_history table.
139 $ret[] = update_sql("CREATE TABLE {workflow_node_history} (
140 nid integer NOT NULL default '0',
141 sid integer NOT NULL default '0',
142 uid integer NOT NULL default '0',
143 stamp integer NOT NULL default '0'
144 );");
145 $ret[] = update_sql("CREATE INDEX {workflow_node_history}_nid_sid_idx ON {workflow_node_history}(nid,sid);");
146
147 // Copy data from the current workflow_node table.
148 $ret[] = update_sql("INSERT INTO {workflow_node_history} SELECT * FROM {workflow_node}");
149
150 // Delete older entries.
151 $result = db_query("SELECT w1.* FROM {workflow_node} w1 LEFT JOIN {workflow_node} AS w2 ON w1.nid = w2.nid AND w1.start < w2.start WHERE w2.start is NULL");
152 while ($record = db_fetch_array($result)) {
153 db_query("DELETE FROM {workflow_node} WHERE nid = %d", $record['nid']);
154 db_query("INSERT INTO {workflow_node} (nid, sid, uid) VALUES (%d, %d, %d)", $record['nid'], $record['sid'], $record['uid']);
155 }
156
157 $ret[] = update_sql("ALTER TABLE {workflow_node} DROP CONSTRAINT {workflow_node}_pkey");
158 $ret[] = update_sql("ALTER TABLE {workflow_node} DROP start");
159
160 // We can now use a unique primary key.
161 $ret[] = update_sql("ALTER TABLE {workflow_node} ADD PRIMARY KEY (nid)");
162 break;
163 }
164
165 return $ret;
166 }
167
168 // Make all tables UTF-8 compatible, workflow_node_history covered above.
169 function workflow_update_2() {
170 return _system_update_utf8(array('workflow_actions', 'workflow_node', 'workflow_states', 'workflow_transitions', 'workflow_type_map', 'workflows'));
171 }
172
173 // Keep record of old states and comment history.
174 function workflow_update_3() {
175 $ret = array();
176
177 switch ($GLOBALS['db_type']) {
178 case 'mysqli':
179 case 'mysql':
180 $ret[] = update_sql("ALTER TABLE {workflow_node_history} ADD old_sid int(10) unsigned NOT NULL AFTER nid");
181 $ret[] = update_sql("ALTER TABLE {workflow_node_history} ADD comment longtext");
182 $ret[] = update_sql("ALTER TABLE {workflows} ADD tab_roles varchar(60) NOT NULL");
183 break;
184
185 case 'pgsql':
186 db_add_column($ret, 'workflow_node_history', 'old_sid', 'integer', array('default' => 0, 'not null' => TRUE));
187 db_add_column($ret, 'workflow_node_history', 'comment', 'text', array('default' => '', 'not null' => TRUE));
188 db_add_column($ret, 'workflows', 'tab_roles', 'varchar(60)', array('default' => '', 'not null' => TRUE));
189 break;
190 }
191
192 return $ret;
193 }
194
195 // Update sequence names to be cross-database compatible.
196 function workflow_update_4() {
197 $ret = array();
198
199 switch ($GLOBALS['db_type']) {
200 case 'mysqli':
201 case 'mysql':
202 db_query('LOCK TABLES {sequences} WRITE');
203 $ret[] = _workflow_fix_seq('workflows', '{workflows}_wid');
204 $ret[] = _workflow_fix_seq('workflow_state', '{workflow_states}_sid');
205 $ret[] = _workflow_fix_seq('workflow_transitions', '{workflow_transitions}_tid');
206 db_query('UNLOCK TABLES');
207 break;
208 }
209
210 return $ret;
211 }
212
213 // Add stamp column in workflow_node to ease JOIN with history table. Needed for Views.
214 function workflow_update_5() {
215 $ret[] = update_sql("ALTER TABLE {workflow_node} ADD stamp int(10) unsigned AFTER uid");
216 $sql = "SELECT MAX(stamp) AS stamp, nid FROM {workflow_node_history} GROUP BY nid";
217 $result = db_query($sql);
218 while ($row = db_fetch_object($result)) {
219 $ret[] = update_sql("UPDATE {workflow_node} SET stamp = $row->stamp WHERE nid = $row->nid");
220 }
221 return $ret;
222 }
223
224 // Helper function to fix sequence table names.
225 function _workflow_fix_seq($old_name, $new_name) {
226 $new_name = db_prefix_tables($new_name);
227 return update_sql("UPDATE {sequences} SET name = '" . $new_name . "' WHERE name = '" . $old_name . "'");
228 }
229
230 // Add scheduling tables
231 function workflow_update_6() {
232 $ret = array();
233
234 switch ($GLOBALS['db_type']) {
235 case 'mysqli':
236 case 'mysql':
237 $ret[] = update_sql(
238 <<<QUERY
239 CREATE TABLE {workflow_scheduled_transition} (
240 nid int(10) unsigned NOT NULL default '0',
241 old_sid int(10) unsigned NOT NULL default '0',
242 sid int(10) unsigned NOT NULL default '0',
243 scheduled int(10) unsigned NOT NULL default '0',
244 comment longtext,
245 KEY nid (nid)
246 ) /*!40100 DEFAULT CHARACTER SET utf8 */;
247 QUERY
248 );
249 break;
250 case 'pgsql':
251 $ret[] = update_sql(
252 <<<QUERY
253 CREATE TABLE {workflow_scheduled_transition} (
254 nid integer NOT NULL default '0',
255 old_sid integer NOT NULL default '0',
256 sid integer NOT NULL default '0',
257 scheduled integer NOT NULL default '0',
258 comment text
259 );
260 QUERY
261 );
262 $ret[] = update_sql(
263 <<<QUERY
264 CREATE INDEX {workflow_scheduled_transition}_nid_idx ON {workflow_scheduled_transition}(nid);
265 QUERY
266 );
267
268 break;
269 }
270
271 return $ret;
272 }
273
274 // We no longer keep track of workflow actions separately in our own table.
275 function workflow_update_5200() {
276 $ret = array();
277 switch ($GLOBALS['db_type']) {
278 case 'mysqli':
279 case 'mysql':
280 case 'pgsql':
281 // Move workflow action assignments to the actions_assignments table.
282 if (module_exists('actions')) {
283 $result = db_query("SELECT * FROM {workflow_actions}");
284 if ($result) {
285 $success = FALSE;
286 while ($data = db_fetch_object($result)) {
287 $success = db_query("INSERT INTO {actions_assignments} (hook, op, aid, weight) VALUES ('%s', '%s', '%s', %d)", 'workflow', 'workflow-'. $data->tid, $data->aid, $data->weight);
288 }
289 }
290 else {
291 // workflow_actions had no records; it can be safely dropped.
292 $success = TRUE;
293 }
294
295 // The workflow_actions table is no longer needed.
296 if ($success) {
297 $ret[] = update_sql("DROP TABLE {workflow_actions}");
298 }
299 }
300 }
301 return $ret;
302 }
303
304 // Add a field to track workflow history entry order.
305 function workflow_update_5201() {
306 $ret = array();
307 switch ($GLOBALS['db_type']) {
308 case 'mysqli':
309 case 'mysql':
310 $ret[] = update_sql("ALTER TABLE {workflow_node_history} ADD hid INT NOT NULL AUTO_INCREMENT PRIMARY KEY");
311 break;
312
313 case 'pgsql':
314 $ret[] = update_sql("ALTER TABLE {workflow_node_history} ADD hid SERIAL");
315 break;
316 }
317 return $ret;
318 }
319
320 // Add status field to workflow states table so that deleted states can simply
321 // be marked deleted without actually being removed (that way they still show up
322 // in workflow history for a node).
323 function workflow_update_5202() {
324 $ret = array();
325 switch ($GLOBALS['db_type']) {
326 case 'mysqli':
327 case 'mysql':
328 $ret[] = update_sql("ALTER TABLE {workflow_states} ADD status TINYINT NOT NULL DEFAULT '1'");
329 break;
330
331 case 'pgsql':
332 $ret[] = update_sql("ALTER TABLE {workflow_states} ADD status SMALLINT NOT NULL DEFAULT '1'");
333 break;
334 }
335 return $ret;
336 }
337
338 // We now require that a transition include both a descriptor and a target state instead
339 // of just a target state. So ops are changing from 'workflow-35' to 'workflow-page-35'.
340 function workflow_update_5203() {
341 $ret = array();
342 if (module_exists('actions')) {
343 $result = db_query("SELECT hook, op, aid, weight FROM {actions_assignments} WHERE hook = 'workflow'");
344 while ($data = db_fetch_object($result)) {
345 $op_parts = explode('-', $data->op);
346 // The ops we have to update have only two parts, e.g., workflow-35.
347 if (count($op_parts) == 3) {
348 continue;
349 }
350 $tid = $op_parts[1];
351 // Assign a type to this hook.
352 $wid = db_result(db_query("SELECT ws.wid FROM {workflow_states} ws LEFT JOIN {workflow_transitions} wt ON ws.sid = wt.sid WHERE wt.tid = %d", $tid));
353 // Get the first node type associated with this hook (if there are multiple types,
354 // we can't decide between them so we take the first one).
355 $type = db_result(db_query("SELECT type FROM {workflow_type_map} WHERE wid = %d LIMIT 1", $wid));
356 $new_op = 'workflow-'. $type .'-'. $tid;
357 $query_result = db_query("UPDATE {actions_assignments} SET op = '%s' WHERE hook = 'workflow' AND op = '%s' AND aid = '%s' AND weight = %d", $new_op, $data->op, $data->aid, $data->weight);
358 $ret[] = array('success' => $query_result !== FALSE, 'query' => check_plain('op '. $data->op .' => '. $new_op));
359 }
360 }
361 return $ret;
362 }
363
364 // The workflow_transitions field was too small, resulting in truncation with many roles.
365 function workflow_update_5204() {
366 $ret = array();
367 switch ($GLOBALS['db_type']) {
368 case 'mysqli':
369 case 'mysql':
370 $ret[] = update_sql("ALTER TABLE {workflow_transitions} CHANGE roles roles VARCHAR(255) NULL DEFAULT NULL");
371 break;
372 case 'pgsql':
373 $ret[] = update_sql("BEGIN;
374 ALTER TABLE {workflow_transitions} ADD COLUMN roles_temp VARCHAR(255);
375 UPDATE roles_temp SET new_col = CAST(roles AS VARCHAR(255));
376 ALTER TABLE {actions_assignments} DROP COLUMN roles;
377 RENAME roles_temp TO roles;
378 COMMIT;");
379 break;
380 }
381 return $ret;
382 }
383
384 // Add a general data field for storing workflow options.
385 function workflow_update_6100() {
386 $ret = array();
387 db_add_field($ret, 'workflows', 'options', array('type' => 'text', 'size' => 'big', 'not null' => FALSE));
388 // Seed existing workflows with the behavior of showing the comment field.
389 $default = serialize(array('comment_log_node' => 1, 'comment_log_tab' => 1));
390 $query_result = db_query("UPDATE {workflows} SET options = '%s'", $default);
391 $ret[] = array('success' => $query_result !== FALSE, 'query' => "UPDATE {workflows} SET options = '$default'");
392 return $ret;
393 }
394
395 // Tables upgraded from 5.x needs AUTOINCREMENT set.
396 function workflow_update_6101() {
397 $ret = array();
398 $workflows = $workflow_states = $workflow_transitions = FALSE;
399 // Test to see if the autoincrement attribute is present.
400 switch ($GLOBALS['db_type']) {
401 case 'mysqli':
402 case 'mysql':
403 $workflows = db_result(db_query("SHOW COLUMNS FROM workflows WHERE field = 'wid' and extra REGEXP 'auto_increment'"));
404 $workflow_states = db_result(db_query("SHOW COLUMNS FROM workflow_states WHERE field = 'sid' and extra REGEXP 'auto_increment'"));
405 $workflow_transitions = db_result(db_query("SHOW COLUMNS FROM workflow_transitions WHERE field = 'tid' and extra REGEXP 'auto_increment'"));
406 break;
407 case 'pgsql':
408 // Not sure how determine if a PostgreSQL field has a sequence.
409 break;
410 }
411 if ($workflows === FALSE) {
412 db_drop_primary_key($ret, 'workflows');
413 db_change_field($ret, 'workflows', 'wid', 'wid', array('type' => 'serial', 'not null' => TRUE), array('primary key' => array('wid')));
414 }
415 if ($workflow_states === FALSE) {
416 db_drop_primary_key($ret, 'workflow_states');
417 db_change_field($ret, 'workflow_states', 'sid', 'sid', array('type' => 'serial', 'not null' => TRUE), array('primary key' => array('sid')));
418 }
419 if ($workflow_transitions === FALSE) {
420 db_drop_primary_key($ret, 'workflow_transitions');
421 db_change_field($ret, 'workflow_transitions', 'tid', 'tid', array('type' => 'serial', 'not null' => TRUE), array('primary key' => array('tid')));
422 }
423 return $ret;
424 }

  ViewVC Help
Powered by ViewVC 1.1.2