[31602] | 1 | ##########################################################################
|
---|
| 2 | #
|
---|
| 3 | # modmetadataaction.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 redistribute 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 |
|
---|
| 26 | # This is a submodule of metadataaction.pm. It is loaded conditionally
|
---|
| 27 | # upon having the permissions to modify (not just get) metadata.
|
---|
| 28 | # With sufficient permissions, these additional subroutines should be made
|
---|
| 29 | # available to metadataaction.pm
|
---|
| 30 |
|
---|
| 31 | # See http://www.perlmonks.org/?node_id=881761 for splitting module into multiple files
|
---|
| 32 | # and how variables declared with 'our' are used there.
|
---|
| 33 |
|
---|
| 34 | package metadataaction;
|
---|
| 35 |
|
---|
| 36 | use strict;
|
---|
| 37 |
|
---|
| 38 | use cgiactions::baseaction;
|
---|
| 39 |
|
---|
| 40 | use dbutil;
|
---|
| 41 | use ghtml;
|
---|
| 42 |
|
---|
| 43 | use JSON;
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | $metadataaction::modmeta_action_table = #OR: our $modmeta_action_table =
|
---|
| 47 | {
|
---|
| 48 | #SET METHODS
|
---|
| 49 | "set-live-metadata" => {
|
---|
| 50 | 'compulsory-args' => [ "d", "metaname", "metavalue" ],
|
---|
| 51 | 'optional-args' => [ ] },
|
---|
| 52 |
|
---|
| 53 | "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
|
---|
| 54 | 'compulsory-args' => [ "metaname", "metavalue" ],
|
---|
| 55 | 'optional-args' => [ "where", "metapos", "metamode", "prevmetavalue", "d", "f" ] },
|
---|
| 56 |
|
---|
| 57 | "set-index-metadata" => {
|
---|
| 58 | 'compulsory-args' => [ "d", "metaname", "metavalue" ],
|
---|
| 59 | 'optional-args' => [ "metapos", "metamode" ] },
|
---|
| 60 |
|
---|
| 61 | "set-archives-metadata" => {
|
---|
| 62 | 'compulsory-args' => [ "d", "metaname", "metavalue" ],
|
---|
| 63 | 'optional-args' => [ "metapos", "metamode", "prevmetavalue" ] }, # metamode can be "accumulate", "override",
|
---|
| 64 |
|
---|
| 65 | "set-import-metadata" => {
|
---|
| 66 | 'compulsory-args' => [ "metaname", "metavalue" ],
|
---|
| 67 | '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
|
---|
| 68 |
|
---|
| 69 | #SET METHODS (ARRAY)
|
---|
| 70 | "set-metadata-array" => {
|
---|
| 71 | 'compulsory-args' => [ "where", "json" ],
|
---|
| 72 | 'optional-args' => [ ],
|
---|
| 73 | 'help-string' => [
|
---|
| 74 | '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"}]',
|
---|
| 75 |
|
---|
| 76 | '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"}]' ] },
|
---|
| 77 |
|
---|
| 78 | # The same examples rewritten for when running the metadata-server.pl script from the commandline:
|
---|
| 79 |
|
---|
| 80 | # 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\"}]",
|
---|
| 81 |
|
---|
| 82 | # 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\"}]"
|
---|
| 83 |
|
---|
| 84 | "set-archives-metadata-array" => {
|
---|
| 85 | 'compulsory-args' => [ "json" ],
|
---|
| 86 | 'optional-args' => [ ] },
|
---|
| 87 |
|
---|
| 88 | "set-import-metadata-array" => {
|
---|
| 89 | 'compulsory-args' => [ "json" ],
|
---|
| 90 | 'optional-args' => [ ] },
|
---|
| 91 |
|
---|
| 92 | "set-index-metadata-array" => {
|
---|
| 93 | 'compulsory-args' => [ "json" ],
|
---|
| 94 | 'optional-args' => [ ] },
|
---|
| 95 |
|
---|
| 96 | "set-live-metadata-array" => {
|
---|
| 97 | 'compulsory-args' => [ "json" ],
|
---|
| 98 | 'optional-args' => [ ] },
|
---|
| 99 |
|
---|
| 100 | #REMOVE METHODS
|
---|
| 101 | "remove-import-metadata" => {
|
---|
| 102 | 'compulsory-args' => [ "d", "metaname" ], #TODO: add f argument
|
---|
| 103 | 'optional-args' => [ "metapos", "metavalue", "metamode" ] }, # only provide metapos arg for SIMPLE collections.
|
---|
| 104 | # Metavalue is now an optional arg for remove_import_metadata() based on what the implementation did, which allowed metavalue to be undefined, and if so, used metapos.
|
---|
| 105 |
|
---|
| 106 | "remove-archives-metadata" => {
|
---|
| 107 | 'compulsory-args' => [ "d", "metaname" ], #TODO: add f argument
|
---|
| 108 | 'optional-args' => [ "metapos", "metavalue", "metamode" ] },
|
---|
| 109 |
|
---|
| 110 | "remove-live-metadata" => {
|
---|
| 111 | 'compulsory-args' => [ "d", "metaname" ],
|
---|
| 112 | 'optional-args' => [ ] },
|
---|
| 113 |
|
---|
| 114 | "remove-index-metadata" => {
|
---|
| 115 | 'compulsory-args' => [ "d", "metaname" ],
|
---|
| 116 | 'optional-args' => [ "metapos", "metavalue" ] },
|
---|
| 117 |
|
---|
| 118 | "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
|
---|
| 119 | 'compulsory-args' => [ "d", "metaname" ],
|
---|
| 120 | 'optional-args' => [ "where", "metapos", "metavalue", "metamode" ] }, # metamode is optional since remove-metadata can call any of remove_import_meta and remove_archives_meta, remove_index_meta, of which the first two accept metamode as an optional param
|
---|
| 121 |
|
---|
| 122 | #INSERT METHODS
|
---|
| 123 | "insert-metadata" => {
|
---|
| 124 | 'compulsory-args' => [ "d", "metaname", "metavalue" ],
|
---|
| 125 | 'optional-args' => [ ] }
|
---|
| 126 | };
|
---|
| 127 |
|
---|
| 128 |
|
---|
| 129 | sub _set_live_metadata
|
---|
| 130 | {
|
---|
| 131 | my $self = shift @_;
|
---|
| 132 |
|
---|
| 133 | my $collect = $self->{'collect'};
|
---|
| 134 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 135 | #my $gsdlhome = $self->{'gsdlhome'};
|
---|
| 136 | my $infodbtype = $self->{'infodbtype'};
|
---|
| 137 |
|
---|
| 138 | # Obtain the collect dir
|
---|
| 139 | my $site = $self->{'site'};
|
---|
| 140 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 141 | ##my $collect_dir = &util::filename_cat($gsdlhome, "collect");
|
---|
| 142 |
|
---|
| 143 |
|
---|
| 144 | # look up additional args
|
---|
| 145 | my $docid = $self->{'d'};
|
---|
| 146 | if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
|
---|
| 147 | $gsdl_cgi->generate_error("No docid (d=...) specified."); # generates error and dies
|
---|
| 148 | }
|
---|
| 149 | my $metavalue = $self->{'metavalue'};
|
---|
| 150 |
|
---|
| 151 | # Generate the dbkey
|
---|
| 152 | my $metaname = $self->{'metaname'};
|
---|
| 153 | my $dbkey = "$docid.$metaname";
|
---|
| 154 |
|
---|
| 155 | # To people who know $collect_tail please add some comments
|
---|
| 156 | # Obtain path to the database
|
---|
| 157 | my $collect_tail = $collect;
|
---|
| 158 | $collect_tail =~ s/^.*[\/|\\]//;
|
---|
| 159 | my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
|
---|
| 160 | my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
|
---|
| 161 |
|
---|
| 162 | # Set the new value
|
---|
| 163 | my $cmd = "gdbmset \"$infodb_file_path\" \"$dbkey\" \"$metavalue\"";
|
---|
| 164 | my $status = system($cmd);
|
---|
| 165 | if ($status != 0) {
|
---|
| 166 | # Catch error if gdbmget failed
|
---|
| 167 | my $mess = "Failed to set metadata key: $dbkey\n";
|
---|
| 168 |
|
---|
| 169 | $mess .= "PATH: $ENV{'PATH'}\n";
|
---|
| 170 | $mess .= "cmd = $cmd\n";
|
---|
| 171 | $mess .= "Exit status: $status\n";
|
---|
| 172 | $mess .= "System Error Message: $!\n";
|
---|
| 173 |
|
---|
| 174 | $gsdl_cgi->generate_error($mess);
|
---|
| 175 | }
|
---|
| 176 | else {
|
---|
| 177 | $gsdl_cgi->generate_ok_message("set-live-metadata successful: Key[$metaname]=$metavalue");
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | #return $status; # in case calling functions have any further use for this
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | sub set_live_metadata
|
---|
| 184 | {
|
---|
| 185 | my $self = shift @_;
|
---|
| 186 |
|
---|
| 187 | my $username = $self->{'username'};
|
---|
| 188 | my $collect = $self->{'collect'};
|
---|
| 189 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 190 |
|
---|
| 191 | if ($baseaction::authentication_enabled) {
|
---|
| 192 | # Ensure the user is allowed to edit this collection
|
---|
| 193 | $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | # Make sure the collection isn't locked by someone else
|
---|
| 197 | $self->lock_collection($username, $collect);
|
---|
| 198 |
|
---|
| 199 | $self->_set_live_metadata(@_);
|
---|
| 200 |
|
---|
| 201 | # Release the lock once it is done
|
---|
| 202 | $self->unlock_collection($username, $collect);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | sub set_index_metadata_entry
|
---|
| 206 | {
|
---|
| 207 | print STDERR "1\n";
|
---|
| 208 | my $self = shift @_;
|
---|
| 209 | my ($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue,$metamode,$prevmetavalue) = @_;
|
---|
| 210 |
|
---|
| 211 | # To people who know $collect_tail please add some comments
|
---|
| 212 | # Obtain path to the database
|
---|
| 213 | my $collect_tail = $collect;
|
---|
| 214 | $collect_tail =~ s/^.*[\/|\\]//;
|
---|
| 215 | my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
|
---|
| 216 | my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
|
---|
| 217 |
|
---|
| 218 | print STDERR "2\n";
|
---|
| 219 | # print STDERR "**** infodb file path = $infodb_file_path\n";
|
---|
| 220 | # print STDERR "***** infodb type = $infodbtype\n";
|
---|
| 221 |
|
---|
| 222 | # Read the docid entry
|
---|
| 223 | my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
|
---|
| 224 |
|
---|
| 225 | # Set the metadata value
|
---|
| 226 | if (defined $metapos) {
|
---|
| 227 | print STDERR "3\n";
|
---|
| 228 | # if metamode=accumulate AND metapos, warn user and then use metapos
|
---|
| 229 | if (defined $metamode && $metamode eq "accumulate") {
|
---|
| 230 | print STDERR "**** Warning: metamode is set to accumulate yet metapos is also provided for $docid\n";
|
---|
| 231 | print STDERR "**** Proceeding by using metapos\n";
|
---|
| 232 | }
|
---|
| 233 | $doc_rec->{$metaname}->[$metapos] = $metavalue;
|
---|
| 234 | }
|
---|
| 235 | elsif (defined $prevmetavalue) {
|
---|
| 236 | print STDERR "4\n";
|
---|
| 237 | my $array = $doc_rec->{$metaname};
|
---|
| 238 | my $length = @$array;
|
---|
| 239 |
|
---|
| 240 | my $found = 0;
|
---|
| 241 | for (my $i = 0; $i < $length; $i++){
|
---|
| 242 | if(defined $doc_rec->{$metaname}->[$i] && $doc_rec->{$metaname}->[$i] eq $prevmetavalue){
|
---|
| 243 | $doc_rec->{$metaname}->[$i] = $metavalue;
|
---|
| 244 | $found = 1;
|
---|
| 245 | last;
|
---|
| 246 | }
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | if($found == 0){
|
---|
| 250 | $doc_rec->{$metaname} = [ $metavalue ];
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
| 253 | elsif (defined $metamode && $metamode eq "override") {
|
---|
| 254 | print STDERR "5\n";
|
---|
| 255 | $doc_rec->{$metaname} = [ $metavalue ];
|
---|
| 256 | }
|
---|
| 257 | else { # default for index was to override, but because accumulate is less destructive,
|
---|
| 258 | # and because accumulate is the default for archives and import, that's the new default for index too
|
---|
| 259 | print STDERR "6\n";
|
---|
| 260 | if(defined $doc_rec->{$metaname}) {
|
---|
| 261 | push(@{$doc_rec->{$metaname}}, $metavalue); # accumulate the value for that metaname
|
---|
| 262 | } else {
|
---|
| 263 | $doc_rec->{$metaname} = [ $metavalue ];
|
---|
| 264 | }
|
---|
| 265 | }
|
---|
| 266 | print STDERR "6\n";
|
---|
| 267 |
|
---|
| 268 | my $status = &dbutil::set_infodb_entry($infodbtype, $infodb_file_path,$docid,$doc_rec);
|
---|
| 269 |
|
---|
| 270 | return $status;
|
---|
| 271 |
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | sub _set_import_metadata
|
---|
| 275 | {
|
---|
| 276 | my $self = shift @_;
|
---|
| 277 |
|
---|
| 278 | my $collect = $self->{'collect'};
|
---|
| 279 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 280 | my $infodbtype = $self->{'infodbtype'};
|
---|
| 281 | # my $gsdlhome = $self->{'gsdlhome'};
|
---|
| 282 |
|
---|
| 283 | # Obtain the collect and archive dir
|
---|
| 284 | my $site = $self->{'site'};
|
---|
| 285 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 286 | ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
|
---|
| 287 | my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
|
---|
| 288 |
|
---|
| 289 | # look up additional args
|
---|
| 290 | # want either d= or f=
|
---|
| 291 | my $docid = $self->{'d'};
|
---|
| 292 | my ($docid_root,$docid_secnum);
|
---|
| 293 | if(defined $docid) {
|
---|
| 294 | ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);
|
---|
| 295 | # as yet no support for setting subsection metadata in metadata.xml
|
---|
| 296 | if ((defined $docid_secnum) && ($docid_secnum !~ m/^\s*$/)) {
|
---|
| 297 | $gsdl_cgi->generate_message("*** No support yet for setting import metadata at subsections level.\n");
|
---|
| 298 | return;
|
---|
| 299 | }
|
---|
| 300 | }
|
---|
| 301 |
|
---|
| 302 | my $import_file = $self->{'f'};
|
---|
| 303 | if ((!defined $docid || $docid =~ m/^\s*$/) && (!defined $import_file || $import_file =~ m/^\s*$/)) {
|
---|
| 304 | $gsdl_cgi->generate_error("No docid (d=...) or import file (f=) specified."); # at least d or f must be specified
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | # Get the parameters and set default mode to "accumulate"
|
---|
| 308 | my $metaname = $self->{'metaname'};
|
---|
| 309 | my $metavalue = $self->{'metavalue'};
|
---|
| 310 | ## $metavalue =~ s/&lt;(.*?)&gt;/<$1>/g;
|
---|
| 311 | $metavalue =~ s/<(.*?)>/<$1>/g;
|
---|
| 312 |
|
---|
| 313 | my $metamode = $self->{'metamode'};
|
---|
| 314 | if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
|
---|
| 315 | # make "accumulate" the default (less destructive, as it won't actually
|
---|
| 316 | # delete any existing values)
|
---|
| 317 | $metamode = "accumulate";
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | # adding metapos and prevmetavalue support to import_metadata subroutines
|
---|
| 321 | my $metapos = $self->{'metapos'}; # don't force undef to 0. Undef has meaning when metamode=override
|
---|
| 322 | my $prevmetavalue = $self->{'prevmetavalue'};
|
---|
| 323 | $metapos = undef if(defined $metapos && ($metapos =~ m/^\s*$/));
|
---|
| 324 | $prevmetavalue = undef if(defined $prevmetavalue && ($prevmetavalue =~ m/^\s*$/));
|
---|
| 325 |
|
---|
| 326 | my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
|
---|
| 327 | 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
|
---|
| 328 |
|
---|
| 329 | my $mess = "set-import-metadata successful: Key[$docid] -> $metadata_xml_filename\n";
|
---|
| 330 | $mess .= " $metaname";
|
---|
| 331 | $mess .= " = $metavalue";
|
---|
| 332 | $mess .= " ($metamode)\n";
|
---|
| 333 |
|
---|
| 334 | $gsdl_cgi->generate_ok_message($mess);
|
---|
| 335 |
|
---|
| 336 | #return $status; # in case calling functions have any further use for this
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | # the version of set_index_meta that doesn't do authentication
|
---|
| 340 | sub _set_archives_metadata
|
---|
| 341 | {
|
---|
| 342 | my $self = shift @_;
|
---|
| 343 |
|
---|
| 344 | my $collect = $self->{'collect'};
|
---|
| 345 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 346 | my $infodbtype = $self->{'infodbtype'};
|
---|
| 347 |
|
---|
| 348 | # Obtain the collect and archive dir
|
---|
| 349 | my $site = $self->{'site'};
|
---|
| 350 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 351 | my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
|
---|
| 352 |
|
---|
| 353 | # look up additional args
|
---|
| 354 | my $docid = $self->{'d'};
|
---|
| 355 | my $metaname = $self->{'metaname'};
|
---|
| 356 | my $metavalue = $self->{'metavalue'};
|
---|
| 357 | my $prevmetavalue = $self->{'prevmetavalue'};
|
---|
| 358 |
|
---|
| 359 | my $metapos = $self->{'metapos'}; # don't force undef to 0. Undef has meaning when metamode=override
|
---|
| 360 | # Don't append "|| undef", since if metapos=0 it will then be set to undef
|
---|
| 361 |
|
---|
| 362 | $metapos = undef if(defined $metapos && ($metapos =~ m/^\s*$/));
|
---|
| 363 | $prevmetavalue = undef if(defined $prevmetavalue && ($prevmetavalue =~ m/^\s*$/));
|
---|
| 364 |
|
---|
| 365 | my $metamode = $self->{'metamode'};
|
---|
| 366 | if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
|
---|
| 367 | # make "accumulate" the default (less destructive, as it won't actually
|
---|
| 368 | # delete any existing values)
|
---|
| 369 | $metamode = "accumulate";
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | my $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid,
|
---|
| 373 | $metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
|
---|
| 374 |
|
---|
| 375 | if ($status == 0) {
|
---|
| 376 | my $mess = "set-archives-metadata successful: Key[$docid]\n";
|
---|
| 377 | $mess .= " $metaname";
|
---|
| 378 | $mess .= "->[$metapos]" if (defined $metapos);
|
---|
| 379 | $mess .= " = $metavalue";
|
---|
| 380 | $mess .= " ($metamode)\n";
|
---|
| 381 |
|
---|
| 382 | $gsdl_cgi->generate_ok_message($mess);
|
---|
| 383 | }
|
---|
| 384 | else {
|
---|
| 385 | my $mess .= "Failed to set archives metadata key: $docid\n";
|
---|
| 386 | $mess .= "Exit status: $status\n";
|
---|
| 387 | if(defined $self->{'error_msg'}) {
|
---|
| 388 | $mess .= "Error Message: $self->{'error_msg'}\n";
|
---|
| 389 | } else {
|
---|
| 390 | $mess .= "System Error Message: $!\n";
|
---|
| 391 | }
|
---|
| 392 | $mess .= "-" x 20 . "\n";
|
---|
| 393 |
|
---|
| 394 | $gsdl_cgi->generate_error($mess);
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | #return $status; # in case calling functions have any further use for this
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 |
|
---|
| 401 | # the version of set_index_meta that doesn't do authentication
|
---|
| 402 | sub _set_index_metadata
|
---|
| 403 | {
|
---|
| 404 | print STDERR "START SET INDEX METADATA\n";
|
---|
| 405 | my $self = shift @_;
|
---|
| 406 |
|
---|
| 407 | my $collect = $self->{'collect'};
|
---|
| 408 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 409 |
|
---|
| 410 | my $site = $self->{'site'};
|
---|
| 411 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 412 |
|
---|
| 413 | # look up additional args
|
---|
| 414 | my $docid = $self->{'d'};
|
---|
| 415 | my $metaname = $self->{'metaname'};
|
---|
| 416 | my $metapos = $self->{'metapos'}; # undef has meaning
|
---|
| 417 | my $metavalue = $self->{'metavalue'};
|
---|
| 418 | my $infodbtype = $self->{'infodbtype'};
|
---|
| 419 | my $metamode = $self->{'metamode'};
|
---|
| 420 | my $prevmetavalue = $self->{'prevmetavalue'};
|
---|
| 421 |
|
---|
| 422 | $metapos = undef if(defined $metapos && ($metapos =~ m/^\s*$/));
|
---|
| 423 | $prevmetavalue = undef if(defined $prevmetavalue && ($prevmetavalue =~ m/^\s*$/));
|
---|
| 424 |
|
---|
| 425 | print STDERR "SETTING INDEX METADATA ENTRY\n";
|
---|
| 426 | my $status = $self->set_index_metadata_entry($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
|
---|
| 427 | print STDERR "DONE SETTING INDEX METADATA ENTRY\n";
|
---|
| 428 | if ($status != 0) {
|
---|
| 429 | # Catch error if set infodb entry failed
|
---|
| 430 | my $mess = "Failed to set metadata key: $docid\n";
|
---|
| 431 |
|
---|
| 432 | $mess .= "PATH: $ENV{'PATH'}\n";
|
---|
| 433 | $mess .= "Exit status: $status\n";
|
---|
| 434 | $mess .= "System Error Message: $!\n";
|
---|
| 435 |
|
---|
| 436 | $gsdl_cgi->generate_error($mess);
|
---|
| 437 | }
|
---|
| 438 | else {
|
---|
| 439 | my $mess = "set-index-metadata successful: Key[$docid]\n";
|
---|
| 440 | $mess .= " $metaname";
|
---|
| 441 | $mess .= "->[$metapos]" if (defined $metapos);
|
---|
| 442 | $mess .= " = $metavalue\n";
|
---|
| 443 |
|
---|
| 444 | $gsdl_cgi->generate_ok_message($mess);
|
---|
| 445 | }
|
---|
| 446 |
|
---|
| 447 | print STDERR "END SET INDEX METADATA\n";
|
---|
| 448 | #return $status; # in case calling functions have any further use for this
|
---|
| 449 | }
|
---|
| 450 |
|
---|
| 451 | sub set_index_metadata
|
---|
| 452 | {
|
---|
| 453 | my $self = shift @_;
|
---|
| 454 |
|
---|
| 455 | my $username = $self->{'username'};
|
---|
| 456 | my $collect = $self->{'collect'};
|
---|
| 457 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 458 | #my $gsdlhome = $self->{'gsdlhome'};
|
---|
| 459 |
|
---|
| 460 | if ($baseaction::authentication_enabled) {
|
---|
| 461 | # Ensure the user is allowed to edit this collection
|
---|
| 462 | $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
|
---|
| 463 | }
|
---|
| 464 |
|
---|
| 465 | my $site = $self->{'site'};
|
---|
| 466 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 467 |
|
---|
| 468 | $gsdl_cgi->checked_chdir($collect_dir);
|
---|
| 469 |
|
---|
| 470 | # Obtain the collect dir
|
---|
| 471 | ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
|
---|
| 472 |
|
---|
| 473 | # Make sure the collection isn't locked by someone else
|
---|
| 474 | $self->lock_collection($username, $collect);
|
---|
| 475 |
|
---|
| 476 | $self->_set_index_metadata(@_);
|
---|
| 477 |
|
---|
| 478 | # Release the lock once it is done
|
---|
| 479 | $self->unlock_collection($username, $collect);
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 | # call this to set the metadata for a combination of dirs archives, import or index, or live
|
---|
| 483 | # if none specified, defaults to index which was the original behaviour of set_metadata.
|
---|
| 484 | sub set_metadata
|
---|
| 485 | {
|
---|
| 486 | my $self = shift @_;
|
---|
| 487 |
|
---|
| 488 | # Testing that not defining a variable, setting it to "" or to " " all return false
|
---|
| 489 | # >perl -e 'my $whichdirs=""; if($whichdirs) {print "$whichdirs\n"};'
|
---|
| 490 |
|
---|
| 491 | my $where = $self->{'where'};
|
---|
| 492 | if(!$where || ($where =~ m/^\s*$/)) {
|
---|
| 493 | $self->set_index_metadata(@_); # call the full version of set_index_meta for the default behaviour
|
---|
| 494 | return;
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | # authenticate and lock collection once, even if processing multiple dirs
|
---|
| 498 | my $username = $self->{'username'};
|
---|
| 499 | my $collect = $self->{'collect'};
|
---|
| 500 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 501 |
|
---|
| 502 | if ($baseaction::authentication_enabled) {
|
---|
| 503 | # Ensure the user is allowed to edit this collection
|
---|
| 504 | $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
|
---|
| 505 | }
|
---|
| 506 |
|
---|
| 507 | if($where =~ m/index/) {
|
---|
| 508 | my $site = $self->{'site'};
|
---|
| 509 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 510 | $gsdl_cgi->checked_chdir($collect_dir);
|
---|
| 511 | }
|
---|
| 512 |
|
---|
| 513 | # Make sure the collection isn't locked by someone else
|
---|
| 514 | $self->lock_collection($username, $collect);
|
---|
| 515 |
|
---|
| 516 |
|
---|
| 517 | # now at last can set the metadata. $where can specify multiple
|
---|
| 518 | # $where is of the form: import|archives|index, or a subset thereof
|
---|
| 519 |
|
---|
| 520 | #my @whichdirs = split('\|', $where);
|
---|
| 521 |
|
---|
| 522 | # just check whether $where contains import/archives/index/live in turn, and
|
---|
| 523 | # for each case, process it accordingly
|
---|
| 524 | if($where =~ m/import/) {
|
---|
| 525 | $self->_set_import_metadata(@_);
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | if($where =~ m/archives/) {
|
---|
| 529 |
|
---|
| 530 | # look up docID arg which is optional to set_metadata because it's optional
|
---|
| 531 | # to set_import, but which is compulsory to set_archives_metadata
|
---|
| 532 | my $docid = $self->{'d'};
|
---|
| 533 | if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
|
---|
| 534 | $gsdl_cgi->generate_error("No docid (d=...) specified."); # generates error and dies
|
---|
| 535 | }
|
---|
| 536 | # we have a docid, so can set archives meta
|
---|
| 537 | $self->_set_archives_metadata(@_);
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | if($where =~ m/index/) {
|
---|
| 541 |
|
---|
| 542 | # look up docID arg which is optional to set_metadata because it's optional
|
---|
| 543 | # to set_import, but which is compulsory to set_archives_metadata
|
---|
| 544 | my $docid = $self->{'d'};
|
---|
| 545 | if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
|
---|
| 546 | $gsdl_cgi->generate_error("No docid (d=...) specified.");
|
---|
| 547 | }
|
---|
| 548 | # we have a docid, so can set index meta
|
---|
| 549 | $self->_set_index_metadata(@_);
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | if($where =~ m/live/) {
|
---|
| 553 | $self->_set_live_metadata(@_); # docid param, d, is compulsory, but is checked for in subroutine
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | # Release the lock once it is done
|
---|
| 557 | $self->unlock_collection($username, $collect);
|
---|
| 558 | }
|
---|
| 559 |
|
---|
| 560 | sub set_metadata_array
|
---|
| 561 | {
|
---|
| 562 | my $self = shift @_;
|
---|
| 563 |
|
---|
| 564 | my $where = $self->{'where'};
|
---|
| 565 | if(!$where || ($where =~ m/^\s*$/)) {
|
---|
| 566 | $self->set_index_metadata_array(@_); # default behaviour is the full version of set_index_meta_array
|
---|
| 567 | return;
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | my $username = $self->{'username'};
|
---|
| 571 | my $collect = $self->{'collect'};
|
---|
| 572 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 573 |
|
---|
| 574 | if ($baseaction::authentication_enabled) {
|
---|
| 575 | # Ensure the user is allowed to edit this collection
|
---|
| 576 | $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | # Not sure if the checked_chdir is necessary, since lock_collection also does a chdir
|
---|
| 580 | # But including the stmt during this code reorganisation to preserve as-is what used to happen
|
---|
| 581 | my $site = $self->{'site'};
|
---|
| 582 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 583 | $gsdl_cgi->checked_chdir($collect_dir);
|
---|
| 584 |
|
---|
| 585 | # Make sure the collection isn't locked by someone else
|
---|
| 586 | $self->lock_collection($username, $collect);
|
---|
| 587 |
|
---|
| 588 | if($where =~ m/import/) {
|
---|
| 589 | $self->_set_import_metadata_array(@_);
|
---|
| 590 | }
|
---|
| 591 | if($where =~ m/archives/) {
|
---|
| 592 | $self->_set_archives_metadata_array(@_);
|
---|
| 593 | }
|
---|
| 594 | if($where =~ m/index/) {
|
---|
| 595 | $self->_set_index_metadata_array(@_);
|
---|
| 596 | }
|
---|
| 597 | if($where =~ m/live/) {
|
---|
| 598 | $self->_set_live_metadata_array(@_);
|
---|
| 599 | }
|
---|
| 600 |
|
---|
| 601 | # Release the lock once it is done
|
---|
| 602 | $self->unlock_collection($username, $collect);
|
---|
| 603 | }
|
---|
| 604 |
|
---|
| 605 | sub _set_index_metadata_array
|
---|
| 606 | {
|
---|
| 607 | my $self = shift @_;
|
---|
| 608 |
|
---|
| 609 | my $collect = $self->{'collect'};
|
---|
| 610 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 611 | my $site = $self->{'site'};
|
---|
| 612 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 613 |
|
---|
| 614 |
|
---|
| 615 | # look up additional args
|
---|
| 616 |
|
---|
| 617 | my $infodbtype = $self->{'infodbtype'};
|
---|
| 618 |
|
---|
| 619 | my $json_str = $self->{'json'};
|
---|
| 620 | my $doc_array = decode_json $json_str;
|
---|
| 621 |
|
---|
| 622 |
|
---|
| 623 | my $global_status = 0;
|
---|
| 624 | my $global_mess = "";
|
---|
| 625 |
|
---|
| 626 | my @all_docids = ();
|
---|
| 627 |
|
---|
| 628 | foreach my $doc_array_rec ( @$doc_array ) {
|
---|
| 629 |
|
---|
| 630 | my $status = -1;
|
---|
| 631 | my $docid = $doc_array_rec->{'docid'};
|
---|
| 632 |
|
---|
| 633 | push(@all_docids,$docid);
|
---|
| 634 |
|
---|
| 635 | my $metaname = $doc_array_rec->{'metaname'};
|
---|
| 636 | if(defined $metaname) {
|
---|
| 637 | my $metapos = $doc_array_rec->{'metapos'}; # can legitimately be undef
|
---|
| 638 | my $metavalue = $doc_array_rec->{'metavalue'};
|
---|
| 639 | my $metamode = $doc_array_rec->{'metamode'} || $self->{'metamode'};
|
---|
| 640 |
|
---|
| 641 | $status = $self->set_index_metadata_entry($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue,$metamode);
|
---|
| 642 | } elsif (defined $doc_array_rec->{'metatable'}) { # if no metaname, we expect a metatable
|
---|
| 643 | my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
|
---|
| 644 |
|
---|
| 645 | foreach my $metatable_rec ( @$metatable ) { # the subarray metatable is an array of hashmaps
|
---|
| 646 | $metaname = $metatable_rec->{'metaname'};
|
---|
| 647 | my $metamode = $metatable_rec->{'metamode'} || $doc_array_rec->{'metamode'} || $self->{'metamode'};
|
---|
| 648 | my $metapos = undef;
|
---|
| 649 | my $metavals = $metatable_rec->{'metavals'}; # a sub-subarray
|
---|
| 650 |
|
---|
| 651 | foreach my $metavalue ( @$metavals ) { # metavals is an array
|
---|
| 652 | $status = $self->set_index_metadata_entry($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue,$metamode); # how do we use metamode in set_meta_entry?
|
---|
| 653 | if($metamode eq "override") { # now, having overridden the metavalue for the first,
|
---|
| 654 | # need to accumulate subsequent metavals for this metaname, else the just-assigned
|
---|
| 655 | # metavalue for this metaname will be lost
|
---|
| 656 | $metamode = "accumulate";
|
---|
| 657 | }
|
---|
| 658 | }
|
---|
| 659 | }
|
---|
| 660 | }
|
---|
| 661 |
|
---|
| 662 | if ($status != 0) {
|
---|
| 663 | # Catch error if set infodb entry failed
|
---|
| 664 | $global_status = $status;
|
---|
| 665 | $global_mess .= "Failed to set metadata key: $docid\n";
|
---|
| 666 | $global_mess .= "Exit status: $status\n";
|
---|
| 667 | $global_mess .= "System Error Message: $!\n";
|
---|
| 668 | $global_mess .= "-" x 20;
|
---|
| 669 | }
|
---|
| 670 | }
|
---|
| 671 |
|
---|
| 672 | if ($global_status != 0) {
|
---|
| 673 | $global_mess .= "PATH: $ENV{'PATH'}\n";
|
---|
| 674 | $gsdl_cgi->generate_error($global_mess);
|
---|
| 675 | }
|
---|
| 676 | else {
|
---|
| 677 | my $mess = "set-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n";
|
---|
| 678 | $gsdl_cgi->generate_ok_message($mess);
|
---|
| 679 | }
|
---|
| 680 | }
|
---|
| 681 |
|
---|
| 682 | sub set_index_metadata_array
|
---|
| 683 | {
|
---|
| 684 | my $self = shift @_;
|
---|
| 685 |
|
---|
| 686 | my $username = $self->{'username'};
|
---|
| 687 | my $collect = $self->{'collect'};
|
---|
| 688 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 689 | # my $gsdlhome = $self->{'gsdlhome'};
|
---|
| 690 |
|
---|
| 691 | if ($baseaction::authentication_enabled) {
|
---|
| 692 | # Ensure the user is allowed to edit this collection
|
---|
| 693 | $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
|
---|
| 694 | }
|
---|
| 695 |
|
---|
| 696 | my $site = $self->{'site'};
|
---|
| 697 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 698 |
|
---|
| 699 | $gsdl_cgi->checked_chdir($collect_dir);
|
---|
| 700 |
|
---|
| 701 | # Obtain the collect dir
|
---|
| 702 | ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
|
---|
| 703 |
|
---|
| 704 | # Make sure the collection isn't locked by someone else
|
---|
| 705 | $self->lock_collection($username, $collect);
|
---|
| 706 |
|
---|
| 707 | $self->_set_index_metadata_array(@_);
|
---|
| 708 |
|
---|
| 709 | # Release the lock once it is done
|
---|
| 710 | $self->unlock_collection($username, $collect);
|
---|
| 711 | }
|
---|
| 712 |
|
---|
| 713 | # experimental, newly added in and untested
|
---|
| 714 | sub _set_live_metadata_array
|
---|
| 715 | {
|
---|
| 716 | my $self = shift @_;
|
---|
| 717 |
|
---|
| 718 | my $collect = $self->{'collect'};
|
---|
| 719 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 720 |
|
---|
| 721 | my $site = $self->{'site'};
|
---|
| 722 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 723 |
|
---|
| 724 |
|
---|
| 725 | # look up additional args
|
---|
| 726 | my $infodbtype = $self->{'infodbtype'};
|
---|
| 727 | # To people who know $collect_tail please add some comments
|
---|
| 728 | # Obtain path to the database
|
---|
| 729 | my $collect_tail = $collect;
|
---|
| 730 | $collect_tail =~ s/^.*[\/|\\]//;
|
---|
| 731 | my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
|
---|
| 732 | my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
|
---|
| 733 |
|
---|
| 734 |
|
---|
| 735 | my $json_str = $self->{'json'};
|
---|
| 736 | my $doc_array = decode_json $json_str;
|
---|
| 737 |
|
---|
| 738 |
|
---|
| 739 | my $global_status = 0;
|
---|
| 740 | my $global_mess = "";
|
---|
| 741 |
|
---|
| 742 | my @all_docids = ();
|
---|
| 743 |
|
---|
| 744 |
|
---|
| 745 | foreach my $doc_array_rec ( @$doc_array ) {
|
---|
| 746 |
|
---|
| 747 | my $status = -1;
|
---|
| 748 | my $docid = $doc_array_rec->{'docid'};
|
---|
| 749 |
|
---|
| 750 | push(@all_docids,$docid);
|
---|
| 751 |
|
---|
| 752 | my $metaname = $doc_array_rec->{'metaname'};
|
---|
| 753 | if(defined $metaname) {
|
---|
| 754 | my $dbkey = "$docid.$metaname";
|
---|
| 755 | my $metavalue = $doc_array_rec->{'metavalue'};
|
---|
| 756 |
|
---|
| 757 | # Set the new value
|
---|
| 758 | my $cmd = "gdbmset \"$infodb_file_path\" \"$dbkey\" \"$metavalue\"";
|
---|
| 759 | $status = system($cmd);
|
---|
| 760 |
|
---|
| 761 | } elsif (defined $doc_array_rec->{'metatable'}) { # if no metaname, we expect a metatable
|
---|
| 762 | my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
|
---|
| 763 | foreach my $metatable_rec ( @$metatable ) {
|
---|
| 764 | $metaname = $metatable_rec->{'metaname'};
|
---|
| 765 | my $dbkey = "$docid.$metaname";
|
---|
| 766 |
|
---|
| 767 | my $metavals = $metatable_rec->{'metavals'}; # a sub-subarray
|
---|
| 768 | foreach my $metavalue ( @$metavals ) {
|
---|
| 769 | my $cmd = "gdbmset \"$infodb_file_path\" \"$dbkey\" \"$metavalue\"";
|
---|
| 770 | $status = system($cmd);
|
---|
| 771 | }
|
---|
| 772 | }
|
---|
| 773 |
|
---|
| 774 | }
|
---|
| 775 |
|
---|
| 776 | if ($status != 0) {
|
---|
| 777 | # Catch error if gdbmget failed
|
---|
| 778 | $global_status = $status;
|
---|
| 779 | $global_mess .= "Failed to set metadata key: $docid\n"; # $dbkey
|
---|
| 780 | $global_mess .= "Exit status: $status\n";
|
---|
| 781 | $global_mess .= "System Error Message: $!\n";
|
---|
| 782 | $global_mess .= "-" x 20;
|
---|
| 783 | }
|
---|
| 784 | }
|
---|
| 785 |
|
---|
| 786 | if ($global_status != 0) {
|
---|
| 787 | $global_mess .= "PATH: $ENV{'PATH'}\n";
|
---|
| 788 | $gsdl_cgi->generate_error($global_mess);
|
---|
| 789 | }
|
---|
| 790 | else {
|
---|
| 791 | my $mess = "set-live-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n";
|
---|
| 792 | $gsdl_cgi->generate_ok_message($mess);
|
---|
| 793 | }
|
---|
| 794 | }
|
---|
| 795 |
|
---|
| 796 | sub set_live_metadata_array
|
---|
| 797 | {
|
---|
| 798 | my $self = shift @_;
|
---|
| 799 |
|
---|
| 800 | my $username = $self->{'username'};
|
---|
| 801 | my $collect = $self->{'collect'};
|
---|
| 802 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 803 |
|
---|
| 804 | if ($baseaction::authentication_enabled) {
|
---|
| 805 | # Ensure the user is allowed to edit this collection
|
---|
| 806 | $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
|
---|
| 807 | }
|
---|
| 808 |
|
---|
| 809 | my $site = $self->{'site'};
|
---|
| 810 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 811 |
|
---|
| 812 | $gsdl_cgi->checked_chdir($collect_dir);
|
---|
| 813 |
|
---|
| 814 | # Make sure the collection isn't locked by someone else
|
---|
| 815 | $self->lock_collection($username, $collect);
|
---|
| 816 |
|
---|
| 817 | $self->_set_live_metadata_array(@_);
|
---|
| 818 |
|
---|
| 819 | # Release the lock once it is done
|
---|
| 820 | $self->unlock_collection($username, $collect);
|
---|
| 821 | }
|
---|
| 822 |
|
---|
| 823 |
|
---|
| 824 | sub dxml_metadata
|
---|
| 825 | {
|
---|
| 826 | my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
|
---|
| 827 | my $metaname = $parser->{'parameters'}->{'metaname'};
|
---|
| 828 | my $metamode = $parser->{'parameters'}->{'metamode'};
|
---|
| 829 |
|
---|
| 830 | ###!!! print STDERR "**** Processing closing </Metadata> tag\n";
|
---|
| 831 |
|
---|
| 832 | my $opt_doc_secnum = $parser->{'parameters'}->{'secnum'};
|
---|
| 833 |
|
---|
| 834 | # Find the right metadata tag and checks if we are going to
|
---|
| 835 | # override it
|
---|
| 836 | #
|
---|
| 837 | # Note: This over writes the first metadata block it
|
---|
| 838 | # encountered. If there are multiple Sections in the doc.xml, it
|
---|
| 839 | # might not behave as you would expect
|
---|
| 840 |
|
---|
| 841 | my $curr_secnum = $parser->{'parameters'}->{'curr_section_num'};
|
---|
| 842 | ## print STDERR "**** checking $opt_doc_secnum <=> $curr_secnum\n";
|
---|
| 843 | ## print STDERR "**** metamode = $metamode\n";
|
---|
| 844 |
|
---|
| 845 | if ((!defined $opt_doc_secnum) || ($opt_doc_secnum eq $curr_secnum))
|
---|
| 846 | {
|
---|
| 847 | my $name_attr = $attrHash->{'name'};
|
---|
| 848 | # print STDOUT "*** testing: $name_attr eq $metaname ? and $metamode eq override ?\n";
|
---|
| 849 | if (($name_attr eq $metaname) && ($metamode eq "override"))
|
---|
| 850 | {
|
---|
| 851 | if (!defined $parser->{'parameters'}->{'poscount'})
|
---|
| 852 | {
|
---|
| 853 | $parser->{'parameters'}->{'poscount'} = 0;
|
---|
| 854 | }
|
---|
| 855 | else
|
---|
| 856 | {
|
---|
| 857 | $parser->{'parameters'}->{'poscount'}++;
|
---|
| 858 | }
|
---|
| 859 |
|
---|
| 860 | if ((defined $parser->{'parameters'}->{'metapos'} && $parser->{'parameters'}->{'poscount'} == $parser->{'parameters'}->{'metapos'})
|
---|
| 861 | || (!defined $parser->{'parameters'}->{'metapos'} && $parser->{'parameters'}->{'poscount'} == 0))
|
---|
| 862 |
|
---|
| 863 | {
|
---|
| 864 | ##print STDERR "#### got match!!\n";
|
---|
| 865 | # Get the value and override the current value
|
---|
| 866 | my $metavalue = $parser->{'parameters'}->{'metavalue'};
|
---|
| 867 | $attrHash->{'_content'} = $metavalue;
|
---|
| 868 |
|
---|
| 869 | # Don't want it to wipe out any other pieces of metadata
|
---|
| 870 | $parser->{'parameters'}->{'metamode'} = "done";
|
---|
| 871 | }
|
---|
| 872 | elsif (defined $parser->{'parameters'}->{'prevmetavalue'} && $parser->{'parameters'}->{'prevmetavalue'} eq $attrHash->{'_content'})
|
---|
| 873 | {
|
---|
| 874 | my $metavalue = $parser->{'parameters'}->{'metavalue'};
|
---|
| 875 | $attrHash->{'_content'} = $metavalue;
|
---|
| 876 | $parser->{'parameters'}->{'metamode'} = "done";
|
---|
| 877 | }
|
---|
| 878 | }
|
---|
| 879 | }
|
---|
| 880 |
|
---|
| 881 | # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
|
---|
| 882 | return [$tagname => $attrHash];
|
---|
| 883 | }
|
---|
| 884 |
|
---|
| 885 | # This method exists purely for catching invalid section numbers that the client
|
---|
| 886 | # requested to edit. Once the parser has reached the end (the final </Archive> tag),
|
---|
| 887 | # we've seen all the Sections in the doc.xml, and none of their section nums matched
|
---|
| 888 | # if the metamode has not been set to 'done' by then.
|
---|
| 889 | sub dxml_archive
|
---|
| 890 | {
|
---|
| 891 | my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
|
---|
| 892 | my $metamode = $parser->{'parameters'}->{'metamode'};
|
---|
| 893 |
|
---|
| 894 | my $curr_secnum = $parser->{'parameters'}->{'curr_section_num'};
|
---|
| 895 | my $opt_doc_secnum = $parser->{'parameters'}->{'secnum'};
|
---|
| 896 |
|
---|
| 897 | # print STDERR "@@@ $tagname Processing a closing </Archive> tag [$curr_secnum|$opt_doc_secnum]\n";
|
---|
| 898 |
|
---|
| 899 | if ($metamode ne "done" && $curr_secnum ne $opt_doc_secnum) {
|
---|
| 900 | print STDERR "@@@ $tagname Finished processing FINAL Section.\n";
|
---|
| 901 |
|
---|
| 902 | my $metaname = $parser->{'parameters'}->{'metaname'};
|
---|
| 903 | my $metavalue = $parser->{'parameters'}->{'metavalue'};
|
---|
| 904 |
|
---|
| 905 | print STDERR "@@@ Requested section number $opt_doc_secnum not found.\n";
|
---|
| 906 | print STDERR "\t(last seen section number in document was $curr_secnum)\n";
|
---|
| 907 | print STDERR "\tDiscarded metadata value '$metavalue' for meta '$metaname'\n";
|
---|
| 908 | print STDERR "\tin section $opt_doc_secnum.\n";
|
---|
| 909 | $parser->{'custom_err_msg'} = "Requested section number $opt_doc_secnum not found.";
|
---|
| 910 | }
|
---|
| 911 |
|
---|
| 912 | # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
|
---|
| 913 | return [$tagname => $attrHash];
|
---|
| 914 | }
|
---|
| 915 |
|
---|
| 916 | sub dxml_description
|
---|
| 917 | {
|
---|
| 918 | my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
|
---|
| 919 | my $metamode = $parser->{'parameters'}->{'metamode'};
|
---|
| 920 |
|
---|
| 921 | my $curr_secnum = $parser->{'parameters'}->{'curr_section_num'};
|
---|
| 922 | my $opt_doc_secnum = $parser->{'parameters'}->{'secnum'} || "";
|
---|
| 923 |
|
---|
| 924 | ###!!! print STDERR "**** Processing a closing </Description> tag \n";
|
---|
| 925 | # print STDERR "@@@ $tagname Processing a closing </Description> tag [$curr_secnum|$opt_doc_secnum]\n";
|
---|
| 926 |
|
---|
| 927 | # Accumulate the metadata
|
---|
| 928 |
|
---|
| 929 | # We'll be accumulating metadata at this point if we haven't found and therefore
|
---|
| 930 | # haven't processed the metadata yet.
|
---|
| 931 | # For subsections, this means that if we're at a matching subsection, but haven't
|
---|
| 932 | # found the correct metaname to override in that subsection, we accumulate it as new
|
---|
| 933 | # meta in the subsection by adding it to the current description.
|
---|
| 934 | # If there's no subsection info for the metadata, it will accumulate at the top level
|
---|
| 935 | # section description if we hadn't found a matching metaname to override at this point.
|
---|
| 936 |
|
---|
| 937 | # Both curr_secnum and opt_doc_secnum can be "". In the former case, it means we're now
|
---|
| 938 | # at the toplevel section. In the latter case, it means we want to process meta in the
|
---|
| 939 | # toplevel section. So the eq check between the values below will work in all cases.
|
---|
| 940 |
|
---|
| 941 | # The only time this won't work is if an opt_doc_secnum beyond the section numbers of
|
---|
| 942 | # this document has been provided. In that case, the metadata for that opt_doc_secnum
|
---|
| 943 | # won't get attached/accumulated to any part of the doc, not even its top-level section.
|
---|
| 944 |
|
---|
| 945 | if ($curr_secnum eq $opt_doc_secnum
|
---|
| 946 | && ($metamode eq "accumulate" || $metamode eq "override")) {
|
---|
| 947 | if ($metamode eq "override") {
|
---|
| 948 | print "Got to end of <Description> block. No metadata value to override. Switching 'metamode' to accumulate\n";
|
---|
| 949 | }
|
---|
| 950 |
|
---|
| 951 | # If we get to here and metamode is override, this means there
|
---|
| 952 | # was no existing value to overide => treat as an append operation
|
---|
| 953 |
|
---|
| 954 | # Tack a new metadata tag on to the end of the <Metadata>+ block
|
---|
| 955 | my $metaname = $parser->{'parameters'}->{'metaname'};
|
---|
| 956 | my $metavalue = $parser->{'parameters'}->{'metavalue'};
|
---|
| 957 |
|
---|
| 958 | my $metadata_attr = {
|
---|
| 959 | '_content' => $metavalue,
|
---|
| 960 | 'name' => $metaname,
|
---|
| 961 | 'mode' => "accumulate"
|
---|
| 962 | };
|
---|
| 963 |
|
---|
| 964 | my $append_metadata = [ "Metadata" => $metadata_attr ];
|
---|
| 965 | my $description_content = $attrHash->{'_content'};
|
---|
| 966 |
|
---|
| 967 | print "Appending metadata to doc.xml\n";
|
---|
| 968 |
|
---|
| 969 | if (ref($description_content)) {
|
---|
| 970 | # got some existing interesting nested content
|
---|
| 971 | push(@$description_content, " ", $append_metadata ,"\n ");
|
---|
| 972 | }
|
---|
| 973 | else {
|
---|
| 974 | #description_content is most likely a string such as "\n"
|
---|
| 975 | $attrHash->{'_content'} = [$description_content, " ", $append_metadata ,"\n" ];
|
---|
| 976 | }
|
---|
| 977 |
|
---|
| 978 | $parser->{'parameters'}->{'metamode'} = "done";
|
---|
| 979 | }
|
---|
| 980 | else {
|
---|
| 981 | # metamode most likely "done" signifying that it has already found a position to add the metadata to.
|
---|
| 982 | ## print STDERR "**** NOT ACCUMULATE?!? \n";
|
---|
| 983 | }
|
---|
| 984 |
|
---|
| 985 | # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
|
---|
| 986 | return [$tagname => $attrHash];
|
---|
| 987 | }
|
---|
| 988 |
|
---|
| 989 |
|
---|
| 990 | sub dxml_start_section
|
---|
| 991 | {
|
---|
| 992 | my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
|
---|
| 993 |
|
---|
| 994 | my $new_depth = scalar(@$contextArray);
|
---|
| 995 |
|
---|
| 996 | # print STDERR "**** START SECTION \n";
|
---|
| 997 |
|
---|
| 998 | if ($new_depth == 1) {
|
---|
| 999 | $parser->{'parameters'}->{'curr_section_depth'} = 1;
|
---|
| 1000 | $parser->{'parameters'}->{'curr_section_num'} = "";
|
---|
| 1001 | }
|
---|
| 1002 |
|
---|
| 1003 | my $old_depth = $parser->{'parameters'}->{'curr_section_depth'};
|
---|
| 1004 | my $old_secnum = $parser->{'parameters'}->{'curr_section_num'};
|
---|
| 1005 |
|
---|
| 1006 | my $new_secnum;
|
---|
| 1007 |
|
---|
| 1008 | if ($new_depth > $old_depth) {
|
---|
| 1009 | # child subsection
|
---|
| 1010 | $new_secnum = "$old_secnum.1";
|
---|
| 1011 | }
|
---|
| 1012 | elsif ($new_depth == $old_depth) {
|
---|
| 1013 | # sibling section => increase it's value by 1
|
---|
| 1014 | my ($tail_num) = ($old_secnum =~ m/\.(\d+)$/);
|
---|
| 1015 | $tail_num++;
|
---|
| 1016 | $new_secnum = $old_secnum;
|
---|
| 1017 | $new_secnum =~ s/\.(\d+)$/\.$tail_num/;
|
---|
| 1018 | }
|
---|
| 1019 | else {
|
---|
| 1020 | # back up to parent section => lopp off tail
|
---|
| 1021 | $new_secnum = $old_secnum;
|
---|
| 1022 | $new_secnum =~ s/\.\d+$//;
|
---|
| 1023 | }
|
---|
| 1024 |
|
---|
| 1025 | $parser->{'parameters'}->{'curr_section_depth'} = $new_depth;
|
---|
| 1026 | $parser->{'parameters'}->{'curr_section_num'} = $new_secnum;
|
---|
| 1027 |
|
---|
| 1028 | 1;
|
---|
| 1029 | }
|
---|
| 1030 |
|
---|
| 1031 | sub edit_xml_file
|
---|
| 1032 | {
|
---|
| 1033 | my $self = shift @_;
|
---|
| 1034 | my ($gsdl_cgi, $filename, $start_rules, $rules, $options) = @_;
|
---|
| 1035 |
|
---|
| 1036 | # use XML::Rules to add it in (read in and out again)
|
---|
| 1037 | my $parser = XML::Rules->new(start_rules => $start_rules,
|
---|
| 1038 | rules => $rules,
|
---|
| 1039 | style => 'filter',
|
---|
| 1040 | output_encoding => 'utf8' );
|
---|
| 1041 |
|
---|
| 1042 | my $xml_in = "";
|
---|
| 1043 | if (!open(MIN,"<$filename")) {
|
---|
| 1044 | $gsdl_cgi->generate_error("Unable to read in $filename: $!");
|
---|
| 1045 | }
|
---|
| 1046 | else {
|
---|
| 1047 | # Read all the text in
|
---|
| 1048 | my $line;
|
---|
| 1049 | while (defined ($line=<MIN>)) {
|
---|
| 1050 | $xml_in .= $line;
|
---|
| 1051 | }
|
---|
| 1052 | close(MIN);
|
---|
| 1053 |
|
---|
| 1054 | my $MOUT;
|
---|
| 1055 | if (!open($MOUT,">$filename")) {
|
---|
| 1056 | $gsdl_cgi->generate_error("Unable to write out to $filename: $!");
|
---|
| 1057 | }
|
---|
| 1058 | else {
|
---|
| 1059 | # Matched lines will get handled by the call backs
|
---|
| 1060 | ## my $xml_out = "";
|
---|
| 1061 |
|
---|
| 1062 | binmode($MOUT,":utf8");
|
---|
| 1063 | $parser->filter($xml_in,$MOUT, $options);
|
---|
| 1064 |
|
---|
| 1065 | # binmode(MOUT,":utf8");
|
---|
| 1066 | # print MOUT $xml_out;
|
---|
| 1067 | close($MOUT);
|
---|
| 1068 | }
|
---|
| 1069 | }
|
---|
| 1070 |
|
---|
| 1071 | # copy across any custom error information that was stored during parsing
|
---|
| 1072 | $self->{'error_msg'} = $parser->{'custom_err_msg'} if(defined $parser->{'custom_err_msg'});
|
---|
| 1073 | }
|
---|
| 1074 |
|
---|
| 1075 | sub edit_doc_xml
|
---|
| 1076 | {
|
---|
| 1077 | my $self = shift @_;
|
---|
| 1078 | my ($gsdl_cgi, $doc_xml_filename, $metaname, $metavalue, $metapos, $metamode, $opt_secnum, $prevmetavalue) = @_;
|
---|
| 1079 |
|
---|
| 1080 | my $info_mess = <<RAWEND;
|
---|
| 1081 | ****************************
|
---|
| 1082 | edit_doc_xml()
|
---|
| 1083 | ****************************
|
---|
| 1084 | RAWEND
|
---|
| 1085 |
|
---|
| 1086 | $info_mess .= " doc_xml_filename = $doc_xml_filename\n" if defined($doc_xml_filename);
|
---|
| 1087 | $info_mess .= " metaname = $metaname\n" if defined($metaname);
|
---|
| 1088 | $info_mess .= " metapos = $metapos\n" if defined($metapos);
|
---|
| 1089 | $info_mess .= " metavalue = $metavalue\n" if defined($metavalue);
|
---|
| 1090 | $info_mess .= " metamode = $metamode\n" if defined($metamode);
|
---|
| 1091 | $info_mess .= " opt_secnum = $opt_secnum\n" if defined($opt_secnum);
|
---|
| 1092 | $info_mess .= " prevmetaval = $prevmetavalue\n" if defined($prevmetavalue);
|
---|
| 1093 |
|
---|
| 1094 | $info_mess .= "****************************\n";
|
---|
| 1095 |
|
---|
| 1096 | $gsdl_cgi->generate_message($info_mess);
|
---|
| 1097 |
|
---|
| 1098 | # To monitor which section/subsection number we are in
|
---|
| 1099 | my @start_rules =
|
---|
| 1100 | ( 'Section' => \&dxml_start_section );
|
---|
| 1101 |
|
---|
| 1102 | # use XML::Rules to add it in (read in and out again)
|
---|
| 1103 | # Set the call back functions
|
---|
| 1104 | my @rules =
|
---|
| 1105 | ( _default => 'raw',
|
---|
| 1106 | 'Metadata' => \&dxml_metadata,
|
---|
| 1107 | 'Description' => \&dxml_description,
|
---|
| 1108 | 'Archive' => \&dxml_archive); # just for catching errors at end
|
---|
| 1109 |
|
---|
| 1110 | # Sets the parameters
|
---|
| 1111 | my $options = { 'metaname' => $metaname,
|
---|
| 1112 | 'metapos' => $metapos,
|
---|
| 1113 | 'metavalue' => $metavalue,
|
---|
| 1114 | 'metamode' => $metamode,
|
---|
| 1115 | 'prevmetavalue' => $prevmetavalue };
|
---|
| 1116 |
|
---|
| 1117 | if (defined $opt_secnum) {
|
---|
| 1118 | $options->{'secnum'} = $opt_secnum;
|
---|
| 1119 | }
|
---|
| 1120 |
|
---|
| 1121 | $self->edit_xml_file($gsdl_cgi,$doc_xml_filename,\@start_rules,\@rules,$options);
|
---|
| 1122 | }
|
---|
| 1123 |
|
---|
| 1124 | sub set_archives_metadata_entry
|
---|
| 1125 | {
|
---|
| 1126 | my $self = shift @_;
|
---|
| 1127 | my ($gsdl_cgi, $archive_dir, $collect_dir, $collect, $infodbtype, $docid, $metaname, $metapos, $metavalue, $metamode, $prevmetavalue) = @_;
|
---|
| 1128 |
|
---|
| 1129 | my $info_mess = <<RAWEND;
|
---|
| 1130 | ****************************
|
---|
| 1131 | set_archives_metadata_entry()
|
---|
| 1132 | ****************************
|
---|
| 1133 | RAWEND
|
---|
| 1134 |
|
---|
| 1135 | $info_mess .= " archive_dir = $archive_dir\n" if defined($archive_dir);
|
---|
| 1136 | $info_mess .= " collect_dir = $collect_dir\n" if defined($collect_dir);
|
---|
| 1137 | $info_mess .= " collect = $collect\n" if defined($collect);
|
---|
| 1138 | $info_mess .= " infodbtype = $infodbtype\n" if defined($infodbtype);
|
---|
| 1139 | $info_mess .= " docid = $docid\n" if defined($docid);
|
---|
| 1140 | $info_mess .= " metaname = $metaname\n" if defined($metaname);
|
---|
| 1141 | $info_mess .= " metapos = $metapos\n" if defined($metapos);
|
---|
| 1142 | $info_mess .= " metavalue = $metavalue\n" if defined($metavalue);
|
---|
| 1143 | $info_mess .= " metamode = $metamode\n" if defined($metamode);
|
---|
| 1144 | $info_mess .= " prevmetaval = $prevmetavalue\n" if defined($prevmetavalue);
|
---|
| 1145 |
|
---|
| 1146 | $info_mess .= "****************************\n";
|
---|
| 1147 |
|
---|
| 1148 | $gsdl_cgi->generate_message($info_mess);
|
---|
| 1149 |
|
---|
| 1150 | # Obtain the doc.xml path for the specified docID
|
---|
| 1151 | my ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);
|
---|
| 1152 |
|
---|
| 1153 | my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
|
---|
| 1154 | my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid_root);
|
---|
| 1155 | my $doc_xml_file = $doc_rec->{'doc-file'}->[0];
|
---|
| 1156 |
|
---|
| 1157 | # The $doc_xml_file is relative to the archives, and now let's get the full path
|
---|
| 1158 | my $archives_dir = &util::filename_cat($collect_dir,$collect,"archives");
|
---|
| 1159 | my $doc_xml_filename = &util::filename_cat($archives_dir,$doc_xml_file);
|
---|
| 1160 |
|
---|
| 1161 | # If we're overriding everything, then $metamode=override combined with $metapos=undefined and $prevmetavalue=undefined
|
---|
| 1162 | # in which case, we need to remove all metavalues for the metaname at the given (sub)section
|
---|
| 1163 | # Thereafter, we will finally be setting the overriding metavalue for this metaname
|
---|
| 1164 | if (!defined $prevmetavalue && !defined $metapos && $metamode eq "override") {
|
---|
| 1165 | # remove all values of $metaname metadata
|
---|
| 1166 | $self->remove_from_doc_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_xml_file), $metaname, undef, undef, $docid_secnum, $metamode);
|
---|
| 1167 | }
|
---|
| 1168 | # Edit the doc.xml file with the specified metadata name, value and position.
|
---|
| 1169 | # TODO: there is a potential problem here as this edit_doc_xml function
|
---|
| 1170 | # is assuming the simple doc.xml situation where there is only one Section and no SubSections.
|
---|
| 1171 | # Running import.pl -groupsize will cause this to have multiple sections in one doc.xml
|
---|
| 1172 |
|
---|
| 1173 | # dxml_metadata method ignores metapos if metamode anything other than override
|
---|
| 1174 | $self->edit_doc_xml($gsdl_cgi,$doc_xml_filename,
|
---|
| 1175 | $metaname,$metavalue,$metapos,$metamode,$docid_secnum,$prevmetavalue);
|
---|
| 1176 |
|
---|
| 1177 | # return 0; # return 0 for now to indicate no error
|
---|
| 1178 | return (defined $self->{'error_msg'}) ? 1 : 0;
|
---|
| 1179 | }
|
---|
| 1180 |
|
---|
| 1181 |
|
---|
| 1182 | sub set_archives_metadata
|
---|
| 1183 | {
|
---|
| 1184 | my $self = shift @_;
|
---|
| 1185 |
|
---|
| 1186 | my $username = $self->{'username'};
|
---|
| 1187 | my $collect = $self->{'collect'};
|
---|
| 1188 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 1189 |
|
---|
| 1190 | if ($baseaction::authentication_enabled) {
|
---|
| 1191 | # Ensure the user is allowed to edit this collection
|
---|
| 1192 | $self->authenticate_user($username, $collect);
|
---|
| 1193 | }
|
---|
| 1194 |
|
---|
| 1195 | # Make sure the collection isn't locked by someone else
|
---|
| 1196 | $self->lock_collection($username, $collect);
|
---|
| 1197 |
|
---|
| 1198 | $self->_set_archives_metadata(@_);
|
---|
| 1199 |
|
---|
| 1200 | # Release the lock once it is done
|
---|
| 1201 | $self->unlock_collection($username, $collect);
|
---|
| 1202 | }
|
---|
| 1203 |
|
---|
| 1204 | sub _set_archives_metadata_array
|
---|
| 1205 | {
|
---|
| 1206 | my $self = shift @_;
|
---|
| 1207 |
|
---|
| 1208 | my $collect = $self->{'collect'};
|
---|
| 1209 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 1210 | my $site = $self->{'site'};
|
---|
| 1211 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 1212 |
|
---|
| 1213 | # look up additional args
|
---|
| 1214 |
|
---|
| 1215 | my $infodbtype = $self->{'infodbtype'};
|
---|
| 1216 |
|
---|
| 1217 | my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
|
---|
| 1218 |
|
---|
| 1219 | my $json_str = $self->{'json'};
|
---|
| 1220 | my $doc_array = decode_json $json_str;
|
---|
| 1221 |
|
---|
| 1222 |
|
---|
| 1223 | my $global_status = 0;
|
---|
| 1224 | my $global_mess = "";
|
---|
| 1225 |
|
---|
| 1226 | my @all_docids = ();
|
---|
| 1227 |
|
---|
| 1228 | foreach my $doc_array_rec ( @$doc_array ) {
|
---|
| 1229 | my $status = -1;
|
---|
| 1230 | my $docid = $doc_array_rec->{'docid'};
|
---|
| 1231 |
|
---|
| 1232 | push(@all_docids,$docid);
|
---|
| 1233 |
|
---|
| 1234 | my $metaname = $doc_array_rec->{'metaname'};
|
---|
| 1235 | if(defined $metaname) {
|
---|
| 1236 |
|
---|
| 1237 | my $metapos = $doc_array_rec->{'metapos'}; # don't force undef to 0. Undef has meaning when metamode=override
|
---|
| 1238 |
|
---|
| 1239 | my $metamode = $doc_array_rec->{'metamode'} || $self->{'metamode'};
|
---|
| 1240 | my $metavalue = $doc_array_rec->{'metavalue'};
|
---|
| 1241 | my $prevmetavalue = $self->{'prevmetavalue'}; # to make this sub behave as _set_archives_metadata
|
---|
| 1242 |
|
---|
| 1243 |
|
---|
| 1244 | if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
|
---|
| 1245 | # make "accumulate" the default (less destructive, as it won't actually
|
---|
| 1246 | # delete any existing values)
|
---|
| 1247 | $metamode = "accumulate";
|
---|
| 1248 | }
|
---|
| 1249 |
|
---|
| 1250 | $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid,
|
---|
| 1251 | $metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
|
---|
| 1252 | } elsif (defined $doc_array_rec->{'metatable'}) { # if no metaname, we expect a metatable
|
---|
| 1253 | my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
|
---|
| 1254 |
|
---|
| 1255 | foreach my $metatable_rec ( @$metatable ) {
|
---|
| 1256 | $metaname = $metatable_rec->{'metaname'};
|
---|
| 1257 | my $metamode = $metatable_rec->{'metamode'} || $doc_array_rec->{'metamode'} || $self->{'metamode'};
|
---|
| 1258 | my $metapos = undef;
|
---|
| 1259 | my $prevmetavalue = undef;
|
---|
| 1260 | my $metavals = $metatable_rec->{'metavals'}; # a sub-subarray
|
---|
| 1261 |
|
---|
| 1262 | foreach my $metavalue ( @$metavals ) {
|
---|
| 1263 | $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect,$infodbtype,
|
---|
| 1264 | $docid,$metaname,$metapos,$metavalue,$metamode,$prevmetavalue);
|
---|
| 1265 |
|
---|
| 1266 | if($metamode eq "override") { # now, having overridden the metavalue for the first,
|
---|
| 1267 | # need to accumulate subsequent metavals for this metaname, else the just-assigned
|
---|
| 1268 | # metavalue for this metaname will be lost
|
---|
| 1269 | $metamode = "accumulate";
|
---|
| 1270 | }
|
---|
| 1271 | }
|
---|
| 1272 | }
|
---|
| 1273 | }
|
---|
| 1274 |
|
---|
| 1275 | if ($status != 0) {
|
---|
| 1276 | # Catch error if set infodb entry failed
|
---|
| 1277 | $global_status = $status;
|
---|
| 1278 | $global_mess .= "Failed to set metadata key: $docid\n";
|
---|
| 1279 | $global_mess .= "Exit status: $status\n";
|
---|
| 1280 | $global_mess .= "System Error Message: $!\n";
|
---|
| 1281 | $global_mess .= "-" x 20 . "\n";
|
---|
| 1282 | }
|
---|
| 1283 | }
|
---|
| 1284 |
|
---|
| 1285 | if ($global_status != 0) {
|
---|
| 1286 | $global_mess .= "PATH: $ENV{'PATH'}\n";
|
---|
| 1287 | $gsdl_cgi->generate_error($global_mess);
|
---|
| 1288 | }
|
---|
| 1289 | else {
|
---|
| 1290 | my $mess = "set-archives-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n";
|
---|
| 1291 | $gsdl_cgi->generate_ok_message($mess);
|
---|
| 1292 | }
|
---|
| 1293 | }
|
---|
| 1294 |
|
---|
| 1295 | sub set_archives_metadata_array
|
---|
| 1296 | {
|
---|
| 1297 | my $self = shift @_;
|
---|
| 1298 |
|
---|
| 1299 | my $username = $self->{'username'};
|
---|
| 1300 | my $collect = $self->{'collect'};
|
---|
| 1301 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 1302 | # my $gsdlhome = $self->{'gsdlhome'};
|
---|
| 1303 |
|
---|
| 1304 | if ($baseaction::authentication_enabled) {
|
---|
| 1305 | # Ensure the user is allowed to edit this collection
|
---|
| 1306 | $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
|
---|
| 1307 | }
|
---|
| 1308 |
|
---|
| 1309 | my $site = $self->{'site'};
|
---|
| 1310 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 1311 |
|
---|
| 1312 | $gsdl_cgi->checked_chdir($collect_dir);
|
---|
| 1313 |
|
---|
| 1314 | # Obtain the collect dir
|
---|
| 1315 | ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
|
---|
| 1316 |
|
---|
| 1317 | # Make sure the collection isn't locked by someone else
|
---|
| 1318 | $self->lock_collection($username, $collect);
|
---|
| 1319 |
|
---|
| 1320 | $self->_set_archives_metadata_array(@_);
|
---|
| 1321 |
|
---|
| 1322 | # Release the lock once it is done
|
---|
| 1323 | $self->unlock_collection($username, $collect);
|
---|
| 1324 | }
|
---|
| 1325 |
|
---|
| 1326 | sub _remove_archives_metadata
|
---|
| 1327 | {
|
---|
| 1328 | my $self = shift @_;
|
---|
| 1329 |
|
---|
| 1330 | my $collect = $self->{'collect'};
|
---|
| 1331 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 1332 | # my $gsdlhome = $self->{'gsdlhome'};
|
---|
| 1333 | my $infodbtype = $self->{'infodbtype'};
|
---|
| 1334 |
|
---|
| 1335 | my $site = $self->{'site'};
|
---|
| 1336 |
|
---|
| 1337 | # Obtain the collect and archive dir
|
---|
| 1338 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 1339 |
|
---|
| 1340 | my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
|
---|
| 1341 |
|
---|
| 1342 | # look up additional args
|
---|
| 1343 | my ($docid, $docid_secnum) = ($self->{'d'} =~ m/^(.*?)(\..*)?$/);
|
---|
| 1344 |
|
---|
| 1345 | my $metaname = $self->{'metaname'};
|
---|
| 1346 | my $metapos = $self->{'metapos'};
|
---|
| 1347 | my $metavalue = $self->{'metavalue'};
|
---|
| 1348 |
|
---|
| 1349 | $metapos = undef if(defined $metapos && ($metapos =~ m/^\s*$/));
|
---|
| 1350 | $metavalue = undef if(defined $metavalue && ($metavalue =~ m/^\s*$/)); # necessary to force fallback to undef here
|
---|
| 1351 |
|
---|
| 1352 | # if the user hasn't told us what to delete, not having given a metavalue or metapos,
|
---|
| 1353 | # default to deleting the first metavalue for the given metaname
|
---|
| 1354 | # Beware that if both metapos AND metavalue are defined, both matches (if any)
|
---|
| 1355 | # seem to get deleted in one single remove_archives_meta action invocation.
|
---|
| 1356 | # Similarly, if 2 identical metavalues for a metaname exist and that metavalue is being
|
---|
| 1357 | # deleted, both get deleted.
|
---|
| 1358 | if(!defined $metapos && !defined $metavalue) {
|
---|
| 1359 | $metapos = 0;
|
---|
| 1360 | }
|
---|
| 1361 |
|
---|
| 1362 | my $metamode = $self->{'metamode'};
|
---|
| 1363 | $metamode = undef if(defined $metamode && ($metamode =~ m/^\s*$/));
|
---|
| 1364 |
|
---|
| 1365 | my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
|
---|
| 1366 | my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
|
---|
| 1367 |
|
---|
| 1368 | # This now stores the full pathname
|
---|
| 1369 | my $doc_filename = $doc_rec->{'doc-file'}->[0];
|
---|
| 1370 |
|
---|
| 1371 | my $status = $self->remove_from_doc_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_filename), $metaname, $metapos, $metavalue, $docid_secnum, $metamode);
|
---|
| 1372 | # my $status = $self->remove_from_doc_xml($gsdl_cgi, &util::filename_cat($archive_dir, $doc_filename), $metaname, $metapos, undef, $docid_secnum);
|
---|
| 1373 |
|
---|
| 1374 | if ($status == 0)
|
---|
| 1375 | {
|
---|
| 1376 | my $mess = "\nremove-archives-metadata successful: \nKey[$docid]\n";
|
---|
| 1377 | $mess .= " $metaname";
|
---|
| 1378 | $mess .= "->[$metapos]" if (defined $metapos);
|
---|
| 1379 | $mess .= " ($metavalue)" if (defined $metavalue);
|
---|
| 1380 | $gsdl_cgi->generate_ok_message($mess);
|
---|
| 1381 | }
|
---|
| 1382 | else
|
---|
| 1383 | {
|
---|
| 1384 | my $mess .= "Failed to remove archives metadata key: $docid\n";
|
---|
| 1385 | $mess .= "Exit status: $status\n";
|
---|
| 1386 | $mess .= "System Error Message: $!\n";
|
---|
| 1387 | $mess .= "-" x 20 . "\n";
|
---|
| 1388 |
|
---|
| 1389 | $gsdl_cgi->generate_error($mess);
|
---|
| 1390 | }
|
---|
| 1391 |
|
---|
| 1392 | #return $status; # in case calling functions have a use for this
|
---|
| 1393 | }
|
---|
| 1394 |
|
---|
| 1395 | sub remove_archives_metadata
|
---|
| 1396 | {
|
---|
| 1397 | my $self = shift @_;
|
---|
| 1398 |
|
---|
| 1399 | my $username = $self->{'username'};
|
---|
| 1400 | my $collect = $self->{'collect'};
|
---|
| 1401 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 1402 |
|
---|
| 1403 | if ($baseaction::authentication_enabled)
|
---|
| 1404 | {
|
---|
| 1405 | # Ensure the user is allowed to edit this collection
|
---|
| 1406 | $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
|
---|
| 1407 | }
|
---|
| 1408 |
|
---|
| 1409 | # Make sure the collection isn't locked by someone else
|
---|
| 1410 | $self->lock_collection($username, $collect);
|
---|
| 1411 |
|
---|
| 1412 | $self->_remove_archives_metadata(@_);
|
---|
| 1413 |
|
---|
| 1414 | # Release the lock once it is done
|
---|
| 1415 | $self->unlock_collection($username, $collect);
|
---|
| 1416 | }
|
---|
| 1417 |
|
---|
| 1418 | sub remove_from_doc_xml
|
---|
| 1419 | {
|
---|
| 1420 | my $self = shift @_;
|
---|
| 1421 | my ($gsdl_cgi, $doc_xml_filename, $metaname, $metapos, $metavalue, $secid, $metamode) = @_;
|
---|
| 1422 |
|
---|
| 1423 | my @start_rules = ('Section' => \&dxml_start_section);
|
---|
| 1424 |
|
---|
| 1425 | # Set the call-back functions for the metadata tags
|
---|
| 1426 | my @rules =
|
---|
| 1427 | (
|
---|
| 1428 | _default => 'raw',
|
---|
| 1429 | 'Metadata' => \&rfdxml_metadata
|
---|
| 1430 | );
|
---|
| 1431 |
|
---|
| 1432 | my $parser = XML::Rules->new
|
---|
| 1433 | (
|
---|
| 1434 | start_rules => \@start_rules,
|
---|
| 1435 | rules => \@rules,
|
---|
| 1436 | style => 'filter',
|
---|
| 1437 | output_encoding => 'utf8',
|
---|
| 1438 | # normalisespaces => 1, # http://search.cpan.org/~jenda/XML-Rules-1.16/lib/XML/Rules.pm
|
---|
| 1439 | stripspaces => 2|0|0 # ineffectual
|
---|
| 1440 | );
|
---|
| 1441 |
|
---|
| 1442 | my $status = 0;
|
---|
| 1443 | my $xml_in = "";
|
---|
| 1444 | if (!open(MIN,"<$doc_xml_filename"))
|
---|
| 1445 | {
|
---|
| 1446 | $gsdl_cgi->generate_error("Unable to read in $doc_xml_filename: $!");
|
---|
| 1447 | $status = 1;
|
---|
| 1448 | }
|
---|
| 1449 | else
|
---|
| 1450 | {
|
---|
| 1451 | # Read them in
|
---|
| 1452 | my $line;
|
---|
| 1453 | while (defined ($line=<MIN>)) {
|
---|
| 1454 | $xml_in .= $line;
|
---|
| 1455 | }
|
---|
| 1456 | close(MIN);
|
---|
| 1457 |
|
---|
| 1458 | # Filter with the call-back functions
|
---|
| 1459 | my $xml_out = "";
|
---|
| 1460 |
|
---|
| 1461 | my $MOUT;
|
---|
| 1462 | if (!open($MOUT,">$doc_xml_filename")) {
|
---|
| 1463 | $gsdl_cgi->generate_error("Unable to write out to $doc_xml_filename: $!");
|
---|
| 1464 | $status = 1;
|
---|
| 1465 | }
|
---|
| 1466 | else {
|
---|
| 1467 | binmode($MOUT,":utf8");
|
---|
| 1468 | $parser->filter($xml_in, $MOUT, {metaname => $metaname, metapos => $metapos, metavalue => $metavalue, secid => $secid, metamode => $metamode});
|
---|
| 1469 | close($MOUT);
|
---|
| 1470 | }
|
---|
| 1471 | }
|
---|
| 1472 | return $status;
|
---|
| 1473 | }
|
---|
| 1474 |
|
---|
| 1475 | sub rfdxml_metadata
|
---|
| 1476 | {
|
---|
| 1477 | my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
|
---|
| 1478 |
|
---|
| 1479 | # For comparisons, toplevel section is indicated by ""
|
---|
| 1480 | my $curr_sec_num = $parser->{'parameters'}->{'curr_section_num'} || "";
|
---|
| 1481 | my $secid = $parser->{'parameters'}->{'secid'} || "";
|
---|
| 1482 |
|
---|
| 1483 | if (!($secid eq $curr_sec_num))
|
---|
| 1484 | {
|
---|
| 1485 | # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
|
---|
| 1486 | return [$tagname => $attrHash];
|
---|
| 1487 | }
|
---|
| 1488 |
|
---|
| 1489 | if ($parser->{'parameters'}->{'metaname'} eq $attrHash->{'name'})
|
---|
| 1490 | {
|
---|
| 1491 | if (!defined $parser->{'parameters'}->{'poscount'})
|
---|
| 1492 | {
|
---|
| 1493 | $parser->{'parameters'}->{'poscount'} = 0;
|
---|
| 1494 | }
|
---|
| 1495 | else
|
---|
| 1496 | {
|
---|
| 1497 | $parser->{'parameters'}->{'poscount'}++;
|
---|
| 1498 | }
|
---|
| 1499 |
|
---|
| 1500 | # if overriding (for set-meta) but no metapos, then clear all the meta for this metaname
|
---|
| 1501 | if ((defined $parser->{'parameters'}->{'metamode'}) && ($parser->{'parameters'}->{'metamode'} eq "override") && (!defined $parser->{'parameters'}->{'metapos'}) &&(!defined $parser->{'parameters'}->{'metavalue'})) {
|
---|
| 1502 | return [];
|
---|
| 1503 | }
|
---|
| 1504 |
|
---|
| 1505 | if ((defined $parser->{'parameters'}->{'metapos'}) && ($parser->{'parameters'}->{'poscount'} == $parser->{'parameters'}->{'metapos'}))
|
---|
| 1506 | {
|
---|
| 1507 | return [];
|
---|
| 1508 | }
|
---|
| 1509 |
|
---|
| 1510 | if ((defined $parser->{'parameters'}->{'metavalue'}) && ($parser->{'parameters'}->{'metavalue'} eq $attrHash->{'_content'}))
|
---|
| 1511 | {
|
---|
| 1512 | return [];
|
---|
| 1513 | }
|
---|
| 1514 | }
|
---|
| 1515 |
|
---|
| 1516 | # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
|
---|
| 1517 | return [$tagname => $attrHash];
|
---|
| 1518 | }
|
---|
| 1519 |
|
---|
| 1520 | sub mxml_metadata
|
---|
| 1521 | {
|
---|
| 1522 | my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
|
---|
| 1523 | my $metaname = $parser->{'parameters'}->{'metaname'};
|
---|
| 1524 | my $metamode = $parser->{'parameters'}->{'metamode'};
|
---|
| 1525 |
|
---|
| 1526 | # Report error if we don't see FileName tag before this
|
---|
| 1527 | die "Fatal Error: Unexpected metadata.xml structure. Undefined current_file, possibly encountered Description before FileName" if (!defined($parser->{'parameters'}->{'current_file'}));
|
---|
| 1528 |
|
---|
| 1529 | # Don't do anything if we are not in the right FileSet
|
---|
| 1530 | my $file_regexp = $parser->{'parameters'}->{'current_file'};
|
---|
| 1531 | if ($file_regexp =~ /\.\*/) {
|
---|
| 1532 | # Only interested in a file_regexp if it specifies precisely one
|
---|
| 1533 | # file.
|
---|
| 1534 | # So, skip anything with a .* in it as it is too general
|
---|
| 1535 | ## print STDERR "@@@@ Skipping entry in metadata.xml where FileName=.* as it is too general\n";
|
---|
| 1536 | return [$tagname => $attrHash];
|
---|
| 1537 | }
|
---|
| 1538 | my $src_file = $parser->{'parameters'}->{'src_file'};
|
---|
| 1539 | if (!($src_file =~ /$file_regexp/)) {
|
---|
| 1540 | return [$tagname => $attrHash];
|
---|
| 1541 | }
|
---|
| 1542 | ## print STDERR "*** mxl metamode = $metamode\n";
|
---|
| 1543 |
|
---|
| 1544 | # Find the right metadata tag and checks if we are going to override it
|
---|
| 1545 | my $name_attr = $attrHash->{'name'};
|
---|
| 1546 | if (($name_attr eq $metaname) && ($metamode eq "override")) {
|
---|
| 1547 |
|
---|
| 1548 | # now metadata.xml functions need to keep track of metapos
|
---|
| 1549 | if (!defined $parser->{'parameters'}->{'poscount'})
|
---|
| 1550 | {
|
---|
| 1551 | $parser->{'parameters'}->{'poscount'} = 0;
|
---|
| 1552 | }
|
---|
| 1553 | else
|
---|
| 1554 | {
|
---|
| 1555 | $parser->{'parameters'}->{'poscount'}++;
|
---|
| 1556 | }
|
---|
| 1557 |
|
---|
| 1558 | # If either the metapos or prevmetavalue is set,
|
---|
| 1559 | # get the value and override the current value
|
---|
| 1560 | my $metavalue = $parser->{'parameters'}->{'metavalue'};
|
---|
| 1561 |
|
---|
| 1562 | if(defined $parser->{'parameters'}->{'prevmetavalue'} && $parser->{'parameters'}->{'prevmetavalue'} eq $attrHash->{'_content'})
|
---|
| 1563 | {
|
---|
| 1564 | $attrHash->{'_content'} = $metavalue;
|
---|
| 1565 |
|
---|
| 1566 | ## print STDERR "**** overriding metadata.xml\n";
|
---|
| 1567 |
|
---|
| 1568 | # Don't want it to wipe out any other pieces of metadata
|
---|
| 1569 | $parser->{'parameters'}->{'metamode'} = "done";
|
---|
| 1570 | }
|
---|
| 1571 | elsif(defined $parser->{'parameters'}->{'metapos'} && $parser->{'parameters'}->{'poscount'} == $parser->{'parameters'}->{'metapos'})
|
---|
| 1572 | {
|
---|
| 1573 | $attrHash->{'_content'} = $metavalue;
|
---|
| 1574 | $parser->{'parameters'}->{'metamode'} = "done";
|
---|
| 1575 | }
|
---|
| 1576 | }
|
---|
| 1577 |
|
---|
| 1578 | # mxml_description will process the metadata if metadata is accumulate,
|
---|
| 1579 | # or if we haven't found the metadata to override
|
---|
| 1580 |
|
---|
| 1581 | # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
|
---|
| 1582 | return [$tagname => $attrHash];
|
---|
| 1583 | }
|
---|
| 1584 |
|
---|
| 1585 |
|
---|
| 1586 | sub mxml_description
|
---|
| 1587 | {
|
---|
| 1588 | my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
|
---|
| 1589 | my $metamode = $parser->{'parameters'}->{'metamode'};
|
---|
| 1590 |
|
---|
| 1591 | # Failed... Report error if we don't see FileName tag before this
|
---|
| 1592 | die "Fatal Error: Unexpected metadata.xml structure. Undefind current_file, possiblely encountered Description before FileName" if (!defined($parser->{'parameters'}->{'current_file'}));
|
---|
| 1593 |
|
---|
| 1594 | # Don't do anything if we are not in the right FileSet
|
---|
| 1595 | my $file_regexp = $parser->{'parameters'}->{'current_file'};
|
---|
| 1596 | if ($file_regexp =~ m/\.\*/) {
|
---|
| 1597 | # Only interested in a file_regexp if it specifies precisely one
|
---|
| 1598 | # file.
|
---|
| 1599 | # So, skip anything with a .* in it as it is too general
|
---|
| 1600 | return [$tagname => $attrHash];
|
---|
| 1601 | }
|
---|
| 1602 | my $src_file = $parser->{'parameters'}->{'src_file'};
|
---|
| 1603 |
|
---|
| 1604 | if (!($src_file =~ m/$file_regexp/)) {
|
---|
| 1605 | return [$tagname => $attrHash];
|
---|
| 1606 | }
|
---|
| 1607 |
|
---|
| 1608 | # Accumulate the metadata block to the end of the description block
|
---|
| 1609 | # Note: This adds metadata block to all description blocks, so if there are
|
---|
| 1610 | # multiple FileSets, it will add to all of them
|
---|
| 1611 | if (($metamode eq "accumulate") || ($metamode eq "override")) {
|
---|
| 1612 |
|
---|
| 1613 | # if metamode was "override" but get to here then it failed to
|
---|
| 1614 | # find an item to override, in which case it should append its
|
---|
| 1615 | # value to the end, just like the "accumulate" mode
|
---|
| 1616 |
|
---|
| 1617 | if ($metamode eq "override") {
|
---|
| 1618 | print "No metadata value to override. Switching 'metamode' to accumulate\n";
|
---|
| 1619 | }
|
---|
| 1620 |
|
---|
| 1621 | # tack a new metadata tag on to the end of the <Metadata>+ block
|
---|
| 1622 | my $metaname = $parser->{'parameters'}->{'metaname'};
|
---|
| 1623 | my $metavalue = $parser->{'parameters'}->{'metavalue'};
|
---|
| 1624 |
|
---|
| 1625 | my $metadata_attr = { '_content' => $metavalue,
|
---|
| 1626 | 'name' => $metaname,
|
---|
| 1627 | 'mode' => "accumulate" };
|
---|
| 1628 |
|
---|
| 1629 | my $append_metadata = [ "Metadata" => $metadata_attr ];
|
---|
| 1630 | my $description_content = $attrHash->{'_content'};
|
---|
| 1631 |
|
---|
| 1632 | ## print STDERR "*** appending to metadata.xml\n";
|
---|
| 1633 |
|
---|
| 1634 | # append the new metadata element to the end of the current
|
---|
| 1635 | # content contained inside this tag
|
---|
| 1636 | if (ref($description_content) eq "") {
|
---|
| 1637 | # => string or numeric literal
|
---|
| 1638 | # this is caused by a <Description> block has no <Metadata> child elements
|
---|
| 1639 | # => set up an empty array in '_content'
|
---|
| 1640 | $attrHash->{'_content'} = [ "\n" ];
|
---|
| 1641 | $description_content = $attrHash->{'_content'};
|
---|
| 1642 | }
|
---|
| 1643 |
|
---|
| 1644 | push(@$description_content," ", $append_metadata ,"\n ");
|
---|
| 1645 | $parser->{'parameters'}->{'metamode'} = "done";
|
---|
| 1646 | }
|
---|
| 1647 |
|
---|
| 1648 | # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
|
---|
| 1649 | return [$tagname => $attrHash];
|
---|
| 1650 | }
|
---|
| 1651 |
|
---|
| 1652 |
|
---|
| 1653 | sub mxml_filename
|
---|
| 1654 | {
|
---|
| 1655 | my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
|
---|
| 1656 |
|
---|
| 1657 | # Store the filename of the Current Fileset
|
---|
| 1658 | # Note: According to http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd
|
---|
| 1659 | # FileName tag must come before Description tag
|
---|
| 1660 | $parser->{'parameters'}->{'current_file'} = $attrHash->{'_content'};
|
---|
| 1661 |
|
---|
| 1662 | # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
|
---|
| 1663 | return [$tagname => $attrHash];
|
---|
| 1664 | }
|
---|
| 1665 |
|
---|
| 1666 |
|
---|
| 1667 | sub mxml_fileset
|
---|
| 1668 | {
|
---|
| 1669 | my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
|
---|
| 1670 |
|
---|
| 1671 | # Initilise the current_file
|
---|
| 1672 | # Note: According to http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd
|
---|
| 1673 | # FileName tag must come before Description tag
|
---|
| 1674 | $parser->{'parameters'}->{'current_file'} = "";
|
---|
| 1675 |
|
---|
| 1676 | # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
|
---|
| 1677 | return [$tagname => $attrHash];
|
---|
| 1678 | }
|
---|
| 1679 |
|
---|
| 1680 | sub mxml_directorymetadata
|
---|
| 1681 | {
|
---|
| 1682 | my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
|
---|
| 1683 |
|
---|
| 1684 | # if we haven't processed the metadata when we reach the end of metadata.xml
|
---|
| 1685 | # it's because there's no particular FileSet element whose FileName matched
|
---|
| 1686 | # In which case, add a new FileSet for this FileName
|
---|
| 1687 | my $metamode = $parser->{'parameters'}->{'metamode'};
|
---|
| 1688 | if($metamode ne "done") {
|
---|
| 1689 |
|
---|
| 1690 | if ($metamode eq "override") {
|
---|
| 1691 | print "No metadata value to override. Switching 'metamode' to accumulate\n";
|
---|
| 1692 | }
|
---|
| 1693 |
|
---|
| 1694 | # If we get to here and metamode is override, this means there
|
---|
| 1695 | # was no existing value to overide => treat as an append operation
|
---|
| 1696 |
|
---|
| 1697 | # Create a new FileSet element and append to DirectoryMetadata
|
---|
| 1698 | # <FileSet>
|
---|
| 1699 | # <FileName>src_file</FileName>
|
---|
| 1700 | # <Description>
|
---|
| 1701 | # <Metadata mode="" name="">metavalue</Metadata>
|
---|
| 1702 | # </Description>
|
---|
| 1703 | # </FileSet>
|
---|
| 1704 | my $src_file = $parser->{'parameters'}->{'src_file'};
|
---|
| 1705 | my $metaname = $parser->{'parameters'}->{'metaname'};
|
---|
| 1706 | my $metavalue = $parser->{'parameters'}->{'metavalue'};
|
---|
| 1707 | my $metadata_attr = {
|
---|
| 1708 | '_content' => $metavalue,
|
---|
| 1709 | 'name' => $metaname,
|
---|
| 1710 | 'mode' => "accumulate"
|
---|
| 1711 | };
|
---|
| 1712 | my $append_metadata = [ "Metadata" => $metadata_attr ];
|
---|
| 1713 | my $description_attr = { '_content' => [ "\n\t\t ", $append_metadata, "\n\t\t"] };
|
---|
| 1714 | my $description_element = [ "Description" => $description_attr ];
|
---|
| 1715 |
|
---|
| 1716 | #_content is not an attribute, it's special and holds the children of this element
|
---|
| 1717 | # including the textnode value embedded in this element if any.
|
---|
| 1718 | my $filename_attr = {'_content' => $src_file};
|
---|
| 1719 | my $filename_element = [ "FileName" => $filename_attr ];
|
---|
| 1720 |
|
---|
| 1721 | my $fileset_attr = {};
|
---|
| 1722 | $fileset_attr->{'_content'} = [ "\n\t\t", $filename_element,"\n\t\t",$description_element ,"\n\t" ];
|
---|
| 1723 | my $fileset = [ "FileSet" => $fileset_attr ]; #my $fileset = [ "FileSet" => {} ];
|
---|
| 1724 |
|
---|
| 1725 |
|
---|
| 1726 | # get children of dirmeta, and push the new FileSet element onto it
|
---|
| 1727 | print "Appending metadata to metadata.xml\n";
|
---|
| 1728 | my $dirmeta_content = $attrHash->{'_content'};
|
---|
| 1729 | if (ref($dirmeta_content)) {
|
---|
| 1730 | # got some existing interesting nested content
|
---|
| 1731 | #push(@$dirmeta_content, " ", $fileset ,"\n ");
|
---|
| 1732 | push(@$dirmeta_content, "\t", $fileset ,"\n");
|
---|
| 1733 | }
|
---|
| 1734 | else {
|
---|
| 1735 | #description_content is most likely a string such as "\n"
|
---|
| 1736 | #$attrHash->{'_content'} = [$dirmeta_content, " ", $fileset ,"\n" ];
|
---|
| 1737 | $attrHash->{'_content'} = [$dirmeta_content, "\t", $fileset ,"\n" ];
|
---|
| 1738 | }
|
---|
| 1739 |
|
---|
| 1740 | $parser->{'parameters'}->{'metamode'} = "done";
|
---|
| 1741 | }
|
---|
| 1742 | # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
|
---|
| 1743 | return [$tagname => $attrHash];
|
---|
| 1744 | }
|
---|
| 1745 |
|
---|
| 1746 |
|
---|
| 1747 | sub edit_metadata_xml
|
---|
| 1748 | {
|
---|
| 1749 | my $self = shift @_;
|
---|
| 1750 | my ($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $metavalue, $metamode, $src_file, $prevmetavalue) = @_;
|
---|
| 1751 |
|
---|
| 1752 | # Set the call-back functions for the metadata tags
|
---|
| 1753 | my @rules =
|
---|
| 1754 | ( _default => 'raw',
|
---|
| 1755 | 'FileName' => \&mxml_filename,
|
---|
| 1756 | 'Metadata' => \&mxml_metadata,
|
---|
| 1757 | 'Description' => \&mxml_description,
|
---|
| 1758 | 'FileSet' => \&mxml_fileset,
|
---|
| 1759 | 'DirectoryMetadata' => \&mxml_directorymetadata);
|
---|
| 1760 |
|
---|
| 1761 | # use XML::Rules to add it in (read in and out again)
|
---|
| 1762 | my $parser = XML::Rules->new(rules => \@rules,
|
---|
| 1763 | style => 'filter',
|
---|
| 1764 | output_encoding => 'utf8',
|
---|
| 1765 | stripspaces => 2|0|0); # http://search.cpan.org/~jenda/XML-Rules-1.16/lib/XML/Rules.pm
|
---|
| 1766 |
|
---|
| 1767 | if (!-e $metadata_xml_filename) {
|
---|
| 1768 |
|
---|
| 1769 | if (open(MOUT,">$metadata_xml_filename")) {
|
---|
| 1770 |
|
---|
| 1771 | my $src_file_re = &util::filename_to_regex($src_file);
|
---|
| 1772 | # shouldn't the following also be in the above utility routine??
|
---|
| 1773 | # $src_file_re =~ s/\./\\./g;
|
---|
| 1774 |
|
---|
| 1775 | print MOUT "<?xml version=\"1.0\"?>\n";
|
---|
| 1776 | print MOUT "<DirectoryMetadata>\n";
|
---|
| 1777 | print MOUT " <FileSet>\n";
|
---|
| 1778 | print MOUT " <FileName>$src_file_re</FileName>\n";
|
---|
| 1779 | print MOUT " <Description>\n";
|
---|
| 1780 | print MOUT " </Description>\n";
|
---|
| 1781 | print MOUT " </FileSet>\n";
|
---|
| 1782 | print MOUT "</DirectoryMetadata>\n";
|
---|
| 1783 |
|
---|
| 1784 | close(MOUT);
|
---|
| 1785 | }
|
---|
| 1786 | else {
|
---|
| 1787 | $gsdl_cgi->generate_error("Unable to create $metadata_xml_filename: $!");
|
---|
| 1788 | }
|
---|
| 1789 | }
|
---|
| 1790 |
|
---|
| 1791 |
|
---|
| 1792 | my $xml_in = "";
|
---|
| 1793 | if (!open(MIN,"<$metadata_xml_filename")) {
|
---|
| 1794 | $gsdl_cgi->generate_error("Unable to read in $metadata_xml_filename: $!");
|
---|
| 1795 | }
|
---|
| 1796 | else {
|
---|
| 1797 | # Read them in
|
---|
| 1798 | my $line;
|
---|
| 1799 | while (defined ($line=<MIN>)) {
|
---|
| 1800 | $xml_in .= $line;
|
---|
| 1801 | }
|
---|
| 1802 | close(MIN);
|
---|
| 1803 |
|
---|
| 1804 | # Filter with the call-back functions
|
---|
| 1805 | my $xml_out = "";
|
---|
| 1806 |
|
---|
| 1807 | my $MOUT;
|
---|
| 1808 | if (!open($MOUT,">$metadata_xml_filename")) {
|
---|
| 1809 | $gsdl_cgi->generate_error("Unable to write out to $metadata_xml_filename: $!");
|
---|
| 1810 | }
|
---|
| 1811 | else {
|
---|
| 1812 | binmode($MOUT,":utf8");
|
---|
| 1813 |
|
---|
| 1814 | # Some wise person please find out how to keep the DTD and encode lines in after it gets filtered by this XML::Rules
|
---|
| 1815 | # At the moment, I will just hack it!
|
---|
| 1816 | #my $header_with_utf8_dtd = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
|
---|
| 1817 | #$header_with_utf8_dtd .= "<!DOCTYPE DirectoryMetadata SYSTEM \"http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd\">";
|
---|
| 1818 | #$xml_out =~ s/\<\?xml\sversion\=\"1.0\"\?\>/$header_with_utf8_dtd/;
|
---|
| 1819 | #print MOUT $xml_out;
|
---|
| 1820 |
|
---|
| 1821 | $parser->filter($xml_in, $MOUT, { metaname => $metaname,
|
---|
| 1822 | metapos => $metapos,
|
---|
| 1823 | metavalue => $metavalue,
|
---|
| 1824 | metamode => $metamode,
|
---|
| 1825 | src_file => $src_file,
|
---|
| 1826 | prevmetavalue => $prevmetavalue,
|
---|
| 1827 | current_file => undef} );
|
---|
| 1828 | close($MOUT);
|
---|
| 1829 | }
|
---|
| 1830 | }
|
---|
| 1831 | }
|
---|
| 1832 |
|
---|
| 1833 |
|
---|
| 1834 | sub set_import_metadata
|
---|
| 1835 | {
|
---|
| 1836 | my $self = shift @_;
|
---|
| 1837 |
|
---|
| 1838 | my $username = $self->{'username'};
|
---|
| 1839 | my $collect = $self->{'collect'};
|
---|
| 1840 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 1841 |
|
---|
| 1842 | if ($baseaction::authentication_enabled) {
|
---|
| 1843 | # Ensure the user is allowed to edit this collection
|
---|
| 1844 | $self->authenticate_user($username, $collect);
|
---|
| 1845 | }
|
---|
| 1846 |
|
---|
| 1847 | # Make sure the collection isn't locked by someone else
|
---|
| 1848 | $self->lock_collection($username, $collect);
|
---|
| 1849 |
|
---|
| 1850 | $self->_set_import_metadata(@_);
|
---|
| 1851 |
|
---|
| 1852 | # Release the lock once it is done
|
---|
| 1853 | $self->unlock_collection($username, $collect);
|
---|
| 1854 |
|
---|
| 1855 | }
|
---|
| 1856 |
|
---|
| 1857 | sub set_import_metadata_array
|
---|
| 1858 | {
|
---|
| 1859 | my $self = shift @_;
|
---|
| 1860 |
|
---|
| 1861 | my $username = $self->{'username'};
|
---|
| 1862 | my $collect = $self->{'collect'};
|
---|
| 1863 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 1864 | # my $gsdlhome = $self->{'gsdlhome'};
|
---|
| 1865 |
|
---|
| 1866 | if ($baseaction::authentication_enabled) {
|
---|
| 1867 | # Ensure the user is allowed to edit this collection
|
---|
| 1868 | $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
|
---|
| 1869 | }
|
---|
| 1870 |
|
---|
| 1871 | my $site = $self->{'site'};
|
---|
| 1872 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 1873 |
|
---|
| 1874 | $gsdl_cgi->checked_chdir($collect_dir);
|
---|
| 1875 |
|
---|
| 1876 | # Make sure the collection isn't locked by someone else
|
---|
| 1877 | $self->lock_collection($username, $collect);
|
---|
| 1878 |
|
---|
| 1879 | $self->_set_import_metadata_array(@_);
|
---|
| 1880 |
|
---|
| 1881 | # Release the lock once it is done
|
---|
| 1882 | $self->unlock_collection($username, $collect);
|
---|
| 1883 |
|
---|
| 1884 | }
|
---|
| 1885 |
|
---|
| 1886 |
|
---|
| 1887 | sub _set_import_metadata_array
|
---|
| 1888 | {
|
---|
| 1889 | my $self = shift @_;
|
---|
| 1890 |
|
---|
| 1891 | my $collect = $self->{'collect'};
|
---|
| 1892 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 1893 |
|
---|
| 1894 | my $site = $self->{'site'};
|
---|
| 1895 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 1896 |
|
---|
| 1897 | # look up additional args
|
---|
| 1898 |
|
---|
| 1899 | my $infodbtype = $self->{'infodbtype'};
|
---|
| 1900 |
|
---|
| 1901 | my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
|
---|
| 1902 | my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
|
---|
| 1903 |
|
---|
| 1904 | my $json_str = $self->{'json'};
|
---|
| 1905 | my $doc_array = decode_json $json_str;
|
---|
| 1906 |
|
---|
| 1907 | my $global_status = 0;
|
---|
| 1908 | my $global_mess = "";
|
---|
| 1909 |
|
---|
| 1910 | my @all_docids = ();
|
---|
| 1911 |
|
---|
| 1912 | foreach my $doc_array_rec ( @$doc_array )
|
---|
| 1913 | {
|
---|
| 1914 | my $status = -1;
|
---|
| 1915 | my $docid = $doc_array_rec->{'docid'};
|
---|
| 1916 |
|
---|
| 1917 | my ($docid_root,$docid_secnum);
|
---|
| 1918 | if(defined $docid) {
|
---|
| 1919 | ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);
|
---|
| 1920 | # as yet no support for setting subsection metadata in metadata.xml
|
---|
| 1921 | if ((defined $docid_secnum) && ($docid_secnum !~ m/^\s*$/)) {
|
---|
| 1922 | $gsdl_cgi->generate_message("*** docid: $docid. No support yet for setting import metadata at subsections level.\n");
|
---|
| 1923 | next; # skip this docid in for loop
|
---|
| 1924 | }
|
---|
| 1925 | }
|
---|
| 1926 |
|
---|
| 1927 | push(@all_docids,$docid); # docid_root rather
|
---|
| 1928 |
|
---|
| 1929 | my $metaname = $doc_array_rec->{'metaname'};
|
---|
| 1930 | if (defined $metaname) {
|
---|
| 1931 | my $metamode = $doc_array_rec->{'metamode'} || $self->{'metamode'};
|
---|
| 1932 | my $metavalue = $doc_array_rec->{'metavalue'};
|
---|
| 1933 | $metavalue =~ s/<(.*?)>/<$1>/g;
|
---|
| 1934 |
|
---|
| 1935 | if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
|
---|
| 1936 | # make "accumulate" the default (less destructive, as it won't actually
|
---|
| 1937 | # delete any existing values)
|
---|
| 1938 | $metamode = "accumulate";
|
---|
| 1939 | }
|
---|
| 1940 |
|
---|
| 1941 | # adding metapos and prevmetavalue support to import_metadata subroutines
|
---|
| 1942 | my $metapos = $doc_array_rec->{'metapos'}; # don't force undef to 0. Undef has meaning when metamode=override
|
---|
| 1943 | my $prevmetavalue = $self->{'prevmetavalue'};
|
---|
| 1944 |
|
---|
| 1945 | $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
|
---|
| 1946 |
|
---|
| 1947 | } elsif (defined $doc_array_rec->{'metatable'}) { # if no metaname, we expect a metatable
|
---|
| 1948 | my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
|
---|
| 1949 |
|
---|
| 1950 | foreach my $metatable_rec ( @$metatable ) {
|
---|
| 1951 | $metaname = $metatable_rec->{'metaname'};
|
---|
| 1952 | my $metamode = $metatable_rec->{'metamode'} || $doc_array_rec->{'metamode'} || $self->{'metamode'};
|
---|
| 1953 | if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
|
---|
| 1954 | # make "accumulate" the default (less destructive, as it won't actually
|
---|
| 1955 | # delete any existing values)
|
---|
| 1956 | $metamode = "accumulate";
|
---|
| 1957 | }
|
---|
| 1958 |
|
---|
| 1959 | # No support for metapos and prevmetavalue in the JSON metatable substructure
|
---|
| 1960 | my $metapos = undef;
|
---|
| 1961 | my $prevmetavalue = undef;
|
---|
| 1962 | my $metavals = $metatable_rec->{'metavals'}; # a sub-subarray
|
---|
| 1963 |
|
---|
| 1964 | foreach my $metavalue ( @$metavals ) {
|
---|
| 1965 | $metavalue =~ s/<(.*?)>/<$1>/g;
|
---|
| 1966 |
|
---|
| 1967 | $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
|
---|
| 1968 | if($metamode eq "override") { # now, having overridden the first metavalue of the metaname,
|
---|
| 1969 | # need to accumulate subsequent metavals for this metaname, else the just-assigned
|
---|
| 1970 | # metavalue for this metaname will be lost
|
---|
| 1971 | $metamode = "accumulate";
|
---|
| 1972 | }
|
---|
| 1973 | }
|
---|
| 1974 | }
|
---|
| 1975 | }
|
---|
| 1976 | }
|
---|
| 1977 |
|
---|
| 1978 | # always a success message
|
---|
| 1979 | my $mess = "set-archives-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n";
|
---|
| 1980 | $gsdl_cgi->generate_ok_message($mess);
|
---|
| 1981 | }
|
---|
| 1982 |
|
---|
| 1983 | # always returns true (1)
|
---|
| 1984 | sub set_import_metadata_entry
|
---|
| 1985 | {
|
---|
| 1986 | my $self = shift @_;
|
---|
| 1987 | my ($gsdl_cgi, $arcinfo_doc_filename, $infodbtype, $docid, $metaname, $metapos, $metavalue, $metamode, $prevmetavalue, $collect, $collect_dir) = @_;
|
---|
| 1988 |
|
---|
| 1989 | my $info_mess = <<RAWEND;
|
---|
| 1990 | ****************************
|
---|
| 1991 | set_import_metadata_entry()
|
---|
| 1992 | ****************************
|
---|
| 1993 | RAWEND
|
---|
| 1994 |
|
---|
| 1995 | $info_mess .= " collect_dir = $collect_dir\n" if defined($collect_dir);
|
---|
| 1996 | $info_mess .= " collect = $collect\n" if defined($collect);
|
---|
| 1997 | $info_mess .= " infodbtype = $infodbtype\n" if defined($infodbtype);
|
---|
| 1998 | $info_mess .= " arcinfo_doc_filename = $arcinfo_doc_filename\n" if defined($arcinfo_doc_filename);
|
---|
| 1999 | $info_mess .= " docid = $docid\n" if defined($docid);
|
---|
| 2000 | $info_mess .= " metaname = $metaname\n" if defined($metaname);
|
---|
| 2001 | $info_mess .= " metapos = $metapos\n" if defined($metapos);
|
---|
| 2002 | $info_mess .= " metavalue = $metavalue\n" if defined($metavalue);
|
---|
| 2003 | $info_mess .= " metamode = $metamode\n" if defined($metamode);
|
---|
| 2004 | $info_mess .= " prevmetaval = $prevmetavalue\n" if defined($prevmetavalue);
|
---|
| 2005 |
|
---|
| 2006 | $info_mess .= "****************************\n";
|
---|
| 2007 |
|
---|
| 2008 | $gsdl_cgi->generate_message($info_mess);
|
---|
| 2009 |
|
---|
| 2010 | # import works with metadata.xml which can have inherited metadata
|
---|
| 2011 | # so setting or removing at a metapos can have unintended effects for a COMPLEX collection
|
---|
| 2012 | # (a collection that has or can have inherited metadata). Metapos has expected behaviour for
|
---|
| 2013 | # a SIMPLE collection, which is one that doesn't have inherited metadata. Assume caller knows
|
---|
| 2014 | # what they're doing if they provide a metapos.
|
---|
| 2015 | if(defined $metapos) {
|
---|
| 2016 | print STDERR "@@@@ WARNING: metapos defined.\n";
|
---|
| 2017 | print STDERR "@@@@ Assuming SIMPLE collection and proceeding to modify the import meta at $metapos.\n";
|
---|
| 2018 | }
|
---|
| 2019 |
|
---|
| 2020 | # Obtain where the metadata.xml is from the archiveinfo-doc.gdb file
|
---|
| 2021 | # If the doc oid is not specified, we assume the metadata.xml is next to the specified "f"
|
---|
| 2022 | my $metadata_xml_file;
|
---|
| 2023 | my $import_filename = undef;
|
---|
| 2024 |
|
---|
| 2025 | if (defined $docid) {
|
---|
| 2026 | # my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
|
---|
| 2027 | my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
|
---|
| 2028 |
|
---|
| 2029 | # This now stores the full pathname
|
---|
| 2030 | $import_filename = $doc_rec->{'src-file'}->[0];
|
---|
| 2031 | $import_filename = &util::placeholders_to_abspath($import_filename);
|
---|
| 2032 |
|
---|
| 2033 | } else { # only for set_import_meta, not the case when calling method is set_import_metadata_array
|
---|
| 2034 | # as the array version of the method doesn't support the -f parameter yet
|
---|
| 2035 | my $import_file = $self->{'f'};
|
---|
| 2036 | $import_filename = &util::filename_cat($collect_dir,$collect,$import_file);
|
---|
| 2037 | }
|
---|
| 2038 |
|
---|
| 2039 | # figure out correct metadata.xml file [?]
|
---|
| 2040 | # Assuming the metadata.xml file is next to the source file
|
---|
| 2041 | # Note: This will not work if it is using the inherited metadata from the parent folder
|
---|
| 2042 | my ($import_tailname, $import_dirname) = File::Basename::fileparse($import_filename);
|
---|
| 2043 | my $metadata_xml_filename = &util::filename_cat($import_dirname,"metadata.xml");
|
---|
| 2044 |
|
---|
| 2045 | # If we're overriding everything, then $prevmetavalue=undefined and
|
---|
| 2046 | # $metamode=override combined with $metapos=undefined
|
---|
| 2047 | # in which case we need to remove all metavalues for the metaname at the given (sub)section
|
---|
| 2048 | # Thereafter, we will finally be able to set the overriding metavalue for this metaname
|
---|
| 2049 | if(!defined $prevmetavalue && !defined $metapos && $metamode eq "override") {
|
---|
| 2050 | ## print STDERR "@@@ REMOVING all import metadata for $metaname\n";
|
---|
| 2051 | $self->remove_from_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, undef, $import_tailname, $metamode); # we're removing all values, so metavalue=undef
|
---|
| 2052 |
|
---|
| 2053 | }
|
---|
| 2054 |
|
---|
| 2055 | # Edit the metadata.xml
|
---|
| 2056 | # Modified by Jeffrey from DL Consulting
|
---|
| 2057 | # Handle the case where there is one metadata.xml file for multiple FileSets
|
---|
| 2058 | # The XML filter needs to know whether it is in the right FileSet
|
---|
| 2059 | # TODO: This doesn't fix the problem where the metadata.xml is not next to the src file.
|
---|
| 2060 | # TODO: This doesn't handle the common metadata (where FileName doesn't point to a single file)
|
---|
| 2061 | $self->edit_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname,
|
---|
| 2062 | $metapos, $metavalue, $metamode, $import_tailname, $prevmetavalue);
|
---|
| 2063 | #return 0;
|
---|
| 2064 | return $metadata_xml_filename;
|
---|
| 2065 | }
|
---|
| 2066 |
|
---|
| 2067 | sub _remove_import_metadata
|
---|
| 2068 | {
|
---|
| 2069 | my $self = shift @_;
|
---|
| 2070 |
|
---|
| 2071 | my $collect = $self->{'collect'};
|
---|
| 2072 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 2073 | # my $gsdlhome = $self->{'gsdlhome'};
|
---|
| 2074 | my $infodbtype = $self->{'infodbtype'};
|
---|
| 2075 |
|
---|
| 2076 | # Obtain the collect dir
|
---|
| 2077 | ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
|
---|
| 2078 | my $site = $self->{'site'};
|
---|
| 2079 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 2080 |
|
---|
| 2081 | ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
|
---|
| 2082 | my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
|
---|
| 2083 |
|
---|
| 2084 | # look up additional args
|
---|
| 2085 | my $docid = $self->{'d'};
|
---|
| 2086 | if ((!defined $docid) || ($docid =~ m/^\s*$/))
|
---|
| 2087 | {
|
---|
| 2088 | $gsdl_cgi->generate_error("No docid (d=...) specified.\n");
|
---|
| 2089 | }
|
---|
| 2090 |
|
---|
| 2091 | my $metaname = $self->{'metaname'};
|
---|
| 2092 | my $metapos = $self->{'metapos'};
|
---|
| 2093 | my $metavalue = $self->{'metavalue'};
|
---|
| 2094 |
|
---|
| 2095 | $metapos = undef if(defined $metapos && ($metapos =~ m/^\s*$/));
|
---|
| 2096 | $metavalue = undef if(defined $metavalue && ($metavalue =~ m/^\s*$/));
|
---|
| 2097 |
|
---|
| 2098 | if(defined $metavalue) { # metavalue is now a compulsory arg for remove_import_metadata()
|
---|
| 2099 | $metavalue =~ s/<(.*?)>/<$1>/g;
|
---|
| 2100 | } elsif (!defined $metapos) { # if given no metavalue or metapos to delete, default to deleting the 1st
|
---|
| 2101 | $metapos = 0;
|
---|
| 2102 | }
|
---|
| 2103 | my $metamode = $self->{'metamode'};
|
---|
| 2104 | $metamode = undef if(defined $metamode && ($metamode =~ m/^\s*$/));
|
---|
| 2105 |
|
---|
| 2106 | # import works with metadata.xml which can have inherited metadata
|
---|
| 2107 | # so setting or removing at a metapos can have unintended effects for a COMPLEX collection
|
---|
| 2108 | # (a collection that has or can have inherited metadata). Metapos has expected behaviour for
|
---|
| 2109 | # a SIMPLE collection, which is one that doesn't have inherited metadata. Assume caller knows
|
---|
| 2110 | # what they're doing if they provide a metapos.
|
---|
| 2111 | if(defined $metapos) {
|
---|
| 2112 | print STDERR "@@@@ WARNING: metapos defined.\n";
|
---|
| 2113 | print STDERR "@@@@ Assuming SIMPLE collection and proceeding to modify the import meta at $metapos.\n";
|
---|
| 2114 | }
|
---|
| 2115 |
|
---|
| 2116 | # Obtain where the metadata.xml is from the archiveinfo-doc.gdb file
|
---|
| 2117 | # If the doc oid is not specified, we assume the metadata.xml is next to the specified "f"
|
---|
| 2118 | my $metadata_xml_file;
|
---|
| 2119 | my $import_filename = undef;
|
---|
| 2120 | if (defined $docid)
|
---|
| 2121 | {
|
---|
| 2122 | my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
|
---|
| 2123 | my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
|
---|
| 2124 |
|
---|
| 2125 | # This now stores the full pathname
|
---|
| 2126 | $import_filename = $doc_rec->{'src-file'}->[0];
|
---|
| 2127 | $import_filename = &util::placeholders_to_abspath($import_filename);
|
---|
| 2128 | }
|
---|
| 2129 |
|
---|
| 2130 | if((!defined $import_filename) || ($import_filename =~ m/^\s*$/))
|
---|
| 2131 | {
|
---|
| 2132 | $gsdl_cgi->generate_error("There is no metadata\n");
|
---|
| 2133 | }
|
---|
| 2134 |
|
---|
| 2135 | # figure out correct metadata.xml file [?]
|
---|
| 2136 | # Assuming the metadata.xml file is next to the source file
|
---|
| 2137 | # Note: This will not work if it is using the inherited metadata from the parent folder
|
---|
| 2138 | my ($import_tailname, $import_dirname) = File::Basename::fileparse($import_filename);
|
---|
| 2139 | my $metadata_xml_filename = &util::filename_cat($import_dirname,"metadata.xml");
|
---|
| 2140 |
|
---|
| 2141 | $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
|
---|
| 2142 |
|
---|
| 2143 | my $mess = "remove-import-metadata successful: Key[$docid] -> $metadata_xml_filename\n";
|
---|
| 2144 | $mess .= " $metaname";
|
---|
| 2145 | $mess .= " = $metavalue\n";
|
---|
| 2146 |
|
---|
| 2147 | $gsdl_cgi->generate_ok_message($mess);
|
---|
| 2148 |
|
---|
| 2149 | #return $status; # in case calling functions have a use for this
|
---|
| 2150 | }
|
---|
| 2151 |
|
---|
| 2152 | sub remove_import_metadata
|
---|
| 2153 | {
|
---|
| 2154 | my $self = shift @_;
|
---|
| 2155 |
|
---|
| 2156 | my $username = $self->{'username'};
|
---|
| 2157 | my $collect = $self->{'collect'};
|
---|
| 2158 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 2159 |
|
---|
| 2160 | if ($baseaction::authentication_enabled) {
|
---|
| 2161 | # Ensure the user is allowed to edit this collection
|
---|
| 2162 | $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
|
---|
| 2163 | }
|
---|
| 2164 |
|
---|
| 2165 | # Make sure the collection isn't locked by someone else
|
---|
| 2166 | $self->lock_collection($username, $collect);
|
---|
| 2167 |
|
---|
| 2168 | $self->_remove_import_metadata(@_);
|
---|
| 2169 |
|
---|
| 2170 | # Release the lock once it is done
|
---|
| 2171 | $self->unlock_collection($username, $collect);
|
---|
| 2172 |
|
---|
| 2173 | }
|
---|
| 2174 |
|
---|
| 2175 | sub remove_from_metadata_xml
|
---|
| 2176 | {
|
---|
| 2177 | my $self = shift @_;
|
---|
| 2178 | my ($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $metavalue, $src_file, $metamode) = @_;
|
---|
| 2179 | # metamode generally has no meaning for removing meta, but is used by set_meta
|
---|
| 2180 | # when overriding all metavals for a metaname, in which case remove_meta is called with metamode
|
---|
| 2181 |
|
---|
| 2182 | # Set the call-back functions for the metadata tags
|
---|
| 2183 | my @rules =
|
---|
| 2184 | (
|
---|
| 2185 | _default => 'raw',
|
---|
| 2186 | 'Metadata' => \&rfmxml_metadata,
|
---|
| 2187 | 'FileName' => \&mxml_filename
|
---|
| 2188 | );
|
---|
| 2189 |
|
---|
| 2190 | my $parser = XML::Rules->new
|
---|
| 2191 | (
|
---|
| 2192 | rules => \@rules,
|
---|
| 2193 | style => 'filter',
|
---|
| 2194 | output_encoding => 'utf8',
|
---|
| 2195 | #normalisespaces => 1,
|
---|
| 2196 | stripspaces => 2|0|0 # ineffectual
|
---|
| 2197 | );
|
---|
| 2198 |
|
---|
| 2199 | my $xml_in = "";
|
---|
| 2200 | if (!open(MIN,"<$metadata_xml_filename"))
|
---|
| 2201 | {
|
---|
| 2202 | $gsdl_cgi->generate_error("Unable to read in $metadata_xml_filename: $!");
|
---|
| 2203 | }
|
---|
| 2204 | else
|
---|
| 2205 | {
|
---|
| 2206 | # Read them in
|
---|
| 2207 | my $line;
|
---|
| 2208 | while (defined ($line=<MIN>)) {
|
---|
| 2209 | $xml_in .= $line;
|
---|
| 2210 | }
|
---|
| 2211 | close(MIN);
|
---|
| 2212 |
|
---|
| 2213 | # Filter with the call-back functions
|
---|
| 2214 | my $xml_out = "";
|
---|
| 2215 |
|
---|
| 2216 | my $MOUT;
|
---|
| 2217 | if (!open($MOUT,">$metadata_xml_filename")) {
|
---|
| 2218 | $gsdl_cgi->generate_error("Unable to write out to $metadata_xml_filename: $!");
|
---|
| 2219 | }
|
---|
| 2220 | else {
|
---|
| 2221 | binmode($MOUT,":utf8");
|
---|
| 2222 | $parser->filter($xml_in, $MOUT, {metaname => $metaname, metapos => $metapos, metavalue => $metavalue, src_file => $src_file, metamode => $metamode, current_file => undef});
|
---|
| 2223 | close($MOUT);
|
---|
| 2224 | }
|
---|
| 2225 | }
|
---|
| 2226 | }
|
---|
| 2227 |
|
---|
| 2228 | sub rfmxml_metadata
|
---|
| 2229 | {
|
---|
| 2230 | my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
|
---|
| 2231 |
|
---|
| 2232 | # metadata.xml does not handle subsections
|
---|
| 2233 |
|
---|
| 2234 | # since metadata.xml now has to deal with metapos, we keep track of the metadata position
|
---|
| 2235 | if (($parser->{'parameters'}->{'src_file'} eq $parser->{'parameters'}->{'current_file'})
|
---|
| 2236 | && $parser->{'parameters'}->{'metaname'} eq $attrHash->{'name'})
|
---|
| 2237 | {
|
---|
| 2238 | if (!defined $parser->{'parameters'}->{'poscount'})
|
---|
| 2239 | {
|
---|
| 2240 | $parser->{'parameters'}->{'poscount'} = 0;
|
---|
| 2241 | }
|
---|
| 2242 | else
|
---|
| 2243 | {
|
---|
| 2244 | $parser->{'parameters'}->{'poscount'}++;
|
---|
| 2245 | }
|
---|
| 2246 |
|
---|
| 2247 | # if overriding but no metapos, then clear all the meta for this metaname
|
---|
| 2248 | if ((defined $parser->{'parameters'}->{'metamode'}) && ($parser->{'parameters'}->{'metamode'} eq "override") && (!defined $parser->{'parameters'}->{'metapos'}) && (!defined $parser->{'parameters'}->{'metavalue'})) {
|
---|
| 2249 | return [];
|
---|
| 2250 | }
|
---|
| 2251 |
|
---|
| 2252 | if ((defined $parser->{'parameters'}->{'metapos'}) && ($parser->{'parameters'}->{'poscount'} == $parser->{'parameters'}->{'metapos'}))
|
---|
| 2253 | {
|
---|
| 2254 | return [];
|
---|
| 2255 | }
|
---|
| 2256 | if ((defined $parser->{'parameters'}->{'metavalue'}) && ($attrHash->{'_content'} eq $parser->{'parameters'}->{'metavalue'}))
|
---|
| 2257 | {
|
---|
| 2258 | return [];
|
---|
| 2259 | }
|
---|
| 2260 | }
|
---|
| 2261 |
|
---|
| 2262 | # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
|
---|
| 2263 | return [$tagname => $attrHash];
|
---|
| 2264 | }
|
---|
| 2265 |
|
---|
| 2266 | sub _remove_live_metadata
|
---|
| 2267 | {
|
---|
| 2268 | my $self = shift @_;
|
---|
| 2269 |
|
---|
| 2270 | my $collect = $self->{'collect'};
|
---|
| 2271 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 2272 | # my $gsdlhome = $self->{'gsdlhome'};
|
---|
| 2273 | my $infodbtype = $self->{'infodbtype'};
|
---|
| 2274 |
|
---|
| 2275 | # Obtain the collect dir
|
---|
| 2276 | ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
|
---|
| 2277 | my $site = $self->{'site'};
|
---|
| 2278 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 2279 |
|
---|
| 2280 |
|
---|
| 2281 | # look up additional args
|
---|
| 2282 | my $docid = $self->{'d'};
|
---|
| 2283 | if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
|
---|
| 2284 | $gsdl_cgi->generate_error("No docid (d=...) specified.");
|
---|
| 2285 | }
|
---|
| 2286 |
|
---|
| 2287 | # Generate the dbkey
|
---|
| 2288 | my $metaname = $self->{'metaname'};
|
---|
| 2289 | my $dbkey = "$docid.$metaname";
|
---|
| 2290 |
|
---|
| 2291 | # To people who know $collect_tail please add some comments
|
---|
| 2292 | # Obtain the live gdbm_db path
|
---|
| 2293 | my $collect_tail = $collect;
|
---|
| 2294 | $collect_tail =~ s/^.*[\/|\\]//;
|
---|
| 2295 | my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
|
---|
| 2296 | my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
|
---|
| 2297 |
|
---|
| 2298 | # Remove the key
|
---|
| 2299 | my $cmd = "gdbmdel \"$infodb_file_path\" \"$dbkey\"";
|
---|
| 2300 | my $status = system($cmd);
|
---|
| 2301 | if ($status != 0) {
|
---|
| 2302 | # Catch error if gdbmdel failed
|
---|
| 2303 | my $mess = "Failed to set metadata key: $dbkey\n";
|
---|
| 2304 |
|
---|
| 2305 | $mess .= "PATH: $ENV{'PATH'}\n";
|
---|
| 2306 | $mess .= "cmd = $cmd\n";
|
---|
| 2307 | $mess .= "Exit status: $status\n";
|
---|
| 2308 | $mess .= "System Error Message: $!\n";
|
---|
| 2309 |
|
---|
| 2310 | $gsdl_cgi->generate_error($mess);
|
---|
| 2311 | }
|
---|
| 2312 | else {
|
---|
| 2313 | $gsdl_cgi->generate_ok_message("DB remove successful: Key[$metaname]");
|
---|
| 2314 | }
|
---|
| 2315 |
|
---|
| 2316 | }
|
---|
| 2317 |
|
---|
| 2318 | sub remove_live_metadata
|
---|
| 2319 | {
|
---|
| 2320 | my $self = shift @_;
|
---|
| 2321 |
|
---|
| 2322 | my $username = $self->{'username'};
|
---|
| 2323 | my $collect = $self->{'collect'};
|
---|
| 2324 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 2325 | my $gsdlhome = $self->{'gsdlhome'};
|
---|
| 2326 |
|
---|
| 2327 | if ($baseaction::authentication_enabled) {
|
---|
| 2328 | # Ensure the user is allowed to edit this collection
|
---|
| 2329 | $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
|
---|
| 2330 | }
|
---|
| 2331 |
|
---|
| 2332 | # Make sure the collection isn't locked by someone else
|
---|
| 2333 | $self->lock_collection($username, $collect);
|
---|
| 2334 |
|
---|
| 2335 | $self->_remove_live_metadata(@_);
|
---|
| 2336 |
|
---|
| 2337 | $self->unlock_collection($username, $collect);
|
---|
| 2338 | }
|
---|
| 2339 |
|
---|
| 2340 | sub remove_metadata
|
---|
| 2341 | {
|
---|
| 2342 | my $self = shift @_;
|
---|
| 2343 |
|
---|
| 2344 | my $where = $self->{'where'};
|
---|
| 2345 | if(!$where || ($where =~ m/^\s*$/)) {
|
---|
| 2346 | $self->remove_index_metadata(@_); # call the full version of set_index_meta for the default behaviour
|
---|
| 2347 | return;
|
---|
| 2348 | }
|
---|
| 2349 |
|
---|
| 2350 | my $username = $self->{'username'};
|
---|
| 2351 | my $collect = $self->{'collect'};
|
---|
| 2352 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 2353 |
|
---|
| 2354 | if ($baseaction::authentication_enabled) {
|
---|
| 2355 | # Ensure the user is allowed to edit this collection
|
---|
| 2356 | $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
|
---|
| 2357 | }
|
---|
| 2358 |
|
---|
| 2359 | # Make sure the collection isn't locked by someone else
|
---|
| 2360 | $self->lock_collection($username, $collect);
|
---|
| 2361 |
|
---|
| 2362 | # check which directories need to be processed, specified in $where as
|
---|
| 2363 | # any combination of import|archives|index|live
|
---|
| 2364 | if($where =~ m/import/) {
|
---|
| 2365 | $self->_remove_import_metadata(@_);
|
---|
| 2366 | }
|
---|
| 2367 | if($where =~ m/archives/) {
|
---|
| 2368 | $self->_remove_archives_metadata(@_);
|
---|
| 2369 | }
|
---|
| 2370 | if($where =~ m/index/) {
|
---|
| 2371 | $self->_remove_index_metadata(@_);
|
---|
| 2372 | }
|
---|
| 2373 |
|
---|
| 2374 | # Release the lock once it is done
|
---|
| 2375 | $self->unlock_collection($username, $collect);
|
---|
| 2376 | }
|
---|
| 2377 |
|
---|
| 2378 | # the internal version, without authentication
|
---|
| 2379 | sub _remove_index_metadata
|
---|
| 2380 | {
|
---|
| 2381 | my $self = shift @_;
|
---|
| 2382 |
|
---|
| 2383 | my $collect = $self->{'collect'};
|
---|
| 2384 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 2385 | # my $gsdlhome = $self->{'gsdlhome'};
|
---|
| 2386 | my $infodbtype = $self->{'infodbtype'};
|
---|
| 2387 |
|
---|
| 2388 | # Obtain the collect dir
|
---|
| 2389 | my $site = $self->{'site'};
|
---|
| 2390 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 2391 | ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
|
---|
| 2392 |
|
---|
| 2393 | # look up additional args
|
---|
| 2394 | my $docid = $self->{'d'};
|
---|
| 2395 | if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
|
---|
| 2396 | $gsdl_cgi->generate_error("No docid (d=...) specified.");
|
---|
| 2397 | }
|
---|
| 2398 | my $metaname = $self->{'metaname'};
|
---|
| 2399 | my $metapos = $self->{'metapos'};
|
---|
| 2400 | my $metavalue = $self->{'metavalue'};
|
---|
| 2401 |
|
---|
| 2402 | $metapos = undef if(defined $metapos && ($metapos =~ m/^\s*$/));
|
---|
| 2403 | $metavalue = undef if(defined $metavalue && ($metavalue =~ m/^\s*$/)); # necessary to force fallback to undef here
|
---|
| 2404 |
|
---|
| 2405 | # To people who know $collect_tail please add some comments
|
---|
| 2406 | # -> In collection groups, I think collect_tailname is the subcollection name,
|
---|
| 2407 | # e.g. colgroup-name/col-tail-name
|
---|
| 2408 | # Obtain the path to the database
|
---|
| 2409 | my $collect_tail = $collect;
|
---|
| 2410 | $collect_tail =~ s/^.*[\/|\\]//;
|
---|
| 2411 | my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
|
---|
| 2412 | my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
|
---|
| 2413 |
|
---|
| 2414 | # Read the docid entry
|
---|
| 2415 | my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
|
---|
| 2416 |
|
---|
| 2417 | # Check to make sure the key does exist
|
---|
| 2418 | if (!defined ($doc_rec->{$metaname})) {
|
---|
| 2419 | $gsdl_cgi->generate_error("No metadata field \"" . $metaname . "\" in the specified document: [" . $docid . "]");
|
---|
| 2420 | }
|
---|
| 2421 |
|
---|
| 2422 | # Obtain the specified metadata pos
|
---|
| 2423 | # if no metavalue or metapos to delete, default to deleting the 1st value for the metaname
|
---|
| 2424 | if(!defined $metapos && !defined $metavalue) {
|
---|
| 2425 | $metapos = 0;
|
---|
| 2426 | }
|
---|
| 2427 |
|
---|
| 2428 |
|
---|
| 2429 | # consider check key is defined before deleting?
|
---|
| 2430 | # Loop through the metadata array and ignore the specified position
|
---|
| 2431 | my $filtered_metadata = [];
|
---|
| 2432 | my $num_metadata_vals = scalar(@{$doc_rec->{$metaname}});
|
---|
| 2433 | for (my $i=0; $i<$num_metadata_vals; $i++) {
|
---|
| 2434 | my $metaval = shift(@{$doc_rec->{$metaname}});
|
---|
| 2435 |
|
---|
| 2436 | if (!defined $metavalue && $i != $metapos) {
|
---|
| 2437 | push(@$filtered_metadata,$metaval);
|
---|
| 2438 | }
|
---|
| 2439 |
|
---|
| 2440 | if(defined $metavalue && !($metavalue eq $metaval))
|
---|
| 2441 | {
|
---|
| 2442 | push(@$filtered_metadata,$metaval);
|
---|
| 2443 | }
|
---|
| 2444 | }
|
---|
| 2445 | $doc_rec->{$metaname} = $filtered_metadata;
|
---|
| 2446 |
|
---|
| 2447 | ## Use the dbutil set_entry method instead of assuming the database is gdbm
|
---|
| 2448 | my $status = &dbutil::set_infodb_entry($infodbtype, $infodb_file_path, $docid, $doc_rec);
|
---|
| 2449 |
|
---|
| 2450 | if ($status != 0) {
|
---|
| 2451 | my $mess = "Failed to set metadata key: $docid\n";
|
---|
| 2452 |
|
---|
| 2453 | $mess .= "PATH: $ENV{'PATH'}\n";
|
---|
| 2454 | $mess .= "Exit status: $status\n";
|
---|
| 2455 | $mess .= "System Error Message: $!\n";
|
---|
| 2456 |
|
---|
| 2457 | $gsdl_cgi->generate_error($mess);
|
---|
| 2458 | }
|
---|
| 2459 | else {
|
---|
| 2460 | my $mess = "DB set (with item deleted) successful: Key[$docid]\n";
|
---|
| 2461 | $mess .= " $metaname";
|
---|
| 2462 | $mess .= "->[$metapos]" if (defined $metapos);
|
---|
| 2463 | $mess .= " ($metavalue)" if (defined $metavalue);
|
---|
| 2464 |
|
---|
| 2465 | $gsdl_cgi->generate_ok_message($mess);
|
---|
| 2466 | }
|
---|
| 2467 |
|
---|
| 2468 | #return $status; # in case calling functions have a use for this
|
---|
| 2469 | }
|
---|
| 2470 |
|
---|
| 2471 | sub remove_index_metadata
|
---|
| 2472 | {
|
---|
| 2473 | my $self = shift @_;
|
---|
| 2474 |
|
---|
| 2475 | my $username = $self->{'username'};
|
---|
| 2476 | my $collect = $self->{'collect'};
|
---|
| 2477 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 2478 | # my $gsdlhome = $self->{'gsdlhome'};
|
---|
| 2479 |
|
---|
| 2480 | if ($baseaction::authentication_enabled) {
|
---|
| 2481 | # Ensure the user is allowed to edit this collection
|
---|
| 2482 | $self->authenticate_user($username, $collect); #&authenticate_user($gsdl_cgi, $username, $collect);
|
---|
| 2483 | }
|
---|
| 2484 |
|
---|
| 2485 | # Obtain the collect dir
|
---|
| 2486 | my $site = $self->{'site'};
|
---|
| 2487 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 2488 | ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
|
---|
| 2489 |
|
---|
| 2490 | # Make sure the collection isn't locked by someone else
|
---|
| 2491 | $self->lock_collection($username, $collect);
|
---|
| 2492 |
|
---|
| 2493 | $self->_remove_index_metadata(@_);
|
---|
| 2494 |
|
---|
| 2495 | # Release the lock once it is done
|
---|
| 2496 | $self->unlock_collection($username, $collect);
|
---|
| 2497 | }
|
---|
| 2498 |
|
---|
| 2499 |
|
---|
| 2500 | # Was trying to reused the codes, but the functions need to be broken
|
---|
| 2501 | # down more before they can be reused, otherwise there will be too
|
---|
| 2502 | # much overhead and duplicate process...
|
---|
| 2503 | sub insert_metadata
|
---|
| 2504 | {
|
---|
| 2505 | my $self = shift @_;
|
---|
| 2506 |
|
---|
| 2507 | my $username = $self->{'username'};
|
---|
| 2508 | my $collect = $self->{'collect'};
|
---|
| 2509 | my $gsdl_cgi = $self->{'gsdl_cgi'};
|
---|
| 2510 | my $gsdlhome = $self->{'gsdlhome'};
|
---|
| 2511 | my $infodbtype = $self->{'infodbtype'};
|
---|
| 2512 |
|
---|
| 2513 | # If the import metadata and gdbm database have been updated, we
|
---|
| 2514 | # need to insert some notification to warn user that the the text
|
---|
| 2515 | # they see at the moment is not indexed and require a rebuild.
|
---|
| 2516 | my $rebuild_pending_macro = "_rebuildpendingmessage_";
|
---|
| 2517 |
|
---|
| 2518 | if ($baseaction::authentication_enabled) {
|
---|
| 2519 | # Ensure the user is allowed to edit this collection
|
---|
| 2520 | $self->authenticate_user($username, $collect);
|
---|
| 2521 | }
|
---|
| 2522 |
|
---|
| 2523 | # Obtain the collect and archive dir
|
---|
| 2524 | my $site = $self->{'site'};
|
---|
| 2525 | my $collect_dir = $gsdl_cgi->get_collection_dir($site);
|
---|
| 2526 | ##my $collect_dir = &util::filename_cat($gsdlhome, "collect");
|
---|
| 2527 | my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
|
---|
| 2528 |
|
---|
| 2529 | # Make sure the collection isn't locked by someone else
|
---|
| 2530 | $self->lock_collection($username, $collect);
|
---|
| 2531 |
|
---|
| 2532 | # Check additional args
|
---|
| 2533 | my $docid = $self->{'d'};
|
---|
| 2534 | if (!defined($docid) || ($docid =~ m/^\s*$/)) {
|
---|
| 2535 | $gsdl_cgi->generate_error("No document id is specified: d=...");
|
---|
| 2536 | }
|
---|
| 2537 | my $metaname = $self->{'metaname'};
|
---|
| 2538 | if (!defined($metaname) || ($metaname =~ m/^\s*$/)) {
|
---|
| 2539 | $gsdl_cgi->generate_error("No metaname is specified: metadataname=...");
|
---|
| 2540 | }
|
---|
| 2541 | my $metavalue = $self->{'metavalue'};
|
---|
| 2542 | if (!defined($metavalue) || ($metavalue =~ m/^\s*$/)) {
|
---|
| 2543 | $gsdl_cgi->generate_error("No metavalue or empty metavalue is specified: metadataname=...");
|
---|
| 2544 | }
|
---|
| 2545 | # make "accumulate" the default (less destructive, as it won't actually
|
---|
| 2546 | # delete any existing values)
|
---|
| 2547 | my $metamode = "accumulate";
|
---|
| 2548 |
|
---|
| 2549 | # metapos/prevmetavalue were never before used in this subroutine, so set them to undefined
|
---|
| 2550 | my $metapos = undef;
|
---|
| 2551 | my $prevmetavalue = undef;
|
---|
| 2552 |
|
---|
| 2553 | #=======================================================================#
|
---|
| 2554 | # set_import_metadata [START]
|
---|
| 2555 | #=======================================================================#
|
---|
| 2556 | # Obtain where the metadata.xml is from the archiveinfo-doc.gdb file
|
---|
| 2557 | # If the doc oid is not specified, we assume the metadata.xml is next to the specified "f"
|
---|
| 2558 | my $metadata_xml_file;
|
---|
| 2559 | my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
|
---|
| 2560 | my $archive_doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
|
---|
| 2561 |
|
---|
| 2562 | # This now stores the full pathname
|
---|
| 2563 | my $import_filename = $archive_doc_rec->{'src-file'}->[0];
|
---|
| 2564 | $import_filename = &util::placeholders_to_abspath($import_filename);
|
---|
| 2565 |
|
---|
| 2566 | # figure out correct metadata.xml file [?]
|
---|
| 2567 | # Assuming the metadata.xml file is next to the source file
|
---|
| 2568 | # Note: This will not work if it is using the inherited metadata from the parent folder
|
---|
| 2569 | my ($import_tailname, $import_dirname)
|
---|
| 2570 | = File::Basename::fileparse($import_filename);
|
---|
| 2571 | my $metadata_xml_filename = &util::filename_cat($import_dirname,"metadata.xml");
|
---|
| 2572 |
|
---|
| 2573 | # Shane's escape characters
|
---|
| 2574 | $metavalue = pack "U0C*", unpack "C*", $metavalue;
|
---|
| 2575 | $metavalue =~ s/\,/,/g;
|
---|
| 2576 | $metavalue =~ s/\:/:/g;
|
---|
| 2577 | $metavalue =~ s/\|/|/g;
|
---|
| 2578 | $metavalue =~ s/\(/(/g;
|
---|
| 2579 | $metavalue =~ s/\)/)/g;
|
---|
| 2580 | $metavalue =~ s/\[/[/g;
|
---|
| 2581 | $metavalue =~ s/\\/\/g;
|
---|
| 2582 | $metavalue =~ s/\]/]/g;
|
---|
| 2583 | $metavalue =~ s/\{/{/g;
|
---|
| 2584 | $metavalue =~ s/\}/}/g;
|
---|
| 2585 | $metavalue =~ s/\"/"/g;
|
---|
| 2586 | $metavalue =~ s/\`/`/g;
|
---|
| 2587 | $metavalue =~ s/\n/_newline_/g;
|
---|
| 2588 |
|
---|
| 2589 | # Edit the metadata.xml
|
---|
| 2590 | # Modified by Jeffrey from DL Consulting
|
---|
| 2591 | # Handle the case where there is one metadata.xml file for multiple FileSets
|
---|
| 2592 | # The XML filter needs to know whether it is in the right FileSet
|
---|
| 2593 | # TODO: This doesn't fix the problem where the metadata.xml is not next to the src file.
|
---|
| 2594 | # TODO: This doesn't handle the common metadata (where FileName doesn't point to a single file)
|
---|
| 2595 | $self->edit_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname,
|
---|
| 2596 | $metapos, $metavalue, $metamode, $import_tailname, $prevmetavalue);
|
---|
| 2597 | #=======================================================================#
|
---|
| 2598 | # set_import_metadata [END]
|
---|
| 2599 | #=======================================================================#
|
---|
| 2600 |
|
---|
| 2601 |
|
---|
| 2602 | #=======================================================================#
|
---|
| 2603 | # set_metadata (accumulate version) [START]
|
---|
| 2604 | #=======================================================================#
|
---|
| 2605 | # To people who know $collect_tail please add some comments
|
---|
| 2606 | # Obtain path to the database
|
---|
| 2607 | my $collect_tail = $collect;
|
---|
| 2608 | $collect_tail =~ s/^.*[\/|\\]//;
|
---|
| 2609 | my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
|
---|
| 2610 | my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
|
---|
| 2611 |
|
---|
| 2612 | # Read the docid entry
|
---|
| 2613 | my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
|
---|
| 2614 |
|
---|
| 2615 | # Protect the quotes
|
---|
| 2616 | $metavalue =~ s/\"/\\\"/g;
|
---|
| 2617 |
|
---|
| 2618 | # Adds the pending macro
|
---|
| 2619 | my $macro_metavalue = $rebuild_pending_macro . $metavalue;
|
---|
| 2620 |
|
---|
| 2621 | # If the metadata doesn't exist, create a new one
|
---|
| 2622 | if (!defined($doc_rec->{$metaname})){
|
---|
| 2623 | $doc_rec->{$metaname} = [ $macro_metavalue ];
|
---|
| 2624 | }
|
---|
| 2625 | # Else, let's acculumate the values
|
---|
| 2626 | else {
|
---|
| 2627 | push(@{$doc_rec->{$metaname}},$macro_metavalue);
|
---|
| 2628 | }
|
---|
| 2629 |
|
---|
| 2630 | ## Use the dbutil set_entry method instead of assuming the database is gdbm
|
---|
| 2631 | my $status = &dbutil::set_infodb_entry($infodbtype, $infodb_file_path, $docid, $doc_rec);
|
---|
| 2632 |
|
---|
| 2633 | if ($status != 0) {
|
---|
| 2634 | # Catch error if gdbmget failed
|
---|
| 2635 | my $mess = "Failed to set metadata key: $docid\n";
|
---|
| 2636 |
|
---|
| 2637 | $mess .= "PATH: $ENV{'PATH'}\n";
|
---|
| 2638 | $mess .= "Exit status: $status\n";
|
---|
| 2639 | $mess .= "System Error Message: $!\n";
|
---|
| 2640 |
|
---|
| 2641 | $gsdl_cgi->generate_error($mess);
|
---|
| 2642 | }
|
---|
| 2643 | else {
|
---|
| 2644 | my $mess = "insert-metadata successful: Key[$docid]\n";
|
---|
| 2645 | $mess .= " [In metadata.xml] $metaname";
|
---|
| 2646 | $mess .= " = $metavalue\n";
|
---|
| 2647 | $mess .= " [In database] $metaname";
|
---|
| 2648 | $mess .= " = $macro_metavalue\n";
|
---|
| 2649 | $mess .= " The new text has not been indexed, rebuilding collection is required\n";
|
---|
| 2650 | $gsdl_cgi->generate_ok_message($mess);
|
---|
| 2651 | }
|
---|
| 2652 | #=======================================================================#
|
---|
| 2653 | # set_metadata (accumulate version) [END]
|
---|
| 2654 | #=======================================================================#
|
---|
| 2655 |
|
---|
| 2656 | # Release the lock once it is done
|
---|
| 2657 | $self->unlock_collection($username, $collect);
|
---|
| 2658 | }
|
---|
| 2659 |
|
---|
| 2660 | # not returning 1; here since this file is conditionally included by metadataction.pm
|
---|
[31617] | 2661 | # and not otherwise meant to be used on its own
|
---|
| 2662 |
|
---|