changeset 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 4caa26c12052
children fd3b61a8a152
files src/reformat_wizards.php www/common.inc.php www/info.php www/loc.php
diffstat 4 files changed, 114 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/src/reformat_wizards.php	Mon Mar 11 15:11:16 2024 +0200
+++ b/src/reformat_wizards.php	Tue Mar 12 15:47:58 2024 +0200
@@ -38,7 +38,7 @@
     // Output each entry in this category
     foreach ($cat as $name => $data)
     {
-      $str = $name.";".
+      $str = implode("|", $data["names"]).";".
         (isset($data["countries"]) ? implode("|", $data["countries"]) : "").";".
         (isset($data["homeURL"]) ? $data["homeURL"] : "").";".
         (isset($data["imageURL"]) ? $data["imageURL"] : "").";";
--- a/www/common.inc.php	Mon Mar 11 15:11:16 2024 +0200
+++ b/www/common.inc.php	Tue Mar 12 15:47:58 2024 +0200
@@ -460,25 +460,43 @@
 }
 
 
-function mpStoreWizInfoField(&$table, $field, $data)
+function mpStoreWizInfoField(&$wizInfo, $field, $data)
 {
   $str = trim($data);
   if (strlen($str) > 0)
-    $table[$field] = $str;
+    $wizInfo[$field] = $str;
 }
 
 
-function mpStoreWizInfoRecordInfo(&$table, $record)
+function mpStoreWizInfoRecordInfo($nline, &$wizInfo, $record)
 {
-  $name = trim($record[1]);
-  if (isset($table[$name]))
+  // Parse and check names
+  $allnames = preg_split("/\s*\|\s*/", trim($record[1]));
+  foreach ($allnames as $name)
   {
-    mpError("Item ".$name." is already set!");
+    if ($name == "" ||
+        $name != strtoupper(substr($name, 0, 1)).strtolower(substr($name, 1)))
+    {
+      mpError("Invalid name in wizard record (line #".$nline."): '".$name."'.");
+    }
+  }
+
+  // Use first name as primary "key"
+  $keyname = $allnames[0];
+  if (isset($wizInfo[$keyname]))
+  {
+    mpError("Wizard record '".$keyname."' is already set on line #".
+      $wizInfo[$keyname]["nline"].", vs line #".$nline);
     return FALSE;
   }
   else
   {
-    $tmp = ["name" => $name, "areas" => 0];
+    $data = [
+      "name" => $keyname,
+      "names" => array_slice($allnames, 1),
+      "areas" => 0,
+      "nline" => $nline,
+    ];
 
     $countries = array_map(
       function ($item)
@@ -492,21 +510,22 @@
       }));
 
     if (count($countries) > 0)
-      $tmp["countries"] = $countries;
+      $data["countries"] = $countries;
 
-    mpStoreWizInfoField($tmp, "homeURL", $record[3]);
-    mpStoreWizInfoField($tmp, "imageURL", $record[4]);
-    mpStoreWizInfoField($tmp, "desc", $record[5]);
+    mpStoreWizInfoField($data, "homeURL", $record[3]);
+    mpStoreWizInfoField($data, "imageURL", $record[4]);
+    mpStoreWizInfoField($data, "desc", $record[5]);
 
-    $table[$name] = $tmp;
+    $wizInfo[$keyname] = $data;
 
     return TRUE;
   }
 }
 
 
-function mpParseWizInfoFile($filename, &$table)
+function mpParseWizInfoFile($filename, &$wizInfo)
 {
+  $startLine = $nline = 0;
   if (($file = @fopen($filename, "r")) === false)
     return FALSE;
 
@@ -514,6 +533,7 @@
   while (!feof($file))
   {
     $line = mpChConv(trim(fgets($file)));
+    $nline++;
 
     if (strlen($line) == 0 || $line[0] == "#")
       continue;
@@ -524,7 +544,7 @@
       {
         $record[5] .= " ".trim(substr($line, 0, -1));
         $contMode = FALSE;
-        mpStoreWizInfoRecordInfo($table, $record);
+        mpStoreWizInfoRecordInfo($startLine, $wizInfo, $record);
       }
       else
       {
@@ -532,13 +552,16 @@
       }
     }
     else
-    if (preg_match("/^([A-Z][a-z]+);([a-z\|]+)?;(https?:\/\/[^;]+|bat)?;([^;]+)?;([^\$]*)\\\$$/", $line, $record))
-      mpStoreWizInfoRecordInfo($table, $record);
+    if (preg_match("/^([A-Za-z\|]+);([a-z\|]+)?;(https?:\/\/[^;]+|bat)?;([^;]+)?;([^\$]*)\\\$$/", $line, $record))
+      mpStoreWizInfoRecordInfo($nline, $wizInfo, $record);
     else
-    if (preg_match("/^([A-Z][a-z]+);([a-z\|]+)?;(https?:\/\/[^;]+|bat)?;([^;]+)?;(.*)$/", $line, $record))
+    if (preg_match("/^([A-Za-z\|]+);([a-z\|]+)?;(https?:\/\/[^;]+|bat)?;([^;]+)?;(.*)$/", $line, $record))
+    {
+      $startLine = $nline;
       $contMode = TRUE;
+    }
     else
-      mpError($line);
+      mpError("Wizard record line #".$nline.": ".$line);
   }
 
   fclose($file);
@@ -546,6 +569,18 @@
 }
 
 
+function mpMakeWizInfoAliases($wizInfo)
+{
+  $aliases = [];
+  foreach ($wizInfo as $name => $data)
+  {
+    foreach ($data["names"] as $nname)
+      $aliases[$nname] = $name;
+  }
+  return $aliases;
+}
+
+
 function mpReadWizInfoFiles()
 {
   $wizInfo = [];
--- a/www/info.php	Mon Mar 11 15:11:16 2024 +0200
+++ b/www/info.php	Tue Mar 12 15:47:58 2024 +0200
@@ -53,71 +53,65 @@
 }
 
 
-/* Print table of wizards
- */
+// Get area counts
 foreach ($locTable as $location)
 {
   foreach ($location["authors"] as $wiz)
   {
     $name = $wiz["name"];
+
+    // Many wizards do not have an entry in wizard info table
     if (!isset($wizTable[$name]))
-      $wizTable[$name] = array("name" => $name, "areas" => 0);
+    {
+      $wizTable[$name] = [
+        "name" => $name,
+        "names" => [],
+        "areas" => 0,
+      ];
+    }
+
     $wizTable[$name]["areas"]++;
   }
 }
 
-if (count($wizTable) > 0)
-{
-  /* Make alphabetically sorted table of wizards
-   */
-  foreach ($wizTable as $alpha => $data)
-    $alphaTable[$alpha[0]][] = $data;
+// Make alphabetically sorted table of wizards
+foreach ($wizTable as $name => $data)
+  $alphaTable[$name[0]][$name] = $data;
 
-  ksort($alphaTable, SORT_STRING);
+ksort($alphaTable, SORT_STRING);
 
-
-  /* Print wizards alphabetically per first character of name
-   */
-  $totalWiz = 0;
-  $maxRow = 6;
-  foreach ($alphaTable as $alpha => $data)
-  if (count($data) > 0)
-  {
-    usort($data, "wizardSort");
+// Print wizards alphabetically per first character of name
+foreach ($alphaTable as $alpha => $data)
+if (count($data) > 0)
+{
+  usort($data, "wizardSort");
 
-    $letter = strtoupper($alpha);
-    echo "<h3><a id=\"ch".$letter."\"></a>".$letter."</h3>\n";
-    echo "<div class=\"locTable\">\n";
-    $n = 0;
-
-    foreach ($data as $wizard)
-    {
-      $totalWiz++;
+  $letter = strtoupper($alpha);
+  echo "<h3><a id=\"ch".$letter."\"></a>".$letter."</h3>\n";
+  echo "<div class=\"locTable\">\n";
 
-      echo
-        "  <div class=\"locWizard ".((count($wizard) > 2 || isset($wizard["desc"])) ? "locWizHasDesc" : "locWizHasNoDesc")."\">".
-        "<a href=\"loc.php?a=".$wizard["name"]."\">".$wizard["name"]."</a>";
+  foreach ($data as $name => $wizard)
+  {
+    echo
+      "  <div class=\"locWizard ".
+      (isset($wizard["desc"]) ? "locWizHasDesc" : "locWizHasNoDesc")."\">".
+      "<a href=\"loc.php?a=".$wizard["name"]."\">".$wizard["name"]."</a>";
 
-      if ($wizard["areas"] > 0)
-        echo " <span class=\"locWizNumAreas\">(".$wizard["areas"].")</span>";
-
-      echo "</div>\n";
-    }
+    if ($wizard["areas"] > 0)
+      echo " <span class=\"locWizNumAreas\">(".$wizard["areas"].")</span>";
 
     echo "</div>\n";
   }
 
-  echo "<p><b>".$totalWiz."</b> wizards.</p>\n".
-    "</div>\n"; // end of contents div
-
-  // Print out the alpha link index
-  mpPrintExtraBoxAlphaList("ch", $alphaTable);
+  echo "</div>\n";
 }
-else
-{
-  echo "<p><b>No wizards known!</b></p>\n".
-    "</div>\n"; // end of contents div
-}
+
+echo
+  "<p><b>".count($wizTable)."</b> wizards.</p>\n".
+  "</div>\n"; // end of contents div
+
+// Print out the alpha link index
+mpPrintExtraBoxAlphaList("ch", $alphaTable);
 
 mpPrintPageFooter(FALSE);
 ?>
\ No newline at end of file
--- a/www/loc.php	Mon Mar 11 15:11:16 2024 +0200
+++ b/www/loc.php	Tue Mar 12 15:47:58 2024 +0200
@@ -111,6 +111,10 @@
       $wizTable = mpReadWizInfoFiles();
       apcu_store("wizTable", $wizTable, 3600);
     }
+
+    // Correct aliases
+    if (isset($wizTable[$authorName]))
+      $authorName = $wizTable[$authorName]["name"];
   }
   else
   {
@@ -231,20 +235,30 @@
 //
 if (isset($authorName))
 {
-  if (isset($wizTable[$authorName]) && count($wizTable[$authorName]) > 1)
+  if (isset($wizTable[$authorName]))
   {
+    $entry = &$wizTable[$authorName];
+
     // Profile picture
-    //$imageURL = $wizImageURL.(qcheck($wizTable[$authorName], "imageURL", $imageName) ? $imageName : "unknown.png");
+    //$imageURL = $wizImageURL.(qcheck($entry, "imageURL", $imageName) ? $imageName : "unknown.png");
     $imageURL = $wizImageURL."unknown.png";
 
     echo
       "<div class=\"wizInfoBox\">\n".
-      "   <div class=\"wizImage\"><img src=\"".$imageURL."\" alt=\"".$authorName."\" /></div>\n".
+      "   <div class=\"wizImage\"><img src=\"".$imageURL."\" alt=\"".$entry["name"]."\" /></div>\n".
       "   <div class=\"wizInfo\">\n".
-      "    <h2>".$authorName."</h2>\n";
+      "    <h2>".$entry["name"];
+
+    if (count($entry["names"]) > 0)
+    {
+      echo " (aka ".implode(" aka ", $entry["names"]).")";
+    }
+
+    echo
+      "</h2>\n";
 
     // Description block
-    if (qcheck($wizTable[$authorName], "desc", $str))
+    if (qcheck($entry, "desc", $str))
     {
       // Handle special tags
       $str = preg_replace("/\~([A-Z][a-z]+)\~/i", "<a href=\"?a=\${1}\">\${1}</a>", $str);
@@ -258,7 +272,7 @@
     // Links, etc.
     echo "    [<a href=\"".mpFingerURL($authorName)."\">Finger</a>]\n";
 
-    if (qcheck($wizTable[$authorName], "homeURL", $s))
+    if (qcheck($entry, "homeURL", $s))
     {
       if ($s == "bat")
         $s = "https://wiz.bat.org/~".strtolower($authorName)."/";
@@ -266,7 +280,7 @@
       echo "    [<a href=\"".$s."\">Homepage</a>]\n";
     }
 
-    if (qcheck($wizTable[$authorName], "countries", $s))
+    if (qcheck($entry, "countries", $s))
     {
       echo "    [".implode(" | ", array_map(
         function ($item)