Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Implementation of hook_drush_command().
*/
function MODULE_drush_command() {
$items = array();
$items['resync-content-taxonomy'] = array(
'description' => "Re-sync content_taxonomy from term_node",
);
return $items;
}
/**
* drush command callback for resync-content-taxonomy
*/
function drush_MODULE_resync_content_taxonomy() {
drush_log("Running " . __FUNCTION__, 'debug');
$ct_fields = content_taxonomy_fields();
// track the wiped tables so they're not re-wiped each time
static $wiped_tables = array();
foreach($ct_fields as $content_type => $fields) {
foreach((array)$fields['fields'] as $field_name => $field) {
$db_info = content_database_info($field);
$table = $db_info['table'];
$column = $db_info['columns']['value']['column'];
// does this field use term_node at all?
// [this is global per field, so it should be safe]
if (! $field['save_term_node']) {
drush_log("$field_name does not save to term_node, not touching.", 'warning');
continue;
}
drush_log("Migrating $field_name for $content_type (table $table, column $column)...", 'notice');
// DELETE existing records in the table
if (! in_array($table, $wiped_tables)) {
db_query("DELETE FROM $table");
drush_log("Deleted existing records in $table.");
$wiped_tables[] = $table;
}
// terms allowed for this field
$allowed_values = content_taxonomy_allowed_values($field);
// find term_node matches for this content type,
// in the allowed values for the field
$node_terms = array();
// joining on nid -- preserve all revisions
$term_q = db_query("SELECT tn.nid, tn.vid, tn.tid from {term_node} tn LEFT JOIN {node} n on n.nid=tn.nid
WHERE n.type = '%s'", $content_type);
$node_term_count = 0;
$count_saved = 0;
while ($row = db_fetch_array($term_q)) {
if (array_key_exists($row['tid'], $allowed_values)) { // allowed for this field
// key by *vid* (for deltas)
$node_terms[ $row['vid'] ][] = $row;
$node_term_count++;
}
}
drush_log(dt("Found @count term_node links for @type nodes that fit into @field_name",
array('@count' => $node_term_count, '@type' => $content_type, '@field_name' => $field_name)));
// copy term-node matches to this field's table
// [for non-multiple fields, delta is redundant]
foreach($node_terms as $vid => $deltas) {
foreach($deltas as $delta => $term) {
if ($field['multiple']) {
$query = "INSERT INTO $table (nid, vid, delta, $column) VALUES (%d, %d, %d, %d)";
$query_args = array($term['nid'], $term['vid'], $delta, $term['tid']);
}
else {
$query = "INSERT INTO $table (nid, vid, $column) VALUES (%d, %d, %d)";
$query_args = array($term['nid'], $term['vid'], $term['tid']);
}
$saved = db_query($query, $query_args);
if ($saved) {
$count_saved++;
}
else {
drush_log("Error saving to {$table}: " . _inline_print_r($row), 'error');
}
} // delta
} // vid
drush_log("Saved $count_saved records for $content_type nodes in $field_name.");
} // fields
} // content types
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment