/
Rename.pm
1419 lines (1217 loc) · 46.8 KB
/
Rename.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::UI::Rename
UI functions for renaming.
=cut
package Foswiki::UI::Rename;
use strict;
use Assert;
use Error qw(:try);
use Foswiki::UI ();
use Foswiki::Render ();
our $MARKER = "\02\03";
=begin TML
---++ StaticMethod rename( $session )
=rename= command handler.
This method is designed to be
invoked via the =UI::run= method.
Rename the given topic. Details of the new topic name are passed in CGI
parameters:
| =skin= | skin(s) to use |
| =newweb= | new web name |
| =newtopic= | new topic name |
| =breaklock= | |
| =attachment= | |
| =confirm= | if defined, requires a second level of confirmation |
| =currentwebonly= | if defined, searches current web only for links to this topic |
| =nonwikiword= | if defined, a non-wikiword is acceptable for the new topic name |
| =redirectto= | If the rename process is successful, rename will redirect to this topic or URL. The parameter value can be a =TopicName=, a =Web.TopicName=, or a URL.%BR% __Note:__ Redirect to a URL only works if it is enabled in =configure= (Miscellaneous ={AllowRedirectUrl}=). |
=cut
# This function is entered twice during an interaction renaming session. The
# first time is when the parameters for the rename are being gathered
# referring topics etc) and in this case, it will terminate at either
# newWebScreen or newTopicOrAttachmentScreen. The second times is when the
# rename is proceeding, and/or all the appropriate parameters have been
# passed by the caller. In this case the rename proceeds.
sub rename {
my $session = shift;
my $oldWeb = $session->{webName};
my $oldTopic = $session->{topicName};
my $query = $session->{request};
my $action = $session->{cgiQuery}->param('action') || '';
Foswiki::UI::checkWebExists( $session, $oldWeb, 'rename' );
my $new_url;
if ( $action eq 'renameweb' ) {
$new_url = _renameWeb( $session, $oldWeb );
}
else {
$new_url = _renameTopicOrAttachment( $session, $oldWeb, $oldTopic );
}
$session->redirect( $new_url, undef, 1 ) if $new_url;
}
# Rename a topic
sub _renameTopicOrAttachment {
my ( $session, $oldWeb, $oldTopic ) = @_;
my $query = $session->{cgiQuery};
my $newTopic = $query->param('newtopic') || '';
my $newWeb = $query->param('newweb') || '';
# Validate the new web name
$newWeb = Foswiki::Sandbox::untaint(
$newWeb,
sub {
my ($web) = @_;
unless ( !$web || Foswiki::isValidWebName( $web, 1 ) ) {
throw Foswiki::OopsException(
'attention',
def => 'invalid_web_name',
params => [$web]
);
}
return $web;
}
);
my $confirm = $query->param('confirm');
unless ( $session->topicExists( $oldWeb, $oldTopic ) ) {
# Item3270: check for the same name starting with a lower case letter.
unless ( $session->topicExists( $oldWeb, lcfirst($oldTopic) ) ) {
throw Foswiki::OopsException(
'accessdenied',
status => 403,
def => 'no_such_topic_rename',
web => $oldWeb,
topic => $oldTopic
);
}
# Untaint is required is use locale is in force
$oldTopic = Foswiki::Sandbox::untaintUnchecked( lcfirst($oldTopic) );
}
if ($newTopic) {
# Purify the new topic name
$newTopic = _safeTopicName($newTopic);
# Validate
$newTopic = Foswiki::Sandbox::untaint(
$newTopic,
sub {
my ( $topic, $nonww ) = @_;
if ( !Foswiki::isValidTopicName( $topic, $nonww ) ) {
throw Foswiki::OopsException(
'attention',
web => $oldWeb,
topic => $oldTopic,
def => 'not_wikiword',
params => [$topic]
);
}
return $topic;
},
Foswiki::isTrue( $query->param('nonwikiword') )
);
}
my $attachment = $query->param('attachment');
my $newAttachment = $query->param('newattachment');
my $old = Foswiki::Meta->load( $session, $oldWeb, $oldTopic );
if ($attachment) {
# Does old attachment exist?
# Attachment exists, validated
$attachment = Foswiki::Sandbox::untaint(
$attachment,
sub {
my ($att) = @_;
if ( !$old->hasAttachment($att) ) {
my $tmplname = $query->param('template') || '';
throw Foswiki::OopsException(
'attention',
web => $oldWeb,
topic => $oldTopic,
def => ( $tmplname eq 'deleteattachment' )
? 'delete_err'
: 'move_err',
params => [
$newWeb,
$newTopic,
$attachment,
$session->i18n->maketext(
'Attachment does not exist')
]
);
}
return $att;
}
);
# Validate the new attachment name, if one was provided
if ($newAttachment) {
$newAttachment = Foswiki::Sandbox::untaint(
$newAttachment,
sub {
my ($att) = @_;
return Foswiki::Sandbox::sanitizeAttachmentName($att);
}
);
}
if ( $newWeb && $newTopic && $newAttachment ) {
Foswiki::UI::checkTopicExists( $session, $newWeb, $newTopic,
'rename' );
my $new = Foswiki::Meta->load( $session, $newWeb, $newTopic );
# does new attachment already exist?
if ( $new->hasAttachment($newAttachment) ) {
throw Foswiki::OopsException(
'attention',
def => 'move_err',
web => $oldWeb,
topic => $oldTopic,
params => [
$newWeb,
$newTopic,
$newAttachment,
$session->i18n->maketext(
'Attachment already exists in new topic')
]
);
}
} # else fall through to new topic screen
}
elsif ($newTopic) {
( $newWeb, $newTopic ) =
$session->normalizeWebTopicName( $newWeb, $newTopic );
Foswiki::UI::checkWebExists( $session, $newWeb, $newTopic, 'rename' );
if ( $session->topicExists( $newWeb, $newTopic ) ) {
throw Foswiki::OopsException(
'attention',
def => 'rename_topic_exists',
web => $oldWeb,
topic => $oldTopic,
params => [ $newWeb, $newTopic ]
);
}
}
Foswiki::UI::checkAccess( $session, 'RENAME', $old );
my $new = Foswiki::Meta->new(
$session,
$newWeb || $old->web,
$newTopic || $old->topic
);
# Has user selected new name yet?
if ( !$newTopic || ($attachment && !$newAttachment) || $confirm ) {
$newAttachment ||= $attachment;
# Must be able to view the source to rename it
Foswiki::UI::checkAccess( $session, 'VIEW', $old );
_newTopicOrAttachmentScreen(
$session, $old, $new, $attachment, $newAttachment, $confirm );
return undef;
}
return undef if ($query && $query->method()
&& uc($query->method()) ne 'POST');
# Update references in referring pages - not applicable to attachments.
my $refs;
unless ($attachment) {
$refs =
_getReferringTopicsListFromURL( $session, $oldWeb, $oldTopic, $newWeb,
$newTopic );
}
_moveTopicOrAttachment(
$session, $old, $new, $attachment, $newAttachment, $refs );
my $new_url;
if ( $newWeb eq $Foswiki::cfg{TrashWebName}
&& $oldWeb ne $Foswiki::cfg{TrashWebName} )
{
# deleting something
if ($attachment) {
# go back to old topic after deleting an attachment
$new_url =
$session->getScriptUrl( 0, 'view', $old->web, $old->topic );
}
else {
# redirect to parent topic, if set
my $meta = Foswiki::Meta->load( $session, $new->web, $new->topic );
my $parent = $meta->get('TOPICPARENT');
my ( $parentWeb, $parentTopic );
if ( $parent && defined $parent->{name} ) {
( $parentWeb, $parentTopic ) =
$session->normalizeWebTopicName( '', $parent->{name} );
}
if ( $parentTopic
&& !( $parentWeb eq $oldTopic && $parentTopic eq $oldTopic )
&& $session->topicExists( $parentWeb, $parentTopic ) )
{
$new_url =
$session->getScriptUrl( 0, 'view', $parentWeb, $parentTopic );
}
else {
# No parent topic, redirect to home topic
$new_url =
$session->getScriptUrl( 0, 'view', $oldWeb,
$Foswiki::cfg{HomeTopicName} );
}
}
}
else {
# redirect to new topic
$new_url = $session->getScriptUrl( 0, 'view', $newWeb, $newTopic );
}
return $new_url;
}
sub _safeTopicName {
my ($topic) = @_;
$topic =~ s/\s//go;
$topic = ucfirst $topic; # Item3270
$topic =~ s![./]!_!g;
$topic =~ s/($Foswiki::cfg{NameFilter})//go;
return $topic;
}
#| =skin= | skin(s) to use |
#| =newsubweb= | new web name |
#| =newparentweb= | new parent web name |
#| =confirm= | if defined, requires a second level of confirmation. Currently accepted values are "getlock", "continue", and "cancel" |
sub _renameWeb {
my ( $session, $oldWeb ) = @_;
my $oldWebObject = Foswiki::Meta->new( $session, $oldWeb );
my $query = $session->{request};
my $cUID = $session->{user};
# If the user is not allowed to rename anything in the current
# web - stop here
Foswiki::UI::checkAccess( $session, 'RENAME', $oldWebObject);
my $newParentWeb = $query->param('newparentweb') || '';
# Validate
if ( $newParentWeb ne "" ) {
$newParentWeb = Foswiki::Sandbox::untaint(
$newParentWeb,
sub {
my $web = shift;
return $web if Foswiki::isValidWebName( $web, 1 );
throw Foswiki::OopsException(
'attention',
def => 'invalid_web_name',
params => [$web]
);
}
);
}
my $newSubWeb = $query->param('newsubweb') || '';
# Validate
if ( $newSubWeb ne "" ) {
$newSubWeb = Foswiki::Sandbox::untaint(
$newSubWeb,
sub {
my $web = shift;
return $web if Foswiki::isValidWebName( $web, 1 );
throw Foswiki::OopsException(
'attention',
def => 'invalid_web_name',
params => [$web]
);
}
);
}
my $newWeb;
if ($newSubWeb) {
if ($newParentWeb) {
$newWeb = $newParentWeb . '/' . $newSubWeb;
}
else {
$newWeb = $newSubWeb;
}
}
# Determine the parent of the 'from' web
my @tmp = split( /[\/\.]/, $oldWeb );
pop(@tmp);
my $oldParentWeb = join( '/', @tmp );
# If the user is not allowed to rename anything in the parent web
# - stop here
# This also ensures we check root webs for ALLOWROOTRENAME and
# DENYROOTRENAME
my $oldParentWebObject = new Foswiki::Meta(
$session, $oldParentWeb || undef );
Foswiki::UI::checkAccess( $session, 'RENAME', $oldParentWebObject );
# If old web is a root web then also stop if ALLOW/DENYROOTCHANGE
# prevents access
if ( !$oldParentWeb ) {
Foswiki::UI::checkAccess( $session, 'CHANGE', $oldParentWebObject );
}
my $newTopic;
my $lockFailure = '';
my $confirm = $query->param('confirm') || '';
Foswiki::UI::checkWebExists( $session, $oldWeb,
$Foswiki::cfg{WebPrefsTopicName}, 'rename' );
if ($newWeb) {
if ($newParentWeb) {
Foswiki::UI::checkWebExists( $session, $newParentWeb,
$Foswiki::cfg{WebPrefsTopicName}, 'rename' );
}
if ( $session->webExists($newWeb) ) {
throw Foswiki::OopsException(
'attention',
def => 'rename_web_exists',
web => $oldWeb,
topic => $Foswiki::cfg{WebPrefsTopicName},
params => [ $newWeb, $Foswiki::cfg{WebPrefsTopicName} ]
);
}
# Check if we have change permission in the new parent
my $newParentWebObject = new Foswiki::Meta( $session, $newParentWeb );
Foswiki::UI::checkAccess( $session, 'CHANGE', $newParentWebObject );
}
if ( !$newWeb || $confirm ) {
my %refs;
my $info = {
totalReferralAccess => 1,
totalWebAccess => 1,
modifyingLockedTopics => 0,
movingLockedTopics => 0
};
# get a topic list for all the topics referring to this web,
# and build up a hash containing permissions and lock info.
my $refs0 = _getReferringTopics( $session, $oldWebObject, 0 );
my $refs1 = _getReferringTopics( $session, $oldWebObject, 1 );
%refs = ( %$refs0, %$refs1 );
$info->{referring}{refs0} = $refs0;
$info->{referring}{refs1} = $refs1;
my $lease_ref;
foreach my $ref ( keys %refs ) {
if ( defined($ref) && $ref ne "" ) {
my (@path) = split( /[.\/]/, $ref );
my $webTopic = pop(@path);
my $webIter = join( '/', @path );
my $topicObject =
Foswiki::Meta->new( $session, $webIter, $webTopic );
if ( $confirm eq 'getlock' ) {
$topicObject->setLease( $Foswiki::cfg{LeaseLength} );
$lease_ref = $topicObject->getLease();
}
elsif ( $confirm eq 'cancel' ) {
$lease_ref = $topicObject->getLease();
if ( $lease_ref->{user} eq $cUID ) {
$topicObject->clearLease();
}
}
my $wit = $webIter . '/' . $webTopic;
$info->{modify}{$wit}{leaseuser} = $lease_ref->{user};
$info->{modify}{$wit}{leasetime} = $lease_ref->{taken};
$info->{modifyingLockedTopics}++
if ( defined( $info->{modify}{$ref}{leaseuser} )
&& $info->{modify}{$ref}{leaseuser} ne $cUID );
$info->{modify}{$ref}{summary} = $refs{$ref};
$info->{modify}{$ref}{access} =
$topicObject->haveAccess( 'CHANGE' );
if ( !$info->{modify}{$ref}{access} ) {
$info->{modify}{$ref}{accessReason} =
$Foswiki::Meta::reason;
}
$info->{totalReferralAccess} = 0
unless $info->{modify}{$ref}{access};
}
}
# Lease topics and build
# up a hash containing permissions and lock info.
my $it = $oldWebObject->eachWeb();
_leaseContents( $session, $info, $oldWebObject->web, $confirm );
while ( $it->hasNext() ) {
my $subweb = $it->next();
require Foswiki::WebFilter;
next unless $Foswiki::WebFilter::public->ok( $session, $subweb );
_leaseContents( $session, $info,
$oldWebObject->web . '/' . $subweb, $confirm );
}
if ( !$info->{totalReferralAccess}
|| !$info->{totalWebAccess}
|| $info->{movingLockedTopics}
|| $info->{modifyingLockedTopics} )
{
# check if the user can rename all the topics in this web.
push(
@{ $info->{movedenied} },
grep { !$info->{move}{$_}{access} }
sort keys %{ $info->{move} }
);
# check if there are any locked topics in this web or
# its subwebs.
push(
@{ $info->{movelocked} },
grep {
defined( $info->{move}{$_}{leaseuser} )
&& $info->{move}{$_}{leaseuser} ne $cUID
}
sort keys %{ $info->{move} }
);
# Next, build up a list of all the referrers which the
# user doesn't have permission to change.
push(
@{ $info->{modifydenied} },
grep { !$info->{modify}{$_}{access} }
sort keys %{ $info->{modify} }
);
# Next, build up a list of all the referrers which are
# currently locked.
push(
@{ $info->{modifylocked} },
grep {
defined( $info->{modify}{$_}{leaseuser} )
&& $info->{modify}{$_}{leaseuser} ne $cUID
}
sort keys %{ $info->{modify} }
);
unless ($confirm) {
my $nocontinue = '';
if ( @{ $info->{movedenied} }
|| @{ $info->{movelocked} } )
{
$nocontinue = 'style="display:none;"';
}
my $mvd = join( ' ', @{ $info->{movedenied} } )
|| ( $session->i18n->maketext('(none)') );
$mvd = substr( $mvd, 0, 300 ) . '... (more)'
if ( length($mvd) > 300 );
my $mvl = join( ' ', @{ $info->{movelocked} } )
|| ( $session->i18n->maketext('(none)') );
$mvl = substr( $mvl, 0, 300 ) . '... (more)'
if ( length($mvl) > 300 );
my $mdd = join( ' ', @{ $info->{modifydenied} } )
|| ( $session->i18n->maketext('(none)') );
$mdd = substr( $mdd, 0, 300 ) . '... (more)'
if ( length($mdd) > 300 );
my $mdl = join( ' ', @{ $info->{modifylocked} } )
|| ( $session->i18n->maketext('(none)') );
$mdl = substr( $mdl, 0, 300 ) . '... (more)'
if ( length($mdl) > 300 );
throw Foswiki::OopsException(
'attention',
web => $oldWeb,
topic => '',
def => 'rename_web_prerequisites',
params => [ $mvd, $mvl, $mdd, $mdl, $nocontinue ]
);
}
}
if ( $confirm eq 'cancel' ) {
# redirect to original web
my $viewURL =
$session->getScriptUrl( 0, 'view', $oldWeb,
$Foswiki::cfg{HomeTopicName} );
$session->redirect($viewURL);
}
elsif (
$confirm ne 'getlock'
|| ( $confirm eq 'getlock'
&& $info->{modifyingLockedTopics}
&& $info->{movingLockedTopics} )
)
{
# Has user selected new name yet?
_newWebScreen( $session, $oldWebObject, $newWeb, $confirm, $info );
return;
}
}
return if ($query && $query->method() && uc($query->method()) ne 'POST');
my $newWebObject = Foswiki::Meta->new( $session, $newWeb );
Foswiki::UI::checkAccess( $session, 'CHANGE', $oldWebObject );
Foswiki::UI::checkAccess( $session, 'CHANGE', $newWebObject );
my $refs = _getReferringTopicsListFromURL($session);
# update referrers. We need to do this before moving,
# because there might be topics inside the newWeb which need updating.
_updateReferringTopics( $session, $refs, \&_replaceWebReferences,
{ oldWeb => $oldWeb, newWeb => $newWeb } );
# Now, we can move the web.
try {
$oldWebObject->move($newWebObject);
}
catch Error with {
my $e = shift;
throw Foswiki::OopsException(
'attention',
web => $oldWeb,
topic => '',
def => 'rename_web_err',
params => [ $e->{-text}, $newWeb ]
);
}
# now remove leases on all topics inside $newWeb.
my $nwom = Foswiki::Meta->new( $session, $newWeb );
my $it = $nwom->eachWeb();
_releaseContents( $session, $newWeb );
while ( $it->hasNext() ) {
my $subweb = $it->next();
require Foswiki::WebFilter;
next unless $Foswiki::WebFilter::public->ok( $session, $subweb );
_releaseContents( $session, "$newWeb/$subweb" );
}
# also remove lease on all referring topics
foreach my $ref (@$refs) {
my @path = split( /[.\/]/, $ref );
my $webTopic = pop(@path);
my $webIter = join( '/', @path );
my $topicObject = Foswiki::Meta->new( $session, $webIter, $webTopic );
$topicObject->clearLease();
}
my $new_url = '';
if ( $newWeb =~ /^$Foswiki::cfg{TrashWebName}\b/
&& $oldWeb !~ /^$Foswiki::cfg{TrashWebName}\b/ )
{
# redirect to parent
if ($oldParentWeb) {
$new_url =
$session->getScriptUrl( 0, 'view', $oldParentWeb,
$Foswiki::cfg{HomeTopicName} );
}
else {
$new_url = $session->getScriptUrl(
0, 'view',
$Foswiki::cfg{UsersWebName},
$Foswiki::cfg{HomeTopicName}
);
}
}
else {
# redirect to new web
$new_url =
$session->getScriptUrl( 0, 'view', $newWeb,
$Foswiki::cfg{HomeTopicName} );
}
return $new_url;
}
sub _leaseContents {
my ( $session, $info, $web, $confirm ) = @_;
my $webObject = Foswiki::Meta->new( $session, $web );
my $it = $webObject->eachTopic();
while ( $it->hasNext() ) {
my $topic = $it->next();
my $lease_ref;
my $topicObject = Foswiki::Meta->new( $session, $web, $topic );
if ( $confirm eq 'getlock' ) {
$topicObject->setLease( $Foswiki::cfg{LeaseLength} );
$lease_ref = $topicObject->getLease();
}
elsif ( $confirm eq 'cancel' ) {
$lease_ref = $topicObject->getLease();
if ( $lease_ref->{user} eq $session->{user} ) {
$topicObject->clearLease();
}
}
my $wit = $web . '/' . $topic;
$info->{move}{$wit}{leaseuser} = $lease_ref->{user};
$info->{move}{$wit}{leasetime} = $lease_ref->{taken};
$info->{movingLockedTopics}++
if ( defined( $info->{move}{$wit}{leaseuser} )
&& $info->{move}{$wit}{leaseuser} ne $session->{user} );
$info->{move}{$wit}{access} = $topicObject->haveAccess('RENAME');
$info->{move}{$wit}{accessReason} = $Foswiki::Meta::reason;
$info->{totalWebAccess} =
( $info->{totalWebAccess} & $info->{move}{$wit}{access} );
}
}
sub _releaseContents {
my ( $session, $web ) = @_;
my $webObject = Foswiki::Meta->new( $session, $web );
my $it = $webObject->eachTopic();
while ( $it->hasNext() ) {
my $topic = $it->next();
my $topicObject = Foswiki::Meta->new( $session, $web, $it->next() );
$topicObject->clearLease();
}
}
# Move the given topic, or an attachment in the topic, correcting refs to the topic in the topic itself, and
# in the list of topics (specified as web.topic pairs) in the \@refs array.
#
# * =$session= - reference to session object
# * =$from= - old topic
# * =$to= - new topic
# * =$attachment= - name of the attachment to move (from oldtopic to newtopic) (undef to move the topic) - must be untaineted
# * =\@refs= - array of webg.topics that must have refs to this topic converted
# Will throw Foswiki::OopsException on an error.
sub _moveTopicOrAttachment {
my ( $session, $from, $to, $attachment, $toattachment, $refs ) = @_;
Foswiki::UI::checkAccess( $session, 'CHANGE', $from );
Foswiki::UI::checkAccess( $session, 'CHANGE', $to );
if ($attachment) {
try {
$from->moveAttachment(
$attachment, $to, new_name => $toattachment );
}
catch Error::Simple with {
throw Foswiki::OopsException(
'attention',
web => $from->web,
topic => $from->topic,
def => 'move_err',
params => [ $to->web, $to->topic, $attachment, shift->{-text} ]
);
};
}
else {
try {
$from->move($to);
}
catch Error with {
my $e = shift;
throw Foswiki::OopsException(
'attention',
web => $from->web,
topic => $from->topic,
def => 'rename_err',
params => [ $e->{-text}, $to->web, $to->topic ]
);
};
if ( $from->web ne $to->web ) {
# If the web changed, replace local refs to the topics
# in $from->web with full $from->web.topic references so that
# they still work.
_replaceWebInternalReferences( $session, $from, $to );
}
# Now let's replace all self-referential links:
require Foswiki::Render;
my $text = $to->text();
my $options = {
oldWeb => $from->web,
oldTopic => $from->topic,
newWeb => $to->web,
newTopic => $to->topic,
inWeb => $to->web,
fullPaths => 0,
};
$text =
$session->renderer->forEachLine( $text, \&_replaceTopicReferences,
$options );
$to->text($text);
$to->put(
'TOPICMOVED',
{
from => $from->web . '.' . $from->topic,
to => $to->web . '.' . $to->topic,
date => time(),
by => $session->{user},
}
);
$to->save( minor => 1, comment => 'rename' );
# update referrers - but _not_ including the moved topic
_updateReferringTopics( $session, $refs, \&_replaceTopicReferences,
$options );
}
}
# _replaceTopicReferences( $text, \%options ) -> $text
#
# Callback designed for use with forEachLine, to replace topic references.
# \%options contains:
# * =oldWeb= => Web of reference to replace
# * =oldTopic= => Topic of reference to replace
# * =newWeb= => Web of new reference
# * =newTopic= => Topic of new reference
# * =inWeb= => the web which the text we are presently processing resides in
# * =fullPaths= => optional, if set forces all links to full web.topic form
sub _replaceTopicReferences {
my ( $text, $args ) = @_;
ASSERT( defined $args->{oldWeb} ) if DEBUG;
ASSERT( defined $args->{oldTopic} ) if DEBUG;
ASSERT( defined $args->{newWeb} ) if DEBUG;
ASSERT( defined $args->{newTopic} ) if DEBUG;
ASSERT( defined $args->{inWeb} ) if DEBUG;
# Do the traditional Foswiki topic references first
my $oldTopic = $args->{oldTopic};
my $newTopic = $args->{newTopic};
my $repl = $newTopic;
my $newWeb = $args->{newWeb};
my $oldWeb = $args->{oldWeb};
my $sameWeb = ( $oldWeb eq $newWeb );
if ( $args->{inWeb} ne $newWeb || $args->{fullPaths} ) {
$repl = $newWeb . '.' . $repl;
}
my $re = Foswiki::Render::getReferenceRE( $oldWeb, $oldTopic );
$text =~ s/($re)/_doReplace($1, $newWeb, $repl)/ge;
# Now URL form
$repl = "/$newWeb/$newTopic";
$re = Foswiki::Render::getReferenceRE( $oldWeb, $oldTopic, url => 1 );
$text =~ s/$re/$repl/g;
return $text;
}
sub _doReplace {
my ( $match, $web, $repl ) = @_;
# TWikibug:Item4661 If there is a web defined in the match, then
# make sure there's a web defined in the replacement.
if ( $match =~ /\./ && $repl !~ /\./ ) {
$repl = $web . '.' . $repl;
}
return $repl;
}
# _replaceWebReferences( $text, \%options ) -> $text
#
# Callback designed for use with forEachLine, to replace text references
# to a web.
# \%options contains:
# * =oldWeb= => Web of reference to replace
# * =newWeb= => Web of new reference
sub _replaceWebReferences {
my ( $text, $args ) = @_;
ASSERT( defined $args->{oldWeb} ) if DEBUG;
ASSERT( defined $args->{newWeb} ) if DEBUG;
ASSERT( $text !~ /$MARKER/ ) if DEBUG;
my $newWeb = $args->{newWeb};
my $oldWeb = $args->{oldWeb};
return $text if $oldWeb eq $newWeb;
# Replace stand-alone web references with $MARKER, to
# prevent matching $newWeb as a URL fragment in the second RE
my $re = Foswiki::Render::getReferenceRE( $oldWeb, undef );
$text =~ s/$re/$MARKER$1/g;
# Now do URLs.
$re = Foswiki::Render::getReferenceRE( $oldWeb, undef, url => 1 );
$text =~ s#$re#/$newWeb/#g;
# Finally do the marker.
$text =~ s/$MARKER/$newWeb/g;
return $text;
}
# _replaceWebInternalReferences( $from, $to )
#
# Change within-web wikiwords that refer to the topic $from so they
# point to $to. $from and $to are Foswiki::Meta.
sub _replaceWebInternalReferences {
my ( $session, $from, $to ) = @_;
my $renderer = $session->renderer;
my $webObject = Foswiki::Meta->new( $session, $from->web());
my $it = $webObject->eachTopic();
my $oldTopic = $from->topic();
my $options = {
# exclude this topic from the list
topics => [ grep { !/^$oldTopic$/ } $it->all() ],
inWeb => $from->web,
inTopic => $from->topic,
oldWeb => $from->web,
#oldTopic => will be filled in by _replaceInternalRefs
newWeb => $from->web,
#newTopic => will be filled in by _replaceInternalRefs
};
my $text = $to->text();
# Replace references that were internal to the source web; they are
# now inter-web
$text = $renderer->forEachLine( $text, \&_replaceInternalRefs, $options );
$to->forEachSelectedValue( qw/^(FIELD|TOPICPARENT)$/, undef,
\&_replaceInternalRefs, $options );
$to->forEachSelectedValue( qw/^TOPICMOVED$/, qw/^by$/,
\&_replaceInternalRefs, $options );
$to->forEachSelectedValue( qw/^FILEATTACHMENT$/, qw/^user$/,
\&_replaceInternalRefs, $options );
# Ok, let's look for links to topics in the
# new web and remove their web qualifiers
$webObject = Foswiki::Meta->new( $session, $to->web());
$it = $webObject->eachTopic();
$options = {
# exclude this topic from the list
topics => [ $it->all() ],
fullPaths => 0,
inWeb => $to->web,
inTopic => $to->topic,
oldWeb => $to->web,
#oldTopic => will be filled in by _replaceInternalRefs
newWeb => $to->web,
#newTopic => will be filled in by _replaceInternalRefs
};
$text = $renderer->forEachLine( $text, \&_replaceInternalRefs, $options );
$to->text($text);
$to->forEachSelectedValue( qw/^(FIELD|TOPICPARENT)$/, undef,
\&_replaceInternalRefs, $options );
$to->forEachSelectedValue( qw/^TOPICMOVED$/, qw/^by$/,
\&_replaceInternalRefs, $options );
$to->forEachSelectedValue( qw/^FILEATTACHMENT$/, qw/^user$/,
\&_replaceInternalRefs, $options );
}
# callback used by _replaceWebInternalReferences to correct references
# to topics that were in the same web previously, but are now in a
# different web because the topic has moved. $args should be populated
# with oldWeb and newWeb already, so just need to add each topic as we
# process it.
sub _replaceInternalRefs {
my ( $text, $args ) = @_;
foreach my $topic ( @{ $args->{topics} } ) {
$args->{fullPaths} = ( $topic ne $args->{inTopic} )
if ( !defined( $args->{fullPaths} ) );
$args->{oldTopic} = $topic;
$args->{newTopic} = $topic;
$text = _replaceTopicReferences( $text, $args );
}
return $text;
}
# Display screen so user can decide on new web, topic, attachment names.
sub _newTopicOrAttachmentScreen {
my ( $session, $from, $to, $attachment, $toattachment,
$confirm, $doAllowNonWikiWord ) = @_;
my $query = $session->{cgiQuery};
my $tmplname = $query->param('template') || '';
my $tmpl = '';
my $currentWebOnly = $query->param('currentwebonly') || '';
my $nonWikiWordFlag = '';
$nonWikiWordFlag = 'checked="checked"' if ($doAllowNonWikiWord);
if ($attachment) {
$tmpl =