Skip to content

Instantly share code, notes, and snippets.

@ossobuffo
Created May 8, 2015 20:45
Show Gist options
  • Save ossobuffo/d5b2826b2a3e57057bfc to your computer and use it in GitHub Desktop.
Save ossobuffo/d5b2826b2a3e57057bfc to your computer and use it in GitHub Desktop.
Script to sync Edge to DevPortal when there are many many devs
<?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 = 'testorama';
// What are the credentials for an orgadmin within that org?
$user = '[email protected]';
$pass = 'super-s3cret';
// What's the connection string for the database?
// Hint: look in ~/.drush/pantheon.aliases.drushrc.php
$connection = 'mysql://pantheon:[email protected]:31337/pantheon';
/* ==== 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,
],
]);
$response = $client->get('developers');
$edge_developers = json_decode($response->getBody(), TRUE);
$drupal_developers = array();
$j = 0;
$total = count($edge_developers);
$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, UNIX_TIMESTAMP(), :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++;
$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;
}
$users_query->execute([
':uid' => $uid,
':name' => $details['userName'],
':pass' => user_password(),
':mail' => $edge_developer,
':status' => ($details['status'] == 'active' ? 1 : 0),
':init' => $edge_developer,
]);
$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);
echo "Done!\n";
exit;
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment