fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-12-07 01:50 pm

[dw-free] better organization of LJ functions

[commit: http://hg.dwscoalition.org/dw-free/rev/904af5fb25f0]

http://bugs.dwscoalition.org/show_bug.cgi?id=3965

New module LJ::Procnotify (in a new file cgi-bin/LJ/Procnotify.pm, moved out
of ljlib.pl)

Patch by [personal profile] kareila.

Files modified:
  • bin/renameuser.pl
  • cgi-bin/Apache/LiveJournal.pm
  • cgi-bin/DW/User/Rename.pm
  • cgi-bin/LJ/Procnotify.pm
  • cgi-bin/LJ/Sysban.pm
  • cgi-bin/ljlib.pl
--------------------------------------------------------------------------------
diff -r 78a3513a71b5 -r 904af5fb25f0 bin/renameuser.pl
--- a/bin/renameuser.pl	Wed Dec 07 20:39:21 2011 +0800
+++ b/bin/renameuser.pl	Wed Dec 07 21:50:46 2011 +0800
@@ -156,8 +156,8 @@
     LJ::MemCache::delete("uidof:$from");
     LJ::MemCache::delete("uidof:$to");
 
-    LJ::procnotify_add("rename_user", { 'user' => $u->{'user'},
-                                        'userid' => $u->{'userid'} });
+    LJ::Procnotify::add( "rename_user", { user   => $u->user,
+                                          userid => $u->userid } );
 
     $dbh->do( "INSERT INTO renames (renid, auth, cartid, renuserid, fromuser, touser, rendate) ".
               "VALUES ( NULL, '[manual]', 0, ?, $qfrom, $qto, ? )",
diff -r 78a3513a71b5 -r 904af5fb25f0 cgi-bin/Apache/LiveJournal.pm
--- a/cgi-bin/Apache/LiveJournal.pm	Wed Dec 07 20:39:21 2011 +0800
+++ b/cgi-bin/Apache/LiveJournal.pm	Wed Dec 07 21:50:46 2011 +0800
@@ -305,7 +305,7 @@
     }
 
     LJ::start_request();
-    LJ::procnotify_check();
+    LJ::Procnotify::check();
     S2::set_domain('LJ');
 
     my $lang = $LJ::DEFAULT_LANG || $LJ::LANGS[0];
diff -r 78a3513a71b5 -r 904af5fb25f0 cgi-bin/DW/User/Rename.pm
--- a/cgi-bin/DW/User/Rename.pm	Wed Dec 07 20:39:21 2011 +0800
+++ b/cgi-bin/DW/User/Rename.pm	Wed Dec 07 21:50:46 2011 +0800
@@ -355,8 +355,8 @@
     DW::User::Rename::_clear_from_cache( $self, $fromusername, $tousername );
 
     # tell everything else that we renamed
-    LJ::procnotify_add( "rename_user", { user => $fromusername,
-                                         userid => $self->userid });
+    LJ::Procnotify::add( "rename_user", { user   => $fromusername,
+                                          userid => $self->userid } );
 
     $token->apply( userid => $self->userid, from => $fromusername, to => $tousername );
 
diff -r 78a3513a71b5 -r 904af5fb25f0 cgi-bin/LJ/Procnotify.pm
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/cgi-bin/LJ/Procnotify.pm	Wed Dec 07 21:50:46 2011 +0800
@@ -0,0 +1,118 @@
+# This code was forked from the LiveJournal project owned and operated
+# by Live Journal, Inc. The code has been modified and expanded by
+# Dreamwidth Studios, LLC. These files were originally licensed under
+# the terms of the license supplied by Live Journal, Inc, which can
+# currently be found at:
+#
+# http://code.livejournal.org/trac/livejournal/browser/trunk/LICENSE-LiveJournal.txt
+#
+# In accordance with the original license, this code and all its
+# modifications are provided under the GNU General Public License.
+# A copy of that license can be found in the LICENSE file included as
+# part of this distribution.
+
+package LJ::Procnotify;
+
+use strict;
+use lib "$ENV{LJHOME}/cgi-bin";
+require 'ljlib.pl';
+
+
+# <LJFUNC>
+# name: LJ::Procnotify::add
+# des: Sends a message to all other processes on all clusters.
+# info: You'll probably never use this yourself.
+# args: cmd, args?
+# des-cmd: Command name.  Currently recognized: "DBI::Role::reload" and "rename_user"
+# des-args: Hashref with key/value arguments for the given command.
+#           See relevant parts of [func[LJ::Procnotify::callback]], for
+#           required args for different commands.
+# returns: new serial number on success; 0 on fail.
+# </LJFUNC>
+sub add {
+    my ($cmd, $argref) = @_;
+    my $dbh = LJ::get_db_writer();
+    return 0 unless $dbh;
+
+    my $args = join('&', map { LJ::eurl($_) . "=" . LJ::eurl($argref->{$_}) }
+                    sort keys %$argref);
+    $dbh->do("INSERT INTO procnotify (cmd, args) VALUES (?,?)",
+             undef, $cmd, $args);
+
+    return 0 if $dbh->err;
+    return $dbh->{'mysql_insertid'};
+}
+
+# <LJFUNC>
+# name: LJ::Procnotify::callback
+# des: Call back function process notifications.
+# info: You'll probably never use this yourself.
+# args: cmd, argstring
+# des-cmd: Command name.
+# des-argstring: String of arguments.
+# returns: new serial number on success; 0 on fail.
+# </LJFUNC>
+sub callback {
+    my ($cmd, $argstring) = @_;
+    my $arg = {};
+    LJ::decode_url_string($argstring, $arg);
+
+    if ($cmd eq "rename_user") {
+        # this looks backwards, but the cache hash names are just odd:
+        delete $LJ::CACHE_USERNAME{$arg->{'userid'}};
+        delete $LJ::CACHE_USERID{$arg->{'user'}};
+        return;
+    }
+
+    # ip/uniq/spamreport bans
+    my %ban_types = (
+                     ip         => \%LJ::IP_BANNED,
+                     uniq       => \%LJ::UNIQ_BANNED,
+                     spamreport => \%LJ::SPAMREPORT_BANNED,
+                    );
+
+    if ( $cmd =~ /^ban_(\w+)$/ && exists $ban_types{$1} ) {
+        my $banarg = $arg->{$1};
+        $ban_types{$1}->{$banarg} = $arg->{exptime};
+        return;
+    }
+
+    if ( $cmd =~ /^unban_(\w+)$/ && exists $ban_types{$1} ) {
+        my $banarg = $arg->{$1};
+        delete $ban_types{$1}->{$banarg};
+        return;
+    }
+
+    # cluster switchovers
+    if ($cmd eq 'cluster_switch') {
+        $LJ::CLUSTER_PAIR_ACTIVE{ $arg->{'cluster'} } = $arg->{ 'role' };
+        return;
+    }
+}
+
+
+# each server process runs this to periodically check for new actions
+
+sub check {
+    my $now = time;
+    return if $LJ::CACHE_PROCNOTIFY_CHECK &&
+              $LJ::CACHE_PROCNOTIFY_CHECK + 30 > $now;
+    $LJ::CACHE_PROCNOTIFY_CHECK = $now;
+
+    my $dbr = LJ::get_db_reader();
+    my $max = $dbr->selectrow_array("SELECT MAX(nid) FROM procnotify");
+    return unless defined $max;
+    my $old = $LJ::CACHE_PROCNOTIFY_MAX;
+    if (defined $old && $max > $old) {
+        my $sth = $dbr->prepare("SELECT cmd, args FROM procnotify ".
+                                "WHERE nid > ? AND nid <= $max ORDER BY nid");
+        $sth->execute($old);
+        while (my ($cmd, $args) = $sth->fetchrow_array) {
+            LJ::Procnotify::callback( $cmd, $args );
+        }
+    }
+    $LJ::CACHE_PROCNOTIFY_MAX = $max;
+}
+
+
+1;
diff -r 78a3513a71b5 -r 904af5fb25f0 cgi-bin/LJ/Sysban.pm
--- a/cgi-bin/LJ/Sysban.pm	Wed Dec 07 20:39:21 2011 +0800
+++ b/cgi-bin/LJ/Sysban.pm	Wed Dec 07 21:50:46 2011 +0800
@@ -630,7 +630,7 @@
 
     my $procopts = { $what => $value, exptime => $until };
 
-    LJ::procnotify_add( "ban_$what", $procopts );
+    LJ::Procnotify::add( "ban_$what", $procopts );
     LJ::MemCache::delete( "sysban:$what" );
 
     return 1;
@@ -643,7 +643,7 @@
 
     my $procopts = { $what => $value };
 
-    LJ::procnotify_add( "unban_$what", $procopts );
+    LJ::Procnotify::add( "unban_$what", $procopts );
     LJ::MemCache::delete( "sysban:$what" );
 
     return 1;
diff -r 78a3513a71b5 -r 904af5fb25f0 cgi-bin/ljlib.pl
--- a/cgi-bin/ljlib.pl	Wed Dec 07 20:39:21 2011 +0800
+++ b/cgi-bin/ljlib.pl	Wed Dec 07 21:50:46 2011 +0800
@@ -89,6 +89,7 @@
 use DW::LatestFeed;
 use LJ::Support;
 use LJ::Keywords;
+use LJ::Procnotify;
 
 # make Unicode::MapUTF8 autoload:
 sub Unicode::MapUTF8::AUTOLOAD {
@@ -1413,100 +1414,6 @@
     return hex(substr($c, 1, 6));
 }
 
-# <LJFUNC>
-# name: LJ::procnotify_add
-# des: Sends a message to all other processes on all clusters.
-# info: You'll probably never use this yourself.
-# args: cmd, args?
-# des-cmd: Command name.  Currently recognized: "DBI::Role::reload" and "rename_user"
-# des-args: Hashref with key/value arguments for the given command.
-#           See relevant parts of [func[LJ::procnotify_callback]], for
-#           required args for different commands.
-# returns: new serial number on success; 0 on fail.
-# </LJFUNC>
-sub procnotify_add {
-    my ($cmd, $argref) = @_;
-    my $dbh = LJ::get_db_writer();
-    return 0 unless $dbh;
-
-    my $args = join('&', map { LJ::eurl($_) . "=" . LJ::eurl($argref->{$_}) }
-                    sort keys %$argref);
-    $dbh->do("INSERT INTO procnotify (cmd, args) VALUES (?,?)",
-             undef, $cmd, $args);
-
-    return 0 if $dbh->err;
-    return $dbh->{'mysql_insertid'};
-}
-
-# <LJFUNC>
-# name: LJ::procnotify_callback
-# des: Call back function process notifications.
-# info: You'll probably never use this yourself.
-# args: cmd, argstring
-# des-cmd: Command name.
-# des-argstring: String of arguments.
-# returns: new serial number on success; 0 on fail.
-# </LJFUNC>
-sub procnotify_callback
-{
-    my ($cmd, $argstring) = @_;
-    my $arg = {};
-    LJ::decode_url_string($argstring, $arg);
-
-    if ($cmd eq "rename_user") {
-        # this looks backwards, but the cache hash names are just odd:
-        delete $LJ::CACHE_USERNAME{$arg->{'userid'}};
-        delete $LJ::CACHE_USERID{$arg->{'user'}};
-        return;
-    }
-
-    # ip/uniq/spamreport bans
-    my %ban_types = (
-                     ip         => \%LJ::IP_BANNED,
-                     uniq       => \%LJ::UNIQ_BANNED,
-                     spamreport => \%LJ::SPAMREPORT_BANNED,
-                    );
-
-    if ( $cmd =~ /^ban_(\w+)$/ && exists $ban_types{$1} ) {
-        my $banarg = $arg->{$1};
-        $ban_types{$1}->{$banarg} = $arg->{exptime};
-        return;
-    }
-
-    if ( $cmd =~ /^unban_(\w+)$/ && exists $ban_types{$1} ) {
-        my $banarg = $arg->{$1};
-        delete $ban_types{$1}->{$banarg};
-        return;
-    }
-
-    # cluster switchovers
-    if ($cmd eq 'cluster_switch') {
-        $LJ::CLUSTER_PAIR_ACTIVE{ $arg->{'cluster'} } = $arg->{ 'role' };
-        return;
-    }
-}
-
-sub procnotify_check
-{
-    my $now = time;
-    return if $LJ::CACHE_PROCNOTIFY_CHECK + 30 > $now;
-    $LJ::CACHE_PROCNOTIFY_CHECK = $now;
-
-    my $dbr = LJ::get_db_reader();
-    my $max = $dbr->selectrow_array("SELECT MAX(nid) FROM procnotify");
-    return unless defined $max;
-    my $old = $LJ::CACHE_PROCNOTIFY_MAX;
-    if (defined $old && $max > $old) {
-        my $sth = $dbr->prepare("SELECT cmd, args FROM procnotify ".
-                                "WHERE nid > ? AND nid <= $max ORDER BY nid");
-        $sth->execute($old);
-        while (my ($cmd, $args) = $sth->fetchrow_array) {
-            LJ::procnotify_callback($cmd, $args);
-        }
-    }
-    $LJ::CACHE_PROCNOTIFY_MAX = $max;
-}
-
 # We're not always running under mod_perl... sometimes scripts (syndication sucker)
 # call paths which end up thinking they need the remote IP, but don't.
 sub get_remote_ip
--------------------------------------------------------------------------------