Ticket #81: sniplocales.pl

File sniplocales.pl, 2.9 kB (added by grimoire, 2 years ago)

Script to rename "Locale Foo" to "Foo" and set up redirects. Probably only works with pre-W::T versions of OpenGuides.

Line 
1#!/usr/bin/perl -w
2
3#
4# Renames nodes from "Locale Foo" to "Foo" and sets up redirects
5# (c) 2006 Dave Page. Licensed under the same terms as Perl itself
6#
7
8use strict;
9
10use OpenGuides::Config;
11use DBI;
12
13        my $prefix = 'Locale ';         # NOTE THE SPACE
14
15        my $config_file = $ENV{OPENGUIDES_CONFIG_FILE} || "wiki.conf";
16        my $config = OpenGuides::Config->new( file => $config_file )
17                or die "Couldn't open and parse config file $config_file";
18
19        my $dsn = 'DBI:' . $config->{'dbtype'} . ':database=' . $config->{dbname};
20        my $dbh = DBI->connect($dsn, $config->{'dbuser'}, $config->{'dbpass'})
21                or die "Couldn't connect to database!";
22
23        my ($statement, $sth);
24
25        # Engage locking
26
27        $dbh->begin_work();
28
29        # Get a list of nodes prefixed with $prefix
30
31        $sth = $dbh->prepare("SELECT name, text FROM content WHERE name LIKE ?");
32        $sth->execute("${prefix}%");
33       
34        my %prefixednodes = %{ $sth->fetchall_hashref('name') };
35
36        # Get a list of existing nodes without that prefix - these will cause conflict
37
38        my @unprefixednodes = map { s/^${prefix}//; $_ } keys %prefixednodes;
39
40        $statement = "SELECT name FROM content WHERE name IN ( " . join(', ', map { $dbh->quote($_) } @unprefixednodes) . " )";
41
42        $sth = $dbh->prepare($statement);
43        $sth->execute();
44
45        my %conflictnodes = %{ $sth->fetchall_hashref('name') } ;
46
47        # Rename all prefixed nodes which don't have conflicts
48
49        foreach my $nodename ( @unprefixednodes ) {
50                if ( exists $conflictnodes{$nodename} ) {       # We should perhaps check for a #REDIRECT here before shooting our mouths off
51                        if ( $prefixednodes{"${prefix}${nodename}"}{text} eq "#REDIRECT [[$nodename]]" ) {
52                                # print "Node $nodename exists, but node ${prefix}${nodename} is a #REDIRECT to it\n";
53                        } else {
54                                warn "CONFLICT: ${prefix}${nodename} and ${nodename} both exist!\n";
55                        }
56                        next;
57                }
58
59                $dbh->do("UPDATE content SET name = " . $dbh->quote($nodename) . " WHERE name = " . $dbh->quote($prefix . $nodename));
60                $dbh->do("UPDATE node SET name = " . $dbh->quote($nodename) . " WHERE name = " . $dbh->quote($prefix . $nodename));
61                $dbh->do("UPDATE metadata SET node = " . $dbh->quote($nodename) . " WHERE node = " . $dbh->quote($prefix . $nodename));
62                $dbh->do("UPDATE internal_links SET link_from = " . $dbh->quote($nodename) . " WHERE link_from = " . $dbh->quote($prefix . $nodename));
63                $dbh->do("UPDATE internal_links SET link_to = " . $dbh->quote($nodename) . " WHERE link_to = " . $dbh->quote($prefix . $nodename));
64
65                $dbh->do("INSERT INTO content (name, version, text, modified, comment) VALUES (" . $dbh->quote($prefix.$nodename)
66                        . ", 1, " . $dbh->quote("#REDIRECT [[$nodename]]") . ", NOW(), 'Automatically generated #REDIRECT' )" );
67                $dbh->do("INSERT INTO node (name, version, text, modified ) VALUES (" . $dbh->quote($prefix.$nodename)
68                        . ", 1, " . $dbh->quote("#REDIRECT [[$nodename]]") . ", NOW() )" );
69
70                print "Node '${prefix}${nodename}' renamed to '$nodename'\n";
71        }
72
73        $dbh->rollback();
74
75        $dbh->disconnect();