]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - maintenance/delete-idle-wiki-users.pl
MediaWiki 1.5.8 (initial commit)
[autoinstallsdev/mediawiki.git] / maintenance / delete-idle-wiki-users.pl
1 #!/usr/bin/perl
2 #
3 # Nuke idle wiki accounts from the wiki's user database.
4 #
5 # Copyright (C) 2005 Ralf Baechle (ralf@linux-mips.org)
6 #
7 # This program is free software; you can redistribute  it and/or modify it
8 # under  the terms of  the GNU General  Public License as published by the
9 # Free Software Foundation;  either version 2 of the  License, or (at your
10 # option) any later version.
11 #
12 # THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
13 # WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
15 # NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
16 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
17 # NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
18 # USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
19 # ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
20 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
21 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22 #
23 # You should have received a copy of the  GNU General Public License along
24 # with this program; if not, write  to the Free Software Foundation, Inc.,
25 # 675 Mass Ave, Cambridge, MA 02139, USA.
26 #
27
28 my $database = "DBI:mysql:database=wikidb;host=localhost";
29 my $dbuser   = "wikiuser";
30 my $dbpasswd = "password";
31
32 use strict;
33 use DBI();
34
35 my $verbose = 0;
36 my $for_real = 1;
37
38 sub do_db_op
39 {
40         my ($dbh, $sql) = @_;
41
42         if ($verbose >= 3) {
43                 print $sql . ";\n"
44         }
45
46         if ($for_real == 1) {
47                 $dbh->do($sql);
48         }
49 }
50
51 sub undo_user
52 {
53         my ($ref, $dbh, $sth, $killed);
54
55         # Connect to the database.
56         $dbh = DBI->connect($database, $dbuser, $dbpasswd, {RaiseError => 1});
57
58         $sth = $dbh->prepare("SELECT * FROM user");
59         $sth->execute();
60
61         $ref = $sth->fetchrow_hashref();
62
63         if ($sth->rows == 0) {
64                 print "There is no user in this wiki.\n";
65                 return;
66         }
67
68         while ($ref = $sth->fetchrow_hashref()) {
69                 my ($user_id, $user_name, $cph, $oph, $edits);
70
71                 $user_name = $ref->{user_name};
72                 $user_id = $ref->{user_id};
73                 if ($verbose >= 2) {
74                         print "Annihilating user " . $user_name .
75                               " has user_id " . $user_id . ".\n";
76                 }
77
78                 $cph = $dbh->prepare("SELECT * FROM cur where " .
79                                 "cur_user = $user_id" .
80                                 " AND " .
81                                 "cur_user_text = " . $dbh->quote("$user_name"));
82                 $cph->execute();
83
84                 $oph = $dbh->prepare("SELECT * FROM old where " .
85                                 "old_user = $user_id" .
86                                 " AND " .
87                                 "old_user_text = " . $dbh->quote("$user_name"));
88                 $oph->execute();
89
90                 $edits = $cph->rows + $oph->rows;
91
92                 $cph->finish();
93                 $oph->finish();
94
95                 if ($edits == 0) {
96                         if ($verbose >= 2) {
97                                 print "Keeping user " . $user_name .
98                                       ", user_id " . $user_id . ".\n";
99                         }
100
101                         do_db_op($dbh,
102                          "DELETE FROM user WHERE user_name = " .
103                          $dbh->quote("$user_name") .
104                          " AND " .
105                          "user_id = $user_id");
106
107                         $killed++;
108                 }
109         }
110
111         $sth->finish();
112
113         $dbh->disconnect();
114
115         if ($verbose >= 1) {
116                 print "Killed " . $killed . " users\n";
117         }
118 }
119
120 my (@users, $user, $this, $opts);
121
122 @users = ();
123 $opts = 1;
124
125 foreach $this (@ARGV) {
126         if ($opts == 1 && $this eq '-v') {
127                 $verbose++;
128         } elsif ($opts == 1 && $this eq '--verbose') {
129                 $verbose = 1;
130         } elsif ($opts == 1 && $this eq '--') {
131                 $opts = 0;
132         } else {
133                 push(@users, $this);
134         }
135 }
136
137 undo_user();
138