Changeset View
Changeset View
Standalone View
Standalone View
create_generated_used.php
1 | #!/usr/bin/php | 1 | #!/usr/bin/php | ||
---|---|---|---|---|---|
2 | <?php | 2 | <?php | ||
3 | /* | 3 | /* | ||
4 | Copyright 2016 Luigi Toscano <luigi.toscano@tiscali.it> | 4 | Copyright 2016 Luigi Toscano <luigi.toscano@tiscali.it> | ||
5 | Copyright 2019 Carl Schwan <carl@carlschwan.eu> | ||||
5 | 6 | | |||
6 | Permission to use, copy, modify, and distribute this software | 7 | Permission to use, copy, modify, and distribute this software | ||
7 | and its documentation for any purpose and without fee is hereby | 8 | and its documentation for any purpose and without fee is hereby | ||
8 | granted, provided that the above copyright notice appear in all | 9 | granted, provided that the above copyright notice appear in all | ||
9 | copies and that both that the copyright notice and this | 10 | copies and that both that the copyright notice and this | ||
10 | permission notice and warranty disclaimer appear in supporting | 11 | permission notice and warranty disclaimer appear in supporting | ||
11 | documentation, and that the name of the author not be used in | 12 | documentation, and that the name of the author not be used in | ||
12 | advertising or publicity pertaining to distribution of the | 13 | advertising or publicity pertaining to distribution of the | ||
Show All 10 Lines | |||||
23 | */ | 24 | */ | ||
24 | 25 | | |||
25 | # This program takes the output of the json files generated by | 26 | # This program takes the output of the json files generated by | ||
26 | # the documentation generator and generates a proper php array | 27 | # the documentation generator and generates a proper php array | ||
27 | # with their content. The output should be saved in a file | 28 | # with their content. The output should be saved in a file | ||
28 | # (generated_used.inc.php) used by the website. | 29 | # (generated_used.inc.php) used by the website. | ||
29 | 30 | | |||
30 | define("JSONFILE_PREFIX", "SITEDATA"); | 31 | define("JSONFILE_PREFIX", "SITEDATA"); | ||
32 | /** | ||||
33 | * Weights of packages: helper function for cmp_packages() | ||||
34 | */ | ||||
35 | function package_weight(string $p): int | ||||
36 | { | ||||
37 | $weights = [ | ||||
38 | 'frameworks' => 0, | ||||
39 | 'kdelibs' => 0, | ||||
40 | 'calligra' => 5, | ||||
41 | 'kdereview' => 10, | ||||
42 | ]; | ||||
43 | if (array_key_exists($p, $weights)) { | ||||
44 | $w = $weights[$p]; | ||||
45 | } elseif (strncmp($p, 'extragear-', strlen('extragear-')) == 0) { | ||||
46 | $w = 7; | ||||
47 | } else { | ||||
48 | $w = 2; | ||||
49 | } | ||||
50 | return $w; | ||||
51 | } | ||||
52 | | ||||
53 | /* | ||||
54 | * Compare packages, so that a certain order is enforced | ||||
55 | * (libraries first, then Applications, Extragear and Review) | ||||
56 | */ | ||||
57 | function cmp_packages(string $p1, string $p2): int | ||||
58 | { | ||||
59 | $w1 = package_weight($p1); | ||||
60 | $w2 = package_weight($p2); | ||||
61 | $dw = $w1 - $w2; | ||||
62 | if ($dw == 0) { | ||||
63 | /* same weight, so normal comparison */ | ||||
64 | return strcmp($p1, $p2); | ||||
65 | } else { | ||||
66 | return $dw; | ||||
67 | } | ||||
68 | } | ||||
31 | 69 | | |||
32 | function extract_from_json($json_dir, $json_file) | 70 | function extract_from_json($json_dir, $json_file) | ||
33 | { | 71 | { | ||
34 | $json_content = file_get_contents($json_dir."/".$json_file); | 72 | $json_content = file_get_contents($json_dir."/".$json_file); | ||
35 | $json_array = json_decode($json_content, true); | 73 | $json_array = json_decode($json_content, true); | ||
36 | $array_string = var_export($json_array, true); | | |||
37 | # full length, without the prefix (with code), without the suffix (.json) | 74 | # full length, without the prefix (with code), without the suffix (.json) | ||
38 | $var_name_len = strlen($json_file) - (strlen(JSONFILE_PREFIX) + 3) - 5; | 75 | $var_name_len = strlen($json_file) - (strlen(JSONFILE_PREFIX) + 3) - 5; | ||
39 | $var_name = substr($json_file, strlen(JSONFILE_PREFIX)+3, $var_name_len); | 76 | $var_name = substr($json_file, strlen(JSONFILE_PREFIX)+3, $var_name_len); | ||
40 | echo "\$$var_name = $array_string;\n"; | 77 | if ($var_name == 'modules_programs') { | ||
78 | // sort modules_prgrams | ||||
79 | uksort($json_array, 'cmp_packages'); | ||||
80 | } | ||||
81 | $array_string = var_export($json_array, true); | ||||
82 | | ||||
83 | echo "const $var_name = $array_string;\n"; | ||||
84 | | ||||
85 | if ($var_name == 'languagelist') { | ||||
86 | // create special variable for routing parameter _locale in config/services.php | ||||
87 | $string = ''; | ||||
88 | foreach ($json_array as $lang) { | ||||
89 | $string .= $lang . '|'; | ||||
90 | } | ||||
91 | $string = rtrim($string,"|"); | ||||
92 | | ||||
93 | // save content | ||||
94 | $parameterFile = fopen("./config/services.php", "w"); | ||||
95 | fwrite($parameterFile, "<?php \n \$container->setParameter('app.locales', '$string');\n"); | ||||
96 | fclose($parameterFile); | ||||
97 | } | ||||
41 | } | 98 | } | ||
42 | 99 | | |||
43 | function convert_files($json_dir) | 100 | function convert_files($json_dir) | ||
44 | { | 101 | { | ||
45 | $all_files = scandir($json_dir); | 102 | $all_files = scandir($json_dir); | ||
46 | echo "<?php\n"; | 103 | echo "<?php\n"; | ||
104 | echo "namespace App\Data;\n"; | ||||
105 | echo "class GeneratedData {\n"; | ||||
106 | | ||||
47 | foreach ($all_files as $afile) { | 107 | foreach ($all_files as $afile) { | ||
48 | if (substr($afile, 0, strlen(JSONFILE_PREFIX)) === JSONFILE_PREFIX) { | 108 | if (substr($afile, 0, strlen(JSONFILE_PREFIX)) === JSONFILE_PREFIX) { | ||
49 | extract_from_json($json_dir, $afile); | 109 | extract_from_json($json_dir, $afile); | ||
50 | } | 110 | } | ||
51 | } | 111 | } | ||
52 | echo "?>\n"; | 112 | echo "}\n"; | ||
53 | } | 113 | } | ||
54 | 114 | | |||
55 | $json_files_dir = "."; | 115 | $json_files_dir = "."; | ||
56 | if (count($argv) > 1) { | 116 | if (count($argv) > 1) { | ||
57 | $json_files_dir = $argv[1]; | 117 | $json_files_dir = $argv[1]; | ||
58 | } | 118 | } | ||
59 | convert_files($json_files_dir); | 119 | convert_files($json_files_dir); | ||
60 | ?> | 120 | ?> |