-
Notifications
You must be signed in to change notification settings - Fork 8
/
replaceblock
executable file
·141 lines (115 loc) · 3.7 KB
/
replaceblock
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
#!/usr/bin/perl
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2004 by Open Source Development Network. See README
# and COPYING for more information, or see http://slashcode.com/.
# $Id$
use strict;
use File::Basename;
use Getopt::Std;
use Slash::DB;
(my $VERSION) = ' $Revision$ ' =~ /\$Revision:\s+([^\s]+)/;
my $PROGNAME = basename($0);
my(@blocktypes) = ('blocks', 'portald');
my(%opts);
usage('Options used incorrectly') unless getopts('hDvu:TM:b:m:', \%opts);
usage() if ($opts{'h'} || !keys %opts);
version() if $opts{'v'};
$opts{'u'} ||= 'slash';
$"='|';
my $regexp = '^(' . "@blocktypes" . ')$';
usage() if $#ARGV < 1 || $opts{h} || ($opts{m} && $opts{M}) ||
($opts{M} && $opts{M} !~ /$regexp/);
my($search, $replace) = @ARGV;
# Catch common errors.
$opts{r}=~ s/^\*//;
$search =~ s/^\*//;
my($dbName, $blockName, $debug, $cond, $count, $replaced) =
($opts{u}, $opts{b}, $opts{D}, '', 0);
my($m_regexp, $m_type) = @opts{qw[m M]};
my $dbobject = new Slash::DB($dbName) ||
die "%%%%%% Can't open database on Virtual User $dbName.\n";
$cond = 'bid=' . $dbobject->{dbh}->quote($blockName) if $blockName;
$cond .= ' type=' . $dbobject->{dbh}->quote($opts{t}) if $opts{t};
if ($cond !~ /^bid/ && !$opts{T}) {
print (!$m_type ? <<EOGT:<<EOTT);
%%%%%% You are about to perform a global search and replace on your blocks
%%%%%% table.
EOGT
%%%%%% You are about to perform a global search and replace on all blocks of
%%%%%% type $m_type.
EOTT
print <<EOT;
%%%%%%
%%%%%% Please confirm that this is what you wish by typing 'YES' at the prompt
%%%%%% below:
EOT
print "> ";
my $response = <STDIN>;
die "%%%%%% Cancelled by user.\n" if $response ne 'YES';
}
print "%%%%%% Searching for: $search\n%%%%%% Replacing with: $replace\n"
if $debug;
my $sth = $dbobject->sqlSelectMany('bid,block', 'blocks', $cond);
while (my($bid, $block) = $sth->fetchrow_array()) {
print "%%%%%% Processing '$bid'\n" if !$blockName;
$count++;
if ($m_regexp) {
eval { next if $bid !~ /$m_regexp/ } ;
die "Invalid block regexp '$m_regexp'!\n" if $@;
print "%%%%%% -- Matched\n" if $debug;
}
my $replace_cond = 'bid='.$dbobject->{dbh}->quote($bid);
print <<EOT if $debug;
%%%%%% Current contents for $block
$block
EOT
my $rc;
eval { $rc = $block =~ s/$search/$replace/g; };
die <<EOT if $@;
Error in regular expression: SEARCH='$search'; REPLACE='$replace'\n
EOT
unless ($rc) {
print "%%%%%% -- Search string not found.\n";
next;
}
$replaced++;
print "%%%%%% Resulting contents\n$block\n" if $debug && $rc;
if (! $opts{T}) {
$dbobject->sqlUpdate('blocks', { block => $block }, $replace_cond)
|| print <<EOT;
%%%%%% -- Error occured while writing to database: $dbobject->{dbh}->errstr
EOT
}
print "%%%%%% -- Testing, replace not performed.\n" if $opts{T};
}
print "%%%%%% $count blocks processed, $replaced blocks substituted.\n";
0;
sub usage {
local $" = "\n\t\t";
print <<EOT;
$PROGNAME: [-b <block>|-m <regexp>] [-M <type>] [-u <DBVirtualUser>] [-Dhv] <s> <r>
-b Block on which we perform operation.
-m Perform operation on blockname matching <regexp> [do not use with -M]
-M Specify Block Type [do not use with -m]
-u Specify Database Virtual User (default: slash) [do not use with -M].
-D Display debugging information.
-T Test mode (does not actually perform replace)
-h Shows this screen.
-v Shows program version.
<s> Search expression
<r> Replacement expression
Types recognized:
@blocktypes
EOT
exit 1;
}
sub version {
print <<EOT;
$PROGNAME $VERSION
This code is a part of Slash, and is released under the GPL.
Copyright 1997-2004 by Open Source Development Network. See README
and COPYING for more information, or see http://slashcode.com/.
EOT
exit;
}
__END__