annotate src/reformat_wizards.php @ 2768:6dcd2aaeee9b

Fix reformatter after common.inc.php changes.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 13 Mar 2024 11:30:27 +0200
parents 78ad0e51b7b5
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2741
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 #!/usr/bin/php
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 <?php
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 require "common.inc.php";
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 function mpWriteWizInfoFile($filename, &$table)
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 {
2743
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
8 // "Categorize" the entries based on the data they have
2741
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
9 $categories = [];
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10 foreach ($table as $name => $data)
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 {
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
12 $score = 0;
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
14 if (isset($data["countries"])) $score++;
2743
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
15 if (isset($data["homeURL"])) $score++;
2741
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16 if (isset($data["homeURL"])) $score++;
2743
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
17 if (isset($data["desc"])) $score = 5;
2741
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
18
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 $categories[$score][$name] = $data;
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20 }
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21
2743
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
22 // Sort categories descendingly
2741
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
23 krsort($categories, SORT_NUMERIC);
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
24
2743
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
25 // File header
2741
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
26 if (($file = @fopen($filename, "w")) === false)
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
27 return FALSE;
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
28
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
29 fwrite($file,
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
30 "# Information about coders/wizards. Format of this file:\n".
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
31 "# name;[3-letter country code];[http://homepageURL|bat];[https?://pictureURL|image.jpg];[freeform description]\$\n\n");
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
32
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
33 foreach ($categories as $id => $cat)
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
34 {
2743
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
35 // Sort each category alphabetically
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
36 ksort($cat, SORT_STRING);
2741
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
37
2743
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
38 // Output each entry in this category
2741
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
39 foreach ($cat as $name => $data)
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
40 {
2768
6dcd2aaeee9b Fix reformatter after common.inc.php changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2763
diff changeset
41 $names = array_merge([$data["name"] ], $data["names"]);
6dcd2aaeee9b Fix reformatter after common.inc.php changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2763
diff changeset
42
6dcd2aaeee9b Fix reformatter after common.inc.php changes.
Matti Hamalainen <ccr@tnsp.org>
parents: 2763
diff changeset
43 $str = implode("|", $names).";".
2743
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
44 (isset($data["countries"]) ? implode("|", $data["countries"]) : "").";".
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
45 (isset($data["homeURL"]) ? $data["homeURL"] : "").";".
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
46 (isset($data["imageURL"]) ? $data["imageURL"] : "").";";
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
47
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
48 // Entries with some information are formatted differently
2741
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
49 if ($id > 0)
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
50 {
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
51 if (isset($data["desc"]) && strlen($data["desc"]) > 0)
2743
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
52 {
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
53 // If we have a description, we format it via black magic:
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
54 // add linefeeds to certain elements ([p|br]) and then
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
55 // run through wordwrap(). Split to lines and indent each
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
56 // line with tab. Combine again. \:D\
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
57 $str .= "\n".
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
58 rtrim(
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
59 implode("\n", array_map(
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
60 function ($item)
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
61 {
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
62 return "\t".$item;
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
63 },
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
64 explode("\n", wordwrap(
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
65 preg_replace("#\s*\[(p|br)\]\s*#", "\n[\\1]\n", $data["desc"]),
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
66 68, "\n")
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
67 ))));
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
68 }
2741
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
69
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
70 $str .= "\n\t\$\n\n";
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
71 }
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
72 else
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
73 {
2743
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
74 $str .= "\$\n";
2741
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
75 }
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
76
2743
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
77 fwrite($file, $str);
2741
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
78 }
2743
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
79
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
80 fwrite($file,
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
81 "\n#########################################################\n\n");
2741
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
82 }
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
83
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
84 fclose($file);
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
85 return TRUE;
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
86 }
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
87
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
88
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
89 // Prevent non-cli execution
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
90 if (php_sapi_name() != "cli" || !empty($_SERVER["REMOTE_ADDR"]))
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
91 die("You can only run this script as a commandline application.\n");
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
92
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
93 if (count($argv) < 2)
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
94 {
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
95 die("Usage: ".$argv[0]." <output wizards.txt>\n");
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
96 }
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
97
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
98 $table = mpReadWizInfoFiles();
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
99 mpWriteWizInfoFile($argv[1], $table);
2755
ee898a9344e7 Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2743
diff changeset
100 if ($errorSet)
ee898a9344e7 Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2743
diff changeset
101 {
ee898a9344e7 Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2743
diff changeset
102 echo implode("", array_map(
ee898a9344e7 Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2743
diff changeset
103 function ($item)
ee898a9344e7 Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2743
diff changeset
104 {
ee898a9344e7 Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2743
diff changeset
105 return "ERROR: ".$item."\n";
ee898a9344e7 Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2743
diff changeset
106 }, $errorMsgs));
ee898a9344e7 Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2743
diff changeset
107 }
2741
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
108
91698e217f84 Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
109 ?>