2 * Copyright (C) 2011-2015 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #define _(String) gettext(String)
28 #include <sys/types.h>
46 static char *ppa_body;
47 static char *pkg_body;
48 static char *pkg_version_body;
51 void html_set_theme_dir(const char *theme_dir)
53 css_dir = path_append(theme_dir, "css");
54 js_dir = path_append(theme_dir, "js");
55 tpl_dir = path_append(theme_dir, "templates");
58 static char *path_new(const char *dir, const char *file, const char *suffixe)
62 /* [dir]/[file][suffixe] */
63 path = malloc(strlen(dir)+1+
65 (suffixe ? strlen(suffixe) : 0) +
71 strcat(path, suffixe);
76 static char *get_header(const char *title, const char *script)
78 char *res, *tmp, *path;
81 path = path_append(tpl_dir, "header.tpl");
82 header = file_get_content(path);
89 res = strrep(tmp, "@SCRIPT@", script);
95 res = strrep(tmp, "@TITLE@", title);
100 log_err("Failed to read header template: %s", path);
109 static const char *get_footer()
114 path = path_append(tpl_dir, "footer.tpl");
115 footer = file_get_content(path);
118 log_err("Failed to read footer template: %s", path);
126 static const char *get_pkg_version_body()
130 if (!pkg_version_body) {
131 path = path_append(tpl_dir, "pkg_version.tpl");
132 pkg_version_body = file_get_content(path);
134 if (!pkg_version_body)
135 log_err("Failed to read package version template: %s",
141 return pkg_version_body;
143 static const char *get_ppa_body()
148 path = path_append(tpl_dir, "ppa.tpl");
149 ppa_body = file_get_content(path);
152 log_err("Failed to read PPA template: %s", path);
160 static const char *get_pkg_body()
165 path = path_append(tpl_dir, "pkg.tpl");
166 pkg_body = file_get_content(path);
169 log_err("Failed to read package template: %s", path);
177 static void json_add_ddts(json_object *json,
178 struct daily_download_total **ddts)
180 json_object_object_add(json, "ddts", ddts_to_json(ddts));
183 static json_object *distro_to_json(struct distro_stats *d)
187 json = json_object_new_object();
189 json_object_object_add(json,
191 json_object_new_string(d->name));
193 json_object_object_add(json,
195 json_object_new_int(d->download_count));
197 json_add_ddts(json, d->ddts);
202 static int version_cmp(const void *o1, const void *o2)
204 struct version_stats **v1, **v2;
206 v1 = (struct version_stats **)o1;
207 v2 = (struct version_stats **)o2;
209 return (*v1)->date_created <= (*v2)->date_created;
212 static struct version_stats **sort_versions(struct version_stats **vers)
215 struct version_stats **tmp, **result;
224 result = malloc((n + 1) * sizeof(struct version_stats *));
225 memcpy(result, vers, n * sizeof(struct version_stats *));
228 qsort(result, n, sizeof(struct version_stats *), version_cmp);
234 pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
236 json_object *json, *json_versions, *json_distros, *json_distro;
237 struct version_stats **versions, **tmp;
238 struct distro_stats **distros, *d;
240 json = json_object_new_object();
242 json_object_object_add(json,
243 "ppa_name", json_object_new_string(ppa->name));
244 json_object_object_add(json,
246 json_object_new_string(ppa->owner));
248 json_object_object_add(json,
249 "name", json_object_new_string(pkg->name));
251 json_versions = json_object_new_array();
252 json_object_object_add(json, "versions", json_versions);
253 versions = sort_versions(pkg->versions);
256 json_object_array_add
258 json_object_new_string((*tmp)->version));
264 distros = pkg->distros;
266 json_distros = json_object_new_array();
267 json_object_object_add(json, "distros", json_distros);
272 if (d->download_count) {
273 json_distro = distro_to_json(d);
275 json_object_array_add(json_distros,
283 json_add_ddts(json, pkg->daily_download_totals);
288 static char *version_to_json(struct ppa_stats *ppa,
289 struct package_stats *pkg,
290 struct version_stats *ver)
293 struct distro_stats **distros, *distro;
294 json_object *json, *json_distros, *json_distro, *json_archs, *json_arch;
295 struct arch_stats **archs;
297 json = json_object_new_object();
299 json_object_object_add(json,
300 "ppa_name", json_object_new_string(ppa->name));
301 json_object_object_add(json,
303 json_object_new_string(ppa->owner));
305 json_object_object_add(json,
306 "pkg_name", json_object_new_string(pkg->name));
308 json_object_object_add(json,
309 "name", json_object_new_string(ver->version));
311 json_object_object_add(json,
312 "date_created", time_to_json(ver->date_created));
314 json_add_ddts(json, ver->daily_download_totals);
316 distros = ver->distros;
317 json_distros = json_object_new_array();
318 json_object_object_add(json, "distros", json_distros);
321 json_distro = json_object_new_object();
323 json_object_array_add(json_distros, json_distro);
325 json_object_object_add(json_distro,
327 json_object_new_string(distro->name));
329 archs = distro->archs;
330 json_archs = json_object_new_array();
331 json_object_object_add(json_distro, "archs", json_archs);
333 json_arch = json_object_new_object();
335 json_object_object_add
338 json_object_new_string((*archs)->name));
340 json_object_object_add
343 json_object_new_int((*archs)->download_count));
345 json_object_array_add(json_archs, json_arch);
352 ret = strdup(json_object_to_json_string(json));
354 json_object_put(json);
359 static json_object *ppa_to_json(struct ppa_stats *ppa)
361 json_object *json, *json_pkgs, *json_pkg;
362 struct package_stats **pkgs;
364 json = json_object_new_object();
366 json_object_object_add(json,
367 "ppa_name", json_object_new_string(ppa->name));
368 json_object_object_add(json,
370 json_object_new_string(ppa->owner));
372 json_add_ddts(json, ppa->daily_download_totals);
374 pkgs = ppa->packages;
375 json_pkgs = json_object_new_array();
376 json_object_object_add(json, "packages", json_pkgs);
378 json_pkg = json_object_new_object();
379 json_object_array_add(json_pkgs, json_pkg);
381 json_object_object_add(json_pkg, "name",
382 json_object_new_string((*pkgs)->name));
384 json_object_object_add
386 json_object_new_int((*pkgs)->download_count));
395 create_html(const char *path,
397 const char *body_template,
406 header = get_header(title, script);
408 log_err(_("Failed to get the header template"));
412 f = fopen(path, "w");
415 log_err(_("Failed to open: %s"), path);
420 fputs(body_template, f);
422 footer = get_footer();
434 static char *ppa_display_name(const struct ppa_stats *ppa)
438 ret = malloc(4+strlen(ppa->name)+1+strlen(ppa->owner)+1);
440 sprintf(ret, "ppa:%s/%s", ppa->owner, ppa->name);
446 index_to_html(struct ppa_stats *ppa, const char *dir)
448 char *path, *json_path, *dname;
452 body = get_ppa_body();
454 log_err("Failed to create PPA page");
458 json = ppa_to_json(ppa);
459 json_path = path_new(dir, "index", ".json");
461 log_debug(_("generating %s"), json_path);
462 json_object_to_file(json_path, json);
463 json_object_put(json);
466 path = path_new(dir, "index", ".html");
467 dname = ppa_display_name(ppa);
468 create_html(path, dname, body, "ppastats_ppa();");
474 version_to_html(struct ppa_stats *ppa,
475 struct package_stats *pkg,
476 struct version_stats *version,
481 const char *script_tpl;
484 body = get_pkg_version_body();
486 log_err("Failed to create package version page");
490 json = version_to_json(ppa, pkg, version);
492 log_err("Failed to create package version page");
496 f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
497 sprintf(f_name, "%s_%s", pkg->name, version->version);
499 path = path_new(dir, f_name, ".html");
501 script_tpl = "var data = %s;\n ppastats_ver();";
502 script = malloc(strlen(script_tpl) - 2 + strlen(json) + 1);
503 sprintf(script, script_tpl, json);
505 create_html(path, f_name, body, script);
514 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
516 char *path, *json_path, *script;
520 body = get_pkg_body();
522 log_err("Failed to create package page: %s", pkg->name);
526 json_path = path_new(dir, pkg->name, ".json");
527 json = pkg_to_json(ppa, pkg);
528 log_debug(_("Generating %s"), json_path);
530 json_object_to_file(json_path, json);
531 json_object_put(json);
534 path = path_new(dir, pkg->name, ".html");
535 script = malloc(strlen("ppastats_pkg(\"\");")+
539 sprintf(script, "ppastats_pkg(\"%s%s\");", pkg->name, ".json");
541 log_debug(_("Generating %s"), path);
543 create_html(path, pkg->name, body, script);
549 pkgs_to_html(struct ppa_stats *ppa,
550 struct package_stats **pkgs,
553 struct version_stats **versions;
556 pkg_to_html(ppa, *pkgs, dir);
558 versions = (*pkgs)->versions;
560 version_to_html(ppa, *pkgs, *versions, dir);
570 ppa_to_html(const char *owner,
572 const char *package_status,
573 const char *output_dir,
574 const int install_static_files,
577 struct ppa_stats *ppastats;
579 char *css_odir, *js_odir;
581 mkdirs(output_dir, 0777);
583 if (install_static_files) {
584 css_odir = path_append(output_dir, "css");
585 js_odir = path_append(output_dir, "js");
587 dir_rcopy(css_dir, css_odir);
588 dir_rcopy(js_dir, js_odir);
594 ppastats = create_ppa_stats(owner, ppa, package_status, ws_size);
596 path = path_new(output_dir, "ppa", ".html");
598 pkgs_to_html(ppastats, ppastats->packages, output_dir);
600 index_to_html(ppastats, output_dir);
602 ppa_stats_free(ppastats);
613 free(pkg_version_body);