Created
October 25, 2012 00:58
-
-
Save joeyyax/3949881 to your computer and use it in GitHub Desktop.
When migrating from basecamp classic to the new basecamp project IDs change. This quick & dirty script will identify projects by name and update the project IDs in your database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ob_start(); | |
// | |
// Config | |
// | |
// Old Basecamp | |
$bc_old = array( | |
'address' => 'https://XXXXXX.basecamphq.com/projects.xml', | |
'username' => 'user_api_token', | |
'password' => 'X', | |
'format' => 'xml' | |
); | |
// New Basecamp | |
$bc_new = array( | |
'address' => 'https://basecamp.com/XXXXXX/api/v1/projects.json', | |
'username' => '', | |
'password' => '', | |
'format' => 'json' | |
); | |
// database | |
$host = 'localhost'; | |
$database = ''; | |
$user = ''; | |
$pass = ''; | |
mysql_connect( $host, $user, $pass ) or die( 'Count not connect: ' . mysql_error() ); | |
mysql_select_db( $database ) or die( "Could not select database: " . mysql_error() ); | |
// | |
// Make update | |
// | |
function get_data( $instance ) { | |
$ch = curl_init(); | |
curl_setopt( $ch, CURLOPT_URL, $instance['address'] ); | |
curl_setopt( $ch, CURLOPT_TIMEOUT, 30 ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt( $ch, CURLOPT_USERAGENT, 'Migrate Project IDs (http://gradybritton.com)' ); | |
curl_setopt( $ch, CURLOPT_USERPWD, $instance['username'] . ':' . $instance['password'] ); | |
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1); | |
curl_setopt( $ch, CURLOPT_UNRESTRICTED_AUTH, 1); | |
$info = curl_getinfo( $ch ); | |
$data = curl_exec( $ch ); | |
curl_close($ch); | |
$data = ( $instance['format'] == 'xml' ? new SimpleXMLElement( $data ) : json_decode( $data ) ); // convert xml/json to php array or object | |
return $data; | |
} | |
// old project ids | |
echo '<p>Getting projects from basecamp classic… '; ob_flush(); flush(); | |
$old = get_data($bc_old); | |
foreach ( $old->project as $k => $v) { | |
$old_projects[] = array( | |
'name' => (string)$v->name, | |
'id' => (string)$v->id | |
); | |
} | |
sort($old_projects); | |
echo '<span style="color:#0a0">Success!</span></p>'; ob_flush(); flush(); | |
// new project ids | |
echo '<p>Getting projects from new basecamp… '; ob_flush(); flush(); | |
$new = get_data($bc_new); | |
foreach ( $new as $k => $v) { | |
$new_projects[] = array( | |
'name' => str_replace(': ','',strstr($v->name,':')), // new basecamp prefixes company names to migrated projects. strip company name | |
'id' => $v->id | |
); | |
} | |
sort($new_projects); | |
echo '<span style="color:#0a0">Success!</span></p>'; ob_flush(); flush(); | |
// loop through projects comparing by project name to u | |
foreach( $old_projects as $old ) { | |
$old_name = $old['name']; | |
$old_id = $old['id']; | |
foreach( $new_projects as $new) { | |
$new_name = $new['name']; | |
$new_id = $new['id']; | |
if ($old_name === $new_name) { | |
echo '<p>Updating ' .$old_name . ' (' . $old_id . ') → ' . $new_name . ' (' . $new_id . ')… '; ob_flush(); flush(); | |
$u = mysql_query("UPDATE projects SET id = '$new_id' WHERE project_id = '$old_id'") or die(mysql_error()); | |
echo ( $u ? '<span style="color:#0a0;">Success!</span>':'<span style="color:#a00;">Failed</span>') . '</p>'; ob_flush(); flush(); | |
} | |
} | |
} | |
// done! | |
die( '<p style="font-weight: bold;">Done!</p>' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment