&1', [1 => ['pipe', 'w']], $pipes);
if (!is_resource($proc)) return [1, 'proc_open nicht möglich'];
$out = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$code = proc_close($proc);
return [$code, rtrim((string)$out)];
}
/**
* Button zurück zum Import-Skript – nur, wenn es neben diesem Skript liegt
*/
function edwiki_back()
{
if (EDWIKI_IMPORT === '' || !is_file(DOKU_INC . EDWIKI_IMPORT)) return '';
return '
Zurück zu '
. hsc(EDWIKI_IMPORT) . '
';
}
function edwiki_page($title, $body)
{
$body .= edwiki_back();
echo '' . hsc($title) . ' '
. ''
. '' . hsc($title) . ' ' . $body . '';
exit;
}
/**
* JSON-Datei schreiben (stabil formatiert, damit Git-Diffs lesbar bleiben)
*/
function edwiki_write_json($file, $data)
{
io_makeFileDir($file);
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return io_saveFile($file, $json . "\n");
}
/**
* struct-Daten nach data/struct/ exportieren
*
* @return array [Anzahl Schemas, Anzahl Datensätze, Fehlermeldungen]
*/
function edwiki_export_struct()
{
global $repo;
$dir = $repo . '/struct';
$errors = [];
if (plugin_isdisabled('struct')) return [0, 0, ['struct-Plugin nicht aktiv']];
$schemas = [];
$rows = 0;
$written = [];
foreach (Schema::getAll() as $table) {
try {
$schema = new Schema($table);
$definition = json_decode($schema->toJSON(), true);
unset($definition['id']); // ändert sich bei jeder Schema-Version
$schemas[$table] = $definition;
$search = new Search();
$search->addSchema($table);
$search->addColumn('*');
$search->addSort('%pageid%', true);
$result = $search->getRows();
$pids = $search->getPids();
$data = [];
foreach ($result as $i => $values) {
$entry = [];
foreach ($values as $value) {
$raw = $value->getRawValue();
$entry[$value->getColumn()->getLabel()] = $raw;
}
$key = $pids[$i] ?? (string)$i;
$data[$key] = $entry;
}
ksort($data, SORT_STRING);
edwiki_write_json("$dir/data/$table.json", $data);
$written["$table.json"] = true;
$rows += count($data);
} catch (\Throwable $e) {
$errors[] = "$table: " . $e->getMessage();
}
}
ksort($schemas, SORT_STRING);
edwiki_write_json("$dir/schemas.json", $schemas);
$patterns = Assignments::getInstance(true)->getAllPatterns();
usort($patterns, fn($a, $b) => [$a['tbl'], $a['pattern']] <=> [$b['tbl'], $b['pattern']]);
edwiki_write_json("$dir/assignments.json", $patterns);
// Dateien gelöschter Schemas entfernen
foreach (glob("$dir/data/*.json") ?: [] as $file) {
if (!isset($written[basename($file)])) @unlink($file);
}
return [count($schemas), $rows, $errors];
}
/**
* data/.gitignore um /struct/ ergänzen, falls nötig
*/
function edwiki_gitignore()
{
global $repo;
$file = $repo . '/.gitignore';
$text = (string)@file_get_contents($file);
if (preg_match('#^!/struct/?\s*$#m', $text)) return;
$text = rtrim($text) . "\n!/struct/\n";
file_put_contents($file, $text);
}
// nur Admins
if (!auth_isadmin()) {
http_response_code(403);
edwiki_page('Kein Zugriff', 'Bitte zuerst im Wiki als Administrator anmelden und die Seite neu laden.
');
}
if (!is_dir($repo . '/.git')) {
edwiki_page('Fehler', 'Kein Git-Repository unter ' . hsc($repo) . ' gefunden.
');
}
// Export immer zuerst erneuern
edwiki_gitignore();
[$schemaCount, $rowCount, $exportErrors] = edwiki_export_struct();
$exportInfo = 'struct-Export erneuert: ' . $schemaCount . ' Schemas, '
. $rowCount . ' Datensätze → data/struct/
';
if ($exportErrors) {
$exportInfo .= 'Fehler beim Export:
' . hsc(implode("\n", $exportErrors)) . ' ';
}
// was liegt an?
[, $status] = edwiki_git(array_merge(['status', '--porcelain', '--untracked-files=all', '--'], $paths));
$lines = $status === '' ? [] : explode("\n", $status);
if (!$INPUT->post->bool('run') || !checkSecurityToken()) {
$body = 'Repository: ' . hsc($repo) . ', Branch ' . hsc($branch) . '
' . $exportInfo;
if (!$lines) {
$body .= 'Alles ist bereits übertragen – nichts zu tun.
';
} else {
$new = count(array_filter($lines, fn($l) => str_starts_with($l, '??')));
$body .= '' . count($lines) . ' Änderungen (davon ' . $new . ' neue Dateien):
'
. '' . hsc($status) . ' '
. ''
. '?? = neu, M = geändert, D = gelöscht
';
}
edwiki_page('Wiki nach GitHub übertragen', $body);
}
if (!$lines) edwiki_page('Nichts zu tun', $exportInfo . 'Alles ist bereits übertragen.
');
// übertragen (Commit-Nachricht nur ASCII: der Server entfernt sonst Umlaute)
$log = '';
$ok = true;
$user = $USERINFO['name'] ?? $INPUT->server->str('REMOTE_USER');
$mail = $USERINFO['mail'] ?? '';
$steps = [
array_merge(['add', '--all', '--'], $paths),
['-c', 'user.name=' . $user, '-c', 'user.email=' . $mail,
'commit', '-m', 'Wiki-Seiten und struct-Export uebertragen (' . count($lines) . ' Aenderungen) by ' . $user],
['push', 'origin', $branch],
];
foreach ($steps as $step) {
[$code, $out] = edwiki_git($step);
$log .= '$ git ' . implode(' ', $step) . "\n" . $out . "\n\n";
if ($code !== 0) {
$ok = false;
break;
}
}
edwiki_page(
$ok ? 'Übertragen' : 'Fehler beim Übertragen',
$exportInfo
. ($ok ? 'Fertig. Seiten und struct-Daten sind auf GitHub.
'
: 'Ein Schritt ist fehlgeschlagen, siehe Ausgabe. Die Wiki-Seiten selbst sind unverändert.
')
. '' . hsc($log) . ' '
);