Scratchpads V1

This commit is contained in:
2026-01-09 09:26:39 +01:00
parent 16a07701ee
commit 0948f50d76
15 changed files with 718 additions and 27 deletions

View File

@@ -11,6 +11,7 @@ class admin_plugin_luxtools_main extends DokuWiki_Admin_Plugin
/** @var string[] */
protected $configKeys = [
'paths',
'scratchpad_paths',
'allow_in_comments',
'defaults',
'extensions',
@@ -51,6 +52,11 @@ class admin_plugin_luxtools_main extends DokuWiki_Admin_Plugin
$paths = $INPUT->str('paths');
$paths = str_replace(["\r\n", "\r"], "\n", $paths);
$newConf['paths'] = $paths;
$scratchpadPaths = $INPUT->str('scratchpad_paths');
$scratchpadPaths = str_replace(["\r\n", "\r"], "\n", $scratchpadPaths);
$newConf['scratchpad_paths'] = $scratchpadPaths;
$newConf['allow_in_comments'] = (int)$INPUT->bool('allow_in_comments');
$newConf['defaults'] = $INPUT->str('defaults');
$newConf['extensions'] = $INPUT->str('extensions');
@@ -88,6 +94,12 @@ class admin_plugin_luxtools_main extends DokuWiki_Admin_Plugin
echo '<textarea name="paths" rows="8" cols="80" class="edit">' . hsc($paths) . '</textarea>';
echo '</label><br />';
// scratchpad_paths: multiline textarea
$scratchpadPaths = (string)$this->getConf('scratchpad_paths');
echo '<label class="block"><span>' . hsc($this->getLang('scratchpad_paths')) . '</span><br />';
echo '<textarea name="scratchpad_paths" rows="6" cols="80" class="edit">' . hsc($scratchpadPaths) . '</textarea>';
echo '</label><br />';
// allow_in_comments
$checked = $this->getConf('allow_in_comments') ? ' checked="checked"' : '';
echo '<label class="block"><span>' . hsc($this->getLang('allow_in_comments')) . '</span> ';
@@ -128,7 +140,11 @@ class admin_plugin_luxtools_main extends DokuWiki_Admin_Plugin
}
/**
* Persist plugin settings to conf/plugins/luxtools.local.php.
* Persist plugin settings to conf/local.php.
*
* DokuWiki loads conf/local.php on each request; values written there will
* be available via getConf(). We write into a dedicated BEGIN/END block so
* updates are idempotent.
*
* @param array $newConf
* @return bool
@@ -138,30 +154,62 @@ class admin_plugin_luxtools_main extends DokuWiki_Admin_Plugin
if (!defined('DOKU_CONF')) return false;
$plugin = 'luxtools';
$confDir = DOKU_CONF . 'plugins/';
$file = $confDir . $plugin . '.local.php';
$file = DOKU_CONF . 'local.php';
if (function_exists('io_mkdir_p')) {
io_mkdir_p($confDir);
} elseif (!@is_dir($confDir)) {
@mkdir($confDir, 0777, true);
$existing = '';
if (@is_file($file) && @is_readable($file)) {
$existing = (string)file_get_contents($file);
}
if ($existing === '') {
$existing = "<?php\n";
}
if (!str_starts_with($existing, "<?php")) {
// unexpected format - do not overwrite
return false;
}
// Only write known keys; ignore extras.
$lines = ["<?php"];
$begin = "// BEGIN LUXTOOLS\n";
$end = "// END LUXTOOLS\n";
// Build the block
$lines = [$begin];
foreach ($this->configKeys as $key) {
if (!array_key_exists($key, $newConf)) continue;
$value = $newConf[$key];
$lines[] = '$conf[' . var_export($key, true) . '] = ' . $this->exportPhpValue($value, $key) . ';';
$lines[] = '$conf[\'plugin\'][\'' . $plugin . '\'][' . var_export($key, true) . '] = ' . $this->exportPhpValue($value, $key) . ';';
}
$lines[] = '';
$content = implode("\n", $lines);
$lines[] = $end;
$block = implode("\n", $lines);
// Replace or append the block in conf/local.php
$beginPos = strpos($existing, $begin);
if ($beginPos !== false) {
$endPos = strpos($existing, $end, $beginPos);
if ($endPos === false) {
// malformed existing block - append a new one
$content = rtrim($existing) . "\n\n" . $block;
} else {
$endPos += strlen($end);
$content = substr($existing, 0, $beginPos) . $block . substr($existing, $endPos);
}
} else {
$content = rtrim($existing) . "\n\n" . $block;
}
$ok = false;
if (function_exists('io_saveFile')) {
return (bool)io_saveFile($file, $content);
$ok = (bool)io_saveFile($file, $content);
} else {
$ok = @file_put_contents($file, $content, LOCK_EX) !== false;
}
return @file_put_contents($file, $content, LOCK_EX) !== false;
// Best-effort cleanup: stop creating/using legacy conf/plugins/luxtools.local.php
$legacy = DOKU_CONF . 'plugins/' . $plugin . '.local.php';
if (@is_file($legacy)) {
@unlink($legacy);
}
return $ok;
}
/**