Page MenuHomePhabricator
Paste P1872

import_from_csv.php
ActivePublic

Authored by epriestley on Oct 22 2015, 12:52 AM.
Tags
None
Referenced Files
F903143: import_from_csv.php
Oct 22 2015, 12:52 AM
Subscribers
None
$ cat import.php
#!/usr/bin/env php
<?php
$root = '/Users/epriestley/dev/phabricator/';
require_once $root.'/scripts/__init_script__.php';
if ($argc !== 2) {
echo "usage: import.php <file>\n";
exit(1);
}
$lines = Filesystem::readFile($argv[1]);
$lines = trim($lines);
$lines = phutil_split_lines($lines, false);
foreach ($lines as $line) {
$parts = explode(',', $line);
if (count($parts) != 4) {
throw new Exception(
pht(
'Expected 4 parts: %s.',
$line));
}
list($realname, $email, $username, $avatar) = $parts;
$existing_user = id(new PhabricatorUser())->loadOneWhere(
'username = %s',
$username);
if ($existing_user) {
echo pht('Skipping existing user "%s".', $username)."\n";
continue;
}
$existing_email = id(new PhabricatorUserEmail())->loadOneWhere(
'address = %s',
$email);
if ($existing_email) {
echo pht('Skipping existing email "%s".', $email)."\n";
continue;
}
$user = id(new PhabricatorUser())
->setUsername($username)
->setRealname($realname)
->setIsApproved(1);
$email_object = id(new PhabricatorUserEmail())
->setAddress($email)
->setIsVerified(1);
id(new PhabricatorUserEditor())
->setActor($user)
->createNewUser($user, $email_object);
echo pht(
'Created user (username="%s", realname="%s", email="%s").',
$username,
$realname,
$email)."\n";
}

Event Timeline

epriestley changed the title of this paste from untitled to import_from_csv.php.
epriestley updated the paste's language from autodetect to autodetect.