Bonjour à tous,

Contexte:
J'ai installé un serveur OpenLDAP sous Debian et j'importe mes entrées par fichiers LDIF au travers de l'interface phpldapadmin.

Problème
Dans un premier temps, l'ensemble de mes entrées se situent dans un fichier .CSV avec le format suivant:

dn,objectClass,cn,sn,l,telephoneNumber,givenName,mail,mobile,physicalDeliveryOfficeName,company
"cn=DUPONT Jacques,ou=001,dc=setec,dc=local",PageBlanche,DUPONT Jacques,Jacques,PARIS,0158642190,DUPONT,dupont@cons.setec.fr,,8IEM,SETEC CONSULTANTS

"cn=ROANI Alessandra,ou=001,dc=setec,dc=local",PageBlanche,ROANI Alessandra,Alessandra,PARIS,0140046380,ROANI,roani@tpi.setec.fr,0602010402,2EME,SETEC TPI
De ce fichier .CSV j'utilise le script PERL suivant que j'ai téléchargé sur Internet. Malheureusement, celui-ci ne gère pas les champs vides (comme le champ ci-dessus en rouge). Je n'y connais absolument rien en PERL est j'aimerais que lorsqu'un champ est vide pour une entrée, celui-ci soit supprimé pour cette entrée dans la génération du fichier LDIF.
La génération du fichier LDIF fonctionne, mais lors de l'intégration à la base il me génère une erreur en disant que la valeur du champ mobile (pour reprendre l'exemple) n'est pas valide.
Par exemple, le champ "mobile" de mon exemple est vide dans l'entrée "DUPONT". Il faudrait dans l'idéal que l'attribut mobile soit supprimé pour cette entrée.

Voici le script:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -
# -  AddData.pl
# -
# -	This program scans CSV export data and maps it to an add data set for ldap
# -
# -	You will be prompted for the file to convert.  The CSV should be in 
# -     a format where the first row has column headings and the first column
# -     should be the DN.  The output file will be the name of the input file
# -     with the LDIF extension added.
# - 
# -  Example:  perl FixData.pl
# -
 
 
use FileHandle;
 
my($csvFile, $inputfile, %domains, $outfile);
my(@linearray, @dnarray, @cnarray, $linecount);
my(%controlhash, $templatefile, %fieldmap, $logfile);
 
 
 
$csvFile = &promptUser("Name of file to be converted from CSV to LDIF 'Add' File");
$inputfile = new FileHandle($csvFile) ||
	die "Can't open the import file!\n";
$outfile = new FileHandle(">$csvFile.ldif") ||
	die "Can't create or open the output file!\n";
$logfile = new FileHandle(">AddData.log") ||
	die "Can't create or open the log file!\n";
 
# - Read the first line of the data file and build the control hash from it.
 
 
print $outfile "\n";
print $outfile "version: 1\n\n";
 
$linecount = 0;
while(defined($line = <$inputfile>)) { 
	$linecount++;
	chomp($line);
	if($linecount == 1) {
		@linearray = split(m',', $line);
		$fieldcount = 0;
 
		foreach $field (split(m',', $line)) {
			$fieldmap{$fieldcount} = $field;
			$fieldcount++;
		}
		next;
	}
 
	@linearray = split(m'"', $line);
	@fieldarray = split(m',', $linearray[2]);
 
	for($k = 0; $k < $fieldcount; $k++) {
		if($k == 0) {
			print $outfile "$fieldmap{$k}: ";
			print $outfile "$linearray[1]\n";
			print $outfile "changetype: add\n";
		} else {
			print $outfile "$fieldmap{$k}: $fieldarray[$k]\n";
		}
	}
	print $outfile "\n";
}
 
sub promptUser {
 
   #-------------------------------------------------------------------#
   #  two possible input arguments - $promptString, and $defaultValue  #
   #  make the input arguments local variables.                        #
   #-------------------------------------------------------------------#
 
   local($promptString,$defaultValue) = @_;
 
   #-------------------------------------------------------------------#
   #  if there is a default value, use the first print statement; if   #
   #  no default is provided, print the second string.                 #
   #-------------------------------------------------------------------#
 
   if ($defaultValue) {
      print $promptString, "[", $defaultValue, "]: ";
   } else {
      print $promptString, ": ";
   }
 
   $| = 1;               # force a flush after our print
   $_ = <STDIN>;         # get the input from STDIN (presumably the keyboard)
 
 
   #------------------------------------------------------------------#
   # remove the newline character from the end of the input the user  #
   # gave us.                                                         #
   #------------------------------------------------------------------#
 
   chomp;
 
   #-----------------------------------------------------------------#
   #  if we had a $default value, and the user gave us input, then   #
   #  return the input; if we had a default, and they gave us no     #
   #  no input, return the $defaultValue.                            #
   #                                                                 # 
   #  if we did not have a default value, then just return whatever  #
   #  the user gave us.  if they just hit the <enter> key,           #
   #  the calling routine will have to deal with that.               #
   #-----------------------------------------------------------------#
 
   if ("$defaultValue") {
      return $_ ? $_ : $defaultValue;    # return $_ if it has a value
   } else {
      return $_;
   }
}
Voici le fichier LDIF qu'il me génère:

version: 1

dn: cn=DUPONT Jacques,ou=003,dc=setec,dc=local
changetype: add
objectClass: PageBlanche
cn: DUPONT Jacques
sn: Jacques
l: PARIS
telephoneNumber: 0158642190
givenName: DUPONT
mail: dupont@cons.setec.fr
mobile: Champ à retirer car il est vide
physicalDeliveryOfficeName: 8IEM
company: SETEC CONSULTANTS

dn: cn=ROANI Alessandra,ou=003,dc=setec,dc=local
changetype: add
objectClass: PageBlanche
cn: ROANI Alessandra
sn: Alessandra
l: PARIS
telephoneNumber: 01 40 04 63 80
givenName: ROANI
mail: roani@tpi.setec.fr
mobile: 0602010402
physicalDeliveryOfficeName: 2EME
company: SETEC TPI