Skip to content

Instantly share code, notes, and snippets.

@cesargalindo
Last active August 29, 2015 14:21
Show Gist options
  • Save cesargalindo/721daa497bad23c4aa13 to your computer and use it in GitHub Desktop.
Save cesargalindo/721daa497bad23c4aa13 to your computer and use it in GitHub Desktop.
<?php
/*
* This script presumes that you place the following in composer.json:
* {
* "require": {
* "guzzlehttp/guzzle": "~4.0"
* }
* }
* and then run composer install in that directory.
*/
use GuzzleHttp\Client;
/* ==== CONFIGURATION SECTION ==== */
// What org do you want to sync?
$org = 'EDGE-ORG';
// What are the credentials for an orgadmin within that org?
$user = 'EMAIL';
$pass = 'PASSWORD';
// What's the connection string for the database?
// Hint: look in ~/.drush/pantheon.aliases.drushrc.php
//$connection = 'mysql://pantheon:[email protected]:31337/pantheon';
$connection = 'mysql://USERNAME:PASSWORD@localhost/DATABSE_NAME';
/* ==== END CONFIGURATION ==== */
$parts = parse_url($connection);
$db_user = $parts['user'];
$db_pass = $parts['pass'];
$db_host = $parts['host'];
$db_port = $parts['port'];
$db_name = ltrim($parts['path'], '/');
$dsn = "mysql:host=$db_host;port=$db_port;dbname=$db_name";
$PDO = new PDO($dsn, $db_user, $db_pass, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
require_once 'vendor/autoload.php';
$client = new Client([
'base_url' => ['https://api.enterprise.apigee.com/v1/o/{org}/', ['org' => $org]],
'defaults' => [
'headers' => ['Accept' => 'application/json'],
'auth' => [$user, $pass],
'allow_redirects' => true,
],
]);
// Export missing mint developers
$tmp_file = '/tmp/missing-mint-developers.txt';
exec('echo "Contains missing developers - ' . date("F j, Y, g:i a") .'" > '. $tmp_file);
// Get latest email
$email = $PDO->query('SELECT mail FROM users ORDER by uid DESC LIMIT 1')->fetchColumn();
// when kicking off manually pick a users name from beginning of user's table
$email = '[email protected]';
$response = $client->get('developers?count=9999&startKey=' . $email);
//$response = $client->get('developers');
$edge_developers = json_decode($response->getBody(), TRUE);
$total = count($edge_developers);
while ($total > 1) {
$j = 0;
$drupal_developers = array();
$uid = $PDO->query('SELECT value FROM sequences')->fetchColumn();
$mail_query = $PDO->prepare('SELECT uid FROM users WHERE mail = :mail OR name = :name');
$users_query = $PDO->prepare('INSERT INTO users (uid, name, pass, mail, created, status, init, uuid) VALUES (:uid, :name, :pass, :mail, :created, :status, :init, :uuid)');
$fn_data_query = $PDO->prepare('INSERT INTO field_data_field_first_name (entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_first_name_value) VALUES (\'user\', \'user\', 0, :uid, :vid, \'und\', 0, :name)');
$fn_rev_query = $PDO->prepare('INSERT INTO field_revision_field_first_name (entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_first_name_value) VALUES (\'user\', \'user\', 0, :uid, :vid, \'und\', 0, :name)');
$ln_data_query = $PDO->prepare('INSERT INTO field_data_field_last_name (entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_last_name_value) VALUES (\'user\', \'user\', 0, :uid, :vid, \'und\', 0, :name)');
$ln_rev_query = $PDO->prepare('INSERT INTO field_revision_field_last_name (entity_type, bundle, deleted, entity_id, revision_id, language, delta, field_last_name_value) VALUES (\'user\', \'user\', 0, :uid, :vid, \'und\', 0, :name)');
foreach ($edge_developers as $edge_developer) {
$j++;
try {
$response = $client->get('developers/' . rawurlencode($edge_developer));
$details = json_decode($response->getBody(), TRUE);
$mail_query->execute([':mail' => $edge_developer, ':name' => $details['userName']]);
$row = $mail_query->fetchObject();
if ($row) {
fputs(STDERR, "[$j/$total] Skipping $edge_developer; already in database.\n");
continue;
} else {
echo "[$j/$total] Processed $edge_developer\n";
echo "uid = $uid \n";
echo "name = " . $details['userName'] . "\n";
echo "pass = " . user_password() . "\n";
echo "mail = " . $edge_developer . "\n";
echo "status = " . $details['status'] . "\n";
$users_query->execute([
':uid' => $uid,
':name' => $details['userName'],
':pass' => user_password(),
':mail' => $edge_developer,
':status' => ($details['status'] == 'active' ? 1 : 0),
':init' => $edge_developer,
':created' => time(),
':uuid' => GUID(),
]);
$fn_data_query->execute([
':uid' => $uid,
':vid' => $uid,
':name' => $details['firstName'],
]);
$fn_rev_query->execute([
':uid' => $uid,
':vid' => $uid,
':name' => $details['firstName'],
]);
$ln_data_query->execute([
':uid' => $uid,
':vid' => $uid,
':name' => $details['lastName'],
]);
$ln_rev_query->execute([
':uid' => $uid,
':vid' => $uid,
':name' => $details['lastName'],
]);
echo "[$j/$total] Processed $edge_developer\n";
$uid++;
$PDO->exec('UPDATE sequences SET value = ' . $uid);
}
} catch
(Exception $e) {
exec('echo "$edge_developer . " >> ' . $tmp_file);
echo "#### missing developer in mint >> " . $edge_developer . "\n";
}
}
// Get new developer list - count
$email = $PDO->query('SELECT mail FROM users ORDER by uid DESC LIMIT 1')->fetchColumn();
$response = $client->get('developers?count=9999&startKey=' . $email);
$edge_developers = json_decode($response->getBody(), TRUE);
$total = count($edge_developers);
echo "\n ###### NEW TOTAL: $total #######\n";
// if ($uid > 6465) { $total = 1; }
}
echo "Done!\n";
function user_password() {
$allowable_characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
$len = strlen($allowable_characters) - 1;
$pass = '';
for ($i = 0; $i < 10; $i++) {
$pass .= $allowable_characters[mt_rand(0, $len)];
}
return $pass;
}
function GUID()
{
if (function_exists('com_create_guid') === true)
{
return trim(com_create_guid(), '{}');
}
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment