LJSUP-17669: Login.bml form refactoring
[livejournal.git] / cgi-bin / LJ / ExpungedUsers.pm
blob92e9e5d1bcc010e731f477cf6843f8a3be5d2325
1 #!/usr/bin/perl
3 package LJ::ExpungedUsers;
5 use strict;
6 use Carp qw(croak);
8 sub load_recent {
9 my $class = shift;
10 my %opts = @_;
12 my $within = delete $opts{within} || 3600;
13 my $limit = delete $opts{limit} || 100;
15 croak "invalid arguments: " . join(",", keys %opts)
16 if %opts;
18 my $dbr = LJ::get_db_reader()
19 or die "unable to contact global reader";
21 my $sth = $dbr->prepare
22 ("SELECT userid, expunge_time FROM expunged_users " .
23 "WHERE expunge_time > UNIX_TIMESTAMP() - ? LIMIT $limit");
24 $sth->execute($within);
26 my @uids = ();
27 my %exp_times = (); # uid => exp_time
28 while (my ($uid, $exp_time) = $sth->fetchrow_array) {
29 push @uids, $uid;
30 $exp_times{$uid} = $exp_time;
33 my $us = LJ::load_userids(@uids);
35 return (grep { $_->[0]->{user} !~ /^ex_/ && !$_->[0]->is_identity }
36 sort { $a->[0]->{user} cmp $b->[0]->{user} }
37 map { [ $us->{$_}, $exp_times{$_} ] } @uids);
40 sub load_single_user {
41 my $class = shift;
42 my $user = shift;
44 $user = LJ::canonical_username($user);
45 croak "invalid user: $user"
46 unless $user;
48 my $u = LJ::load_user($user);
50 # is the user actually expunged?
51 return unless $u && $u->is_expunged;
53 # did someone rename to it?
54 return if $u->user =~ /^ex_/;
56 # ding.
57 return $u;
60 sub fuzzy_load_user {
61 my $class = shift;
62 my $user = shift;
64 $user = LJ::canonical_username($user);
65 croak "invalid user: $user"
66 unless $user;
68 return $class->_query_result_array
69 ("SELECT userid, expunge_time FROM expunged_users " .
70 "WHERE user LIKE ? LIMIT 10", $user . "%");
73 sub _query_result_array {
74 my $class = shift;
75 my ($sql, @vals) = @_;
77 my $dbr = LJ::get_db_reader()
78 or die "unable to contact global reader";
80 my $sth = $dbr->prepare($sql);
81 $sth->execute(@vals);
82 die $dbr->errstr if $dbr->err;
84 my @rows = ();
85 while (my ($uid, $exp_time) = $sth->fetchrow_array) {
86 push @rows, [ $uid => $exp_time ];
89 my $us = LJ::load_userids(map { $_->[0] } @rows);
90 my @ret = ();
91 foreach my $row (@rows) {
92 my $u = $us->{$row->[0]};
94 # someone already renamed to this?
95 next if $u->{user} =~ /^ex_/;
97 # push all users except for identity users
98 push @ret, [ $u => $row->[1] ] unless $u->is_identity;
101 return @ret;
104 sub random_by_letter {
105 my $class = shift;
106 my %opts = @_;
108 my $letter = substr(delete $opts{letter}, 0, 1) || '0';
109 my $prev_max = delete $opts{prev_max} || 0;
110 my $limit = delete $opts{limit} || 100;
112 croak "invalid arguments: " . join(",", keys %opts)
113 if %opts;
115 my $dbr = LJ::get_db_reader()
116 or die "unable to contact global reader";
118 my $min = $prev_max + 0;
120 my $sth = $dbr->prepare
121 ("SELECT userid, expunge_time FROM expunged_users " .
122 "WHERE (user LIKE ? OR user LIKE ?) AND expunge_time>? " .
123 "LIMIT $limit");
124 $sth->execute(uc($letter) . '%', lc($letter) . '%', $min);
125 die $dbr->errstr if $dbr->err;
127 my @rows = ();
128 while (my ($uid, $exp_time) = $sth->fetchrow_array) {
129 push @rows, [ $uid => $exp_time ];
132 # if we got less than the limit, then we hit the beginning
133 if (@rows < $limit) {
134 my $new_limit = $limit - @rows;
135 $sth = $dbr->prepare
136 ("SELECT userid, expunge_time FROM expunged_users " .
137 "WHERE (user LIKE ? OR user LIKE ?) AND expunge_time>? " .
138 "LIMIT $new_limit");
139 $sth->execute(uc($letter) . '%', lc($letter). '%', 0);
140 die $dbr->errstr if $dbr->err;
142 while (my ($uid, $exp_time) = $sth->fetchrow_array) {
143 push @rows, [ $uid => $exp_time ];
147 my $us = LJ::load_userids(map { $_->[0] } @rows);
148 my @ret = ();
149 foreach my $row (@rows) {
150 my $u = $us->{$row->[0]};
152 # someone already renamed to this?
153 next if $u->{user} =~ /^ex_/;
155 # push all users except for identity users
156 push @ret, [ $u => $row->[1] ] unless $u->is_identity;
159 return @ret;