From: <no...@kr...> - 2008-02-29 21:10:19
|
Revision: 4919 Author: noam Date: 2008-02-29 16:10:18 -0500 (Fri, 29 Feb 2008) Log Message: ----------- Added test-mode to all utf-8 conversion scripts. Replaced double-prompt with single prompt in krang-to-utf8. Improved output. Modified Paths: -------------- branches/krang_v3_02/bin/krang_charset_convert_data branches/krang_v3_02/bin/krang_charset_convert_tables branches/krang_v3_02/bin/krang_prune_versions branches/krang_v3_02/bin/krang_to_utf8 branches/krang_v3_02/lib/Krang/Media.pm branches/krang_v3_02/lib/Krang/Story.pm branches/krang_v3_02/lib/Krang/Template.pm Modified: branches/krang_v3_02/bin/krang_charset_convert_data =================================================================== --- branches/krang_v3_02/bin/krang_charset_convert_data 2008-02-28 23:06:40 UTC (rev 4918) +++ branches/krang_v3_02/bin/krang_charset_convert_data 2008-02-29 21:10:18 UTC (rev 4919) @@ -70,6 +70,10 @@ This parameter (which is mutually exclusive with --only-story-id) will convert all tables and all stories except the given one. This can be specified multiple times. +=item --test-mode + +This is a mode in which actions will be displayed but not performed. + =item --verbose Show more information about what is being converted. @@ -135,16 +139,18 @@ use MIME::Base64 qw(encode_base64 decode_base64); # options -my ($from, $to, $help, $man, $all_instances, @only_site_ids, @only_story_ids, @skip_story_ids, $v); +my ($from, $to, $help, $man, $all_instances, @only_site_ids, @only_story_ids, @skip_story_ids, $test_mode, $v); GetOptions( 'from=s' => \$from, 'to=s' => \$to, 'help' => \$help, 'man' => \$man, + 'test_mode' => \$test_mode, 'all-instances' => \$all_instances, 'site-id=s' => \@only_site_ids, 'only-story-id=s' => \@only_story_ids, 'skip-story-id=s' => \@skip_story_ids, + 'test-mode' => \$test_mode, 'verbose' => \$v, ) or pod2usage(2); pod2usage(1) if $help; @@ -159,6 +165,9 @@ die "You can't use both --only-story-id and --skip-story-id at the same time!\n" if @only_story_ids and @skip_story_ids; +print "* * * TEST MODE - NO ACTUAL CHANGES WILL BE WRITTEN TO THE DATABASE * * *\n\n" + if $test_mode; + # figure out which instances we're going to use, and loop through them foreach my $instance ($all_instances ? pkg('Conf')->instances() : ($ENV{KRANG_INSTANCE})) { pkg('Conf')->instance($instance); @@ -249,7 +258,7 @@ . " WHERE " . join(' AND ', map { " $_ = ? " } @id_fields); my $update_sth = $dbh->prepare_cached($update_sql); - $sth->execute(@binds); + $sth->execute(@binds) unless $test_mode; my $count = 0; while(my $row = $sth->fetchrow_hashref) { my @data = map { $row->{$_} } @fields; @@ -259,7 +268,7 @@ @data = map { convert_scalar($_) || '' } @data; # now put the data back - $update_sth->execute(@data, @ids); + $update_sth->execute(@data, @ids) unless $test_mode; $count++; } print " Updated $count rows in $name\n" if $v; @@ -270,7 +279,7 @@ foreach my $template_id (pkg('Template')->find(ids_only => 1)) { my ($template) = pkg('Template')->find(template_id => $template_id); if ($template->deployed) { - $template->deploy; + $template->deploy unless $test_mode; print " Re-deployed template $template_id\n" if $v; } } @@ -310,7 +319,7 @@ )); my $update_sth = $dbh->prepare_cached('UPDATE element SET data = ? WHERE element_id = ?'); - $fetch_sth->execute(@story_ids, @only_story_ids ? () : @site_ids); + $fetch_sth->execute(@story_ids, @only_story_ids ? () : @site_ids) unless $test_mode; my $count = 0; while(my $row = $fetch_sth->fetchrow_hashref) { @@ -334,7 +343,7 @@ if ($frozen) { $data = encode_base64(nfreeze($data)); } - $update_sth->execute($data, $id); + $update_sth->execute($data, $id) unless $test_mode; $count++; } else { warn " !Could not convert element #$id which is a " @@ -342,6 +351,8 @@ } } print " Converted $count elements\n" if $v; + print "\n* * * TEST MODE - NO ACTUAL CHANGES WERE WRITTEN TO THE DATABASE * * *\nn" + if ($test_mode && $v); } # restore original instance (in case we were called by another Krang script) Modified: branches/krang_v3_02/bin/krang_charset_convert_tables =================================================================== --- branches/krang_v3_02/bin/krang_charset_convert_tables 2008-02-28 23:06:40 UTC (rev 4918) +++ branches/krang_v3_02/bin/krang_charset_convert_tables 2008-02-29 21:10:18 UTC (rev 4919) @@ -52,6 +52,10 @@ This optional parameter (which may be used multiple times) skips a particular table. +=item --test-mode + +This is a mode in which actions will be displayed but not performed. + =item --verbose Show more information about what is being converted. @@ -112,7 +116,7 @@ use Krang::ClassLoader DB => qw(dbh); # options -my ($to, $help, $man, $all_instances, @instances, @only_tables, @skip_tables, $v); +my ($to, $help, $man, $all_instances, @instances, @only_tables, @skip_tables, $test_mode, $v); GetOptions( 'to=s' => \$to, 'help' => \$help, @@ -120,6 +124,7 @@ 'all-instances' => \$all_instances, 'only-table=s' => \@only_tables, 'skip-table=s' => \@skip_tables, + 'test-mode' => \$test_mode, 'verbose' => \$v, ) or pod2usage(2); pod2usage(1) if $help; @@ -135,6 +140,9 @@ my %do_these = @only_tables ? map { $_ => 1 } @only_tables : (); my %skip_these = @skip_tables ? map { $_ => 1 } @skip_tables : (); +print "* * * TEST MODE - NO ACTUAL CHANGES WILL BE WRITTEN TO THE DATABASE * * *\n\n" + if $test_mode; + foreach my $instance (@instances) { pkg('Conf')->instance($instance); my $db_name = pkg('Conf')->get('InstanceDBName'); @@ -179,15 +187,17 @@ . $col->{null} . ($col->{default} ? "DEFAULT $col->{default}" : '') . " $col->{extra}"; - $dbh->do($sql); + $dbh->do($sql) unless $test_mode; } } # now change the default charset of the whole table print " Changing default charset to $to_charset\n" if $v; - $dbh->do("ALTER TABLE $table DEFAULT CHARACTER SET $to_charset"); + $dbh->do("ALTER TABLE $table DEFAULT CHARACTER SET $to_charset") unless $test_mode; } } print " Done\n"; +print "\n\n* * * TEST MODE - NO ACTUAL CHANGES WERE WRITTEN TO THE DATABASE * * *\n\n" + if ($test_mode && $v); Modified: branches/krang_v3_02/bin/krang_prune_versions =================================================================== --- branches/krang_v3_02/bin/krang_prune_versions 2008-02-28 23:06:40 UTC (rev 4918) +++ branches/krang_v3_02/bin/krang_prune_versions 2008-02-29 21:10:18 UTC (rev 4919) @@ -53,6 +53,10 @@ it is pruned, ensuring that the database's version table has the latest copy. (NOTE: Running in this mode forcibly checks in any checked-out objects.) +=item --test-mode + +This is a mode in which actions will be displayed but not performed. + =item --verbose If specified once, every object that has versions deleted will be displayed. If @@ -107,12 +111,15 @@ use Getopt::Long; use Pod::Usage; -my ($help, $man, $per_story, $per_media, $per_template, $all_instances, $first_resave); +my ($help, $man, $per_story, $per_media, $per_template, $all_instances, $first_resave, $test_mode); my $verbose = 0; print "\nkrang_prune_versions\n". "--------------------\n"; +print "\n* * * TEST MODE - NO ACTUAL CHANGES WILL BE WRITTEN TO THE DATABASE * * *\n\n" + if $test_mode; + pod2usage(2) unless GetOptions( 'versions-per-story=s' => \$per_story, @@ -120,6 +127,7 @@ 'versions-per-template=s' => \$per_template, 'all-instances' => \$all_instances, 'first-resave' => \$first_resave, + 'test-mode' => \$test_mode, 'verbose+' => \$verbose, 'help' => \$help, 'man' => \$man, @@ -145,9 +153,10 @@ if (my $versions_to_keep = $versions_to_keep{$type}) { print "\npruning $type objects\n"; my %args = ($versions_to_keep eq 'default' ? () : (number_to_keep => $versions_to_keep)); + $args{test_mode} = 1 if $test_mode; foreach my $object_id (pkg($pkg_name)->find(ids_only => 1)) { my ($object) = pkg($pkg_name)->find($type.'_id' => $object_id); - if ($first_resave) { + if ($first_resave && !$test_mode) { # before modifying current version of object, record its location my $checked_out_by = $object->checked_out && $object->checked_out_by; my $desk_id = $type->can('desk_id') && $object->desk_id; @@ -178,3 +187,6 @@ } } print "\ndone\n\n"; + +print "* * * TEST MODE - NO ACTUAL CHANGES WERE WRITTEN TO THE DATABASE * * *\n\n" + if ($test_mode && $verbose); Modified: branches/krang_v3_02/bin/krang_to_utf8 =================================================================== --- branches/krang_v3_02/bin/krang_to_utf8 2008-02-28 23:06:40 UTC (rev 4918) +++ branches/krang_v3_02/bin/krang_to_utf8 2008-02-29 21:10:18 UTC (rev 4919) @@ -37,6 +37,10 @@ Prune all but the latest (converted) versions of stories, media and template to prevent ISEs when Reverting. +=item --test-mode + +This is a mode in which actions will be displayed but not performed. (In this mode no backup will be performed.) + =over =cut @@ -82,13 +86,14 @@ use Getopt::Long; use Term::Prompt qw(prompt); -my ($from, $to, $help, $man, $v, $no_prompt, $no_backup, $remove_old_versions); +my ($from, $to, $help, $man, $v, $no_prompt, $no_backup, $remove_old_versions, $test_mode); GetOptions( 'no-prompt' => \$no_prompt, 'no-backup' => \$no_backup, 'remove-old-versions' => \$remove_old_versions, 'help' => \$help, 'man' => \$man, + 'test-mode' => \$test_mode, 'verbose' => \$v, ) or pod2usage(2); pod2usage(1) if $help; @@ -100,32 +105,29 @@ print "\nkrang_to_utf8\n-------------\n"; +print "\n(*TEST MODE*)\n" if $test_mode; + if (pkg('Charset')->is_utf8) { - print "\nYour krang.conf file already specifies UTF8 as the character set.\n\nIf this is a recent change and you haven't actually converted or saved any data since making it, please revert the change and re-run krang_to_utf8.\n\n"; + print "\nYour krang.conf file already specifies UTF-8 as the character set.\n\nIf this is a recent change and you haven't actually converted or saved any data since making it, please revert the change and re-run krang_to_utf8.\n\n"; exit; } +my $old_charset = pkg('Charset')->mysql_charset; unless ($no_prompt) { - exit 1 - unless prompt("y", "\nThis program will stop krang, convert the entire database of every instance to UTF-8, and update krang.conf\n\nAre you sure you want to proceed?", "y/n", "y"); + my $prompt = "\nThis program will stop krang, convert the entire database of every instance to UTF-8, and update krang.conf\n\n"; + $prompt .= $old_charset ? "Are you ready to proceed?" : "Your krang.conf has no character set specified.\n\nIs it safe to assume that your data is stored in Latin1 (i.e. ISO-8859 or Windows-1252), and that you're ready to convert everything to UTF-8?"; + exit 1 unless prompt ("y",$prompt,"y/n","n"); } +$old_charset ||= "latin1"; my $verbose = ($v ? '--verbose' : ''); +my $test = ($test_mode ? '--test-mode' : ''); my $bin = catdir($ENV{KRANG_ROOT}, "bin"); -my $old_charset = pkg('Charset')->mysql_charset; -unless ($old_charset || $no_prompt) { - unless (prompt("y","\nYour krang.conf has no character set specified.\nIs it safe to assume your data is stored in Latin1 (i.e. ISO-8859 or Windows-1252)?","y/n","y")) { - print "\nPlease add the line \"Charset <YOUR_OLD_CHARACTER_SET>\" to krang.conf and rerun this script.\n\n"; - exit; - } -} -$old_charset ||= "latin1"; - -print "\nSTOPPING KRANG\n\n"; +print "\n\nSTOPPING KRANG\n\n"; system ("$bin/krang_ctl stop"); -unless ($no_backup) { +unless ($no_backup || $test_mode) { print "\nBACKING UP EVERYTHING\n\n"; if (system ("$bin/krang_backup $verbose")) { print "\nBACKUP FAILED: UTF8 UPGRADE ABORTED\n\n"; @@ -134,24 +136,29 @@ } print "\nCONVERTING DATA TO UTF8\n\n"; -if (system ("$bin/krang_charset_convert_data --all-instances --from $old_charset --to utf8 $verbose")) { +if (system ("$bin/krang_charset_convert_data --all-instances --from $old_charset --to utf8 $verbose $test")) { print "\nDATA-CONVERSION FAILED: UTF8 UPGRADE INCOMPLETE (YOU MAY NEED TO RESTORE BACKUP!)\n\n"; exit; } print "\nCONVERTING TABLE DEFINITIONS TO UTF8\n\n"; # skip history - it's huge and only has ASCII anyway -if (system ("$bin/krang_charset_convert_tables --all-instances --to utf8 --skip-table history $verbose")) { +if (system ("$bin/krang_charset_convert_tables --all-instances --to utf8 --skip-table history $verbose $test")) { print "\nTABLE-CONVERSION FAILED: UTF8 UPGRADE INCOMPLETE (YOU MAY NEED TO RESTORE BACKUP!)\n\n"; exit; } my $krang_conf_file = catfile($ENV{KRANG_ROOT}, "conf", "krang.conf"); unless (-e $krang_conf_file) { - print "\nCOULDN'T FIND KRANG.CONF! PLEASE LOCATE IT AND ADD THE LINE 'CHARSET UTF8'; ASSUMING THERE WERE NO OTHER ERRORS, YOUR DATA AND TABLES SHOULD NOW BE CONVERTED.\n\n"; - print "THE ERROR DID PREVENT DELETION OF OLD VERSIONS: AFTER CHANGING KRANG.CONF, SEE BIN/KRANG_PRUNE_VERSIONS TO ACCOMPLISH THIS MANUALLY.\n\n" if $remove_old_versions; + if ($test_mode) { + print "\nCOULDN'T FIND KRANG.CONF!\n\n"; + } else { + print "\nCOULDN'T FIND KRANG.CONF! PLEASE LOCATE IT AND ADD THE LINE 'CHARSET UTF8'; ASSUMING THERE WERE NO OTHER ERRORS, YOUR DATA AND TABLES SHOULD NOW BE CONVERTED.\n\n"; + print "THE ERROR DID PREVENT DELETION OF OLD VERSIONS: AFTER CHANGING KRANG.CONF, SEE BIN/KRANG_PRUNE_VERSIONS TO ACCOMPLISH THIS MANUALLY.\n\n" if $remove_old_versions; + } exit; } +print "\n\nUPDATING $krang_conf_file\n--------\n"; my $output; open KRANG_CONF, "<$krang_conf_file"; my $added_charset; @@ -159,9 +166,9 @@ if ($_ =~ /^\s*\#*\s*Charset\s/i) { chomp($_); if ($added_charset++) { - print "\nREMOVING '$_' FROM $krang_conf_file\n"; + print "REMOVING '$_'\n"; } else { - print "\nREPLACING '$_' WITH 'Charset UTF-8' IN $krang_conf_file\n"; + print "REPLACING '$_' WITH 'Charset UTF-8'\n"; $output .= "Charset UTF-8\n"; } } else { @@ -169,15 +176,19 @@ } } unless ($added_charset) { - print "\nADDING 'Charset UTF-8' AT THE END OF $krang_conf_file\n"; + print "ADDING 'Charset UTF-8' AT THE END\n"; $output =~ s/\s+$//; $output .= "\n\nCharset UTF-8\n"; } close KRANG_CONF; -open KRANG_CONF, "> $krang_conf_file"; -print KRANG_CONF $output; -close KRANG_CONF; +unless ($test_mode) { + open KRANG_CONF, "> $krang_conf_file"; + print KRANG_CONF $output; + close KRANG_CONF; +} +print "\n"; + my $versions; if ($remove_old_versions) { print "\nREMOVING OLD VERSIONS OF STORIES, TEMPLATES, AND MEDIA OBJECTS FROM DATABASE\n"; @@ -187,7 +198,7 @@ $versions = 999999; } -if (system ("$bin/krang_prune_versions --all-instances --first-resave --versions-per-media $versions --versions-per-story $versions --versions-per-template $versions $verbose")) { +if (system ("$bin/krang_prune_versions --all-instances --first-resave --versions-per-media $versions --versions-per-story $versions --versions-per-template $versions $verbose $test")) { print "KRANG_PRUNE_VERSIONS FAILED: NOT THE END OF THE WORLD; YOU CAN RUN THIS BY HAND LATER.\n(MAKE SURE TO USE THE '--first-resave' ARGUMENT)\n"; } @@ -195,3 +206,6 @@ system ("$bin/krang_ctl start"); print "\nUTF8 UPGRADE COMPLETE!\n\nASSUMING NO ERRORS ABOVE, ALL YOU HAVE TO DO NOW IS MAKE SURE YOUR\nCATEGORY TEMPLATES AND/OR SERVER CONFIGURATION INCLUDE A UTF8 HEADER\n(e.g. \"Content-Type: text/html; charset=utf-8\") AND THEN RE-PUBLISH!\n\n"; + +print "* * * TEST MODE - NO ACTUAL CHANGES WERE WRITTEN TO THE DATABASE OR KRANG.CONF * * *\n\n" + if $test_mode; Modified: branches/krang_v3_02/lib/Krang/Media.pm =================================================================== --- branches/krang_v3_02/lib/Krang/Media.pm 2008-02-28 23:06:40 UTC (rev 4918) +++ branches/krang_v3_02/lib/Krang/Media.pm 2008-02-29 21:10:18 UTC (rev 4919) @@ -490,7 +490,7 @@ my @versions_to_delete = splice(@all_versions, 0, $number_to_delete); $dbh->do('DELETE FROM media_version WHERE media_id = ? AND version IN ('. join(',', ("?") x @versions_to_delete) . ')', - undef, $self->media_id, @versions_to_delete); + undef, $self->media_id, @versions_to_delete) unless $args{test_mode};; return $number_to_delete; } Modified: branches/krang_v3_02/lib/Krang/Story.pm =================================================================== --- branches/krang_v3_02/lib/Krang/Story.pm 2008-02-28 23:06:40 UTC (rev 4918) +++ branches/krang_v3_02/lib/Krang/Story.pm 2008-02-29 21:10:18 UTC (rev 4919) @@ -648,7 +648,7 @@ my @versions_to_delete = splice(@all_versions, 0, $number_to_delete); $dbh->do('DELETE FROM story_version WHERE story_id = ? AND version IN ('. join(',', ("?") x @versions_to_delete) . ')', - undef, $self->story_id, @versions_to_delete); + undef, $self->story_id, @versions_to_delete) unless $args{test_mode}; return $number_to_delete; } Modified: branches/krang_v3_02/lib/Krang/Template.pm =================================================================== --- branches/krang_v3_02/lib/Krang/Template.pm 2008-02-28 23:06:40 UTC (rev 4918) +++ branches/krang_v3_02/lib/Krang/Template.pm 2008-02-29 21:10:18 UTC (rev 4919) @@ -1054,7 +1054,7 @@ my @versions_to_delete = splice(@all_versions, 0, $number_to_delete); $dbh->do('DELETE FROM template_version WHERE template_id = ? AND version IN ('. join(',', ("?") x @versions_to_delete) . ')', - undef, $self->template_id, @versions_to_delete); + undef, $self->template_id, @versions_to_delete) unless $args{test_mode}; return $number_to_delete; } |