-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_folder_pages.pl
836 lines (715 loc) · 24.1 KB
/
create_folder_pages.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
#!/bin/env perl
# nbt, 2021-10-26
# creates the .md files for folders
# to be invoked with a param, either for
# - an extended folder id (e.g., pe/000012)
# - a collection id (e.g., pe)
# - 'ALL' (to (re-) create all collections)
use strict;
use warnings;
use utf8;
use lib './lib';
use Data::Dumper;
use Encode;
use HTML::Template;
use JSON;
use Path::Tiny;
use Readonly;
use YAML;
use ZBW::PM20x::Folder;
use ZBW::PM20x::Vocab;
$Data::Dumper::Sortkeys = 1;
# used only for debugging
my $json = JSON->new;
Readonly my $FOLDER_DATA => path('/pm20/data/rdf/pm20.extended.jsonld');
Readonly my $FOLDER_ROOT => $ZBW::PM20x::Folder::FOLDER_ROOT;
Readonly my $URL_DATA_ROOT => path('/pm20/data/folderdata');
Readonly my $FILMDATA_ROOT => path('../data/filmdata');
Readonly my $FOLDER_WEBROOT => path('/pm20/web/folder');
Readonly my %TITLE => %{ YAML::LoadFile('archive_titles.yaml') };
Readonly my @COLLECTIONS => qw/ co pe sh wa /;
Readonly my @LANGUAGES => qw/ en de /;
our @company_relations = (
{
field => 'parentOrganization',
label => {
en => 'Parent organization',
de => 'Übergeordnet',
},
},
{
field => 'subOrganization',
label => {
en => 'Subsidiary',
de => 'Untergeordnet',
},
},
{
field => 'precedingCorporateBody',
label => {
en => 'Preceeding org.',
de => 'Vorgänger',
},
},
{
field => 'succeedingCorporateBody',
label => {
en => 'Succeeding org.',
de => 'Nachfolger',
},
},
{
field => 'relatedCorporateBody',
label => {
en => 'Related org.',
de => 'Verwandte Org.',
},
},
);
my $tmpl = HTML::Template->new(
filename => 'html_tmpl/folder.md.tmpl',
utf8 => 1,
);
my $filming_def_ref = YAML::LoadFile('filming_def.yaml');
# lists of ids per collection
my %collection_ids;
# lookup table for all ids
my %folder_id;
load_ids( \%collection_ids, \%folder_id );
my $subject_voc = ZBW::PM20x::Vocab->new('subject');
# check arguments
if ( scalar(@ARGV) == 1 ) {
if ( $ARGV[0] =~ m:^(co|pe|wa|sh)$: ) {
my $collection = $1;
mk_collection($collection);
} elsif ( $ARGV[0] =~ m:^(co|pe)/(\d{6}): ) {
load_ids( \%collection_ids );
my $collection = $1;
my $folder_nk = $2;
mk_folder( $collection, $folder_nk );
} elsif ( $ARGV[0] =~ m:^(sh|wa)/(\d{6},\d{6})$: ) {
my $collection = $1;
my $folder_nk = $2;
mk_folder( $collection, $folder_nk );
} elsif ( $ARGV[0] eq 'ALL' ) {
load_ids( \%collection_ids );
mk_all();
} else {
&usage;
}
} else {
&usage;
}
####################
sub mk_all {
foreach my $collection (@COLLECTIONS) {
mk_collection($collection);
}
}
sub mk_collection {
my $collection = shift or die "param missing";
my @pages_for_sitemap;
my $i = 0;
foreach my $folder_nk ( sort @{ $collection_ids{$collection} } ) {
$i++;
##next if ($i < 8100);
mk_folder( $collection, $folder_nk, \@pages_for_sitemap );
# debug and progress info
if ( $i % 100 == 0 ) {
print "$i folders done (up to $collection/$folder_nk)\n";
}
}
# write a list of pages to index for Google etc.
# (used in create_sitemap.pl)
$URL_DATA_ROOT->child("${collection}_for_sitemap.lst")
->spew( join( "\n", @pages_for_sitemap ) );
}
sub mk_folder {
my $collection = shift || die "param missing";
my $folder_nk = shift || die "param missing";
my $pages_for_sitemap_ref = shift;
my $folder = ZBW::PM20x::Folder->new( $collection, $folder_nk );
# check if folder dir exists in the source tree
my $rel_path = $folder->get_folder_hashed_path();
my $full_path = $FOLDER_ROOT->child($rel_path);
if ( $folder->get_doc_count and -d $FOLDER_ROOT and not -d $full_path ) {
die "$full_path does not exist\n";
}
# create folder dir (including hashed level) in the web tree
my $folder_dir = $FOLDER_WEBROOT->child($rel_path);
$folder_dir->mkpath;
# TODO type public/intern (currently not necessary)
my $type = 'dummy';
my $folderdata_raw = $folder->get_folderdata_raw;
#
# wikidata link (use only first one)
my $wdlink;
for my $exact_match ( @{ $folderdata_raw->{exactMatch} } ) {
next if $wdlink;
my $uri = $exact_match->{'@id'};
next unless $uri =~ m/wikidata\.org/;
$wdlink = $uri;
}
# main loop
foreach my $lang (@LANGUAGES) {
my $label = $folder->get_folderlabel($lang);
my $collection_title = $TITLE{collection}{$collection}{$lang};
my $backlink = "../../about.$lang.html";
my $backlink_title =
$collection_title . ( $lang eq 'de' ? '-Mappen' : ' folders' );
if ( $collection eq 'sh' or $collection eq 'wa' ) {
$backlink = "../../../../../../category/about.$lang.html";
$backlink_title =
$lang eq 'de' ? 'Mappen nach Systematik' : 'Folders by category system';
}
if ( $collection eq 'wa' ) {
$backlink = '../../' . $backlink;
}
my %tmpl_var = (
"is_$lang" => 1,
provenance => $TITLE{provenance}{hh}{$lang},
coll => $collection_title,
label => $label,
folder_uri => $folder->get_folder_uri,
dfgview_url => $folder->get_dfgview_url,
iiifview_url => $folder->get_iiifview_url,
fid => "$collection/$folder_nk",
backlink => $backlink,
backlink_title => $backlink_title,
modified => $folder->get_modified,
doc_counts => $folder->format_doc_counts($lang) || undef,
);
# parse data for decription and jsonld
my $folderdata = parse_folderdata( $lang, $collection, $folder );
# add description meta tag
$tmpl_var{meta_description} = add_meta_description( $lang, $folderdata );
# add schema jsonld
$tmpl_var{schema_jsonld} =
add_schema_jsonld( $lang, $collection, $folderdata );
# TODO is raw, partly invalid jsonld useful?
##$tmpl_var{jsonld} = add_jsonld($folderdata_raw);
if ($wdlink) {
$tmpl_var{wdlink} = $wdlink;
}
if ( $folderdata_raw->{temporal} ) {
my @holdings;
foreach my $hold ( @{ $folderdata_raw->{temporal} } ) {
my $hold_str;
if ( $hold =~ m/^GB: (.+)$/ ) {
$hold_str =
$lang eq 'de' ? "Geschäftsberichte: $1" : "Annual reports: $1";
} else {
$hold_str =
$lang eq 'de'
? "Zeitungsausschnitte: $hold"
: "Newspaper clippings: $hold";
}
push( @holdings, $hold_str );
}
$tmpl_var{holdings} = join( '<br>', @holdings );
}
if ( $collection eq 'pe' or $collection eq 'co' ) {
$tmpl_var{from_to} =
( $folderdata_raw->{dateOfBirthAndDeath} || $folderdata_raw->{fromTo} );
$tmpl_var{gnd} = $folderdata_raw->{gndIdentifier};
foreach my $date (qw/birth death founding dissolution/) {
my $date_formatted = $folderdata_raw->{ $date . 'Date' };
next unless $date_formatted;
if ( $date_formatted =~ m/(\d{4})-00-00T00:00:00/ ) {
$date_formatted = $1;
} elsif ( $date_formatted =~ m/(.+)?T00:00:00/ ) {
$date_formatted = $1;
}
$tmpl_var{$date} = $date_formatted;
}
}
if ( $folderdata_raw->{activity} ) {
my $values =
join( '<br>', @{ get_activities( $lang, $folderdata_raw ) } );
$tmpl_var{activity} = $values;
}
if ( $folderdata_raw->{nationality} ) {
$tmpl_var{nationality} =
get_field_values( $lang, $folderdata_raw, 'nationality' );
}
if ( $folderdata_raw->{hasOccupation} and $lang eq 'de' ) {
$tmpl_var{occupation} = $folderdata_raw->{hasOccupation};
}
if ( $folderdata_raw->{note} and $lang eq 'de' ) {
my @notes = @{ $folderdata_raw->{note} };
$tmpl_var{note} = join( "<br>", @notes );
}
$tmpl_var{company_relations_loop} =
get_company_relations( $lang, $folder, \%folder_id );
if ( $folderdata_raw->{location} ) {
$tmpl_var{location} =
get_field_values( $lang, $folderdata_raw, 'location' );
}
if ( $folderdata_raw->{broaderLocation} ) {
$tmpl_var{broader_location} =
get_field_values( $lang, $folderdata_raw, 'broaderLocation' );
}
if ( $folderdata_raw->{industry} ) {
$tmpl_var{industry} =
get_field_values( $lang, $folderdata_raw, 'industry' );
}
if ( $folderdata_raw->{hasNACECode} ) {
$tmpl_var{hasNACECode} =
join( ', ', @{ $folderdata_raw->{hasNACECode} } );
}
if ( $folderdata_raw->{organizationType} ) {
$tmpl_var{organization_type} =
get_field_values( $lang, $folderdata_raw, 'organizationType' );
}
if ( $folderdata_raw->{includesInstitutionNamed} ) {
my @institutions;
foreach my $inst ( @{ $folderdata_raw->{includesInstitutionNamed} } ) {
my @ext;
if ( $inst->{note} ) {
push( @ext, $inst->{note}[0] );
}
if ( $inst->{gndIdentifier} ) {
push( @ext, "GND $inst->{gndIdentifier}" );
}
my $inst_entry = $inst->{name};
if (@ext) {
$inst_entry .= ' (' . join( ', ', @ext ) . ')';
}
push( @institutions, $inst_entry );
}
$tmpl_var{includes_institutions} = join( '; ', @institutions );
}
if ( $folderdata_raw->{editorialNote} ) {
## TODO remove temporary fix (should always be array)
$tmpl_var{editorial_note} =
( ref $folderdata_raw->{editorialNote} eq 'ARRAY' )
? join( '; ', @{ $folderdata_raw->{editorialNote} } )
: $folderdata_raw->{editorialNote};
}
if ( $collection eq 'sh' ) {
## hack for the labels
$label =~ m/^(.+?) : (.+)$/;
$tmpl_var{country_name} = $1;
$tmpl_var{subject_name} = $2;
# TODO Add general link to film/h2_sh and microfiche hint
##$tmpl_var{microfiche_period} = '1961-1980';
$tmpl_var{signature} = $folderdata_raw->{notation};
foreach my $part (qw/country subject/) {
$folderdata_raw->{$part}{'@id'} =~ m;/pressemappe20(/.+)$;;
my $url = "$1/about.$lang.html";
$tmpl_var{"${part}_url"} = $url;
next unless $part eq 'subject';
$folderdata_raw->{subject}{'@id'} =~ m;/i/(.+)$;;
my $id = $1;
my $broader_id = $subject_voc->broader($id);
## skip if top class
next if $id eq '156329';
next if not $broader_id;
$tmpl_var{broader_name} = $subject_voc->label( $lang, $broader_id );
$tmpl_var{broader_url} =
"/category/subject/i/$broader_id/about.$lang.html";
}
}
if ( $collection eq 'wa' ) {
## hack for the labels
$label =~ m/^(.+?) : (.+)$/;
$tmpl_var{ware_name} = $1;
$tmpl_var{country_name} = $2;
# TODO Add link to category wa page and microfiche hint
##$tmpl_var{microfiche_period} = '1961-1998';
foreach my $part (qw/country ware/) {
$folderdata_raw->{$part}{'@id'} =~ m;/pressemappe20(/.+)$;;
my $url = "$1/about.$lang.html";
$tmpl_var{"${part}_url"} = $url;
}
# check, if there is a 'World' folder for the ware
$folderdata_raw->{ware}{'@id'} =~ m;/i/(.+)$;;
my $id = $1;
my $world_nk = "$id,141728";
if ( defined $folder_id{"$collection/$world_nk"} ) {
## skip if the current folder is the world folder
if ( $folder_nk ne $world_nk ) {
my $world_folder = ZBW::PM20x::Folder->new( $collection, $world_nk );
$tmpl_var{world_name} = $world_folder->get_folderlabel($lang);
$tmpl_var{world_url} =
'/folder/'
. $world_folder->get_folder_hashed_path
. "/about.$lang.html";
}
}
}
# film sections, do not exist for persons
if ( $collection eq 'co' ) {
my $company_id = "co/$folder_nk";
foreach my $filming (qw/ 1 2 /) {
# skip filming 1 when folder exists
next if ( $filming == 1 and $folder->get_doc_count );
my $filming_ref = $filming_def_ref->{$filming};
my @filmsectionlist = $folder->get_filmsectionlist($filming);
##print Dumper \@filmsectionlist;
# create general entry if material in filming 2 _may_ exist
# (filming 1 is already completely evaluated, so skip if nothing was
# found)
if ( not @filmsectionlist ) {
next if $filming == 1;
next unless $folder->company_may_have_material('filming2');
# create an "empty" entry with link to filmlist
my %entry = (
"is_$lang" => 1,
filming_title => $filming_ref->{title}{$lang},
legal => $filming_ref->{legal}{$lang},
filmlist_link => "/film/h${filming}_co.de.html",
);
push( @{ $tmpl_var{filming_loop} }, \%entry );
next;
}
my @filmsection_loop;
foreach my $section ( sort @filmsectionlist ) {
my $film_id = substr( $section->{'@id'}, 25 );
my $entry = {
"is_$lang" => 1,
filmviewer_url => "https://pm20.zbw.eu/film/$film_id",
film_id => $film_id,
first_img => $section->{title},
};
push( @filmsection_loop, $entry );
}
my %filming_data = (
"is_$lang" => 1,
filming_title => $filming_ref->{title}{$lang},
legal => $filming_ref->{legal}{$lang},
filmsection_loop => \@filmsection_loop,
total_number_of_images => $folder->get_film_img_count($filming),
);
push( @{ $tmpl_var{filming_loop} }, \%filming_data );
}
# may microfiche data exist?
if ( $folder->company_may_have_material('microfiche') ) {
$tmpl_var{microfiche_period} = "1961-1980";
}
}
$tmpl_var{signature} = $folderdata_raw->{notation};
# mark folders for which only metadata exists
if ( not $tmpl_var{doc_counts}
and not $tmpl_var{filming_loop}
and not $tmpl_var{microfiche_period} )
{
$tmpl_var{metadata_only} = 1;
}
$tmpl->clear_params;
$tmpl->param( \%tmpl_var );
##print Dumper \%tmpl_var;
# write file for the folder
my $fn = write_page( $type, $lang, $folder, $tmpl );
# collect URLs of pages to add in sitemap
if ( $tmpl_var{'doc_counts'} and $pages_for_sitemap_ref ) {
$fn =~ s/\.md$/.html/;
$fn =~ s|/pm20/web/|./|;
push( @{$pages_for_sitemap_ref}, "$fn" );
}
}
}
sub load_ids {
my $coll_id = shift;
my $folder_id = shift;
# create a list of numerical keys for each collection
my $data = decode_json( $FOLDER_DATA->slurp );
foreach my $entry ( @{ $data->{'@graph'} } ) {
$entry->{identifier} =~ m/^(co|pe|sh|wa)\/(\d{6}(?:,\d{6})?)$/;
push( @{ $coll_id->{$1} }, $2 );
# and create a lookup table for all existing keys
$folder_id->{ $entry->{identifier} } = 1;
}
}
sub usage {
print "Usage: $0 {folder-id}|{collection}|ALL\n";
exit 1;
}
sub write_page {
my $type = shift || die "param missing";
my $lang = shift || die "param missing";
my $folder = shift || die "param missing";
my $tmpl = shift || die "param missing";
my $page_dir = $folder->get_folder_hashed_path();
$page_dir = $FOLDER_WEBROOT->child($page_dir);
my $page_file = $page_dir->child("about.$lang.md");
# remove blank lines (necessary for pipe tables)
# within fenced block
my $lines = $tmpl->output();
$lines =~ m/\A(.*?::: .*?\n)(.*)(\n:::\n.*)\z/ms;
my $start = $1;
my $fenced = $2;
my $end = $3;
$fenced =~ s/\n+/\n/mg;
$lines = "$start$fenced$end";
$page_file->spew_utf8($lines);
##print "written $page_file\n";
return $page_file;
}
sub get_field_values {
my $lang = shift || die "param missing";
my $folderdata_raw = shift || die "param missing";
my $field = shift || die "param missing";
my @field_values;
foreach my $field_ref ( @{ $folderdata_raw->{$field} } ) {
next unless $field_ref->{'@language'} eq $lang;
push( @field_values, $field_ref->{'@value'} );
}
my $values = join( '; ', @field_values );
return $values;
}
sub get_company_relations {
my $lang = shift || die "param missing";
my $folder = shift || die "param missing";
my $folder_id = shift || die "param missing";
my @field_entries;
my $folderdata_raw = $folder->get_folderdata_raw;
foreach my $field_ref (@company_relations) {
my $field_name = $field_ref->{field};
next unless $folderdata_raw->{$field_name};
foreach my $occ ( @{ $folderdata_raw->{$field_name} } ) {
my $folder2 = ZBW::PM20x::Folder->new_from_uri( $occ->{url} );
my %entry = (
field_label => $field_ref->{label}{$lang},
name => $occ->{name},
);
# create link only if there is data for the linked folder
if ( $folder_id->{ $folder2->{folder_id} } ) {
my $path =
$folder->get_relpath_to_folder($folder2)->child("/about.$lang.html");
$entry{url} = "$path";
}
push( @field_entries, \%entry );
}
}
return \@field_entries;
}
sub parse_folderdata {
my $lang = shift || die "param missing";
my $collection = shift || die "param missing";
my $folder = shift || die "param missing";
my $folderdata_raw = $folder->get_folderdata_raw;
my $folderdata;
my $extension = "";
# set default name for "about"
$folderdata->{name} = $folder->get_folderlabel($lang);
if ( get_wd_uri($folderdata_raw) ) {
if ( $collection eq 'pe' or $collection eq 'co' ) {
# uri of the person or organization item
$folderdata->{wikidata} = get_wd_uri($folderdata_raw);
} else {
$folderdata->{wikidata_folder_item} = get_wd_uri($folderdata_raw);
}
}
# dates (for pe and co)
foreach my $field (qw/ birthDate deathDate foundingDate dissolutionDate/) {
if ( my $date = $folderdata_raw->{$field} ) {
$date =~ s/(.*)?T00:00:00$/$1/;
$folderdata->{$field} = $date;
}
}
if ( $collection eq 'pe' ) {
##print Dumper $folderdata_raw;
my $name = $folderdata->{name};
if ( $name =~ m/^(.*)?, (.*)$/ and $name =~ m/^</ ) {
$folderdata->{name} = "$2 $1";
}
my $from_to = $folderdata_raw->{dateOfBirthAndDeath};
if ($from_to) {
$extension = $from_to;
}
# for description property
my @desc_parts;
# hasOccupation entry has insufficient and invalid schema.org markup, can
# be used only for description
if ( $lang eq 'de' and $folderdata_raw->{hasOccupation} ) {
push( @desc_parts, $folderdata_raw->{hasOccupation} );
}
if ( $folderdata_raw->{activity} ) {
my $activities =
join( ', ', @{ get_activities( $lang, $folderdata_raw ) } );
if ($from_to) {
$extension = "$extension; $activities";
} else {
$extension = $activities;
}
push( @desc_parts, "$activities" );
}
$folderdata->{foldername} = "$folderdata->{name} ($extension)";
if ( $folderdata_raw->{nationality} ) {
foreach my $field_ref ( @{ $folderdata_raw->{nationality} } ) {
next unless $field_ref->{'@language'} eq $lang;
$folderdata->{nationality} = {
'@type' => 'Country',
name => $field_ref->{'@value'},
};
}
}
# pretty description
if ( scalar(@desc_parts) gt 1 ) {
$folderdata->{description} = "$desc_parts[0]. ($desc_parts[1])";
} else {
$folderdata->{description} = $desc_parts[0];
}
}
if ( $collection eq 'co' ) {
##print Dumper $folderdata_raw;
my $from_to = $folderdata_raw->{fromTo};
if ($from_to) {
$extension = $from_to;
}
if ( $folderdata_raw->{location} ) {
## use only first location
my @locations = get_field_values( $lang, $folderdata_raw, 'location' );
$folderdata->{location} = {
'@type' => 'Place',
name => $locations[0],
};
## create/extend extension
if ($from_to) {
$extension = "$extension; ";
}
$extension .= join( ', ', @locations );
}
$folderdata->{foldername} = "$folderdata->{name} ($extension)";
if ( $folderdata_raw->{industry} ) {
my @industries = get_field_values( $lang, $folderdata_raw, 'industry' );
$folderdata->{description} = join( ', ', @industries );
}
}
if ( $collection eq 'sh' ) {
$folderdata->{name} =~ m/^(.*)? : (.*)$/;
$folderdata->{name} = $1;
$folderdata->{foldername} = $folder->get_folderlabel($lang);
if ( get_wd_uri( $folderdata_raw->{country} ) ) {
$folderdata->{wikidata} = get_wd_uri( $folderdata_raw->{country} );
}
}
if ( $collection eq 'wa' ) {
$folderdata->{name} =~ m/^(.*)? : (.*)$/;
my $ware = $1;
my $geo = $2;
$folderdata->{name} = $ware;
if ( $geo =~ m/^(Welt|World)$/ ) {
$folderdata->{foldername} = $ware;
} else {
$folderdata->{foldername} = "$ware in $geo";
}
if ( get_wd_uri( $folderdata_raw->{ware} ) ) {
$folderdata->{wikidata} = get_wd_uri( $folderdata_raw->{ware} );
}
}
return $folderdata;
}
sub add_meta_description {
my $lang = shift || die "param missing";
my $folderdata = shift || die "param missing";
my $desc = (
$lang eq 'en'
? "Dossier about $folderdata->{foldername}"
: "Dossier zu $folderdata->{foldername}"
);
$desc .= (
$lang eq 'en'
? ". From German and international press, 1908-1949."
: ". Aus deutscher und internationaler Presse, 1908-1949."
);
##print "$desc\n";
return $desc;
}
sub add_schema_jsonld {
my $lang = shift || die "param missing";
my $collection = shift || die "param missing";
my $folderdata = shift || die "param missing";
my %schema_type = (
pe => "Person",
co => "Organization",
sh => "Country",
wa => "ProductGroup"
);
my $schema_data_ref = {
'@type' => "CreativeWork",
name => $folderdata->{foldername},
isPartOf => {
'@type' => 'Collection',
name => (
$lang eq 'en'
? '20th Century Press Archives'
: 'Pressearchiv 20. Jahrhundert'
),
sameAs => 'http://www.wikidata.org/entity/Q36948990',
},
};
my $about = { '@type' => $schema_type{$collection}, };
foreach my $prop (
qw / name description nationality birthDate deathDate foundingDate dissolutionDate location /
)
{
if ( $folderdata->{$prop} ) {
$about->{$prop} = $folderdata->{$prop};
}
}
if ( $collection eq 'pe' or $collection eq 'co' ) {
$about->{name} = $folderdata->{name};
if ( $folderdata->{wikidata} ) {
$about->{sameAs} = $folderdata->{wikidata};
}
}
if ( $collection eq 'sh' or $collection eq 'wa' ) {
if ( $folderdata->{wikidata_folder_item} ) {
$schema_data_ref->{sameAs} = $folderdata->{wikidata_folder_item};
}
if ( $folderdata->{wikidata} ) {
$about->{sameAs} = $folderdata->{wikidata};
}
## replace Country with City
if ( $folderdata->{name} =~ m/(Hamburg|Berlin)/ ) {
$about->{'@type'} = 'City';
}
}
$schema_data_ref->{about} = $about;
my $schema_ld = {
'@context' => 'https://schema.org/',
'@graph' => [$schema_data_ref],
};
# will be utf8-encoded later in template
return decode( 'UTF-8', encode_json($schema_ld) );
}
sub add_jsonld {
my $folderdata_raw = shift || die "param missing";
my $ld = {
'@context' => 'https://pm20.zbw.eu/schema/context.jsonld',
'@graph' => [$folderdata_raw],
};
# will be utf8-encoded later in template
return decode( 'UTF-8', encode_json($ld) );
}
sub get_wd_uri {
my $folderdata_raw = shift || die "param missing";
my $uri;
foreach my $ref ( @{ $folderdata_raw->{exactMatch} } ) {
next unless $ref->{'@id'} =~ m{wikidata.org/entity/Q\d+$};
$uri = $ref->{'@id'};
}
return $uri;
}
sub get_activities {
my $lang = shift || die "param missing";
my $folderdata_raw = shift || die "param missing";
my @field_values;
foreach my $field_ref ( @{ $folderdata_raw->{activity} } ) {
my @entry;
foreach my $part (qw/ location about /) {
if ( not $field_ref->{$part} ) {
warn "missing activity $part", Dumper $field_ref;
next;
}
foreach my $subfield_ref ( @{ $field_ref->{$part} } ) {
next unless $subfield_ref->{'@language'} eq $lang;
push( @entry, $subfield_ref->{'@value'} );
}
}
push( @field_values, join( ' - ', @entry ) );
}
return \@field_values;
}