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.
This commit is contained in:
Alex Schroeder
2015-12-16 15:50:35 +01:00
parent 864c6f0f61
commit 2ffb2dd8fa
2 changed files with 16 additions and 5 deletions

View File

@@ -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');

View File

@@ -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;