source: main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm@ 27333

Last change on this file since 27333 was 27333, checked in by ak19, 11 years ago
  1. Added the metadataaction::get_import_metadata_array() subroutine for completion and tested it. 2. A couple of unicode subroutines print warnings when parameters are undefined. Since they can be undefined sometimes, added a test for this so as to avoid the unnecessary warnings.
File size: 111.8 KB
Line 
1###########################################################################
2#
3# metadataaction.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 2009 New Zealand Digital Library Project
9#
10# This program is free software; you can redistr te it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package metadataaction;
27
28use strict;
29
30use cgiactions::baseaction;
31
32use dbutil;
33use ghtml;
34
35use JSON;
36
37
38BEGIN {
39# unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan/perl-5.8");
40 require XML::Rules;
41}
42
43@metadataaction::ISA = ('baseaction');
44
45my $action_table =
46{
47 #GET METHODS
48 "get-import-metadata" => {
49 'compulsory-args' => [ "d", "metaname" ],
50 'optional-args' => [ "metapos" ] },
51
52 "get-archives-metadata" => {
53 'compulsory-args' => [ "d", "metaname" ],
54 'optional-args' => [ "metapos" ] },
55
56 "get-index-metadata" => {
57 'compulsory-args' => [ "d", "metaname" ],
58 'optional-args' => [ "metapos" ] },
59
60 "get-metadata" => { # alias for get-index-metadata
61 'compulsory-args' => [ "d", "metaname" ],
62 'optional-args' => [ "metapos" ] },
63
64 "get-live-metadata" => {
65 'compulsory-args' => [ "d", "metaname" ],
66 'optional-args' => [ ] },
67
68 "get-metadata-array" => { # where param can be ONE of: index (default), import, archives, live
69 'compulsory-args' => [ "json" ],
70 'optional-args' => [ "where" ],
71 'help-string' => [
72 'metadata-server.pl?a=get-metadata-array&c=demo&where=index&json=[{"docid":"HASHc5bce2d6d3e5b04e470ec9","metatable":[{"metaname":"username","metapos":"all"},{"metaname":"usertimestamp","metapos":"all"}, {"metaname":"usercomment","metapos":"all"}]}]'
73 ]},
74
75 #SET METHODS
76 "set-live-metadata" => {
77 'compulsory-args' => [ "d", "metaname", "metavalue" ],
78 'optional-args' => [ ] },
79
80 "set-metadata" => { # generic set-meta function. The 'where' param can be set to any combination of index|archives|import|live. docid d is still compulsory for setting index, archives and live meta
81 'compulsory-args' => [ "metaname", "metavalue" ],
82 'optional-args' => [ "where", "metapos", "metamode", "prevmetavalue", "d", "f" ] },
83
84 "set-index-metadata" => {
85 'compulsory-args' => [ "d", "metaname", "metavalue" ],
86 'optional-args' => [ "metapos", "metamode" ] },
87
88 "set-archives-metadata" => {
89 'compulsory-args' => [ "d", "metaname", "metavalue" ],
90 'optional-args' => [ "metapos", "metamode", "prevmetavalue" ] }, # metamode can be "accumulate", "override",
91
92 "set-import-metadata" => {
93 'compulsory-args' => [ "metaname", "metavalue" ],
94 'optional-args' => [ "d", "f", "metamode", "metapos", "prevmetavalue" ] }, # metamode can be "accumulate", "override", or "unique-id". Also need to add the ability to specify a previous metadata value to overwrite (because we can't use metapos). Metapos now supported, but assumes you are working with a Simple (instead of Complex) collection
95
96 #SET METHODS (ARRAY)
97 "set-metadata-array" => {
98 'compulsory-args' => [ "where", "json" ],
99 'optional-args' => [ ],
100 'help-string' => [
101 'A simple example: metadata-server.pl?a=set-metadata-array&where=archives|index|import&c=demo&json=[{"docid":"HASHc5bce2d6d3e5b04e470ec9","metaname":"Title","metavalue":"Tralalala","metamode":"accumulate"},{"docid":"HASHbe483fa4df4e096335d1c8","metaname":"Title","metavalue":"Lala was here","metapos":0, "metamode":"override"}]',
102
103 'A more complex example: metadata-server.pl?a=set-metadata-array&where=archives|index&c=demo&json=[{"docid":"HASHc5bce2d6d3e5b04e470ec9.1","metatable":[{"metaname":"Title","metavals":["Transformers","Robots in disguise","Autobots"]}],"metamode":"override"},{"docid":"HASHbe483fa4df4e096335d1c8.2","metaname":"Title","metavalue":"Pinky was here","metamode":"accumulate"}]' ] },
104
105# The same examples rewritten for when running the metadata-server.pl script from the commandline:
106
107# the simple example: metadata-server.pl a="set-metadata-array" where="archives|index|import" c="demo" json="[{\"docid\":\"HASHc5bce2d6d3e5b04e470ec9\",\"metaname\":\"Title\",\"metavalue\":\"Tralalala\",\"metamode\":\"accumulate\"},{\"docid\":\"HASHbe483fa4df4e096335d1c8\",\"metaname\":\"Title\",\"metavalue\":\"Lala was here\",\"metapos\":0, \"metamode\":\"override\"}]",
108
109# the more complex example: metadata-server.pl a="set-metadata-array" where="archives|index" c="demo" json="[{\"docid\":\"HASHc5bce2d6d3e5b04e470ec9.1\",\"metatable\":[{\"metaname\":\"Title\",\"metavals\":[\"Transformers\",\"Robots in disguise\",\"Autobots\"]}],\"metamode\":\"override\"},{\"docid\":\"HASHbe483fa4df4e096335d1c8.2\",\"metaname\":\"Title\",\"metavalue\":\"Pinky was here\",\"metamode\":\"accumulate\"}]"
110
111 "set-archives-metadata-array" => {
112 'compulsory-args' => [ "json" ],
113 'optional-args' => [ ] },
114
115 "set-import-metadata-array" => {
116 'compulsory-args' => [ "json" ],
117 'optional-args' => [ ] },
118
119 "set-index-metadata-array" => {
120 'compulsory-args' => [ "json" ],
121 'optional-args' => [ ] },
122
123 "set-live-metadata-array" => {
124 'compulsory-args' => [ "json" ],
125 'optional-args' => [ ] },
126
127 #REMOVE METHODS
128 "remove-import-metadata" => {
129 'compulsory-args' => [ "d", "metaname", "metavalue" ], #TODO: add f argument
130 'optional-args' => [ "metapos" ] }, # only provide metapos arg for SIMPLE collections
131
132 "remove-archives-metadata" => {
133 'compulsory-args' => [ "d", "metaname" ], #TODO: add f argument
134 'optional-args' => [ "metapos", "metavalue" ] },
135
136 "remove-live-metadata" => {
137 'compulsory-args' => [ "d", "metaname" ],
138 'optional-args' => [ ] },
139
140 "remove-index-metadata" => {
141 'compulsory-args' => [ "d", "metaname" ],
142 'optional-args' => [ "metapos", "metavalue" ] },
143
144 "remove-metadata" => { # generic remove-meta function. The 'where' param can be set to any combination of index|archives|import|live. docid d is still compulsory for setting index, archives and live meta
145 'compulsory-args' => [ "d", "metaname" ],
146 'optional-args' => [ "where", "metapos", "metavalue" ] },
147
148 #INSERT METHODS
149 "insert-metadata" => {
150 'compulsory-args' => [ "d", "metaname", "metavalue" ],
151 'optional-args' => [ ] }
152};
153
154
155sub new
156{
157 my $class = shift (@_);
158 my ($gsdl_cgi,$iis6_mode) = @_;
159
160 # Treat metavalue specially. To transmit this through a GET request
161 # the Javascript side has url-encoded it, so here we need to decode
162 # it before proceeding
163
164 my $url_encoded_metavalue = $gsdl_cgi->param("metavalue");
165 my $url_decoded_metavalue = &unicode::url_decode($url_encoded_metavalue,1);
166 my $unicode_array = &unicode::utf82unicode($url_decoded_metavalue);
167
168 $url_decoded_metavalue = join("",map(chr($_),@$unicode_array));
169 $gsdl_cgi->param("metavalue",$url_decoded_metavalue);
170
171 my $self = new baseaction($action_table,$gsdl_cgi,$iis6_mode);
172
173 return bless $self, $class;
174}
175
176
177sub get_live_metadata
178{
179 my $self = shift @_;
180
181 my $username = $self->{'username'};
182 my $collect = $self->{'collect'};
183 my $gsdl_cgi = $self->{'gsdl_cgi'};
184 my $gsdlhome = $self->{'gsdlhome'};
185 my $infodbtype = $self->{'infodbtype'};
186
187 # live metadata gets/saves value scoped (prefixed) by the current usename
188 # so (for now) let's not bother to enforce authentication
189
190 # Obtain the collect dir
191 my $site = $self->{'site'};
192 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
193 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
194
195 # No locking collection when getting metadata, only when setting it
196# $self->lock_collection($username, $collect); # Make sure the collection isn't locked by someone else
197
198 # look up additional args
199 my $docid = $self->{'d'};
200 if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
201 $gsdl_cgi->generate_error("No docid (d=...) specified.");
202 }
203
204 # Generate the dbkey
205 my $metaname = $self->{'metaname'};
206 my $dbkey = "$docid.$metaname";
207
208 # To people who know $collect_tail please add some comments
209 # Obtain path to the database
210 my $collect_tail = $collect;
211 $collect_tail =~ s/^.*[\/|\\]//;
212 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
213 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
214
215 # Obtain the content of the key
216 my $cmd = "gdbmget $infodb_file_path $dbkey";
217 if (open(GIN,"$cmd |") == 0) {
218 # Catch error if gdbmget failed
219 my $mess = "Failed to get metadata key: $metaname\n";
220 $mess .= "$!\n";
221
222 $gsdl_cgi->generate_error($mess);
223 }
224 else {
225 binmode(GIN,":utf8");
226 # Read everything in and concatenate them into $metavalue
227 my $metavalue = "";
228 my $line;
229 while (defined ($line=<GIN>)) {
230 $metavalue .= $line;
231 }
232 close(GIN);
233 chomp($metavalue); # Get rid off the tailing newlines
234 $gsdl_cgi->generate_ok_message("$metavalue");
235 }
236
237 # Release the lock once it is done
238# $self->unlock_collection($username, $collect);
239}
240
241# just calls the index version
242sub get_metadata
243{
244 my $self = shift @_;
245 $self->get_index_metadata(@_);
246}
247
248# JSON version that will get the requested metadata values
249# from the requested source (index, import, archives or live)
250# One of the params is a JSON string and the return value is JSON too
251# http://forums.asp.net/t/1844684.aspx/1 - Web api method return json in string
252sub get_metadata_array
253{
254 my $self = shift @_;
255
256 my $where = $self->{'where'};
257 if (!$where) {
258 $where = "index"; # default behaviour is to get the values from index
259 }
260
261 # Only when setting metadata do we perform authentication and do we lock the collection
262 # not when getting metadata
263
264 # for get_meta_array, the where param can only be ONE of import, archives, index, live
265 if($where =~ m/index/) {
266 $self->_get_index_metadata_array(@_);
267 }
268 elsif($where =~ m/archives/) {
269 $self->_get_archives_metadata_array(@_);
270 }
271 elsif($where =~ m/import/) {
272 $self->_get_import_metadata_array(@_);
273 }
274 elsif($where =~ m/live/) {
275# $self->_get_live_metadata_array(@_);
276 }
277}
278
279sub _get_import_metadata_array {
280
281 my $self = shift @_;
282
283 my $collect = $self->{'collect'};
284 my $gsdl_cgi = $self->{'gsdl_cgi'};
285 my $site = $self->{'site'};
286 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
287
288 # look up additional args
289 my $infodbtype = $self->{'infodbtype'};
290
291 my $archive_dir = &util::filename_cat($collect_dir, $collect, "archives");
292 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
293 my $json_str = $self->{'json'};
294 my $doc_array = decode_json $json_str;
295
296 my $json_result_str = "[";
297 my $first_doc_rec = 1;
298 foreach my $doc_array_rec ( @$doc_array ) {
299
300 my $docid = $doc_array_rec->{'docid'}; # no subsection metadata support in metadata.xml, only toplevel meta
301
302 if($first_doc_rec) {
303 $first_doc_rec = 0;
304 } else {
305 $json_result_str .= ",";
306 }
307 $json_result_str = $json_result_str . "{\"docid\":\"" . $docid . "\"";
308
309 my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
310 $json_result_str = $json_result_str . ",\"metatable\":[";
311
312 my $first_rec = 1;
313 foreach my $metatable_rec ( @$metatable ) { # the subarray metatable is an array of hashmaps
314
315 # Read the docid entry
316 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
317 # This now stores the full pathname
318 my $import_filename = $doc_rec->{'src-file'}->[0];
319
320 # figure out correct metadata.xml file [?]
321 # Assuming the metadata.xml file is next to the source file
322 # Note: This will not work if it is using the inherited metadata from the parent folder
323 my ($import_tailname, $import_dirname) = File::Basename::fileparse($import_filename);
324 my $metadata_xml_filename = &util::filename_cat($import_dirname, "metadata.xml");
325
326
327 if($first_rec) {
328 $first_rec = 0;
329 } else {
330 $json_result_str .= ",";
331 }
332
333 my $metaname = $metatable_rec->{'metaname'};
334 $json_result_str .= "{\"metaname\":\"$metaname\",\"metavals\":[";
335
336 my $metapos = $metatable_rec->{'metapos'}; # 0... 1|all|undefined
337 if(!defined $metapos) {
338 $metapos = 0;
339 }
340
341 # Obtain the specified metadata value(s)
342 my $metavalue;
343
344 if($metapos ne "all") { # get the value at a single metapos
345 $metavalue = $self->get_metadata_from_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $import_tailname);
346
347 #print STDERR "**** Metafilename, metaname, metapos, sec_num: $metadata_xml_filename, $metaname, $metapos, $import_tailname\n";
348
349 $json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}";
350
351 } else {
352 my $first_metaval = 1;
353 $metapos = 0;
354 $metavalue = $self->get_metadata_from_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $import_tailname);
355
356 while (defined $metavalue && $metavalue ne "") {
357 if($first_metaval) {
358 $first_metaval = 0;
359 } else {
360 $json_result_str .= ",";
361 }
362
363 $json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}";
364
365 $metapos++;
366 $metavalue = $self->get_metadata_from_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $import_tailname);
367 }
368 }
369
370 $json_result_str .= "]}"; # close metavals array and metatable record
371 }
372
373 $json_result_str .= "]}"; # close metatable array and docid record
374 }
375
376 $json_result_str .= "]"; # close array of docids
377 $gsdl_cgi->generate_ok_message($json_result_str."\n");
378}
379
380# Unused method, but included for completion. Tested, works. Takes a JSON string and returns a JSON string.
381# For more information on the format of the output, see _get_index_metadata_array, which is in use.
382sub _get_archives_metadata_array {
383
384 my $self = shift @_;
385
386 my $collect = $self->{'collect'};
387 my $gsdl_cgi = $self->{'gsdl_cgi'};
388 my $site = $self->{'site'};
389 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
390
391 # look up additional args
392 my $infodbtype = $self->{'infodbtype'};
393
394 my $archive_dir = &util::filename_cat($collect_dir, $collect, "archives");
395 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
396
397 my $json_str = $self->{'json'};
398 my $doc_array = decode_json $json_str;
399
400 my $json_result_str = "[";
401 my $first_doc_rec = 1;
402 foreach my $doc_array_rec ( @$doc_array ) {
403
404 my $docid = $doc_array_rec->{'docid'};
405
406 if($first_doc_rec) {
407 $first_doc_rec = 0;
408 } else {
409 $json_result_str .= ",";
410 }
411 $json_result_str = $json_result_str . "{\"docid\":\"" . $docid . "\"";
412
413 my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
414 $json_result_str = $json_result_str . ",\"metatable\":[";
415
416 my $first_rec = 1;
417 foreach my $metatable_rec ( @$metatable ) { # the subarray metatable is an array of hashmaps
418
419 my ($docid, $docid_secnum) = ($doc_array_rec->{'docid'} =~ m/^(.*?)(\..*)?$/);
420 $docid_secnum = "" if (!defined $docid_secnum);
421
422 # Read the docid entry
423 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
424 # This now stores the full pathname
425 my $doc_filename = $doc_rec->{'doc-file'}->[0];
426 $doc_filename = &util::filename_cat($archive_dir, $doc_filename);
427
428 if($first_rec) {
429 $first_rec = 0;
430 } else {
431 $json_result_str .= ",";
432 }
433
434 my $metaname = $metatable_rec->{'metaname'};
435 $json_result_str .= "{\"metaname\":\"$metaname\",\"metavals\":[";
436
437 my $metapos = $metatable_rec->{'metapos'}; # 0... 1|all|undefined
438 if(!defined $metapos) {
439 $metapos = 0;
440 }
441
442
443 # Obtain the specified metadata value(s)
444 my $metavalue;
445
446 if($metapos ne "all") { # get the value at a single metapos
447
448 $metavalue = $self->get_metadata_from_archive_xml($gsdl_cgi, $doc_filename, $metaname, $metapos, $docid_secnum);
449 #print STDERR "**** Docname, metaname, metapos, sec_num: $doc_filename, $metaname, $metapos, $docid_secnum\n";
450
451 $json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}";
452
453 } else {
454 my $first_metaval = 1;
455 $metapos = 0;
456 $metavalue = $self->get_metadata_from_archive_xml($gsdl_cgi, $doc_filename, $metaname, $metapos, $docid_secnum);
457
458 while (defined $metavalue && $metavalue ne "") {
459 if($first_metaval) {
460 $first_metaval = 0;
461 } else {
462 $json_result_str .= ",";
463 }
464
465 $json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}";
466
467 $metapos++;
468 $metavalue = $self->get_metadata_from_archive_xml($gsdl_cgi, $doc_filename, $metaname, $metapos, $docid_secnum);
469 }
470 }
471
472 $json_result_str .= "]}"; # close metavals array and metatable record
473 }
474
475 $json_result_str .= "]}"; # close metatable array and docid record
476 }
477
478 $json_result_str .= "]"; # close array of docids
479 $gsdl_cgi->generate_ok_message($json_result_str."\n");
480}
481
482# takes a JSON string and returns a JSON string
483# resulting string is of the form:
484# [{"docid":"HASHc5bce2d6d3e5b04e470ec9","metatable":[{"metaname":"username","metavals":[{"metapos":"0","metavalue":"me"},{"metapos":"1","metavalue":"admin"}]},{"metaname":"usertimestamp","metavals":[{"metapos":"0","metavalue":"1367900586888"},{"metapos":"1","metavalue":"1367900616574"}]},{"metaname":"usercomment","metavals":[{"metapos":"0","metavalue":"Hi"},{"metapos":"1","metavalue":"Hello"}]}]}]
485sub _get_index_metadata_array
486{
487 my $self = shift @_;
488
489 my $collect = $self->{'collect'};
490 my $gsdl_cgi = $self->{'gsdl_cgi'};
491 my $site = $self->{'site'};
492 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
493
494 # look up additional args
495 my $infodbtype = $self->{'infodbtype'};
496
497 # To people who know $collect_tail please add some comments
498 # Obtain the path to the database
499 my $collect_tail = $collect;
500 $collect_tail =~ s/^.*[\/\\]//;
501 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
502 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
503
504 my $json_str = $self->{'json'};
505 my $doc_array = decode_json $json_str;
506
507 my $json_result_str = "[";
508 my $first_doc_rec = 1;
509
510 foreach my $doc_array_rec ( @$doc_array ) {
511
512 my $docid = $doc_array_rec->{'docid'};
513
514 if($first_doc_rec) {
515 $first_doc_rec = 0;
516 } else {
517 $json_result_str .= ",";
518 }
519 $json_result_str = $json_result_str . "{\"docid\":\"" . $docid . "\"";
520
521 my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
522 $json_result_str = $json_result_str . ",\"metatable\":[";
523
524 my $first_rec = 1;
525 foreach my $metatable_rec ( @$metatable ) { # the subarray metatable is an array of hashmaps
526 if($first_rec) {
527 $first_rec = 0;
528 } else {
529 $json_result_str .= ",";
530 }
531
532 my $metaname = $metatable_rec->{'metaname'};
533 $json_result_str .= "{\"metaname\":\"$metaname\",\"metavals\":[";
534
535 my $metapos = $metatable_rec->{'metapos'}; # 0... 1|all|undefined
536 if(!defined $metapos) {
537 $metapos = 0;
538 }
539
540 # Read the docid entry
541 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
542
543 # Basically loop through and unescape_html the values
544 foreach my $k (keys %$doc_rec) {
545 my @escaped_v = ();
546 foreach my $v (@{$doc_rec->{$k}}) {
547 my $ev = &ghtml::unescape_html($v);
548 push(@escaped_v, $ev);
549 }
550 $doc_rec->{$k} = \@escaped_v;
551 }
552
553 # Obtain the specified metadata value(s)
554 my $metavalue;
555
556 if($metapos ne "all") { # get the value at a single metapos
557
558 $metavalue = $doc_rec->{$metaname}->[$metapos];
559 $json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}";
560
561 } else {
562 my $first_metaval = 1;
563 $metapos = 0;
564 $metavalue = $doc_rec->{$metaname}->[$metapos];
565
566 while (defined $metavalue) {
567 if($first_metaval) {
568 $first_metaval = 0;
569 } else {
570 $json_result_str .= ",";
571 }
572
573 $json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}";
574
575 $metapos++;
576 $metavalue = $doc_rec->{$metaname}->[$metapos];
577 }
578 }
579
580 $json_result_str .= "]}"; # close metavals array and metatable record
581 }
582
583 $json_result_str .= "]}"; # close metatable array and docid record
584 }
585
586 $json_result_str .= "]"; # close array of docids
587
588 $gsdl_cgi->generate_ok_message($json_result_str."\n");
589}
590
591
592sub get_index_metadata
593{
594 my $self = shift @_;
595
596 my $username = $self->{'username'};
597 my $collect = $self->{'collect'};
598 my $gsdl_cgi = $self->{'gsdl_cgi'};
599 my $gsdlhome = $self->{'gsdlhome'};
600
601 # Obtain the collect dir
602 my $site = $self->{'site'};
603 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
604 ##my $collect_dir = &util::filename_cat($gsdlhome, "collect");
605
606 # look up additional args
607 my $docid = $self->{'d'};
608 my $metaname = $self->{'metaname'};
609 my $metapos = $self->{'metapos'};
610 my $infodbtype = $self->{'infodbtype'};
611
612 # To people who know $collect_tail please add some comments
613 # Obtain path to the database
614 my $collect_tail = $collect;
615 $collect_tail =~ s/^.*[\/\\]//;
616 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
617 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
618
619 # Read the docid entry
620 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
621
622 # Basically loop through and unescape_html the values
623 foreach my $k (keys %$doc_rec) {
624 my @escaped_v = ();
625 foreach my $v (@{$doc_rec->{$k}}) {
626 my $ev = &ghtml::unescape_html($v);
627 push(@escaped_v, $ev);
628 }
629 $doc_rec->{$k} = \@escaped_v;
630 }
631
632 # Obtain the specified metadata value
633 $metapos = 0 if (!defined $metapos);
634 my $metavalue = $doc_rec->{$metaname}->[$metapos];
635 $gsdl_cgi->generate_ok_message("$metavalue");
636
637}
638
639
640sub get_import_metadata
641{
642 my $self = shift @_;
643
644 my $username = $self->{'username'};
645 my $collect = $self->{'collect'};
646 my $gsdl_cgi = $self->{'gsdl_cgi'};
647 my $gsdlhome = $self->{'gsdlhome'};
648
649 # Obtain the collect dir
650 my $site = $self->{'site'};
651 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
652 ##my $collect_dir = &util::filename_cat($gsdlhome, "collect");
653
654 # look up additional args
655 my $docid = $self->{'d'};
656 my $metaname = $self->{'metaname'};
657 my $metapos = $self->{'metapos'};
658 $metapos = 0 if (!defined $metapos); # gets the first value by default since metapos defaults to 0
659
660 my $infodbtype = $self->{'infodbtype'};
661 if (!defined $docid)
662 {
663 $gsdl_cgi->generate_error("No docid (d=...) specified.\n");
664 }
665
666 # Obtain where the metadata.xml is from the archiveinfo-doc.gdb file
667 # If the doc oid is not specified, we assume the metadata.xml is next to the specified "f"
668 my $metadata_xml_file;
669 my $import_filename = undef;
670
671
672 my $archive_dir = &util::filename_cat($collect_dir, $collect, "archives");
673 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
674 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
675
676 # This now stores the full pathname
677 $import_filename = $doc_rec->{'src-file'}->[0];
678
679 # figure out correct metadata.xml file [?]
680 # Assuming the metadata.xml file is next to the source file
681 # Note: This will not work if it is using the inherited metadata from the parent folder
682 my ($import_tailname, $import_dirname) = File::Basename::fileparse($import_filename);
683 my $metadata_xml_filename = &util::filename_cat($import_dirname, "metadata.xml");
684
685 $gsdl_cgi->generate_ok_message($self->get_metadata_from_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $import_tailname));
686
687}
688
689sub get_metadata_from_metadata_xml
690{
691 my $self = shift @_;
692 my ($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $src_file) = @_;
693
694 my @rules =
695 (
696 _default => 'raw',
697 'Metadata' => \&gfmxml_metadata,
698 'FileName' => \&mxml_filename
699 );
700
701 my $parser = XML::Rules->new
702 (
703 rules => \@rules,
704 output_encoding => 'utf8'
705 );
706
707 my $xml_in = "";
708 if (!open(MIN,"<$metadata_xml_filename"))
709 {
710 $gsdl_cgi->generate_error("Unable to read in $metadata_xml_filename: $!");
711 }
712 else
713 {
714 # Read them in
715 my $line;
716 while (defined ($line=<MIN>)) {
717 $xml_in .= $line;
718 }
719 close(MIN);
720
721 $parser->parse($xml_in, {metaname => $metaname, metapos => $metapos, src_file => $src_file});
722
723 if(defined $parser->{'pad'}->{'metavalue'})
724 {
725 return $parser->{'pad'}->{'metavalue'};
726 }
727 else
728 {
729 return "";
730 }
731 }
732}
733
734sub gfmxml_metadata
735{
736 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
737
738 # no subsection support yet in metadata.xml
739
740 if (($parser->{'parameters'}->{'src_file'} eq $parser->{'parameters'}->{'current_file'}) && $parser->{'parameters'}->{'metaname'} eq $attrHash->{'name'})
741 {
742 if (!defined $parser->{'parameters'}->{'poscount'})
743 {
744 $parser->{'parameters'}->{'poscount'} = 0;
745 }
746 else
747 {
748 $parser->{'parameters'}->{'poscount'}++;
749 }
750
751 # gets the first value by default, since metapos defaults to 0
752 if (($parser->{'parameters'}->{'poscount'} == $parser->{'parameters'}->{'metapos'}))
753 {
754 if($parser->{'parameters'}->{'metapos'} > 0) {
755 print STDERR "@@@@ WARNING: non-zero metapos.\n";
756 print STDERR "@@@@ Assuming SIMPLE collection and proceeding to retrieve the import meta at position: ".$parser->{'parameters'}->{'metapos'}.".\n";
757 }
758 $parser->{'pad'}->{'metavalue'} = $attrHash->{'_content'};
759 }
760 }
761}
762
763sub get_archives_metadata
764{
765 my $self = shift @_;
766
767 my $username = $self->{'username'};
768 my $collect = $self->{'collect'};
769 my $gsdl_cgi = $self->{'gsdl_cgi'};
770# my $gsdlhome = $self->{'gsdlhome'};
771 my $infodbtype = $self->{'infodbtype'};
772
773 # Obtain the collect dir
774 my $site = $self->{'site'};
775 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
776
777 my $archive_dir = &util::filename_cat($collect_dir, $collect, "archives");
778
779 # look up additional args
780 my ($docid, $docid_secnum) = ($self->{'d'} =~ m/^(.*?)(\..*)?$/);
781 $docid_secnum = "" if (!defined $docid_secnum);
782
783 my $metaname = $self->{'metaname'};
784 my $metapos = $self->{'metapos'};
785 $metapos = 0 if (!defined $metapos);
786
787 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
788 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
789
790 # This now stores the full pathname
791 my $doc_filename = $doc_rec->{'doc-file'}->[0];
792
793 $gsdl_cgi->generate_ok_message($self->get_metadata_from_archive_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_filename), $metaname, $metapos, $docid_secnum));
794
795}
796
797sub get_metadata_from_archive_xml
798{
799 my $self = shift @_;
800 my ($gsdl_cgi, $doc_xml_filename, $metaname, $metapos, $secid) = @_;
801
802 my @start_rules = ('Section' => \&dxml_start_section);
803
804 my @rules =
805 (
806 _default => 'raw',
807 'Metadata' => \&gfdxml_metadata
808 );
809
810 my $parser = XML::Rules->new
811 (
812 start_rules => \@start_rules,
813 rules => \@rules,
814 output_encoding => 'utf8'
815 );
816
817 my $xml_in = "";
818 if (!open(MIN,"<$doc_xml_filename"))
819 {
820 $gsdl_cgi->generate_error("Unable to read in $doc_xml_filename: $!");
821 }
822 else
823 {
824 # Read them in
825 my $line;
826 while (defined ($line=<MIN>)) {
827 $xml_in .= $line;
828 }
829 close(MIN);
830
831 $parser->parse($xml_in, {metaname => $metaname, metapos => $metapos, secid => $secid});
832
833 if(defined $parser->{'pad'}->{'metavalue'})
834 {
835 return $parser->{'pad'}->{'metavalue'};
836 }
837 else
838 {
839 return "";
840 }
841 }
842}
843
844sub gfdxml_metadata
845{
846 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
847
848 if(!($parser->{'parameters'}->{'secid'} eq $parser->{'parameters'}->{'curr_section_num'}))
849 {
850 return;
851 }
852
853 if ($parser->{'parameters'}->{'metaname'} eq $attrHash->{'name'})
854 {
855 if (!defined $parser->{'parameters'}->{'poscount'})
856 {
857 $parser->{'parameters'}->{'poscount'} = 0;
858 }
859 else
860 {
861 $parser->{'parameters'}->{'poscount'}++;
862 }
863 }
864
865 if (($parser->{'parameters'}->{'metaname'} eq $attrHash->{'name'}) && ($parser->{'parameters'}->{'poscount'} == $parser->{'parameters'}->{'metapos'}))
866 {
867 $parser->{'pad'}->{'metavalue'} = $attrHash->{'_content'};
868 }
869}
870
871sub _set_live_metadata
872{
873 my $self = shift @_;
874
875 my $collect = $self->{'collect'};
876 my $gsdl_cgi = $self->{'gsdl_cgi'};
877 #my $gsdlhome = $self->{'gsdlhome'};
878 my $infodbtype = $self->{'infodbtype'};
879
880 # Obtain the collect dir
881 my $site = $self->{'site'};
882 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
883 ##my $collect_dir = &util::filename_cat($gsdlhome, "collect");
884
885
886 # look up additional args
887 my $docid = $self->{'d'};
888 if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
889 $gsdl_cgi->generate_error("No docid (d=...) specified."); # generates error and dies
890 }
891 my $metavalue = $self->{'metavalue'};
892
893 # Generate the dbkey
894 my $metaname = $self->{'metaname'};
895 my $dbkey = "$docid.$metaname";
896
897 # To people who know $collect_tail please add some comments
898 # Obtain path to the database
899 my $collect_tail = $collect;
900 $collect_tail =~ s/^.*[\/\\]//;
901 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
902 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
903
904 # Set the new value
905 my $cmd = "gdbmset \"$infodb_file_path\" \"$dbkey\" \"$metavalue\"";
906 my $status = system($cmd);
907 if ($status != 0) {
908 # Catch error if gdbmget failed
909 my $mess = "Failed to set metadata key: $dbkey\n";
910
911 $mess .= "PATH: $ENV{'PATH'}\n";
912 $mess .= "cmd = $cmd\n";
913 $mess .= "Exit status: $status\n";
914 $mess .= "System Error Message: $!\n";
915
916 $gsdl_cgi->generate_error($mess);
917 }
918 else {
919 $gsdl_cgi->generate_ok_message("set-live-metadata successful: Key[$metaname]=$metavalue");
920 }
921
922 #return $status; # in case calling functions have any further use for this
923}
924
925sub set_live_metadata
926{
927 my $self = shift @_;
928
929 my $username = $self->{'username'};
930 my $collect = $self->{'collect'};
931 my $gsdl_cgi = $self->{'gsdl_cgi'};
932
933 if ($baseaction::authentication_enabled) {
934 # Ensure the user is allowed to edit this collection
935 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
936 }
937
938 # Make sure the collection isn't locked by someone else
939 $self->lock_collection($username, $collect);
940
941 $self->_set_live_metadata(@_);
942
943 # Release the lock once it is done
944 $self->unlock_collection($username, $collect);
945}
946
947sub set_index_metadata_entry
948{
949 my $self = shift @_;
950 my ($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue,$metamode) = @_;
951
952 # To people who know $collect_tail please add some comments
953 # Obtain path to the database
954 my $collect_tail = $collect;
955 $collect_tail =~ s/^.*[\/\\]//;
956 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
957 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
958
959# print STDERR "**** infodb file path = $infodb_file_path\n";
960# print STDERR "***** infodb type = $infodbtype\n";
961
962 # Read the docid entry
963 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
964
965 # Set the metadata value
966 if (defined $metapos) {
967 # if metamode=accumulate AND metapos, warn user and then use metapos
968 if (defined $metamode && $metamode eq "accumulate") {
969 print STDERR "**** Warning: metamode is set to accumulate yet metapos is also provided for $docid\n";
970 print STDERR "**** Proceeding by using metapos\n";
971 }
972 $doc_rec->{$metaname}->[$metapos] = $metavalue;
973 }
974 elsif (defined $metamode && $metamode eq "override") {
975 $doc_rec->{$metaname} = [ $metavalue ];
976 }
977 else { # default for index was to override, but because accumulate is less destructive,
978 # and because accumulate is the default for archives and import, that's the new default for index too
979 if(defined $doc_rec->{$metaname}) {
980 push(@{$doc_rec->{$metaname}}, $metavalue); # accumulate the value for that metaname
981 } else {
982 $doc_rec->{$metaname} = [ $metavalue ];
983 }
984 }
985
986 my $status = &dbutil::set_infodb_entry($infodbtype, $infodb_file_path,$docid,$doc_rec);
987
988 return $status;
989
990}
991
992sub _set_import_metadata
993{
994 my $self = shift @_;
995
996 my $collect = $self->{'collect'};
997 my $gsdl_cgi = $self->{'gsdl_cgi'};
998 my $infodbtype = $self->{'infodbtype'};
999# my $gsdlhome = $self->{'gsdlhome'};
1000
1001 # Obtain the collect and archive dir
1002 my $site = $self->{'site'};
1003 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1004 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
1005 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
1006
1007 # look up additional args
1008 # want either d= or f=
1009 my $docid = $self->{'d'};
1010 my ($docid_root,$docid_secnum);
1011 if(defined $docid) {
1012 ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);
1013 # as yet no support for setting subsection metadata in metadata.xml
1014 if ((defined $docid_secnum) && ($docid_secnum !~ m/^\s*$/)) {
1015 $gsdl_cgi->generate_message("*** No support yet for setting import metadata at subsections level.\n");
1016 return;
1017 }
1018 }
1019
1020 my $import_file = $self->{'f'};
1021 if ((!defined $docid) && (!defined $import_file)) {
1022 $gsdl_cgi->generate_error("No docid (d=...) or import file (f=) specified.");
1023 }
1024
1025 # Get the parameters and set default mode to "accumulate"
1026 my $metaname = $self->{'metaname'};
1027 my $metavalue = $self->{'metavalue'};
1028## $metavalue =~ s/&amp;lt;(.*?)&amp;gt;/<$1>/g;
1029 $metavalue =~ s/&lt;(.*?)&gt;/<$1>/g;
1030
1031 my $metamode = $self->{'metamode'};
1032 if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
1033 # make "accumulate" the default (less destructive, as won't actually
1034 # delete any existing values)
1035 $metamode = "accumulate";
1036 }
1037
1038 # adding metapos and prevmetavalue support to import_metadata subroutines
1039 my $metapos = $self->{'metapos'}; # don't force undef to 0. Undef has meaning when metamode=override
1040 my $prevmetavalue = $self->{'prevmetavalue'};
1041
1042 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
1043 my $metadata_xml_filename = $self->set_import_metadata_entry($gsdl_cgi, $arcinfo_doc_filename, $infodbtype, $docid_root, $metaname, $metapos,$metavalue, $metamode,$prevmetavalue, $collect, $collect_dir); # at this point, docid_root = docid
1044
1045 my $mess = "set-import-metadata successful: Key[$docid] -> $metadata_xml_filename\n";
1046 $mess .= " $metaname";
1047 $mess .= " = $metavalue";
1048 $mess .= " ($metamode)\n";
1049
1050 $gsdl_cgi->generate_ok_message($mess);
1051
1052 #return $status; # in case calling functions have any further use for this
1053}
1054
1055# the version of set_index_meta that doesn't do authentication
1056sub _set_archives_metadata
1057{
1058 my $self = shift @_;
1059
1060 my $collect = $self->{'collect'};
1061 my $gsdl_cgi = $self->{'gsdl_cgi'};
1062 my $infodbtype = $self->{'infodbtype'};
1063
1064 # Obtain the collect and archive dir
1065 my $site = $self->{'site'};
1066 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1067 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
1068
1069 # look up additional args
1070 my $docid = $self->{'d'};
1071 my $metaname = $self->{'metaname'};
1072 my $metavalue = $self->{'metavalue'};
1073 my $prevmetavalue = $self->{'prevmetavalue'};
1074
1075 my $metapos = $self->{'metapos'}; # don't force undef to 0. Undef has meaning when metamode=override
1076 # Don't append "|| undef", since if metapos=0 it will then be set to undef
1077
1078 my $metamode = $self->{'metamode'};
1079 if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
1080 # make "accumulate" the default (less destructive, as won't actually
1081 # delete any existing values)
1082 $metamode = "accumulate";
1083 }
1084
1085 my $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid,
1086 $metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
1087
1088 if ($status == 0) {
1089 my $mess = "set-archives-metadata successful: Key[$docid]\n";
1090 $mess .= " $metaname";
1091 $mess .= "->[$metapos]" if (defined $metapos);
1092 $mess .= " = $metavalue";
1093 $mess .= " ($metamode)\n";
1094
1095 $gsdl_cgi->generate_ok_message($mess);
1096 }
1097 else {
1098 my $mess .= "Failed to set archives metadata key: $docid\n";
1099 $mess .= "Exit status: $status\n";
1100 if(defined $self->{'error_msg'}) {
1101 $mess .= "Error Message: $self->{'error_msg'}\n";
1102 } else {
1103 $mess .= "System Error Message: $!\n";
1104 }
1105 $mess .= "-" x 20 . "\n";
1106
1107 $gsdl_cgi->generate_error($mess);
1108 }
1109
1110 #return $status; # in case calling functions have any further use for this
1111}
1112
1113
1114# the version of set_index_meta that doesn't do authentication
1115sub _set_index_metadata
1116{
1117 my $self = shift @_;
1118
1119 my $collect = $self->{'collect'};
1120 my $gsdl_cgi = $self->{'gsdl_cgi'};
1121
1122 my $site = $self->{'site'};
1123 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1124
1125 # look up additional args
1126 my $docid = $self->{'d'};
1127 my $metaname = $self->{'metaname'};
1128 my $metapos = $self->{'metapos'}; # undef has meaning
1129 my $metavalue = $self->{'metavalue'};
1130 my $infodbtype = $self->{'infodbtype'};
1131 my $metamode = $self->{'metamode'};
1132
1133 my $status = $self->set_index_metadata_entry($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue,$metamode);
1134
1135 if ($status != 0) {
1136 # Catch error if set infodb entry failed
1137 my $mess = "Failed to set metadata key: $docid\n";
1138
1139 $mess .= "PATH: $ENV{'PATH'}\n";
1140 $mess .= "Exit status: $status\n";
1141 $mess .= "System Error Message: $!\n";
1142
1143 $gsdl_cgi->generate_error($mess);
1144 }
1145 else {
1146 my $mess = "set-index-metadata successful: Key[$docid]\n";
1147 $mess .= " $metaname";
1148 $mess .= "->[$metapos]" if (defined $metapos);
1149 $mess .= " = $metavalue\n";
1150
1151 $gsdl_cgi->generate_ok_message($mess);
1152 }
1153
1154 #return $status; # in case calling functions have any further use for this
1155}
1156
1157sub set_index_metadata
1158{
1159 my $self = shift @_;
1160
1161 my $username = $self->{'username'};
1162 my $collect = $self->{'collect'};
1163 my $gsdl_cgi = $self->{'gsdl_cgi'};
1164 #my $gsdlhome = $self->{'gsdlhome'};
1165
1166 if ($baseaction::authentication_enabled) {
1167 # Ensure the user is allowed to edit this collection
1168 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
1169 }
1170
1171 my $site = $self->{'site'};
1172 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1173
1174 $gsdl_cgi->checked_chdir($collect_dir);
1175
1176 # Obtain the collect dir
1177 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
1178
1179 # Make sure the collection isn't locked by someone else
1180 $self->lock_collection($username, $collect);
1181
1182 $self->_set_index_metadata(@_);
1183
1184 # Release the lock once it is done
1185 $self->unlock_collection($username, $collect);
1186}
1187
1188# call this to set the metadata for a combination of dirs archives, import or index, or live
1189# if none specified, defaults to index which was the original behaviour of set_metadata.
1190sub set_metadata
1191{
1192 my $self = shift @_;
1193
1194 # Testing that not defining a variable, setting it to "" or to " " all return false
1195 # >perl -e 'my $whichdirs=""; if($whichdirs) {print "$whichdirs\n"};'
1196
1197 my $where = $self->{'where'};
1198 if(!$where) {
1199 $self->set_index_metadata(@_); # call the full version of set_index_meta for the default behaviour
1200 return;
1201 }
1202
1203 # authenticate and lock collection once, even if processing multiple dirs
1204 my $username = $self->{'username'};
1205 my $collect = $self->{'collect'};
1206 my $gsdl_cgi = $self->{'gsdl_cgi'};
1207
1208 if ($baseaction::authentication_enabled) {
1209 # Ensure the user is allowed to edit this collection
1210 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
1211 }
1212
1213 if($where =~ m/index/) {
1214 my $site = $self->{'site'};
1215 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1216 $gsdl_cgi->checked_chdir($collect_dir);
1217 }
1218
1219 # Make sure the collection isn't locked by someone else
1220 $self->lock_collection($username, $collect);
1221
1222
1223 # now at last can set the metadata. $where can specify multiple
1224 # $where is of the form: import|archives|index, or a subset thereof
1225
1226 #my @whichdirs = split('\|', $where);
1227
1228 # just check whether $where contains import/archives/index/live in turn, and
1229 # for each case, process it accordingly
1230 if($where =~ m/import/) {
1231 $self->_set_import_metadata(@_);
1232 }
1233
1234 if($where =~ m/archives/) {
1235
1236 # look up docID arg which is optional to set_metadata because it's optional
1237 # to set_import, but which is compulsory to set_archives_metadata
1238 my $docid = $self->{'d'};
1239 if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
1240 $gsdl_cgi->generate_error("No docid (d=...) specified."); # generates error and dies
1241 }
1242 # we have a docid, so can set archives meta
1243 $self->_set_archives_metadata(@_);
1244 }
1245
1246 if($where =~ m/index/) {
1247
1248 # look up docID arg which is optional to set_metadata because it's optional
1249 # to set_import, but which is compulsory to set_archives_metadata
1250 my $docid = $self->{'d'};
1251 if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
1252 $gsdl_cgi->generate_error("No docid (d=...) specified.");
1253 }
1254 # we have a docid, so can set index meta
1255 $self->_set_index_metadata(@_);
1256 }
1257
1258 if($where =~ m/live/) {
1259 $self->_set_live_metadata(@_); # docid param, d, is compulsory, but is checked for in subroutine
1260 }
1261
1262 # Release the lock once it is done
1263 $self->unlock_collection($username, $collect);
1264}
1265
1266sub set_metadata_array
1267{
1268 my $self = shift @_;
1269
1270 my $where = $self->{'where'};
1271 if(!$where) {
1272 $self->set_index_metadata_array(@_); # default behaviour is the full version of set_index_meta_array
1273 return;
1274 }
1275
1276 my $username = $self->{'username'};
1277 my $collect = $self->{'collect'};
1278 my $gsdl_cgi = $self->{'gsdl_cgi'};
1279
1280 if ($baseaction::authentication_enabled) {
1281 # Ensure the user is allowed to edit this collection
1282 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
1283 }
1284
1285 # Not sure if the checked_chdir is necessary, since lock_collection also does a chdir
1286 # But including the stmt during this code reorganisation to preserve as-is what used to happen
1287 my $site = $self->{'site'};
1288 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1289 $gsdl_cgi->checked_chdir($collect_dir);
1290
1291 # Make sure the collection isn't locked by someone else
1292 $self->lock_collection($username, $collect);
1293
1294 if($where =~ m/import/) {
1295 $self->_set_import_metadata_array(@_);
1296 }
1297 if($where =~ m/archives/) {
1298 $self->_set_archives_metadata_array(@_);
1299 }
1300 if($where =~ m/index/) {
1301 $self->_set_index_metadata_array(@_);
1302 }
1303 if($where =~ m/live/) {
1304 $self->_set_live_metadata_array(@_);
1305 }
1306
1307 # Release the lock once it is done
1308 $self->unlock_collection($username, $collect);
1309}
1310
1311sub _set_index_metadata_array
1312{
1313 my $self = shift @_;
1314
1315 my $collect = $self->{'collect'};
1316 my $gsdl_cgi = $self->{'gsdl_cgi'};
1317 my $site = $self->{'site'};
1318 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1319
1320
1321 # look up additional args
1322
1323 my $infodbtype = $self->{'infodbtype'};
1324
1325 my $json_str = $self->{'json'};
1326 my $doc_array = decode_json $json_str;
1327
1328
1329 my $global_status = 0;
1330 my $global_mess = "";
1331
1332 my @all_docids = ();
1333
1334 foreach my $doc_array_rec ( @$doc_array ) {
1335
1336 my $status = -1;
1337 my $docid = $doc_array_rec->{'docid'};
1338
1339 push(@all_docids,$docid);
1340
1341 my $metaname = $doc_array_rec->{'metaname'};
1342 if(defined $metaname) {
1343 my $metapos = $doc_array_rec->{'metapos'}; # can legitimately be undef
1344 my $metavalue = $doc_array_rec->{'metavalue'};
1345 my $metamode = $doc_array_rec->{'metamode'} || $self->{'metamode'};
1346
1347 $status = $self->set_index_metadata_entry($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue,$metamode);
1348 } elsif (defined $doc_array_rec->{'metatable'}) { # if no metaname, we expect a metatable
1349 my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
1350
1351 foreach my $metatable_rec ( @$metatable ) { # the subarray metatable is an array of hashmaps
1352 $metaname = $metatable_rec->{'metaname'};
1353 my $metamode = $metatable_rec->{'metamode'} || $doc_array_rec->{'metamode'} || $self->{'metamode'};
1354 my $metapos = undef;
1355 my $metavals = $metatable_rec->{'metavals'}; # a sub-subarray
1356
1357 foreach my $metavalue ( @$metavals ) { # metavals is an array
1358 $status = $self->set_index_metadata_entry($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue,$metamode); # how do we use metamode in set_meta_entry?
1359 if($metamode eq "override") { # now, having overridden the metavalue for the first,
1360 # need to accumulate subsequent metavals for this metaname, else the just-assigned
1361 # metavalue for this metaname will be lost
1362 $metamode = "accumulate";
1363 }
1364 }
1365 }
1366 }
1367
1368 if ($status != 0) {
1369 # Catch error if set infodb entry failed
1370 $global_status = $status;
1371 $global_mess .= "Failed to set metadata key: $docid\n";
1372 $global_mess .= "Exit status: $status\n";
1373 $global_mess .= "System Error Message: $!\n";
1374 $global_mess .= "-" x 20;
1375 }
1376 }
1377
1378 if ($global_status != 0) {
1379 $global_mess .= "PATH: $ENV{'PATH'}\n";
1380 $gsdl_cgi->generate_error($global_mess);
1381 }
1382 else {
1383 my $mess = "set-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n";
1384 $gsdl_cgi->generate_ok_message($mess);
1385 }
1386}
1387
1388sub set_index_metadata_array
1389{
1390 my $self = shift @_;
1391
1392 my $username = $self->{'username'};
1393 my $collect = $self->{'collect'};
1394 my $gsdl_cgi = $self->{'gsdl_cgi'};
1395# my $gsdlhome = $self->{'gsdlhome'};
1396
1397 if ($baseaction::authentication_enabled) {
1398 # Ensure the user is allowed to edit this collection
1399 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
1400 }
1401
1402 my $site = $self->{'site'};
1403 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1404
1405 $gsdl_cgi->checked_chdir($collect_dir);
1406
1407 # Obtain the collect dir
1408 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
1409
1410 # Make sure the collection isn't locked by someone else
1411 $self->lock_collection($username, $collect);
1412
1413 $self->_set_index_metadata_array(@_);
1414
1415 # Release the lock once it is done
1416 $self->unlock_collection($username, $collect);
1417}
1418
1419# experimental, newly added in and untested
1420sub _set_live_metadata_array
1421{
1422 my $self = shift @_;
1423
1424 my $collect = $self->{'collect'};
1425 my $gsdl_cgi = $self->{'gsdl_cgi'};
1426
1427 my $site = $self->{'site'};
1428 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1429
1430
1431 # look up additional args
1432 my $infodbtype = $self->{'infodbtype'};
1433 # To people who know $collect_tail please add some comments
1434 # Obtain path to the database
1435 my $collect_tail = $collect;
1436 $collect_tail =~ s/^.*[\/\\]//;
1437 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
1438 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
1439
1440
1441 my $json_str = $self->{'json'};
1442 my $doc_array = decode_json $json_str;
1443
1444
1445 my $global_status = 0;
1446 my $global_mess = "";
1447
1448 my @all_docids = ();
1449
1450
1451 foreach my $doc_array_rec ( @$doc_array ) {
1452
1453 my $status = -1;
1454 my $docid = $doc_array_rec->{'docid'};
1455
1456 push(@all_docids,$docid);
1457
1458 my $metaname = $doc_array_rec->{'metaname'};
1459 if(defined $metaname) {
1460 my $dbkey = "$docid.$metaname";
1461 my $metavalue = $doc_array_rec->{'metavalue'};
1462
1463 # Set the new value
1464 my $cmd = "gdbmset \"$infodb_file_path\" \"$dbkey\" \"$metavalue\"";
1465 $status = system($cmd);
1466
1467 } elsif (defined $doc_array_rec->{'metatable'}) { # if no metaname, we expect a metatable
1468 my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
1469 foreach my $metatable_rec ( @$metatable ) {
1470 $metaname = $metatable_rec->{'metaname'};
1471 my $dbkey = "$docid.$metaname";
1472
1473 my $metavals = $metatable_rec->{'metavals'}; # a sub-subarray
1474 foreach my $metavalue ( @$metavals ) {
1475 my $cmd = "gdbmset \"$infodb_file_path\" \"$dbkey\" \"$metavalue\"";
1476 $status = system($cmd);
1477 }
1478 }
1479
1480 }
1481
1482 if ($status != 0) {
1483 # Catch error if gdbmget failed
1484 $global_status = $status;
1485 $global_mess .= "Failed to set metadata key: $docid\n"; # $dbkey
1486 $global_mess .= "Exit status: $status\n";
1487 $global_mess .= "System Error Message: $!\n";
1488 $global_mess .= "-" x 20;
1489 }
1490 }
1491
1492 if ($global_status != 0) {
1493 $global_mess .= "PATH: $ENV{'PATH'}\n";
1494 $gsdl_cgi->generate_error($global_mess);
1495 }
1496 else {
1497 my $mess = "set-live-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n";
1498 $gsdl_cgi->generate_ok_message($mess);
1499 }
1500}
1501
1502sub set_live_metadata_array
1503{
1504 my $self = shift @_;
1505
1506 my $username = $self->{'username'};
1507 my $collect = $self->{'collect'};
1508 my $gsdl_cgi = $self->{'gsdl_cgi'};
1509
1510 if ($baseaction::authentication_enabled) {
1511 # Ensure the user is allowed to edit this collection
1512 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
1513 }
1514
1515 my $site = $self->{'site'};
1516 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1517
1518 $gsdl_cgi->checked_chdir($collect_dir);
1519
1520 # Make sure the collection isn't locked by someone else
1521 $self->lock_collection($username, $collect);
1522
1523 $self->_set_live_metadata_array(@_);
1524
1525 # Release the lock once it is done
1526 $self->unlock_collection($username, $collect);
1527}
1528
1529
1530sub dxml_metadata
1531{
1532 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
1533 my $metaname = $parser->{'parameters'}->{'metaname'};
1534 my $metamode = $parser->{'parameters'}->{'metamode'};
1535
1536 print STDERR "**** Processing closing </Metadata> tag\n";
1537
1538 my $opt_doc_secnum = $parser->{'parameters'}->{'secnum'};
1539
1540 # Find the right metadata tag and checks if we are going to
1541 # override it
1542 #
1543 # Note: This over writes the first metadata block it
1544 # encountered. If there are multiple Sections in the doc.xml, it
1545 # might not behave as you would expect
1546
1547 my $curr_secnum = $parser->{'parameters'}->{'curr_section_num'};
1548## print STDERR "**** checking $opt_doc_secnum <=> $curr_secnum\n";
1549## print STDERR "**** metamode = $metamode\n";
1550
1551 if ((!defined $opt_doc_secnum) || ($opt_doc_secnum eq $curr_secnum))
1552 {
1553 my $name_attr = $attrHash->{'name'};
1554 if (($name_attr eq $metaname) && ($metamode eq "override"))
1555 {
1556 if (!defined $parser->{'parameters'}->{'poscount'})
1557 {
1558 $parser->{'parameters'}->{'poscount'} = 0;
1559 }
1560 else
1561 {
1562 $parser->{'parameters'}->{'poscount'}++;
1563 }
1564
1565 if(defined $parser->{'parameters'}->{'metapos'} && $parser->{'parameters'}->{'poscount'} == $parser->{'parameters'}->{'metapos'})
1566 {
1567 ##print STDERR "#### got match!!\n";
1568 # Get the value and override the current value
1569 my $metavalue = $parser->{'parameters'}->{'metavalue'};
1570 $attrHash->{'_content'} = $metavalue;
1571
1572 # Don't want it to wipe out any other pieces of metadata
1573 $parser->{'parameters'}->{'metamode'} = "done";
1574 }
1575 elsif(defined $parser->{'parameters'}->{'prevmetavalue'} && $parser->{'parameters'}->{'prevmetavalue'} eq $attrHash->{'_content'})
1576 {
1577 my $metavalue = $parser->{'parameters'}->{'metavalue'};
1578 $attrHash->{'_content'} = $metavalue;
1579 $parser->{'parameters'}->{'metamode'} = "done";
1580 }
1581 }
1582 }
1583
1584 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
1585 return [$tagname => $attrHash];
1586}
1587
1588# This method exists purely for catching invalid section numbers that the client
1589# requested to edit. Once the parser has reached the end (the final </Archive> tag),
1590# we've seen all the Sections in the doc.xml, and none of their section nums matched
1591# if the metamode has not been set to 'done' by then.
1592sub dxml_archive
1593{
1594 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
1595 my $metamode = $parser->{'parameters'}->{'metamode'};
1596
1597 my $curr_secnum = $parser->{'parameters'}->{'curr_section_num'};
1598 my $opt_doc_secnum = $parser->{'parameters'}->{'secnum'};
1599
1600# print STDERR "@@@ $tagname Processing a closing </Archive> tag [$curr_secnum|$opt_doc_secnum]\n";
1601
1602 if ($metamode ne "done" && $curr_secnum ne $opt_doc_secnum) {
1603 print STDERR "@@@ $tagname Finished processing FINAL Section.\n";
1604
1605 my $metaname = $parser->{'parameters'}->{'metaname'};
1606 my $metavalue = $parser->{'parameters'}->{'metavalue'};
1607
1608 print STDERR "@@@ Requested section number $opt_doc_secnum not found.\n";
1609 print STDERR "\t(last seen section number in document was $curr_secnum)\n";
1610 print STDERR "\tDiscarded metadata value '$metavalue' for meta '$metaname'\n";
1611 print STDERR "\tin section $opt_doc_secnum.\n";
1612 $parser->{'custom_err_msg'} = "Requested section number $opt_doc_secnum not found.";
1613 }
1614
1615 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
1616 return [$tagname => $attrHash];
1617}
1618
1619sub dxml_description
1620{
1621 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
1622 my $metamode = $parser->{'parameters'}->{'metamode'};
1623
1624 my $curr_secnum = $parser->{'parameters'}->{'curr_section_num'};
1625 my $opt_doc_secnum = $parser->{'parameters'}->{'secnum'} || "";
1626
1627 print STDERR "**** Processing a closing </Description> tag \n";
1628# print STDERR "@@@ $tagname Processing a closing </Description> tag [$curr_secnum|$opt_doc_secnum]\n";
1629
1630 # Accumulate the metadata
1631
1632 # We'll be accumulating metadata at this point if we haven't found and therefore
1633 # haven't processed the metadata yet.
1634 # For subsections, this means that if we're at a matching subsection, but haven't
1635 # found the correct metaname to override in that subsection, we accumulate it as new
1636 # meta in the subsection by adding it to the current description.
1637 # If there's no subsection info for the metadata, it will accumulate at the top level
1638 # section description if we hadn't found a matching metaname to override at this point.
1639
1640 # Both curr_secnum and opt_doc_secnum can be "". In the former case, it means we're now
1641 # at the toplevel section. In the latter case, it means we want to process meta in the
1642 # toplevel section. So the eq check between the values below will work in all cases.
1643
1644 # The only time this won't work is if an opt_doc_secnum beyond the section numbers of
1645 # this document has been provided. In that case, the metadata for that opt_doc_secnum
1646 # won't get attached/accumulated to any part of the doc, not even its top-level section.
1647
1648 if ($curr_secnum eq $opt_doc_secnum
1649 && ($metamode eq "accumulate" || $metamode eq "override")) {
1650 if ($metamode eq "override") {
1651 print "No metadata value to override. Switching 'metamode' to accumulate\n";
1652 }
1653
1654 # If we get to here and metamode is override, this means there
1655 # was no existing value to overide => treat as an append operation
1656
1657 # Tack a new metadata tag on to the end of the <Metadata>+ block
1658 my $metaname = $parser->{'parameters'}->{'metaname'};
1659 my $metavalue = $parser->{'parameters'}->{'metavalue'};
1660
1661 my $metadata_attr = {
1662 '_content' => $metavalue,
1663 'name' => $metaname,
1664 'mode' => "accumulate"
1665 };
1666
1667 my $append_metadata = [ "Metadata" => $metadata_attr ];
1668 my $description_content = $attrHash->{'_content'};
1669
1670 print "Appending metadata to doc.xml\n";
1671
1672 if (ref($description_content)) {
1673 # got some existing interesting nested content
1674 push(@$description_content, " ", $append_metadata ,"\n ");
1675 }
1676 else {
1677 #description_content is most likely a string such as "\n"
1678 $attrHash->{'_content'} = [$description_content, " ", $append_metadata ,"\n" ];
1679 }
1680
1681 $parser->{'parameters'}->{'metamode'} = "done";
1682 }
1683 else {
1684 # metamode most likely "done" signifying that it has already found a position to add the metadata to.
1685## print STDERR "**** NOT ACCUMULATE?!? \n";
1686 }
1687
1688 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
1689 return [$tagname => $attrHash];
1690}
1691
1692
1693sub dxml_start_section
1694{
1695 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
1696
1697 my $new_depth = scalar(@$contextArray);
1698
1699# print STDERR "**** START SECTION \n";
1700
1701 if ($new_depth == 1) {
1702 $parser->{'parameters'}->{'curr_section_depth'} = 1;
1703 $parser->{'parameters'}->{'curr_section_num'} = "";
1704 }
1705
1706 my $old_depth = $parser->{'parameters'}->{'curr_section_depth'};
1707 my $old_secnum = $parser->{'parameters'}->{'curr_section_num'};
1708
1709 my $new_secnum;
1710
1711 if ($new_depth > $old_depth) {
1712 # child subsection
1713 $new_secnum = "$old_secnum.1";
1714 }
1715 elsif ($new_depth == $old_depth) {
1716 # sibling section => increase it's value by 1
1717 my ($tail_num) = ($old_secnum =~ m/\.(\d+)$/);
1718 $tail_num++;
1719 $new_secnum = $old_secnum;
1720 $new_secnum =~ s/\.(\d+)$/\.$tail_num/;
1721 }
1722 else {
1723 # back up to parent section => lopp off tail
1724 $new_secnum = $old_secnum;
1725 $new_secnum =~ s/\.\d+$//;
1726 }
1727
1728 $parser->{'parameters'}->{'curr_section_depth'} = $new_depth;
1729 $parser->{'parameters'}->{'curr_section_num'} = $new_secnum;
1730
1731 1;
1732}
1733
1734sub edit_xml_file
1735{
1736 my $self = shift @_;
1737 my ($gsdl_cgi, $filename, $start_rules, $rules, $options) = @_;
1738
1739 # use XML::Rules to add it in (read in and out again)
1740 my $parser = XML::Rules->new(start_rules => $start_rules,
1741 rules => $rules,
1742 style => 'filter',
1743 output_encoding => 'utf8' );
1744
1745 my $xml_in = "";
1746 if (!open(MIN,"<$filename")) {
1747 $gsdl_cgi->generate_error("Unable to read in $filename: $!");
1748 }
1749 else {
1750 # Read all the text in
1751 my $line;
1752 while (defined ($line=<MIN>)) {
1753 $xml_in .= $line;
1754 }
1755 close(MIN);
1756
1757 my $MOUT;
1758 if (!open($MOUT,">$filename")) {
1759 $gsdl_cgi->generate_error("Unable to write out to $filename: $!");
1760 }
1761 else {
1762 # Matched lines will get handled by the call backs
1763## my $xml_out = "";
1764
1765 binmode($MOUT,":utf8");
1766 $parser->filter($xml_in,$MOUT, $options);
1767
1768# binmode(MOUT,":utf8");
1769# print MOUT $xml_out;
1770 close($MOUT);
1771 }
1772 }
1773
1774 # copy across any custom error information that was stored during parsing
1775 $self->{'error_msg'} = $parser->{'custom_err_msg'} if(defined $parser->{'custom_err_msg'});
1776}
1777
1778sub edit_doc_xml
1779{
1780 my $self = shift @_;
1781 my ($gsdl_cgi, $doc_xml_filename, $metaname, $metavalue, $metapos, $metamode, $opt_secnum, $prevmetavalue) = @_;
1782
1783 my $info_mess = <<RAWEND;
1784****************************
1785 edit_doc_xml()
1786****************************
1787doc_xml_filename = $doc_xml_filename
1788metaname = $metaname
1789metavalue = $metavalue
1790metapos = $metapos
1791metamode = $metamode
1792opt_secnum = $opt_secnum
1793prevmetavalue = $prevmetavalue
1794****************************
1795RAWEND
1796
1797 $gsdl_cgi->generate_message($info_mess);
1798
1799 # To monitor which section/subsection number we are in
1800 my @start_rules =
1801 ( 'Section' => \&dxml_start_section );
1802
1803 # use XML::Rules to add it in (read in and out again)
1804 # Set the call back functions
1805 my @rules =
1806 ( _default => 'raw',
1807 'Metadata' => \&dxml_metadata,
1808 'Description' => \&dxml_description,
1809 'Archive' => \&dxml_archive); # just for catching errors at end
1810
1811 # Sets the parameters
1812 my $options = { 'metaname' => $metaname,
1813 'metapos' => $metapos,
1814 'metavalue' => $metavalue,
1815 'metamode' => $metamode,
1816 'prevmetavalue' => $prevmetavalue };
1817
1818 if (defined $opt_secnum) {
1819 $options->{'secnum'} = $opt_secnum;
1820 }
1821
1822 $self->edit_xml_file($gsdl_cgi,$doc_xml_filename,\@start_rules,\@rules,$options);
1823}
1824
1825sub set_archives_metadata_entry
1826{
1827 my $self = shift @_;
1828 my ($gsdl_cgi, $archive_dir, $collect_dir, $collect, $infodbtype, $docid, $metaname, $metapos, $metavalue, $metamode, $prevmetavalue) = @_;
1829
1830 my $info_mess = <<RAWEND;
1831****************************
1832 set_archives_metadata_entry()
1833****************************
1834archive_dir = $archive_dir
1835collect_dir = $collect_dir
1836collect = $collect
1837infodbtype = $infodbtype
1838docid = $docid
1839metaname = $metaname
1840metapos = $metapos
1841metavalue = $metavalue
1842metamode = $metamode
1843prevmetavalue = $prevmetavalue
1844****************************
1845RAWEND
1846
1847 $gsdl_cgi->generate_message($info_mess);
1848
1849 # Obtain the doc.xml path for the specified docID
1850 my ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);
1851
1852 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
1853 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid_root);
1854 my $doc_xml_file = $doc_rec->{'doc-file'}->[0];
1855
1856 # The $doc_xml_file is relative to the archives, and now let's get the full path
1857 my $archives_dir = &util::filename_cat($collect_dir,$collect,"archives");
1858 my $doc_xml_filename = &util::filename_cat($archives_dir,$doc_xml_file);
1859
1860 # If we're overriding everything, then $metamode=override combined with $metapos=undefined
1861 # in which case, we need to remove all metavalues for the metaname at the given (sub)section
1862 # Thereafter, we will finally be setting the overriding metavalue for this metaname
1863 if(!defined $metapos && $metamode eq "override") {
1864 $self->remove_from_doc_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_xml_file), $metaname, $metapos, undef, $docid_secnum, $metamode);
1865 }
1866
1867 # Edit the doc.xml file with the specified metadata name, value and position.
1868 # TODO: there is a potential problem here as this edit_doc_xml function
1869 # is assuming the simple doc.xml situation where there is only one Section and no SubSections.
1870 # Running import.pl -groupsize will cause this to have multiple sections in one doc.xml
1871
1872 # dxml_metadata method ignores metapos if metamode anything other than override
1873 $self->edit_doc_xml($gsdl_cgi,$doc_xml_filename,
1874 $metaname,$metavalue,$metapos,$metamode,$docid_secnum,$prevmetavalue);
1875
1876 # return 0; # return 0 for now to indicate no error
1877 return (defined $self->{'error_msg'}) ? 1 : 0;
1878}
1879
1880
1881sub set_archives_metadata
1882{
1883 my $self = shift @_;
1884
1885 my $username = $self->{'username'};
1886 my $collect = $self->{'collect'};
1887 my $gsdl_cgi = $self->{'gsdl_cgi'};
1888
1889 if ($baseaction::authentication_enabled) {
1890 # Ensure the user is allowed to edit this collection
1891 $self->authenticate_user($username, $collect);
1892 }
1893
1894 # Make sure the collection isn't locked by someone else
1895 $self->lock_collection($username, $collect);
1896
1897 $self->_set_archives_metadata(@_);
1898
1899 # Release the lock once it is done
1900 $self->unlock_collection($username, $collect);
1901}
1902
1903sub _set_archives_metadata_array
1904{
1905 my $self = shift @_;
1906
1907 my $collect = $self->{'collect'};
1908 my $gsdl_cgi = $self->{'gsdl_cgi'};
1909 my $site = $self->{'site'};
1910 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
1911
1912 # look up additional args
1913
1914 my $infodbtype = $self->{'infodbtype'};
1915
1916 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
1917
1918 my $json_str = $self->{'json'};
1919 my $doc_array = decode_json $json_str;
1920
1921
1922 my $global_status = 0;
1923 my $global_mess = "";
1924
1925 my @all_docids = ();
1926
1927 foreach my $doc_array_rec ( @$doc_array ) {
1928 my $status = -1;
1929 my $docid = $doc_array_rec->{'docid'};
1930
1931 push(@all_docids,$docid);
1932
1933 my $metaname = $doc_array_rec->{'metaname'};
1934 if(defined $metaname) {
1935
1936 my $metapos = $doc_array_rec->{'metapos'}; # don't force undef to 0. Undef has meaning when metamode=override
1937
1938 my $metamode = $doc_array_rec->{'metamode'} || $self->{'metamode'};
1939 my $metavalue = $doc_array_rec->{'metavalue'};
1940 my $prevmetavalue = $self->{'prevmetavalue'}; # to make this sub behave as _set_archives_metadata
1941
1942
1943 if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
1944 # make "accumulate" the default (less destructive, as it won't actually
1945 # delete any existing values)
1946 $metamode = "accumulate";
1947 }
1948
1949 $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid,
1950 $metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
1951 } elsif (defined $doc_array_rec->{'metatable'}) { # if no metaname, we expect a metatable
1952 my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
1953
1954 foreach my $metatable_rec ( @$metatable ) {
1955 $metaname = $metatable_rec->{'metaname'};
1956 my $metamode = $metatable_rec->{'metamode'} || $doc_array_rec->{'metamode'} || $self->{'metamode'};
1957 my $metapos = undef;
1958 my $prevmetavalue = undef;
1959 my $metavals = $metatable_rec->{'metavals'}; # a sub-subarray
1960
1961 foreach my $metavalue ( @$metavals ) {
1962 $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect,$infodbtype,
1963 $docid,$metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
1964
1965 if($metamode eq "override") { # now, having overridden the metavalue for the first,
1966 # need to accumulate subsequent metavals for this metaname, else the just-assigned
1967 # metavalue for this metaname will be lost
1968 $metamode = "accumulate";
1969 }
1970 }
1971 }
1972 }
1973
1974 if ($status != 0) {
1975 # Catch error if set infodb entry failed
1976 $global_status = $status;
1977 $global_mess .= "Failed to set metadata key: $docid\n";
1978 $global_mess .= "Exit status: $status\n";
1979 $global_mess .= "System Error Message: $!\n";
1980 $global_mess .= "-" x 20 . "\n";
1981 }
1982 }
1983
1984 if ($global_status != 0) {
1985 $global_mess .= "PATH: $ENV{'PATH'}\n";
1986 $gsdl_cgi->generate_error($global_mess);
1987 }
1988 else {
1989 my $mess = "set-archives-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n";
1990 $gsdl_cgi->generate_ok_message($mess);
1991 }
1992}
1993
1994sub set_archives_metadata_array
1995{
1996 my $self = shift @_;
1997
1998 my $username = $self->{'username'};
1999 my $collect = $self->{'collect'};
2000 my $gsdl_cgi = $self->{'gsdl_cgi'};
2001# my $gsdlhome = $self->{'gsdlhome'};
2002
2003 if ($baseaction::authentication_enabled) {
2004 # Ensure the user is allowed to edit this collection
2005 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
2006 }
2007
2008 my $site = $self->{'site'};
2009 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
2010
2011 $gsdl_cgi->checked_chdir($collect_dir);
2012
2013 # Obtain the collect dir
2014 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
2015
2016 # Make sure the collection isn't locked by someone else
2017 $self->lock_collection($username, $collect);
2018
2019 $self->_set_archives_metadata_array(@_);
2020
2021 # Release the lock once it is done
2022 $self->unlock_collection($username, $collect);
2023}
2024
2025sub _remove_archives_metadata
2026{
2027 my $self = shift @_;
2028
2029 my $collect = $self->{'collect'};
2030 my $gsdl_cgi = $self->{'gsdl_cgi'};
2031# my $gsdlhome = $self->{'gsdlhome'};
2032 my $infodbtype = $self->{'infodbtype'};
2033
2034 my $site = $self->{'site'};
2035
2036 # Obtain the collect and archive dir
2037 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
2038
2039 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
2040
2041 # look up additional args
2042 my ($docid, $docid_secnum) = ($self->{'d'} =~ m/^(.*?)(\..*)?$/);
2043
2044 my $metaname = $self->{'metaname'};
2045 my $metapos = $self->{'metapos'};
2046
2047 my $metavalue = $self->{'metavalue'} || undef; # necessary to force fallback to undef here
2048
2049 # if the user hasn't told us what to delete, not having given a metavalue or metapos,
2050 # default to deleting the first metavalue for the given metaname
2051 # Beware that if both metapos AND metavalue are defined, both matches (if any)
2052 # seem to get deleted in one single remove_archives_meta action invocation.
2053 # Similarly, if 2 identical metavalues for a metaname exist and that metavalue is being
2054 # deleted, both get deleted.
2055 if(!defined $metapos && !defined $metavalue) {
2056 $metapos = 0;
2057 }
2058
2059 my $metamode = $self->{'metamode'} || undef;
2060
2061 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
2062 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
2063
2064 # This now stores the full pathname
2065 my $doc_filename = $doc_rec->{'doc-file'}->[0];
2066
2067 my $status = $self->remove_from_doc_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_filename), $metaname, $metapos, $metavalue, $docid_secnum, $metamode);
2068# my $status = $self->remove_from_doc_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_filename), $metaname, $metapos, undef, $docid_secnum);
2069
2070 if ($status == 0)
2071 {
2072 my $mess = "remove-archives-metadata successful: Key[$docid]\n";
2073 $mess .= " $metaname";
2074 $mess .= "->[$metapos]" if (defined $metapos);
2075
2076 $gsdl_cgi->generate_ok_message($mess);
2077 }
2078 else
2079 {
2080 my $mess .= "Failed to remove archives metadata key: $docid\n";
2081 $mess .= "Exit status: $status\n";
2082 $mess .= "System Error Message: $!\n";
2083 $mess .= "-" x 20 . "\n";
2084
2085 $gsdl_cgi->generate_error($mess);
2086 }
2087
2088 #return $status; # in case calling functions have a use for this
2089}
2090
2091sub remove_archives_metadata
2092{
2093 my $self = shift @_;
2094
2095 my $username = $self->{'username'};
2096 my $collect = $self->{'collect'};
2097 my $gsdl_cgi = $self->{'gsdl_cgi'};
2098
2099 if ($baseaction::authentication_enabled)
2100 {
2101 # Ensure the user is allowed to edit this collection
2102 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
2103 }
2104
2105 # Make sure the collection isn't locked by someone else
2106 $self->lock_collection($username, $collect);
2107
2108 $self->_remove_archives_metadata(@_);
2109
2110 # Release the lock once it is done
2111 $self->unlock_collection($username, $collect);
2112}
2113
2114sub remove_from_doc_xml
2115{
2116 my $self = shift @_;
2117 my ($gsdl_cgi, $doc_xml_filename, $metaname, $metapos, $metavalue, $secid, $metamode) = @_;
2118
2119 my @start_rules = ('Section' => \&dxml_start_section);
2120
2121 # Set the call-back functions for the metadata tags
2122 my @rules =
2123 (
2124 _default => 'raw',
2125 'Metadata' => \&rfdxml_metadata
2126 );
2127
2128 my $parser = XML::Rules->new
2129 (
2130 start_rules => \@start_rules,
2131 rules => \@rules,
2132 style => 'filter',
2133 output_encoding => 'utf8',
2134# normalisespaces => 1, # http://search.cpan.org/~jenda/XML-Rules-1.16/lib/XML/Rules.pm
2135 stripspaces => 2|0|0 # ineffectual
2136 );
2137
2138 my $status = 0;
2139 my $xml_in = "";
2140 if (!open(MIN,"<$doc_xml_filename"))
2141 {
2142 $gsdl_cgi->generate_error("Unable to read in $doc_xml_filename: $!");
2143 $status = 1;
2144 }
2145 else
2146 {
2147 # Read them in
2148 my $line;
2149 while (defined ($line=<MIN>)) {
2150 $xml_in .= $line;
2151 }
2152 close(MIN);
2153
2154 # Filter with the call-back functions
2155 my $xml_out = "";
2156
2157 my $MOUT;
2158 if (!open($MOUT,">$doc_xml_filename")) {
2159 $gsdl_cgi->generate_error("Unable to write out to $doc_xml_filename: $!");
2160 $status = 1;
2161 }
2162 else {
2163 binmode($MOUT,":utf8");
2164 $parser->filter($xml_in, $MOUT, {metaname => $metaname, metapos => $metapos, metavalue => $metavalue, secid => $secid, metamode => $metamode});
2165 close($MOUT);
2166 }
2167 }
2168 return $status;
2169}
2170
2171sub rfdxml_metadata
2172{
2173 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
2174
2175 # For comparisons, toplevel section is indicated by ""
2176 my $curr_sec_num = $parser->{'parameters'}->{'curr_section_num'} || "";
2177 my $secid = $parser->{'parameters'}->{'secid'} || "";
2178
2179 if (!($secid eq $curr_sec_num))
2180 {
2181 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
2182 return [$tagname => $attrHash];
2183 }
2184
2185 if ($parser->{'parameters'}->{'metaname'} eq $attrHash->{'name'})
2186 {
2187 if (!defined $parser->{'parameters'}->{'poscount'})
2188 {
2189 $parser->{'parameters'}->{'poscount'} = 0;
2190 }
2191 else
2192 {
2193 $parser->{'parameters'}->{'poscount'}++;
2194 }
2195
2196 # if overriding (for set-meta) but no metapos, then clear all the meta for this metaname
2197 if ((defined $parser->{'parameters'}->{'metamode'}) && ($parser->{'parameters'}->{'metamode'} eq "override") && (!defined $parser->{'parameters'}->{'metapos'})) {
2198 return [];
2199 }
2200 }
2201
2202 if ((defined $parser->{'parameters'}->{'metapos'}) && ($parser->{'parameters'}->{'metaname'} eq $attrHash->{'name'}) && ($parser->{'parameters'}->{'poscount'} == $parser->{'parameters'}->{'metapos'}))
2203 {
2204 return [];
2205 }
2206
2207 if ((defined $parser->{'parameters'}->{'metavalue'}) && ($parser->{'parameters'}->{'metaname'} eq $attrHash->{'name'}) && ($parser->{'parameters'}->{'metavalue'} eq $attrHash->{'_content'}))
2208 {
2209 return [];
2210 }
2211
2212 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
2213 return [$tagname => $attrHash];
2214}
2215
2216sub mxml_metadata
2217{
2218 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
2219 my $metaname = $parser->{'parameters'}->{'metaname'};
2220 my $metamode = $parser->{'parameters'}->{'metamode'};
2221
2222 # Report error if we don't see FileName tag before this
2223 die "Fatal Error: Unexpected metadata.xml structure. Undefined current_file, possibly encountered Description before FileName" if (!defined($parser->{'parameters'}->{'current_file'}));
2224
2225 # Don't do anything if we are not in the right FileSet
2226 my $file_regexp = $parser->{'parameters'}->{'current_file'};
2227 if ($file_regexp =~ /\.\*/) {
2228 # Only interested in a file_regexp if it specifies precisely one
2229 # file.
2230 # So, skip anything with a .* in it as it is too general
2231## print STDERR "@@@@ Skipping entry in metadata.xml where FileName=.* as it is too general\n";
2232 return [$tagname => $attrHash];
2233 }
2234 my $src_file = $parser->{'parameters'}->{'src_file'};
2235 if (!($src_file =~ /$file_regexp/)) {
2236 return [$tagname => $attrHash];
2237 }
2238## print STDERR "*** mxl metamode = $metamode\n";
2239
2240 # Find the right metadata tag and checks if we are going to override it
2241 my $name_attr = $attrHash->{'name'};
2242 if (($name_attr eq $metaname) && ($metamode eq "override")) {
2243
2244 # now metadata.xml functions need to keep track of metapos
2245 if (!defined $parser->{'parameters'}->{'poscount'})
2246 {
2247 $parser->{'parameters'}->{'poscount'} = 0;
2248 }
2249 else
2250 {
2251 $parser->{'parameters'}->{'poscount'}++;
2252 }
2253
2254 # If either the metapos or prevmetavalue is set,
2255 # get the value and override the current value
2256 my $metavalue = $parser->{'parameters'}->{'metavalue'};
2257
2258 if(defined $parser->{'parameters'}->{'prevmetavalue'} && $parser->{'parameters'}->{'prevmetavalue'} eq $attrHash->{'_content'})
2259 {
2260 $attrHash->{'_content'} = $metavalue;
2261
2262 ## print STDERR "**** overriding metadata.xml\n";
2263
2264 # Don't want it to wipe out any other pieces of metadata
2265 $parser->{'parameters'}->{'metamode'} = "done";
2266 }
2267 elsif(defined $parser->{'parameters'}->{'metapos'} && $parser->{'parameters'}->{'poscount'} == $parser->{'parameters'}->{'metapos'})
2268 {
2269 $attrHash->{'_content'} = $metavalue;
2270 $parser->{'parameters'}->{'metamode'} = "done";
2271 }
2272 }
2273
2274 # mxml_description will process the metadata if metadata is accumulate,
2275 # or if we haven't found the metadata to override
2276
2277 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
2278 return [$tagname => $attrHash];
2279}
2280
2281
2282sub mxml_description
2283{
2284 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
2285 my $metamode = $parser->{'parameters'}->{'metamode'};
2286
2287 # Failed... Report error if we don't see FileName tag before this
2288 die "Fatal Error: Unexpected metadata.xml structure. Undefind current_file, possiblely encountered Description before FileName" if (!defined($parser->{'parameters'}->{'current_file'}));
2289
2290 # Don't do anything if we are not in the right FileSet
2291 my $file_regexp = $parser->{'parameters'}->{'current_file'};
2292 if ($file_regexp =~ m/\.\*/) {
2293 # Only interested in a file_regexp if it specifies precisely one
2294 # file.
2295 # So, skip anything with a .* in it as it is too general
2296 return [$tagname => $attrHash];
2297 }
2298 my $src_file = $parser->{'parameters'}->{'src_file'};
2299
2300 if (!($src_file =~ m/$file_regexp/)) {
2301 return [$tagname => $attrHash];
2302 }
2303
2304 # Accumulate the metadata block to the end of the description block
2305 # Note: This adds metadata block to all description blocks, so if there are
2306 # multiple FileSets, it will add to all of them
2307 if (($metamode eq "accumulate") || ($metamode eq "override")) {
2308
2309 # if metamode was "override" but get to here then it failed to
2310 # find an item to override, in which case it should append its
2311 # value to the end, just like the "accumulate" mode
2312
2313 if ($metamode eq "override") {
2314 print "No metadata value to override. Switching 'metamode' to accumulate\n";
2315 }
2316
2317 # tack a new metadata tag on to the end of the <Metadata>+ block
2318 my $metaname = $parser->{'parameters'}->{'metaname'};
2319 my $metavalue = $parser->{'parameters'}->{'metavalue'};
2320
2321 my $metadata_attr = { '_content' => $metavalue,
2322 'name' => $metaname,
2323 'mode' => "accumulate" };
2324
2325 my $append_metadata = [ "Metadata" => $metadata_attr ];
2326 my $description_content = $attrHash->{'_content'};
2327
2328## print STDERR "*** appending to metadata.xml\n";
2329
2330 # append the new metadata element to the end of the current
2331 # content contained inside this tag
2332 if (ref($description_content) eq "") {
2333 # => string or numeric literal
2334 # this is caused by a <Description> block has no <Metadata> child elements
2335 # => set up an empty array in '_content'
2336 $attrHash->{'_content'} = [ "\n" ];
2337 $description_content = $attrHash->{'_content'};
2338 }
2339
2340 push(@$description_content," ", $append_metadata ,"\n ");
2341 $parser->{'parameters'}->{'metamode'} = "done";
2342 }
2343
2344 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
2345 return [$tagname => $attrHash];
2346}
2347
2348
2349sub mxml_filename
2350{
2351 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
2352
2353 # Store the filename of the Current Fileset
2354 # Note: According to http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd
2355 # FileName tag must come before Description tag
2356 $parser->{'parameters'}->{'current_file'} = $attrHash->{'_content'};
2357
2358 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
2359 return [$tagname => $attrHash];
2360}
2361
2362
2363sub mxml_fileset
2364{
2365 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
2366
2367 # Initilise the current_file
2368 # Note: According to http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd
2369 # FileName tag must come before Description tag
2370 $parser->{'parameters'}->{'current_file'} = "";
2371
2372 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
2373 return [$tagname => $attrHash];
2374}
2375
2376sub mxml_directorymetadata
2377{
2378 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
2379
2380 # if we haven't processed the metadata when we reach the end of metadata.xml
2381 # it's because there's no particular FileSet element whose FileName matched
2382 # In which case, add a new FileSet for this FileName
2383 my $metamode = $parser->{'parameters'}->{'metamode'};
2384 if($metamode ne "done") {
2385
2386 if ($metamode eq "override") {
2387 print "No metadata value to override. Switching 'metamode' to accumulate\n";
2388 }
2389
2390 # If we get to here and metamode is override, this means there
2391 # was no existing value to overide => treat as an append operation
2392
2393 # Create a new FileSet element and append to DirectoryMetadata
2394 # <FileSet>
2395 # <FileName>src_file</FileName>
2396 # <Description>
2397 # <Metadata mode="" name="">metavalue</Metadata>
2398 # </Description>
2399 # </FileSet>
2400 my $src_file = $parser->{'parameters'}->{'src_file'};
2401 my $metaname = $parser->{'parameters'}->{'metaname'};
2402 my $metavalue = $parser->{'parameters'}->{'metavalue'};
2403 my $metadata_attr = {
2404 '_content' => $metavalue,
2405 'name' => $metaname,
2406 'mode' => "accumulate"
2407 };
2408 my $append_metadata = [ "Metadata" => $metadata_attr ];
2409 my $description_attr->{'_content'} = [ "\n\t\t ", $append_metadata, "\n\t\t"];
2410 my $description_element = [ "Description" => $description_attr ];
2411
2412 #_content is not an attribute, it's special and holds the children of this element
2413 # including the textnode value embedded in this element if any.
2414 my $filename_attr = {'_content' => $src_file};
2415 my $filename_element = [ "FileName" => $filename_attr ];
2416
2417 my $fileset_attr = {};
2418 $fileset_attr->{'_content'} = [ "\n\t\t", $filename_element,"\n\t\t",$description_element ,"\n\t" ];
2419 my $fileset = [ "FileSet" => $fileset_attr ]; #my $fileset = [ "FileSet" => {} ];
2420
2421
2422 # get children of dirmeta, and push the new FileSet element onto it
2423 print "Appending metadata to metadata.xml\n";
2424 my $dirmeta_content = $attrHash->{'_content'};
2425 if (ref($dirmeta_content)) {
2426 # got some existing interesting nested content
2427 #push(@$dirmeta_content, " ", $fileset ,"\n ");
2428 push(@$dirmeta_content, "\t", $fileset ,"\n");
2429 }
2430 else {
2431 #description_content is most likely a string such as "\n"
2432 #$attrHash->{'_content'} = [$dirmeta_content, " ", $fileset ,"\n" ];
2433 $attrHash->{'_content'} = [$dirmeta_content, "\t", $fileset ,"\n" ];
2434 }
2435
2436 $parser->{'parameters'}->{'metamode'} = "done";
2437 }
2438 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
2439 return [$tagname => $attrHash];
2440}
2441
2442
2443sub edit_metadata_xml
2444{
2445 my $self = shift @_;
2446 my ($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $metavalue, $metamode, $src_file, $prevmetavalue) = @_;
2447
2448 # Set the call-back functions for the metadata tags
2449 my @rules =
2450 ( _default => 'raw',
2451 'FileName' => \&mxml_filename,
2452 'Metadata' => \&mxml_metadata,
2453 'Description' => \&mxml_description,
2454 'FileSet' => \&mxml_fileset,
2455 'DirectoryMetadata' => \&mxml_directorymetadata);
2456
2457 # use XML::Rules to add it in (read in and out again)
2458 my $parser = XML::Rules->new(rules => \@rules,
2459 style => 'filter',
2460 output_encoding => 'utf8',
2461 stripspaces => 2|0|0); # http://search.cpan.org/~jenda/XML-Rules-1.16/lib/XML/Rules.pm
2462
2463 if (!-e $metadata_xml_filename) {
2464
2465 if (open(MOUT,">$metadata_xml_filename")) {
2466
2467 my $src_file_re = &util::filename_to_regex($src_file);
2468 # shouldn't the following also be in the above utility routine??
2469 # $src_file_re =~ s/\./\\./g;
2470
2471 print MOUT "<?xml version=\"1.0\"?>\n";
2472 print MOUT "<DirectoryMetadata>\n";
2473 print MOUT " <FileSet>\n";
2474 print MOUT " <FileName>$src_file_re</FileName>\n";
2475 print MOUT " <Description>\n";
2476 print MOUT " </Description>\n";
2477 print MOUT " </FileSet>\n";
2478 print MOUT "</DirectoryMetadata>\n";
2479
2480 close(MOUT);
2481 }
2482 else {
2483 $gsdl_cgi->generate_error("Unable to create $metadata_xml_filename: $!");
2484 }
2485 }
2486
2487
2488 my $xml_in = "";
2489 if (!open(MIN,"<$metadata_xml_filename")) {
2490 $gsdl_cgi->generate_error("Unable to read in $metadata_xml_filename: $!");
2491 }
2492 else {
2493 # Read them in
2494 my $line;
2495 while (defined ($line=<MIN>)) {
2496 $xml_in .= $line;
2497 }
2498 close(MIN);
2499
2500 # Filter with the call-back functions
2501 my $xml_out = "";
2502
2503 my $MOUT;
2504 if (!open($MOUT,">$metadata_xml_filename")) {
2505 $gsdl_cgi->generate_error("Unable to write out to $metadata_xml_filename: $!");
2506 }
2507 else {
2508 binmode($MOUT,":utf8");
2509
2510 # Some wise person please find out how to keep the DTD and encode lines in after it gets filtered by this XML::Rules
2511 # At the moment, I will just hack it!
2512 #my $header_with_utf8_dtd = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
2513 #$header_with_utf8_dtd .= "<!DOCTYPE DirectoryMetadata SYSTEM \"http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd\">";
2514 #$xml_out =~ s/\<\?xml\sversion\=\"1.0\"\?\>/$header_with_utf8_dtd/;
2515 #print MOUT $xml_out;
2516
2517 $parser->filter($xml_in, $MOUT, { metaname => $metaname,
2518 metapos => $metapos,
2519 metavalue => $metavalue,
2520 metamode => $metamode,
2521 src_file => $src_file,
2522 prevmetavalue => $prevmetavalue,
2523 current_file => undef} );
2524 close($MOUT);
2525 }
2526 }
2527}
2528
2529
2530sub set_import_metadata
2531{
2532 my $self = shift @_;
2533
2534 my $username = $self->{'username'};
2535 my $collect = $self->{'collect'};
2536 my $gsdl_cgi = $self->{'gsdl_cgi'};
2537
2538 if ($baseaction::authentication_enabled) {
2539 # Ensure the user is allowed to edit this collection
2540 $self->authenticate_user($username, $collect);
2541 }
2542
2543 # Make sure the collection isn't locked by someone else
2544 $self->lock_collection($username, $collect);
2545
2546 $self->_set_import_metadata(@_);
2547
2548 # Release the lock once it is done
2549 $self->unlock_collection($username, $collect);
2550
2551}
2552
2553sub set_import_metadata_array
2554{
2555 my $self = shift @_;
2556
2557 my $username = $self->{'username'};
2558 my $collect = $self->{'collect'};
2559 my $gsdl_cgi = $self->{'gsdl_cgi'};
2560# my $gsdlhome = $self->{'gsdlhome'};
2561
2562 if ($baseaction::authentication_enabled) {
2563 # Ensure the user is allowed to edit this collection
2564 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
2565 }
2566
2567 my $site = $self->{'site'};
2568 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
2569
2570 $gsdl_cgi->checked_chdir($collect_dir);
2571
2572 # Make sure the collection isn't locked by someone else
2573 $self->lock_collection($username, $collect);
2574
2575 $self->_set_import_metadata_array(@_);
2576
2577 # Release the lock once it is done
2578 $self->unlock_collection($username, $collect);
2579
2580}
2581
2582
2583sub _set_import_metadata_array
2584{
2585 my $self = shift @_;
2586
2587 my $collect = $self->{'collect'};
2588 my $gsdl_cgi = $self->{'gsdl_cgi'};
2589
2590 my $site = $self->{'site'};
2591 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
2592
2593 # look up additional args
2594
2595 my $infodbtype = $self->{'infodbtype'};
2596
2597 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
2598 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
2599
2600 my $json_str = $self->{'json'};
2601 my $doc_array = decode_json $json_str;
2602
2603 my $global_status = 0;
2604 my $global_mess = "";
2605
2606 my @all_docids = ();
2607
2608 foreach my $doc_array_rec ( @$doc_array )
2609 {
2610 my $status = -1;
2611 my $docid = $doc_array_rec->{'docid'};
2612
2613 my ($docid_root,$docid_secnum);
2614 if(defined $docid) {
2615 ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);
2616 # as yet no support for setting subsection metadata in metadata.xml
2617 if ((defined $docid_secnum) && ($docid_secnum !~ m/^\s*$/)) {
2618 $gsdl_cgi->generate_message("*** docid: $docid. No support yet for setting import metadata at subsections level.\n");
2619 next; # skip this docid in for loop
2620 }
2621 }
2622
2623 push(@all_docids,$docid); # docid_root rather
2624
2625 my $metaname = $doc_array_rec->{'metaname'};
2626 if (defined $metaname) {
2627 my $metamode = $doc_array_rec->{'metamode'} || $self->{'metamode'};
2628 my $metavalue = $doc_array_rec->{'metavalue'};
2629 $metavalue =~ s/&lt;(.*?)&gt;/<$1>/g;
2630
2631 if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
2632 # make "accumulate" the default (less destructive, as won't actually
2633 # delete any existing values)
2634 $metamode = "accumulate";
2635 }
2636
2637 # adding metapos and prevmetavalue support to import_metadata subroutines
2638 my $metapos = $doc_array_rec->{'metapos'}; # don't force undef to 0. Undef has meaning when metamode=override
2639 my $prevmetavalue = $self->{'prevmetavalue'};
2640
2641 $self->set_import_metadata_entry($gsdl_cgi, $arcinfo_doc_filename, $infodbtype, $docid_root, $metaname, $metapos, $metavalue, $metamode, $prevmetavalue, $collect, $collect_dir); # at this point, docid_root = docid
2642
2643 } elsif (defined $doc_array_rec->{'metatable'}) { # if no metaname, we expect a metatable
2644 my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
2645
2646 foreach my $metatable_rec ( @$metatable ) {
2647 $metaname = $metatable_rec->{'metaname'};
2648 my $metamode = $metatable_rec->{'metamode'} || $doc_array_rec->{'metamode'} || $self->{'metamode'};
2649 if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
2650 # make "accumulate" the default (less destructive, as won't actually
2651 # delete any existing values)
2652 $metamode = "accumulate";
2653 }
2654
2655 # No support for metapos and prevmetavalue in the JSON metatable substructure
2656 my $metapos = undef;
2657 my $prevmetavalue = undef;
2658 my $metavals = $metatable_rec->{'metavals'}; # a sub-subarray
2659
2660 foreach my $metavalue ( @$metavals ) {
2661 $metavalue =~ s/&lt;(.*?)&gt;/<$1>/g;
2662
2663 $self->set_import_metadata_entry($gsdl_cgi, $arcinfo_doc_filename, $infodbtype, $docid_root, $metaname, $metapos, $metavalue, $metamode, $prevmetavalue, $collect, $collect_dir); # at this point, docid_root = docid
2664 if($metamode eq "override") { # now, having overridden the first metavalue of the metaname,
2665 # need to accumulate subsequent metavals for this metaname, else the just-assigned
2666 # metavalue for this metaname will be lost
2667 $metamode = "accumulate";
2668 }
2669 }
2670 }
2671 }
2672 }
2673
2674 # always a success message
2675 my $mess = "set-archives-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n";
2676 $gsdl_cgi->generate_ok_message($mess);
2677}
2678
2679# always returns true (1)
2680sub set_import_metadata_entry
2681{
2682 my $self = shift @_;
2683 my ($gsdl_cgi, $arcinfo_doc_filename, $infodbtype, $docid, $metaname, $metapos, $metavalue, $metamode, $prevmetavalue, $collect, $collect_dir) = @_;
2684
2685 my $info_mess = <<RAWEND;
2686****************************
2687 set_import_metadata_entry()
2688****************************
2689collect = $collect
2690collect_dir = $collect_dir
2691infodbtype = $infodbtype
2692arcinfo_doc_filename = $arcinfo_doc_filename
2693docid = $docid
2694metaname = $metaname
2695metapos = $metapos
2696metavalue = $metavalue
2697metamode = $metamode
2698prevmetavalue = $prevmetavalue
2699****************************
2700RAWEND
2701
2702 $gsdl_cgi->generate_message($info_mess);
2703
2704 # import works with metadata.xml which can have inherited metadata
2705 # so setting or removing at a metapos can have unintended effects for a COMPLEX collection
2706 # (a collection that has or can have inherited metadata). Metapos has expected behaviour for
2707 # a SIMPLE collection, which is one that doesn't have inherited metadata. Assume caller knows
2708 # what they're doing if they provide a metapos.
2709 if(defined $metapos) {
2710 print STDERR "@@@@ WARNING: metapos defined.\n";
2711 print STDERR "@@@@ Assuming SIMPLE collection and proceeding to modify the import meta at $metapos.\n";
2712 }
2713
2714 # Obtain where the metadata.xml is from the archiveinfo-doc.gdb file
2715 # If the doc oid is not specified, we assume the metadata.xml is next to the specified "f"
2716 my $metadata_xml_file;
2717 my $import_filename = undef;
2718
2719 if (defined $docid) {
2720 # my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
2721 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
2722
2723 # This now stores the full pathname
2724 $import_filename = $doc_rec->{'src-file'}->[0];
2725 } else { # only for set_import_meta, not the case when calling method is set_import_metadata_array
2726 # as the array version of the method doesn't support the -f parameter yet
2727 my $import_file = $self->{'f'};
2728 $import_filename = &util::filename_cat($collect_dir,$collect,$import_file);
2729 }
2730
2731 # figure out correct metadata.xml file [?]
2732 # Assuming the metadata.xml file is next to the source file
2733 # Note: This will not work if it is using the inherited metadata from the parent folder
2734 my ($import_tailname, $import_dirname) = File::Basename::fileparse($import_filename);
2735 my $metadata_xml_filename = &util::filename_cat($import_dirname,"metadata.xml");
2736
2737 # If we're overriding everything, then $metamode=override combined with $metapos=undefined
2738 # in which case, we need to remove all metavalues for the metaname at the given (sub)section
2739 # Thereafter, we will finally be able to set the overriding metavalue for this metaname
2740 if(!defined $metapos && $metamode eq "override") {
2741## print STDERR "@@@ REMOVING all import metadata for $metaname\n";
2742 $self->remove_from_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, undef, $import_tailname, $metamode); # we're removing all values, so metavalue=undef
2743
2744 }
2745
2746 # Edit the metadata.xml
2747 # Modified by Jeffrey from DL Consulting
2748 # Handle the case where there is one metadata.xml file for multiple FileSets
2749 # The XML filter needs to know whether it is in the right FileSet
2750 # TODO: This doesn't fix the problem where the metadata.xml is not next to the src file.
2751 # TODO: This doesn't handle the common metadata (where FileName doesn't point to a single file)
2752 $self->edit_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname,
2753 $metapos, $metavalue, $metamode, $import_tailname, $prevmetavalue);
2754 #return 0;
2755 return $metadata_xml_filename;
2756}
2757
2758sub _remove_import_metadata
2759{
2760 my $self = shift @_;
2761
2762 my $collect = $self->{'collect'};
2763 my $gsdl_cgi = $self->{'gsdl_cgi'};
2764# my $gsdlhome = $self->{'gsdlhome'};
2765 my $infodbtype = $self->{'infodbtype'};
2766
2767 # Obtain the collect dir
2768 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
2769 my $site = $self->{'site'};
2770 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
2771
2772 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
2773 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
2774
2775 # look up additional args
2776 my $docid = $self->{'d'};
2777 if ((!defined $docid) || ($docid =~ m/^\s*$/))
2778 {
2779 $gsdl_cgi->generate_error("No docid (d=...) specified.\n");
2780 }
2781
2782 my $metaname = $self->{'metaname'};
2783 my $metapos = $self->{'metapos'};
2784 my $metavalue = $self->{'metavalue'};
2785 if(defined $metavalue) {
2786 $metavalue =~ s/&lt;(.*?)&gt;/<$1>/g;
2787 } elsif (!defined $metapos) { # if given no metavalue or metapos to delete, default to deleting the 1st
2788 $metapos = 0;
2789 }
2790 my $metamode = $self->{'metamode'} || undef;
2791
2792 # import works with metadata.xml which can have inherited metadata
2793 # so setting or removing at a metapos can have unintended effects for a COMPLEX collection
2794 # (a collection that has or can have inherited metadata). Metapos has expected behaviour for
2795 # a SIMPLE collection, which is one that doesn't have inherited metadata. Assume caller knows
2796 # what they're doing if they provide a metapos.
2797 if(defined $metapos) {
2798 print STDERR "@@@@ WARNING: metapos defined.\n";
2799 print STDERR "@@@@ Assuming SIMPLE collection and proceeding to modify the import meta at $metapos.\n";
2800 }
2801
2802 # Obtain where the metadata.xml is from the archiveinfo-doc.gdb file
2803 # If the doc oid is not specified, we assume the metadata.xml is next to the specified "f"
2804 my $metadata_xml_file;
2805 my $import_filename = undef;
2806 if (defined $docid)
2807 {
2808 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
2809 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
2810
2811 # This now stores the full pathname
2812 $import_filename = $doc_rec->{'src-file'}->[0];
2813 }
2814
2815 if((!defined $import_filename) || ($import_filename =~ m/^\s*$/))
2816 {
2817 $gsdl_cgi->generate_error("There is no metadata\n");
2818 }
2819
2820 # figure out correct metadata.xml file [?]
2821 # Assuming the metadata.xml file is next to the source file
2822 # Note: This will not work if it is using the inherited metadata from the parent folder
2823 my ($import_tailname, $import_dirname) = File::Basename::fileparse($import_filename);
2824 my $metadata_xml_filename = &util::filename_cat($import_dirname,"metadata.xml");
2825
2826 $self->remove_from_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $metavalue, $import_tailname, $metamode); # metamode has no meaning for removing meta, but is used by set_meta when overriding All
2827
2828 my $mess = "remove-import-metadata successful: Key[$docid] -> $metadata_xml_filename\n";
2829 $mess .= " $metaname";
2830 $mess .= " = $metavalue\n";
2831
2832 $gsdl_cgi->generate_ok_message($mess);
2833
2834 #return $status; # in case calling functions have a use for this
2835}
2836
2837sub remove_import_metadata
2838{
2839 my $self = shift @_;
2840
2841 my $username = $self->{'username'};
2842 my $collect = $self->{'collect'};
2843 my $gsdl_cgi = $self->{'gsdl_cgi'};
2844
2845 if ($baseaction::authentication_enabled) {
2846 # Ensure the user is allowed to edit this collection
2847 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
2848 }
2849
2850 # Make sure the collection isn't locked by someone else
2851 $self->lock_collection($username, $collect);
2852
2853 $self->_remove_import_metadata(@_);
2854
2855 # Release the lock once it is done
2856 $self->unlock_collection($username, $collect);
2857
2858}
2859
2860sub remove_from_metadata_xml
2861{
2862 my $self = shift @_;
2863 my ($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $metavalue, $src_file, $metamode) = @_;
2864 # metamode generally has no meaning for removing meta, but is used by set_meta
2865 # when overriding all metavals for a metaname, in which case remove_meta is called with metamode
2866
2867 # Set the call-back functions for the metadata tags
2868 my @rules =
2869 (
2870 _default => 'raw',
2871 'Metadata' => \&rfmxml_metadata,
2872 'FileName' => \&mxml_filename
2873 );
2874
2875 my $parser = XML::Rules->new
2876 (
2877 rules => \@rules,
2878 style => 'filter',
2879 output_encoding => 'utf8',
2880 #normalisespaces => 1,
2881 stripspaces => 2|0|0 # ineffectual
2882 );
2883
2884 my $xml_in = "";
2885 if (!open(MIN,"<$metadata_xml_filename"))
2886 {
2887 $gsdl_cgi->generate_error("Unable to read in $metadata_xml_filename: $!");
2888 }
2889 else
2890 {
2891 # Read them in
2892 my $line;
2893 while (defined ($line=<MIN>)) {
2894 $xml_in .= $line;
2895 }
2896 close(MIN);
2897
2898 # Filter with the call-back functions
2899 my $xml_out = "";
2900
2901 my $MOUT;
2902 if (!open($MOUT,">$metadata_xml_filename")) {
2903 $gsdl_cgi->generate_error("Unable to write out to $metadata_xml_filename: $!");
2904 }
2905 else {
2906 binmode($MOUT,":utf8");
2907 $parser->filter($xml_in, $MOUT, {metaname => $metaname, metapos => $metapos, metavalue => $metavalue, src_file => $src_file, metamode => $metamode, current_file => undef});
2908 close($MOUT);
2909 }
2910 }
2911}
2912
2913sub rfmxml_metadata
2914{
2915 my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
2916
2917 # metadata.xml does not handle subsections
2918
2919 # since metadata.xml now has to deal with metapos, we keep track of the metadata position
2920 if (($parser->{'parameters'}->{'src_file'} eq $parser->{'parameters'}->{'current_file'})
2921 && $parser->{'parameters'}->{'metaname'} eq $attrHash->{'name'})
2922 {
2923 if (!defined $parser->{'parameters'}->{'poscount'})
2924 {
2925 $parser->{'parameters'}->{'poscount'} = 0;
2926 }
2927 else
2928 {
2929 $parser->{'parameters'}->{'poscount'}++;
2930 }
2931
2932 # if overriding but no metapos, then clear all the meta for this metaname
2933 if ((defined $parser->{'parameters'}->{'metamode'}) && ($parser->{'parameters'}->{'metamode'} eq "override") && (!defined $parser->{'parameters'}->{'metapos'})) {
2934 return [];
2935 }
2936
2937 if ((defined $parser->{'parameters'}->{'metapos'}) && ($parser->{'parameters'}->{'poscount'} == $parser->{'parameters'}->{'metapos'}))
2938 {
2939 return [];
2940 }
2941
2942 if ((defined $parser->{'parameters'}->{'metavalue'}) && ($attrHash->{'_content'} eq $parser->{'parameters'}->{'metavalue'}))
2943 {
2944 return [];
2945 }
2946 }
2947
2948 # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
2949 return [$tagname => $attrHash];
2950}
2951
2952sub _remove_live_metadata
2953{
2954 my $self = shift @_;
2955
2956 my $collect = $self->{'collect'};
2957 my $gsdl_cgi = $self->{'gsdl_cgi'};
2958# my $gsdlhome = $self->{'gsdlhome'};
2959 my $infodbtype = $self->{'infodbtype'};
2960
2961 # Obtain the collect dir
2962 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
2963 my $site = $self->{'site'};
2964 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
2965
2966
2967 # look up additional args
2968 my $docid = $self->{'d'};
2969 if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
2970 $gsdl_cgi->generate_error("No docid (d=...) specified.");
2971 }
2972
2973 # Generate the dbkey
2974 my $metaname = $self->{'metaname'};
2975 my $dbkey = "$docid.$metaname";
2976
2977 # To people who know $collect_tail please add some comments
2978 # Obtain the live gdbm_db path
2979 my $collect_tail = $collect;
2980 $collect_tail =~ s/^.*[\/\\]//;
2981 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
2982 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
2983
2984 # Remove the key
2985 my $cmd = "gdbmdel \"$infodb_file_path\" \"$dbkey\"";
2986 my $status = system($cmd);
2987 if ($status != 0) {
2988 # Catch error if gdbmdel failed
2989 my $mess = "Failed to set metadata key: $dbkey\n";
2990
2991 $mess .= "PATH: $ENV{'PATH'}\n";
2992 $mess .= "cmd = $cmd\n";
2993 $mess .= "Exit status: $status\n";
2994 $mess .= "System Error Message: $!\n";
2995
2996 $gsdl_cgi->generate_error($mess);
2997 }
2998 else {
2999 $gsdl_cgi->generate_ok_message("DB remove successful: Key[$metaname]");
3000 }
3001
3002}
3003
3004sub remove_live_metadata
3005{
3006 my $self = shift @_;
3007
3008 my $username = $self->{'username'};
3009 my $collect = $self->{'collect'};
3010 my $gsdl_cgi = $self->{'gsdl_cgi'};
3011 my $gsdlhome = $self->{'gsdlhome'};
3012
3013 if ($baseaction::authentication_enabled) {
3014 # Ensure the user is allowed to edit this collection
3015 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
3016 }
3017
3018 # Make sure the collection isn't locked by someone else
3019 $self->lock_collection($username, $collect);
3020
3021 $self->_remove_live_metadata(@_);
3022
3023 $self->unlock_collection($username, $collect);
3024}
3025
3026sub remove_metadata
3027{
3028 my $self = shift @_;
3029
3030 my $where = $self->{'where'};
3031 if(!$where) {
3032 $self->remove_index_metadata(@_); # call the full version of set_index_meta for the default behaviour
3033 return;
3034 }
3035
3036 my $username = $self->{'username'};
3037 my $collect = $self->{'collect'};
3038 my $gsdl_cgi = $self->{'gsdl_cgi'};
3039
3040 if ($baseaction::authentication_enabled) {
3041 # Ensure the user is allowed to edit this collection
3042 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
3043 }
3044
3045 # Make sure the collection isn't locked by someone else
3046 $self->lock_collection($username, $collect);
3047
3048 # check which directories need to be processed, specified in $where as
3049 # any combination of import|archives|index|live
3050 if($where =~ m/import/) {
3051 $self->_remove_import_metadata(@_);
3052 }
3053 if($where =~ m/archives/) {
3054 $self->_remove_archives_metadata(@_);
3055 }
3056 if($where =~ m/index/) {
3057 $self->_remove_index_metadata(@_);
3058 }
3059
3060 # Release the lock once it is done
3061 $self->unlock_collection($username, $collect);
3062}
3063
3064# the internal version, without authentication
3065sub _remove_index_metadata
3066{
3067 my $self = shift @_;
3068
3069 my $collect = $self->{'collect'};
3070 my $gsdl_cgi = $self->{'gsdl_cgi'};
3071# my $gsdlhome = $self->{'gsdlhome'};
3072 my $infodbtype = $self->{'infodbtype'};
3073
3074 # Obtain the collect dir
3075 my $site = $self->{'site'};
3076 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
3077 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
3078
3079 # look up additional args
3080 my $docid = $self->{'d'};
3081 if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
3082 $gsdl_cgi->generate_error("No docid (d=...) specified.");
3083 }
3084 my $metaname = $self->{'metaname'};
3085 my $metapos = $self->{'metapos'};
3086 my $metavalue = $self->{'metavalue'} || undef; # necessary to force fallback to undef here
3087
3088 # To people who know $collect_tail please add some comments
3089 # Obtain the path to the database
3090 my $collect_tail = $collect;
3091 $collect_tail =~ s/^.*[\/\\]//;
3092 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
3093 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
3094
3095 # Read the docid entry
3096 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
3097
3098 # Check to make sure the key does exist
3099 if (!defined ($doc_rec->{$metaname})) {
3100 $gsdl_cgi->generate_error("No metadata field \"" . $metaname . "\" in the specified document: [" . $docid . "]");
3101 }
3102
3103 # Obtain the specified metadata pos
3104 # if no metavalue or metapos to delete, default to deleting the 1st value for the metaname
3105 if(!defined $metapos && !defined $metavalue) {
3106 $metapos = 0;
3107 }
3108
3109
3110 # consider check key is defined before deleting?
3111 # Loop through the metadata array and ignore the specified position
3112 my $filtered_metadata = [];
3113 my $num_metadata_vals = scalar(@{$doc_rec->{$metaname}});
3114 for (my $i=0; $i<$num_metadata_vals; $i++) {
3115 my $metaval = shift(@{$doc_rec->{$metaname}});
3116
3117 if (!defined $metavalue && $i != $metapos) {
3118 push(@$filtered_metadata,$metaval);
3119 }
3120
3121 if(defined $metavalue && !($metavalue eq $metaval))
3122 {
3123 push(@$filtered_metadata,$metaval);
3124 }
3125 }
3126 $doc_rec->{$metaname} = $filtered_metadata;
3127
3128 ## Use the dbutil set_entry method instead of assuming the database is gdbm
3129 my $status = &dbutil::set_infodb_entry($infodbtype, $infodb_file_path, $docid, $doc_rec);
3130
3131 if ($status != 0) {
3132 my $mess = "Failed to set metadata key: $docid\n";
3133
3134 $mess .= "PATH: $ENV{'PATH'}\n";
3135 $mess .= "Exit status: $status\n";
3136 $mess .= "System Error Message: $!\n";
3137
3138 $gsdl_cgi->generate_error($mess);
3139 }
3140 else {
3141 my $mess = "DB set (with item deleted) successful: Key[$docid]\n";
3142 $mess .= " $metaname";
3143 $mess .= "->[$metapos]" if (defined $metapos);
3144
3145 $gsdl_cgi->generate_ok_message($mess);
3146 }
3147
3148 #return $status; # in case calling functions have a use for this
3149}
3150
3151sub remove_index_metadata
3152{
3153 my $self = shift @_;
3154
3155 my $username = $self->{'username'};
3156 my $collect = $self->{'collect'};
3157 my $gsdl_cgi = $self->{'gsdl_cgi'};
3158# my $gsdlhome = $self->{'gsdlhome'};
3159
3160 if ($baseaction::authentication_enabled) {
3161 # Ensure the user is allowed to edit this collection
3162 $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
3163 }
3164
3165 # Obtain the collect dir
3166 my $site = $self->{'site'};
3167 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
3168 ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
3169
3170 # Make sure the collection isn't locked by someone else
3171 $self->lock_collection($username, $collect);
3172
3173 $self->_remove_index_metadata(@_);
3174
3175 # Release the lock once it is done
3176 $self->unlock_collection($username, $collect);
3177}
3178
3179
3180# Was trying to reused the codes, but the functions need to be broken
3181# down more before they can be reused, otherwise there will be too
3182# much overhead and duplicate process...
3183sub insert_metadata
3184{
3185 my $self = shift @_;
3186
3187 my $username = $self->{'username'};
3188 my $collect = $self->{'collect'};
3189 my $gsdl_cgi = $self->{'gsdl_cgi'};
3190 my $gsdlhome = $self->{'gsdlhome'};
3191 my $infodbtype = $self->{'infodbtype'};
3192
3193 # If the import metadata and gdbm database have been updated, we
3194 # need to insert some notification to warn user that the the text
3195 # they see at the moment is not indexed and require a rebuild.
3196 my $rebuild_pending_macro = "_rebuildpendingmessage_";
3197
3198 if ($baseaction::authentication_enabled) {
3199 # Ensure the user is allowed to edit this collection
3200 $self->authenticate_user($username, $collect);
3201 }
3202
3203 # Obtain the collect and archive dir
3204 my $site = $self->{'site'};
3205 my $collect_dir = $gsdl_cgi->get_collection_dir($site);
3206 ##my $collect_dir = &util::filename_cat($gsdlhome, "collect");
3207 my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
3208
3209 # Make sure the collection isn't locked by someone else
3210 $self->lock_collection($username, $collect);
3211
3212 # Check additional args
3213 my $docid = $self->{'d'};
3214 if (!defined($docid)) {
3215 $gsdl_cgi->generate_error("No document id is specified: d=...");
3216 }
3217 my $metaname = $self->{'metaname'};
3218 if (!defined($metaname)) {
3219 $gsdl_cgi->generate_error("No metaname is specified: metadataname=...");
3220 }
3221 my $metavalue = $self->{'metavalue'};
3222 if (!defined($metavalue) || $metavalue eq "") {
3223 $gsdl_cgi->generate_error("No metavalue or empty metavalue is specified: metadataname=...");
3224 }
3225 # make "accumulate" the default (less destructive, as won't actually
3226 # delete any existing values)
3227 my $metamode = "accumulate";
3228
3229 # metapos/prevmetavalue were never before used in this subroutine, so set them to undefined
3230 my $metapos = undef;
3231 my $prevmetavalue = undef;
3232
3233 #=======================================================================#
3234 # set_import_metadata [START]
3235 #=======================================================================#
3236 # Obtain where the metadata.xml is from the archiveinfo-doc.gdb file
3237 # If the doc oid is not specified, we assume the metadata.xml is next to the specified "f"
3238 my $metadata_xml_file;
3239 my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
3240 my $archive_doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
3241
3242 # This now stores the full pathname
3243 my $import_filename = $archive_doc_rec->{'src-file'}->[0];
3244
3245 # figure out correct metadata.xml file [?]
3246 # Assuming the metadata.xml file is next to the source file
3247 # Note: This will not work if it is using the inherited metadata from the parent folder
3248 my ($import_tailname, $import_dirname)
3249 = File::Basename::fileparse($import_filename);
3250 my $metadata_xml_filename = &util::filename_cat($import_dirname,"metadata.xml");
3251
3252 # Shane's escape characters
3253 $metavalue = pack "U0C*", unpack "C*", $metavalue;
3254 $metavalue =~ s/\,/&#44;/g;
3255 $metavalue =~ s/\:/&#58;/g;
3256 $metavalue =~ s/\|/&#124;/g;
3257 $metavalue =~ s/\(/&#40;/g;
3258 $metavalue =~ s/\)/&#41;/g;
3259 $metavalue =~ s/\[/&#91;/g;
3260 $metavalue =~ s/\\/&#92;/g;
3261 $metavalue =~ s/\]/&#93;/g;
3262 $metavalue =~ s/\{/&#123;/g;
3263 $metavalue =~ s/\}/&#125;/g;
3264 $metavalue =~ s/\"/&#34;/g;
3265 $metavalue =~ s/\`/&#96;/g;
3266 $metavalue =~ s/\n/_newline_/g;
3267
3268 # Edit the metadata.xml
3269 # Modified by Jeffrey from DL Consulting
3270 # Handle the case where there is one metadata.xml file for multiple FileSets
3271 # The XML filter needs to know whether it is in the right FileSet
3272 # TODO: This doesn't fix the problem where the metadata.xml is not next to the src file.
3273 # TODO: This doesn't handle the common metadata (where FileName doesn't point to a single file)
3274 $self->edit_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname,
3275 $metapos, $metavalue, $metamode, $import_tailname, $prevmetavalue);
3276 #=======================================================================#
3277 # set_import_metadata [END]
3278 #=======================================================================#
3279
3280
3281 #=======================================================================#
3282 # set_metadata (accumulate version) [START]
3283 #=======================================================================#
3284 # To people who know $collect_tail please add some comments
3285 # Obtain path to the database
3286 my $collect_tail = $collect;
3287 $collect_tail =~ s/^.*[\/\\]//;
3288 my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
3289 my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
3290
3291 # Read the docid entry
3292 my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
3293
3294 # Protect the quotes
3295 $metavalue =~ s/\"/\\\"/g;
3296
3297 # Adds the pending macro
3298 my $macro_metavalue = $rebuild_pending_macro . $metavalue;
3299
3300 # If the metadata doesn't exist, create a new one
3301 if (!defined($doc_rec->{$metaname})){
3302 $doc_rec->{$metaname} = [ $macro_metavalue ];
3303 }
3304 # Else, let's acculumate the values
3305 else {
3306 push(@{$doc_rec->{$metaname}},$macro_metavalue);
3307 }
3308
3309 ## Use the dbutil set_entry method instead of assuming the database is gdbm
3310 my $status = &dbutil::set_infodb_entry($infodbtype, $infodb_file_path, $docid, $doc_rec);
3311
3312 if ($status != 0) {
3313 # Catch error if gdbmget failed
3314 my $mess = "Failed to set metadata key: $docid\n";
3315
3316 $mess .= "PATH: $ENV{'PATH'}\n";
3317 $mess .= "Exit status: $status\n";
3318 $mess .= "System Error Message: $!\n";
3319
3320 $gsdl_cgi->generate_error($mess);
3321 }
3322 else {
3323 my $mess = "insert-metadata successful: Key[$docid]\n";
3324 $mess .= " [In metadata.xml] $metaname";
3325 $mess .= " = $metavalue\n";
3326 $mess .= " [In database] $metaname";
3327 $mess .= " = $macro_metavalue\n";
3328 $mess .= " The new text has not been indexed, rebuilding collection is required\n";
3329 $gsdl_cgi->generate_ok_message($mess);
3330 }
3331 #=======================================================================#
3332 # set_metadata (accumulate version) [END]
3333 #=======================================================================#
3334
3335 # Release the lock once it is done
3336 $self->unlock_collection($username, $collect);
3337}
3338
33391;
Note: See TracBrowser for help on using the repository browser.