<?php include "head.php"; ?>
<?php
$q = $_GET['file'];
$title = pathinfo(basename($q), PATHINFO_FILENAME);
$l = @file_get_contents("./mediawiki/wiki_pages/$q");
if ($l === false) {
    $l = "No such wiki entry<br><small>The ARPD only fetches updated & new articles from the teengovernment wiki every 6 hours, please wait for changes to reflect if a page is new.</small>";
}

function wikiToHtml($text) {
    // Handle redirects
    if (preg_match('/^#REDIRECT\s*\[\[(.*?)\]\]/i', $text, $matches)) {
        $target = htmlspecialchars($matches[1]);
        return "<script>window.location.replace(\"/wiki.php?file=$target.txt\");</script> <p>Redirect to <a href=\"/wiki.php?file=$target.txt\">$target</a></p>";
    }

    // Remove categories
    $text = preg_replace('/\[\[Category:.*?\]\]/', '', $text);

    // Headings
    $text = preg_replace_callback('/^(={2,6})\s*(.*?)\s*\1$/m', function($matches) {
        $level = strlen($matches[1]);
        $content = htmlspecialchars($matches[2]);
        return "<h$level>$content</h$level>";
    }, $text);

    // Bold
    $text = preg_replace("/'''(.*?)'''/", '<b>$1</b>', $text);

    // Italic
    $text = preg_replace("/''(.*?)''/", '<i>$1</i>', $text);

    // File links with optional size, alignment, thumb, caption
    $text = preg_replace_callback('/\[\[File:(.*?)(?:\|([^\]]+))?\]\]/i', function($matches) {
        $parts = explode('|', $matches[1]);
        $filename = htmlspecialchars(array_shift($parts));
        $filename = ucfirst($filename);
        $hash = md5($filename);
        $folderPath = $hash[0] . '/' . substr($hash, 0, 2); // "1/1a"
        $thumbnailUrl = "https://static.wikitide.net/teengovernmentwiki/thumb/$folderPath/$filename/200px-$filename";

        $caption = '';
        $width = '';
        $align = '';
        foreach ($parts as $part) {
            if (preg_match('/^\d+px$/', $part)) $width = $part;
            elseif (in_array(strtolower($part), ['left','right','center'])) $align = $part;
            elseif (strtolower($part) === 'thumb') continue;
            else $caption = htmlspecialchars($part);
        }
        $style = ($width ? "width:$width;" : "") . ($align ? " float:$align; margin:5px;" : "");
        return "<div style=\"$style\"><img src=\"$thumbnailUrl\" alt=\"$caption\"><div>$caption</div></div>";
    }, $text);

    // Links with pipe [[Link|Text]]
    $text = preg_replace_callback('/\[\[(.*?)\|(.*?)\]\]/', function($matches) {
        $url = htmlspecialchars($matches[1]);
        $label = htmlspecialchars($matches[2]);
        return "<a href=\"/wiki.php?file=$url.txt\">$label</a>";
    }, $text);

    // Links without pipe [[Link]]
    $text = preg_replace_callback('/\[\[(.*?)\]\]/', function($matches) {
        $url = htmlspecialchars($matches[1]);
        return "<a href=\"/wiki.php?file=$url.txt\">$url</a>";
    }, $text);

    // External links [https://example.com Label]
    $text = preg_replace_callback('/\[(https?:\/\/[^\s]+)\s+(.*?)\]/', function($matches) {
        $url = htmlspecialchars($matches[1]);
        $label = htmlspecialchars($matches[2]);
        return "<a href=\"$url\">$label</a>";
    }, $text);

    // References placeholder
    $text = str_replace('<references />', '<div class="references">References go here</div>', $text);

    // Convert newlines to paragraphs
    $text = nl2br(trim($text));

    return $text;
}

$f = wikiToHtml($l);
?>
<div class="law">
    <h2><?= $title; ?></h2>
<p><?= $f; ?></p>
<small>To edit one of these go to the wiki at <a href="https://teengovernment.miraheze.org/wiki/<?= $title; ?>">teengovernment.miraheze.org</a></small>
</div>
<?php include "foot.php"; ?>
