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