first commit

This commit is contained in:
Jörg Lohrer 2025-03-22 06:42:17 +01:00
commit 45255b2c80
4 changed files with 70 additions and 0 deletions

0
README.md Normal file
View file

42
import.php Normal file
View file

@ -0,0 +1,42 @@
<?php
$url = 'https://git.rpi-virtuell.de/Comenius-Institut/FOERBICO/raw/branch/main/Website/content/posts/2025-02-25-KLT-M%C3%BCnster/index.md'; // <- Ersetzen mit echter URL
$markdown = file_get_contents($url);
preg_match('/^---\s*(.*?)\s*---\s*(.*)$/s', $markdown, $matches);
$yaml_raw = $matches[1];
$content_md = $matches[2];
require 'lib/spyc.php';
require 'lib/Parsedown.php';
$yaml = Spyc::YAMLLoadString($yaml_raw);
$html = (new Parsedown())->text($content_md);
$data = [
'title' => $yaml['name'] ?? 'Kein Titel',
'content' => $html,
'status' => 'publish',
'excerpt' => $yaml['description'] ?? '',
'slug' => $yaml['url'] ?? '',
];
$response = wp_rest_post($data);
echo "\n✅ Beitrag ID: " . ($response['id'] ?? 'Fehler');
function wp_rest_post($data) {
$url = 'http://markdownimport.local/wp-json/wp/v2/posts';
$username = 'wpadmin';
$password = 'CZOm Nsa4 Gptt HS8c UVbl qSuK';
$auth = base64_encode("$username:$password");
$options = [
'http' => [
'method' => 'POST',
'header' => "Authorization: Basic $auth\r\nContent-Type: application/json\r\n",
'content' => json_encode($data),
]
];
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return json_decode($result, true);
}

6
lib/Parsedown.php Normal file
View file

@ -0,0 +1,6 @@
<?php
class Parsedown {
public function text($text) {
return '<p>' . nl2br(htmlspecialchars($text)) . '</p>';
}
}

22
lib/spyc.php Normal file
View file

@ -0,0 +1,22 @@
<?php
class Spyc {
public static function YAMLLoadString($input) {
return self::YAMLLoadInline($input);
}
private static function YAMLLoadInline($input) {
$lines = explode("\n", $input);
$data = [];
foreach ($lines as $line) {
if (preg_match('/^([a-zA-Z0-9_\-]+):\s*(.*)$/', trim($line), $matches)) {
$key = $matches[1];
$value = $matches[2];
if ($value === 'true') $value = true;
elseif ($value === 'false') $value = false;
elseif (is_numeric($value)) $value = $value + 0;
$data[$key] = $value;
}
}
return $data;
}
}