source: wiki-toolkit/trunk/bin/wiki-toolkit-delete-node @ 423

Last change on this file since 423 was 423, checked in by nick, 14 years ago

Command line delete tool

  • Property svn:executable set to *
File size: 4.2 KB
Line 
1#!/usr/bin/perl -w
2use strict;
3use Getopt::Long;
4
5my ($dbtype, $dbname, $dbuser, $dbpass, $dbhost, $dbport, $help, $id, $node_name, $version);
6GetOptions( "type=s"         => \$dbtype,
7            "name=s"         => \$dbname,
8            "user=s"         => \$dbuser,
9            "pass=s"         => \$dbpass,
10            "host=s"         => \$dbhost,
11            "port=s"         => \$dbport,
12            "help"           => \$help,
13            "id=s"           => \$id,
14            "nodename=s"     => \$node_name,
15            "version=s"      => \$version,
16           );
17
18unless (defined($dbtype)) {
19    print "You must supply a database type with the --type option.\n";
20    print "Further help can be found by typing 'perldoc $0'\n";
21    exit 1;
22}
23
24unless (defined($dbname)) {
25    print "You must supply a database name with the --name option.\n";
26    print "Further help can be found by typing 'perldoc $0'\n";
27    exit 1;
28}
29
30if(defined($id) and defined($node_name)) {
31        print "You should supply either a node name, or an id, but not both.\n";
32        print "Further help can be found by typing 'perldoc $0'\n";
33        exit 1;
34}
35if(not defined($id) and not defined($node_name)) {
36        print "You must supply the id of the node with the --id option,\n";
37        print " or the node name of the node with the --nodename option.\n";
38        print "Further help can be found by typing 'perldoc $0'\n";
39        exit 1;
40}
41
42if ($help) {
43    print "Help can be found by typing 'perldoc $0'\n";
44    exit 0;
45}
46
47my %setup_modules = ( postgres => "Wiki::Toolkit::Store::Pg",
48                      mysql    => "Wiki::Toolkit::Store::MySQL",
49                      sqlite  => "Wiki::Toolkit::Store::SQLite"
50);
51
52unless ( defined($setup_modules{$dbtype}) ) {
53    print "dbtype must be one of 'postgres', 'mysql', and 'sqlite'\n";
54    print "further help can be found by typing 'perldoc $0'\n";
55    exit 1;
56}
57
58# Load classes
59require Wiki::Toolkit;
60my $class = $setup_modules{$dbtype};
61eval "require $class";
62if ( $@ ) {
63    print "Couldn't 'use' $class: $@\n";
64    exit 1;
65}
66
67# Create a store instance
68my $store;
69my $args = "dbname=>'$dbname', dbuser=>'$dbuser'";
70if($dbpass) { $args .= ", dbpass=>'$dbpass'"; }
71if($dbhost) { $args .= ", dbhost=>'$dbhost'"; }
72if($dbport) { $args .= ", dbport=>'$dbport'"; }
73eval "\$store = $class->new($args);";
74
75# Create a Wiki instance
76my $wiki = Wiki::Toolkit->new(store=>$store);
77
78# If they gave the ID, get the name
79if($id) {
80        $node_name = $wiki->store->node_name_for_id($id);
81        unless($node_name) {
82                die("No node found with id '$id'\n");
83        }
84}
85
86# Report what we're going to do
87print "Deleting node with name '$node_name'";
88if($id) {
89        print " (id $id)";
90}
91if($version) {
92        print " at version $version";
93}
94print "\n";
95
96# Do the delete
97$wiki->delete_node(name => $node_name, verion => $version);
98
99# All done
100print "done.\n";
101
102
103=head1 NAME
104
105wiki-toolkit-delete-node - Delete a node stored in a Wiki::Toolkit instance.
106
107=head1 SYNOPSIS
108
109  # Delete a node in a Wiki::Toolkit instance
110
111  wiki-toolkit-delete-node --type postgres
112                   --name mywiki \
113                   --user wiki  \
114                   --pass wiki \
115                   --host 'db.example.com' \
116                   --port 1234 \
117                   --nodename MyNodeName
118
119  wiki-toolkit-delete-node --type postgres
120                   --name mywiki \
121                   --user wiki  \
122                   --pass wiki \
123                   --host 'db.example.com' \
124                   --port 1234 \
125                   --id 2 \
126                   --version 7
127
128=head1 DESCRIPTION
129
130Takes four mandatory arguments:
131
132=over 4
133
134=item type
135
136The database type.  Should be one of 'postgres', 'mysql' and 'sqlite'.
137
138=item name
139
140The database name.
141
142=item nodename
143
144The name of the node to be deleted.
145
146=item id
147
148The id of the node to be deleted
149
150=back
151
152four optional arguments:
153
154=over 4
155
156=item user
157
158The user that connects to the database. It must have permission
159to create and drop tables in the database.
160
161=item pass
162
163The user's database password.
164
165=item host
166
167The hostname of the machine the database server is running on (omit
168for local databases).
169
170=item port
171
172The port number that the database server is expecting connections to.
173
174=item version
175
176The version number of the node to delete
177
178=back
179
180=head1 AUTHOR
181
182Nick Burch
183
184=head1 COPYRIGHT
185
186     Copyright (C) 2006 Nick Burch.  All Rights Reserved.
187
188This code is free software; you can redistribute it and/or modify it
189under the same terms as Perl itself.
190
191=head1 SEE ALSO
192
193L<Wiki::Toolkit>
194
195=cut
196
1971;
Note: See TracBrowser for help on using the repository browser.