source: main/trunk/greenstone2/bin/script/csv-usernames-to-db.pl@ 24371

Last change on this file since 24371 was 22331, checked in by ak19, 14 years ago

Goes with the changes made in revision 21822: fixed several additional perl files that depended on perl 5.8 to work and used to fail with Perl 5.10.

  • Property svn:executable set to *
File size: 6.2 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# csv-usernames-to-db.pl --
6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 1999 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
28
29# This program converts username details (password, group information etc)
30# into the format used by Greenstone, and store them in etc/users.gdb
31
32package cu2db;
33
34BEGIN {
35 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
36 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
37 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
38 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
39# unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan/perl-5.8");
40
41}
42
43use strict;
44no strict 'refs'; # allow filehandles to be variables and vice versa
45no strict 'subs'; # allow barewords (eg STDERR) as function arguments
46
47use FileHandle;
48use util;
49use gsprintf 'gsprintf';
50use printusage;
51use parse2;
52
53
54my $arguments =
55 [ { 'name' => "fieldseparator",
56 'desc' => "{cu2db.field-separator}",
57 'type' => "string",
58 'deft' => ",",
59 'reqd' => "no" },
60 { 'name' => "alreadyencrypted",
61 'desc' => "{cu2db.already-encrypted}",
62 'type' => "flag",
63 'reqd' => "no" },
64 { 'name' => "verbosity",
65 'desc' => "{scripts.verbosity}",
66 'type' => "int",
67 'range' => "0,",
68 'deft' => "1",
69 'reqd' => "no" },
70 { 'name' => "language",
71 'desc' => "{scripts.language}",
72 'type' => "string",
73 'reqd' => "no",
74 'hiddengli' => "yes" },
75 { 'name' => "out",
76 'desc' => "{scripts.out}",
77 'type' => "string",
78 'deft' => "STDERR",
79 'reqd' => "no",
80 'hiddengli' => "yes" },
81 { 'name' => "faillog",
82 'desc' => "{import.faillog}",
83 'type' => "string",
84 'deft' => &util::filename_cat($ENV{'GSDLHOME'},"etc", "error.txt"),
85 'reqd' => "no",
86 'modegai' => "3" },
87 { 'name' => "xml",
88 'desc' => "{scripts.xml}",
89 'type' => "flag",
90 'reqd' => "no",
91 'hiddengai' => "yes" },
92 { 'name' => "gai",
93 'desc' => "{scripts.gai}",
94 'type' => "flag",
95 'reqd' => "no",
96 'hiddengai' => "yes" }];
97
98
99
100my $options = { 'name' => "csv-usernames-to-db.pl",
101 'desc' => "{cu2db.desc}",
102 'args' => $arguments };
103
104
105sub convert_csv_to_db
106{
107 my ($csv_filename,$fieldseparator,$alreadyencrypted) = @_;
108
109 my $db_filename = &util::filename_cat($ENV{'GSDLHOME'},"etc","users.gdb");
110
111 my $cmd = "txt2db -append \"$db_filename\"";
112
113 if (!open(DBOUT,"| $cmd")) {
114 print STDERR "Error: failed to run\n $cmd\n";
115 print STDERR "$!\n";
116 exit(-1);
117 }
118
119 binmode(DBOUT, ":utf8");
120
121 if (!open(FIN,"<$csv_filename")) {
122 print STDERR "Error: Unable to open file $csv_filename\n";
123 print STDERR "$!\n";
124 exit(-1);
125 }
126
127 my $line;
128 while (defined ($line = <FIN>)) {
129 chomp $line;
130 my ($username,$password,$groups,$comment) = split(/$fieldseparator/,$line);
131
132 if (!$alreadyencrypted) {
133 $password = crypt($password,"Tp");
134 }
135
136 print DBOUT "[$username]\n";
137 print DBOUT "<comment>$comment\n";
138 print DBOUT "<enabled>true\n";
139 print DBOUT "<groups>$groups\n";
140 print DBOUT "<password>$password\n";
141 print DBOUT "<username>$username\n";
142 print DBOUT "-" x 70, "\n";
143 }
144
145 close(FIN);
146 close(DBOUT);
147}
148
149
150sub main
151{
152 my ($fieldseparator,$alreadyencrypted);
153 my ($language, $out, $faillog);
154
155 my $xml = 0;
156 my $gai = 0;
157
158 my $service = "csv-usernames";
159
160 my $hashParsingResult = {};
161 # general options available to all plugins
162 my $intArgLeftinAfterParsing
163 = parse2::parse(\@ARGV,$arguments,$hashParsingResult,
164 "allow_extra_options");
165 # Parse returns -1 if something has gone wrong
166 if ($intArgLeftinAfterParsing == -1)
167 {
168 &PrintUsage::print_txt_usage($options, "{cu2db.params}");
169 die "\n";
170 }
171
172 foreach my $strVariable (keys %$hashParsingResult)
173 {
174 eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
175 }
176
177
178 # If $language has been specified, load the appropriate resource bundle
179 # (Otherwise, the default resource bundle will be loaded automatically)
180 if ($language && $language =~ /\S/) {
181 &gsprintf::load_language_specific_resource_bundle($language);
182 }
183
184 if ($xml) {
185 &PrintUsage::print_xml_usage($options);
186 print "\n";
187 return;
188 }
189
190 if ($gai) { # the gli wants strings to be in UTF-8
191 &gsprintf::output_strings_in_UTF8;
192 }
193
194 # now check that we had exactly one leftover arg, which should be
195 # the collection name. We don't want to do this earlier, cos
196 # -xml arg doesn't need a collection name
197 # Or if the user specified -h, then we output the usage also
198 if ($intArgLeftinAfterParsing != 1 || (@ARGV && $ARGV[0] =~ /^\-+h/))
199 {
200 &PrintUsage::print_txt_usage($options, "{cu2db.params}");
201 die "\n";
202 }
203
204 my $csv_filename = shift @ARGV;
205
206 my $close_out = 0;
207 if ($out !~ /^(STDERR|STDOUT)$/i) {
208 open (OUT, ">$out") ||
209 (&gsprintf(STDERR, "{common.cannot_open_output_file}: $!\n", $out) && die);
210 $out = 'cu2db::OUT';
211 $close_out = 1;
212 }
213 $out->autoflush(1);
214
215 # check that we can open the faillog
216 if ($faillog eq "") {
217 $faillog = &util::filename_cat($ENV{'GSDLHOME'}, "etc", "error.txt");
218 }
219 open (FAILLOG, ">$faillog") ||
220 (&gsprintf(STDERR, "{script.cannot_open_fail_log}\n", $faillog) && die);
221
222
223 my $faillogname = $faillog;
224 $faillog = 'cu2db::FAILLOG';
225 $faillog->autoflush(1);
226
227 convert_csv_to_db($csv_filename,$fieldseparator,$alreadyencrypted);
228
229
230 close OUT if $close_out;
231 close FAILLOG;
232}
233
234
235&main();
Note: See TracBrowser for help on using the repository browser.