View source
<?php
function flashnode_install() {
if (db_table_exists('flash')) {
_flashnode_migrate_from_flash();
}
else {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query('
CREATE TABLE {flashnode} (
nid INT(10) UNSIGNED NOT NULL ,
height INT(10) UNSIGNED NOT NULL ,
width INT(10) UNSIGNED NOT NULL ,
display TINYINT(3) UNSIGNED NOT NULL ,
substitution LONGTEXT NOT NULL ,
flashvars LONGTEXT NOT NULL ,
base VARCHAR(255) DEFAULT NULL ,
vid INT(10) UNSIGNED NOT NULL ,
fid INT(10) UNSIGNED NOT NULL ,
PRIMARY KEY (nid)
) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;');
break;
case 'pgsql':
db_query("\n create table {flashnode} (\n nid integer not null,\n height integer not null,\n width integer not null,\n display integer not null,\n substitution text not null default '',\n flashvars text not null default '',\n base VARCHAR(255) default NULL,\n vid integer not null,\n fid integer not null,\n PRIMARY KEY (nid)\n );");
break;
}
}
drupal_set_message(t('Flash node has been setup.'));
}
function flashnode_update_1() {
if (db_table_exists('flash')) {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$ret[] = update_sql('RENAME TABLE {flash} TO {flashnode}');
break;
case 'pgsql':
$ret[] = update_sql('ALTER TABLE {flash} RENAME TO {flashnode}');
break;
}
}
return $ret;
}
function flashnode_update_2() {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$ret[] = update_sql('ALTER TABLE {flashnode} DROP version');
$ret[] = update_sql('ALTER TABLE {flashnode} DROP build');
break;
case 'pgsql':
break;
}
return $ret;
}
function flashnode_update_3() {
$ret[] = update_sql("UPDATE {filters} SET module='flashnode' WHERE module='flash'");
return $ret;
}
function flashnode_update_4() {
$ret = array();
$result = db_query("SELECT nid, vid, body, teaser FROM {node_revisions} WHERE body LIKE '%[flash|nid=%' OR teaser LIKE '%[flash|nid=%'");
while ($node = db_fetch_object($result)) {
$node->body = str_replace('[flash|nid=', '[flashnode|nid=', $node->body);
$node->teaser = str_replace('[flash|nid=', '[flashnode|nid=', $node->teaser);
if (db_query("UPDATE {node_revisions} SET body='%s', teaser='%s' WHERE vid=%d", $node->body, $node->teaser, $node->vid)) {
$ret[] = array(
'success' => 1,
'query' => 'Updated filter in node ' . $node->nid,
);
}
else {
$ret[] = array(
'success' => 0,
'query' => 'Failed to update filter in node ' . $node->nid,
);
}
}
cache_clear_all('*', 'cache_filter', true);
return $ret;
}
function flashnode_update_5() {
$ret[] = update_sql("UPDATE {files} SET filename='_flashnode' WHERE filename='_flash'");
return $ret;
}
function flashnode_update_6() {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$ret[] = update_sql("ALTER TABLE {flashnode} ADD substitution longtext NOT NULL");
break;
case 'pgsql':
db_add_column($ret, 'flashnode', 'substitution', 'text', array(
'default' => '',
'not null' => TRUE,
));
break;
}
return $ret;
}
function flashnode_update_7() {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$ret[] = update_sql("ALTER TABLE {flashnode} ADD flashvars longtext NOT NULL");
$ret[] = update_sql("ALTER TABLE {flashnode} ADD base VARCHAR(255) NOT NULL");
break;
case 'pgsql':
db_add_column($ret, 'flashnode', 'flashvars', 'text', array(
'default' => '',
'not null' => TRUE,
));
db_add_column($ret, 'flashnode', 'base', 'varchar(255)', array(
'default' => '',
'not null' => TRUE,
));
break;
}
return $ret;
}
function flashnode_update_8() {
$ret[] = update_sql("UPDATE {flashnode} SET substitution='!default'");
return $ret;
}
function flashnode_update_9() {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
$ret[] = update_sql("ALTER TABLE {flashnode} ADD vid int NOT NULL default '0'");
$ret[] = update_sql("ALTER TABLE {flashnode} ADD fid int NOT NULL default '0'");
$ret[] = update_sql("UPDATE {flashnode} fn INNER JOIN {node} n ON fn.nid = n.nid SET fn.vid = n.vid");
$ret[] = update_sql("UPDATE {flashnode} fn INNER JOIN {files} f ON fn.nid = f.nid SET fn.fid = f.fid");
break;
case 'pgsql':
db_add_column($ret, 'flashnode', 'vid', 'int', array(
'default' => 0,
'not null' => TRUE,
));
db_add_column($ret, 'flashnode', 'fid', 'int', array(
'default' => 0,
'not null' => TRUE,
));
$ret[] = update_sql("UPDATE {flashnode} fn SET vid = n.vid FROM {node} n WHERE fn.nid = n.nid");
$ret[] = update_sql("UPDATE {flashnode} fn SET fid = f.fid FROM {files} f WHERE fn.nid = f.nid");
break;
}
return $ret;
}
function _flashnode_migrate_from_flash() {
_flashnode_update_utf8();
if (db_num_rows(db_query("SHOW COLUMNS FROM {%s} LIKE '%s'", 'flash', 'fid'))) {
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query('ALTER TABLE {flash} DROP fid');
break;
case 'pgsql':
break;
}
}
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query('ALTER TABLE {flash} DROP version');
db_query('ALTER TABLE {flash} DROP build');
db_query('RENAME TABLE {flash} TO {flashnode}');
break;
case 'pgsql':
db_query('ALTER TABLE {flash} RENAME TO {flashnode}');
break;
}
db_query("UPDATE {filters} SET module='flashnode' WHERE module='flash'");
$result = db_query("SELECT vid, body, teaser FROM {node_revisions} WHERE body LIKE '%[flash|nid=%' OR teaser LIKE '%[flash|nid=%'");
while ($node = db_fetch_object($result)) {
$node->body = str_replace('[flash|nid=', '[flashnode|nid=', $node->body);
$node->teaser = str_replace('[flash|nid=', '[flashnode|nid=', $node->teaser);
db_query("UPDATE {node_revisions} SET body='%s', teaser='%s' WHERE vid=%d", $node->body, $node->teaser, $node->vid);
}
cache_clear_all('*', 'cache_filter', true);
db_query("UPDATE {variable} SET name='flashnode_default_path' WHERE name='flash_default_path'");
db_query("UPDATE {variable} SET name='flashnode_updated' WHERE name='flash_updated'");
db_query("DELETE FROM {variable} WHERE name='flash_version'");
db_query("DELETE FROM {variable} WHERE name='flash_build'");
db_query("UPDATE {files} SET filename='_flashnode' WHERE filename='_flash'");
db_query("UPDATE {node} SET type='flashnode' WHERE type='flash'");
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query("ALTER TABLE {flashnode} ADD substitution longtext NOT NULL");
db_query("ALTER TABLE {flashnode} ADD flashvars longtext NOT NULL");
db_query("ALTER TABLE {flashnode} ADD base VARCHAR(255) NOT NULL");
break;
case 'pgsql':
_flashnode_db_add_column($ret, 'flashnode', 'substitution', 'text', array(
'default' => '',
'not null' => TRUE,
));
_flashnode_db_add_column($ret, 'flashnode', 'flashvars', 'text', array(
'default' => '',
'not null' => TRUE,
));
_flashnode_db_add_column($ret, 'flashnode', 'base', 'varchar(255)', array(
'default' => '',
'not null' => TRUE,
));
break;
}
db_query("UPDATE {flashnode} SET substitution='!default'");
drupal_set_message('Successfully migrated database from {flash} to {flashnode}.');
return;
}
function _flashnode_update_utf8() {
switch ($GLOBALS['db_type']) {
case 'mysqli':
break;
case 'mysql':
if (version_compare(mysql_get_server_info($GLOBALS['active_db']), '4.1.0', '<')) {
return;
}
break;
case 'pgsql':
return;
}
global $db_url;
$url = parse_url(is_array($db_url) ? $db_url['default'] : $db_url);
$db_name = substr($url['path'], 1);
$result = db_fetch_array(db_query('SHOW CREATE DATABASE `%s`', $db_name));
if (preg_match('/utf8/i', array_pop($result))) {
return;
}
db_query('ALTER TABLE {flash} DEFAULT CHARACTER SET utf8');
return;
}
function _flashnode_db_add_column(&$ret, $table, $column, $type, $attributes = array()) {
if (array_key_exists('not null', $attributes) and $attributes['not null']) {
$not_null = 'NOT NULL';
}
if (array_key_exists('default', $attributes)) {
if (is_null($attributes['default'])) {
$default_val = 'NULL';
$default = 'default NULL';
}
elseif ($attributes['default'] === FALSE) {
$default = '';
}
else {
$default_val = "{$attributes['default']}";
$default = "default {$attributes['default']}";
}
}
db_query("ALTER TABLE {" . $table . "} ADD {$column} {$type}");
if ($default) {
db_query("ALTER TABLE {" . $table . "} ALTER {$column} SET {$default}");
}
if ($not_null) {
if ($default) {
db_query("UPDATE {" . $table . "} SET {$column} = {$default_val}");
}
db_query("ALTER TABLE {" . $table . "} ALTER {$column} SET NOT NULL");
}
}