-
Notifications
You must be signed in to change notification settings - Fork 2
/
Core.pm
1271 lines (1059 loc) · 38.6 KB
/
Core.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
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2006-2014 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version. For
# more details read LICENSE in the root of this distribution.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
package Foswiki::Plugins::ClassificationPlugin::Core;
use strict;
use warnings;
use vars qw(
%hierarchies
%loadTimeStamps
%modTimeStamps
%cachedIndexFields
$baseWeb $baseTopic
$purgeMode
@changedCats
);
use constant TRACE => 0; # toggle me
use constant FIXFORMFIELDS => 1; # work around a bug in Foswiki
use constant MEMORYCACHE => 0; # set to 1 for experimental memory cache
use Foswiki::Plugins::DBCachePlugin::Core ();
use Foswiki::Form ();
use Foswiki::OopsException ();
use Error qw( :try );
###############################################################################
sub writeDebug {
print STDERR '- ClassificationPlugin::Core - '.$_[0]."\n" if TRACE;
#Foswiki::Func::writeDebug('- ClassificationPlugin::Core - '.$_[0]) if TRACE;
}
###############################################################################
sub init {
($baseWeb, $baseTopic) = @_;
$purgeMode = 0;
@changedCats = ();
%modTimeStamps = ();
%cachedIndexFields = ();
unless (MEMORYCACHE) {
%hierarchies = ();
%loadTimeStamps = ();
}
}
###############################################################################
sub finish {
writeDebug("called finish()");
foreach my $hierarchy (values %hierarchies) {
next unless defined $hierarchy;
my $web = $hierarchy->{web};
$hierarchy->finish();
undef $modTimeStamps{$web};
unless (MEMORYCACHE) {
undef $hierarchies{$web};
undef $loadTimeStamps{$web};
}
}
writeDebug("done finish()");
}
###############################################################################
sub OP_subsumes {
my ($r, $l, $map) = @_;
my $lval = $l->matches( $map );
my $rval = $r->matches( $map );
return 0 unless ( defined $lval && defined $rval);
my $web = $Foswiki::Plugins::DBCachePlugin::Core::dbQueryCurrentWeb || $baseWeb;
my $hierarchy = getHierarchy($web);
return $hierarchy->subsumes($lval, $rval);
}
###############################################################################
sub OP_isa {
my ($r, $l, $map) = @_;
my $lval = $l->matches( $map );
my $rval = $r->matches( $map );
return 0 unless ( defined $lval && defined $rval);
my $web = $Foswiki::Plugins::DBCachePlugin::Core::dbQueryCurrentWeb || $baseWeb;
my $hierarchy = getHierarchy($web);
my $cat = $hierarchy->getCategory($rval);
return 0 unless $cat;
return ($cat->contains($lval))?1:0;
}
###############################################################################
sub OP_distance {
my ($r, $l, $map) = @_;
my $lval = $l->matches( $map );
my $rval = $r->matches( $map );
return 0 unless ( defined $lval && defined $rval);
my $web = $Foswiki::Plugins::DBCachePlugin::Core::dbQueryCurrentWeb || $baseWeb;
my $hierarchy = getHierarchy($web);
my $dist = $hierarchy->distance($lval, $rval);
return $dist || 0;
}
###############################################################################
sub handleSIMILARTOPICS {
my ($session, $params, $theTopic, $theWeb) = @_;
#writeDebug("called handleSIMILARTOPICS()");
my $thisTopic = $params->{_DEFAULT} || $params->{topic} || $baseTopic;
my $thisWeb = $params->{web} || $baseWeb;
my $theFormat = $params->{format} || '$topic';
my $theHeader = $params->{header} || '';
my $theFooter = $params->{footer} || '';
my $theSep = $params->{separator};
my $theLimit = $params->{limit};
my $theSkip = $params->{skip};
my $theThreshold = $params->{threshold} || 0.3;
$theThreshold =~ s/[^\d\.]//go;
$theThreshold = 0.3 unless $theThreshold;
$theThreshold = $theThreshold/100 if $theThreshold > 1.0;
$theSep = ', ' unless defined $theSep;
$theLimit = 10 unless defined $theLimit;
my $hierarchy = getHierarchy($thisWeb);
my @similarTopics = $hierarchy->getSimilarTopics($thisTopic, $theThreshold);
return '' unless @similarTopics;
my %wmc = ();
map {$wmc{$_} = $hierarchy->computeSimilarity($thisTopic, $_)} @similarTopics;
@similarTopics = sort {$wmc{$b} <=> $wmc{$a}} @similarTopics;
# format result
my @lines;
my $index = 0;
foreach my $topic (@similarTopics) {
$index++;
next if $theSkip && $index <= $theSkip;
last if $theLimit && $index > $theLimit;
push @lines, expandVariables($theFormat,
'topic'=>$topic,
'web'=>$thisWeb,
'index'=>$index,
'similarity'=> int($wmc{$topic}*1000)/10,
);
}
return '' unless @lines;
$theHeader = expandVariables($theHeader, count=>$index);
$theFooter = expandVariables($theFooter, count=>$index);
$theSep = expandVariables($theSep);
return $theHeader.join($theSep, @lines).$theFooter;
}
###############################################################################
sub handleHIERARCHY {
my ($session, $params, $theTopic, $theWeb) = @_;
#writeDebug("called handleHIERARCHY(".$params->stringify().")");
my $thisWeb = $params->{_DEFAULT} || $params->{web} || $baseWeb;
$thisWeb =~ s/\./\//go;
my $hierarchy = getHierarchy($thisWeb);
return $hierarchy->traverse($params);
}
###############################################################################
sub handleISA {
my ($session, $params, $theTopic, $theWeb) = @_;
#writeDebug("called handleISA()");
my $thisWeb = $params->{web} || $baseWeb;
my $thisTopic = $params->{_DEFAULT} || $params->{topic} || $baseTopic;
my $theCategory = $params->{cat} || 'TopCategory';
#writeDebug("topic=$thisTopic, theCategory=$theCategory");
return 1 if $theCategory =~ /^(Top|TopCategory)$/oi;
return 0 if $theCategory =~ /^(Bottom|BottomCategory)$/oi;
return 0 unless $theCategory;
($thisWeb, $thisTopic) =
Foswiki::Func::normalizeWebTopicName($thisWeb, $thisTopic);
my $hierarchy = getHierarchy($thisWeb);
foreach my $catName (split(/\s*,\s*/, $theCategory)) {
#writeDebug("testing $catName");
my $cat = $hierarchy->getCategory($catName);
next unless $cat;
return 1 if $cat->contains($thisTopic);
}
#writeDebug("not found");
return 0;
}
###############################################################################
sub handleSUBSUMES {
my ($session, $params, $theTopic, $theWeb) = @_;
my $thisWeb = $params->{web} || $baseWeb;
my $theCat1 = $params->{_DEFAULT} || $baseTopic;
my $theCat2 = $params->{cat} || '';
#writeDebug("called handleSUBSUMES($theCat1, $theCat2)");
return 0 unless $theCat2;
my $hierarchy = getHierarchy($thisWeb);
my $cat1 = $hierarchy->getCategory($theCat1);
return 0 unless $cat1;
my $result = 0;
foreach my $catName (split(/\s*,\s*/,$theCat2)) {
$catName =~ s/^\s+//g;
$catName =~ s/\s+$//g;
next unless $catName;
my $cat2 = $hierarchy->getCategory($catName);
next unless $cat2;
$result = $cat1->subsumes($cat2) || 0;
last if $result;
}
#writeDebug("result=$result");
return $result;
}
###############################################################################
sub handleDISTANCE {
my ($session, $params, $theTopic, $theWeb) = @_;
my $thisWeb = $params->{web} || $baseWeb;
my $theFrom = $params->{_DEFAULT} || $params->{from} || $baseTopic;
my $theTo = $params->{to} || 'TopCategory';
my $theAbs = $params->{abs} || 'off';
my $theFormat = $params->{format} || '$dist';
my $theUndef = $params->{undef} || '';
#writeDebug("called handleDISTANCE($theFrom, $theTo)");
my $hierarchy = getHierarchy($thisWeb);
my $distance = $hierarchy->distance($theFrom, $theTo);
return $theUndef unless defined $distance;
$distance = abs($distance) if $theAbs eq 'on';
#writeDebug("distance=$distance");
my $result = $theFormat;
$result =~ s/\$dist/$distance/g;
return $result;
}
###############################################################################
sub handleCATINFO {
my ($session, $params, $theTopic, $theWeb) = @_;
#writeDebug("called handleCATINFO(".$params->stringify().")");
my $theCat = $params->{cat};
my $theFormat = $params->{format} || '$link';
my $theSep = $params->{separator};
my $theHeader = $params->{header} || '';
my $theFooter = $params->{footer} || '';
my $thisWeb = $params->{web} || $baseWeb;
my $thisTopic = $params->{_DEFAULT} || $params->{topic};
my $theSubsumes = $params->{subsumes} || '';
my $theParentSubsumes = $params->{parentsubsumes} || '';
my $theSortChildren = $params->{sortchildren} || 'off';
my $theMaxChildren = $params->{maxchildren} || 0;
my $theHideNull = $params->{hidenull} || 'off';
my $theNull = $params->{null} || '';
my $theExclude = $params->{exclude} || '';
my $theInclude = $params->{include} || '';
my $theTruncate = $params->{truncate} || '';
my $theMatchAttr = $params->{matchattr} || 'name';
my $theMatchCase = $params->{matchcase} || 'on';
my $theLimit = $params->{limit};
my $theSkip = $params->{skip};
$theLimit =~ s/[^\d]//g if defined $theLimit;
$theSkip =~ s/[^\d]//g if defined $theSkip;
$theMatchAttr = 'name' unless $theMatchAttr =~ /^(name|title)$/;
$theSep = ', ' unless defined $theSep;
$theMaxChildren =~ s/[^\d]//go;
$theMaxChildren = 0 unless defined $theMaxChildren;
my $hierarchy;
my $categories;
if ($theCat) { # cats mode
if ($thisWeb eq 'any') {
$hierarchy = findHierarchy($theCat);
return '' unless $hierarchy;
$thisWeb = $hierarchy->{web};
} else {
($thisWeb, $theCat) =
Foswiki::Func::normalizeWebTopicName($thisWeb, $theCat);
$hierarchy = getHierarchy($thisWeb);
}
push @$categories, $theCat;
} elsif ($thisTopic) { # topic mode
($thisWeb, $thisTopic) =
Foswiki::Func::normalizeWebTopicName($thisWeb, $thisTopic);
$hierarchy = getHierarchy($thisWeb);
$categories = $hierarchy->getCategoriesOfTopic($thisTopic) if $hierarchy;
} else { # find mode
$hierarchy = getHierarchy($thisWeb);
@$categories = $hierarchy->getCategoryNames();
}
return expandVariables($theNull) unless $hierarchy;
return expandVariables($theNull) unless $categories;
#writeDebug("categories=".join(', ', @$categories));
my @result;
my $doneBreadCrumbs = 0;
my $index = 0;
$theSubsumes =~ s/^\s+//go;
$theSubsumes =~ s/\s+$//go;
$theParentSubsumes =~ s/^\s+//go;
$theParentSubsumes =~ s/\s+$//go;
my $subsumesCat = $hierarchy->getCategory($theSubsumes);
my $parentSubsumesCat = $hierarchy->getCategory($theParentSubsumes);
foreach my $catName (sort @$categories) {
next if $theCat && $theCat ne 'TopCategory' && $catName =~ /BottomCategory|TopCategory/;
my $category = $hierarchy->getCategory($catName);
next unless $category;
if ($theMatchCase eq 'on') {
next if $theExclude && $category->{$theMatchAttr} =~ /^($theExclude)$/;
next if $theInclude && $category->{$theMatchAttr} !~ /^($theInclude)$/;
} else {
next if $theExclude && $category->{$theMatchAttr} =~ /^($theExclude)$/i;
next if $theInclude && $category->{$theMatchAttr} !~ /^($theInclude)$/i;
}
#writeDebug("found $catName");
# skip catinfo from another branch of the hierarchy
next if $subsumesCat && !$hierarchy->subsumes($subsumesCat, $category);
$index++;
next if $theSkip && $index <= $theSkip;
my $line = $theFormat;
my @parents;
if ($line =~ /\$parent/) {
@parents = sort {uc($a->{title}) cmp uc($b->{title})} $category->getParents($parentSubsumesCat);
}
my $parentLinks = '';
if ($line =~ /\$parents?\b/ || $line =~ /\$parents?links?/) {
my @links = ();
foreach my $parent (@parents) {
push @links, $parent->getLink();
}
$parentLinks = join($theSep, @links);
}
my $parentsName = '';
if ($line =~ /\$parents?names?/) {
my @names = ();
foreach my $parent (@parents) {
push @names, $parent->{name};
}
$parentsName = join($theSep, @names);
}
my $parentsTitle = '';
if ($line =~ /\$parents?title/) {
my @titles = ();
foreach my $parent (@parents) {
push @titles, $parent->{title};
}
$parentsTitle = join($theSep, @titles);
}
my $parentUrls = '';
if ($line =~ /\$parents?urls?/) {
my @urls = ();
foreach my $parent (@parents) {
push @urls, $parent->getUrl();
}
$parentUrls = join($theSep, @urls);
}
my $breadCrumbs = '';
my $breadCrumbNames = '';
if ($line =~ /\$(breadcrumb(name)?)s?/ && !$doneBreadCrumbs) {
my @breadCrumbs = ();
my @breadCrumbNames = ();
my %seen = ();
my $parent = $category;
unless ($theCat) {
if (Foswiki::Func::topicExists($thisWeb, $thisTopic)) {
push @breadCrumbs, "[[$thisWeb.$thisTopic]]";
push @breadCrumbNames, $thisTopic;
$seen{$thisTopic} = 1;
}
}
while ($parent) {
last if $seen{$parent->{name}};
$seen{$parent->{name}} = 1;
next if $theExclude && $parent->{name} =~ /^($theExclude)$/;
push @breadCrumbs, $parent->getLink();
push @breadCrumbNames, $parent->{name};
my @parents = $parent->getParents();
last unless @parents;
$parent = shift @parents;
last if $parent eq $parent->{hierarchy}{_top};
}
$breadCrumbs = join($theSep, reverse @breadCrumbs);
$breadCrumbNames = join($theSep, reverse @breadCrumbNames);
$doneBreadCrumbs = 1;
}
my @children;
my $moreChildren = '';
if ($line =~ /\$children/) {
@children = sort {uc($a->{title}) cmp uc($b->{title})} $category->getChildren();
@children = grep {$_->{name} ne 'BottomCategory'} @children;
if ($theHideNull eq 'on') {
@children = grep {$_->countLeafs() > 0}
@children;
}
if ($theSortChildren eq 'on') {
@children =
sort {$b->countLeafs() <=> $a->countLeafs() ||
$a->{title} cmp $b->{title}}
@children;
}
if ($theMaxChildren && $theMaxChildren < @children) {
if (splice(@children, $theMaxChildren)) {
$moreChildren = $params->{morechildren} || '';
}
}
}
my $children = '';
if ($line =~ /\$children(links)?\b/) {
my @links = ();
foreach my $child (@children) {
push @links, $child->getLink();
}
$children = join($theSep, @links);
}
my $childrenName = '';
if ($line =~ /\$children?names?/) {
my @names = ();
foreach my $child (@children) {
push @names, $child->{name};
}
$childrenName = join($theSep, @names);
}
my $childrenTitle = '';
if ($line =~ /\$childrentitle/) {
my @titles = ();
foreach my $child (@children) {
push @titles, $child->{title};
}
$childrenTitle = join($theSep, @titles);
}
my $childrenUrls = '';
if ($line =~ /\$childrenurls?/) {
my @urls = ();
foreach my $child (@children) {
push @urls, $child->getUrl();
}
$childrenUrls = join($theSep, @urls);
}
my $tags = '';
if ($line =~ /\$tags/) {
$tags = join($theSep, sort $category->getTagsOfTopics());
}
my $isCyclic = 0;
$isCyclic = $category->isCyclic() if $theFormat =~ /\$cyclic/;
my $countLeafs = '';
$countLeafs = $category->countLeafs() if $theFormat=~ /\$leafs/;
my $nrTopics = '';
$nrTopics = $category->countTopics() if $theFormat=~ /\$count/;
my $title = $category->{title} || $catName;
my $link = $category->getLink();
my $url = $category->getUrl();
my $summary = $category->{summary} || '';
my $iconUrl = $category->getIconUrl();
my $truncTitle = $title;
$truncTitle =~ s/$theTruncate// if $theTruncate;
$line =~ s/\$more/$moreChildren/g;
$line =~ s/\$index/$index/g;
$line =~ s/\$link/$link/g;
$line =~ s/\$url/$url/g;
$line =~ s/\$web/$thisWeb/g;
$line =~ s/\$origweb/$category->{origWeb}/g;
$line =~ s/\$order/$category->{order}/g;
$line =~ s/\$(name|topic)/$catName/g;
$line =~ s/\$title/$title/g;
$line =~ s/\$trunctitle/$truncTitle/g;
$line =~ s/\$summary/$summary/g;
$line =~ s/\$parents?name/$parentsName/g;
$line =~ s/\$parents?title/$parentsTitle/g;
$line =~ s/\$parents?links?/$parentLinks/g;
$line =~ s/\$parents?urls?/$parentUrls/g;
$line =~ s/\$parents?/$parentLinks/g;
$line =~ s/\$cyclic/$isCyclic/g;
$line =~ s/\$leafs/$countLeafs/g;
$line =~ s/\$count/$nrTopics/g;
$line =~ s/\$breadcrumbnames?/$breadCrumbNames/g;
$line =~ s/\$breadcrumbs?/$breadCrumbs/g;
$line =~ s/\$children?name/$childrenName/g;
$line =~ s/\$childrentitle/$childrenTitle/g;
$line =~ s/\$childrenurls?/$childrenUrls/g;
$line =~ s/\$children(links?)?/$children/g;
$line =~ s/\$icon/$iconUrl/g;
$line =~ s/\$tags/$tags/g;
$line =~ s/,/,/g; # hack around MAKETEXT where args are comma separated accidentally
push @result, $line if $line;
last if $theLimit && $index >= $theLimit;
}
return expandVariables($theNull) unless @result;
my $result = $theHeader.join($theSep, @result).$theFooter;
$result = expandVariables($result, 'count'=>scalar(@$categories));
#writeDebug("result=$result");
return $result;
}
###############################################################################
sub handleTAGINFO {
my ($session, $params, $theTopic, $theWeb) = @_;
#writeDebug("called handleTAGINFO(".$params->stringify().")");
my $theFormat = $params->{format} || '$link';
my $theSep = $params->{separator};
my $theHeader = $params->{header} || '';
my $theFooter = $params->{footer} || '';
my $thisWeb = $params->{web} || $baseWeb;
my $thisTopic = $params->{_DEFAULT} || $params->{topic} || $baseTopic;
my $theExclude = $params->{exclude} || '';
my $theInclude = $params->{include} || '';
my $theLimit = $params->{limit};
my $theSkip = $params->{skip};
$theLimit =~ s/[^\d]//g if defined $theLimit;
$theSkip =~ s/[^\d]//g if defined $theSkip;
$theSep = ', ' unless defined $theSep;
($thisWeb, $thisTopic) =
Foswiki::Func::normalizeWebTopicName($thisWeb, $thisTopic);
# get tags
my $db = Foswiki::Plugins::DBCachePlugin::Core::getDB($thisWeb);
return '' unless $db;
my $topicObj = $db->fastget($thisTopic);
return '' unless $topicObj;
my $form = $topicObj->fastget('form');
return '' unless $form;
my $formObj = $topicObj->fastget($form);
return '' unless $formObj;
my $tags = $formObj->fastget('Tag');
return '' unless $tags;
my @tags = split(/\s*,\s*/, $tags);
my @result;
my $context = Foswiki::Func::getContext();
my $index = 0;
foreach my $tag (sort @tags) {
$tag =~ s/^\s+//go;
$tag =~ s/\s+$//go;
next if $theExclude && $tag =~ /^($theExclude)$/;
next if $theInclude && $tag !~ /^($theInclude)$/;
$index++;
next if $theSkip && $index <= $theSkip;
my $line = $theFormat;
my $url;
if ($context->{SolrPluginEnabled}) {
# SMELL: WikiWords are autolinked in parameter position ... wtf
$url = '<noautolink>%SOLRSCRIPTURL{topic="'.$thisWeb.'.WebSearch" tag="'.$tag.'" web="'.$thisWeb.'" union="web" separator="&&"}%</noautolink>'; # && to please MAKETEXT :(
} else {
$url = Foswiki::Func::getScriptUrlPath($thisWeb, "WebTagCloud", "view", tag=>$tag);
}
my $class = $tag;
$class =~ s/["' ]/_/g;
$class = "tag_".$class;
my $link = "<a href='$url' rel='tag' class='\$class'><noautolink>$tag</noautolink></a>";
$line =~ s/\$index/$index/g;
$line =~ s/\$url/$url/g;
$line =~ s/\$link/$link/g;
$line =~ s/\$class/$class/g;
$line =~ s/\$name/$tag/g;
push @result, $line;
last if $theLimit && $index >= $theLimit;
}
my $count = scalar(@tags);
my $result = $theHeader.join($theSep, @result).$theFooter;
$result = expandVariables($result,
'web'=>$thisWeb,
'count'=>$count,
'index'=>$index,
);
#writeDebug("result='$result'");
return $result;
}
###############################################################################
# reparent based on the category we are in
# takes the first category in alphabetic order
sub beforeSaveHandler {
my ($text, $topic, $web, $meta) = @_;
writeDebug("beforeSaveHandler($web, $topic)");
my $doAutoReparent = Foswiki::Func::getPreferencesFlag('CLASSIFICATIONPLUGIN_AUTOREPARENT', $web);
my $session = $Foswiki::Plugins::SESSION;
unless ($meta) {
$meta = new Foswiki::Meta($session, $web, $topic, $text);
#writeDebug("creating a new meta object");
}
my %isCatField = ();
my %isTagField = ();
my $formName = $meta->getFormName();
if ($formName) {
my ($theFormWeb, $theForm) = Foswiki::Func::normalizeWebTopicName($web, $formName);
my $formDef;
writeDebug("form definition at $theFormWeb, $theForm");
if (Foswiki::Func::topicExists($theFormWeb, $theForm)) {
try {
$formDef = new Foswiki::Form($session, $theFormWeb, $theForm);
} catch Foswiki::OopsException with {
my $e = shift;
print STDERR "ERROR: can't read form definition $theForm in ClassificationPlugin::Core::beforeSaveHandler\n";
};
if ($formDef) {
foreach my $fieldDef (@{$formDef->getFields()}) {
#writeDebug("formDef field $fieldDef->{name} type=$fieldDef->{type}");
$isCatField{$fieldDef->{name}} = 1 if $fieldDef->{type} eq 'cat';
$isTagField{$fieldDef->{name}} = 1 if $fieldDef->{type} eq 'tag';
}
}
}
}
# There's a serious bug in all Foswiki's that it rewrites all of the
# topic text - including the meta data - if a topic gets moved to
# a different web. In an attempt to keep linking WikiWords intact,
# it rewrites the DataForm, i.e. the names and titles of the
# formfields. This however breaks mostly every code that relies
# on the formfields to be named like they where in the beginning.
# AFAICS, there's no case where renaming the formfield names is
# desired.
#
# What we do here is to loop pre-process the topic being saved right here
# and remove any leading webname from the those formfields
# playing a central role in this plugin, TopicType and Category.
#
if (FIXFORMFIELDS) {
#if (TRACE) {
# use Data::Dumper;
# $Data::Dumper::Maxdepth = 3;
# writeDebug("BEFORE FIXFORMFIELDS");
# writeDebug(Dumper($meta));
#}
foreach my $field ($meta->find('FIELD')) {
if ($field->{name} =~ /TopicType|Category/) {
$field->{name} =~ s/^.*[\.\/](.*?)$/$1/;
$field->{title} =~ s/^.*[\.\/](.*?)$/$1/;
}
if ($isCatField{$field->{name}}) {
writeDebug("before, value=$field->{value}");
$field->{value} =~ s/^top=.*$//; # clean up top= in value definition
my $item;
$field->{value} = join(', ',
map {
$item = $_;
$item =~ s/^.*[\.\/](.*?)$/$1/;
$_ = $item;
}
split(/\s*,\s*/, $field->{value})
); # remove accidental web part from categories
writeDebug("after, value=$field->{value}");
}
}
#if (TRACE) {
# use Data::Dumper;
# $Data::Dumper::Maxdepth = 3;
# writeDebug("AFTER FIXFORMFIELDS");
# writeDebug(Dumper($meta));
#}
}
if ($web eq $Foswiki::cfg{TrashWebName}) {
writeDebug("detected a move from $baseWeb to trash");
$web = $baseWeb;# operations are on the baseWeb
}
# get topic type info
my $topicType = $meta->get('FIELD', 'TopicType');
return unless $topicType;
$topicType = $topicType->{value};
# fix topic type depending on the form
writeDebug("old TopicType=$topicType");
my @topicType = split(/\s*,\s*/, $topicType);
my $index = scalar(@topicType)+3;
my %newTopicType = map {$_ =~ s/^.*\.//; $_ => $index--} @topicType;
if ($formName =~ /^Applications[\.\/]ClassificationApp[\.\/]Category$/) {
$newTopicType{Category} = 2;
$newTopicType{CategorizedTopic} = 1;
$newTopicType{WikiTopic} = 0;
}
elsif ($formName =~ /^Applications[\.\/]ClassificationApp[\.\/]CategorizedTopic$/) {
$newTopicType{CategorizedTopic} = 1;
$newTopicType{WikiTopic} = 0;
}
elsif ($formName =~ /^Applications[\.\/]ClassificationApp[\.\/]TaggedTopic$/) {
$newTopicType{TaggedTopic} = 1;
$newTopicType{WikiTopic} = 0;
}
elsif ($formName =~ /^Applications[\.\/]ClassificationApp[\.\/]ClassifiedTopic$/) {
$newTopicType{ClassifiedTopic} = 3;
$newTopicType{CategorizedTopic} = 2;
$newTopicType{TaggedTopic} = 1;
$newTopicType{WikiTopic} = 0;
}
if ($formName !~ /^Applications[\.\/]TopicStub$/) {
delete $newTopicType{TopicStub};
}
if (keys %newTopicType) {
my @newTopicType;
foreach my $item (sort {$newTopicType{$b} <=> $newTopicType{$a}} keys %newTopicType) {
push @newTopicType, $item;
}
my $newTopicType = join(', ', @newTopicType);
writeDebug("new TopicType=$newTopicType");
$meta->putKeyed('FIELD', {name =>'TopicType', title=>'TopicType', value=>$newTopicType});
}
# get categories of this topic,
# must get it from current meta data
return unless $topicType =~ /ClassifiedTopic|CategorizedTopic|Category|TaggedTopic/;
my $hierarchy = getHierarchy($web);
my $catFields = $hierarchy->getCatFields(split(/\s*,\s*/,$topicType));
# get old categories from store
my $db = Foswiki::Plugins::DBCachePlugin::Core::getDB($web);
my $topicObj = $db->fastget($topic);
my %oldCats;
if (!$topicObj) {
$purgeMode = 2; # new topic
} else {
my $form = $topicObj->fastget("form");
if (!$form) {
$purgeMode = 2; # new form
} else {
$form = $topicObj->fastget($form);
foreach my $field (@$catFields) {
my $cats = $form->fastget($field);
next unless $cats;
foreach my $cat (split(/\s*,\s*/,$cats)) {
$cat =~ s/^\s+//go;
$cat =~ s/\s+$//go;
$oldCats{$cat} = 1;
}
}
}
}
# get new categories from meta data
my %newCats;
foreach my $field (@$catFields) {
my $cats = $meta->get('FIELD',$field);
next unless $cats;
my $title = $cats->{title};
$cats = $cats->{value};
next unless $cats;
# assigning TopCategory only empties the cat field
if ($cats eq 'TopCategory') {
#writeDebug("found TopCategory assignment");
$meta->putKeyed('FIELD', {name =>$field, title=>$title, value=>''});
next;
}
foreach my $cat (split(/\s*,\s*/,$cats)) {
$cat =~ s/^\s+//go;
$cat =~ s/\s+$//go;
$newCats{$cat} = 1;
}
}
# set the new parent topic
if ($doAutoReparent) {
writeDebug("autoreparenting");
my $newParentCat;
foreach my $cat (sort keys %newCats) {
if ($cat ne 'TopCategory') {
$newParentCat = $cat;
last;
}
}
my $homeTopicName = $Foswiki::cfg{HomeTopicName};
$newParentCat = $homeTopicName unless defined $newParentCat;
writeDebug("newParentCat=$newParentCat");
$meta->remove('TOPICPARENT');
$meta->putKeyed('TOPICPARENT', {name=>$newParentCat});
} else {
writeDebug("not autoreparenting");
}
# get changed categories
my %changedCats = ();
foreach my $cat (keys %oldCats) {
$changedCats{$cat} = 1 unless $newCats{$cat};
}
foreach my $cat (keys %newCats) {
$changedCats{$cat} = 1 unless $oldCats{$cat};
}
@changedCats = keys %changedCats; #remember
# add self
if (@changedCats && !$changedCats{$topic} && $topicType =~ /\bCategory\b/) {
push @changedCats, $topic;
#writeDebug("adding self to changedCats");
}
# cache invalidation: compute the purgeMode to be executed after save
$purgeMode = 1 if $topicType =~ /\bTaggedTopic\b/;
$purgeMode = 2 if $topicType =~ /\bCategorizedTopic\b/;
$purgeMode = 3 if $topicType =~ /\bClassifiedTopic\b/;
$purgeMode = 4 if $topicType =~ /\bCategory\b/;
# try even harder if it missing the CategorizedTopic TopicType but
# still uses categories
if ($purgeMode < 2) {
my $hierarchy = getHierarchy($web);
my $catFields = $hierarchy->getCatFields(split(/\s*,\s*/,$topicType));
if ($catFields && @$catFields) {
$purgeMode = ($purgeMode < 1)?2:3;
}
}
writeDebug("purgeMode=$purgeMode");
#writeDebug("changedCats=".join(',', @changedCats));
}
###############################################################################
sub afterSaveHandler {
#my ($text, $topic, $web, $meta) = @_;
my $topic = $_[1];
my $web = $_[2];
writeDebug("afterSaveHandler($web, $topic)");
if ($web eq $Foswiki::cfg{TrashWebName}) {
#writeDebug("detected a move from $baseWeb to trash");
$web = $baseWeb;# operations are on the baseWeb
}
$web =~ s/\//./go;
if ($purgeMode) {
writeDebug("purging hierarchy $web");
my $hierarchy = getHierarchy($web);
# delete the cached html page
my $cache = $Foswiki::Plugins::SESSION->{cache} || $Foswiki::Plugins::SESSION->{cache};
if (defined $cache) {
foreach my $catName (@changedCats) {
my $cat = $hierarchy->getCategory($catName);
next unless $cat;
if ($cat->{origWeb} eq $web) {
# category is a topic in this web
$cache->deletePage($web, $catName);
} else {
# category is displayed via the Category topic as it is imported
$cache->deletePage($web, 'Category');
}
}
}
writeDebug("purging @changedCats");
$hierarchy->purgeCache($purgeMode, \@changedCats);
$purgeMode = 0; # reset
}
}
###############################################################################
sub afterRenameHandler {
my ($fromWeb, $fromTopic, $fromAttachment, $toWeb, $toTopic, $toAttachment) = @_;
return if $fromAttachment || $toAttachment;
writeDebug("afterRenameHandler($fromWeb, $fromTopic, $toWeb, $toTopic)");
my ($meta) = Foswiki::Func::readTopic($toWeb, $toTopic);
my $formName = $meta->getFormName();
#print STDERR "formName=$formName\n";
return unless $formName =~ /^Applications[\.\/]ClassificationApp[\.\/]Category$/;
my $hierarchy = getHierarchy($fromWeb);
if ($hierarchy) {
my @changedCats = ($fromTopic);
push @changedCats, $toTopic if $fromWeb eq $toWeb && $fromTopic ne $toTopic;
#print STDERR "purge cache for $hierarchy->{web}, affects categories @changedCats\n";
$hierarchy->purgeCache(4, \@changedCats);
}
if ($fromWeb ne $toWeb && $toWeb ne $Foswiki::cfg{TrashWebName}) {
$hierarchy = getHierarchy($toWeb);
if ($hierarchy) {
my @changedCats = ($toTopic);
$hierarchy->purgeCache(4, \@changedCats);
}
} else {
writeDebug("detected rename to trash");
}
}
###############################################################################
sub getTopicTypes {
my ($web, $topic) = @_;
my $db = Foswiki::Plugins::DBCachePlugin::Core::getDB($web);
return undef unless $db;
my $topicObj = $db->fastget($topic);
return undef unless $topicObj;
my $form = $topicObj->fastget("form");
return undef unless $form;
$form = $topicObj->fastget($form);
return undef unless $form;
my $topicTypes = $form->fastget('TopicType');
return undef unless $topicTypes;
my @topicTypes = split(/\s*,\s*/, $topicTypes);
return \@topicTypes;
}
################################################################################