Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save austinginder/ace9db33b7b472f6cdeadab7e951ef16 to your computer and use it in GitHub Desktop.

Select an option

Save austinginder/ace9db33b7b472f6cdeadab7e951ef16 to your computer and use it in GitHub Desktop.

Revisions

  1. austinginder created this gist Feb 6, 2020.
    48 changes: 48 additions & 0 deletions migration-captaincore-v0.9-to-v0.10-rename-dirs.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    <?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 );
    }

    }
    }