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