Crude Flexinode to CCK converter
starbow - January 26, 2007 - 23:17
I am upgrading a site from 4.6 to 5.0, by way of 4.7. Along the way I want to move several hundred nodes from Flexinode types to CCK types. I haven't found any easy way to do this, so I wrote a module that works, in a very limited and crude way. Your millage may vary, recommended for advanced user use only, back-up your db before use, etc.
Limitations:
- Only converts text types
- All fields that are being converted must have unique names
- You must build the matching cck type by hand
- All fields in the cck type must have identical labels to the flexinode type fields
Steps for use (4.7):
- Make sure all your flexinode fields are unique.
- Turn off the Flexinode module.
- Turn on the CCK & Text modules.
- Create a new CCK type for each old type - each type must have exactly the same name, and each fields must have the same name as the old field.
- Run admin/flexiconvert/convert for each node type, selected the matching names in both selectors
- Good Luck!
<?php
/**
* Implementation of hook_help().
*/
function flexiconvert_help($section) {
switch ($section) {
case 'admin/modules#description':
return t('Tool to help convert flexinodes to cck nodes');
case 'admin/help#flexiconvert':
return t('Flexiconvert will convert some kinds of flexinode types into cck types.' .
'Limitations: <ul>' .
'<li>Only converts text types</li>' .
'<li>All fields that are being converted must have unique names</li>' .
'<li>You must build the matching cck type by hand</li>' .
'<li>All fields in the cck type must have identical labels to the flexinode type fields</li>' .
'</ul>'
);
}
}
/**
* Implementation of hook_menu().
*/
function flexiconvert_menu($may_cache) {
global $user;
$items = array();
if (!$may_cache) {
$items[] = array(
'path' => 'admin/flexiconvert/convert',
'title' => t('flexiconvert'),
'callback' => 'flexiconvert_page',
'access' => ($user->id == 0),
);
}
return $items;
}
function flexiconvert_submit( $form_id, $form_values ) {
$flexitype = $form_values['flexitype'];
$ccktype = $form_values['ccktype'];
variable_set( 'flexiconvert_flexitype', $flexitype );
variable_set( 'flexiconvert_ccktype', $ccktype );
$flexitype_name = 'flexinode-'. $flexitype;
drupal_set_message( "Syncing flexinode: $flexitype_name to cck: $ccktype " );
$fields = array();
$result = db_query( "SELECT field_id, label FROM {flexinode_field} WHERE ctype_id = ". $flexitype );
while( $row = db_fetch_array( $result ) ) {
$label = $row['label'];
$cckfield = strtolower( $label );
$cckfield = 'field_'. str_replace( ' ', '_', $cckfield ); // .'_value';
$fields[ $row['field_id'] ] = $cckfield;
drupal_set_message( "Mapping $label (flexifield-".$row['field_id'].") to $cckfield" );
}
// gather all the field data, ordered per node
$nodes = array();
foreach( $fields as $flexifield_id => $cckfield ) {
$sql = "SELECT nid, textual_data, numeric_data FROM {flexinode_data} WHERE field_id = $flexifield_id";
//drupal_set_message( $sql );
$result = db_query( $sql );
while( $row = db_fetch_array( $result ) ) {
$nodes[ $row['nid'] ][$cckfield]['text'] = $row['textual_data'];
$nodes[ $row['nid'] ][$cckfield]['format'] = $row['numeric_data'];
//drupal_set_message( 'Nodes['. $row['nid'] . "][$cckfield] = ". $row['textual_data'] );
}
}
$sql = "SELECT nid FROM {node} WHERE type = '$flexitype_name'";
// drupal_set_message( $sql );
$result = db_query( $sql );
$count_to_convert = db_num_rows($result);
drupal_set_message( "There are $count_to_convert items to convert" );
$fields_str = '';
$count = 0;
while( $row = db_fetch_array( $result ) ) {
$count++;
// INSERT flexinode data as rows in cck table
$nid = $row['nid'];
$node = $nodes[$nid];
$cck_table = "node_$ccktype";
$sql = "insert into {$cck_table} (nid, vid) value ('$nid', '$nid')";
//drupal_set_message( "$count: $sql" );
db_query( $sql );
foreach( $node as $cckfield => $value ) {
db_query( "UPDATE {%s} SET %s = '%s' WHERE nid = %d", $cck_table, $cckfield . '_value', $value['text'], $nid );
if( $value['format'] != 0 ) {
db_query( "UPDATE {%s} SET %s = '%d' WHERE nid = %d", $cck_table, $cckfield . '_format', $value['format'], $nid );
}
}
}
$result = db_query( "SELECT nid FROM {$cck_table}" );
$count_converted = db_num_rows($result);
drupal_set_message( "Inserted '$count_converted' items ($count)" );
if( $count_converted == $count_to_convert ) {
drupal_set_message( "Yay, all nodes converted" );
// finally convert the node type fields
$sql = "UPDATE {node} SET type = '$ccktype' WHERE type = '$flexitype_name'";
//drupal_set_message( "Not Done: ". $sql );
db_query( $sql );
}
else {
drupal_set_message( "Boo!!");
}
}
function flexiconvert_page() {
drupal_set_title( "Flexiconvert" );
$types = array();
$result = db_query( "SELECT name, ctype_id FROM {flexinode_type}" );
while( $row = db_fetch_array( $result ) ) {
$types[ $row['ctype_id'] ] = $row['name'];
}
$form = array();
$form['flexitype'] = array(
'#type' => 'select',
'#title' => t('Flexinode to read from'),
'#options' => $types,
'#default_value' => variable_get( 'flexiconvert_flexitype', NULL ),
);
$ccktypes = array();
$result = db_query( "SELECT label, type_name FROM {node_type_content}" );
while( $row = db_fetch_array( $result ) ) {
$ccktypes[ $row['type_name'] ] = $row['label'];
}
$form['ccktype'] = array(
'#type' => 'select',
'#title' => t('CCK Type to insert into'),
'#options' => $ccktypes,
'#default_value' => variable_get( 'flexiconvert_ccktype', NULL ),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Do It'),
);
return drupal_get_form('flexiconvert', $form);
}
?>
This is great
I'm tracking this and will give it a try. You should post this to the cck and flexinode project pages as well. This is really great.
Will this change the node id?
I need for the node id to remain the same, due to signups still being active. Will this change the node id?
Otherwise, this looks promising.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Glenda: "Are you a good witch or a bad witch"
Katrina: " ... whatever ..."
katrinamessenger.com
It will leave the node ids
It will leave the node ids the same. That was part of my requirements as well.
kewl! I will give it a
kewl! I will give it a try.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Glenda: "Are you a good witch or a bad witch"
Katrina: " ... whatever ..."
katrinamessenger.com
Great!
Just used this with over 2300 nodes, works great! Converted everything in less than a minute. Now onto the 5.0 update!
thank you!
Only converts text types
This is so important to have. We've got hundreds if not thousands of events over the past few years that were created using Flexinode. Migrating them to CCK is the only sensible thing to do.
My only question is, what happens with we have a date or time field? Is anything converted?