LJSUP-17669: Login.bml form refactoring
[livejournal.git] / cgi-bin / synlib.pl
blobccc766b2a6c7b2a1628e9d6bdff74589010af8b9
1 #!/usr/bin/perl
4 package LJ::Syn;
5 use strict;
7 #get_feeds_rating ( { offset => '', length => '', sort_by => '', search_name => '' } )
8 sub get_feeds_rating_segment {
9 my $args = shift;
10 my $offset = $args->{"offset"} || 0;
11 my $length = $args->{"length"} || 5;
12 my $sort_by = $args->{"sort_by"} || "soccap";
14 ##### Search mode if set search_name parameter ####
15 my $search_id;
16 my $search_name = $args->{"search_name"};
17 if ($search_name) {
18 my $user = LJ::load_user($search_name);
19 $search_id = $user->id() if LJ::isu($user);
21 ###################################################
23 my @data = @{_get_feeds_from_db()};
24 my $total = scalar @data;
26 my @uids = map { $_->[0]} @data;
27 my $users = LJ::User->get_authority_multi( \@uids );
28 foreach (@data) {
29 $_->[3] = $users->{$_->[0]};
32 ###### by default values sorting by number of watchers, if we are need sorting by social capital then do it.
33 @data = reverse sort { $a->[3] <=> $b->[3] } @data if $sort_by eq "soccap";
35 my $search_res;
36 my $position = 1;
37 foreach (@data) {
38 $_ = {
39 position => $position,
40 journal_id => $_->[0],
41 xml_url => $_->[1],
42 watchers => $_->[2],
43 soccap => $_->[3],
45 $position++;
47 ##### in search mode we are trying find needed position
48 if ( $_->{"journal_id"} == $search_id ) {
49 $search_res = $_->{"position"};
50 $_->{"found"} = 1;
51 $offset = POSIX::floor( $search_res / $length ) * $length;
55 my @data = splice(@data, $offset, $length);
57 my $result = \@data;
58 return { total => $total, data => $result, search_res => $search_res };
61 sub get_popular_feeds
63 my $popsyn = LJ::MemCache::get("popsyn");
64 unless ($popsyn) {
65 $popsyn = _get_feeds_from_db();
67 # load u objects so we can get usernames
68 my %users;
69 LJ::load_userids_multiple([ map { $_, \$users{$_} } map { $_->[0] } @$popsyn ]);
70 unshift @$_, $users{$_->[0]}->{'user'}, $users{$_->[0]}->{'name'} foreach @$popsyn;
71 # format is: [ user, name, userid, synurl, numreaders ]
72 # set in memcache
73 my $expire = time() + 3600; # 1 hour
74 LJ::MemCache::set("popsyn", $popsyn, $expire);
76 return $popsyn;
79 sub get_popular_feed_ids {
80 my $popsyn_ids = LJ::MemCache::get("popsyn_ids");
81 unless ($popsyn_ids) {
82 my $popsyn = _get_feeds_from_db();
83 @$popsyn_ids = map { $_->[0] } @$popsyn;
85 # set in memcache
86 my $expire = time() + 3600; # 1 hour
87 LJ::MemCache::set("popsyn_ids", $popsyn_ids, $expire);
89 return $popsyn_ids;
92 sub _get_feeds_from_db {
93 my $popsyn = [];
95 my $dbr = LJ::get_db_reader();
96 my $sth = $dbr->prepare("SELECT userid, synurl, numreaders FROM syndicated ".
97 "WHERE numreaders > 0 ".
98 "AND lastnew > DATE_SUB(NOW(), INTERVAL 14 DAY) ".
99 "ORDER BY numreaders DESC LIMIT 1000");
100 $sth->execute();
101 while (my @row = $sth->fetchrow_array) {
102 push @$popsyn, [ @row ];
105 return $popsyn;