view 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
line wrap: on
line source

#!/usr/bin/php
<?php
require "common.inc.php";


function mpWriteWizInfoFile($filename, &$table)
{
  // "Categorize" the entries based on the data they have
  $categories = [];
  foreach ($table as $name => $data)
  {
    $score = 0;

    if (isset($data["countries"])) $score++;
    if (isset($data["homeURL"])) $score++;
    if (isset($data["homeURL"])) $score++;
    if (isset($data["desc"])) $score = 5;

    $categories[$score][$name] = $data;
  }

  // Sort categories descendingly
  krsort($categories, SORT_NUMERIC);

  // File header
  if (($file = @fopen($filename, "w")) === false)
    return FALSE;

  fwrite($file,
    "# Information about coders/wizards. Format of this file:\n".
    "# name;[3-letter country code];[http://homepageURL|bat];[https?://pictureURL|image.jpg];[freeform description]\$\n\n");

  foreach ($categories as $id => $cat)
  {
    // Sort each category alphabetically
    ksort($cat, SORT_STRING);

    // Output each entry in this category
    foreach ($cat as $name => $data)
    {
      $names = array_merge([$data["name"] ], $data["names"]);

      $str = implode("|", $names).";".
        (isset($data["countries"]) ? implode("|", $data["countries"]) : "").";".
        (isset($data["homeURL"]) ? $data["homeURL"] : "").";".
        (isset($data["imageURL"]) ? $data["imageURL"] : "").";";

      // Entries with some information are formatted differently
      if ($id > 0)
      {
        if (isset($data["desc"]) && strlen($data["desc"]) > 0)
        {
          // If we have a description, we format it via black magic:
          // add linefeeds to certain elements ([p|br]) and then
          // run through wordwrap(). Split to lines and indent each
          // line with tab. Combine again. \:D\
          $str .= "\n".
            rtrim(
            implode("\n", array_map(
            function ($item)
            {
              return "\t".$item;
            },
            explode("\n", wordwrap(
              preg_replace("#\s*\[(p|br)\]\s*#", "\n[\\1]\n", $data["desc"]),
              68, "\n")
            ))));
        }

        $str .= "\n\t\$\n\n";
      }
      else
      {
        $str .= "\$\n";
      }

      fwrite($file, $str);
    }

    fwrite($file,
      "\n#########################################################\n\n");
  }

  fclose($file);
  return TRUE;
}


// Prevent non-cli execution
if (php_sapi_name() != "cli" || !empty($_SERVER["REMOTE_ADDR"]))
  die("You can only run this script as a commandline application.\n");

if (count($argv) < 2)
{
  die("Usage: ".$argv[0]." <output wizards.txt>\n");
}

$table = mpReadWizInfoFiles();
mpWriteWizInfoFile($argv[1], $table);
if ($errorSet)
{
  echo implode("", array_map(
    function ($item)
    {
      return "ERROR: ".$item."\n";
    }, $errorMsgs));
}

?>