[interchange-cvs] interchange - heins modified dist/lib/UI/pages/admin/search_replace.html
interchange-core@interchange.redhat.com
interchange-core@interchange.redhat.com
Sat Feb 2 22:43:00 2002
User: heins
Date: 2002-02-03 03:42:49 GMT
Added: dist/lib/UI/pages/admin search_replace.html
Log:
* Add minimal search/replace feature for the database.
* TODO: Needs some online help -- you can do a parenthisized
substitution (one) and replace partial strings, but needs
to be documented.
Revision Changes Path
1.1 interchange/dist/lib/UI/pages/admin/search_replace.html
rev 1.1, prev_rev 1.0
Index: search_replace.html
===================================================================
[set page_title]Search and replace a database field[/set]
[set ui_class]Admin[/set]
[set page_banner]Search and Replace: Mass-update data in a table[/set]
[set page_perm]tables[/set]
[set help_name]genedit[/set]
[set icon_name]icon_config.gif[/set]
[tmp meta_header]
[include include/table_populator]
<SCRIPT LANGUAGE=JavaScript>
function change_ta (ta_field, selector) {
if(selector == 'complex') {
ta_field.rows = 20;
ta_field.disabled = false;
}
else {
ta_field.rows = 1;
ta_field.disabled = true;
}
return;
}
</SCRIPT>
[/tmp]
[seti ui_body_extra][/seti]
@_UI_STD_HEAD_@
<!-- ----- BEGIN REAL STUFF ----- -->
<form action="[area __UI_BASE__/flex_select]" method=GET>
<input type=hidden name=mv_action value=back>
<table border=0 __UI_T_PROPERTIES__>
<tr>
<td colspan=2 bgcolor=__UI_C_TOPBLOCKBAR__><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>
<tr bgcolor="__UI_C_INTBLOCK__">
<td width=25%>
1. Choose a table to operate on:
</td>
<td>
[if !value area_table]
[value name=area_table set=products hide=1]
[value name=area_column set=category hide=1]
[/if]
<select name="area_table"
onChange="
populateTable(
this.form.area_table.options,
this.form.area_column.options,
this.form.area_table.options[this.form.area_table.selectedIndex].value
);
"
size=1>
[loop list="[list-databases]" option=area_table]
<OPTION VALUE="[loop-code]">[loop-code]</OPTION>
[/loop]
</select>
</td>
</tr>
<tr>
<td>
2. Choose a column to operate on:
</td>
<td>
<select name="area_column">
<option value=""> -- none --
[loop list="[db-columns table='[value area_table]']" option=area_column]
<option>[loop-code]
[/loop]
</select>
</td>
</tr>
<tr>
<td>
3. Set what to search for in that column:
</td>
<td>
<input name="search_for" type="text" size="30"> (Perl regular expression, case sensitive)
</td>
</tr>
<tr>
<td>
4. Choose the replacement string
</td>
<td>
<input name="replace_with" type="text" size="30"> (Perl regular expression, case sensitive)
</td>
</tr>
<tr>
<td>
5. Click this button to do it:
</td>
<td>
[button text="Search and Replace"]
[flag type=write table="[cgi area_table]"]
[seti ui_failure]
[try]
[perl tables="[cgi area_table]"]
my $go_page = $CGI->{mv_nextpage};
$CGI->{mv_nextpage} = '@@MV_PAGE@@';
my $tab = $CGI->{area_table}
or return "no source table";
my $db = $Db{$tab}
or return "no source table";
my $key = $db->config('KEY');
my $col = $CGI->{area_column}
or return "no column specified";
for(\$key, \$col) {
$$_ = lc($$_);
return "bad column name '$$_'" if $$_ =~ /\W/;
}
$col ne $key
or return "No search and replace allowed on primary key field.";
my $q = "select $key, $col from $tab";
my $ary = $db->query($q)
or return qq{query "$q" failed};
$Scratch->{try_message} = "Compiling regex";
my $sf = $CGI->{search_for};
my $rw = $CGI->{replace_with};
my $regex;
if($sf =~ m{(^|[^\\])\(} and $sf =~ m{[^\\]\)}) {
Debug("regex with parens, '$sf'");
$sf =~ s/(.*?)\((.*)\)(.*)/($1)($2)($3)/;
Debug("regex with parens, now '$sf'");
$regex = qr{$sf};
}
else {
Debug("regex without parens, '$sf'");
$regex = qr{()($sf)()};
Debug("regex without parens, now '$sf'");
}
delete $Scratch->{try_message};
my @set;
for(@$ary) {
next unless $_->[1] =~ s{$regex}{$1$rw$3};
push @set, $_;
}
my @messages;
my $count = 0;
if(@set) {
my $settor = $db->field_settor($col);
for(@set) {
my $status = $settor->(@$_);
($count++, next) if $status;
push @messages, qq{failed to set key=$_->[0] col=$col to '$_->[1]'};
}
}
else {
@messages = qq{'$sf' not found in column $col};
}
$CGI->{mv_nextpage} = $go_page;
$CGI->{mv_data_table} = $tab;
unshift @messages, "Updated $count rows.";
$Scratch->{ui_message} = join "<BR>", @messages;
return;
[/perl]
[/try]
[/seti]
[catch]
[seti ui_failure]Error: [scratch try_message][/seti]
[/catch]
[/button]
<BR>
</td>
</tr>
<tr>
<td colspan=2 bgcolor=__UI_C_TOPBLOCKBAR__><img src="bg.gif" width=__UI_MAIN_WIDTH__ height=1></td>
</tr>
</table>
</form>
<!-- ----- END REAL STUFF ----- -->
@_UI_STD_FOOTER_@
<!-- page: @@MV_PAGE@@ -->