use dcModuleDefine rather than properties array

master
Jean-Christian Paul Denis 2023-04-06 21:14:56 +02:00
parent e794fc1e70
commit 1877d40fce
Signed by: JcDenis
GPG Key ID: 1B5B8C5B90B6C951
11 changed files with 72 additions and 99 deletions

View File

@ -16,6 +16,7 @@ namespace Dotclear\Plugin\improve;
/* dotclear */ /* dotclear */
use dcCore; use dcCore;
use dcModuleDefine;
use dcPage; use dcPage;
/* clearbricks */ /* clearbricks */
@ -36,8 +37,8 @@ use ArrayObject;
*/ */
abstract class Action abstract class Action
{ {
/** @var array<string,mixed> Current module */ /** @var dcModuleDefine Current module */
protected $module = []; protected $module;
/** @var string Current full path */ /** @var string Current full path */
protected $path_full = ''; protected $path_full = '';
@ -84,6 +85,7 @@ abstract class Action
final public function __construct() final public function __construct()
{ {
$this->class_name = str_replace(Utils::getActionsNS(), '', get_called_class()); $this->class_name = str_replace(Utils::getActionsNS(), '', get_called_class());
$this->module = new dcModuleDefine('undefined');
$settings = dcCore::app()->blog->settings->get(My::id())->get('settings_' . $this->class_name); $settings = dcCore::app()->blog->settings->get(My::id())->get('settings_' . $this->class_name);
if (null != $settings) { if (null != $settings) {
@ -291,9 +293,9 @@ abstract class Action
* *
* @see Improve::sanitizeModule() * @see Improve::sanitizeModule()
* *
* @param array<string> $module Full array of module definitons * @param dcModuleDefine $module Module definitons
*/ */
final public function setModule(array $module): bool final public function setModule(dcModuleDefine $module): bool
{ {
$this->module = $module; $this->module = $module;

View File

@ -17,6 +17,7 @@ namespace Dotclear\Plugin\improve;
/* dotclear */ /* dotclear */
use dcCore; use dcCore;
use dcLog; use dcLog;
use dcModuleDefine;
/* clearbricks */ /* clearbricks */
use path; use path;
@ -190,10 +191,9 @@ class Core
return $this->disabled; return $this->disabled;
} }
public function fixModule(string $type, string $id, array $properties, array $actions): float public function fixModule(dcModuleDefine $module, array $actions): float
{ {
$time_start = microtime(true); $time_start = microtime(true);
$module = Module::clean($type, $id, $properties);
$workers = []; $workers = [];
foreach ($actions as $action) { foreach ($actions as $action) {
@ -210,10 +210,10 @@ class Core
// action: open module // action: open module
$action->openModule(); $action->openModule();
} }
if (!isset($module['sroot']) || !$module['root_writable'] || !is_writable($module['sroot'])) { if (!$module->get('root_writable') || !is_writable($module->get('root'))) {
throw new Exception(__('Module path is not writable')); throw new Exception(__('Module path is not writable'));
} }
$tree = self::getModuleFiles($module['sroot']); $tree = self::getModuleFiles($module->get('root'));
foreach ($tree as $file) { foreach ($tree as $file) {
if (!file_exists($file[0])) { if (!file_exists($file[0])) {
continue; continue;

View File

@ -54,13 +54,6 @@ class Manage extends dcNsProcess
&& dcCore::app()->auth->isSuperAdmin() && dcCore::app()->auth->isSuperAdmin()
&& My::phpCompliant(); && My::phpCompliant();
if (static::$init) {
self::$improve = new Core();
self::$type = self::getType();
self::$module = self::getModule();
self::$action = self::getAction();
}
return static::$init; return static::$init;
} }
@ -125,51 +118,34 @@ class Manage extends dcNsProcess
private static function comboModules(): array private static function comboModules(): array
{ {
$allow_distrib = (bool) dcCore::app()->blog->settings->get(My::id())->get('allow_distrib');
$official = [
'plugin' => explode(',', DC_DISTRIB_PLUGINS),
'theme' => explode(',', DC_DISTRIB_THEMES),
];
if (!(dcCore::app()->themes instanceof dcThemes)) { if (!(dcCore::app()->themes instanceof dcThemes)) {
dcCore::app()->themes = new dcThemes(); dcCore::app()->themes = new dcThemes();
dcCore::app()->themes->loadModules(dcCore::app()->blog->themes_path, null); dcCore::app()->themes->loadModules(dcCore::app()->blog->themes_path, null);
} }
$combo_modules = []; $combo_modules = [];
$modules = self::getModules(self::$type == 'plugin' ? 'plugins' : 'themes'); foreach (self::$type == 'plugin' ? dcCore::app()->plugins->getDefines() : dcCore::app()->themes->getDefines() as $module) {
foreach ($modules as $id => $m) { if (!$module->get('root_writable') || !dcCore::app()->blog->settings->get(My::id())->get('allow_distrib') && $module->get('distributed')) {
if (!$m['root_writable'] || !$allow_distrib && in_array($id, $official[self::$type])) {
continue; continue;
} }
$combo_modules[sprintf(__('%s (%s)'), __($m['name']), $id)] = $id; $combo_modules[sprintf(__('%s (%s)'), __($module->get('name')), $module->getId())] = $module->getId();
} }
dcUtils::lexicalKeySort($combo_modules); dcUtils::lexicalKeySort($combo_modules);
return array_merge([__('Select a module') => '-'], $combo_modules); return array_merge([__('Select a module') => '-'], $combo_modules);
} }
public static function getModules(string $type, ?string $id = null): ?array
{
$type = $type == 'themes' ? 'themes' : 'plugins';
$modules = array_merge(dcCore::app()->{$type}->getDisabledModules(), dcCore::app()->{$type}->getModules());
if (empty($id)) {
return $modules;
} elseif (array_key_exists($id, $modules)) {
return $modules[$id];
}
return null;
}
public static function process(): bool public static function process(): bool
{ {
if (!static::$init) { if (!static::$init) {
return false; return false;
} }
self::$improve = new Core();
self::$type = self::getType();
self::$module = self::getModule();
self::$action = self::getAction();
$log_id = ''; $log_id = '';
$done = self::setPreferences(); $done = self::setPreferences();
@ -181,9 +157,7 @@ class Manage extends dcNsProcess
} else { } else {
try { try {
$time = self::$improve->fixModule( $time = self::$improve->fixModule(
self::$type, self::$type == 'plugin' ? dcCore::app()->plugins->getDefine(self::$module) : dcCore::app()->themes->getDefine(self::$module),
self::$module,
self::getModules(self::$type == 'plugin' ? 'plugins' : 'themes', self::$module),
$_POST['actions'] $_POST['actions']
); );
$log_id = self::$improve->writeLogs(); $log_id = self::$improve->writeLogs();

View File

@ -184,10 +184,10 @@ class cssheader extends Action
$this->bloc_wildcards, $this->bloc_wildcards,
[ [
date('Y'), date('Y'),
$this->module['id'], $this->module->getId(),
$this->module['name'], $this->module->get('name'),
$this->module['author'], $this->module->get('author'),
$this->module['type'], $this->module->get('type'),
dcCore::app()->auth->getInfo('user_cn'), dcCore::app()->auth->getInfo('user_cn'),
dcCore::app()->auth->getinfo('user_name'), dcCore::app()->auth->getinfo('user_name'),
dcCore::app()->auth->getInfo('user_email'), dcCore::app()->auth->getInfo('user_email'),

View File

@ -97,7 +97,7 @@ class dcstore extends Action
$content = $this->prettyXML($content); $content = $this->prettyXML($content);
try { try {
files::putContent($this->module['sroot'] . '/dcstore.xml', $content); files::putContent($this->module->get('root') . DIRECTORY_SEPARATOR . 'dcstore.xml', $content);
$this->setSuccess(__('Write dcstore.xml file.')); $this->setSuccess(__('Write dcstore.xml file.'));
} catch (Exception $e) { } catch (Exception $e) {
$this->setError(__('Failed to write dcstore.xml file')); $this->setError(__('Failed to write dcstore.xml file'));
@ -114,37 +114,34 @@ class dcstore extends Action
$rsp = new xmlTag('module'); $rsp = new xmlTag('module');
# id # id
if (empty($this->module['id'])) { $rsp->id = $this->module->getId();
$this->setError(__('unkow module id'));
}
$rsp->id = $this->module['id'];
# name # name
if (empty($this->module['oname'])) { if (empty($this->module->get('name'))) {
$this->setError(__('unknow module name')); $this->setError(__('unknow module name'));
} }
$rsp->name($this->module['oname']); $rsp->name($this->module->get('name'));
# version # version
if (empty($this->module['version'])) { if (empty($this->module->get('version'))) {
$this->setError(__('unknow module version')); $this->setError(__('unknow module version'));
} }
$rsp->version($this->module['version']); $rsp->version($this->module->get('version'));
# author # author
if (empty($this->module['author'])) { if (empty($this->module->get('author'))) {
$this->setError(__('unknow module author')); $this->setError(__('unknow module author'));
} }
$rsp->author($this->module['author']); $rsp->author($this->module->get('author'));
# desc # desc
if (empty($this->module['desc'])) { if (empty($this->module->get('desc'))) {
$this->setError(__('unknow module description')); $this->setError(__('unknow module description'));
} }
$rsp->desc($this->module['desc']); $rsp->desc($this->module->get('desc'));
# repository # repository
if (empty($this->module['repository'])) { if (empty($this->module->get('repository'))) {
$this->setError(__('no repository set in _define.php')); $this->setError(__('no repository set in _define.php'));
} }
@ -156,44 +153,44 @@ class dcstore extends Action
$rsp->file($file_pattern); $rsp->file($file_pattern);
# da dc_min or requires core # da dc_min or requires core
if (!empty($this->module['requires']) && is_array($this->module['requires'])) { if (!empty($this->module->get('requires')) && is_array($this->module->get('requires'))) {
foreach ($this->module['requires'] as $req) { foreach ($this->module->get('requires') as $req) {
if (!is_array($req)) { if (!is_array($req)) {
$req = [$req]; $req = [$req];
} }
if ($req[0] == 'core') { if ($req[0] == 'core') {
$this->module['dc_min'] = $req[1]; $this->module->set('dc_min', $req[1]);
break; break;
} }
} }
} }
if (empty($this->module['dc_min'])) { if (empty($this->module->get('dc_min'))) {
$this->setWarning(__('no minimum dotclear version')); $this->setWarning(__('no minimum dotclear version'));
} else { } else {
$rsp->insertNode(new xmlTag('da:dcmin', $this->module['dc_min'])); $rsp->insertNode(new xmlTag('da:dcmin', $this->module->get('dc_min')));
} }
# da details # da details
if (empty($this->module['details'])) { if (empty($this->module->get('details'))) {
$this->setWarning(__('no details URL')); $this->setWarning(__('no details URL'));
} else { } else {
$rsp->insertNode(new xmlTag('da:details', $this->module['details'])); $rsp->insertNode(new xmlTag('da:details', $this->module->get('details')));
} }
# da sshot # da sshot
//$rsp->insertNode(new xmlTag('da:sshot', $this->module['sshot'])); //$rsp->insertNode(new xmlTag('da:sshot', $this->module['sshot']));
# da section # da section
if (!empty($this->module['section'])) { if (!empty($this->module->get('section'))) {
$rsp->insertNode(new xmlTag('da:section', $this->module['section'])); $rsp->insertNode(new xmlTag('da:section', $this->module->get('section')));
} }
# da support # da support
if (empty($this->module['support'])) { if (empty($this->module->get('support'))) {
$this->setWarning(__('no support URL')); $this->setWarning(__('no support URL'));
} else { } else {
$rsp->insertNode(new xmlTag('da:support', $this->module['support'])); $rsp->insertNode(new xmlTag('da:support', $this->module->get('support')));
} }
# da tags # da tags
@ -229,10 +226,10 @@ class dcstore extends Action
'%author%', '%author%',
], ],
[ [
$this->module['type'], $this->module->get('type'),
$this->module['id'], $this->module->getId(),
$this->module['version'], $this->module->get('version'),
$this->module['author'], $this->module->get('author'),
], ],
$this->pattern $this->pattern
)); ));

View File

@ -154,9 +154,9 @@ class gitshields extends Action
], ],
[ [
$this->username, $this->username,
$this->module['id'], $this->module->getId(),
$dotclear = $this->getDotclearVersion(), $dotclear = $this->getDotclearVersion(),
$this->module['type'], $this->module->get('type'),
'', '', '', '',
], ],
$v $v
@ -169,8 +169,8 @@ class gitshields extends Action
private function getDotclearVersion(): string private function getDotclearVersion(): string
{ {
$version = null; $version = null;
if (!empty($this->module['requires']) && is_array($this->module['requires'])) { if (!empty($this->module->get('requires')) && is_array($this->module->get('requires'))) {
foreach ($this->module['requires'] as $req) { foreach ($this->module->get('requires') as $req) {
if (!is_array($req)) { if (!is_array($req)) {
$req = [$req]; $req = [$req];
} }
@ -180,8 +180,8 @@ class gitshields extends Action
break; break;
} }
} }
} elseif (!empty($this->module['dc_min'])) { } elseif (!empty($this->module->get('dc_min'))) {
$version = $this->module['dc_min']; $version = $this->module->get('dc_min');
} }
return $version ?: dcCore::app()->getVersion('core'); return $version ?: dcCore::app()->getVersion('core');

View File

@ -131,13 +131,13 @@ class licensefile extends Action
private function writeFullLicense(): ?bool private function writeFullLicense(): ?bool
{ {
try { try {
$full = file_get_contents(__DIR__ . '/licensefile/' . $this->getSetting('action_version') . '.full.txt'); $full = file_get_contents(__DIR__ . DIRECTORY_SEPARATOR . 'licensefile' . DIRECTORY_SEPARATOR . $this->getSetting('action_version') . '.full.txt');
if (empty($full)) { if (empty($full)) {
$this->setError(__('Failed to load license content')); $this->setError(__('Failed to load license content'));
return null; return null;
} }
files::putContent($this->module['root'] . '/LICENSE', str_replace("\r\n", "\n", $full)); files::putContent($this->module->get('root') . DIRECTORY_SEPARATOR . 'LICENSE', str_replace("\r\n", "\n", $full));
$this->setSuccess(__('Write new license file "LICENSE"')); $this->setSuccess(__('Write new license file "LICENSE"'));
} catch (Exception $e) { } catch (Exception $e) {
$this->setError(__('Failed to write new license file')); $this->setError(__('Failed to write new license file'));
@ -150,13 +150,13 @@ class licensefile extends Action
private function deleteFullLicense(bool $only_one = false): bool private function deleteFullLicense(bool $only_one = false): bool
{ {
foreach (self::fileExists($this->module['root']) as $file) { foreach (self::fileExists($this->module->get('root')) as $file) {
if ($only_one && $file != 'LICENSE') { if ($only_one && $file != 'LICENSE') {
continue; continue;
} }
if (!files::isDeletable($this->module['root'] . '/' . $file)) { if (!files::isDeletable($this->module->get('root') . DIRECTORY_SEPARATOR . $file)) {
$this->setWarning(sprintf(__('Old license file is not deletable (%s)'), $file)); $this->setWarning(sprintf(__('Old license file is not deletable (%s)'), $file));
} elseif (!@unlink($this->module['root'] . '/' . $file)) { } elseif (!@unlink($this->module->get('root') . DIRECTORY_SEPARATOR . $file)) {
$this->setError(sprintf(__('Failed to delete old license file (%s)'), $file)); $this->setError(sprintf(__('Failed to delete old license file (%s)'), $file));
} else { } else {
$this->setSuccess(sprintf(__('Delete old license file "%s"'), $file)); $this->setSuccess(sprintf(__('Delete old license file "%s"'), $file));

View File

@ -137,7 +137,7 @@ class phpcsfixer extends Action
'%sphp %s/phpcsfixer/libs/php-cs-fixer.phar fix %s --config=%s/phpcsfixer/phpcsfixer.rules.php --using-cache=no', '%sphp %s/phpcsfixer/libs/php-cs-fixer.phar fix %s --config=%s/phpcsfixer/phpcsfixer.rules.php --using-cache=no',
$this->phpexe_path, $this->phpexe_path,
__DIR__, __DIR__,
$this->module['sroot'], $this->module->get('root'),
__DIR__ __DIR__
); );

View File

@ -183,10 +183,10 @@ class phpheader extends Action
$this->bloc_wildcards, $this->bloc_wildcards,
[ [
date('Y'), date('Y'),
$this->module['id'], $this->module->getId(),
$this->module['name'], $this->module->get('name'),
$this->module['author'], $this->module->get('author'),
$this->module['type'], $this->module->get('type'),
dcCore::app()->auth->getInfo('user_cn'), dcCore::app()->auth->getInfo('user_cn'),
dcCore::app()->auth->getinfo('user_name'), dcCore::app()->auth->getinfo('user_name'),
dcCore::app()->auth->getInfo('user_email'), dcCore::app()->auth->getInfo('user_email'),

View File

@ -292,7 +292,7 @@ class phpstan extends Action
], ],
[ [
$this->run_level, $this->run_level,
(string) path::real($this->module['sroot'], false), (string) path::real($this->module->get('root'), false),
(string) path::real(DC_ROOT, false), (string) path::real(DC_ROOT, false),
(string) path::real(__DIR__ . '/phpstan', false), (string) path::real(__DIR__ . '/phpstan', false),
], ],

View File

@ -189,10 +189,10 @@ class zip extends Action
$file = str_replace( $file = str_replace(
self::$filename_wildcards, self::$filename_wildcards,
[ [
$this->module['type'], $this->module->get('type'),
$this->module['id'], $this->module->getId(),
$this->module['version'], $this->module->get('version'),
$this->module['author'], $this->module->get('author'),
time(), time(),
], ],
$file $file
@ -223,8 +223,8 @@ class zip extends Action
$zip->addExclusion($e); $zip->addExclusion($e);
} }
$zip->addDirectory( $zip->addDirectory(
path::real($this->module['root']), path::real($this->module->get('root')),
$this->module['id'], $this->module->getId(),
true true
); );
$zip->close(); $zip->close();