-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZIM.pm
1159 lines (1040 loc) · 48.6 KB
/
ZIM.pm
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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package ZIM;
# == ZIM.pm ==
# based on zimHttpServer.pl written by Pedro González (2012/04/06)
# turned into ZIM.pm by Rene K. Mueller (2020/03/28) with enhancements as listed below
#
# License: see LICENSE file
#
# Description:
# provides basic OO interface to ZIM files as provided by kiwix.org
#
# History:
# 2020/04/06: 0.0.12: server(): async snippet retrieval (experimental)
# 2020/04/06: 0.0.11: more detailed search results in server()
# 2020/04/04: 0.0.10: in server() don't resolve redirects internally, but via browser (*.stackexchange.zim rely on it)
# 2020/04/03: 0.0.9: adding url2id cache, fixing file-not-found for server() library operation
# 2020/03/31: 0.0.8: preliminary 64bit cluster size support to support large fulltext indexes (>4GB), improved server() web-gui supporting library (multiple zim files)
# 2020/03/30: 0.0.7: support large article extraction direct to file (without large memory consumption) for wikipedia.zim xapian indices
# 2020/03/30: 0.0.6: preliminary support for library (multiple zim) for server()
# 2020/03/30: 0.0.5: renaming methods: article(url) and articleById(n)
# 2020/03/30: 0.0.4: further code clean up, REST: enable CORS by default, offset & limit considered
# 2020/03/29: 0.0.3: server() barely functional
# 2020/03/29: 0.0.2: fts() with kiwix full text xapian-based indexes (fts and title) support
# 2020/03/28: 0.0.1: initial version, just using zimHttpServer.pl and objectivy it step by step, added info() to return header plus some additional info
our $NAME = "ZIM";
our $VERSION = '0.0.12.c';
use strict;
use Search::Xapian ':all';
use Time::HiRes 'time';
# use IO::Uncompress::AnyUncompress qw(anyuncompress $AnyUncompressError);
use JSON;
use IO::Socket;
use IO::Select;
sub new {
my($class) = shift;
my $self = { };
my $arg = shift;
bless($self,$class);
foreach my $k (keys %$arg) {
$self->{$k} = $arg->{$k};
}
@{$self->{error}} = ();
if($arg->{library}) {
# -- create internal catalog with metadata for webgui
foreach my $f (@{$arg->{library}}) {
my $b = $f; $b =~ s/\.zim$//; $b =~ s/.+\///;
my $me = new ZIM({file=>$f,verbose=>$self->{verbose}});
print "INF: library: adding $b ($f)\n" if($self->{verbose});
if($self->{normalize} && $me->{name}) {
print "INF: normalize $b -> $me->{name}\n" if($self->{verbose});
$b = $me->{name};
}
$self->{catalog}->{$b} = $me;
$me->entry($me->{header}->{mainPage});
$me->{home} = "/$b/".$me->{article}->{namespace}."/".$me->{article}->{url};
my $title = $me->{title};
$title =~ s/([\w ]{12,24})\s[\W\S].*/$1/; # -- truncate title sensible
$me->error(1); # -- clear error if favicon(s) are not found
push(@{$self->{_catalog}},{ base=>$b, home=>$me->{home}, title=>$title, meta=>$me->{header}, desc => $me->{desc}, icon => "$b$me->{icon}", lan => $me->{lan} });
}
return $self;
}
die "ZIM.pm: ERR: zim file must end with .zim, '$arg->{file}' does not\n" unless($arg->{file}=~/\.zim$/);
open (my $fh, $arg->{file}) || die "ZIM.pm: ERR: file not found <$arg->{file}>\n";
$self->{fh} = $fh;
# -- read zim header
read($fh, $_, 4); $self->{header}->{"magicNumber"} = unpack("c*"); # ZIM\x04
read($fh, $_, 4); $self->{header}->{"version"} = unpack("I");
read($fh, $_, 16); $self->{header}->{"uuid"} = unpack("H*");
read($fh, $_, 4); $self->{header}->{"articleCount"} = unpack("I");
read($fh, $_, 4); $self->{header}->{"clusterCount"} = unpack("I");
read($fh, $_, 8); $self->{header}->{"urlPtrPos"} = unpack("Q");
read($fh, $_, 8); $self->{header}->{"titlePtrPos"} = unpack("Q");
read($fh, $_, 8); $self->{header}->{"clusterPtrPos"} = unpack("Q");
read($fh, $_, 8); $self->{header}->{"mimeListPos"} = unpack("Q");
read($fh, $_, 4); $self->{header}->{"mainPage"} = unpack("I");
read($fh, $_, 4); $self->{header}->{"layoutPage"} = unpack("I");
read($fh, $_, 8); $self->{header}->{"checksumPos"} = unpack("Q");
$self->{header}->{file} = $arg->{file};
@_ = stat($arg->{file}); # -- retrieve metadata of zim file itself
$self->{header}->{filesize} = $_[7];
$self->{header}->{mtime} = $_[9];
# -- load MIME TYPE LIST
my @mime;
seek($fh, $self->{header}->{"mimeListPos"}, 0);
$/ = "\x00";
for(my $a=0; 1; $a++){
my $b = <$fh>;
chop($b);
last if($b eq "");
$mime[$a] = $b;
}
$self->{mime} = \@mime;
$/ = "\n";
my $me = $self;
my $b = $arg->{file}; $b =~ s/\.zim$//;
$b =~ s/.*\///;
$me->entry($me->{header}->{mainPage});
$self->{home} = "/".$me->{article}->{namespace}."/".$me->{article}->{url};
$self->{title} = $self->{title} || $me->article("/M/Title") || $me->article("/M/Creator") || $b;
$self->{desc} = $self->{desc} || $me->article("/M/Description") || "";
$self->{lan} = $self->{lan} || lc($me->article("/M/Language")) || "en";
$self->{lan} = substr($self->{lan},0,2) if(length($self->{lan})>2);
$self->{lan} = 'en' if($self->{lan} eq 'mu'); # -- multiple: fallback to en
$self->{name} = $me->article("/M/Name") || $self->{name} || $b;
$b = $self->{name} if($self->{normalize});
my $icon;
foreach my $i ('/-/favicon','/I/favicon.png') {
$icon = $i, last if($me->article($i));
}
$self->error(1); # -- clear error if favicon(s) are not found
$self->{icon} = $self->{icon} || $icon;
# -- if we are just one, make single catalog entry
push(@{$self->{_catalog}},{ home=>$self->{home}, title=>$self->{title}, meta=>$self->{header}, icon => $self->{icon}, desc => $self->{desc}, lan => $self->{lan} });
return $self;
}
sub info {
my $self = shift;
return $self->{header};
}
sub error {
my $self = shift;
my $clear = shift;
my $s = join("\n",@{$self->{error}});
$self->{error} = [] if($clear);
return $s;
}
# -- read ARTICLE NUMBER (sort by url) into URL POINTER LIST
# return ARTICLE NUMBER POINTER
sub url_pointer {
my $self = shift;
my $article = shift;
my $fh = $self->{fh};
#push(@{$self->{error}},"article number $article exceeds $self->{header}->{articleCount}"), return ''
return -1 if $article >= $self->{header}->{"articleCount"};
my $pos = $self->{header}->{"urlPtrPos"};
$pos += $article*8;
seek($fh, $pos, 0);
read($fh, $_, 8); my $ret = unpack("Q");
return $ret;
}
# -- read ARTICLE NUMBER (sort by title) into TITLE POINTER LIST
# return ARTICLE NUMBER (not pointer)
sub title_pointer {
my $self = shift;
my $article_by_title = shift;
my $fh = $self->{fh};
return -1 if $article_by_title >= $self->{header}->{"articleCount"};
my $pos = $self->{header}->{"titlePtrPos"};
$pos += $article_by_title*4;
seek($fh, $pos,0);
read($fh, $_, 4); my $ret = unpack("I");
return $ret;
}
# -- read ARTICLE NUMBER
# load ARTICLE ENTRY that is point by ARTICLE NUMBER POINTER
# or load REDIRECT ENTRY
sub entry {
# directory entries
# article entry
# redirect entry
my $self = shift;
my $article = shift;
my $fh = $self->{fh};
$self->{article} = undef;
$self->{article}->{number} = $article;
my $pos = $self->url_pointer($article);
return $pos if($pos<0);
seek($fh, $pos,0);
read($fh, $_, 2); $self->{article}->{"mimetype"} = unpack("s");
read($fh, $_, 1); $self->{article}->{"parameter_len"} = unpack("H*");
read($fh, $_, 1); $self->{article}->{"namespace"} = unpack("a");
read($fh, $_, 4); $self->{article}->{"revision"} = unpack("I");
if($self->{article}->{"mimetype"} < 0){
read($fh, $_, 4); $self->{article}->{"redirect_index"} = unpack("I");
} else {
read($fh, $_, 4); $self->{article}->{"cluster_number"} = unpack("I");
read($fh, $_, 4); $self->{article}->{"blob_number"} = unpack("I");
}
$/ = "\x00";
$self->{article}->{"url"} = <$fh>;
$self->{article}->{"title"} = <$fh>;
chop($self->{article}->{"url"});
chop($self->{article}->{"title"});
$/ = "\n";
read($fh, $_, $self->{article}->{"parameter_len"}); $self->{article}->{"parameter"} = unpack("H*");
}
# -- read CLUSTER NUMBER into CLUSTER POINTER LIST
# return CLUSTER NUMBER POINTER
sub cluster_pointer {
my $self = shift;
my $cluster = shift;
my $fh = $self->{fh};
if($cluster >= $self->{header}->{"clusterCount"}) {
return $self->{header}->{"checksumPos"}
#push(@{$self->{error}},"cluster_pointer: #$cluster exceeds maximum of $self->{header}->{clusterCount}");
#die "ZIM.pm: cluster #$cluster exceeds maximum of $self->{header}->{clusterCount}\n";
#return 0;
}
my $pos = $self->{header}->{"clusterPtrPos"};
$pos += $cluster*8;
seek($fh, $pos,0);
read($fh, $_, 8); my $ret = unpack("Q");
return $ret;
}
# -- read CLUSTER NUMBER
# decompress CLUSTER
# read BLOB NUMBER
sub cluster_blob {
my $self = shift;
my $cluster = shift;
my $blob = shift;
my $opts = shift;
my $fh = $self->{fh};
my $ret;
print "INF: #$$: cluster blob (cluster=$cluster)\n" if($self->{verbose}>2);
my $pos = $self->cluster_pointer($cluster);
my $size = $self->cluster_pointer($cluster+1) - $pos - 1;
print "INF: #$$: cluster blob (cluster=$cluster, pos=$pos, size=$size)\n" if($self->{verbose}>2);
seek($fh, $pos, 0);
my %cluster;
read($fh, $_, 1); $cluster{compression_type} = unpack("C");
$cluster{offset_size} = $cluster{compression_type} & (1<<4) ? 8 : 4; # -- see https://openzim.org/wiki/ZIM_file_format
# print "$cluster{compression_type}:$cluster{offset_size}\n";
# -- compressed?
if(($cluster{compression_type} & 0x0f) == 4) {
my $data;
# -- FIXIT: do not read large compressed files into memory, but small chunks only to files
read($fh, $data, $size);
# -- FIXIT: use XZ library without creating tmp file (seems ::Uncompress work only via files, not data direct)
if(1) { # -- old code
# -- extract data separately into a file, uncompress, and extract part of it
my $tmp = "/tmp/zim_tmpfile_cluster$cluster-pid$$";
open(DATA, ">$tmp.xz");
print DATA $data;
close(DATA);
#`xz -d -f $tmp.xz`;
# -- decompress it
if(fork()==0) {
exec('xz','-d','-f',"$tmp.xz");
}
wait;
open(DATA, $tmp);
seek(DATA, $blob*4, 0);
read(DATA, $_, 4); my $posStart = unpack("I");
read(DATA, $_, 4); my $posEnd = unpack("I");
seek(DATA, $posStart, 0);
read(DATA, $ret, $posEnd-$posStart); # -- read into memory
close(DATA);
unlink $tmp;
} else { # -- new code
if(0) {
my $s = anyuncompress $data => $ret; # -- throws error (not usable): can't handle data, only files
my $off = $blob*4;
$_ = substr($ret,$off,4); my $posStart = unpack("I"); $off += 4;
$_ = substr($ret,$off,4); my $posEnd = unpack("I"); $off += 4;
$ret = substr($ret,$posStart,$posEnd-$posStart);
} else { # -- still cumbersome, and additionally won't work (yet)
my $file = "/tmp/zim_tmpfile_cluster$cluster-pid$$";
open(my $fhz,">","$file.xz");
print $fhz $data;
close($fhz);
my $z = new IO::Uncompress::AnyUncompress("$file.xz"); # -- must be a file, can't handle data itself
if($z) {
seek($z, $blob*4, 0);
read($z, $_, 4); my $posStart = unpack("I");
read($z, $_, 4); my $posEnd = unpack("I");
seek($z, $posStart, 0);
read($z, $ret, $posEnd-$posStart);
close($z);
} else {
# -- uncomment next 2 lines as soon IO::Uncompress::AnyUncompress really works
#print STDERR "ZIM.pm: ERR: #$$: XZ uncompress failed: $AnyUncompressError\n";
#push(@{$self->{error}},"XZ uncompress failed: $AnyUncompressError");
$ret = '';
}
unlink $file;
}
}
if($opts && $opts->{dest}) {
print "INF: #$$: writing $opts->{dest}\n" if($self->{verbose});
open(my $fhx,">",$opts->{dest});
print $fhx $ret;
close $fhx;
} else {
return $ret;
}
} else {
my $data;
if(1 && ($size > 200_000_000 || ($opts && $opts->{dest}))) {
if($opts && $opts->{dest}) {
print "INF: #$$: writing $opts->{dest}\n" if($self->{verbose});
my $off = tell $fh;
seek($fh, $off+$blob*4, 0);
read($fh, $_, $cluster{offset_size}); my $posStart = unpack($cluster{offset_size}==4?"I":"Q");
read($fh, $_, $cluster{offset_size}); my $posEnd = unpack($cluster{offset_size}==4?"I":"Q");
seek($fh, $off+$posStart, 0);
$size = $posEnd-$posStart;
print "INF: #$$: extract chunk size=$size (offset=$off, start=$posStart, end=$posEnd)\n" if($self->{verbose}>2);
open(OUT,">",$opts->{dest});
my $bs = 4096*1024; # -- use 4MB chunks
while(1) {
my $n = read($fh,$ret,$size >= $bs ? $bs : $size);
$size -= $n;
print OUT $ret;
last if($size <= 0 || $n <= 0);
}
close OUT;
return;
} else {
print STDERR "ZIM.pm: ERR: file too large (".fnum3($size)." bytes) to read into memory, rather extract to a file\n";
exit 0;
}
} else {
read($fh, $data, $size);
}
$_ = substr $data, $blob*4, 4; my $posStart = unpack("I");
$_ = substr $data, $blob*4+4, 4; my $posEnd = unpack("I");
$ret = substr $data, $posStart, $posEnd-$posStart;
if($opts && $opts->{dest}) {
print "INF: #$$: writing $opts->{dest}\n" if($self->{verbose});
open(my $fhx,">",$opts->{dest});
print $fhx $ret;
close $fhx;
}
return $ret;
}
}
# -- read ARTICLE NUMBER
# return DATA
sub articleById {
my $self = shift;
my $an = shift;
my $opts = shift;
print "INF: #$$: articleById: #$an\n" if($self->{verbose}>1);
while(1) { # -- resolve redirects
my $p = $self->entry($an);
#print to_json($self->{article},{pretty=>1,canonical=>1});
push(@{$self->{error}},"article not found #$an"), return '' if($p<0);
if(defined $self->{article}->{"redirect_index"} && !($opts && $opts->{no_redirect})) {
$an = $self->{article}->{"redirect_index"};
} else {
return $opts && $opts->{metadata} ?
$self->{article} :
$opts && $opts->{no_redirect} && $self->{article}->{redirect_index} ?
$self->{article} :
$self->cluster_blob($self->{article}->{"cluster_number"}, $self->{article}->{"blob_number"}, $opts);
last;
}
}
}
# -- search url
# return DATA
sub article {
my $self = shift;
my $url = shift;
my $opts = shift;
my $max = $self->{header}->{"articleCount"};
my $min = 0;
my $an;
if(!$url) { # -- no url provided, then display mainPage
return $self->articleById($self->{header}->{mainPage},$opts);
}
if(defined $self->{_url2id_cache}->{$url}) {
$an = $self->{_url2id_cache}->{$url};
} else {
while(1) { # -- simple binary search
$an = int(($max+$min)/2);
my $p = $self->entry($an,$opts);
push(@{$self->{error}},"article <$url> not found"), return '' if($p<0);
if("/$self->{article}->{namespace}/$self->{article}->{url}" gt "$url") {
$max = $an-1;
} elsif("/$self->{article}->{namespace}/$self->{article}->{url}" lt "$url") {
$min = $an+1;
} else {
last;
}
# -- binary search (above) failed: we need to create an index, and fetch the title to compare
if(0 && $max < $min){
$self->{article} = undef;
$self->{article}->{url} = "pattern=$url";
$self->{article}->{namespace} = "SEARCH";
return "", unless $url =~ /^\/A/;
# ($url) = grep {length($_)>1} split(/[\/\.\s]/, $url);
$url =~ s#/A/##;
my $m;
foreach my $f ($self->index($url)) {
print "INF: #$$: > $_" if($self->{verbose});
$m .= "<a href=\"$_\">$_</a><br/>\n";
}
$self->{article}->{mimetype} = 0; # need for Content-Type: text/html; charset=utf-8
return $m;
}
if($max < $min) {
push(@{$self->{error}},"'$url' not found");
$self->{_url2id_cache}->{$url} = -1; # -- make not-found also a hit: -1
return '';
}
}
$self->{_url2id_cache}->{$url} = $an;
}
push(@{$self->{error}},"'$url' not found"), return '' if($an<0);
return $self->articleById($an,$opts);
}
sub make_index {
my $self = shift;
# -- create index
my $file = $self->{file};
$file =~ s/zim$/index/;
unless(-e $file) {
$| = 1;
print "INF: #$$: writing $file (index)\n" if($self->{verbose});
open(INDEX, ">$file");
for(my $n = 0; $n<$self->{header}->{"articleCount"}; $n++) {
$self->entry($n);
my $url = "/$self->{article}->{namespace}/$self->{article}->{url}";
print INDEX "$url\n";
print "\r$n" if($self->{verbose} && ($n%100) == 0);
$self->{_url2id_cache}->{$url} = $n;
}
print "\n" if($self->{verbose});
$| = 0;
close(INDEX);
}
}
sub index {
my $self = shift;
my $url = shift;
my $file = $self->{file};
$file =~ s/zim$/index/;
my @r;
$self->make_index();
# -- list or search index
print "INF: #$$: searching '$url' in $file\n" if($self->{verbose});
my $n = 0;
open(INDEX, "$file");
while(<INDEX>){
chop;
# print "INF: #$$: > $_\n" if($self->{verbose}>3);
if(defined $url) {
push(@r,$_) if($self->{case_insens} && /$url/i || /$url/);
} else {
push(@r,$_);
$self->{_url2id_cache}->{$_} = $n++;
}
}
close(INDEX);
return \@r;
}
my %_xapian_lan = ( # -- https://xapian.org/docs/apidoc/html/classXapian_1_1Stem.html
ar => 'arabic',
hy => 'armenian',
eu => 'basque',
ca => 'catalan',
da => 'danish',
nl => 'dutch',
en => 'english',
lovins => 'english_lovins',
porter => 'english_porter',
fi => 'finnish',
fr => 'french',
de => 'german',
hu => 'hungarian',
id => 'indonesian',
ga => 'irish',
it => 'italian',
lt => 'lithuanian',
ne => 'nepali',
no => 'norwegian',
nb => 'norwegian',
nn => 'norwegian',
pt => 'portuguese',
ro => 'romanian',
ru => 'russian',
es => 'spanish',
sv => 'swedish',
ta => 'tamil',
tr => 'turkish',
);
sub fts {
my $self = shift;
my $q = shift;
my $opts = shift;
my $file;
$file->{fulltext} = $self->{file}; $file->{fulltext} =~ s/zim$/fulltext.xapian/;
$file->{title} = $self->{file}; $file->{title} =~ s/zim$/title.xapian/;
if(!-e $file->{fulltext}) {
print "INF: #$$: extract /X/fulltext/xapian -> $file->{fulltext}\n";
$self->article("/X/fulltext/xapian",{dest=>$file->{fulltext}});
$self->error(1); # -- in case extraction failed
if(!-e $file->{fulltext}) { # -- failed? let's try another url (older)
print "INF: #$$: extract /Z//fulltextIndex/xapian -> $file->{fulltext}\n";
$self->article("/Z//fulltextIndex/xapian",{dest=>$file->{fulltext}});
$self->error(1); # -- in case extraction failed
}
}
if(!-e $file->{title}) {
print "INF: #$$: extract /X/title/xapian -> $file->{title}\n";
$self->article("/X/title/xapian",{dest=>$file->{title}});
$self->error(1); # -- in case extraction failed
}
my $file_xapian = $file->{$opts->{index}||'fulltext'};
$file_xapian = $file->{title} if(!-e $file_xapian && -e $file->{title}); # -- fallback
print "INF: #$$: xapian index $file_xapian\n" if($self->{verbose}>1);
if(1) {
if(!-e $file_xapian) {
print STDERR "WARN: #$$: no xapian index available for $self->{header}->{file}\n" unless($self->{quiet});
if($opts && $opts->{fallback}) {
my @r = @{$self->index($q)}; # -- fallback, try to provide some results
my $n = 0;
@r = map {
my $a;
$a->{data} = $a->{url} = $_;
$a->{title} = $a->{url}; $a->{title} =~ s/_/ /g; $a->{title} =~ s/^\/A\///; $a->{title} =~ s/\//: /g; $a->{title} =~ s/\.html?$//;
$a->{score} = (length($a->{title})-index(lc $a->{title},lc $q)*0.05) / length($a->{title}) if(length($a->{title}));
$a->{rank} = $n++;
$a;
} @r;
@r = sort { $b->{score} <=> $a->{score} } @r;
return \@r;
}
return [];
}
my $db = Search::Xapian::Database->new($file_xapian);
# -- this is bad: stemmer requires language setting, so we need to know it in advance
# see https://xapian.org/docs/apidoc/html/classXapian_1_1Stem.html
# -- 1) we set stemmer per index, given we have a single language per set (bad idea: gutenberg_mul_all has multiple languages)
# 2) we define overall language for all indices (bad idea: we might miss results)
# => stemming is bad
my $stem = Search::Xapian::Stem->new(($opts && $_xapian_lan{$opts->{lan}})||$_xapian_lan{lc($self->{lan})}||'en');
my $enq;
if(1) {
my $qp = Search::Xapian::QueryParser->new();
$qp->set_stemmer($stem);
$qp->set_database($db);
$qp->set_stemming_strategy(STEM_ALL); # -- essential
$enq = Search::Xapian::Enquire->new($db);
$enq->set_query($qp->parse_query($q));
} else {
$q = $stem->stem_word($q);
$enq = $db->enquire($q);
}
print "INF: #$$: xapian query: ".$enq->get_query()->get_description()."\n" if($self->{verbose}>1);
$opts = $opts || { };
my $meta;
my $mset = $enq->get_mset($opts->{offset}||0,$opts->{limit}||100);
$meta->{total} += $mset->get_matches_estimated();
my @r = $mset->items();
#my @r = $enq->matches($opts->{offset}||0,$opts->{limit}||100);
my @re;
foreach my $m (@r) {
my $doc = $m->get_document();
my $e = { _id => $m->get_docid(), rank => $m->get_rank()+1, weight => $m->get_weight(), score => $m->get_percent()/100, url => "/".$doc->get_data() };
$self->article($e->{url},{metadata=>1});
#foreach my $k (keys %{$self->{article}}) {
foreach my $k (qw(title revision number namespace mimetype)) {
$e->{$k} = $self->{article}->{$k};
}
$e->{id} = $e->{number}; delete $e->{number};
$e->{mimetype} = $e->{mimetype} >= 0 ? $self->{mime}->[$e->{mimetype}] : "";
#print to_json($e,{pretty=>1,canonical=>1});
push(@re,$e);
}
return wantarray ? (\@re,$meta) : \@re;
}
#if(!-e $file_tt && $self->article("/X/title/xapian",{dest=>$file_title})) {
#}
}
sub server {
my $self = shift;
my $fh = $self->{fh};
$self->{port} = $self->{port} || 8080;
$self->{ip} = $self->{ip} || '0.0.0.0';
if(0) { # -- old code
my ($server_ip, $server_port) = ($self->{ip}, $self->{port});
my ($PF_UNIX, $PF_INET, $PF_IMPLINK, $PF_NS) = (1..4) ;
my ($SOCK_STREAM, $SOCK_DGRAM, $SOCK_RAW, $SOCK_SEQPACKET, $SOCK_RDM) = (1..5) ;
my ($d1, $d2, $prototype) = getprotobyname ("tcp");
socket(SSOCKET, $PF_INET, $SOCK_STREAM, $prototype) || die "ZIM.pm: ERR: socket: $!";
bind(SSOCKET, pack("SnCCCCx8", 2, $server_port, split(/\./,$server_ip))) || die "ZIM.pm: ERR: bind: $!";
listen(SSOCKET, 5) || die "ZIM.pm: ERR: connect: $!";
$SIG{CHLD} = 'IGNORE';
$SIG{PIPE} = 'IGNORE';
while(1) {
my $client_addr = accept(CSOCKET,SSOCKET) || die "ZIM.pm: ERR: $!";
last unless fork; # -- parent remains in while(), child exits
}
$self->processRequest(\*CSOCKET);
} else { # -- new code
my $server = IO::Socket::INET->new(
LocalAddr => $self->{ip},
LocalPort => $self->{port},
Type => SOCK_STREAM,
Reuse => 1,
# Blocking => 0,
# Timeout => 0.5,
Listen => 10 )
or die "ZIM.pm: ERROR: can't start a tcp server on $self->{ip}:$self->{port}: $!\n";
$SIG{PIPE} = 'IGNORE';
$SIG{CHLD} = 'IGNORE';
my $rs = new IO::Select();
$rs->add($server);
while(1) {
my($rhs) = IO::Select->select($rs,undef,undef,0.1);
foreach my $rh (@$rhs) {
if($rh==$server) {
my $c = $rh->accept();
$rs->add($c);
} else {
if(fork()==0) {
$self->processRequest($rh);
exit;
}
close($rh);
$rs->remove($rh);
}
}
}
}
}
sub processRequest {
my $self = shift;
my $cs = shift;
my $fh;
# -- we need to reopen zim file as we forked process
if($self->{catalog}) {
foreach my $e (sort keys %{$self->{catalog}}) {
open(my $fh,"<",$self->{catalog}->{$e}->{file});
$self->{catalog}->{$e}->{fh} = $fh;
}
} else {
open($fh,"<",$self->{file});
$self->{fh} = $fh;
}
# $cs->autoflush(1);
while(1) { # -- keep-alive operation (exit only if we fail to send)
my $http_header;
timeout(1,sub {
while(1) { # -- read the http request header
if(0) {
my $m;
recv($cs, $m, 1000, 0);
$http_header .= $m;
last if(length($m)<1000);
} else { # -- read line-wise, performs better than recv()
$_ = <$cs>;
$http_header .= $_;
last if(/^\s*$/);
}
}
});
exit if($@); # -- timeout, exit (child) process
# -- processing request
if($http_header =~ /^GET (.+) HTTP\/1\./){
# -- Request-Line Request HTTP-message
# ("OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "TRACE", "CONNECT", "$token+");
my $url = $1;
print "INF: #$$: server: requested $url\n" if($self->{verbose});
my(@header,$status,$body,$mime);
$status = 200;
if($url =~ /^\/rest\?(.*)/) {
my $in;
my $st = time();
foreach my $kv (split(/&/,$1)) {
if($kv=~/(\w+)=(.*)/) {
my($k,$v) = ($1,$2);
$k =~ s/%(..)/chr(hex($1))/eg;
$v =~ s/%(..)/chr(hex($1))/eg;
$in->{$k} = $v;
} elsif($kv=~/(\w+)/) {
$in->{$1}++;
}
}
my $res = { }; # -- response
$res->{server} = {
name => "zim web-server $::VERSION ($NAME $VERSION)",
time => time(),
date => scalar localtime()
};
if($in->{q}) {
my @r;
$in->{meta}++;
if($self->{catalog}) {
if($in->{content}) {
if($self->{catalog}->{$in->{content}}) {
my($rs,$meta) = $self->{catalog}->{$in->{content}}->fts($in->{q},$in);
@r = map { $_->{icon} = $self->{catalog}->{$in->{content}}->{icon}; $_->{base} = $in->{content}; $_->{url} = "/$in->{content}$_->{url}"; $_ } @$rs;
$res->{results}->{total} = $meta->{total} if($meta && $meta->{total});
}
} else {
foreach my $e (sort keys %{$self->{catalog}}) {
my $me = $self->{catalog}->{$e};
my $st = time();
my($rs,$meta) = $self->{catalog}->{$e}->fts($in->{q},$in);
$res->{results}->{total} += $meta->{total} if($meta && $meta->{total});
push(@r,map { $_->{icon} = $self->{catalog}->{$e}->{icon}; $_->{base} = $e; $_->{url} = "/$e$_->{url}"; $_ } @$rs); # -- rebase
push(@{$res->{server}->{performed}},"fts $e:$in->{q}".sprintf(" %.1fms",(time()-$st)*1000)." ".(ref($meta)&&defined $meta->{total}?$meta->{total}." hits":""));
}
@r = sort { $b->{score}*$b->{weight} <=> $a->{score}*$a->{weight} } @r; # -- sort according score (merge all results)
my $r = 0;
@r = map { $_->{rank} = $r++; $_ } @r; # -- rerank
@r = splice(@r,$in->{offset},$in->{limit}) if($in->{offset}||$in->{limit}); # -- apply limit & offset
}
} else {
my($rs,$meta) = $self->fts($in->{q},$in);
@r = @$rs;
$res->{results}->{total} = $meta->{total} if($meta && $meta->{total});
}
if($in->{snippets}) { # -- let's try to extract relevant snippets
my $st = time();
for(my $i=0; $i<$in->{snippets}; $i++) {
last if($i>=@r);
my $me = $self;
my $u;
if($self->{catalog} && $r[$i]->{base}) {
$me = $self->{catalog}->{$r[$i]->{base}};
$u = $r[$i]->{url};
$u =~ s/\/[^\/]+//;
} else {
$u = $r[$i]->{url};
}
my $body = $me->article($u);
my $headers = [];
foreach(split(/\n/,$body)) {
push(@{$headers->[$1-1]},$2) if(/<h(\d)[^>]*>([^<]+)<\/h/i);
}
# push(@{$headers->[0]},$r[$i]->{title}) unless($headers->[0]);
$body =~ s/<script>[^<]+<\/script>//mg;
$body =~ s/<\/?(p|br)>/ /ig;
$body =~ s/<[^>]+>//g;
$body =~ s/\s+/ /g;
my $exp = '';
my $q = $in->{q};
for(my $n=0; $n<5;) {
if($body =~ s/(.{5,40})($q)(.{5,40})//i) {
$exp .= "..$1<b>$2</b>$3..";
$n++;
} else {
last;
}
}
$r[$i]->{snippet} = $exp;
$r[$i]->{headers} = $headers;
}
push(@{$res->{server}->{performed}},"retrieved ".($in->{snippets}*1)." snippets".sprintf(" %.1fms",(time-$st)*1000));
}
$res->{results}->{hits} = \@r;
} elsif($in->{catalog}) {
$res->{results} = { catalog => $self->{_catalog} ? $self->{_catalog} : [] };
}
$res->{server}->{elapsed} = time()-$st;
$body = to_json($res, { pretty => $in->{_pretty}, canonical => 1});
$mime = 'application/json';
} else {
my $me = $self; # -- me might point later to catalog entry itself
my $base;
$url =~ s/%(..)/chr(hex($1))/eg;
if($self->{catalog}) { # -- dealing with a catalog, determine which entry
if($url =~ s/\/([\w\.\-]+)\//\//) {
$base = $1;
if($self->{catalog}->{$base}) {
$me = $self->{catalog}->{$base};
} else {
push(@{$self->{error}},"unknown catalog entry '$base'");
$body = "unknown catalog entry";
}
} else { # -- provide overview of items in catalog
$body = "<!DOCTYPE html><html><head><title>".($self->{title}?$self->{title}:"ZIM Catalog")."</title><meta name=\"viewport\" content=\"width=device-width\"><link href=\"data:image/x-png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAABUAAAAVAB++UihAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAHFSURBVFiF7Vavq8JgFL3bHuiwLehXLKJgXrGKImIy+gdYDWKyDNvCkn+DGMRgWrFrEsFkXNYJCgoWmeclRd/ew/1yM7wDt9zv3nvO7rZ7P46IQBGCj5L88wRIkkS6rtPhcKDRaESiKIYiAjfTNA2P6HQ6eDx/hz11IJlMPilLpVJveF477moKhQJOpxMAYLfbIZfLvb0D9NPBGEO1WoUkSWGQg7upiAqf9RtGgS8/yY1Gg0qlkuN4RVHINE2b3/MH1O/34QbZbNZWw1cHxuMxrdfrP88TiQSpqkrxeJwMw6Dtdvtr3HsmHM9jMpkAAPb7PfL5vLM5EJT1ej0AgGVZqNVqzgdREFapVGBZFgBAUZRX8cGSM8ZgmiYAYDqdguf58ARwHAdd1wEAm80GjDEnecEJaLVa9/deLped5gVDnslk7ptU0zQ3uf7JBUHAbDYDAKxWK8RisXAFtNttAMDlcoEsy27z/ZGn02kcj0cAgKqqXmr4E3CbdoZhQBTFcAUUi8X7oqnX655q+LoRLZdLkmWZzuczDYfDl/GDwYDm87nN70m5IAiuVjEANJtNWx3P6/h6vVK323WVs1gsbL7/S2nkAr4BqNINAhXMUgwAAAAASUVORK5CYII=\" rel=\"shortcut icon\" type=\"image/x-png\">
</head><style>html{font-family:Helvetica,Sans;margin:0;padding:0}body{background:#eee;margin:1.5em 3em}.icon{float:right;vertical-align:middle;height:2.5em}a,a:visited{color:#444;text-decoration:none}.entry{text-align:left;width:20%;display:inline-block;margin:0.8em 1.2em;border:1px solid #ccc;border-radius:0.3em;padding:0.5em 0.8em;background:#fff;box-shadow:0 0 0.5em 0.1em #888;overflow:hidden;white-space:nowrap}.entry:hover{box-shadow: 0 0 0.5em 0.1em #55c;background:#eef}.entry .title{display:inline-block;width:85%;white-space:nowrap;font-size:1.4em;text-overflow:ellipsis;overflow:hidden;}.desc{display:none;font-size:0.8em;opacity:0.8;text-overflow:ellipsis;overflow:hidden;}.catalog{text-align:center}.meta{margin:0.3em 0;font-size:0.7em;opacity:0.8}.id{font-size:0.8em;opacity:0.5;margin:0.3em 0}.footer{text-align:center;margin-top:3em;font-size:0.8em;opacity:0.7}.zim_summary{margin:0.5em 0;opacity:0.5;font-size:0.8em;text-align:right}</style><body><div class=catalog>";
my($tot);
foreach my $e (@{$self->{_catalog}}) {
my $meta = fnum3($e->{meta}->{articleCount}) . " articles".
"<div class=id>$e->{base} (".fnum3(int($e->{meta}->{filesize}/1024/1024))." MiB)</div>";
$body .= "<a href=\"$e->{home}\"><span class=entry><img class=icon src=\"$e->{icon}\"><span class=title>$e->{title}</span><div class=desc>$e->{desc}</div><div class=meta>$meta</div></span></a>";
$tot->{size} += $e->{meta}->{filesize};
$tot->{articles} += $e->{meta}->{articleCount};
}
$body .= "</div><div class=footer>".
(@{$self->{_catalog}} > 1 ? sprintf("%d sets, %s articles, %s MiB - ",scalar @{$self->{_catalog}},fnum3($tot->{articles}),fnum3(int($tot->{size}/1024/1024))) : "").
"<a href=\"https://github.com/Spiritdude/ZIM\">zim web-server</a> $::VERSION ($NAME $VERSION)</div>".
"</body></html>";
$mime = 'text/html';
}
}
# -- after this point $me points to current zim file
# -- homepage requested?
if((!$body) && $url eq "/") {
$me->entry($me->{header}->{mainPage});
$url = "/".$me->{article}->{namespace}."/".$me->{article}->{url};
push(@header,"Location: $url"); # -- let's redirect browser, so images and script all work properly
$status = 307;
$body = 'redirect';
}
$url = "/-/favicon" if $url eq "/favicon.ico";
# $url =~ s#(/.*?/.*?)$#$1#;
# $url = "/A$url" unless $body || $url =~ "/.*/"; # -- complete url if necessary (disabled)
print "INF: #$$: server: serving ".($base?"$base:":"")."$url\n" if($self->{verbose});
$body = $body || $me->article($url,{no_redirect=>1});
if(ref($body) eq 'HASH') { # -- redirect? let's do it in the browser so call content is properly referenced
$status = 307;
$me->articleById($body->{redirect_index},{no_redirect=>1}); # -- retrieve new entry, so we know the new url
$body = $me->{article};
push(@header,"Location: ".($base ? "/$base" : "") . "/$body->{namespace}/$body->{url}");
$body = 'redirect';
}
$mime = $mime || ($me->{article}->{mimetype} >= 0 ? $me->{mime}->[$me->{article}->{mimetype}] : "text/plain");
if(1 && $mime eq 'text/html') { # -- we need to change/tamper the HTML ...
my $mh = "";
# $mh .= "<base href=\"/$base/\">";
$mh .= "<style>body{margin-top:3em !important}.zim_header{z-index:1000;position:fixed;width:100%;padding:0.5em 1em;text-align:center;background:#bbb;box-shadow:0 0 0.5em 0.1em #888;top:0;left:0;text-decoration:none}.zim_header.small{font-size:0.8em;}.zim_entry{background:#eee;margin:0 0.3em;padding:0.3em 0.6em;border:1px solid #ccc;border-radius:0.3em;text-decoration:none;color:#226}.zim_entry:hover{background:#eee}.zim_entry.selected{background:#eef;border:2px solid}.zim_search{margin:0 0.5em;padding:0.2em 0.3em;background:#ffc;border:1px solid #aa8;border-radius:0.3em;}.zim_search_input,.zim_search_input:focus{padding:0.1em 0.3em;background:none;border:none;outline:none}.zim_content{margin:0 0.5em}.zim_results{z-index:200;display:none;margin:1em 4em;padding:1em 2em;background:#fff;border:1px solid #888;box-shadow: 0 0 0.5em 0.1em #888}.zim_results.active{display:block}#_zim_results_hint{font-size:0.8em;opacity:0.7}.hit{margin-bottom:1.5em}.hit .title{font-size:1.1em;color:#00c;margin:0.25em 0;display:block;}.snippet{font-size:0.8em;opacity:0.6}.snippet .heads{font-weight:bold;font-size:0.9em;margin-top:0.5em;}.hit_icon{vertical-align:middle;height:1.5em;margin-right: 0.5em;}\@keyframes blink{0%{opacity:0}50%{opacity:1}100%{opacity:0}}.blink{animation:blink 1s infinite ease-in-out}.hit_summary{opacity:0.5;font-size:0.7em;margin-bottom:0.5em}.hit .url{color:#484;font-size:0.7em;margin-bottom:0.3em}</style>";
$mh .= <<EOT1;
<script>
var _zim_base = "$base";
function fnum3(v) { return v.toString().replace(/\\B(?=(\\d{3})+(?!\\d))/g,','); }
function xhr(u,f,opts) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
if(xhr.status >= 200 && xhr.status < 300) {
f(opts&&opts.format=='raw'?xhr.responseText:JSON.parse(xhr.responseText));
}
};
xhr.open('GET',u + (
opts && (opts.data || opts.param)?
'?'+Object.keys(opts.data||opts.param).map(function(k){return k+'='+encodeURIComponent(opts.data[k])}).join('&') :
''
));
xhr.send();
}
function _zim_search() {
var q = document.getElementById('_zim_search_q').value;
var id2 = document.getElementById('_zim_content');
var content = id2 && id2.type == 'checkbox' ?
( id2.checked ? _zim_base : "" ) : // checkbox checked?
_zim_base; // fallback to base
q = q.replace(/^\\s+/,'');
q = q.replace(/\\s+\$/,'');
//q = q.toLowerCase(); // not required anymore, as we stem at backend
if(q.length==0)
return;
var _id = document.getElementById('_zim_results_hint');
_id.innerHTML = 'searching ...';
_id.classList.toggle('blink',true);
var id = document.getElementById('_zim_results');
id.classList.toggle('active',false);
id.innerHTML = '...';
var st = new Date()*1;
xhr('/rest',function(data) {
//console.log(xhr.responseText);
//var data = JSON.parse(xhr.responseText);
//console.log(data);
if(data.results.hits.length>0)
id.classList.toggle('active',true);
else
id.classList.toggle('active',false);
var o = '';
//id.innerHTML = JSON.stringify(data);
if(data && data.results && data.results.hits) {
window.scrollTo({top:0}); // make sure we see the results
var _id = document.getElementById('_zim_results_hint');
//_id.innerHTML = data.results.hits.length + ' results';
var elapsed = fnum3(new Date()*1 - st) + 'ms';
_id.innerHTML = fnum3(data.results.hits.length) + ' results ' + (data.results.total ? 'of '+fnum3(data.results.total) : '' )+ ' ('+elapsed+')';
_id.classList.toggle('blink',false);
var cid = 0;
var re = new RegExp("(.{5,30})(" + q.replace(/[\\?\\.\\\\]/g,"") + ")(.{5,30})","i"); // -- used for create snippets
for(var e of data.results.hits) {
if(e.title.length==0) {
e.title = e.url.replace(/.*\\//,'');
}
var sn = e.snippet || '';
var hd = '';
for(var h in e.headers) {
for(var t in e.headers[h]) {
if(hd.length>0)
hd += ' · ';
if(e.headers[h][t])
hd += '<span class=snippet_header_'+h+'>' + e.headers[h][t] + '</span>';
if(t>10)
break;
}
}
if(hd.length>0)
sn = sn + '<div class=heads>' + hd + '</div>';
var ico = e.base ? '<img class=hit_icon src="/' + e.base + e.icon + '">' : '';
var lnk = e.url;
lnk = lnk.replace(/\\//,'');
if(e.base)
lnk = lnk.replace(/\\//,': ');
o += '<div class=hit>'+
'<a class=title href="' + e.url + '">' +