(* COCANWIKI - a wiki written in Objective CAML. * Written by Richard W.M. Jones . * Copyright (C) 2004 Merjis Ltd. * $Id: largest_pages.ml,v 1.6 2006/03/28 16:24:07 rich Exp $ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. *) open Apache open Registry open Cgi open Printf open Cocanwiki open Cocanwiki_template let limit = 50 let modem_speed = 56000 / 10 (* 56kbps modem. *) let overhead = 2 (* Number of seconds to open connection + render page. *) let run r (q : cgi) dbh hostid _ _ = let template = get_template dbh hostid "largest_pages.html" in (* Grab the pages from the database, ordered by size. * NB. At the moment we count the size of the markup, which is only an * approximation of the final size of the page. Also, we are not taking * into account images. *) let rows = PGSQL(dbh) "select p.id, p.url, p.title, sum (length (c.content)) from pages p, contents c where p.hostid = $hostid and p.url is not null and p.redirect is null and c.pageid = p.id group by 1, 2, 3 order by 4 desc" in let table = List.map (fun (pageid, page, title, size) -> let page = Option.get page in let size = Int64.to_int (Option.get size) in let download_time = overhead + size / modem_speed in (* seconds *) let download_time = if download_time <= 4 then "<= 4 s" else if download_time < 60 then sprintf "%d s" download_time else sprintf "%d m %d s" (download_time / 60) (download_time mod 60) in let size = if size < 4096 then sprintf "%d bytes" size else sprintf "%d K" (size / 1024) in [ "pageid", Template.VarString (Int32.to_string pageid); "page", Template.VarString page; "title", Template.VarString title; "size", Template.VarString size; "download_time", Template.VarString download_time ] ) rows in template#table "pages" table; q#template template let () = register_script ~restrict:[CanView] run