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
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 {
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
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
42 (isset($data["countries"]) ? implode("|", $data["countries"]) : "").";".
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
43 (isset($data["homeURL"]) ? $data["homeURL"] : "").";".
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
44 (isset($data["imageURL"]) ? $data["imageURL"] : "").";";
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
45
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
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
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
50 {
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
51 // If we have a description, we format it via black magic:
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
52 // add linefeeds to certain elements ([p|br]) and then
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
53 // run through wordwrap(). Split to lines and indent each
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
54 // line with tab. Combine again. \:D\
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
55 $str .= "\n".
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
56 rtrim(
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
57 implode("\n", array_map(
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
58 function ($item)
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
59 {
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
60 return "\t".$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 explode("\n", wordwrap(
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
63 preg_replace("#\s*\[(p|br)\]\s*#", "\n[\\1]\n", $data["desc"]),
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
64 68, "\n")
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
65 ))));
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
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
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
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
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
75 fwrite($file, $str);
2741
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
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
78 fwrite($file,
be34110271ed Improve reformatter.
Matti Hamalainen <ccr@tnsp.org>
parents: 2741
diff changeset
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 ?>