Mercurial > hg > batmud > maputils
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 |
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 | 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 | 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 | 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 | 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 | 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 | 35 // Sort each category alphabetically |
36 ksort($cat, SORT_STRING); | |
2741
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 |
2743 | 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 | 44 (isset($data["countries"]) ? implode("|", $data["countries"]) : "").";". |
45 (isset($data["homeURL"]) ? $data["homeURL"] : "").";". | |
46 (isset($data["imageURL"]) ? $data["imageURL"] : "").";"; | |
47 | |
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 | 52 { |
53 // If we have a description, we format it via black magic: | |
54 // add linefeeds to certain elements ([p|br]) and then | |
55 // run through wordwrap(). Split to lines and indent each | |
56 // line with tab. Combine again. \:D\ | |
57 $str .= "\n". | |
58 rtrim( | |
59 implode("\n", array_map( | |
60 function ($item) | |
61 { | |
62 return "\t".$item; | |
63 }, | |
64 explode("\n", wordwrap( | |
65 preg_replace("#\s*\[(p|br)\]\s*#", "\n[\\1]\n", $data["desc"]), | |
66 68, "\n") | |
67 )))); | |
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 | 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 | 77 fwrite($file, $str); |
2741
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 } |
2743 | 79 |
80 fwrite($file, | |
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 ?> |