body = str_replace('[flash|nid=', '[flashnode|nid=', $node->body); $node->teaser = str_replace('[flash|nid=', '[flashnode|nid=', $node->teaser); // Update the database with the changes 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); } } // Reset the cache to ensure any pages using filters are updated cache_clear_all('*', 'cache_filter', true); return $ret; } /** * Update files table - rename _flash filename to _flashnode for consistency */ function flashnode_update_5() { $ret[] = update_sql("UPDATE {files} SET filename='_flashnode' WHERE filename='_flash'"); return $ret; } /** * Add substitution column as flashnode 5.2 makes this user-definable on a per * node basis */ 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; } /** * Add flashvars and base columns as flashnode 5.2 makes these user-definable * on a per node basis */ 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; } /** * Set substitution column value to !default */ function flashnode_update_8() { $ret[] = update_sql("UPDATE {flashnode} SET substitution='!default'"); return $ret; } /** * Add two additional columns in preparation for Drupal 6 * Need to add fid and vid * Set vid = nid, and set fid based on the entries in {files} * In Drupal 6 nid will be dropped, so we need fid as of now! */ function flashnode_update_9() { // Add new columns 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'"); // Populate columns with data $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)); // Populate columns with data $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; } /** * If flashnode is being installed in place of flash then to Drupal it looks * like the first time this module has been installed. In reality we have an * existing table we want to keep with our existing flash content, so we have to * simulate the update routines as part of installation. */ function _flashnode_migrate_from_flash() { /** * Do update to utf8, if required - at this stage there are no text fields to * convert so it will just change the default character set if required */ _flashnode_update_utf8(); /** * See if fid column exists, and drop it if it does. Not all versions of flash * included fid so it may already not exist! Not all Postgres can drop * columns, so omit dropping in that case */ 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': //db_query('ALTER TABLE {flash} DROP COLUMN fid'); break; } } /** * Drop version and build, unless Postgres, then rename table from {flash} to * {flashnode} to bring table name in line with node name */ 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} DROP COLUMN version'); //db_query('ALTER TABLE {flash} DROP COLUMN build'); db_query('ALTER TABLE {flash} RENAME TO {flashnode}'); break; } // Update filters table db_query("UPDATE {filters} SET module='flashnode' WHERE module='flash'"); // Get all existing nodes that contain macros $result = db_query("SELECT vid, body, teaser FROM {node_revisions} WHERE body LIKE '%[flash|nid=%' OR teaser LIKE '%[flash|nid=%'"); // Iterate through the results while ($node = db_fetch_object($result)) { // Update body and teaser $node->body = str_replace('[flash|nid=', '[flashnode|nid=', $node->body); $node->teaser = str_replace('[flash|nid=', '[flashnode|nid=', $node->teaser); // Update the database db_query("UPDATE {node_revisions} SET body='%s', teaser='%s' WHERE vid=%d", $node->body, $node->teaser, $node->vid); } // Reset the cache to ensure any pages using filters are updated cache_clear_all('*', 'cache_filter', true); /** * Amend the variable table by deleting default version and build which are no * longer required, and change the name on the remaining variables to * flashnode_ to make consistent with module name */ 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'"); // Update files table - change filename from _flash to _flashnode db_query("UPDATE {files} SET filename='_flashnode' WHERE filename='_flash'"); // Update node table - change type from flash to flashnode db_query("UPDATE {node} SET type='flashnode' WHERE type='flash'"); // Add substitution and base column 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; } // Set default value of substitution db_query("UPDATE {flashnode} SET substitution='!default'"); // End of update routine drupal_set_message('Successfully migrated database from {flash} to {flashnode}.'); return; } /** * Convert a 4.7 table to UTF-8 as part of installation as we won't get it via * update.php * * This code taken from install.inc and we have to repeat it here as it is not * available to us during a module install, only module update, but we might * need to migrate the table over */ function _flashnode_update_utf8() { switch ($GLOBALS['db_type']) { // Only for MySQL 4.1+ case 'mysqli': break; case 'mysql': if (version_compare(mysql_get_server_info($GLOBALS['active_db']), '4.1.0', '<')) { return; } break; case 'pgsql': return; } // See if database uses UTF-8 already 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; } /** * Add a column to a database using syntax appropriate for PostgreSQL. * Save result of SQL commands in $ret array. * * This code is the same as update.php except we replace update_sql with * db_query. We have to repeat it here as during a module install we don't have * the function available to us. */ 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"); } }