Newer
Older
Digital_Repository / Repositories / Misc / import_users.pl
nstanger on 23 Mar 2010 2 KB - Added bulk user import script.
  1. #!/usr/bin/perl -w -I/home/eprints3/perl_lib
  2.  
  3. use EPrints::DataObj::User;
  4. use EPrints::Session;
  5. use strict;
  6.  
  7. my $session = EPrints::Session->new( 1 , $ARGV[0] );
  8. exit unless( defined $session );
  9.  
  10. # Set to 1 to actually make changes to the database
  11. my $forreal = 1;
  12.  
  13. # Get user dataset
  14. my $user_ds = $session->get_archive()->get_dataset( "user" );
  15.  
  16.  
  17. my $input_filename = $ARGV[1];
  18.  
  19. ( defined $input_filename ) or die "No input filename specified.\n";
  20.  
  21. # Try to open the input file.
  22. open INFILE, "<$input_filename"
  23. or die "ERROR: couldn't open file '$input_filename' for reading.\n";
  24.  
  25.  
  26. while( <INFILE> )
  27. {
  28.  
  29. next if /^#/;
  30. next if /^$/;
  31. #username type password given name family name email department organisation address country homepage
  32. /^([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t"?([^"]+)"?\t([^\t]+)\t(.*)$/;
  33.  
  34. my $username = $1;
  35. my $usertype = $2;
  36. my $password = EPrints::Utils::crypt_password( $3 );
  37. my $givenname = $4;
  38. my $familyname = $5;
  39. my $email = $6;
  40. my $department = $7;
  41. my $organisation = $8;
  42. my $address = $9;
  43. my $country = $10;
  44. my $homepage = $11;
  45. # Does account already exist?
  46. my $searchexp = new EPrints::SearchExpression( session=>$session, dataset=>$user_ds );
  47. $searchexp->add_field( $user_ds->get_field( "username" ), $username );
  48. my $searchid = $searchexp->perform_search;
  49. my( $user ) = $searchexp->get_records;
  50. $searchexp->dispose( );
  51.  
  52. if( !defined $user )
  53. {
  54.  
  55. # New account
  56. if( $forreal )
  57. {
  58. $user = EPrints::User::create( $session, $usertype );
  59. $user->set_value( "username", $username );
  60. print "CREATING: $username\n";
  61. }
  62. else
  63. {
  64. print "(WOULD BE) CREATING: $username\n";
  65. }
  66.  
  67. }
  68. else
  69. {
  70.  
  71. # Update account
  72. if( $forreal )
  73. {
  74. print "UPDATING: $username\n";
  75. }
  76. else
  77. {
  78. print "(WOULD BE) UPDATING: $username\n";
  79. }
  80.  
  81. }
  82.  
  83. # Set metadata
  84. if( $forreal )
  85. {
  86.  
  87. my $name = {};
  88. $name->{family} = $familyname;
  89. $name->{given} = $givenname;
  90. $user->set_value( "name", $name );
  91. $user->set_value( "email", $email );
  92. $user->set_value( "password", $password );
  93. $user->set_value( "dept", $department );
  94. $user->set_value( "org", $organisation );
  95. $user->set_value( "address", $address );
  96. $user->set_value( "country", $country );
  97. $user->set_value( "url", $homepage );
  98. $user->commit();
  99.  
  100. }
  101. else
  102. {
  103.  
  104. print "FAMILY = $familyname\n";
  105. print "GIVEN = $givenname\n";
  106. print "EMAIL = $email\n";
  107. print "PASSWORD = $password\n";
  108. print "DEPARTMENT = $department\n";
  109. print "ORGANISATION = $organisation\n";
  110. print "ADDRESS = $address\n";
  111. print "COUNTRY = $country\n";
  112. print "URL = $homepage\n\n";
  113.  
  114. }
  115.  
  116. }
  117.  
  118. # Clean up.
  119. close INFILE;