Net_LDAP | Net_MAC |
Net_LDAP | |
PEAR Manual | |
LDIF support was added to Net_LDAP in release 1.1.0a1.
LDIF files are in detail described at RFC 2849. Shortly, they contain directory data in an plain text, human readable kind, much like a SQL file does. However, unlike SQL files LDIF files are mostly data based, not action based. There are two different LDIF file contents, which can be mixed freely - content and change files. The first and most often used one is the LDIF content file:
Example LDIF content file
# # This is a content LDIF file. # It contains one single entry featuring several # attributes and one comment (this one). # attr1, attr4 and cn are single valued, the others are # multivalued. objectclass is a special case, LDAP servers # will interpret this operational attribute to define the # classes the object will belong to and thus, which attributes # it may contain. the OCL-attribute is usually multivalued. # version: 1 dn: cn=test1,ou=example,dc=cno objectclass: someobjectclass attr1: 12345 attr2: 1234 attr2: baz attr3: foo attr3: bar attr4: brrrzztt cn: test1 |
LDIF files could describe not only the data an entry contains, but also various changes to the entry itself. If such an LDIF file would then be given to a LDAP server, he would interpret those changes instead just importing the data. Note in the example below, that even though LDIF content and LDIF change files could be mixed freely, this is not true for individual entries: a specific entry may be either describing content or changes, but not both.
Example LDIF change file
# # This is a content+change LDIF file. # It does contain the (shortened) entry from the example above to show # that LDIF files can contain multiple entry modes. # The second entry is a change entry. In this case, some # operations will be done on the entries attributes. # version: 1 dn: cn=test1,ou=example,dc=cno objectclass: someobjectclass attr1: 12345 cn: test1 # Delete attr1, replace values of attr2 and add new attribute attr42 # The attribute "changetype" is special: it says, what to do with # this entries dataset. It could also be "delete" or "add" to delete # a whole entry or to add a completely fresh one. "modrdn" will # move the entry to a new location once the LDIF file is imported. dn: cn=test2,ou=example,dc=cno changetype: modify delete: attr1 - replace: attr2 attr2: 123456_newtest - add: attr42 attr42: the answer |
Before we can start using Net_LDAP_LDIF we must say some short words about how error handling works. Net_LDAP_LDIF was designed to have mostly the same API as the original PERL Net::LDAP::LDIF has. Because of this, the methods of Net_LDAP_LDIF do not return a Net_LDAP_Error object. You must use the error() method that will return a Net_LDAP_Error object in case of failure or true in case everything was ok. In LDIF reading mode, you can additionally use error_lines() to get knowledge about where in the input file the error occured.
Regardless if you want to read or write a LDIF file, you always have to use the constructor of Net_LDAP_LDIF to initialize your access to the LDIF file. You need to pass at least one parameter to Net_LDAP_LDIF(): the path of the file that should be read or written. You may pass the open mode as second parameter. The possible file open modes are "r" (read), "w" (write, clears the file first) and "a" (append to the end). In case you omit the open mode, read mode is assumed. The third optional parameter is an associative array containing one or several of the following options:
Name | Description | Default |
encode | Some DN values in LDIF cannot be written verbatim and have to be encoded in some way. Possible values are: "none", "canonical" and "base64" (RFC default) | base64 |
onerror | What should be done on errors? "undef" will let error handling in your hands, in this case you use error() and error_lines() to process errors manually. "die" aborts the script printing the error - this is sometimes useful for CLI scripts. "warn" just prints out the error but continues like "undef" would. | undef |
change | Turning this to "1" (true) will tell Net_LDAP_LDIF to write change sets instead of content files. | false |
lowercase | Set this to true to convert attribute names to lowercase when writing. | 0 |
sort | If true, sort attribute names when writing entries according to the rule: objectclass first then all other attributes alphabetically sorted by attribute name | 0 |
version | Set the LDIF version to write to the resulting LDIF file. According to RFC 2849 currently the only legal value for this option is 1 currently. | 1 |
wrap | Number of columns where output line wrapping shall occur. Setting it to 40 or lower inhibits wrapping. Useful for better human readability of the resulting file. | 78 |
One of the two modes how Net_LDAP_LDIF can be used is to read a LDIF file and parse its contents into an array of Net_LDAP_Entry objects. This is done using the read_entry()-method which will return the next entry. If you want to fetch all entries, you use the eof() to detect the end of the input file:
Parsing a LDIF file into Net_LDAP_Entry objects
|
Writing an LDIF file is very easy too. Just pass the entries you want to have written to the write_entry()-method. Beware, that if you have opened the file in "w" write mode this will clear any previous data of that file. Use "a" (append) if you just want add data.
Writing entries
|
The process of writing changes is exactly the same like writing entry contents. However there are two differences: Firstly you need to pass the "changes" option and secondly, the entries you want to write need changes. Entries not containing changes will silently be ignored since there is nothing to write.
Writing entry changes
|
Resulting LDIF change file
version: 1 dn: cn=foo,dc=example,dc=cno changetype: modify add: someattr someattr: added - replace: attr1 attr1: replaced - dn: cn=baz,dc=example,dc=cno changetype: modify delete: attr2 - delete: attr3 attr3: bar - |
Sometimes you are interested in the lines inside the LDIF file. For those cases you can use the current_lines() and next_lines() methods. They work in the current context, which may be confusing: current_lines() will always return the lines that have built up the current Net_LDAP_Entry object when called current_entry() after read_entry() has been called. next_lines() will always return the lines, that will build up the next entry from the current point of view, meaning "relative to the entry that was just been read". However, you can override this by activating the "force" parameter of next_lines() which allows you to loop over all entries. current_entry() behaves exactly like current_lines().
If you think, that the lines you have read would be better in form of an Net_LDAP_Entry object, use the parseLines() method to parse those lines into an entry. This is a good way if you need just a few specific entries of a large LDIF file.
Reading LDIF lines
|
Net_LDAP | Net_MAC |
Net_LDAP | |
PEAR Manual | |