use
lib
"$FindBin::Bin/../../lib"
;
my
$search_string
;
my
$replacement_string
;
my
$result
= GetOptions(
"search|s=s"
=> \
$search_string
,
"replace|r=s"
=> \
$replacement_string
,
) or
die
&usage
();
if
( !
$search_string
|| !
$replacement_string
) {
&usage
;
exit
1;
}
my
$schema
= schema_connect();
my
$page_rs
=
$schema
->resultset(
'Page'
);
my
$found_search_string
= 0;
while
(
my
$page
=
$page_rs
->
next
) {
my
$page_content
=
$schema
->resultset(
'Content'
)->search(
{
page
=>
$page
->id,
version
=>
$page
->content_version,
}
);
if
(
$page_content
->count == 1 ) {
my
$content
=
$page_content
->first->body;
if
(
$content
=~ m{
$search_string
} ) {
my
$number_of_replacements
=
$content
=~ s{
$search_string
}{
$replacement_string
}mxg;
$number_of_replacements
||= 0;
print
"Made $number_of_replacements replacments in page: "
,
$page
->name_orig,
" \n"
;
if
(
$number_of_replacements
) {
$found_search_string
= 1; }
$page_content
->first->update( {
body
=>
$content
} );
}
}
elsif
(
$page_content
->count > 1 ) {
print
"ERROR: Seems there is more than one
'latest content version'
of
the page:
", $page->name, "
\n";
}
else
{
print
"NOTICE: Page "
,
$page
->name,
" is only a prototype and therefore has no content.\n"
;
}
}
if
( not
$found_search_string
) {
print
"Did not find search string: $search_string\n"
; }
sub
schema_connect {
my
@db_cfg
= @{ MojoMojo->config()->{
'Model::DBIC'
}->{
'connect_info'
} };
return
MojoMojo::Schema->
connect
(
@db_cfg
[
qw/0 1 2/
] );
}
sub
usage {
print
"Usage:
perl script/util/search_and_replace.pl --search=SEARCH_STRING --replace=REPLACEMENT_STRING\n";
}