"Fossies" - the Fresh Open Source Software Archive

Member "perl-ldap-0.66/lib/Net/LDAP/Control/Sort.pm" (8 Apr 2015, 4784 Bytes) of package /linux/privat/perl-ldap-0.66.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Sort.pm" see the Fossies "Dox" file reference documentation.

    1 # Copyright (c) 1999-2004 Graham Barr <gbarr@pobox.com>. All rights reserved.
    2 # This program is free software; you can redistribute it and/or
    3 # modify it under the same terms as Perl itself.
    4 
    5 package Net::LDAP::Control::Sort;
    6 
    7 use Net::LDAP::Control;
    8 
    9 our @ISA = qw(Net::LDAP::Control);
   10 our $VERSION = '0.04';
   11 
   12 use Net::LDAP::ASN qw(SortRequest);
   13 use strict;
   14 
   15 sub init {
   16   my($self) = @_;
   17 
   18   if (exists $self->{value}) {
   19     $self->value($self->{value});
   20   }
   21   elsif (exists $self->{order}) {
   22     $self->order(ref($self->{order}) ? @{$self->{order}} : $self->{order});
   23   }
   24 
   25   $self;
   26 }
   27 
   28 sub value {
   29   my $self = shift;
   30 
   31   if (@_) {
   32     my $value = shift;
   33 
   34     delete $self->{value};
   35     delete $self->{order};
   36     delete $self->{error};
   37 
   38     my $asn = $SortRequest->decode($value);
   39 
   40     unless ($asn) {
   41       $self->{error} = $@;
   42       return undef;
   43     }
   44 
   45     $self->{order} = [ map {
   46       ($_->{reverseOrder} ? '-' : '')
   47       . $_->{type}
   48       . (defined($_->{orderingRule}) ? ":$_->{orderingRule}" : '')
   49     } @{$asn->{order}}];
   50 
   51     return $self->{value} = $value;
   52   }
   53 
   54   unless (defined $self->{value}) {
   55     $self->{value} = $SortRequest->encode(
   56       order => [
   57     map {
   58       /^(-)?([^:]+)(?::(.+))?/;
   59       {
   60         type => $2,
   61         (defined $1 ? (reverseOrder => 1)  : ()),
   62         (defined $3 ? (orderingRule => $3) : ())
   63       }
   64     } @{$self->{order} || []}
   65       ]
   66     ) or $self->{error} = $@;
   67   }
   68 
   69   $self->{value};
   70 }
   71 
   72 sub valid { exists shift->{order} }
   73 
   74 sub order {
   75   my $self = shift;
   76 
   77   if (@_) {
   78     # @_ can either be a list, or a single item.
   79     # if a single item it can be a string, which needs
   80     # to be split on spaces, or a reference to a list
   81     #
   82     # Each element has three parts
   83     #  leading - (optional)
   84     #  an attribute name
   85     #  :match-rule (optional)
   86 
   87     my @order = (@_ == 1) ? split(/\s+/, $_[0]) : @_;
   88 
   89     delete $self->{value};
   90     delete $self->{order};
   91     delete $self->{error};
   92 
   93     foreach (@order) {
   94       next  if /^-?[^:]+(?::.+)?$/;
   95 
   96       $self->{error} = "Bad order argument '$_'";
   97       return;
   98     }
   99 
  100     $self->{order} = \@order;
  101   }
  102 
  103   return @{$self->{order}};
  104 }
  105 
  106 1;
  107 
  108 __END__
  109 
  110 
  111 =head1 NAME
  112 
  113 Net::LDAP::Control::Sort - Server Side Sort (SSS) control object
  114 
  115 =head1 SYNOPSIS
  116 
  117  use Net::LDAP::Control::Sort;
  118  use Net::LDAP::Constant qw(LDAP_CONTROL_SORTRESULT);
  119 
  120  $sort = Net::LDAP::Control::Sort->new(
  121    order => "cn -phone"
  122  );
  123 
  124  $mesg = $ldap->search( @args, control => [ $sort ]);
  125 
  126  ($resp) = $mesg->control( LDAP_CONTROL_SORTRESULT );
  127 
  128  print "Results are sorted\n"  if $resp and !$resp->result;
  129 
  130 =head1 DESCRIPTION
  131 
  132 C<Net::LDAP::Control::Sort> is a sub-class of
  133 L<Net::LDAP::Control>.  It provides a class
  134 for manipulating the LDAP Server Side Sort (SSS) request control
  135 C<1.2.840.113556.1.4.473> as defined in RFC-2891
  136 
  137 If the server supports sorting, then the response from a search
  138 operation will include a sort result control. This control is handled
  139 by L<Net::LDAP::Control::SortResult>.
  140 
  141 =head1 CONSTRUCTOR ARGUMENTS
  142 
  143 =over 4
  144 
  145 =item order
  146 
  147 A string which defines how entries may be sorted. It consists of
  148 multiple directives, separated by whitespace. Each directive describes how
  149 to sort entries using a single attribute. If two entries have identical
  150 attributes, then the next directive in the list is used.
  151 
  152 Each directive specifies a sorting order as follows
  153 
  154   -attributeType:orderingRule
  155 
  156 The leading C<-> is optional, and if present indicates that the sorting order should
  157 be reversed. C<attributeType> is the attribute name to sort by. C<orderingRule> is optional and
  158 indicates the rule to use for the sort and should be valid for the given C<attributeType>.
  159 
  160 Any one attributeType should only appear once in the sorting list.
  161 
  162 B<Examples>
  163 
  164   "cn"         sort by cn using the default ordering rule for the cn attribute
  165   "-cn"        sort by cn using the reverse of the default ordering rule
  166   "age cn"     sort by age first, then by cn using the default ordering rules
  167   "cn:1.2.3.4" sort by cn using the ordering rule defined as 1.2.3.4
  168 
  169 =back
  170 
  171 
  172 =head1 METHODS
  173 
  174 As with L<Net::LDAP::Control> each constructor argument
  175 described above is also available as a method on the object which will
  176 return the current value for the attribute if called without an argument,
  177 and set a new value for the attribute if called with an argument.
  178 
  179 =head1 SEE ALSO
  180 
  181 L<Net::LDAP>,
  182 L<Net::LDAP::Control::SortResult>,
  183 L<Net::LDAP::Control>,
  184 http://www.ietf.org/rfc/rfc2891.txt
  185 
  186 =head1 AUTHOR
  187 
  188 Graham Barr E<lt>gbarr@pobox.comE<gt>
  189 
  190 Please report any bugs, or post any suggestions, to the perl-ldap mailing list
  191 E<lt>perl-ldap@perl.orgE<gt>
  192 
  193 =head1 COPYRIGHT
  194 
  195 Copyright (c) 1999-2004 Graham Barr. All rights reserved. This program is
  196 free software; you can redistribute it and/or modify it under the same
  197 terms as Perl itself.
  198 
  199 =cut