The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl -w
use strict;
use Getopt::Long;

my ($dbtype, $dbname, $dbuser, $dbpass, $dbhost, $dbport, $help, $oldname, $newname);
GetOptions( "type=s"         => \$dbtype,
            "name=s"         => \$dbname,
            "user=s"         => \$dbuser,
            "pass=s"         => \$dbpass,
            "host=s"         => \$dbhost,
            "port=s"         => \$dbport,
            "help"           => \$help,
            "oldname=s"      => \$oldname,
            "newname=s"      => \$newname,
          );

unless (defined($dbtype)) {
    print "You must supply a database type with the --type option.\n";
    print "Further help can be found by typing 'perldoc $0'\n";
    exit 1;
}

unless (defined($dbname)) {
    print "You must supply a database name with the --name option.\n";
    print "Further help can be found by typing 'perldoc $0'\n";
    exit 1;
}

unless (defined($oldname)) {
    print "You must supply the old node name with the --oldname option.\n";
    print "Further help can be found by typing 'perldoc $0'\n";
    exit 1;
}

unless (defined($newname)) {
    print "You must supply the new node name with the --newname option.\n";
    print "Further help can be found by typing 'perldoc $0'\n";
    exit 1;
}

if ($help) {
    print "Help can be found by typing 'perldoc $0'\n";
    exit 0;
}

my %setup_modules = ( postgres => "Wiki::Toolkit::Store::Pg",
                      mysql    => "Wiki::Toolkit::Store::MySQL",
                      sqlite  => "Wiki::Toolkit::Store::SQLite"
);

unless ( defined($setup_modules{$dbtype}) ) {
    print "dbtype must be one of 'postgres', 'mysql', and 'sqlite'\n";
    print "further help can be found by typing 'perldoc $0'\n";
    exit 1;
}

# Load classes
require Wiki::Toolkit;
my $class = $setup_modules{$dbtype};
eval "require $class";
if ( $@ ) {
    print "Couldn't 'use' $class: $@\n";
    exit 1;
}

# Create a store instance
my $store;
my $args = "dbname=>'$dbname', dbuser=>'$dbuser'";
if($dbpass) {
    $args .= ", dbpass=>'$dbpass'";
}
if($dbhost) {
    $args .= ", dbhost=>'$dbhost'";
}
if($dbport) {
    $args .= ", dbport=>'$dbport'";
}
eval "\$store = $class->new($args);";

# Create a Wiki instance
my $wiki = Wiki::Toolkit->new(store=>$store);

# Do the rename
$wiki->rename_node(old_name=>$oldname, new_name=>$newname);

# All done
print "Renamed '$oldname' to '$newname'\n";

=head1 NAME

wiki-toolkit-rename-node - Rename a node stored in a Wiki::Toolkit instance.

=head1 SYNOPSIS

  # Rename a node in a Wiki::Toolkit instance, updating internal links
  #  and references if the formatter supports link re-writing.

  wiki-toolkit-rename-node --type postgres
           --name mywiki \
           --user wiki  \
           --pass wiki \
           --host 'db.example.com' \
           --port 1234
           --oldname MyOldNodeName \
           --nemname FancyNewNodeName

=head1 DESCRIPTION

Takes four mandatory arguments:

=over 4

=item type

The database type.  Should be one of 'postgres', 'mysql' and 'sqlite'.

=item name

The database name.

=item oldname

The name of the node to be renamed.

=item newname

The new name for the node.

=back

four optional arguments:

=over 4

=item user

The user that connects to the database. It must have permission
to create and drop tables in the database.

=item pass

The user's database password.

=item host

The hostname of the machine the database server is running on (omit
for local databases).

=item port

The port number that the database server is expecting connections to.

=back

=head1 AUTHOR

Nick Burch

=head1 COPYRIGHT

     Copyright (C) 2006 Nick Burch.  All Rights Reserved.

This code is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=head1 SEE ALSO

L<Wiki::Toolkit>

=cut

1;