Changes done on the Mac.
[cocanwiki.git] / scripts / largest_pages.ml
1 (* COCANWIKI - a wiki written in Objective CAML.
2  * Written by Richard W.M. Jones <rich@merjis.com>.
3  * Copyright (C) 2004 Merjis Ltd.
4  * $Id: largest_pages.ml,v 1.5 2006/03/28 13:20:00 rich Exp $
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; see the file COPYING.  If not, write to
18  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *)
21
22 open Apache
23 open Registry
24 open Cgi
25 open Printf
26
27 open Cocanwiki
28 open Cocanwiki_template
29
30 let limit = 50
31 let modem_speed = 56000 / 10            (* 56kbps modem. *)
32 let overhead = 2 (* Number of seconds to open connection + render page. *)
33
34 let run r (q : cgi) dbh hostid _ _ =
35   let template = get_template dbh hostid "largest_pages.html" in
36
37   (* Grab the pages from the database, ordered by size.
38    * NB.  At the moment we count the size of the markup, which is only an
39    * approximation of the final size of the page.  Also, we are not taking
40    * into account images.
41    *)
42   let rows =
43     PGSQL(dbh)
44       "select p.id, p.url, p.title, sum (length (c.content))
45          from pages p, contents c
46         where p.hostid = $hostid and p.url is not null and p.redirect is null
47           and c.pageid = p.id group by 1, 2, 3 order by 4 desc" in
48
49   let table =
50     List.map
51       (function [pageid; page; title; Some size] ->
52          let size = Int32.to_int size in
53          let download_time = overhead + size / modem_speed in (* seconds *)
54          let download_time =
55            if download_time <= 4 then "<= 4 s"
56            else if download_time < 60 then sprintf "%d s" download_time
57            else sprintf "%d m %d s" (download_time / 60) (download_time mod 60)
58          in
59
60          let size =
61            if size < 4096 then sprintf "%d bytes" size
62            else sprintf "%d K" (size / 1024) in
63
64          [ "pageid", Template.VarString (Int32.to_string pageid);
65            "page", Template.VarString page;
66            "title", Template.VarString title;
67            "size", Template.VarString size;
68            "download_time", Template.VarString download_time ]
69        | _ -> assert false) in
70   template#table "pages" table;
71
72   q#template template
73
74 let () =
75   register_script ~restrict:[CanView] run