/
Address.pm
1717 lines (1451 loc) · 59.2 KB
/
Address.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
# See bottom of file for license and copyright information
package Foswiki::Address;
=begin TML
---+ package Foswiki::Address
This class is used to handle pointers to Foswiki 'resources', which might be
webs, topics or parts of topics (such as attachments or metadata), optionally
of a specific revision.
The primary goal is to end the tyranny of arbitrary
=(web, topic, attachment, rev...)= tuples. Users of =Foswiki::Address= should
be able to enjoy programmatically updating, stringifying, parsing, validating,
comparing and passing around of _address objects_ that might eventually be
understood by the wider Foswiki universe, without having to maintain proprietary
parse/stringify/validate/comparison handling code that must always be
considerate of the recipient for such tuples.
This class does not offer any interaction with resources themselves; rather,
functionality is provided to create, hold, manipulate, test
__and de/serialise addresses__
Fundamentally, =Foswiki::Address= can be thought of as an interface to a hash of
the components necessary to address a specific Foswiki resource.
<verbatim>
my $addr = {
web => 'Web/SubWeb',
topic => 'Topic',
attachment => 'Attachment.pdf',
rev => 3
};
</verbatim>
<blockquote class="foswikiHelp">%X% __Unresolved issues__
* Is this class necessary, or should we make a cleaner, lighter
=Foswiki::Meta2= - where 'unloaded' objects are no heavier than
=Foswiki::Address= and provide the same functionality?
* Should the physical file attachment be treated separately to the metadata
view of the file attachment(s)? Desirables:
* ability to unambiguously create pointers to an attachment's data (file)
* ability for Foswiki core to calculate an http URL for it
* ability to create pointers to properties (metadata) of the attachment
* _These questions are slightly loaded in favour of distinguishing
between the datastream and metadata about the attachment. In an ideal
world a file attachment would be a first-class citizen to topics: rather
than topic text, we have the iostream; attachments would have their own
user metadata, dataforms..._
* Duplicating %SYSTEMWEB%.QuerySearch parser functionality. 80% of the code
in this class is related to parsing "string forms" of addresses of Foswiki
resources... querysearch parser needs some refactoring so we can delete the
parser code here.
* API usability - can we stop passing around (web, topic, attachment, rev)
tuples - will the =->new()= constructor make sense to plugin authors, core
hackers? __FEEDBACK WELCOME__, please comment at
Foswiki:Development.TopicAddressing
</blockquote>
=cut
use strict;
use warnings;
use Assert;
use Foswiki::Func();
use Foswiki::Meta();
#use Data::Dumper;
use constant TRACE => 0; # Don't forget to uncomment dumper
use constant TRACE2 => 0;
use constant TRACEVALID => 0;
use constant TRACEATTACH => 0;
#our @THESE;
my %atomiseAs = (
root => \&_atomiseAsRoot,
web => \&_atomiseAsWeb,
topic => \&_atomiseAsTopic,
attachment => \&_atomiseAsAttachment,
META => \&_atomiseAsTOM,
meta => \&_atomiseAsTOM,
SECTION => \&_atomiseAsTOM,
text => \&_atomiseAsTOM,
'*' => \&_atomiseAsTOM
);
# The question is: what do we have? The hash is accessed as follows:
# $pathtypes{ $tompath[0] }->{ scalar(@tompath) }
my %pathtypes = (
attachment => { 1 => 'attachments', 2 => 'attachment' },
META => { 1 => 'meta', 2 => 'metatype', 3 => 'metamember', 4 => 'metakey' },
SECTION => { 1 => 'sections', 2 => 'section' },
text => { 1 => 'text' }
);
# I tried to create a logical parser, but it kept ending up as spaghetti.
# So we use a lookup table instead... (probably?) easier to follow, faster.
my %plausibletable = (
# root keys represent the path separator signature of the form:
# combinations of s, S, d, D - where:
# s = <part>/<part> - sequence of two parts separated by '/'
# d = <part>.<part> - sequence of two parts separated by '.'
# S = <part>/<part>/<part>[/]... - sequence > 2 parts separated by '/'
# D = <part>.<part>.<part>[.]... - sequence > 2 parts separated by '.'
#
# sub keys are the type considered; values of the sub keys indicate
# the plausibility that the given form could be the type indicated:
# 0/undef - not plausible
# 1 - plausible without using any context
# 2 - normal ("unambiguous") form
# 'webpath' - plausible if given webpath context
# 'topic' - plausible if given webpath & topic context
#
# Foo
'' => { webpath => 1, topic => 'webpath', attachment => 'topic' },
# Foo.Bar
'd' => { webpath => 1, topic => 2, attachment => 'topic' },
# Foo/Bar
's' => { webpath => 1, topic => 1, attachment => 'webpath' },
# Foo/Bar.Dog
'sd' => { webpath => 0, topic => 2, attachment => 'webpath' },
# Foo.Bar/Dog
'ds' => { webpath => 0, topic => 1, attachment => 2 },
# Foo/Bar/Dog
'S' => { webpath => 1, topic => 1, attachment => 1 },
# Foo.Bar.Dog
'D' => { webpath => 1, topic => 1, attachment => 'topic' },
# Foo.Bar/Cat/Dog
'dS' => { webpath => 0, topic => 1, attachment => 1 },
# Foo/Bar.Cat.Dog
'sD' => { webpath => 0, topic => 0, attachment => 'webpath' },
# Foo/Bar/Dog.Cat
'Sd' => { webpath => 0, topic => 2, attachment => 1 },
# Foo.Bar.Dog/Cat
'Ds' => { webpath => 0, topic => 1, attachment => 1 },
# Foo.Bar.Dog/Cat/Bat
'DS' => { webpath => 0, topic => 0, attachment => 1 },
# Foo/Bar/Dog.Cat.Bat
'SD' => { webpath => 0, topic => 0, attachment => 1 },
# Foo/Bar.Dog/Cat
'sds' => { webpath => 0, topic => 0, attachment => 2 },
# Foo/Bar/Dog.Cat/Bat
'Sds' => { webpath => 0, topic => 0, attachment => 2 },
# Foo.Bar/Dog.Cat
'dsd' => { webpath => 0, topic => 0, attachment => 2 },
# Foo.Bar.Dog/Cat.Bat
'Dsd' => { webpath => 0, topic => 0, attachment => 1 },
# Foo.Bar/Dog.Cat.Bat
'dsD' => { webpath => 0, topic => 0, attachment => 2 },
# Foo/Bar.Dog/Cat.Bat
'sdsd' => { webpath => 0, topic => 0, attachment => 2 },
# Foo/Bar.Dog/Cat.B.a.t
'sdsD' => { webpath => 0, topic => 0, attachment => 2 },
# Foo/Bar/Dog.Cat/B.at
'Sdsd' => { webpath => 0, topic => 0, attachment => 2 },
# Foo/Bar/Dog.Cat/B.a.t
'SdsD' => { webpath => 0, topic => 0, attachment => 2 }
);
my %sepidentchars =
( 0 => { '.' => 'd', '/' => 's' }, 1 => { '.' => 'D', '/' => 'S' } );
=begin TML
---++ ClassMethod new( %constructor ) => $addrObj
Create a =Foswiki::Address= instance
The constructor takes two main forms:
---+++ Explicit form
*Example:*
<verbatim>
my $addrObj = Foswiki::Address->new(
web => 'Web/SubWeb',
topic => 'Topic',
attachment => 'Attachment.pdf',
rev => 3
);</verbatim>
*Options:*
| *Param* | *Description* | *Notes* |
| =web= | =$string= of web path, %BR% used if =webpath= is empty/null | |
| =webpath= | =\@arrayref= of web path, root web first | |
| =topic= | =$string= topic name | |
| =rev= | =$integer= revision number. | If the tompath is to a =attachment= datastream, rev applies to that file; topic rev otherwise |
| =tompath= | =\@arrayref= of a "TOM" path, one of:%BR% =META=, =text=, =SECTION=, =attachment=. | See table below |
| =string= | string representation of an object | eg. 'Web/SubWeb.Topic/Attachment.pdf@3' |
*path forms:*
| *tompath* | *Description* |
| =['attachment']= | All datastreams attached to a topic |
| =['attachment', 'Attachment.pdf']= | Datastream of the file attachment named 'Attachment.pdf' |
| =['META']= | All =META= on a topic |
| =['META', 'FIELD']= | All =META:FIELD= members on a topic |
| =['META', 'FIELD', { name => 'Colour' }]= | The =META:FIELD= member whose =name='Colour'= |
| =['META', 'FIELD', 3]= | The fourth =META:FIELD= member |
| =['META', 'FIELD', { name => 'Colour' }, 'title']= | The ='title'= attribute on the =META:FIELD= member whose =name='Colour'= |
| =['META', 'FIELD', 3, 'title']= | The ='title'= attribute on the fourth =META:FIELD= member |
| =['text']= | The topic text |
| =['SECTION']= | All topic sections as defined by %SYSTEMWEB%.VarSTARTSECTION |
| =['SECTION', {name => 'foo'}]= | The topic section named 'foo' |
| =['SECTION', {name => 'foo', type => 'include'}]= | The topic section named 'foo' of =type='include'= |
*Example:* Point to the value of a formfield =LastName= in =Web/SubWeb.Topic=,
<verbatim>
my $addrObj = Foswiki::Address->new(
web => 'Web/SubWeb',
topic => 'Topic',
tompath => ['META', 'FIELD', {name => LastName}, 'value']
);</verbatim>
*Equivalent:* %JQREQUIRE{"chili"}%<verbatim class="tml">
%QUERY{"'Web/SubWeb.Topic'/META:FIELD[name='LastName'].value"}%
or
%QUERY{"'Web/SubWeb.Topic'/LastName"}%
</verbatim>
---+++ String form
*Example:*
<verbatim>
my $addrObj = Foswiki::Address->new(
string => 'Web/SubWeb.Topic/Attachment.pdf@3',
%opts
);</verbatim>
<blockquote class="foswikiHelp">%X% String form instantiation requires parsing
of the address string which comes with many options and caveats - refer to the
documentation for =parse()=.</blockquote>
=cut
sub new {
my ( $class, %opts ) = @_;
my $this;
if ( $opts{string} ) {
ASSERT( not $opts{topic} or ( $opts{webpath} and $opts{topic} ) )
if DEBUG;
# $this->{parseopts} = {
# web => $opts{web},
# webpath => $opts{webpath},
# topic => $opts{topic},
# rev => $opts{rev},
# isA => $opts{isA},
# existAs => undef,
# catchAs => $opts{catchAs},
# existHints => $opts{existHints},
# string => $opts{string}
# };
# 15% faster if we do it like this...
$this->{parseopts} = \%opts;
if ( not $opts{isA} ) {
# transpose the existAs array into hash keys
if ( $opts{existAs} ) {
ASSERT( ref( $opts{existAs} ) eq 'ARRAY' ) if DEBUG;
ASSERT( scalar( @{ $opts{existAs} } ) ) if DEBUG;
$this->{parseopts}->{existAsList} = $opts{existAs};
$this->{parseopts}->{existAs} =
{ map { $_ => 1 } @{ $opts{existAs} } };
}
else {
$this->{parseopts}->{existAsList} = [qw(attachment topic)];
$this->{parseopts}->{existAs} = { attachment => 1, topic => 1 };
}
}
$this = bless( $this, $class );
$this->parse( $opts{string} );
}
else {
# 'Web/SubWeb' vs [qw(Web SubWeb)] (supplied as web vs webpath): if the latter
# is absent, derive it from the former (supplied as web vs webpath)
if ( not $opts{webpath} and $opts{web} ) {
$opts{webpath} = [ split( /[\/\.]/, $opts{web} ) ];
}
# $this = {
# webpath => $opts{webpath},
# topic => $opts{topic},
# tompath => $opts{tompath},
# rev => $opts{rev},
# };
print STDERR "\$this: " . Data::Dumper->Dump( [ \%opts ] )
if TRACEATTACH;
if ( $opts{attachment} and not $opts{tompath} ) {
print STDERR "Assigning {tompath} from {attachment}\n"
if TRACEATTACH;
$opts{tompath} = [ 'attachment', $opts{attachment} ];
}
elsif ( not $opts{attachment}
and $opts{tompath}
and ref( $opts{tompath} ) eq 'ARRAY'
and $opts{tompath}->[0] eq 'attachment'
and $opts{tompath}->[1] )
{
print STDERR "Assigning {attachment} from {tompath}\n"
if TRACEATTACH;
$opts{attachment} = $opts{tompath}->[1];
}
if ( DEBUG and $opts{attachment} and $opts{tompath} ) {
ASSERT(
ref( $opts{tompath} ) eq 'ARRAY'
and $opts{tompath}->[0] ne 'attachment'
or ( $opts{tompath}->[1]
and $opts{tompath}->[1] eq $opts{attachment} )
) if DEBUG;
}
#$this->parse( $_[0]->{string} );
$this = bless( \%opts, $class );
}
#push(@THESE, $this);
return $this;
}
=begin TML
---++ ClassMethod finish( )
Clean up the object, releasing any memory stored in it.
=cut
sub finish {
my ($this) = @_;
$this->{root} = undef;
$this->{web} = undef;
$this->{webpath} = undef;
$this->{topic} = undef;
$this->{rev} = undef;
$this->{tompath} = undef;
$this->{attachment} = undef;
$this->{isA} = undef;
$this->{type} = undef;
$this->{parseopts} = undef;
$this->{stringified} = undef;
$this->{stringifiedwebsep} = undef;
$this->{stringifiedtopicsep} = undef;
return;
}
=begin TML
---++ ClassMethod parse( $string, %opts ) -> $success
Parse the given string (using options provided at instantiation, unless =%opts=
overrides them) and update the instance with the resulting address.
Examples of valid path strings include:
* =Web/=
* =Web/SubWeb/=
* =Web/SubWeb.Topic= or =Web/SubWeb/Topic= or =Web.SubWeb.Topic=
* =Web/SubWeb.Topic@2= or =Web/SubWeb/Topic@2= or =Web.SubWeb.Topic@2=
* =Web/SubWeb.Topic/Attachment.pdf= or =Web/SubWeb/Topic/Attachment.pdf= or
=Web.SubWeb.Topic/Attachment.pdf=
* =Web/SubWeb.Topic/Attachment.pdf@3= or =Web/SubWeb/Topic/Attachment.pdf@3=
or =Web.SubWeb.Topic/Attachment.pdf@3=
"String" addresses are notoriously ambiguous: Foswiki traditionally allows web
& topic separators '.' & '/' to be used interchangably. For example, the
following strings could be topics or attachments (or even webs):
* =Foo.Bar=
* =Foo.Bar.Cat.Dog=
* =Foo/Bar=
* =Foo/Bar/Cat/Dog=
To resolve the ambiguity, components of ambiguous strings are tested for
existence as webs, topics or attachments and used as hints to help resolve them,
so it follows that:
<blockquote class="foswikiHelp">%X% Ambiguous address strings cannot be
considered stable; exactly which resource they resolve to depends on the
hinting algorithm, the parameters and hints supplied to it, and the existence
(or non-existence) of other resources</blockquote>
*Options:*
| *Param* | *Description* | *Values* | *Notes* |
| =webpath= or =web= %BR% =topic= | context hints | refer to explicit form |\
if =string= is ambiguous (and possibly not fully qualified, Eg. topic-only or\
attachment-only), the hinting algorithm tests =string= against them |
| =isA= | resource type specification | =$type= - 'web', 'topic',\
'attachment' | parse =string= to resolve to the specified type; exist hinting\
is skipped |
| =catchAs= | default resource type | =$type= - 'web', 'topic', 'attachment', 'none' |\
if =string= is ambiguous AND (exist hinting fails OR is disabled), THEN\
assume =string= to be (web, topic, file attachment or unparseable) |
| =existAs= | resource types to test | =\@typelist= containing one\
or more of 'web', 'topic', 'attachment' | if =string= is ambiguous, test (in\
order) as each of the specified types. Default: =[qw(attachment topic)]= |
| =existHints= | exist hinting enable/disable | =$boolean= |\
enable/disable hinting through web/topic/attachment existence checks.\
=string= *is assumed to be using the 'unambiguous' conventions below*; if it\
isn't, =catchAs= is used |
#UnambiguousStrings
---+++ Unambiguous strings
To build less ambiguous address strings, use the following conventions:
* Terminate web addresses with '/'
* Separate subwebs in the web path with '/'
* Separate topic from web path with '.'
* Separate file attachments from topics with '/'
Examples:
* =Web/SubWeb/=, =Web/=
* =Web/SubWeb.Topic=
* =Web.Topic/Attachment.pdf=
* =Web/SubWeb.Topic/Attachment.pdf=
Many strings commonly used in Foswiki will always be ambiguous (such as =Foo=,
=Foo/Bar=, =Foo/Bar/Cat=, =Foo.Bar.Cat=). Supplying an =isA= specification will
prevent the parser from using the (somewhat expensive) exist hinting heuristics.
<blockquote class="foswikiHelp">%I% In order to simplify the algorithm, a
string may only parse out as a web if:
* It is of the form =Foo/=, or
* =isA => 'web'= is specified, or
* No other type is possible, and =catchAs => 'web'= is specified
</blockquote>
The exist hinting algorithm is skipped if:
* =isA= specified
* =string= not ambiguous
If =string= is ambiguous, the hinting algorithm works roughly as follows:
* if exist hinting is disabled
* and =catchAs= is specified (parse as the =catchAs= type), otherwise
* the string cannot be parsed
* if exist hinting is enabled, the string is checked for existence as each of
the =existAs= types (default is 'attachment', 'topic')
* if there is an exact match against one of the =existAs= types (finish), otherwise
* if there were partial matches (select the combination which scores
highest), otherwise
* if =catchAs= was specified (parse as that type), otherwise
* the string cannot be parsed
The following table attempts to explain how ambiguous forms can be interpreted
and resolved.
| *String form* | *existHints* | *ambiguous* | *web[s]* | *topic* | *possible types* |
| =Foo/= | | | | | web |
| =Foo= | | %X% | | | web %BR% needs =isA => 'web'= or =catchAs => 'web'=,%BR% error otherwise |
| =Foo= | | | set | | topic |
| =Foo= | | 1 | set | set | topic, attachment |
| =Foo/Bar/= | | | | | web |
| =Foo/Bar= | | | | | topic |
| =Foo/Bar= | | 1 | set | | topic, attachment |
| =Foo.Bar= | | | | | topic |
| =Foo.Bar= | | 1 | set | set | topic, attachment |
| =Foo/Bar/Dog/= | | | | | web |
| =Foo/Bar/Dog= | | 1 | | | topic, attachment |
| =Foo.Bar/Dog= | 0 | | | | attachment |
| =Foo.Bar/Dog= | | 1 | | | topic, attachment |
| =Foo.Bar/D.g= | | | | | attachment |
| =Foo/Bar.Dog= | | | | | topic |
| =Foo/Bar.Dog= | | 1 | set | | topic, attachment |
| =Foo.Bar.Dog= | | | | | topic |
| =Foo.Bar.Dog= | | 1 | set | set | topic, attachment |
| =Foo/Bar/Dog/Cat/= | | | | | web |
| =Foo/Bar.Dog.Cat= | | | | | topic |
| =Foo/Bar.Dog.Cat= | | 1 | set | | topic, attachment |
| =Foo/Bar.Dog/Cat= | | | | | attachment |
| =Foo/Bar.Dog/C.t= | | | | | attachment |
| =Foo/Bar/Dog.Cat= | 0 | | | | topic |
| =Foo/Bar/Dog.Cat= | | 1 | | | topic, attachment |
| =Foo/Bar/Dog/Cat= | | 1 | | | topic, attachment |
| =Foo/Bar/Dog/C.t= | | 1 | | | topic, attachment |
| =Foo.Bar.Dog/Cat= | 0 | | | | attachment |
| =Foo.Bar.Dog/Cat= | | 1 | | | topic, attachment |
| =Foo.Bar.Dog/C.t= | | | | | attachment |
=cut
sub parse {
my ( $this, $path, %opts ) = @_;
$this->_invalidate();
if ( not $this->{parseopts} ) {
$this->{parseopts} = {
web => $opts{web},
webpath => $opts{webpath},
topic => $opts{topic},
rev => $opts{rev},
existAsList => [qw(attachment topic)],
existAs => { attachment => 1, topic => 1 }
};
}
%opts = ( %{ $this->{parseopts} }, %opts );
ASSERT(
( !defined $opts{rev} || $opts{rev} =~ /^[-\+]?\d+$/ ),
"rev: '"
. ( defined $opts{rev} ? $opts{rev} : 'undef' )
. "' is numeric"
) if DEBUG;
ASSERT( $opts{isA} or defined $opts{existAs} ) if DEBUG;
if ( $path =~ s/\@([-\+]?\d+)$// ) {
$this->{rev} = $1;
}
# if necessary, populate webpath from web parameter
if ( not $opts{webpath} and $opts{web} ) {
$opts{webpath} = [ split( /[\/\.]/, $opts{web} ) ];
}
ASSERT( not $opts{webpath} or ref( $opts{webpath} ) eq 'ARRAY' ) if DEBUG;
# Because of the way we split, 'Foo/' causes final element to be empty
if ( $opts{webpath} and not $opts{webpath}->[-1] ) {
pop( @{ $opts{webpath} } );
}
# pre-compute web's string form (avoid unnecessary join()s)
if ( not $opts{web} and $opts{webpath} ) {
$opts{web} = join( '/', @{ $opts{webpath} } );
}
# Is the path explicit?
if ( not $opts{isA} ) {
if ( substr( $path, -1, 1 ) eq '/' ) {
if ( length($path) > 1 ) {
$opts{isA} = 'web';
}
else {
# $path eq '/' - the mythical "root" path
$opts{isA} = 'root';
}
}
elsif ( substr( $path, 0, 1 ) eq '\'' or $path =~ /\[/ ) {
$opts{isA} = '*';
}
}
# Here we go... short-circuit testing if we already have an isA spec
if ( $opts{isA} ) {
print STDERR "parse(): isA: $opts{isA}\n" if TRACE2;
ASSERT( $atomiseAs{ $opts{isA} } ) if DEBUG;
$atomiseAs{ $opts{isA} }->( $this, $this, $path, \%opts );
}
else {
my @separators = ( $path =~ m/([\.\/])/g );
my $sepboost = 0;
my $sepident = '';
my $lastsep;
my $plaus;
my @trylist;
my $normalform;
my %typeatoms;
my %typescores;
my $parsed;
ASSERT( ref( $opts{existAsList} ) eq 'ARRAY' ) if DEBUG;
if ( scalar(@separators) ) {
# build the separator-based identity of the path string, Eg.
# Foo/Bar/Dog.Cat/B.a.t = 'SdsD'
# TemporaryAddressTestsTestWeb/SubWeb/SubSubWeb.Topic/Atta.hme.t
foreach my $sep (@separators) {
if ( defined $lastsep ) {
if ( $lastsep ne $sep ) {
$sepident .= $sepidentchars{$sepboost}->{$lastsep};
$lastsep = $sep;
$sepboost = 0;
}
else {
$sepboost = 1;
}
}
else {
$lastsep = $sep;
}
}
$sepident .= $sepidentchars{$sepboost}->{$lastsep};
}
$plaus = $plausibletable{$sepident};
print STDERR "Identity\t$sepident calculated for $path, plaustable: "
. Data::Dumper->Dump( [$plaus] )
if TRACE;
# Is the identity known?
if ($plaus) {
# Default to exist hinting enabled
if ( not defined $opts{existHints} ) {
$opts{existHints} = 1;
}
# (ab)using %opts to match values from the plausible table
$opts{1} = 1;
$opts{2} = 1;
# @trylist is the intersection of existAs list and the plausible
# list. existAs ordering is used unless string is "unambiguous"
# form, in which case that type is positioned first.
foreach my $type ( @{ $opts{existAsList} } ) {
# If the type is plausible, and the options support it
if ( $plaus->{$type} and $opts{ $plaus->{$type} } ) {
# If an "unambiguous" form, put it first in the @trylist.
if ( $plaus->{$type} eq 2 ) {
unshift( @trylist, $type );
$normalform = $type;
# If existHints are allowed, add the plausible type to list
}
elsif ( $opts{existHints} ) {
push( @trylist, $type );
}
}
}
# Exist hinting. The first complete hit, or the hit which matches
# the most (out of the existAsList, Eg.: attachment, topic, web)
# wins. The former should naturally fall out of the latter, unless
# the existAs list is not ordered smallestthing-first
if ( $opts{existHints} ) {
my $i = 0;
my $ntrylist = scalar(@trylist);
my $besttype;
my $bestscore;
my $bestscoredtype;
# If a complete hit is detected, we set $besttype & exit early
while ( $ntrylist > $i and not $besttype ) {
my $score;
my $type = $trylist[$i];
$i += 1;
print STDERR "Trying to atomise $path as $type...\n"
if TRACE;
ASSERT( $atomiseAs{$type} ) if DEBUG;
$typeatoms{$type} =
$atomiseAs{$type}->( $this, {}, $path, \%opts );
print STDERR "Atomised $path as $type, result: "
. Data::Dumper->Dump( [ $typeatoms{$type} ] )
if TRACE;
( $besttype, $score ) =
$this->_existScore( $typeatoms{$type}, $type );
if (TRACE) {
print STDERR 'existScore: '
. ( $score || '' )
. ' besttype: '
. ( $besttype || '' ) . "\n";
}
if ( $score
and ( not defined $bestscore or $bestscore < $score ) )
{
$bestscoredtype = $type;
$bestscore = $score;
}
}
# Unless we already got a perfect hit; find the type for this
# path that gives the highest score
if ( not $besttype ) {
$besttype = $bestscoredtype;
}
# Copy the atoms from the best hit into our instance.
if ($besttype) {
$this->{web} = $typeatoms{$besttype}->{web};
$this->{webpath} = $typeatoms{$besttype}->{webpath};
$this->{topic} = $typeatoms{$besttype}->{topic};
$this->{tompath} = $typeatoms{$besttype}->{tompath};
$this->{attachment} = $typeatoms{$besttype}->{attachment};
$parsed = 1;
}
}
}
if ( not $parsed ) {
my $type = $normalform || $opts{catchAs};
if ($type) {
ASSERT( $atomiseAs{$type} ) if DEBUG;
$typeatoms{$type} =
$atomiseAs{$type}->( $this, $this, $path, \%opts );
}
}
}
return $this->isValid();
}
#sub _atomiseAs {
# my ( $this, $that, $path, $type, $opts ) = @_;
#
# ASSERT($path) if DEBUG;
# ASSERT($type) if DEBUG;
# ASSERT( $atomiseAs{$type} ) if DEBUG;
# $atomiseAs{$type}->( $this, $that, $path, $opts );
#
# return $that;
#}
sub _atomiseAsRoot {
my ( $this, $that, $path, $opts ) = @_;
print STDERR "_atomiseAsRoot():\n" if TRACE2;
ASSERT( $path eq '/' ) if DEBUG;
$that->{root} = 1;
$that->{web} = undef;
$that->{webpath} = undef;
$that->{topic} = undef;
$that->{tompath} = undef;
$that->{attachment} = undef;
return $that;
}
sub _atomiseAsWeb {
my ( $this, $that, $path, $opts ) = @_;
print STDERR "_atomiseAsWeb():\n" if TRACE2;
$that->{web} = $path;
$that->{webpath} = [ split( /[\.\/]/, $path ) ];
ASSERT( $that->{web} and ref( $that->{webpath} ) eq 'ARRAY' ) if DEBUG;
# If we had a path that looks like 'Foo/'
if ( not $that->{webpath}->[-1] ) {
pop( @{ $that->{webpath} } );
chop( $that->{web} );
}
$that->{topic} = undef;
$that->{tompath} = undef;
$that->{attachment} = undef;
return $that;
}
sub _atomiseAsTopic {
my ( $this, $that, $path, $opts ) = @_;
ASSERT($path) if DEBUG;
my @parts = split( /[\.\/]/, $path );
my $nparts = scalar(@parts);
print STDERR "_atomiseAsTopic(): path: $path, nparts: $nparts\n" if TRACE2;
if ( $nparts == 1 ) {
if ( $opts->{webpath}
and ref( $opts->{webpath} ) eq 'ARRAY'
and scalar( @{ $opts->{webpath} } ) )
{
$that->{web} = $opts->{web};
$that->{webpath} = $opts->{webpath};
$that->{topic} = $path;
}
}
else {
$that->{webpath} = [ @parts[ 0 .. ( $nparts - 2 ) ] ];
$that->{web} = undef;
# $that->{web} = join( '/', @{ $that->{webpath} } );
$that->{topic} = $parts[-1];
}
$that->{tompath} = undef;
$that->{attachment} = undef;
ASSERT( $that->{webpath} or not $that->{topic} ) if DEBUG;
# ASSERT( $that->{web} ) if DEBUG;
return $that;
}
sub _atomiseAsAttachment {
my ( $this, $that, $path, $opts ) = @_;
print STDERR "_atomiseAsAttachment():\n" if TRACE2;
ASSERT($path) if DEBUG;
if ( my ( $lhs, $file ) = ( $path =~ /^(.*?)\/([^\/]+)$/ ) ) {
$that = $this->_atomiseAsTopic( $that, $lhs, $opts );
$that->{tompath} = [ 'attachment', $file ];
$that->{attachment} = $file;
}
else {
if ( $opts->{webpath} and $opts->{topic} ) {
$that->{webpath} = $opts->{webpath};
$that->{web} = $opts->{web};
$that->{topic} = $opts->{topic};
$that->{tompath} = [ 'attachment', $path ];
$that->{attachment} = $path;
}
}
return $that;
}
=begin TML
---++ PRIVATE ClassMethod _atomiseAsTOM ( $that, $path, $opts ) => $that
Parse a small subset ('static' meta path forms) of QuerySearch (VarQUERY)
compatible expressions.
=$opts= is a hashref holding default context
'topic'/ ref part is optional; =_atomiseAsTOM()= falls-back to default topic
context supplied in =$opts= otherwise. In other words, both of these forms are
supported:
* ='Web/SubWeb.Topic@3'/META:FIELD[name='Colour'].value=
* =META:FIELD[name='Colour'].value=
| *Form* | *tompath* | *type* |
| =META= | =['META']= | meta |
| =META:FIELD= | =['META', 'FIELD']= | metatype |
| =META:FIELD[name='Colour']= | =['META', 'FIELD', {name => 'Colour'}]= | metamember |
| =META:FIELD[3]= | =['META', 'FIELD', 3]= | metamember |
| =META:FIELD[name='Colour'].value= | =['META', 'FIELD', {name => 'Colour'}, 'value']= | metakey |
| =META:FIELD[3].value= | =['META', 'FIELD', 3, 'value']= | metakey |
| =fields= | =['META', 'FIELD']= | metatype |
| =fields[name='Colour']= | =['META', 'FIELD', {name => 'Colour'}]= | metamember |
| =fields[3]= | =['META', 'FIELD', 3]= | metamember |
| =fields[name='Colour'].value= | =['META', 'FIELD', 3, 'value']= | metakey |
| =MyForm= | =['META', 'FIELD', {form => 'MyForm'}]= | metatype |
| =MyForm[name='Colour']= | =['META', 'FIELD', {form => 'MyForm', name => 'Colour'}]= | metamember |
| =MyForm[name='Colour'].value= | =['META', 'FIELD', {form => 'MyForm', name => 'Colour'}, 'value']= | metakey |
| =MyForm.Colour= | =['META', 'FIELD', {form => 'MyForm', name => 'Colour'}, 'value']= | metakey |
| =Colour= | =['META', 'FIELD', {name => 'Colour'}, 'value']= | metakey |
=cut
sub _atomiseAsTOM {
my ( $this, $that, $path, $opts ) = @_;
print STDERR "_atomiseAsTOM():\n" if TRACE2;
# QuerySearch meta path?
# SMELL: This should be done in the query parser...
# ... or at least use Regexp::Grammars
# TODO: member selectors may only be on 1 or 2 keys, or array index
if (
$path =~ /^
( # 1
'([^']+)' # 2 'Web.Topic@123'
\s* \/ \s*
)?
(META:)? # 3 META:
([^\[\s\.]+) # 4 PART, FIELD, alias, MyForm, FieldName
(\s* \[ \s* # 5 [............]
( # 6 n (or)
[-\+]?\d+
|( # 7 name='foo'[ AND bar='cat' [ AND dog='bat' ...]]
([^=\s]+) # 8 name
\s* = \s* # =
'([^']+)' # 9 'foo'
( # 10 multi-key selector?
\s* AND \s*
([^=\s]+) # 11 bar
\s* = \s* # =
'([^']+)' # 12 'cat'
)?
)
)
\s* \])?
(\s* \. \s* # 13 .
(\w+?) # 14 value
)?
$/x
)
{
my $webtopicrev = $2;
my @tompath;
my $doneselector;
my $doneaccessor;
if ($3) { # META:
@tompath = ('META');
push( @tompath, $4 );
if ( not $5 and $14 ) { # Eg. META:TOPICINFO.author
push( @tompath, undef, $14 );
$doneselector = 1;
$doneaccessor = 1;
}
}
elsif ( $pathtypes{$4} ) { # META, attachment, SECTION, text
@tompath = ($4);
}
elsif ( $Foswiki::Meta::aliases{$4} ) { # fields, attachments, info
@tompath = ('META');
# strip off the 'META:' part
push( @tompath, substr( $Foswiki::Meta::aliases{$4}, 5 ) );
if ( not $5 and $14 ) { # Eg. info.author
push( @tompath, undef, $14 );
$doneselector = 1;
$doneaccessor = 1;
}
}
elsif ($4) { # SomeFormField or SomethingForm
@tompath = ('META');
push( @tompath, 'FIELD' );
if ( not( $14 or $6 ) ) { # SomeFormField
# SMELL: This catches "'Web.Topic@123'/MyForm" & "MyForm"
push( @tompath, { name => $4 }, 'value' );
$doneselector = 1;
$doneaccessor = 1;
}
elsif ( substr( $4, -4, 4 ) eq 'Form' ) { # SomethingForm
push( @tompath, { form => $4 } );
if ($8) { # SomethingForm[a=b
ASSERT( defined $9 ) if DEBUG;
$tompath[-1]->{$8} = $9;
if ($11) { # SomethingForm[a=b AND c=d]
ASSERT( defined $12 ) if DEBUG;
$tompath[-1]->{$11} = $12;
}
$doneselector = 1;
}
elsif ($6) { # SomethingForm[n]
push( @tompath, $6 );
$doneselector = 1;
ASSERT( $6 =~ /^\d+$/ ) if DEBUG;
}
elsif ($14) {
$tompath[-1]->{name} = $14;
push( @tompath, 'value' );
$doneaccessor = 1;
}
}
elsif (DEBUG) { # form not /Form$/ or alias from disabled plugin
ASSERT(0);
}
}
elsif (DEBUG) { # Shouldn't get here
ASSERT(0);
}
if ( not $doneselector and $6 ) { # SOMETHING[...]
if ($8) { # SOMETHING[a=b
ASSERT( defined $9 ) if DEBUG;
push( @tompath, { $8 => $9 } );
if ($11) { # SOMETHING[a=b AND c=d]
ASSERT( defined $12 ) if DEBUG;
$tompath[-1]->{$11} = $12;
}
}
else { # SOMETHING[n]
ASSERT($6) if DEBUG;
push( @tompath, $6 );
ASSERT( $6 =~ /^\d+$/ ) if DEBUG;
}
$doneselector = 1;
}
if ( not $doneaccessor and $14 ) {
push( @tompath, $14 );
}
$that->{tompath} = \@tompath;
if ($webtopicrev) {
my $refAddr = Foswiki::Address->new(
string => $webtopicrev,
isA => 'topic',
webpath => $opts->{webpath},
web => $opts->{web}
);
$that->{web} = $refAddr->{web};
$that->{webpath} = $refAddr->{webpath};
$that->{topic} = $refAddr->{topic};
$that->{rev} = $refAddr->{rev};
ASSERT(
( !defined $that->{rev} || $that->{rev} =~ /^[-\+]?\d+$/ ),
"rev '"
. ( defined $that->{rev} ? $that->{rev} : 'undef' )
. "' is numeric"
) if DEBUG;
}
else {
$that->{webpath} = $opts->{webpath};
$that->{topic} = $opts->{topic};
$that->{rev} = undef;