1<?php
2// This file is part of BOINC.
3// http://boinc.berkeley.edu
4// Copyright (C) 2008 University of California
5//
6// BOINC is free software; you can redistribute it and/or modify it
7// under the terms of the GNU Lesser General Public License
8// as published by the Free Software Foundation,
9// either version 3 of the License, or (at your option) any later version.
10//
11// BOINC is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14// See the GNU Lesser General Public License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public License
17// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
18
19require_once("../inc/cache.inc");
20require_once("../inc/util.inc");
21require_once("../inc/user.inc");
22require_once("../inc/host.inc");
23require_once("../inc/boinc_db.inc");
24
25check_get_args(array("sort_by", "offset"));
26
27$config = get_config();
28$hosts_per_page = parse_config($config, "<hosts_per_page>");
29if (!$hosts_per_page) {
30    $hosts_per_page = 20;
31}
32define ('ITEM_LIMIT', 10000);
33
34function get_top_hosts($offset, $sort_by) {
35    global $hosts_per_page;
36    $db = BoincDb::get(true);
37    if ($sort_by == "total_credit") {
38        $sort_order = "total_credit desc";
39    } else {
40        $sort_order = "expavg_credit desc";
41    }
42    return BoincHost::enum(null, "order by $sort_order limit $offset, $hosts_per_page");
43}
44
45$sort_by = get_str("sort_by", true);
46switch ($sort_by) {
47case "total_credit":
48case "expavg_credit":
49    break;
50default:
51    $sort_by = "expavg_credit";
52}
53
54$offset = get_int("offset", true);
55if (!$offset) $offset=0;
56if ($offset % $hosts_per_page) $offset = 0;
57
58if ($offset >= ITEM_LIMIT) {
59    error_page(tra("Limit exceeded - Sorry, first %1 items only", ITEM_LIMIT));
60}
61
62$cache_args = "sort_by=$sort_by&offset=$offset";
63$cacheddata = get_cached_data(TOP_PAGES_TTL, $cache_args);
64if ($cacheddata){
65    $data = unserialize($cacheddata);
66} else {
67    $data = get_top_hosts($offset,$sort_by);
68    set_cached_data(TOP_PAGES_TTL, serialize($data), $cache_args);
69};
70
71
72// Now display what we've got (either gotten from cache or from DB)
73//
74page_head(tra("Top hosts"));
75top_host_table_start($sort_by);
76$i = 1 + $offset;
77$n = sizeof($data);
78foreach($data as $host) {
79    show_host_row($host, $i, false, true, false);
80    $i++;
81}
82end_table();
83
84if ($offset > 0) {
85    $new_offset = $offset - $hosts_per_page;
86    echo "<a href=top_hosts.php?sort_by=$sort_by&amp;offset=$new_offset>".tra("Previous %1", $hosts_per_page)."</a> &middot; ";
87
88}
89
90if ($n==$hosts_per_page){ //If we aren't on the last page
91    $new_offset = $offset + $hosts_per_page;
92    echo "<a href=top_hosts.php?sort_by=$sort_by&amp;offset=$new_offset>".tra("Next %1", $hosts_per_page)."</a>";
93}
94
95page_tail();
96
97
98?>
99