Skip to content

Instantly share code, notes, and snippets.

@vierbergenlars
Created August 19, 2016 19:52
Show Gist options
  • Save vierbergenlars/63db1c06f86a2ac9fadaa682d026da04 to your computer and use it in GitHub Desktop.
Save vierbergenlars/63db1c06f86a2ac9fadaa682d026da04 to your computer and use it in GitHub Desktop.
<?php
if($_SERVER['argc'] != 2) {
printf("%s dokuwiki_users\n", $_SERVER['argv'][0]);
echo "Converts all users found in the dokuwiki users.auth.php file 'dokuwiki_users'\n";
echo "to SQL to be imported in the authserver database. (on stdout) \n";
exit(1);
}
$dokuwikiUsers = file($_SERVER['argv'][1], FILE_SKIP_EMPTY_LINES|FILE_IGNORE_NEW_LINES);
$dokuwikiUsers = array_filter($dokuwikiUsers, function($line) { return $line[0] !== '#'; });
$dokuwikiUsers = array_map(function($line) {
list($username, $passwordHash, $displayName, $email, $groups) = explode(':', $line);
$email = strtolower($email);
$username = strtolower($username);
return filter_var_array(
compact('username', 'passwordHash', 'displayName', 'email', 'groups'),
FILTER_SANITIZE_STRING,
FILTER_FLAG_STRIP_LOW|FILTER_FLAG_STRIP_HIGH|FILTER_FLAG_STRIP_BACKTICK
);
}, $dokuwikiUsers);
$emailAddresses = [];
foreach($dokuwikiUsers as $user) {
if(isset($emailAddresses[$user['email']])) {
fprintf(STDERR, "Skipping user ${user['username']}: duplicate email address ${user['email']}\n");
continue;
}
echo "INSERT INTO auth_users (username, password, roles, is_active, display_name, guid) \
VALUE('${user['username']}', '${user['passwordHash']}', 'ROLE_USER', 1, '${user['displayName']}', UUID());\n";
echo "INSERT INTO EmailAddress (user_id, email, verified, primary_mail) \
VALUE(LAST_INSERT_ID(), '${user['email']}', 1, 1);\n";
$emailAddresses[$user['email']] = true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment