fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-03-10 04:05 am

[dw-free] Need function to set current site scheme in DW::SiteScheme

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

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

Add functions to set site skin for the user, or just for the current
request. Update in various locations to use this.

Patch by [personal profile] exor674.

Files modified:
  • cgi-bin/DW/Controller/Dev.pm
  • cgi-bin/DW/SiteScheme.pm
  • cgi-bin/LJ/Setting/SiteScheme.pm
  • cgi-bin/LJ/User.pm
--------------------------------------------------------------------------------
diff -r 87b564b441db -r af46f441997b cgi-bin/DW/Controller/Dev.pm
--- a/cgi-bin/DW/Controller/Dev.pm	Thu Mar 10 11:27:54 2011 +0800
+++ b/cgi-bin/DW/Controller/Dev.pm	Thu Mar 10 12:05:10 2011 +0800
@@ -19,6 +19,7 @@ use strict;
 use strict;
 use warnings;
 use DW::Routing;
+use DW::SiteScheme;
 
 use JSON;
 
@@ -26,7 +27,9 @@ DW::Routing->register_static( '/dev/clas
 
 if ( $LJ::IS_DEV_SERVER ) {
     DW::Routing->register_string( '/dev/tests/index', \&tests_index_handler, app => 1 );
-    DW::Routing->register_regex( '/dev/tests/([^/]+)(?:/(.*))?', \&tests_handler, app => 1 )
+    DW::Routing->register_regex( '/dev/tests/([^/]+)(?:/(.*))?', \&tests_handler, app => 1 );
+
+    DW::Routing->register_string( '/dev/testhelper/jsondump', \&testhelper_json_handler, app => 1, format => "json" );
 }
 
 sub tests_index_handler {
@@ -34,7 +37,7 @@ sub tests_index_handler {
 
     my $r = DW::Request->get;
 
-    $r->note( bml_use_scheme => "global" );
+    DW::SiteScheme->set_for_request( 'global' );
     return DW::Template->render_template( "dev/tests-all.tt", {
         all_tests => [ map { $_ =~ m!tests/([^/]+)\.js!; } glob("$LJ::HOME/views/dev/tests/*.js") ]
     } );
@@ -50,7 +53,7 @@ sub tests_handler {
     if ( ! defined $lib ) {
         return $r->redirect("$LJ::SITEROOT/dev/tests/$test/");
     } elsif ( ! $lib ) {
-        $r->note( bml_use_scheme => "global" );
+        DW::SiteScheme->set_for_request( 'global' );
         return DW::Template->render_template( "dev/tests-all.tt", {
             test => $test,
         } );
@@ -81,7 +84,7 @@ sub tests_handler {
 
     # force a site scheme which only shows the bare content
     # but still prints out resources included using need_res
-    $r->note( bml_use_scheme => "global" );
+    DW::SiteScheme->set_for_request( 'global' );
 
     # we don't validate the test name, so be careful!
     return DW::Template->render_template( "dev/tests.tt", {
@@ -91,12 +94,6 @@ sub tests_handler {
             tests    => $testcontent,
             includes => \@includes,
          } );
-}
-
-
-# test helpers
-if ( $LJ::IS_DEV_SERVER ) {
-    DW::Routing->register_string( '/dev/testhelper/jsondump', \&testhelper_json_handler, app => 1, format => "json" )
 }
 
 sub testhelper_json_handler {
diff -r 87b564b441db -r af46f441997b cgi-bin/DW/SiteScheme.pm
--- a/cgi-bin/DW/SiteScheme.pm	Thu Mar 10 11:27:54 2011 +0800
+++ b/cgi-bin/DW/SiteScheme.pm	Thu Mar 10 12:05:10 2011 +0800
@@ -25,7 +25,7 @@ package DW::SiteScheme;
 package DW::SiteScheme;
 use strict;
 
-our %sitescheme_data = (
+my %sitescheme_data = (
     blueshift => { parent => 'common', title => "Blueshift" },
     celerity => { parent => 'common', title => "Celerity" },
     common => { parent => 'global', internal => 1 },
@@ -38,8 +38,15 @@ our %sitescheme_data = (
 
 my $data_loaded = 0;
 
-our @sitescheme_order = ();
+my @sitescheme_order = ();
 
+=head2 C<< DW::SiteScheme->get( $scheme ) >>
+
+$scheme defaults to the current sitescheme.
+
+Returns a DW::SiteScheme object.
+
+=cut
 sub get {
     my ( $class, $scheme ) = @_;
     $class->__load_data;
@@ -178,4 +185,65 @@ sub current {
         'global';
 }
 
+=head2 C<< DW::SiteScheme->set_for_request( $scheme ) >>
+
+Set the sitescheme for the request.
+
+Note: this must be called early enough in a request
+before calling into bml_handler for BML, or before render_template for TT
+otherwise has no action.
+
+=cut
+sub set_for_request {
+    my $r = DW::Request->get;
+
+    return 0 unless exists $sitescheme_data{$_[1]};
+    $r->note( 'bml_use_scheme', $_[1] );
+
+    return 1;
+}
+
+=head2 C<< DW::SiteScheme->set_for_user( $scheme, $u ) >>
+
+Set the sitescheme for the user.
+
+If $u does not exist, this will default to remote
+if $u ( or remote ) is undef, this will only set the cookie.
+
+Note: If done early enough in the process this will affect the current request.
+See the note on set_for_request
+
+=cut
+sub set_for_user {
+    my $r = DW::Request->get;
+
+    my $scheme = $_[1];
+    my $u = exists $_[2] ? $_[2] : LJ::get_remote();
+
+    return 0 unless exists $sitescheme_data{$scheme};
+    my $cval = $scheme;
+    if ( $scheme eq $sitescheme_order[0] && !$LJ::SAVE_SCHEME_EXPLICITLY ) {
+        $cval = undef;
+        $r->delete_cookie( domain  => ".$LJ::DOMAIN", name => 'BMLschemepref' );
+    }
+
+    my $expires = undef;
+    if ($u) {
+        # set a userprop to remember their schemepref
+        $u->set_prop( schemepref => $scheme );
+
+        # cookie expires when session expires
+        $expires = $u->{_session}->{timeexpire} if $u->{_session}->{exptype} eq "long";
+    }
+
+    $r->add_cookie(
+        name    => 'BMLschemepref',
+        value   => $cval,
+        expires => $expires,
+        domain  => ".$LJ::DOMAIN",
+    ) if $cval;
+
+    return 1;
+}
+
 1;
diff -r 87b564b441db -r af46f441997b cgi-bin/LJ/Setting/SiteScheme.pm
--- a/cgi-bin/LJ/Setting/SiteScheme.pm	Thu Mar 10 11:27:54 2011 +0800
+++ b/cgi-bin/LJ/Setting/SiteScheme.pm	Thu Mar 10 12:05:10 2011 +0800
@@ -99,33 +99,13 @@ sub save {
 
     my $r = DW::Request->get;
 
-    my $val = my $cval = $class->get_arg($args, "sitescheme");
+    my $val = $class->get_arg($args, "sitescheme");
     return 1 unless $val;
-    my @bml_schemes = DW::SiteScheme->available;
 
-    # don't set cookie for default scheme
-    if ($val eq $bml_schemes[0]->{scheme} && !$LJ::SAVE_SCHEME_EXPLICITLY) {
-        $cval = "";
-        $r->delete_cookie( domain  => ".$LJ::DOMAIN", name => 'BMLschemepref' );
+    unless ( DW::SiteScheme->set_for_user( $val, $u ) ) {
+        return 0;
     }
-
-    my $expires = undef;
-    if ($u) {
-        # set a userprop to remember their schemepref
-        $u->set_prop( schemepref => $val );
-
-        # cookie expires when session expires
-        $expires = $u->{_session}->{timeexpire}
-            if $u->{_session}->{exptype} eq "long";
-    }
-
-    $r->add_cookie(
-        name    => 'BMLschemepref',
-        value   => $cval,
-        expires => $expires,
-        domain  => ".$LJ::DOMAIN",
-    ) if $cval;
-    BML::set_scheme($val);
+    BML::set_scheme( $val );
 
     return 1;
 }
diff -r 87b564b441db -r af46f441997b cgi-bin/LJ/User.pm
--- a/cgi-bin/LJ/User.pm	Thu Mar 10 11:27:54 2011 +0800
+++ b/cgi-bin/LJ/User.pm	Thu Mar 10 12:05:10 2011 +0800
@@ -43,6 +43,7 @@ use DW::User::ContentFilters;
 use DW::User::ContentFilters;
 use DW::User::Edges;
 use DW::InviteCodes::Promo;
+use DW::SiteScheme;
 
 use LJ::Community;
 use LJ::Subscription;
@@ -8813,7 +8814,7 @@ sub make_journal {
         # render it in the lynx site scheme.
         if ( $stylearg eq 'light' ) {
             $fallback = 'bml';
-            $r->note(bml_use_scheme => 'lynx');
+            DW::SiteScheme->set_for_request( 'lynx' );
         }
 
         # but if the user specifies which they want, override the fallback we picked
--------------------------------------------------------------------------------

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org