From bdec0944d2f948bd09d53613ab864c9bf435c4ff Mon Sep 17 00:00:00 2001 From: hylom Date: Sat, 22 Oct 2016 03:05:38 +0900 Subject: [PATCH] Model: add Skins --- src/newslash_web/lib/Newslash/Model/Skins.pm | 113 +++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 src/newslash_web/lib/Newslash/Model/Skins.pm diff --git a/src/newslash_web/lib/Newslash/Model/Skins.pm b/src/newslash_web/lib/Newslash/Model/Skins.pm new file mode 100644 index 00000000..e7cec98e --- /dev/null +++ b/src/newslash_web/lib/Newslash/Model/Skins.pm @@ -0,0 +1,113 @@ +package Newslash::Model::Skins; +use Newslash::Model::Base -base; + +use URI (); + +## WORNING: +## This is legacy from slash. deprecated, and you can use only backword-compatible. + +use constant MAINPAGE_SKID => 1; + +######################################################## +# Allow lookup of a skin by either numeric primary key ID, +# or by name. skins.name is a unique column so that will +# not present a problem. +sub getSkin { + my($self, $skid, $options) = @_; + + if (!$skid) { + #if ($ENV{GATEWAY_INTERFACE}) { + # #errorLog("cannot getSkin for empty skid"); + #} + #$skid = getCurrentStatic('mainpage_skid'); + $skid = MAINPAGE_SKID; +; + } + my $skins = $self->getSkins($options); + if ($skid !~ /^\d+$/) { + for my $id (sort keys %$skins) { + next unless (exists($skins->{$id}{name})); + if ($skins->{$id}{name} eq $skid) { + return $skins->{$id}; + } + } + return undef; + } + return $skins->{$skid}; +} + +######################################################## +sub getSkins { + my($self, $options) = @_; + my $db = $self->new_instance_of('LegacyDB'); + + my $ref_func = sub{ + my($self, $options) = @_; + #my $constants = getCurrentStatic(); + + #my $colors = $self->sqlSelectAllHashref([qw( skid name )], "*", "skin_colors", "", "GROUP BY skid, name"); + #my $skins_ref = $self->sqlSelectAllHashref( "skid", "*", "skins"); + my $colors = $db->sqlSelectAllHashref([qw( skid name )], "*", "skin_colors", "", "GROUP BY skid, name"); + my $skins_ref = $db->sqlSelectAllHashref( "skid", "*", "skins"); + #if (my $regex = $constants->{debughash_getSkins}) { + # $skins_ref = debugHash($regex, $skins_ref); + #} + + for my $skid (keys %$skins_ref) { + # Set rootdir etc., based on hostname/url, or mainpage's if none + #my $host_skid = $skins_ref->{$skid}{hostname} ? $skid : $constants->{mainpage_skid}; + #my $url_skid = $skins_ref->{$skid}{url} ? $skid : $constants->{mainpage_skid}; + #my $color_skid = $colors->{$skid} ? $skid : $constants->{mainpage_skid}; + my $host_skid = $skins_ref->{$skid}{hostname} ? $skid : MAINPAGE_SKID; + my $url_skid = $skins_ref->{$skid}{url} ? $skid : MAINPAGE_SKID; + my $color_skid = $colors->{$skid} ? $skid : MAINPAGE_SKID; + + # Blank index_handler defaults to index.pl. + $skins_ref->{$skid}{index_handler} ||= 'index.pl'; + + # Adjust min and max and warn if wacky value. + $skins_ref->{$skid}{artcount_max} = $skins_ref->{$skid}{artcount_min} + if $skins_ref->{$skid}{artcount_max} < $skins_ref->{$skid}{artcount_min}; + #warn "skin $skid has artcount_max of 0" if !$skins_ref->{$skid}{artcount_max}; + $self->warn("skin $skid has artcount_max of 0") if !$skins_ref->{$skid}{artcount_max}; + + # Convert an index_handler of foo.pl to an index_static of + # foo.shtml, for convenience. + ($skins_ref->{$skid}{index_static} = $skins_ref->{$skid}{index_handler}) =~ s/\.pl$/.shtml/; + + # Massage the skin_colors data into this hashref in an + # appropriate place. + for my $name (keys %{$colors->{$color_skid}}) { + $skins_ref->{$skid}{skincolors}{$name} = $colors->{$color_skid}{$name}{skincolor}; + } + + $skins_ref->{$skid}{basedomain} = $skins_ref->{$host_skid}{hostname}; + + $skins_ref->{$skid}{absolutedir} = $skins_ref->{$url_skid}{url} + || "http://$skins_ref->{$skid}{basedomain}"; + $skins_ref->{$skid}{absolutedir} =~ s{/+$}{}; + + my $rootdir_uri = URI->new($skins_ref->{$skid}{absolutedir}); + + $rootdir_uri->scheme(''); + $skins_ref->{$skid}{rootdir} = $rootdir_uri->as_string; + $skins_ref->{$skid}{rootdir} =~ s{/+$}{}; + #if (!$skins_ref->{$skid}{rootdir}) { print STDERR scalar(localtime) . " MySQL.pm No rootdir for skid $skid hostname $skins_ref->{$skid}{hostname}\n" } + + # XXXSKIN - untested; can we reuse $rootdir_uri ? + #if ($constants->{use_https_for_absolutedir_secure}) { + # $rootdir_uri->scheme('https'); + # $skins_ref->{$skid}{absolutedir_secure} = $rootdir_uri->as_string; + # $skins_ref->{$skid}{absolutedir_secure} =~ s{/+$}{}; + #} else { + $skins_ref->{$skid}{absolutedir_secure} = $skins_ref->{$skid}{absolutedir}; + #} + } + return $skins_ref; + }; + #return $self->cache_local_and_memcached('getSkins', $ref_func); + return $ref_func($options); +} + + +1; -- 2.11.0