/[virtual-ldap]/lib/LDAP/Koha.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /lib/LDAP/Koha.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 47 by dpavlin, Fri Apr 17 21:39:45 2009 UTC revision 62 by dpavlin, Thu Feb 18 16:22:32 2010 UTC
# Line 15  use File::Slurp; Line 15  use File::Slurp;
15    
16  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
17    
18    my $debug = 0; # XXX very slow
19    
20  # XXX test with:  # XXX test with:
21  #  #
22  # ldapsearch -h localhost -p 2389 -b dc=ffzg,dc=hr -x 'otherPager=200903160021'  # ldapsearch -h localhost -p 2389 -b dc=ffzg,dc=hr -x 'otherPager=200903160021'
# Line 25  our $database = 'koha'; Line 27  our $database = 'koha';
27  our $user     = 'unconfigured-user';  our $user     = 'unconfigured-user';
28  our $passwd   = 'unconfigured-password';  our $passwd   = 'unconfigured-password';
29    
30  our $max_results = 15; # 100; # FIXME  our $max_results = $ENV{MAX_RESULTS} || 3000; # FIXME must be enough for all users
31    our $objectclass_default = 'hrEduPerson';
32    
33  our $objectclass = 'HrEduPerson';  our $objectclass;
34    
35  $SIG{__DIE__} = sub {  $SIG{__DIE__} = sub {
36          warn "!!! DIE ", @_;          warn "!!! DIE ", @_;
# Line 45  my $ldap_sql_mapping = { Line 48  my $ldap_sql_mapping = {
48          'objectGUID'    => 'borrowernumber',          'objectGUID'    => 'borrowernumber',
49          'displayName'   => 'surname',          'displayName'   => 'surname',
50          'sn'            => 'surname',          'sn'            => 'surname',
51          'pager'         => 'rfid_sid',          'pager'         => 'a.attribute',       # was: rfid_sid
52  };  };
53    
54  sub __sql_column {  sub __sql_column {
# Line 119  sub __ldap_search_to_sql { Line 122  sub __ldap_search_to_sql {
122  sub _dn_attributes {  sub _dn_attributes {
123          my ($row,$base) = @_;          my ($row,$base) = @_;
124    
125          warn "## row = ",dump( $row );          warn "## row = ",dump( $row ) if $debug;
126    
127          die "no objectClass column in ",dump( $row ) unless defined $row->{objectClass};          die "no objectClass column in ",dump( $row ) unless defined $row->{objectClass};
128    
129          $row->{objectClass} = [ split(/\s+/, $row->{objectClass}) ] if $row->{objectClass} =~ m{\n};          $row->{objectClass} = [ split(/\s+/, $row->{objectClass}) ] if $row->{objectClass} =~ m{\n};
130    
131          warn "## row = ",dump( $row );          warn "## row = ",dump( $row ) if $debug;
132    
133          my $dn = delete( $row->{dn} ) || die "no dn in ",dump( $row );          my $dn = delete( $row->{dn} ) || die "no dn in ",dump( $row );
134    
135          # this does some sanity cleanup for our data          # this does some sanity cleanup for our data
136          my $base_as_domain = $base;  #       my $base_as_domain = $base;
137          $base_as_domain =~ s{dn=}{.};  #       $base_as_domain =~ s{dn=}{.};
138          $base_as_domain =~ s{^\.}{@};  #       $base_as_domain =~ s{^\.}{@};
139          $dn =~ s{$base_as_domain$}{};  #       $dn =~ s{$base_as_domain$}{};
140    #
141          $dn .= ',' . $base unless $dn =~ m{,}; # add base if none present  #       $dn .= ',' . $base unless $dn =~ m{,}; # add base if none present
142    
143          return ($dn, $row);          return ($dn, $row);
144  }  }
# Line 156  sub search { Line 159  sub search {
159    
160                  my $sql_where = '';                  my $sql_where = '';
161                  @values = ();                  @values = ();
162                    $objectclass = '';
163    
164                  foreach my $filter ( keys %{ $reqData->{'filter'} } ) {                  foreach my $filter ( keys %{ $reqData->{'filter'} } ) {
165    
# Line 177  sub search { Line 181  sub search {
181                                          }                                          }
182                                  }                                  }
183    
                                 $sql_where .= ' ' . join( " $filter ", @limits );  
   
184                          } else {                          } else {
185                                  __ldap_search_to_sql( $filter, $reqData->{'filter'}->{$filter} );                                  __ldap_search_to_sql( $filter, $reqData->{'filter'}->{$filter} );
186                          }                          }
187    
188                            $sql_where .= ' ' . join( " $filter ", @limits ) if @limits;
189    
190                  }                  }
191    
192                    $objectclass ||= $objectclass_default;
193    
194                    my $sql_select = read_file( lc "sql/$objectclass.sql" );
195                  if ( $sql_where ) {                  if ( $sql_where ) {
196                          $sql_where = " where $sql_where";                          if ( $sql_select !~ m{where}i ) {
197                                    $sql_where = " where $sql_where";
198                            } else {
199                                    $sql_where = " and $sql_where";
200                            }
201                  }                  }
202    
                 my $sql_select = read_file( lc "sql/$objectclass.sql" );  
203    
204                  warn "# SQL:\n$sql_select\n", $sql_where ? $sql_where : '-- no where', "\n# DATA: ",dump( @values );                  my $sql
205                  my $sth = $dbh->prepare( $sql_select . $sql_where . " LIMIT $max_results" ); # XXX remove limit?                          = $sql_select
206                            . $sql_where
207    #                       . ( $objectclass =~ m{person}i ? " LIMIT $max_results" : '' ) # add limit just for persons
208                            ;
209    
210                    warn "# SQL:\n$sql\n# DATA: ",dump( @values );
211                    my $sth = $dbh->prepare( $sql );
212                  $sth->execute( @values );                  $sth->execute( @values );
213    
214                  warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} );                  warn "# ", $sth->rows, " results for ",dump( $reqData->{'filter'} );
215    
216                    my $last_dn = '?';
217                    my $entry;
218    
219                  while (my $row = $sth->fetchrow_hashref) {                  while (my $row = $sth->fetchrow_hashref) {
220    
221                          my ( $dn, $attributes ) = _dn_attributes( $row, $base );                          my ( $dn, $attributes ) = _dn_attributes( $row, $base );
222    
223                          my $entry = Net::LDAP::Entry->new;                          warn "# dn $last_dn ... $dn\n";
                         $entry->dn( $dn );  
                         $entry->add( %$attributes );  
224    
225                          #$entry->changetype( 'modify' );                          if ( $dn ne $last_dn ) {
226    
227                          warn "### entry ",$entry->dump( \*STDERR );                                  if ( $entry ) {
228                                            #$entry->changetype( 'modify' );
229                                            warn "### entry ",$entry->dump( \*STDERR );
230                                            push @entries, $entry;
231                                            undef $entry;
232                                    }
233    
234                                    $dn =~ s{@[^,]+}{};
235    
236                                    $entry = Net::LDAP::Entry->new;
237                                    $entry->dn( $dn );
238    
239                                    $entry->add( %$attributes );
240    
241                            } else {
242                                    foreach my $n ( keys %$attributes ) {
243                                            my $v = $attributes->{$n};
244                                            warn "# attr $n = $v\n";
245                                            $entry->add( $n, $v ) if $entry->get_value( $n ) ne $v;
246                                    }
247                            }
248    
249    
250                            $last_dn = $dn;
251    
252                    }
253    
254                    if ( $entry ) {
255                            warn "### last entry ",$entry->dump( \*STDERR );
256                          push @entries, $entry;                          push @entries, $entry;
257                  }                  }
258    

Legend:
Removed from v.47  
changed lines
  Added in v.62

  ViewVC Help
Powered by ViewVC 1.1.26