OAI-PMH second try
[koha.git] / C4 / OAI / DC.pm
1 #  ---------------------------------------------------------------------
2 #   Dublin Core helper class
3 #    v1.0
4 #    January 2007
5 #  ------------------+--------------------------------------------------
6 #   Ph. Jaillon      | 
7 #  ------------------+----------------------+---------------------------
8 #   Department of Computer Science          |      
9 #  -----------------------------------------+-------------+-------------
10 #   Ecole Nationale Superieure des Mines de St-Etienne    |  www.emse.fr 
11 #  -------------------------------------------------------+-------------
12
13 =head1 OAI::DC Dublin Core formating helper
14          
15 OAI::DC is an helper class for Dublin Core metadata. As Dublin Core have a well known
16 set of fields, OAI::DC is a subclass of the OAI::DP class and it implements a default
17 behavior to build correct answers. The data references returned by Archive_GetRecord
18 and Archive_ListRecords must be instance providing the following method (they are used
19 to translate your own data to Dublin Core) : Title(), Identifier(), Subject(), Creator(),
20 Date(), Description(), Publisher(), Language() and Type(). The semantic of these methods is
21 the same as the corresponding Dublin Core field.
22
23 To return correct metadata, you must provide or overide theses methods:
24
25 =over 
26
27 =over
28
29 =item B<new>: initialization step,
30
31 =item B<dispose>: clean up step,
32
33 =item B<Archive_ListSets>: return list of defined sets,
34
35 =item B<Archive_GetRecord>: return a record,
36
37 =item B<Archive_ListRecords>: return a list of records,
38
39 =item B<Archive_ListIdentifiers>: return a list of record identifiers,
40
41 =back
42
43 =back
44
45 =head2 new
46
47 =over
48
49 Object of this method is to build a new instance of your OAI data provider. At this step
50 you can overide somme default information about the repository, you can also initiate
51 connexion to a database... Parameters to the new method are user defined.
52
53 =back
54
55 =head2 dispose
56
57 =over
58
59 It's time to disconnect from database (if required). Must explicitly call SUPER::dispose().
60
61 =back
62
63 =head2 Archive_ListSets
64
65 =over
66
67 Return a reference to an array of list set. Each list set is a reference to a two element array.
68 The first element is the set name of the set and the second is its short description.
69
70         sub Archive_ListSets {
71                 [
72                         [ 'SET1', 'Description of the SET1'],
73                         [ 'SET2', 'Description of the SET2'],
74                 ];
75         }
76
77 =back
78
79 =head2 Archive_GetRecord
80
81 =over
82
83 This method take a record identifier and metadata format as parameter. It must return a reference to
84 the data associated to identifier. Data are reference to a hash and must provide methodes describe
85 at the begining of DC section.
86
87 =back
88
89 =head2 Archive_ListRecords
90
91 =over
92
93 Object of this method is to return a list of records occording to the user query. Parameters of the method
94 are the set, the from date, the until date, the metadata type required and a resumption token if supported.
95
96 The method must return a reference to a list of records, the metadata type of the answer and reference to
97 token information. Token information must be undefined or a reference to a hash with the I<completeListSize>
98 and the I<cursor> keys set.
99
100 =back
101
102 =cut
103
104 package C4::OAI::DC;
105
106 use Encode;
107 use C4::OAI::DP;
108 use vars ('@ISA');
109 @ISA = ("C4::OAI::DP");
110
111 # format DC record
112 sub FormatDC
113 {
114    my ($self, $hashref) = @_;
115
116    return undef if( $hashref->Status() eq 'deleted' );
117
118    {
119       title       => $hashref->Title(),
120       identifier  => $hashref->Identifier(),
121       subject     => $hashref->Subject(),
122       creator     => $hashref->Creator(),
123       date        => $hashref->Date(),
124       description => $hashref->Description(),
125       publisher   => $hashref->Publisher(),
126       language    => $hashref->Language(),
127       type        => $hashref->Type(),
128       mdorder     => [ qw (title creator subject description contributor publisher date type format identifier source language relation coverage rights) ]
129    };
130 }
131
132 # format header for ListIdentifiers
133 sub Archive_FormatHeader
134 {
135    my ($self, $hashref, $metadataFormat) = @_;
136    
137    $self->FormatHeader ($hashref->Identifier()->[0] ,
138                         $hashref->DateStamp(),
139                         '',
140                         $hashref->Set()
141                        );
142 }
143
144 # retrieve records from the source archive as required
145 sub Archive_FormatRecord
146 {
147    my ($self, $hashref, $metadataFormat) = @_;
148    
149    if ($self->MetadataFormatisValid ($metadataFormat) == 0)
150    {
151       $self->AddError ('cannotDisseminateFormat', 'The value of metadataPrefix ('.$metadataFormat.') is not supported by the repository');
152       return '';
153    }
154
155    my $dc = $self->FormatDC ($hashref);
156    my $header = "<oaidc:dc xmlns=\"http://purl.org/dc/elements/1.1/\" ".
157                 "xmlns:oaidc=\"http://www.openarchives.org/OAI/2.0/oai_dc/\" ".
158                 "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ".
159                 "xsi:schemaLocation=\"http://www.openarchives.org/OAI/2.0/oai_dc/ ".
160                 "http://www.openarchives.org/OAI/2.0/oai_dc.xsd\">\n";
161    my $footer = "</oaidc:dc>\n";
162    my $metadata = '';
163
164    $metadata = $header . encode("utf8", decode( "iso-8859-1",$self->{'utility'}->FormatXML($dc))) . $footer if( $dc );
165
166    $self->FormatRecord ($hashref->Identifier()->[0] ,
167                         $hashref->DateStamp(),
168                         $hashref->Status(),
169                         $hashref->Set(),
170                         $metadata,
171                         '',
172                        );
173 }
174
175
176 # get full list of mdps or list for specific identifier
177 sub Archive_ListMetadataFormats
178 {
179    my ($self, $identifier) = @_;
180    
181    if ((! defined $identifier) || ($identifier eq '')) {
182       return ['oai_dc'];
183    }
184    else {
185       $self->AddError ('idDoesNotExist', 'The value of the identifier argument is unknown or illegal in this repository');
186    }
187    return [];
188 }
189
190
191 # get full list of sets from the archive
192 sub Archive_ListSets
193 {
194         [];
195 }
196                               
197
198 # get a single record from the archive
199 sub Archive_GetRecord
200 {
201    my ($self, $identifier, $metadataFormat) = @_;
202
203    $self->AddError ('idDoesNotExist', 'The value of the identifier argument is unknown or illegal in this repository');
204    undef;
205 }
206
207 # list metadata records from the archive
208 sub Archive_ListRecords
209 {
210    my ($self, $set, $from, $until, $metadataPrefix, $resumptionToken) = @_;
211    my $tokenInfo = undef;
212
213         $self->AddError ('noRecordsMatch', 'The combination of the values of arguments results in an empty set');
214         ( [], $resumptionToken, $metadataPrefix, $tokenInfo );
215 }
216
217
218 # list identifiers (headers) from the archive
219 sub Archive_ListIdentifiers
220 {
221    my ($self, $set, $from, $until, $metadataPrefix, $resumptionToken) = @_;
222
223    if (($metadataPrefix ne '') && ($self->MetadataFormatisValid ($metadataPrefix) == 0))
224    {
225       $self->AddError ('cannotDisseminateFormat', 'The value of metadataPrefix ('.$metadataPrefix.')is not supported by the repository');
226       return '';
227    }
228    
229    $self->Archive_ListRecords ($set, $from, $until, $metadataPrefix, $resumptionToken);
230 }
231
232 1;
233