Skip to content

Instantly share code, notes, and snippets.

@mattmcmanus
Created November 22, 2011 16:40
Show Gist options
  • Save mattmcmanus/1386130 to your computer and use it in GitHub Desktop.
Save mattmcmanus/1386130 to your computer and use it in GitHub Desktop.
Bulk node cloning in drupal
<?php
// Assumptions - READ ME
// ====================================
// * You have terms setup, which are literal academic terms setup as taxonomy terms. Ie: 2011 - Spring, 2012 - Spring
// * You have another taxonmy setup for academic units. In this setup there are Units 1-23
//
// What does this do?
// ====================================
// It duplicates specific nodes. For example:
// * Duplicate Units 5-9,15-20 and PT800 from spring 2011 to spring 2012
//
// How to run this script
// ====================================
// drush @site-alias php-script /path/to/this/script.php
// If you don't have drush aliases setup then you can use --uri="www.example.com"
// Enter your variables
$oldTermID = 82; // The ID of the term you want to duplicate, "2011 - Spring" for example
$newTermID = 87; // The newly created (IE, you just made it) ID for term you want to duplicate TO, "2012 - Spring"
$unitsToDuplicate = array(41, 46, 47, 49, 50, 56, 57, 58, 59, 60, 61, 79);
print "\n\n= = = = = = = = Beginning semester duplicate script = = = = = = = = = = =\n";
print "= = = Written by Matthew McManus ([email protected]) July 2010 = = =\n";
print "= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n";
// Generate the query
$query = "select distinct n.nid from node n inner join term_node semester on n.vid = semester.vid inner join term_node unit on n.vid = unit.vid where semester.tid = $oldTermID and n.type = 'lecture' and (";
$or = '';
foreach ($unitsToDuplicate as $unit) {
$query .= "$or unit.tid = $unit";
$or = ' or';
}
$query .= ") order by n.nid;";
// Grab the NIDs
$nidsToDuplicate = db_query($query);
print " * Old semester NIDs selected\n";
print " * Duplicating NID: ";
// Iterate through each node
while( $nid = db_result($nidsToDuplicate) ){
$node = node_load($nid); // Load the old node
// Clean out old identifiers
$node->nid = null; $node->vid = null;
// Do standard drupal prep on a node
if( ! function_exists("node_object_prepare")) {
include_once(drupal_get_path('module', 'node') . '/node.pages.inc');
}
node_object_prepare(&$node);
// Switch Semester Taxonomy
$node->taxonomy[$newTermID]['tid'] = $newTermID; // Adding new term
unset($node->taxonomy[$oldTermID]); // Removing the old
$node->uid = 3;// Set Joanies UID
$node->status = 0; // Save it as a draft
$node->path = null; // Clear the old path
node_save(&$node); // Take it home!
print $nid.", ";
unset($node);
}
print "\n= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n";
print "= = = = = = = = = = = = = = = SUCCESS ! = = = = = = = = = = = = = = = = =\n";
print "= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment