/
workflow.install
754 lines (678 loc) · 28.9 KB
/
workflow.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
<?php
// $Id: workflow.install,v 1.13.2.3.2.2 2010/08/30 14:45:49 q0rban Exp $
/**
* Implementation of hook_install().
*/
function workflow_install() {
drupal_install_schema('workflow');
}
/**
* Implementation of hook_uninstall().
*/
function workflow_uninstall() {
variable_del('workflow_states_per_page');
// Delete type-workflow mapping variables.
foreach (node_get_types() as $type => $name) {
variable_del('workflow_'. $type);
}
drupal_uninstall_schema('workflow');
}
/**
* Implementation of hook_schema().
*/
function workflow_schema() {
$schema['workflows'] = array(
'fields' => array(
'wid' => array('type' => 'serial', 'not null' => TRUE),
'name' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
'label' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
'tab_roles' => array('type' => 'varchar', 'length' => '60', 'not null' => TRUE, 'default' => ''),
'options' => array('type' => 'text', 'size' => 'big', 'not null' => FALSE, 'serialize' => TRUE)),
'primary key' => array('wid'),
'unique keys' => array('name' => array('name')),
);
$schema['workflow_type_map'] = array(
'fields' => array(
'type' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
'workflow_name' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => '')),
'indexes' => array(
'type' => array('type'),
'workflow_name' => array('workflow_name')),
);
$schema['workflow_transitions'] = array(
'fields' => array(
'tid' => array('type' => 'serial', 'not null' => TRUE),
'state_name' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
'target_state_name' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
'roles' => array('type' => 'varchar', 'length' => '255', 'not null' => FALSE)),
'primary key' => array('tid'),
'indexes' => array(
'state_name' => array('state_name'),
'target_state_name' => array('target_state_name')),
);
$schema['workflow_states'] = array(
'export' => array(
'key name' => 'Machine Name',
'primary key' => 'sid',
'identifier' => 'workflow_state',
'default hook' => 'workflow_default_states',
'api' => array(
'owner' => 'workflow',
'api' => 'workflow_default_states',
'minimum version' => 2,
'current version' => 2,
),
),
'fields' => array(
'sid' => array('type' => 'serial', 'not null' => TRUE),
'name' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
'workflow_name' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
'label' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
'weight' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0, 'disp-width' => '4'),
'sysid' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 0, 'disp-width' => '4'),
'status' => array('type' => 'int', 'size' => 'tiny', 'not null' => TRUE, 'default' => 1, 'disp-width' => '4')),
'primary key' => array('sid'),
'unique keys' => array('name' => array('name')),
'indexes' => array(
'sysid' => array('sysid'),
'workflow_name' => array('workflow_name')),
);
$schema['workflow_scheduled_transition'] = array(
'fields' => array(
'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
'old_state_name' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
'state_name' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
'scheduled' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
'comment' => array('type' => 'text', 'size' => 'big', 'not null' => FALSE)),
'indexes' => array(
'nid' => array('nid')),
);
$schema['workflow_node_history'] = array(
'fields' => array(
'hid' => array('type' => 'serial', 'not null' => TRUE),
'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
'old_state_name' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
'state_name' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
'stamp' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
'comment' => array('type' => 'text', 'size' => 'big', 'not null' => FALSE)),
'primary key' => array('hid'),
'indexes' => array(
'nid' => array('nid', 'state_name')),
);
$schema['workflow_node'] = array(
'fields' => array(
'nid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
'state_name' => array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => ''),
'uid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '10'),
'stamp' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0, 'disp-width' => '11')),
'primary key' => array('nid'),
'indexes' => array(
'nid' => array('nid', 'state_name')),
);
return $schema;
}
// Introduce workflow_node_history table so workflow_node is joinable for views.module.
function workflow_update_1() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
// Create new workflow_node_history table.
$ret[] = update_sql("CREATE TABLE {workflow_node_history} (
nid int(10) unsigned NOT NULL default '0',
sid int(10) unsigned NOT NULL default '0',
uid int(10) unsigned NOT NULL default '0',
stamp int(10) unsigned NOT NULL default '0',
KEY nid (nid,sid)
) /*!40100 DEFAULT CHARACTER SET utf8 */;");
// Copy data from the current workflow_node table.
$ret[] = update_sql("INSERT INTO {workflow_node_history} SELECT * FROM {workflow_node}");
// Delete older entries.
$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");
while ($record = db_fetch_array($result)) {
db_query("DELETE FROM {workflow_node} WHERE nid = %d", $record['nid']);
db_query("INSERT INTO {workflow_node} (nid, sid, uid) VALUES (%d, %d, %d)", $record['nid'], $record['sid'], $record['uid']);
}
$ret[] = update_sql("ALTER TABLE {workflow_node} DROP PRIMARY KEY");
$ret[] = update_sql("ALTER TABLE {workflow_node} DROP start");
// We can now use a unique primary key.
$ret[] = update_sql("ALTER TABLE {workflow_node} ADD PRIMARY KEY (nid)");
break;
case 'pgsql':
// Create new workflow_node_history table.
$ret[] = update_sql("CREATE TABLE {workflow_node_history} (
nid integer NOT NULL default '0',
sid integer NOT NULL default '0',
uid integer NOT NULL default '0',
stamp integer NOT NULL default '0'
);");
$ret[] = update_sql("CREATE INDEX {workflow_node_history}_nid_sid_idx ON {workflow_node_history}(nid,sid);");
// Copy data from the current workflow_node table.
$ret[] = update_sql("INSERT INTO {workflow_node_history} SELECT * FROM {workflow_node}");
// Delete older entries.
$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");
while ($record = db_fetch_array($result)) {
db_query("DELETE FROM {workflow_node} WHERE nid = %d", $record['nid']);
db_query("INSERT INTO {workflow_node} (nid, sid, uid) VALUES (%d, %d, %d)", $record['nid'], $record['sid'], $record['uid']);
}
$ret[] = update_sql("ALTER TABLE {workflow_node} DROP CONSTRAINT {workflow_node}_pkey");
$ret[] = update_sql("ALTER TABLE {workflow_node} DROP start");
// We can now use a unique primary key.
$ret[] = update_sql("ALTER TABLE {workflow_node} ADD PRIMARY KEY (nid)");
break;
}
return $ret;
}
// Make all tables UTF-8 compatible, workflow_node_history covered above.
function workflow_update_2() {
return _system_update_utf8(array('workflow_actions', 'workflow_node', 'workflow_states', 'workflow_transitions', 'workflow_type_map', 'workflows'));
}
// Keep record of old states and comment history.
function workflow_update_3() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
$ret[] = update_sql("ALTER TABLE {workflow_node_history} ADD old_sid int(10) unsigned NOT NULL AFTER nid");
$ret[] = update_sql("ALTER TABLE {workflow_node_history} ADD comment longtext");
$ret[] = update_sql("ALTER TABLE {workflows} ADD tab_roles varchar(60) NOT NULL");
break;
case 'pgsql':
db_add_column($ret, 'workflow_node_history', 'old_sid', 'integer', array('default' => 0, 'not null' => TRUE));
db_add_column($ret, 'workflow_node_history', 'comment', 'text', array('default' => '', 'not null' => TRUE));
db_add_column($ret, 'workflows', 'tab_roles', 'varchar(60)', array('default' => '', 'not null' => TRUE));
break;
}
return $ret;
}
// Update sequence names to be cross-database compatible.
function workflow_update_4() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
db_query('LOCK TABLES {sequences} WRITE');
$ret[] = _workflow_fix_seq('workflows', '{workflows}_wid');
$ret[] = _workflow_fix_seq('workflow_state', '{workflow_states}_sid');
$ret[] = _workflow_fix_seq('workflow_transitions', '{workflow_transitions}_tid');
db_query('UNLOCK TABLES');
break;
}
return $ret;
}
// Add stamp column in workflow_node to ease JOIN with history table. Needed for Views.
function workflow_update_5() {
$ret[] = update_sql("ALTER TABLE {workflow_node} ADD stamp int(10) unsigned AFTER uid");
$sql = "SELECT MAX(stamp) AS stamp, nid FROM {workflow_node_history} GROUP BY nid";
$result = db_query($sql);
while ($row = db_fetch_object($result)) {
$ret[] = update_sql("UPDATE {workflow_node} SET stamp = $row->stamp WHERE nid = $row->nid");
}
return $ret;
}
// Helper function to fix sequence table names.
function _workflow_fix_seq($old_name, $new_name) {
$new_name = db_prefix_tables($new_name);
return update_sql("UPDATE {sequences} SET name = '" . $new_name . "' WHERE name = '" . $old_name . "'");
}
// Add scheduling tables
function workflow_update_6() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
$ret[] = update_sql(
<<<QUERY
CREATE TABLE {workflow_scheduled_transition} (
nid int(10) unsigned NOT NULL default '0',
old_sid int(10) unsigned NOT NULL default '0',
sid int(10) unsigned NOT NULL default '0',
scheduled int(10) unsigned NOT NULL default '0',
comment longtext,
KEY nid (nid)
) /*!40100 DEFAULT CHARACTER SET utf8 */;
QUERY
);
break;
case 'pgsql':
$ret[] = update_sql(
<<<QUERY
CREATE TABLE {workflow_scheduled_transition} (
nid integer NOT NULL default '0',
old_sid integer NOT NULL default '0',
sid integer NOT NULL default '0',
scheduled integer NOT NULL default '0',
comment text
);
QUERY
);
$ret[] = update_sql(
<<<QUERY
CREATE INDEX {workflow_scheduled_transition}_nid_idx ON {workflow_scheduled_transition}(nid);
QUERY
);
break;
}
return $ret;
}
// We no longer keep track of workflow actions separately in our own table.
function workflow_update_5200() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
case 'pgsql':
// Move workflow action assignments to the actions_assignments table.
if (module_exists('actions')) {
$result = db_query("SELECT * FROM {workflow_actions}");
if ($result) {
$success = FALSE;
while ($data = db_fetch_object($result)) {
$success = db_query("INSERT INTO {actions_assignments} (hook, op, aid, weight) VALUES ('%s', '%s', '%s', %d)", 'workflow', 'workflow-'. $data->tid, $data->aid, $data->weight);
}
}
else {
// workflow_actions had no records; it can be safely dropped.
$success = TRUE;
}
// The workflow_actions table is no longer needed.
if ($success) {
$ret[] = update_sql("DROP TABLE {workflow_actions}");
}
}
}
return $ret;
}
// Add a field to track workflow history entry order.
function workflow_update_5201() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
$ret[] = update_sql("ALTER TABLE {workflow_node_history} ADD hid INT NOT NULL AUTO_INCREMENT PRIMARY KEY");
break;
case 'pgsql':
$ret[] = update_sql("ALTER TABLE {workflow_node_history} ADD hid SERIAL");
break;
}
return $ret;
}
// Add status field to workflow states table so that deleted states can simply
// be marked deleted without actually being removed (that way they still show up
// in workflow history for a node).
function workflow_update_5202() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
$ret[] = update_sql("ALTER TABLE {workflow_states} ADD status TINYINT NOT NULL DEFAULT '1'");
break;
case 'pgsql':
$ret[] = update_sql("ALTER TABLE {workflow_states} ADD status SMALLINT NOT NULL DEFAULT '1'");
break;
}
return $ret;
}
// We now require that a transition include both a descriptor and a target state instead
// of just a target state. So ops are changing from 'workflow-35' to 'workflow-page-35'.
function workflow_update_5203() {
$ret = array();
if (module_exists('actions')) {
$result = db_query("SELECT hook, op, aid, weight FROM {actions_assignments} WHERE hook = 'workflow'");
while ($data = db_fetch_object($result)) {
$op_parts = explode('-', $data->op);
// The ops we have to update have only two parts, e.g., workflow-35.
if (count($op_parts) == 3) {
continue;
}
$tid = $op_parts[1];
// Assign a type to this hook.
$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));
// Get the first node type associated with this hook (if there are multiple types,
// we can't decide between them so we take the first one).
$type = db_result(db_query("SELECT type FROM {workflow_type_map} WHERE wid = %d LIMIT 1", $wid));
$new_op = 'workflow-'. $type .'-'. $tid;
$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);
$ret[] = array('success' => $query_result !== FALSE, 'query' => check_plain('op '. $data->op .' => '. $new_op));
}
}
return $ret;
}
// The workflow_transitions field was too small, resulting in truncation with many roles.
function workflow_update_5204() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
$ret[] = update_sql("ALTER TABLE {workflow_transitions} CHANGE roles roles VARCHAR(255) NULL DEFAULT NULL");
break;
case 'pgsql':
$ret[] = update_sql("BEGIN;
ALTER TABLE {workflow_transitions} ADD COLUMN roles_temp VARCHAR(255);
UPDATE roles_temp SET new_col = CAST(roles AS VARCHAR(255));
ALTER TABLE {actions_assignments} DROP COLUMN roles;
RENAME roles_temp TO roles;
COMMIT;");
break;
}
return $ret;
}
// Add a general data field for storing workflow options.
function workflow_update_6100() {
$ret = array();
db_add_field($ret, 'workflows', 'options', array('type' => 'text', 'size' => 'big', 'not null' => FALSE));
// Seed existing workflows with the behavior of showing the comment field.
$default = serialize(array('comment_log_node' => 1, 'comment_log_tab' => 1));
$query_result = db_query("UPDATE {workflows} SET options = '%s'", $default);
$ret[] = array('success' => $query_result !== FALSE, 'query' => "UPDATE {workflows} SET options = '$default'");
return $ret;
}
// Tables upgraded from 5.x needs AUTOINCREMENT set.
function workflow_update_6101() {
$ret = array();
$workflows = $workflow_states = $workflow_transitions = FALSE;
// Test to see if the autoincrement attribute is present.
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
$workflows = db_result(db_query("SHOW COLUMNS FROM {workflows} WHERE field = 'wid' and extra REGEXP 'auto_increment'"));
$workflow_states = db_result(db_query("SHOW COLUMNS FROM {workflow_states} WHERE field = 'sid' and extra REGEXP 'auto_increment'"));
$workflow_transitions = db_result(db_query("SHOW COLUMNS FROM {workflow_transitions} WHERE field = 'tid' and extra REGEXP 'auto_increment'"));
break;
case 'pgsql':
// Not sure how determine if a PostgreSQL field has a sequence.
break;
}
if ($workflows === FALSE) {
db_drop_primary_key($ret, 'workflows');
db_change_field($ret, 'workflows', 'wid', 'wid', array('type' => 'serial', 'not null' => TRUE), array('primary key' => array('wid')));
}
if ($workflow_states === FALSE) {
db_drop_primary_key($ret, 'workflow_states');
db_change_field($ret, 'workflow_states', 'sid', 'sid', array('type' => 'serial', 'not null' => TRUE), array('primary key' => array('sid')));
}
if ($workflow_transitions === FALSE) {
db_drop_primary_key($ret, 'workflow_transitions');
db_change_field($ret, 'workflow_transitions', 'tid', 'tid', array('type' => 'serial', 'not null' => TRUE), array('primary key' => array('tid')));
}
return $ret;
}
/**
* Update Workflow to use machine names
*/
function workflow_update_6200() {
$ret = array();
$schema = _workflow_update_6200_schema();
_workflow_update_6200($ret, $schema);
return $ret;
}
/**
* Helper function to the 6200 update. workflow_access needs this functionality
* as well, so it stands alone from the update function.
*/
function _workflow_update_6200(&$ret, $schema) {
// Rename existing fields and add new ones as needed.
foreach ($schema as $table => $table_schema) {
if (!empty($table_schema['changed fields'])) {
foreach ($table_schema['changed fields'] as $args) {
db_change_field($ret, $table, $args['field'], $args['field_new'], $args['spec']);
}
}
if (!empty($table_schema['fields'])) {
// Add new fields to appropriate tables.
foreach ($table_schema['fields'] as $name => $field) {
db_add_field($ret, $table, $name, $field);
}
}
}
}
/**
* Set up machine names on existing workflows. We do this in a separate update
* so we can do them in batches of 100 via $sandbox.
*/
function workflow_update_6201(&$sandbox = NULL) {
$ret = array();
$schema = _workflow_update_6200_schema();
// We use $sandbox here to do these in batches of 100.
if (!isset($sandbox['max_wid'])) {
$sandbox['current_wid'] = 0;
$sandbox['max_wid'] = db_result(db_query('SELECT MAX(wid) FROM {workflows}'));
}
$result = db_query("SELECT * FROM {workflows} WHERE wid > %d ORDER BY wid ASC LIMIT 100", $sandbox['current_wid']);
while ($obj = db_fetch_object($result)) {
// First we'll create a new machine name for existing workflows.
$name = _workflow_update_6200_name_gen($obj->label, $obj->wid);
$sql = "UPDATE {workflows} SET name = '%s' WHERE wid = %d";
$success = db_query($sql, $name, $obj->wid);
$ret[] = array('success' => $success !== FALSE, 'query' => check_plain($sql));
// Update the workflow type map with the new workflow name.
$sql = "UPDATE {workflow_type_map} SET workflow_name = '%s' WHERE wid = %d";
$success = db_query($sql, $name, $obj->wid);
$ret[] = array('success' => $success !== FALSE, 'query' => check_plain($sql));
// Update the workflow states with the new workflow name.
$sql = "UPDATE {workflow_states} SET workflow_name = '%s' WHERE wid = %d";
$success = db_query($sql, $name, $obj->wid);
$ret[] = array('success' => $success !== FALSE, 'query' => check_plain($sql));
$sandbox['current_wid'] = $obj->wid;
}
$ret['#finished'] = empty($sandbox['max_wid']) ? 1 : ($sandbox['current_wid'] / $sandbox['max_wid']);
return $ret;
}
/**
* Set up machine names on existing workflows states. Again, in a separate update
* so we can do them in batches of 100 via $sandbox.
*/
function workflow_update_6202(&$sandbox = NULL) {
$ret = array();
$schema = _workflow_update_6200_schema();
// We use $sandbox here to do these in batches of 100.
if (!isset($sandbox['max_sid'])) {
$sandbox['current_sid'] = 0;
$sandbox['max_sid'] = db_result(db_query('SELECT MAX(sid) FROM {workflow_states}'));
}
$result = db_query("SELECT * FROM {workflow_states} WHERE sid > %d ORDER BY sid ASC LIMIT 100", $sandbox['current_sid']);
while ($obj = db_fetch_object($result)) {
// Create a new machine name for existing states.
if ($obj->label == t('(creation)')) {
$name = _workflow_creation_state($obj->workflow_name);
}
else {
$name = _workflow_update_6200_name_gen($obj->label, $obj->sid);
}
$sql = "UPDATE {workflow_states} SET name = '%s' WHERE sid = %d";
$success = db_query($sql, $name, $obj->sid);
$ret[] = array('success' => $success !== FALSE, 'query' => check_plain($sql));
// Update the workflow_transitions table. There are two columns to update:
// state_name, and target_state_name.
$sql = "UPDATE {workflow_transitions} SET state_name = '%s' WHERE sid = %d";
$success = db_query($sql, $name, $obj->sid);
$ret[] = array('success' => $success !== FALSE, 'query' => check_plain($sql));
// target_sid to target_state_name
$sql = "UPDATE {workflow_transitions} SET target_state_name = '%s' WHERE target_sid = %d";
$success = db_query($sql, $name, $obj->sid);
$ret[] = array('success' => $success !== FALSE, 'query' => check_plain($sql));
// Update the workflow_scheduled_transition table. There are two columns to update:
// old_state_name, and state_name.
$sql = "UPDATE {workflow_scheduled_transition} SET state_name = '%s' WHERE sid = %d";
$success = db_query($sql, $name, $obj->sid);
$ret[] = array('success' => $success !== FALSE, 'query' => check_plain($sql));
// target_sid to target_state_name
$sql = "UPDATE {workflow_scheduled_transition} SET old_state_name = '%s' WHERE old_sid = %d";
$success = db_query($sql, $name, $obj->sid);
$ret[] = array('success' => $success !== FALSE, 'query' => check_plain($sql));
// Update the workflow_node_history table. There are two columns to update:
// old_state_name, and state_name.
$sql = "UPDATE {workflow_node_history} SET state_name = '%s' WHERE sid = %d";
$success = db_query($sql, $name, $obj->sid);
$ret[] = array('success' => $success !== FALSE, 'query' => check_plain($sql));
// target_sid to target_state_name
$sql = "UPDATE {workflow_node_history} SET old_state_name = '%s' WHERE old_sid = %d";
$success = db_query($sql, $name, $obj->sid);
$ret[] = array('success' => $success !== FALSE, 'query' => check_plain($sql));
// Update the workflow_node table with a state_name.
$sql = "UPDATE {workflow_node} SET state_name = '%s' WHERE sid = %d";
$success = db_query($sql, $name, $obj->sid);
$ret[] = array('success' => $success !== FALSE, 'query' => check_plain($sql));
$sandbox['current_sid'] = $obj->sid;
}
$ret['#finished'] = empty($sandbox['max_sid']) ? 1 : ($sandbox['current_sid'] / $sandbox['max_sid']);
return $ret;
}
/**
* Add/delete unique keys/indexes and drop old fields where necessary based on
* the new 6200 schema.
*/
function workflow_update_6203() {
$ret = array();
$schema = _workflow_update_6200_schema();
_workflow_update_6203($ret, $schema);
return $ret;
}
/**
* Helper function to the 6203 update. workflow_access needs this functionality
* as well, so it stands alone from the update function.
*/
function _workflow_update_6203(&$ret, $schema) {
// Now that that's done,
foreach ($schema as $table => $table_schema) {
if (!empty($table_schema['deleted indexes'])) {
// Delete old indexes.
foreach ($table_schema['deleted indexes'] as $index) {
db_drop_index($ret, $table, $index);
}
}
if (!empty($table_schema['indexes'])) {
// Add new indexes to appropriate tables.
foreach ($table_schema['indexes'] as $name => $fields) {
db_add_index($ret, $table, $name, $fields);
}
}
if (!empty($table_schema['unique keys'])) {
// Add new unique keys to appropriate tables.
foreach ($table_schema['unique keys'] as $name => $fields) {
db_add_unique_key($ret, $table, $name, $fields);
}
}
if (!empty($table_schema['deleted fields'])) {
// Delete fields as needed.
foreach ($table_schema['deleted fields'] as $field) {
db_drop_field($ret, $table, $field);
}
}
}
}
/**
* Helper function to set up the 6200 schema changes in an array.
*/
function _workflow_update_6200_schema() {
// Set up the schema changes in an array.
// Every name field is the same.
$name_field = array('type' => 'varchar', 'length' => '255', 'not null' => TRUE, 'default' => '');
$schema = array();
$schema['workflows'] = array(
'changed fields' => array(
'name' => array(
'field' => 'name',
'field_new' => 'label',
'spec' => $name_field,
),
),
'fields' => array('name' => $name_field + array('sql')),
'unique keys' => array('name' => array('name')),
);
$schema['workflow_type_map'] = array(
'fields' => array('workflow_name' => $name_field),
'deleted fields' => array('wid'),
'indexes' => array(
'type' => array('type'),
'workflow_name' => array('workflow_name'),
),
'deleted indexes' => array('type'),
);
$schema['workflow_transitions'] = array(
'fields' => array(
'state_name' => $name_field,
'target_state_name' => $name_field,
),
'deleted fields' => array('sid', 'target_sid'),
'indexes' => array(
'state_name' => array('state_name'),
'target_state_name' => array('target_state_name')
),
'deleted indexes' => array('sid', 'target_sid'),
);
$schema['workflow_states'] = array(
'changed fields' => array(
'state' => array(
'field' => 'state',
'field_new' => 'label',
'spec' => $name_field,
),
),
'fields' => array(
'name' => $name_field,
'workflow_name' => $name_field,
),
'unique keys' => array('name' => array('name')),
'deleted fields' => array('wid'),
'indexes' => array('workflow_name' => array('workflow_name')),
'deleted indexes' => array('wid'),
);
$schema['workflow_scheduled_transition'] = array(
'fields' => array(
'old_state_name' => $name_field,
'state_name' => $name_field,
),
'deleted fields' => array('sid', 'old_sid'),
);
$schema['workflow_node_history'] = array(
'fields' => array(
'old_state_name' => $name_field,
'state_name' => $name_field,
),
'deleted fields' => array('sid', 'old_sid'),
'indexes' => array(
'nid' => array('nid', 'state_name'),
),
'deleted indexes' => array('nid'),
);
$schema['workflow_node'] = array(
'fields' => array(
'state_name' => $name_field,
),
'deleted fields' => array('sid'),
'indexes' => array(
'nid' => array('nid', 'state_name'),
),
'deleted indexes' => array('nid'),
);
return $schema;
}
/**
* Turn a ascii string into a machine name.
*/
function _workflow_update_6200_name_gen($string, $id) {
// First let's make it lowercase.
$output = strtolower($string);
// Convert spaces to underscores.
$output = str_replace(' ', '_', $output);
// Then filter out any silly characters.
$output = preg_replace("/[^[:alnum:]_]/", '', $output);
// We add one here to account for the underscore delimiter.
$length = strlen($output) + strlen($id) + 1;
// Trim the name down if necessary. This is a HIGHLY unlikely occurence.
$output = _workflow_update_6200_name_trim($output, $length - 255);
return $output .'_'. $id;
}
/**
* If necessary, trim a string down to 255 characters.
*/
function _workflow_update_6200_name_trim($string, $num) {
// Only trim if $num is positive.
if ($num > 0) {
$new_length = strlen($string) - $num;
$string = substr($string, 0, $new_length);
}
return $string;
}