Created
February 6, 2020 21:30
-
-
Save austinginder/ace9db33b7b472f6cdeadab7e951ef16 to your computer and use it in GitHub Desktop.
One time script to migrate directories to new Site IDs. Requires `ids-sites_{$captain_id}.json` outputted from https://gist.github.com/austinginder/54c55adc2834be9c940597e3ebccd8e6
This file contains hidden or 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 | |
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