LJSUP-17669: Login.bml form refactoring
[livejournal.git] / cgi-bin / ljmood.pl
blobe22916873e4b6c5cdedd3e6dd8c3b12e2681b459
1 package LJ;
2 use strict;
4 # <LJFUNC>
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.
9 # </LJFUNC>
10 sub load_mood_theme
12 &nodb;
13 my $themeid = shift;
14 return if $LJ::CACHE_MOOD_THEME{$themeid};
15 return unless $themeid;
17 # check memcache
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} || {}};
22 # fall back to db
23 my $dbh = LJ::get_db_writer()
24 or return 0;
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;
44 # set in memcache
45 LJ::MemCache::set($memkey, $LJ::CACHE_MOOD_THEME{$themeid}, 3600)
46 if %{$LJ::CACHE_MOOD_THEME{$themeid} || {}};
48 return 1;
51 # <LJFUNC>
52 # name: LJ::load_moods
53 # class:
54 # des:
55 # info:
56 # args:
57 # des-:
58 # returns:
59 # </LJFUNC>
60 sub 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");
65 $sth->execute;
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;
73 # <LJFUNC>
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.
81 # </LJFUNC>
82 sub get_mood_picture
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;
99 return 1;
100 } else {
101 $moodid = (defined $LJ::CACHE_MOODS{$moodid} ?
102 $LJ::CACHE_MOODS{$moodid}->{'parent'} : 0);
105 while ($moodid);
106 return 0;
109 sub mood_size_attributes {
110 my %pic = @_;
112 my @attrs;
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'};
117 } else {
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)
129 sub mood_name
131 my ($moodid) = @_;
132 LJ::load_moods() unless $LJ::CACHED_MOODS;
133 my $m = $LJ::CACHE_MOODS{$moodid};
134 return $m ? $m->{'name'} : undef;
137 # mood id to desc
138 sub mood_theme_des
140 my ($themeid) = @_;
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)
147 sub mood_id
149 my ($mood) = @_;
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'}");
155 return undef;
158 sub get_moods
160 LJ::load_moods() unless $LJ::CACHED_MOODS;
161 return \%LJ::CACHE_MOODS;