added string replace fct
[ppastats.git] / src / html.c
1 /*
2  * Copyright (C) 2011-2012 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19
20 #include <libintl.h>
21 #define _(String) gettext(String)
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/time.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29
30 #include <json/json.h>
31
32 #include "html.h"
33 #include "io.h"
34 #include "log.h"
35 #include "lp.h"
36 #include "lp_ws.h"
37 #include "ppastats.h"
38
39 static char *footer;
40 static char *ppa_body;
41 static char *pkg_body;
42
43 #define HTML_VERSION_TEMPLATE \
44 "<html>\n\
45   <head>\n\
46     <link type=\"text/css\"\n\
47           rel=\"stylesheet\"\n\
48           href=\n\
49 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
50     <link type=\"text/css\" href=\"css/ppastats.css\" rel=\"stylesheet\" />\n\
51     <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
52           rel=\"stylesheet\" />\n\
53 <!--[if lt IE 9]><script \
54 language=\"javascript\" \
55 type=\"text/javascript\" \
56 src=\"js/excanvas.js\"></script><![endif]-->\n\
57     <script type=\"text/javascript\" src=\"js/jquery.min.js\"></script>\n\
58     <script type=\"text/javascript\"\n\
59             src=\"js/jquery.jqplot.min.js\"></script>\n\
60     <script type=\"text/javascript\"\n\
61             src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
62     <script type=\"text/javascript\" src=\"js/ppastats.js\"></script>\n\
63     <script>var data = %s;\n\
64             ppastats_ver();\n\
65     </script>\n\
66   </head>\n\
67   <body>\n\
68     <h1><span id=\"pkg_name\">N/A</span></h1>\n\
69     <div id=\"version\"><em>Version:</em></div>\n\
70     <p><em>PPA</em>: \n\
71        <a href=\"index.html\">\n\
72            <span id=\"ppa_owner\">N/A</span>/<span id=\"ppa_name\">N/A</span>\n\
73        </a></p>\n\
74     <div id=\"charts\">\n\
75         <div id=\"chart\"></div>\n\
76     </div>\n\
77     <div id=\"details\">\n\
78       <div class=\"distros\">\n\
79         <em>Distros</em>:\n\
80         <ul id=\"distros\"></ul>\n\
81       </div>\n\
82     </div>\n"
83
84 #define HTML_HEADER \
85 "<html>\n\
86         <head>\n\
87                 <title>%s</title>\n\
88                 <link type=\"text/css\"\n\
89                       rel=\"stylesheet\"\n\
90                       href=\
91 \"http://fonts.googleapis.com/css?family=Ubuntu:regular,bold&subset=Latin\">\n\
92                 <link type=\"text/css\" href=\"css/ppastats.css\"\n\
93                       rel=\"stylesheet\"/>\n\
94                 <link type=\"text/css\" href=\"css/jquery.jqplot.min.css\"\n\
95                       rel=\"stylesheet\" />\n\
96 <!--[if lt IE 9]><script \
97 language=\"javascript\" \
98 type=\"text/javascript\" \
99 src=\"js/excanvas.js\"></script><![endif]-->\n\
100                 <script type=\"text/javascript\"\n\
101                         src=\"js/jquery.min.js\"></script>\n\
102                 <script type=\"text/javascript\"\n\
103                         src=\"js/jquery.jqplot.min.js\"></script>\n\
104                 <script type=\"text/javascript\"\n\
105                         src=\"js/jqplot.dateAxisRenderer.min.js\"></script>\n\
106                 <script type=\"text/javascript\"\n\
107                         src=\"js/ppastats.js\"></script>\n\
108                 <script>%s</script>\n\
109         </head>\n\
110         <body>\n"
111
112 static char *path_new(const char *dir, const char *file, const char *suffixe)
113 {
114         char *path;
115
116         /* [dir]/[file][suffixe] */
117         path = malloc(strlen(dir)+1+
118                       strlen(file)+
119                       (suffixe ? strlen(suffixe) : 0) +
120                       1);
121
122         strcpy(path, dir);
123         strcat(path, "/");
124         strcat(path, file);
125         strcat(path, suffixe);
126
127         return path;
128 }
129
130 static const char *get_footer()
131 {
132         const char *path;
133
134         if (!footer) {
135                 path = DEFAULT_WWW_DIR"/footer.tpl";
136                 footer = file_get_content(path);
137
138                 if (!footer)
139                         log_err("Failed to read footer template: %s", path);
140         }
141
142         return footer;
143 }
144
145 static const char *get_ppa_body()
146 {
147         const char *path;
148
149         if (!ppa_body) {
150                 path = DEFAULT_WWW_DIR"/ppa.tpl";
151                 ppa_body = file_get_content(path);
152
153                 if (!ppa_body)
154                         log_err("Failed to read PPA template: %s", path);
155         }
156
157         return ppa_body;
158 }
159
160 static const char *get_pkg_body()
161 {
162         const char *path;
163
164         if (!pkg_body) {
165                 path = DEFAULT_WWW_DIR"/pkg.tpl";
166                 pkg_body = file_get_content(path);
167
168                 if (!pkg_body)
169                         log_err("Failed to read package template: %s", path);
170         }
171
172         return pkg_body;
173 }
174
175 static struct json_object *date_to_json(struct tm *tm)
176 {
177         json_object *json;
178
179         json = json_object_new_array();
180         json_object_array_add(json, json_object_new_int(tm->tm_year+1900));
181         json_object_array_add(json, json_object_new_int(tm->tm_mon+1));
182         json_object_array_add(json, json_object_new_int(tm->tm_mday));
183
184         return json;
185 }
186
187 static void json_add_ddts(json_object *json,
188                           struct daily_download_total **ddts)
189 {
190         json_object *json_ddt, *json_ddts;
191         struct daily_download_total *ddt;
192
193         json_ddts = json_object_new_array();
194         json_object_object_add(json, "ddts", json_ddts);
195
196         if (!ddts)
197                 return ;
198
199         while (*ddts) {
200                 ddt = *ddts;
201
202                 json_ddt = json_object_new_object();
203                 json_object_object_add(json_ddt,
204                                        "value",
205                                        json_object_new_int(ddt->count));
206                 json_object_object_add(json_ddt,
207                                        "time",
208                                        date_to_json(&ddt->date));
209
210                 json_object_array_add(json_ddts, json_ddt);
211
212                 ddts++;
213         }
214 }
215
216 static json_object *distro_to_json(struct distro_stats *d)
217 {
218         json_object *json;
219
220         json = json_object_new_object();
221
222         json_object_object_add(json,
223                                "name",
224                                json_object_new_string(d->name));
225
226         json_object_object_add(json,
227                                "count",
228                                json_object_new_int(d->download_count));
229
230         json_add_ddts(json, d->ddts);
231
232         return json;
233 }
234
235 static json_object *
236 pkg_to_json(struct ppa_stats *ppa, struct package_stats *pkg)
237 {
238         json_object *json, *json_versions, *json_distros, *json_distro;
239         struct version_stats **versions;
240         struct distro_stats **distros, *d;
241
242         json = json_object_new_object();
243
244         json_object_object_add(json,
245                                "ppa_name", json_object_new_string(ppa->name));
246         json_object_object_add(json,
247                                "ppa_owner",
248                                json_object_new_string(ppa->owner));
249
250         json_object_object_add(json,
251                                "name", json_object_new_string(pkg->name));
252
253         json_versions = json_object_new_array();
254         json_object_object_add(json, "versions", json_versions);
255         versions = pkg->versions;
256         while (*versions) {
257                 json_object_array_add
258                         (json_versions,
259                          json_object_new_string((*versions)->version));
260
261                 versions++;
262         }
263
264         distros = pkg->distros;
265         if (distros) {
266                 json_distros = json_object_new_array();
267                 json_object_object_add(json, "distros", json_distros);
268
269                 while (*distros) {
270                         d = *distros;
271
272                         if (d->download_count) {
273                                 json_distro = distro_to_json(d);
274
275                                 json_object_array_add(json_distros,
276                                                       json_distro);
277                         }
278
279                         distros++;
280                 }
281         }
282
283         json_add_ddts(json, pkg->daily_download_totals);
284
285         return json;
286 }
287
288 static char *version_to_json(struct ppa_stats *ppa,
289                              struct package_stats *pkg,
290                              struct version_stats *ver)
291 {
292         char *ret;
293         struct distro_stats **distros, *distro;
294         json_object *json, *json_distros, *json_distro, *json_archs, *json_arch;
295         struct arch_stats **archs;
296
297         json = json_object_new_object();
298
299         json_object_object_add(json,
300                                "ppa_name", json_object_new_string(ppa->name));
301         json_object_object_add(json,
302                                "ppa_owner",
303                                json_object_new_string(ppa->owner));
304
305         json_object_object_add(json,
306                                "pkg_name", json_object_new_string(pkg->name));
307
308         json_object_object_add(json,
309                                "name", json_object_new_string(ver->version));
310
311         json_add_ddts(json, ver->daily_download_totals);
312
313         distros = ver->distros;
314         json_distros = json_object_new_array();
315         json_object_object_add(json, "distros", json_distros);
316         while (*distros) {
317                 distro = *distros;
318                 json_distro = json_object_new_object();
319
320                 json_object_array_add(json_distros, json_distro);
321
322                 json_object_object_add(json_distro,
323                                        "name",
324                                        json_object_new_string(distro->name));
325
326                 archs = distro->archs;
327                 json_archs = json_object_new_array();
328                 json_object_object_add(json_distro, "archs", json_archs);
329                 while (*archs) {
330                         json_arch = json_object_new_object();
331
332                         json_object_object_add
333                                 (json_arch,
334                                  "name",
335                                  json_object_new_string((*archs)->name));
336
337                         json_object_object_add
338                                 (json_arch,
339                                  "count",
340                                  json_object_new_int((*archs)->download_count));
341
342                         json_object_array_add(json_archs, json_arch);
343                         archs++;
344                 }
345
346                 distros++;
347         }
348
349         ret = strdup(json_object_to_json_string(json));
350
351         json_object_put(json);
352
353         return ret;
354 }
355
356 static json_object *ppa_to_json(struct ppa_stats *ppa)
357 {
358         json_object *json, *json_pkgs, *json_pkg;
359         struct package_stats **pkgs;
360
361         json = json_object_new_object();
362
363         json_object_object_add(json,
364                                "ppa_name", json_object_new_string(ppa->name));
365         json_object_object_add(json,
366                                "ppa_owner",
367                                json_object_new_string(ppa->owner));
368
369         json_add_ddts(json, ppa->daily_download_totals);
370
371         pkgs = ppa->packages;
372         json_pkgs = json_object_new_array();
373         json_object_object_add(json, "packages", json_pkgs);
374         while (*pkgs) {
375                 json_pkg = json_object_new_object();
376                 json_object_array_add(json_pkgs, json_pkg);
377
378                 json_object_object_add(json_pkg, "name",
379                                        json_object_new_string((*pkgs)->name));
380
381                 json_object_object_add
382                         (json_pkg, "count",
383                          json_object_new_int((*pkgs)->download_count));
384
385                 pkgs++;
386         }
387
388         return json;
389 }
390
391 static void
392 version_to_html(struct ppa_stats *ppa,
393                 struct package_stats *pkg,
394                 struct version_stats *version,
395                 const char *dir)
396 {
397         char *f_name, *path;
398         FILE *f;
399         const char *footer;
400
401         f_name = malloc(strlen(pkg->name)+1+strlen(version->version)+1);
402         sprintf(f_name, "%s_%s", pkg->name, version->version);
403
404         path = path_new(dir, f_name, ".html");
405         f = fopen(path, "w");
406
407         if (!f) {
408                 log_err(_("failed to open: %s"), path);
409                 return ;
410         }
411
412         fprintf(f, HTML_VERSION_TEMPLATE,
413                 version_to_json(ppa, pkg, version));
414
415         footer = get_footer();
416         if (footer)
417                 fputs(footer, f);
418
419         fclose(f);
420
421         free(path);
422         free(f_name);
423 }
424
425 static void
426 create_html(const char *path,
427             const char *title,
428             const char *body_template,
429             const char *script)
430 {
431         FILE *f;
432         const char *footer;
433
434         f = fopen(path, "w");
435
436         if (!f) {
437                 log_err(_("failed to open: %s"), path);
438                 return ;
439         }
440
441         fprintf(f, HTML_HEADER, title, script);
442         fputs(body_template, f);
443
444         footer = get_footer();
445         if (footer)
446                 fputs(footer, f);
447
448         fclose(f);
449 }
450
451 static char *ppa_display_name(const struct ppa_stats *ppa)
452 {
453         char *ret;
454
455         ret = malloc(4+strlen(ppa->name)+1+strlen(ppa->owner)+1);
456
457         sprintf(ret, "ppa:%s/%s", ppa->owner, ppa->name);
458
459         return ret;
460 }
461
462 static void
463 index_to_html(struct ppa_stats *ppa, const char *dir)
464 {
465         char *path, *json_path, *dname;
466         json_object *json;
467         const char *body;
468
469         body = get_ppa_body();
470         if (!body) {
471                 log_err("Failed to create PPA page");
472                 return ;
473         }
474
475         json = ppa_to_json(ppa);
476         json_path = path_new(dir, "index", ".json");
477
478         log_debug(_("generating %s"), json_path);
479         json_object_to_file(json_path, json);
480         json_object_put(json);
481         free(json_path);
482
483         path = path_new(dir, "index", ".html");
484         dname = ppa_display_name(ppa);
485         create_html(path, dname, body, "ppastats_ppa();");
486         free(path);
487         free(dname);
488 }
489
490 static void
491 pkg_to_html(struct ppa_stats *ppa, struct package_stats *pkg, const char *dir)
492 {
493         char *path, *json_path, *script;
494         json_object *json;
495         const char *body;
496
497         body = get_pkg_body();
498         if (!body) {
499                 log_err("Failed to create package page: %s", pkg->name);
500                 return ;
501         }
502
503         json_path = path_new(dir, pkg->name, ".json");
504         json = pkg_to_json(ppa, pkg);
505         log_debug(_("generating %s"), json_path);
506
507         json_object_to_file(json_path, json);
508         json_object_put(json);
509         free(json_path);
510
511         path = path_new(dir, pkg->name, ".html");
512         script = malloc(strlen("ppastats_pkg(\"\");")+
513                         strlen(pkg->name)+
514                         strlen(".json")+
515                         1);
516         sprintf(script, "ppastats_pkg(\"%s%s\");", pkg->name, ".json");
517
518         log_debug(_("generating %s"), path);
519
520         create_html(path, pkg->name, body, script);
521         free(path);
522         free(script);
523 }
524
525 static void
526 pkgs_to_html(struct ppa_stats *ppa,
527              struct package_stats **pkgs,
528              const char *dir)
529 {
530         struct version_stats **versions;
531
532         while (*pkgs) {
533                 pkg_to_html(ppa, *pkgs, dir);
534
535                 versions = (*pkgs)->versions;
536                 while (*versions) {
537                         version_to_html(ppa, *pkgs, *versions, dir);
538
539                         versions++;
540                 }
541
542                 pkgs++;
543         }
544 }
545
546 void
547 ppa_to_html(const char *owner,
548             const char *ppa,
549             const char *package_status,
550             const char *output_dir,
551             const int install_static_files)
552 {
553         struct ppa_stats *ppastats;
554         char *path, *f_dst;
555         char *css_dir, *js_dir;
556         int i;
557         static char *www_files[]
558                 = { DEFAULT_WWW_DIR"/jquery.min.js", "js/jquery.min.js",
559                     DEFAULT_WWW_DIR"/ppastats.js", "js/ppastats.js",
560                     DEFAULT_WWW_DIR"/jqplot.dateAxisRenderer.min.js",
561                     "js/jqplot.dateAxisRenderer.min.js",
562                     DEFAULT_WWW_DIR"/jquery.jqplot.min.js",
563                     "js/jquery.jqplot.min.js",
564                     DEFAULT_WWW_DIR"/excanvas.js", "js/excanvas.js",
565                     DEFAULT_WWW_DIR"/ppastats.css", "css/ppastats.css",
566                     DEFAULT_WWW_DIR"/jquery.jqplot.min.css",
567                     "css/jquery.jqplot.min.css" };
568
569         mkdirs(output_dir, 0777);
570
571         if (install_static_files) {
572                 css_dir = path_append(output_dir, "css");
573                 js_dir = path_append(output_dir, "js");
574
575                 mkdir(css_dir, 0777);
576                 mkdir(js_dir, 0777);
577
578                 for (i = 0; i < 7; i++) {
579                         f_dst = path_append(output_dir, www_files[2*i+1]);
580
581                         log_debug(_("copying %s %s"), www_files[2*i], f_dst);
582                         fcopy(www_files[2*i], f_dst);
583
584                         free(f_dst);
585                 }
586                 free(css_dir);
587                 free(js_dir);
588         }
589
590         ppastats = create_ppa_stats(owner, ppa, package_status);
591
592         path = path_new(output_dir, "ppa", ".html");
593
594         pkgs_to_html(ppastats, ppastats->packages, output_dir);
595
596         index_to_html(ppastats, output_dir);
597
598         ppa_stats_free(ppastats);
599
600         free(path);
601 }