One time script to migrate directories to new Site IDs. Requires `ids-sites_{$captain_id}.json` outputted from https://gist.github.com/austinginder/54c55adc2834be9c940597e3ebccd8e6
<?php | |
if ( count ( $args ) == 0 ) { | |
echo "Error: missing <captain_id> argument."; | |
return; | |
} | |
$json = $_SERVER['HOME'] . "/.captaincore-cli/config.json"; | |
$config_data = json_decode ( file_get_contents( $json ) ); | |
$system = $config_data[0]->system; | |
$path = $system->path; | |
$captain_id = $args[0]; | |
if ( $system->captaincore_fleet == "true" ) { | |
$path = "{$path}/$captain_id"; | |
} | |
$ids_sites_json = "ids-sites_{$captain_id}.json"; | |
if ( ! file_exists( $ids_sites_json ) ) { | |
echo "Error: missing file $ids_sites_json"; | |
return; | |
} | |
$sites = json_decode( file_get_contents( $ids_sites_json ) ); | |
foreach( $sites as $site ) { | |
if ( ! empty( $site->new_id ) && ! empty( $site->legacy_id ) ) { | |
$lookup = ( new CaptainCore\Sites )->get( $site->new_id ); | |
// Skip records missing site field | |
if ( empty( $lookup->site ) ) { | |
continue; | |
} | |
$directory = "{$path}/{$lookup->site}_{$site->legacy_id}"; | |
$directory_new = "{$path}/{$lookup->site}_{$site->new_id}"; | |
// Check if unable to move | |
if ( file_exists( $directory ) && file_exists( $directory_new ) ) { | |
echo "Error can't move $directory as destination {$lookup->site}_{$site->new_id} exists\n"; | |
continue; | |
} | |
// Rename CaptainCore directories given new ID | |
if ( file_exists( $directory ) ) { | |
echo "Renaming $directory to {$lookup->site}_{$site->new_id}\n"; | |
rename( $directory, $directory_new ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment