View source
<?php
function workflow_install() {
$result = array();
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
$result[] = db_query(<<<QUERY
CREATE TABLE {workflow_states} (
sid int(10) unsigned NOT NULL default '0',
wid int(10) unsigned NOT NULL default '0',
state varchar(255) NOT NULL default '',
weight tinyint(4) NOT NULL default '0',
sysid tinyint(4) NOT NULL default '0',
status tinyint(4) NOT NULL default '1',
PRIMARY KEY (sid),
KEY wid (wid),
KEY sysid (sysid)
) /*!40100 DEFAULT CHARACTER SET utf8 */;
QUERY
);
$result[] = db_query(<<<QUERY
CREATE TABLE {workflow_transitions} (
tid int(10) unsigned NOT NULL default '0',
sid int(10) unsigned NOT NULL default '0',
target_sid int(10) unsigned NOT NULL default '0',
roles varchar(255) default NULL,
PRIMARY KEY (tid),
KEY sid (sid),
KEY target_sid (target_sid)
) /*!40100 DEFAULT CHARACTER SET utf8 */;
QUERY
);
$result[] = db_query(<<<QUERY
CREATE TABLE {workflows} (
wid int(10) unsigned NOT NULL default '0',
name varchar(255) NOT NULL default '',
tab_roles varchar(60) NOT NULL default '',
PRIMARY KEY (wid)
) /*!40100 DEFAULT CHARACTER SET utf8 */;
QUERY
);
$result[] = db_query(<<<QUERY
CREATE TABLE {workflow_type_map} (
type varchar(255) NOT NULL default '',
wid int(10) unsigned NOT NULL default '0',
KEY type (type,wid)
) /*!40100 DEFAULT CHARACTER SET utf8 */;
QUERY
);
$result[] = db_query(<<<QUERY
CREATE TABLE {workflow_node} (
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(11) unsigned NOT NULL default '0',
PRIMARY KEY (nid),
KEY nid (nid,sid)
) /*!40100 DEFAULT CHARACTER SET utf8 */;
QUERY
);
$result[] = db_query(<<<QUERY
CREATE TABLE {workflow_node_history} (
hid int(10) unsigned NOT NULL auto_increment,
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',
uid int(10) unsigned NOT NULL default '0',
stamp int(10) unsigned NOT NULL default '0',
comment longtext,
PRIMARY KEY (hid),
KEY nid (nid,sid)
) /*!40100 DEFAULT CHARACTER SET utf8 */;
QUERY
);
$result[] = db_query(<<<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':
$result[] = db_query(<<<QUERY
CREATE TABLE {workflow_states} (
sid SERIAL,
wid integer NOT NULL default '0',
state varchar(255) NOT NULL default '',
weight smallint NOT NULL default '0',
sysid smallint NOT NULL default '0',
status smallint NOT NULL default '1',
PRIMARY KEY (sid)
);
QUERY
);
$result[] = db_query(<<<QUERY
CREATE INDEX {workflow_states}_wid_idx ON {workflow_states}(wid);
QUERY
);
$result[] = db_query(<<<QUERY
CREATE INDEX {workflow_states}_sysid_idx ON {workflow_states}(sysid);
QUERY
);
$result[] = db_query(<<<QUERY
CREATE TABLE {workflow_transitions} (
tid SERIAL,
sid integer NOT NULL default '0',
target_sid integer NOT NULL default '0',
roles varchar(60) default NULL,
PRIMARY KEY (tid)
);
QUERY
);
$result[] = db_query(<<<QUERY
CREATE INDEX {workflow_transitions}_sid_idx ON {workflow_transitions}(sid);
QUERY
);
$result[] = db_query(<<<QUERY
CREATE INDEX {workflow_transitions}_target_sid_idx ON {workflow_transitions}(target_sid);
QUERY
);
$result[] = db_query(<<<QUERY
CREATE TABLE {workflows} (
wid SERIAL,
name varchar(255) NOT NULL default '',
tab_roles varchar(60) NOT NULL default '',
PRIMARY KEY (wid)
);
QUERY
);
$result[] = db_query(<<<QUERY
CREATE TABLE {workflow_type_map} (
type varchar(255) NOT NULL default '',
wid integer NOT NULL default '0'
);
QUERY
);
$result[] = db_query(<<<QUERY
CREATE INDEX {workflow_type_map}_type_wid_idx ON {workflow_type_map}(type,wid);
QUERY
);
$result[] = db_query(<<<QUERY
CREATE TABLE {workflow_node} (
nid SERIAL,
sid integer NOT NULL default '0',
uid integer NOT NULL default '0',
stamp integer NOT NULL default '0',
PRIMARY KEY (nid)
);
QUERY
);
$result[] = db_query(<<<QUERY
CREATE INDEX {workflow_node}_nid_sid_idx ON {workflow_node}(nid,sid);
QUERY
);
$result[] = db_query(<<<QUERY
CREATE TABLE {workflow_node_history} (
hid serial,
nid integer NOT NULL default '0',
old_sid integer NOT NULL default '0',
sid integer NOT NULL default '0',
uid integer NOT NULL default '0',
stamp integer NOT NULL default '0',
comment text
);
QUERY
);
$result[] = db_query(<<<QUERY
CREATE INDEX {workflow_node_history}_nid_sid_idx ON {workflow_node_history}(nid,sid);
QUERY
);
$result[] = db_query(<<<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
);
$result[] = db_query(<<<QUERY
CREATE INDEX {workflow_scheduled_transition}_nid_idx ON {workflow_scheduled_transition}(nid);
QUERY
);
break;
}
if (count($result) != count(array_filter($result))) {
drupal_set_message(t('Drupal was unable to install the database tables for the workflow module.'), 'error');
}
}
function workflow_uninstall() {
db_query('DROP TABLE {workflow_states}');
db_query('DROP TABLE {workflow_transitions}');
db_query('DROP TABLE {workflows}');
db_query('DROP TABLE {workflow_type_map}');
db_query('DROP TABLE {workflow_node}');
db_query('DROP TABLE {workflow_node_history}');
db_query('DROP TABLE {workflow_scheduled_transition}');
if (module_exists('actions')) {
db_query("DELETE FROM {actions} WHERE callback = 'workflow_select_given_state_action'");
db_query("DELETE FROM {actions} WHERE callback = 'workflow_select_next_state_action'");
}
variable_del('workflow_states_per_page');
foreach (node_get_types() as $type => $name) {
variable_del('workflow_' . $type);
}
}
function workflow_update_1() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
$ret[] = update_sql("CREATE TABLE {workflow_node_history} (\n nid int(10) unsigned NOT NULL default '0',\n sid int(10) unsigned NOT NULL default '0',\n uid int(10) unsigned NOT NULL default '0',\n stamp int(10) unsigned NOT NULL default '0',\n KEY nid (nid,sid)\n ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
$ret[] = update_sql("INSERT INTO {workflow_node_history} SELECT * FROM {workflow_node}");
$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");
$ret[] = update_sql("ALTER TABLE {workflow_node} ADD PRIMARY KEY (nid)");
break;
case 'pgsql':
$ret[] = update_sql("CREATE TABLE {workflow_node_history} (\n nid integer NOT NULL default '0',\n sid integer NOT NULL default '0',\n uid integer NOT NULL default '0',\n stamp integer NOT NULL default '0'\n );");
$ret[] = update_sql("CREATE INDEX {workflow_node_history}_nid_sid_idx ON {workflow_node_history}(nid,sid);");
$ret[] = update_sql("INSERT INTO {workflow_node_history} SELECT * FROM {workflow_node}");
$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");
$ret[] = update_sql("ALTER TABLE {workflow_node} ADD PRIMARY KEY (nid)");
break;
}
return $ret;
}
function workflow_update_2() {
return _system_update_utf8(array(
'workflow_actions',
'workflow_node',
'workflow_states',
'workflow_transitions',
'workflow_type_map',
'workflows',
));
}
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;
}
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;
}
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;
}
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 . "'");
}
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;
}
function workflow_update_5200() {
$ret = array();
switch ($GLOBALS['db_type']) {
case 'mysqli':
case 'mysql':
case 'pgsql':
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 {
$success = TRUE;
}
if ($success) {
$ret[] = update_sql("DROP TABLE {workflow_actions}");
}
}
}
return $ret;
}
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;
}
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;
}
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);
if (count($op_parts) == 3) {
continue;
}
$tid = $op_parts[1];
$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));
$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;
}
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;\n ALTER TABLE {workflow_transitions} ADD COLUMN roles_temp VARCHAR(255);\n UPDATE roles_temp SET new_col = CAST(roles AS VARCHAR(255));\n ALTER TABLE {actions_assignments} DROP COLUMN roles;\n RENAME roles_temp TO roles;\n COMMIT;");
break;
}
return $ret;
}