Mercurial > hg > batmud > maputils
annotate src/reformat_wizards.php @ 2763:78ad0e51b7b5
Improve wizards.txt parser, add functionality for specifying alternative /
additional names as some wizards have used more than one.
Also other improvements in wizard data handling.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 12 Mar 2024 15:47:58 +0200 |
parents | ee898a9344e7 |
children | 6dcd2aaeee9b |
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 { |
2763
78ad0e51b7b5
Improve wizards.txt parser, add functionality for specifying alternative /
Matti Hamalainen <ccr@tnsp.org>
parents:
2755
diff
changeset
|
41 $str = implode("|", $data["names"]).";". |
2743 | 42 (isset($data["countries"]) ? implode("|", $data["countries"]) : "").";". |
43 (isset($data["homeURL"]) ? $data["homeURL"] : "").";". | |
44 (isset($data["imageURL"]) ? $data["imageURL"] : "").";"; | |
45 | |
46 // Entries with some information are formatted differently | |
2741
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 if ($id > 0) |
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 { |
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 if (isset($data["desc"]) && strlen($data["desc"]) > 0) |
2743 | 50 { |
51 // If we have a description, we format it via black magic: | |
52 // add linefeeds to certain elements ([p|br]) and then | |
53 // run through wordwrap(). Split to lines and indent each | |
54 // line with tab. Combine again. \:D\ | |
55 $str .= "\n". | |
56 rtrim( | |
57 implode("\n", array_map( | |
58 function ($item) | |
59 { | |
60 return "\t".$item; | |
61 }, | |
62 explode("\n", wordwrap( | |
63 preg_replace("#\s*\[(p|br)\]\s*#", "\n[\\1]\n", $data["desc"]), | |
64 68, "\n") | |
65 )))); | |
66 } | |
2741
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 |
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 $str .= "\n\t\$\n\n"; |
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 else |
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 { |
2743 | 72 $str .= "\$\n"; |
2741
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 } |
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 |
2743 | 75 fwrite($file, $str); |
2741
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 } |
2743 | 77 |
78 fwrite($file, | |
79 "\n#########################################################\n\n"); | |
2741
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 } |
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 |
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 fclose($file); |
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 return TRUE; |
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 } |
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 |
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 // Prevent non-cli execution |
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 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
|
89 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
|
90 |
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 if (count($argv) < 2) |
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 die("Usage: ".$argv[0]." <output wizards.txt>\n"); |
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 |
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 $table = mpReadWizInfoFiles(); |
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 mpWriteWizInfoFile($argv[1], $table); |
2755
ee898a9344e7
Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents:
2743
diff
changeset
|
98 if ($errorSet) |
ee898a9344e7
Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents:
2743
diff
changeset
|
99 { |
ee898a9344e7
Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents:
2743
diff
changeset
|
100 echo implode("", array_map( |
ee898a9344e7
Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents:
2743
diff
changeset
|
101 function ($item) |
ee898a9344e7
Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents:
2743
diff
changeset
|
102 { |
ee898a9344e7
Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents:
2743
diff
changeset
|
103 return "ERROR: ".$item."\n"; |
ee898a9344e7
Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents:
2743
diff
changeset
|
104 }, $errorMsgs)); |
ee898a9344e7
Add error message handling to reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents:
2743
diff
changeset
|
105 } |
2741
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
106 |
91698e217f84
Import PHP utility for reformatting wizards.txt
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
107 ?> |