/
Store.pm
2153 lines (1646 loc) · 62.3 KB
/
Store.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
=begin TML
---+ package Foswiki::Store
This module hosts the generic storage backend. This module provides
the interface layer between the "real" store provider - which is hidden
behind a handler - and the rest of the system. it is responsible for
checking for topic existance, access permissions, and all the other
general admin tasks that are common to all store implementations.
This module knows nothing about how the data is actually _stored_ -
that knowledge is entirely encapsulated in the handlers.
The general contract for methods in the class requires that errors
are signalled using exceptions. Foswiki::AccessControlException is
used for access control exceptions, and Error::Simple for all other
types of error.
=cut
package Foswiki::Store;
use strict;
use Assert;
use Error qw( :try );
require Foswiki;
require Foswiki::Meta;
require Foswiki::Sandbox;
require Foswiki::AccessControlException;
use vars qw( $STORE_FORMAT_VERSION );
$STORE_FORMAT_VERSION = '1.1';
BEGIN {
# Do a dynamic 'use locale' for this module
if ( $Foswiki::cfg{UseLocale} ) {
require locale;
import locale();
}
}
=begin TML
---++ ClassMethod new($session)
Construct a Store module, linking in the chosen sub-implementation.
=cut
sub new {
my ( $class, $session ) = @_;
my $this = bless( { session => $session }, $class );
$this->{IMPL} = 'Foswiki::Store::' . $Foswiki::cfg{StoreImpl};
eval 'require ' . $this->{IMPL};
if ($@) {
die "$this->{IMPL} compile failed $@";
}
return $this;
}
=begin TML
---++ ObjectMethod finish()
Break circular references.
=cut
# Note to developers; please undef *all* fields in the object explicitly,
# whether they are references or not. That way this method is "golden
# documentation" of the live fields in the object.
sub finish {
my $this = shift;
undef $this->{IMPL};
undef $this->{session};
}
# PRIVATE
# Get the handler for the current store implementation.
# $web, $topic and $attachment _must_ be untainted.
sub _getHandler {
my ( $this, $web, $topic, $attachment ) = @_;
my $handler = $this->{IMPL}->new( $this->{session}, $web, $topic, $attachment );
my $map = $Foswiki::cfg{Plugins}{TWikiCompatibilityPlugin}{WebSearchPath};
if ($Foswiki::cfg{Plugins}{TWikiCompatibilityPlugin}{Enabled}
&& defined ($map)
&& defined ($map->{$web})
&& !$handler->storedDataExists()
) {
#try the other
my $newhandler = $this->{IMPL}->new( $this->{session}, $map->{$web}, $topic, $attachment );
if ($newhandler->storedDataExists()) {
$handler = $newhandler;
}
}
return $handler;
}
=begin TML
---++ ObjectMethod readTopic($user, $web, $topic, $version) -> ($metaObject, $text)
Reads the given version of a topic and it's meta-data. If the version
is undef, then read the most recent version. The version number must be
an integer, or undef for the latest version.
if $user is defined, view permission will be required for the topic
read to be successful. Access control violations are flagged by a
Foswiki::AccessControlException. Permissions are checked for the user
name passed in.
If the topic contains a web specification (is of the form Web.Topic) the
web specification will override whatever is passed in $web.
The metadata and topic text are returned separately, with the metadata in a
Foswiki::Meta object. (The topic text is, as usual, just a string.)
=cut
sub readTopic {
my ( $this, $user, $web, $topic, $version ) = @_;
$web =~ s#\.#/#go;
if ( defined $version ) {
$version = $this->cleanUpRevID($version);
}
# SMELL: assumes that the backend can't store meta outside the topic
my $text = $this->readTopicRaw( $user, $web, $topic, $version );
my $meta = new Foswiki::Meta( $this->{session}, $web, $topic, $text );
# Override meta with that blended from pub.
if (
$Foswiki::cfg{AutoAttachPubFiles}
&& $web eq $this->{session}->{webName}
&& # only check the currently requested topic
$topic eq $this->{session}->{topicName}
)
{
my @knownAttachments = $meta->find('FILEATTACHMENT');
my @attachmentsFoundInPub =
_findAttachments( $this, $web, $topic, \@knownAttachments );
my @validAttachmentsFound;
foreach my $foundAttachment (@attachmentsFoundInPub) {
my ( $fileName, $origName ) =
Foswiki::Sandbox::sanitizeAttachmentName(
$foundAttachment->{name} );
#test if the attachment filenam would need sanitizing, if so, ignore it.
if ( $fileName ne $origName ) {
$this->{session}->writeWarning(
"AutoAttachPubFiles ignoring $origName, in $web.$topic - not a valid Attachment filename"
);
}
else {
push @validAttachmentsFound, $foundAttachment;
}
}
$meta->putAll( 'FILEATTACHMENT', @validAttachmentsFound )
if @validAttachmentsFound;
}
return ( $meta, $meta->text() );
}
=begin TML
---++ ObjectMethod _findAttachments($session, $web, $topic, $knownAttachments) -> @attachmentsFoundInPub
Synchronise the attachment list with what's actually on disk Returns an ARRAY
of FILEATTACHMENTs. These can be put in the new meta using
meta->put('FILEATTACHMENTS', $tree)
This function is only called when the AutoAttachPubFiles configuration option is set.
IDEA On Windows machines where the underlying filesystem can store arbitary
meta data against files, this might replace/fulfil the COMMENT purpose
TODO consider logging when things are added to metadata
=cut
sub _findAttachments {
my ( $this, $web, $topic, $attachmentsKnownInMeta ) = @_;
my $session = $this->{session};
ASSERT( $session->isa('Foswiki') ) if DEBUG;
my $store = $this;
my %filesListedInPub = $store->getAttachmentList( $web, $topic );
my %filesListedInMeta = ();
# You need the following lines if you want metadata to supplement
# the filesystem
if ( defined $attachmentsKnownInMeta ) {
%filesListedInMeta = map { $_->{name} => $_ } @$attachmentsKnownInMeta;
}
foreach my $file ( keys %filesListedInPub ) {
if ( $filesListedInMeta{$file} ) {
# Bring forward any missing yet wanted attributes
#SMELL: this will over-write (empty) any meta data field not listed here :(
foreach my $field qw(comment attr user version) {
if ( $filesListedInMeta{$file}{$field} ) {
$filesListedInPub{$file}{$field} =
$filesListedInMeta{$file}{$field};
}
}
# Develop:Bugs.Item452 - WHY IS USER STILL WRONG?
}
}
# A comparison of the keys of the $filesListedInMeta and %filesListedInPub
# would show files that were in Meta but have disappeared from Pub.
# SMELL Meta really ought index its attachments in a hash by attachment
# name but this is not the case
#
# SMELL: Do not change this from array to hash, you would lose the
# proper attachment sequence
#
my @deindexedBecauseMetaDoesnotIndexAttachments = values(%filesListedInPub);
return @deindexedBecauseMetaDoesnotIndexAttachments;
}
=begin TML
---++ ObjectMethod readTopicRaw( $user, $web, $topic, $version ) -> $topicText
Reads the given version of a topic, without separating out any embedded
meta-data. If the version is undef, then read the most recent version.
The version number must be an integer or undef.
If $user is defined, view permission will be required for the topic
read to be successful. Access control violations are flagged by a
Foswiki::AccessControlException. Permissions are checked for the user
name passed in.
If the topic contains a web specification (is of the form Web.Topic) the
web specification will override whatever is passed in $web.
SMELL: DO NOT CALL THIS METHOD UNLESS YOU HAVE NO CHOICE. This method breaks
encapsulation of the store, as it assumes meta is stored embedded in the text.
Other implementors of store will be forced to insert meta-data to ensure
correct operation of View raw=debug and the 'repRev' mode of Edit.
$web and $topic _must_ be untainted.
=cut
sub readTopicRaw {
my ( $this, $user, $web, $topic, $version ) = @_;
$web =~ s#\.#/#go;
# test if topic contains a webName to override $web
( $web, $topic ) = $this->{session}->normalizeWebTopicName( $web, $topic );
my $text;
my $handler = _getHandler( $this, $web, $topic );
unless ($version) {
$text = $handler->getLatestRevision();
}
else {
$text = $handler->getRevision($version);
}
# Note: passing undef as meta will cause extraction of the meta
# from the (raw) text passed
# SMELL: assumes that the backend can't store meta outside the topic
if (
$user
&& !$this->{session}->security->checkAccessPermission(
'VIEW', $user, $text, undef, $topic, $web
)
)
{
my $users = $this->{session}->{users};
throw Foswiki::AccessControlException( 'VIEW', $user, $web, $topic,
$this->{session}->security->getReason() );
}
return $text;
}
=begin TML
---++ ObjectMethod moveAttachment( $oldWeb, $oldTopic, $oldAttachment, $newWeb, $newTopic, $newAttachment, $user )
Move an attachment from one topic to another.
The caller to this routine should check that all topics are valid.
All parameters must be defined, and must be untainted.
=cut
sub moveAttachment {
my ( $this, $oldWeb, $oldTopic, $oldAttachment, $newWeb, $newTopic,
$newAttachment, $user )
= @_;
my $users = $this->{session}->{users};
$this->lockTopic( $user, $oldWeb, $oldTopic );
try {
my ( $ometa, $otext ) = $this->readTopic( undef, $oldWeb, $oldTopic );
if (
$user
&& !$this->{session}->security->checkAccessPermission(
'CHANGE', $user, $otext, $ometa, $oldTopic, $oldWeb
)
)
{
throw Foswiki::AccessControlException( 'CHANGE', $user, $oldWeb,
$oldTopic, $this->{session}->security->getReason() );
}
my ( $nmeta, $ntext ) = $this->readTopic( undef, $newWeb, $newTopic );
if (
$user
&& !$this->{session}->security->checkAccessPermission(
'CHANGE', $user, $ntext, $nmeta, $newTopic, $newWeb
)
)
{
throw Foswiki::AccessControlException( 'CHANGE', $user, $newWeb,
$newTopic, $this->{session}->security->getReason() );
}
# Remove file attachment from old topic
my $handler = _getHandler( $this, $oldWeb, $oldTopic, $oldAttachment );
$handler->moveAttachment( $newWeb, $newTopic, $newAttachment );
my $fileAttachment = $ometa->get( 'FILEATTACHMENT', $oldAttachment );
$ometa->remove( 'FILEATTACHMENT', $oldAttachment );
_noHandlersSave( $this, $user, $oldWeb, $oldTopic, $otext, $ometa,
{ notify => 0 } );
# we move the attachment in the the old topic
# so we need to remove the attachment in nmeta
if ( $oldTopic eq $newTopic && $oldWeb eq $newWeb ) {
$nmeta->remove( 'FILEATTACHMENT', $oldAttachment );
}
# Add file attachment to new topic
$fileAttachment->{name} = $newAttachment;
$fileAttachment->{movefrom} =
$oldWeb . '.' . $oldTopic . '.' . $oldAttachment;
$fileAttachment->{moveby} = $user;
$fileAttachment->{movedto} =
$newWeb . '.' . $newTopic . '.' . $newAttachment;
$fileAttachment->{movedwhen} = time();
$nmeta->putKeyed( 'FILEATTACHMENT', $fileAttachment );
_noHandlersSave(
$this, $user, $newWeb,
$newTopic,
$ntext, $nmeta,
{
dontlog => 1,
notify => 0,
comment => 'moved'
}
);
$this->{session}->writeLog(
'move',
$fileAttachment->{movefrom}
. ' moved to '
. $fileAttachment->{movedto},
$users->webDotWikiName($user)
);
}
finally {
$this->unlockTopic( $user, $oldWeb, $oldTopic );
$this->unlockTopic( $user, $newWeb, $newTopic );
};
# alert plugins of attachment move
$this->{session}->{plugins}
->dispatch( 'afterRenameHandler', $oldWeb, $oldTopic, $oldAttachment,
$newWeb, $newTopic, $newAttachment );
}
=begin TML
---++ ObjectMethod getAttachmentStream( $user, $web, $topic, $attName ) -> \*STREAM
* =$user= - the user doing the reading, or undef if no access checks
* =$web= - The web
* =$topic= - The topic
* =$attName= - Name of the attachment
Open a standard input stream from an attachment.
If $user is defined, view permission will be required for the topic
read to be successful. Access control violations and errors will
cause exceptions to be thrown.
Permissions are checked for the user name passed in.
=cut
sub getAttachmentStream {
my ( $this, $user, $web, $topic, $att ) = @_;
if (
$user
&& !$this->{session}->security->checkAccessPermission(
'VIEW', $user, undef, undef, $topic, $web
)
)
{
my $users = $this->{session}->{users};
throw Foswiki::AccessControlException( 'VIEW', $user, $web, $topic,
$this->{session}->security->getReason() );
}
my $handler = _getHandler( $this, $web, $topic, $att );
return $handler->getStream();
}
=begin TML
---++ ObjectMethod getAttachmentList($web, $topic)
returns @($attachmentName => [stat]) for any given web, topic
=cut
sub getAttachmentList {
my ( $this, $web, $topic ) = @_;
my $handler = _getHandler( $this, $web, $topic );
return $handler->getAttachmentList( $web, $topic );
}
=begin TML
---++ ObjectMethod attachmentExists( $web, $topic, $att ) -> $boolean
Determine if the attachment already exists on the given topic
=cut
sub attachmentExists {
my ( $this, $web, $topic, $att ) = @_;
my $handler = _getHandler( $this, $web, $topic, $att );
return $handler->storedDataExists();
}
=begin TML
---++ ObjectMethod _removeAutoAttachmentsFromMeta
This is where we are going to remove from meta any entry that is marked as an automatic attachment.
=cut
sub _removeAutoAttachmentsFromMeta {
my ( $this, $meta ) = @_;
# use Data::Dumper;
# die "removeAutoAttachmentsFromMeta".Dumper($meta);
return $meta;
}
=begin TML
---++ ObjectMethod moveTopic( $oldWeb, $oldTopic, $newWeb, $newTopic, $user )
All parameters must be defined and must be untainted.
=cut
sub moveTopic {
my ( $this, $oldWeb, $oldTopic, $newWeb, $newTopic, $user ) = @_;
my $handler = _getHandler( $this, $oldWeb, $oldTopic, '' );
my $rev = $handler->numRevisions();
my $users = $this->{session}->{users};
# will block
$this->lockTopic( $user, $oldWeb, $oldTopic );
# Clear outstanding leases. We assume that the caller has checked
# that the lease is OK to kill.
$this->clearLease( $oldWeb, $oldTopic )
if $this->getLease( $oldWeb, $oldTopic );
try {
my $otext = $this->readTopicRaw( undef, $oldWeb, $oldTopic );
# Note: undef $meta param will cause $otext to be parsed for meta
if (
$user
&& !$this->{session}->security->checkAccessPermission(
'CHANGE', $user, $otext, undef, $oldTopic, $oldWeb
)
)
{
throw Foswiki::AccessControlException( 'CHANGE', $user, $oldWeb,
$oldTopic, $this->{session}->security->getReason() );
}
my ( $nmeta, $ntext );
if ( $this->topicExists( $newWeb, $newTopic ) ) {
( $nmeta, $ntext ) = $this->readTopic( undef, $newWeb, $newTopic );
}
if (
$user
&& !$this->{session}->security->checkAccessPermission(
'CHANGE', $user, $ntext, $nmeta, $newTopic, $newWeb
)
)
{
throw Foswiki::AccessControlException( 'CHANGE', $user, $newWeb,
$newTopic, $this->{session}->security->getReason() );
}
$handler->moveTopic( $newWeb, $newTopic );
}
finally {
$this->unlockTopic( $user, $oldWeb, $oldTopic );
};
if ( $newWeb ne $oldWeb ) {
# Record that it was moved away
$handler->recordChange( $user, $rev );
}
$handler = _getHandler( $this, $newWeb, $newTopic, '' );
$handler->recordChange( $user, $rev );
# Log rename
if ( $Foswiki::cfg{Log}{rename} ) {
my $old = $oldWeb . '.' . $oldTopic;
my $new = $newWeb . '.' . $newTopic;
$this->{session}->writeLog( 'rename', $old, "moved to $new", $user );
}
# alert plugins of topic move
$this->{session}->{plugins}
->dispatch( 'afterRenameHandler', $oldWeb, $oldTopic, '', $newWeb,
$newTopic, '' );
}
=begin TML
---++ ObjectMethod moveWeb( $oldWeb, $newWeb, $user )
Move a web.
All parrameters must be defined and must be untainted.
=cut
sub moveWeb {
my ( $this, $oldWeb, $newWeb, $user ) = @_;
$oldWeb =~ s/\./\//go;
$newWeb =~ s/\./\//go;
my (@webList) = $this->getListOfWebs( 'public', $oldWeb );
unshift( @webList, $oldWeb );
foreach my $webIter (@webList) {
if ($webIter) {
my @webTopicList = $this->getTopicNames($webIter);
foreach my $webTopic (@webTopicList) {
$this->lockTopic( $user, $webIter, $webTopic );
}
}
}
my @newParentPath = split( /\//, $newWeb );
pop(@newParentPath);
my $newParent = join( '/', @newParentPath );
my $handler = _getHandler( $this, $oldWeb );
$handler->moveWeb($newWeb);
(@webList) = $this->getListOfWebs( 'public', $newWeb );
unshift( @webList, $newWeb );
foreach my $webIter (@webList) {
if ($webIter) {
my @webTopicList = $this->getTopicNames($webIter);
foreach my $webTopic (@webTopicList) {
$this->unlockTopic( $user, $webIter, $webTopic );
}
}
}
# Log rename
if ( $Foswiki::cfg{Log}{rename} ) {
$this->{session}
->writeLog( 'renameweb', $oldWeb, 'moved to ' . $newWeb, $user );
}
# alert plugins of web move
$this->{session}->{plugins}
->dispatch( 'afterRenameHandler', $oldWeb, '', '', $newWeb, '', '' );
}
=begin TML
---++ ObjectMethod readAttachment( $user, $web, $topic, $attachment, $theRev ) -> $text
Read the given version of an attachment, returning the content.
View permission on the topic is required for the
read to be successful. Access control violations are flagged by a
Foswiki::AccessControlException. Permissions are checked for the user
passed in.
If $theRev is not given, the most recent rev is assumed.
=cut
sub readAttachment {
my ( $this, $user, $web, $topic, $attachment, $theRev ) = @_;
if (
$user
&& !$this->{session}->security->checkAccessPermission(
'VIEW', $user, undef, undef, $topic, $web
)
)
{
my $users = $this->{session}->{users};
throw Foswiki::AccessControlException( 'VIEW', $user, $web, $topic,
$this->{session}->security->getReason() );
}
my $handler = _getHandler( $this, $web, $topic, $attachment );
return $handler->getRevision($theRev);
}
=begin TML
---++ ObjectMethod getRevisionNumber ( $web, $topic, $attachment ) -> $integer
Get the revision number of the most recent revision. Returns
the integer revision number or '' if the topic doesn't exist.
WORKS FOR ATTACHMENTS AS WELL AS TOPICS
=cut
sub getRevisionNumber {
my ( $this, $web, $topic, $attachment ) = @_;
$attachment = '' unless $attachment;
my $handler = _getHandler( $this, $web, $topic, $attachment );
return $handler->numRevisions();
}
=begin TML
---+++ ObjectMethod getWorkArea( $key ) -> $directorypath
Gets a private directory uniquely identified by $key. The directory is
intended as a work area for plugins. The directory will exist.
=cut
sub getWorkArea {
my ( $this, $key ) = @_;
my $handler = _getHandler( $this, );
return $handler->getWorkArea($key);
}
=begin TML
---++ ObjectMethod getRevisionDiff ( $user, $web, $topic, $rev1, $rev2, $contextLines ) -> \@diffArray
Return reference to an array of [ diffType, $right, $left ]
* =$user= - the user id, or undef to suppress access control checks
* =$web= - the web
* =$topic= - the topic
* =$rev1= Integer revision number
* =$rev2= Integer revision number
* =$contextLines= - number of lines of context required
=cut
sub getRevisionDiff {
my ( $this, $user, $web, $topic, $rev1, $rev2, $contextLines ) = @_;
ASSERT( defined($contextLines) ) if DEBUG;
if ($user) {
my $r1;
try {
# Make sure both revs are readable.
$r1 = $this->readTopicRaw( $user, $web, $topic, $rev1 );
}
catch Foswiki::AccessControlException with {
$r1 = undef;
};
my $r2;
try {
$r2 = $this->readTopicRaw( $user, $web, $topic, $rev2 );
}
catch Foswiki::AccessControlException with {
$r2 = undef;
};
my $rd;
if ( !defined($r1) ) {
$rd = [ [ '-', " *Revision $rev1 is unreadable* ", '' ] ];
if ( !defined($r2) ) {
push( @$rd, [ '+', '', " *Revision $rev2 is unreadable* " ] );
}
else {
foreach ( split( /\r?\n/, $r2 ) ) {
push( @$rd, [ '+', '', $_ ] );
}
}
}
elsif ( !defined($r2) ) {
$rd = [ [ '+', '', " *Revision $rev2 is unreadable* " ] ];
foreach ( split( /\r?\n/, $r1 ) ) {
push( @$rd, [ '-', $_, '' ] );
}
}
return $rd if $rd;
}
my $rcs = _getHandler( $this, $web, $topic );
return $rcs->revisionDiff( $rev1, $rev2, $contextLines );
}
=begin TML
---++ ObjectMethod getRevisionInfo($web, $topic, $rev, $attachment) -> ( $date, $user, $rev, $comment )
Get revision info of a topic.
* =$web= Web name, optional, e.g. ='Main'=
* =$topic= Topic name, required, e.g. ='TokyoOffice'=
* =$rev= revision number. If 0, undef, or out-of-range, will get info about the most recent revision.
* =$attachment= attachment filename; undef for a topic
Return list with: ( last update date, last user id, =
| $date | in epochSec |
| $user | user *object* |
| $rev | the revision number |
| $comment | WHAT COMMENT? |
e.g. =( 1234561, 'phoeny', 5, 'no comment' )
NOTE NOTE NOTE if you are working within the Foswiki code DO NOT USE THIS
FUNCTION FOR GETTING REVISION INFO OF TOPICS - use
Foswiki::Meta::getRevisionInfo instead. This is essential to allow clean
transition to a topic object model later, and avoids the risk of confusion
coming from meta and Store revision information being out of step.
(it's OK to use it for attachments)
=cut
sub getRevisionInfo {
my ( $this, $web, $topic, $rev, $attachment ) = @_;
$rev ||= 0;
my $handler = _getHandler( $this, $web, $topic, $attachment );
my ( $rrev, $date, $user, $comment ) = $handler->getRevisionInfo($rev);
$rev = $rrev;
return ( $date, $user, $rev, $comment );
}
=begin TML
---++ StaticMethod dataEncode( $uncoded ) -> $coded
Encode meta-data fields, escaping out selected characters. The encoding
is chosen to avoid problems with parsing the attribute values, while
minimising the number of characters encoded so searches can still work
(fairly) sensibly.
The encoding has to be exported because Foswiki (and plugins) use
encoded field data in other places e.g. RDiff, mainly as a shorthand
for the properly parsed meta object. Some day we may be able to
eliminate that....
=cut
sub dataEncode {
my $datum = shift;
$datum =~ s/([%"\r\n{}])/'%'.sprintf('%02x',ord($1))/ge;
return $datum;
}
=begin TML
---++ StaticMethod dataDecode( $encoded ) -> $decoded
Decode escapes in a string that was encoded using dataEncode
The encoding has to be exported because Foswiki (and plugins) use
encoded field data in other places e.g. RDiff, mainly as a shorthand
for the properly parsed meta object. Some day we may be able to
eliminate that....
=cut
sub dataDecode {
my $datum = shift;
$datum =~ s/%([\da-f]{2})/chr(hex($1))/gei;
return $datum;
}
# STATIC Build a hash by parsing name=value comma separated pairs
# SMELL: duplication of Foswiki::Attrs, using a different
# system of escapes :-(
sub _readKeyValues {
my ( $args, $format ) = @_;
my $res = {};
$format =~ s/[^\d\.]+//g if $format;
my $oldStyle = ( !$format || $format =~ /[^\d.]/ || $format < 1.1 );
# Format of data is name='value' name1='value1' [...]
$args =~ s/\s*([^=]+)="([^"]*)"/_singleKey($1,$2,$res,$oldStyle)/ge;
return $res;
}
sub _singleKey {
my ( $key, $value, $res, $oldStyle ) = @_;
if ($oldStyle) {
# Old decoding retained for backward compatibility
# (this encoding is badly broken)
$value =~ s/%_N_%/\n/g;
$value =~ s/%_Q_%/\"/g;
$value =~ s/%_P_%/%/g;
}
else {
$value = dataDecode($value);
}
$res->{$key} = $value;
return '';
}
=begin TML
---++ ObjectMethod saveTopic( $user, $web, $topic, $text, $meta, $options )
* =$user= - user doing the saving (object)
* =$web= - web for topic
* =$topic= - topic to atach to
* =$text= - topic text
* =$meta= - topic meta-data
* =$options= - Ref to hash of options
=$options= may include:
| =dontlog= | don't log this change in twiki log |
| =hide= | if the attachment is to be hidden in normal topic view |
| =comment= | comment for save |
| =file= | Temporary file name to upload |
| =minor= | True if this is a minor change (used in log) |
| =savecmd= | Save command |
| =forcedate= | grr |
| =unlock= | |
Save a new revision of the topic, calling plugins handlers as appropriate.
=cut
sub saveTopic {
my ( $this, $user, $web, $topic, $text, $meta, $options ) = @_;
ASSERT($user) if DEBUG;
$web =~ s#\.#/#go;
$meta = _removeAutoAttachmentsFromMeta( $this, $meta );
my $users = $this->{session}->{users};
$options = {} unless defined($options);
if (
$user
&& !$this->{session}->security->checkAccessPermission(
'CHANGE', $user, undef, undef, $topic, $web
)
)
{
throw Foswiki::AccessControlException( 'CHANGE', $user, $web, $topic,
$this->{session}->security->getReason() );
}
my $plugins = $this->{session}->{plugins};
# Semantics inherited from Cairo. See
# TWiki:Codev.BugBeforeSaveHandlerBroken
if ( $plugins->haveHandlerFor('beforeSaveHandler') ) {
my $before = '';
if ($meta) {
# write the meta into the topic text. Nasty compatibility
# requirement.
$meta->text($text);
$text = $meta->getEmbeddedStoreForm();
$before = $meta->stringify();
}
$plugins->dispatch( 'beforeSaveHandler', $text, $topic, $web, $meta );
# remove meta again
my $after = new Foswiki::Meta( $this->{session}, $web, $topic, $text );
$text = $after->text();
# If there are no changes in the $meta object, take the meta
# from the text. Nasty compatibility requirement.
if ( !$meta || $meta->stringify() eq $before ) {
$meta = $after;
}
}
my $error;
try {
_noHandlersSave( $this, $user, $web, $topic, $text, $meta, $options );
}
catch Error::Simple with {
$error = shift;
};
# Semantics inherited from Cairo. See
# TWiki:Codev.BugBeforeSaveHandlerBroken
if ( $plugins->haveHandlerFor('afterSaveHandler') ) {
if ($meta) {
$meta->text($text);
$text = $meta->getEmbeddedStoreForm();
}
$plugins->dispatch( 'afterSaveHandler', $text, $topic, $web,
$error ? $error->{-text} : '', $meta );
}
throw $error if $error;
}
=begin TML
---++ ObjectMethod saveAttachment ($web, $topic, $attachment, $user, $opts )
* =$user= - user doing the saving
* =$web= - web for topic
* =$topic= - topic to atach to
* =$attachment= - name of the attachment
* =$opts= - Ref to hash of options
=$opts= may include:
| =dontlog= | don't log this change in twiki log |
| =comment= | comment for save |
| =hide= | if the attachment is to be hidden in normal topic view |
| =stream= | Stream of file to upload |
| =file= | Name of a file to use for the attachment data. ignored is stream is set. |
| =filepath= | Client path to file |
| =filesize= | Size of uploaded data |
| =filedate= | Date |
| =tmpFilename= | Pathname of the server file the stream is attached to. Required if stream is set. |
Saves a new revision of the attachment, invoking plugin handlers as
appropriate.
If file is not set, this is a properties-only save.
=cut
sub saveAttachment {
my ( $this, $web, $topic, $attachment, $user, $opts ) = @_;
ASSERT( defined($opts) ) if DEBUG;
my $action;
my $plugins = $this->{session}->{plugins};
my $attrs;
my $users = $this->{session}->{users};
$this->lockTopic( $user, $web, $topic );
try {
# update topic
my ( $meta, $text ) = $this->readTopic( undef, $web, $topic, undef );
if (
$user