[dw-free] Refactor DW::SiteScheme / remove some duplicated cdoe
[commit: http://hg.dwscoalition.org/dw-free/rev/cd67817249a4]
http://bugs.dwscoalition.org/show_bug.cgi?id=3409
Refactor DW::SiteScheme to reduce code duplication, and pull in some of the
related code that's scattered across different modules.
Patch by
exor674.
Files modified:
http://bugs.dwscoalition.org/show_bug.cgi?id=3409
Refactor DW::SiteScheme to reduce code duplication, and pull in some of the
related code that's scattered across different modules.
Patch by
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Files modified:
- cgi-bin/DW/SiteScheme.pm
- cgi-bin/LJ/PageStats.pm
- cgi-bin/LJ/S2.pm
- cgi-bin/LJ/Setting/SiteScheme.pm
- cgi-bin/weblib.pl
-------------------------------------------------------------------------------- diff -r bddd722b57e1 -r cd67817249a4 cgi-bin/DW/SiteScheme.pm --- a/cgi-bin/DW/SiteScheme.pm Mon Jan 24 14:44:27 2011 +0800 +++ b/cgi-bin/DW/SiteScheme.pm Mon Jan 24 16:16:14 2011 +0800 @@ -7,7 +7,7 @@ # Authors: # Andrea Nall <anall@andreanall.com> # -# Copyright (c) 2010 by Dreamwidth Studios, LLC. +# Copyright (c) 2010-2011 by Dreamwidth Studios, LLC. # # This program is free software; you may redistribute it and/or modify it under # the same terms as Perl itself. For a copy of the license, please reference @@ -25,38 +25,65 @@ package DW::SiteScheme; package DW::SiteScheme; use strict; -my %sitescheme_inheritance = ( - blueshift => 'common', - celerity => 'common', - common => 'global', - 'gradation-horizontal' => 'common', - 'gradation-vertical' => 'common', - lynx => 'common', +our %sitescheme_data = ( + blueshift => { parent => 'common', title => "Blueshift" }, + celerity => { parent => 'common', title => "Celerity" }, + common => { parent => 'global', internal => 1 }, + 'gradation-horizontal' => { parent => 'common', title => "Gradation Horizontal" }, + 'gradation-vertical' => { parent => 'common', title => "Gradation Vertical" }, + lynx => { parent => 'common', title => "Lynx (light mode)" }, ); -my $default_sitescheme = "blueshift"; +my $data_loaded = 0; -eval "use DW::SiteScheme::Local;"; +our @sitescheme_order = (); -# no POD, should only be called from Local module -# DW::SiteScheme->register_siteschemes( foo => 'bar' ); -sub register_siteschemes { - my ( $self, %data ) = @_; - %sitescheme_inheritance = ( - %sitescheme_inheritance, - %data - ); +sub __load_data { + return if $data_loaded; + $data_loaded = 1; + + # function to merge additional site schemes into our base site scheme data + # new site scheme row overwrites original site schemes, if there is a conflict + my $merge_data = sub { + my ( %data ) = @_; + + foreach my $k ( keys %data ) { + $sitescheme_data{$k} = { %{ $sitescheme_data{$k} || {} }, %{ $data{$k} } }; + } + }; + + my @schemes = @LJ::SCHEMES; + + LJ::Hooks::run_hooks( 'modify_scheme_list', \@schemes, $merge_data ); + + # take the final site scheme list (after all modificatios) + foreach my $row ( @schemes ) { + my $scheme = $row->{scheme}; + + # copy over any information from the modified scheme list + # into the site scheme data + my $targ = ( $sitescheme_data{$scheme} ||= {} ); + foreach my $k ( keys %$row ) { + $targ->{$k} = $row->{$k}; + } + next if $targ->{disabled}; + + # and then add it to the list of site schemes + push @sitescheme_order, $scheme; + } } -# no POD, should only be called from Local module -# DW::SiteScheme->register_siteschemes( 'foo' ); -sub register_default_sitescheme { - $default_sitescheme = $_[1]; +=head2 C<< DW::SiteScheme->available >> + +=cut +sub available { + $_[0]->__load_data; + return map { $sitescheme_data{$_} } @sitescheme_order; } -=head2 C<< DW::SiteScheme->determine_current_sitescheme >> +=head2 C<< DW::SiteScheme->current >> -Determine the siteschehe, using the following in order: +Get the user's current sitescheme, using the following in order: =over @@ -66,7 +93,7 @@ Determine the siteschehe, using the foll =item BMLschemepref cookie -=item Default sitescheme +=item Default sitescheme ( first sitescheme in sitescheme_order ) =item 'global' @@ -74,8 +101,9 @@ Determine the siteschehe, using the foll =cut -sub determine_current_sitescheme { +sub current { my $r = DW::Request->get; + $_[0]->__load_data; my $rv; @@ -84,12 +112,13 @@ sub determine_current_sitescheme { $r->get_args->{usescheme} || $r->cookie( 'BMLschemepref' ); } + return $rv || - $default_sitescheme || + $sitescheme_order[0] || 'global'; } -=head2 C<< DW::SiteScheme->get_sitescheme_inheritance( $scheme ) >> +=head2 C<< DW::SiteScheme->inheritance( $scheme ) >> Scheme defaults to the current sitescheme. @@ -97,13 +126,16 @@ Returns the inheritance array, with the =cut -sub get_sitescheme_inheritance { +sub inheritance { my ( $self, $scheme ) = @_; - $scheme ||= $self->determine_current_sitescheme; + $self->__load_data; + + $scheme ||= $self->current; + my @scheme; push @scheme, $scheme; - push @scheme, $scheme while ( $scheme = $sitescheme_inheritance{$scheme} ); + push @scheme, $scheme while ( $scheme = $sitescheme_data{$scheme}->{parent} ); return @scheme; } -1; \ No newline at end of file +1; diff -r bddd722b57e1 -r cd67817249a4 cgi-bin/LJ/PageStats.pm --- a/cgi-bin/LJ/PageStats.pm Mon Jan 24 14:44:27 2011 +0800 +++ b/cgi-bin/LJ/PageStats.pm Mon Jan 24 16:16:14 2011 +0800 @@ -13,6 +13,7 @@ package LJ::PageStats; use strict; +use DW::SiteScheme; my $all_modules; @@ -218,12 +219,7 @@ sub groups { } sub scheme { - my ($self) = @_; - - my $scheme = BML::get_scheme(); - $scheme = (LJ::site_schemes())[0]->{'scheme'} unless $scheme; - - return $scheme; + return DW::SiteScheme->current; } sub language { diff -r bddd722b57e1 -r cd67817249a4 cgi-bin/LJ/S2.pm --- a/cgi-bin/LJ/S2.pm Mon Jan 24 14:44:27 2011 +0800 +++ b/cgi-bin/LJ/S2.pm Mon Jan 24 16:16:14 2011 +0800 @@ -903,7 +903,7 @@ sub siteviews_style { my $public = get_public_layers(); my $theme = "siteviews/default"; - foreach my $candidate ( DW::SiteScheme->get_sitescheme_inheritance ) { + foreach my $candidate ( DW::SiteScheme->inheritance ) { if ( $public->{"siteviews/$candidate"} ) { $theme = "siteviews/$candidate"; last; diff -r bddd722b57e1 -r cd67817249a4 cgi-bin/LJ/Setting/SiteScheme.pm --- a/cgi-bin/LJ/Setting/SiteScheme.pm Mon Jan 24 14:44:27 2011 +0800 +++ b/cgi-bin/LJ/Setting/SiteScheme.pm Mon Jan 24 16:16:14 2011 +0800 @@ -15,6 +15,7 @@ use base 'LJ::Setting'; use base 'LJ::Setting'; use strict; use warnings; +use DW::SiteScheme; sub should_render { my ($class, $u) = @_; @@ -40,7 +41,7 @@ sub option { my $r = DW::Request->get; - my @bml_schemes = LJ::site_schemes(); + my @bml_schemes = DW::SiteScheme->available; return "" unless @bml_schemes; my $show_hidden = $opts{getargs}->{view} && $opts{getargs}->{view} eq "schemes"; @@ -80,7 +81,7 @@ sub error_check { return 1 unless $val; my @scheme_names; - foreach my $scheme (LJ::site_schemes()) { + foreach my $scheme (DW::SiteScheme->available) { push @scheme_names, $scheme->{scheme}; } @@ -98,7 +99,7 @@ sub save { my $val = my $cval = $class->get_arg($args, "sitescheme"); return 1 unless $val; - my @bml_schemes = LJ::site_schemes(); + my @bml_schemes = DW::SiteScheme->available; # don't set cookie for default scheme if ($val eq $bml_schemes[0]->{scheme} && !$LJ::SAVE_SCHEME_EXPLICITLY) { diff -r bddd722b57e1 -r cd67817249a4 cgi-bin/weblib.pl --- a/cgi-bin/weblib.pl Mon Jan 24 14:44:27 2011 +0800 +++ b/cgi-bin/weblib.pl Mon Jan 24 16:16:14 2011 +0800 @@ -3805,20 +3805,6 @@ sub pageview_unique_string { return $uniq; } -# <LJFUNC> -# name: LJ::site_schemes -# class: web -# des: Returns a list of available BML schemes. -# args: none -# return: array -# </LJFUNC> -sub site_schemes { - my @schemes = @LJ::SCHEMES; - LJ::Hooks::run_hooks('modify_scheme_list', \@schemes); - @schemes = grep { !$_->{disabled} } @schemes; - return @schemes; -} - # sets up appropriate js for journals that need a special statusvis message at the top # returns some js that must be added onto the journal page's head sub statusvis_message_js { --------------------------------------------------------------------------------