From 2ffb2dd8fa48dfef8f7f148c247dff39a7a0afbb Mon Sep 17 00:00:00 2001 From: Alex Schroeder Date: Wed, 16 Dec 2015 15:50:35 +0100 Subject: [PATCH] Replace: no pagination The old code would only call the the function provided if pagination indicated that it was OK to do so. Thus, only the first ten pages got replaced. This has been fixed and tests have been added. --- t/search.t | 13 ++++++++++++- wiki.pl | 8 ++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/t/search.t b/t/search.t index 52ef6af4..772dbabd 100644 --- a/t/search.t +++ b/t/search.t @@ -15,7 +15,7 @@ require 't/test.pl'; package OddMuse; -use Test::More tests => 72; +use Test::More tests => 89; use utf8; # tests contain UTF-8 characters and it matters add_module('mac.pl'); @@ -116,6 +116,17 @@ xpath_test($page, '//a[@class="more"][@href="http://localhost/wiki.pl?search=Som $page = get_page('search=Something preview=1 offset=10 num=10 replace=Other pwd=foo'); test_page($page, map { "Page_$_" } ('K' .. 'M')); +# Now do the replacement + +$page = get_page('search=Something replace=Other pwd=foo'); +test_page($page, 'Replaced: Something → Other', '13 pages found', + map { "Page_$_" } ('A' .. 'M')); + +# Verify that the change has been made + +test_page(get_page('search=Other'), 'Search for: Other', '13 pages found'); + + # Replace with backreferences, where the replacement pattern is no longer found. # Take 'fuuz and barz.' and replace ([a-z]+)z with x$1 results in 'xfuu and xbar.' test_page(get_page('"search=([a-z]%2b)z" replace=x%241 pwd=foo'), '1 pages found'); diff --git a/wiki.pl b/wiki.pl index 37539271..5508d4e1 100755 --- a/wiki.pl +++ b/wiki.pl @@ -3580,7 +3580,7 @@ sub SearchExtract { sub ReplaceAndSave { my ($from, $to) = @_; RequestLockOrError(); # fatal - my @result = Replace($from, $to, sub { + my @result = Replace($from, $to, 1, sub { my ($id, $new) = @_; Save($id, $new, $from . ' → ' . $to, 1, ($Page{host} ne $q->remote_addr())); }); @@ -3590,7 +3590,7 @@ sub ReplaceAndSave { sub ReplaceAndDiff { my ($from, $to) = @_; - my @found = Replace($from, $to, sub { + my @found = Replace($from, $to, 0, sub { my ($id, $new) = @_; print $q->h2(GetPageLink($id)), $q->div({-class=>'diff'}, ImproveDiff(DoDiff($Page{text}, $new))); }); @@ -3606,7 +3606,7 @@ sub ReplaceAndDiff { } sub Replace { - my ($from, $to, $func) = @_; # $func takes $id and $new text + my ($from, $to, $all, $func) = @_; # $func takes $id and $new text my $lang = GetParam('lang', ''); my $num = GetParam('num', 10); my $offset = GetParam('offset', 0); @@ -3626,7 +3626,7 @@ sub Replace { }; if (s/$from/$replacement->()/egi) { # allows use of backreferences push (@result, $id); - $func->($id, $_) if @result > $offset and @result <= $offset + $num; + $func->($id, $_) if $all or @result > $offset and @result <= $offset + $num; } } return @result;