fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2010-06-21 09:25 am

[dw-free] Declaration of which themes are default/should go in the base category, is duplicated

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

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

Deduplication of logic: no need to declare a separate "base" category --
instead, use the known default_themes, to determine which theme to display
for each layout under the "Base Layouts" category.

Patch by [personal profile] fu.

Files modified:
  • cgi-bin/LJ/S2Theme.pm
  • cgi-bin/LJ/S2Theme/bases.pm
  • cgi-bin/LJ/S2Theme/basicboxes.pm
  • cgi-bin/LJ/S2Theme/blanket.pm
  • cgi-bin/LJ/S2Theme/boxesandborders.pm
  • cgi-bin/LJ/S2Theme/brittle.pm
  • cgi-bin/LJ/S2Theme/core2base.pm
  • cgi-bin/LJ/S2Theme/crossroads.pm
  • cgi-bin/LJ/S2Theme/drifting.pm
  • cgi-bin/LJ/S2Theme/easyread.pm
  • cgi-bin/LJ/S2Theme/fluidmeasure.pm
  • cgi-bin/LJ/S2Theme/funkycircles.pm
  • cgi-bin/LJ/S2Theme/modular.pm
  • cgi-bin/LJ/S2Theme/negatives.pm
  • cgi-bin/LJ/S2Theme/nouveauoleanders.pm
  • cgi-bin/LJ/S2Theme/refriedtablet.pm
  • cgi-bin/LJ/S2Theme/skittlishdreams.pm
  • cgi-bin/LJ/S2Theme/steppingstones.pm
  • cgi-bin/LJ/S2Theme/tranquilityiii.pm
  • cgi-bin/LJ/S2Theme/zesty.pm
  • cgi-bin/LJ/Widget/ThemeChooser.pm
--------------------------------------------------------------------------------
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme.pm
--- a/cgi-bin/LJ/S2Theme.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -28,25 +28,12 @@ sub init {
 # Class Methods
 ##################################################
 
-# returns the uniq of the default theme for the given layout id or uniq (for lazy migration)
-sub default_theme {
-    my $class = shift;
-    my $layout = shift;
-    my %opts = @_;
+sub default_themes {
+    my $class = $_[0];
 
-    # turn the given $layout into a uniq if it's an id
-    my $pub = LJ::S2::get_public_layers();
-    if ($layout =~ /^\d+$/) {
-        $layout = $pub->{$layout}->{uniq};
-    }
+    my %default_themes;
 
-    # return if this is a custom layout
-    return "" unless ref $pub->{$layout};
-
-    # remove the /layout part of the uniq to just get the layout name
-    $layout =~ s/\/layout$//;
-
-    my %default_themes = (
+    %default_themes = (
         bases => 'bases/tropical',
         basicboxes => 'basicboxes/green',
         blanket => 'blanket/peach',
@@ -68,8 +55,35 @@ sub default_theme {
         zesty => 'zesty/white',
     );
 
-    my %local_default_themes = eval "use LJ::S2Theme_local; 1;" ? $class->local_default_themes($layout, %opts) : ();
-    my $default_theme = $default_themes{$layout} || $local_default_themes{$layout};
+    my %local_default_themes = eval "use LJ::S2Theme_local; 1;"
+        ? $class->local_default_themes
+        : ();
+
+    %default_themes = ( %default_themes, %local_default_themes );
+
+    return %default_themes;
+}
+
+# returns the uniq of the default theme for the given layout id or uniq (for lazy migration)
+sub default_theme {
+    my $class = shift;
+    my $layout = shift;
+    my %opts = @_;
+
+    # turn the given $layout into a uniq if it's an id
+    my $pub = LJ::S2::get_public_layers();
+    if ($layout =~ /^\d+$/) {
+        $layout = $pub->{$layout}->{uniq};
+    }
+
+    # return if this is a custom layout
+    return "" unless ref $pub->{$layout};
+
+    # remove the /layout part of the uniq to just get the layout name
+    $layout =~ s/\/layout$//;
+
+    my %default_themes = $class->default_themes;
+    my $default_theme = $default_themes{$layout};
     die "Default theme for layout $layout does not exist." unless $default_theme;
     return $default_theme;
 }
@@ -175,9 +189,24 @@ sub load_default_of {
     return $default_theme ? $class->load_by_uniq($default_theme) : undef;
 }
 
+sub load_default_themes {
+    my $class = $_[0];
+
+    my @themes;
+
+    my %default_themes = $class->default_themes;
+    return unless %default_themes;
+
+    foreach my $uniq ( values %default_themes ) {
+        my $theme = $class->load_by_uniq( $uniq, silent_failure => 1 );
+        push @themes, $theme if $theme;
+    }
+
+    return @themes;
+}
+
 sub load_by_uniq {
-    my $class = shift;
-    my $uniq = shift;
+    my ( $class, $uniq, %opts ) = @_;
 
     my $pub = LJ::S2::get_public_layers();
     if ($pub->{$uniq} && $pub->{$uniq}->{type} eq "theme") {
@@ -186,7 +215,13 @@ sub load_by_uniq {
         return $class->load_by_layoutid($pub->{$uniq}->{s2lid});
     }
 
-    die "Given uniq is not a valid layout or theme: $uniq";
+    my $msg = "Given uniq is not a valid layout or theme: $uniq";
+    if ( $opts{silent_failure} ) {
+        warn $msg;
+        return undef;
+    } else {
+        die $msg;
+    }
 }
 
 sub load_by_cat {
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/bases.pm
--- a/cgi-bin/LJ/S2Theme/bases.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/bases.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -33,7 +33,7 @@ sub designer { "twtd" }
 
 package LJ::S2Theme::bases::tropical;
 use base qw( LJ::S2Theme::bases );
-sub cats { qw( base ) }
+sub cats { qw() }
 
 package LJ::S2Theme::bases::summerholiday;
 use base qw( LJ::S2Theme::bases );
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/basicboxes.pm
--- a/cgi-bin/LJ/S2Theme/basicboxes.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/basicboxes.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -26,7 +26,7 @@ sub designer { "kareila" }
 
 package LJ::S2Theme::basicboxes::green;
 use base qw( LJ::S2Theme::basicboxes );
-sub cats { qw( base ) }
+sub cats { qw() }
 
 package LJ::S2Theme::basicboxes::leaf;
 use base qw( LJ::S2Theme::basicboxes );
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/blanket.pm
--- a/cgi-bin/LJ/S2Theme/blanket.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/blanket.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -49,7 +49,7 @@ sub designer { "dancing_serpent" }
 
 package LJ::S2Theme::blanket::peach;
 use base qw( LJ::S2Theme::blanket );
-sub cats { qw( base ) }
+sub cats { qw() }
 
 package LJ::S2Theme::blanket::shallows;
 use base qw( LJ::S2Theme::blanket );
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/boxesandborders.pm
--- a/cgi-bin/LJ/S2Theme/boxesandborders.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/boxesandborders.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -17,7 +17,7 @@ sub cats { qw( ) }
 
 package LJ::S2Theme::boxesandborders::gray;
 use base qw( LJ::S2Theme::boxesandborders );
-sub cats { qw( base ) }
+sub cats { qw() }
 
 sub designer { "branchandroot" }
 
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/brittle.pm
--- a/cgi-bin/LJ/S2Theme/brittle.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/brittle.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -58,7 +58,7 @@ sub designer { "dancing_serpent" }
 
 package LJ::S2Theme::brittle::rust;
 use base qw( LJ::S2Theme::brittle );
-sub cats { qw( base ) }
+sub cats { qw() }
 
 package LJ::S2Theme::brittle::softblue;
 use base qw( LJ::S2Theme::brittle );
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/core2base.pm
--- a/cgi-bin/LJ/S2Theme/core2base.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/core2base.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -36,5 +36,5 @@ sub designer { "zvi" }
 
 package LJ::S2Theme::core2base::testing;
 use base qw( LJ::S2Theme::core2base );
-sub cats { qw( base )}
+sub cats { qw()}
 1;
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/crossroads.pm
--- a/cgi-bin/LJ/S2Theme/crossroads.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/crossroads.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -36,7 +36,7 @@ sub cats { qw( ) }
 
 package LJ::S2Theme::crossroads::lettuce;
 use base qw( LJ::S2Theme::crossroads );
-sub cats { qw( base ) }
+sub cats { qw() }
 
 package LJ::S2Theme::crossroads::lilac;
 use base qw( LJ::S2Theme::crossroads );
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/drifting.pm
--- a/cgi-bin/LJ/S2Theme/drifting.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/drifting.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -1,7 +1,7 @@ package LJ::S2Theme::drifting;
 package LJ::S2Theme::drifting;
 use base qw(LJ::S2Theme);
 
-sub cats { qw( base ) }
+sub cats { qw() }
 sub designer { "Jennie Griner" }
 
 sub layouts { ( "2l" => "two-columns-left" ) }
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/easyread.pm
--- a/cgi-bin/LJ/S2Theme/easyread.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/easyread.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -13,7 +13,7 @@ sub designer { "zvi" }
 
 package LJ::S2Theme::easyread::green;
 use base qw( LJ::S2Theme::easyread );
-sub cats { qw( base featured ) }
+sub cats { qw( featured ) }
 
 package LJ::S2Theme::easyread::hcblack;
 use base qw( LJ::S2Theme::easyread );
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/fluidmeasure.pm
--- a/cgi-bin/LJ/S2Theme/fluidmeasure.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/fluidmeasure.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -42,7 +42,7 @@ sub designer { "dancing_serpent" }
 
 package LJ::S2Theme::fluidmeasure::spice;
 use base qw( LJ::S2Theme::fluidmeasure );
-sub cats { qw( base ) }
+sub cats { qw() }
 
 package LJ::S2Theme::fluidmeasure::summerdark;
 use base qw( LJ::S2Theme::fluidmeasure );
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/funkycircles.pm
--- a/cgi-bin/LJ/S2Theme/funkycircles.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/funkycircles.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -53,7 +53,7 @@ sub cats { qw( ) }
 
 package LJ::S2Theme::funkycircles::darkpurple;
 use base qw( LJ::S2Theme::funkycircles );
-sub cats { qw( base ) }
+sub cats { qw() }
 
 package LJ::S2Theme::funkycircles::earthygreen;
 use base qw( LJ::S2Theme::funkycircles );
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/modular.pm
--- a/cgi-bin/LJ/S2Theme/modular.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/modular.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -32,7 +32,7 @@ sub cats { qw ( ) }
 
 package LJ::S2Theme::modular::mediterraneanpeach;
 use base qw( LJ::S2Theme::modular );
-sub cats { qw ( base ) }
+sub cats { qw () }
 
 package LJ::S2Theme::modular::olivetree;
 use base qw( LJ::S2Theme::modular );
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/negatives.pm
--- a/cgi-bin/LJ/S2Theme/negatives.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/negatives.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -14,7 +14,7 @@ sub designer { "daven" }
 
 package LJ::S2Theme::negatives::black;
 use base qw( LJ::S2Theme::negatives );
-sub cats { qw( base ) }
+sub cats { qw() }
 
 package LJ::S2Theme::negatives::blastedsands;
 use base qw( LJ::S2Theme::negatives );
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/nouveauoleanders.pm
--- a/cgi-bin/LJ/S2Theme/nouveauoleanders.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/nouveauoleanders.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -25,7 +25,7 @@ sub cats { qw( featured ) }
 
 package LJ::S2Theme::nouveauoleanders::sienna;
 use base qw( LJ::S2Theme::nouveauoleanders );
-sub cats { qw( base ) }
+sub cats { qw() }
 
 package LJ::S2Theme::nouveauoleanders::wisteria;
 use base qw( LJ::S2Theme::nouveauoleanders );
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/refriedtablet.pm
--- a/cgi-bin/LJ/S2Theme/refriedtablet.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/refriedtablet.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -30,7 +30,7 @@ sub designer { "dancing_serpent" }
 
 package LJ::S2Theme::refriedtablet::refriedclassic;
 use base qw( LJ::S2Theme::refriedtablet );
-sub cats { qw( base ) }
+sub cats { qw() }
 
 package LJ::S2Theme::refriedtablet::refriedjewels;
 use base qw( LJ::S2Theme::refriedtablet );
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/skittlishdreams.pm
--- a/cgi-bin/LJ/S2Theme/skittlishdreams.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/skittlishdreams.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -33,7 +33,7 @@ sub cats { qw() }
 
 package LJ::S2Theme::skittlishdreams::orange;
 use base qw( LJ::S2Theme::skittlishdreams );
-sub cats { qw( base ) }
+sub cats { qw() }
 
 package LJ::S2Theme::skittlishdreams::pink;
 use base qw( LJ::S2Theme::skittlishdreams );
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/steppingstones.pm
--- a/cgi-bin/LJ/S2Theme/steppingstones.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/steppingstones.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -39,7 +39,7 @@ sub cats { qw( ) }
 
 package LJ::S2Theme::steppingstones::purple;
 use base qw( LJ::S2Theme::steppingstones );
-sub cats { qw( base ) }
+sub cats { qw() }
 
 package LJ::S2Theme::steppingstones::shadows;
 use base qw( LJ::S2Theme::steppingstones );
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/tranquilityiii.pm
--- a/cgi-bin/LJ/S2Theme/tranquilityiii.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/tranquilityiii.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -32,7 +32,7 @@ sub designer { "dancing_serpent" }
 
 package LJ::S2Theme::tranquilityiii::nightsea;
 use base qw( LJ::S2Theme::tranquilityiii );
-sub cats { qw( base ) }
+sub cats { qw() }
 
 package LJ::S2Theme::tranquilityiii::nnwm2009;
 use base qw( LJ::S2Theme::tranquilityiii );
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/S2Theme/zesty.pm
--- a/cgi-bin/LJ/S2Theme/zesty.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/S2Theme/zesty.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -1,7 +1,7 @@ package LJ::S2Theme::zesty;
 package LJ::S2Theme::zesty;
 use base qw( LJ::S2Theme );
 
-sub cats { qw( base ) }
+sub cats { qw() }
 sub designer { "exampleusername" }
 
 1;
diff -r 9d070efc2781 -r 138f3a485251 cgi-bin/LJ/Widget/ThemeChooser.pm
--- a/cgi-bin/LJ/Widget/ThemeChooser.pm	Thu Jun 17 22:17:09 2010 +0800
+++ b/cgi-bin/LJ/Widget/ThemeChooser.pm	Mon Jun 21 17:31:26 2010 +0800
@@ -58,6 +58,9 @@ sub render_body {
     } elsif ($cat eq "custom") {
         push @getargs, "cat=custom";
         @themes = LJ::S2Theme->load_by_user($u);
+    } elsif ($cat eq "base") {
+        push @getargs, "cat=base";
+        @themes = LJ::S2Theme->load_default_themes();
     } elsif ($cat) {
         push @getargs, "cat=$cat";
         @themes = LJ::S2Theme->load_by_cat($cat);
@@ -81,10 +84,8 @@ sub render_body {
     @themes = LJ::Customize->remove_duplicate_themes(@themes);
 
     if ( $cat eq "base" ) {
-        # sort themes with custom at the end, then alphabetically by layout
-        @themes =
-            sort { $a->is_custom <=> $b->is_custom }
-            sort { lc $a->layout_name cmp lc $b->layout_name } @themes;
+        # sort alphabetically by layout
+        @themes = sort { lc $a->layout_name cmp lc $b->layout_name } @themes;
     } else {
         # sort themes with custom at the end, then alphabetically by theme
         @themes =
--------------------------------------------------------------------------------