5 # name: LJ::load_mood_theme
6 # des: Loads and caches a mood theme, or returns immediately if already loaded.
7 # args: dbarg?, themeid
8 # des-themeid: the mood theme ID to load.
14 return if $LJ::CACHE_MOOD_THEME
{$themeid};
15 return unless $themeid;
18 my $memkey = [$themeid, "moodthemedata:$themeid"];
19 return if $LJ::CACHE_MOOD_THEME
{$themeid} = LJ
::MemCache
::get
($memkey) and
20 %{$LJ::CACHE_MOOD_THEME
{$themeid} || {}};
23 my $dbh = LJ
::get_db_writer
()
26 $LJ::CACHE_MOOD_THEME
{$themeid} = {};
28 my $sth = $dbh->prepare("SELECT moodid, picurl, width, height FROM moodthemedata WHERE moodthemeid=?");
29 $sth->execute($themeid);
30 return 0 if $dbh->err;
32 while (my ($id, $pic, $w, $h) = $sth->fetchrow_array) {
33 $LJ::CACHE_MOOD_THEME
{$themeid}->{$id} = { 'pic' => $pic, 'w' => $w, 'h' => $h };
36 my $des_sth = $dbh->prepare("SELECT name, des FROM moodthemes WHERE moodthemeid=?");
37 $des_sth->execute($themeid);
38 return 0 if $dbh->err;
40 my ($name, $des) = $des_sth->fetchrow_array;
41 $LJ::CACHE_MOOD_THEME
{$themeid}->{name
} = $name;
42 $LJ::CACHE_MOOD_THEME
{$themeid}->{des
} = $des;
45 LJ
::MemCache
::set
($memkey, $LJ::CACHE_MOOD_THEME
{$themeid}, 3600)
46 if %{$LJ::CACHE_MOOD_THEME
{$themeid} || {}};
52 # name: LJ::load_moods
62 return if $LJ::CACHED_MOODS
;
63 my $dbr = LJ
::get_db_reader
();
64 my $sth = $dbr->prepare("SELECT moodid, mood, parentmood FROM moods");
66 while (my ($id, $mood, $parent) = $sth->fetchrow_array) {
67 $LJ::CACHE_MOODS
{$id} = { 'name' => $mood, 'parent' => $parent, 'id' => $id };
68 if ($id > $LJ::CACHED_MOOD_MAX
) { $LJ::CACHED_MOOD_MAX
= $id; }
70 $LJ::CACHED_MOODS
= 1;
74 # name: LJ::get_mood_picture
75 # des: Loads a mood icon hashref given a themeid and moodid.
76 # args: themeid, moodid, ref
77 # des-themeid: Integer; mood themeid.
78 # des-moodid: Integer; mood id.
79 # des-ref: Hashref to load mood icon data into.
80 # returns: Boolean; 1 on success, 0 otherwise.
84 my ($themeid, $moodid, $ref) = @_;
85 LJ
::load_mood_theme
($themeid) unless $LJ::CACHE_MOOD_THEME
{$themeid};
86 LJ
::load_moods
() unless $LJ::CACHED_MOODS
;
89 if ($LJ::CACHE_MOOD_THEME
{$themeid} &&
90 $LJ::CACHE_MOOD_THEME
{$themeid}->{$moodid}) {
91 %{$ref} = %{$LJ::CACHE_MOOD_THEME
{$themeid}->{$moodid}};
92 if ($ref->{'pic'} =~ m!^/!) {
93 $ref->{'pic'} =~ s!^/img!!;
94 $ref->{'pic'} = $LJ::IMGPREFIX
. $ref->{'pic'};
96 $ref->{'pic'} = "#invalid" unless
97 $ref->{'pic'} =~ m!^https?://[^\'\"\0\s]+$!;
98 $ref->{'moodid'} = $moodid;
101 $moodid = (defined $LJ::CACHE_MOODS
{$moodid} ?
102 $LJ::CACHE_MOODS
{$moodid}->{'parent'} : 0);
109 sub mood_size_attributes
{
113 if ($pic{'w'} && $pic{'h'}) {
114 push @attrs, "width='$pic{'w'}'";
115 push @attrs, "height='$pic{'h'}'";
116 push @attrs, qq{class="$pic{class}"} if $pic{'class'};
118 my @classes = qw(meta-mood-img);
119 push @classes, $pic{'class'} if $pic{'class'};
120 my $classes = join(' ', @classes);
121 push @attrs, qq{class="$classes"};
124 return join(' ', @attrs);
128 # mood id to name (or undef)
132 LJ
::load_moods
() unless $LJ::CACHED_MOODS
;
133 my $m = $LJ::CACHE_MOODS
{$moodid};
134 return $m ?
$m->{'name'} : undef;
141 LJ
::load_mood_theme
($themeid);
142 my $m = $LJ::CACHE_MOOD_THEME
{$themeid};
143 return $m ?
$m->{'des'} : undef;
146 # mood name to id (or undef)
150 return undef unless $mood;
151 LJ
::load_moods
() unless $LJ::CACHED_MOODS
;
152 foreach my $m (values %LJ::CACHE_MOODS
) {
153 return $m->{'id'} if $mood eq LJ
::Lang
::ml
("mood.$m->{'name'}");
160 LJ
::load_moods
() unless $LJ::CACHED_MOODS
;
161 return \
%LJ::CACHE_MOODS
;