diff --git a/CHANGELOG.md b/CHANGELOG.md index 94fa334..7b1a119 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,16 @@ 0.2 -dev - [ ] add global config for file size limit -- [ ] add function to summarize by action what was done - [ ] add module to check depracated Dotclear function - [ ] add module to check deprecated PHP function - [ ] add module to check directory structure -- [ ] add DA badge to module _gitshields_ +- [ ] write documentation of php class + +0.1.2 +- add logs / report systeme +- add DA badge to module _gitshields_ +- change function args, less is better +- change interface, lighter names +- add function to summarize by action what was done 0.1.1 - fix php < 8.0 diff --git a/_define.php b/_define.php index c7d0e31..0cacea7 100644 --- a/_define.php +++ b/_define.php @@ -19,7 +19,7 @@ $this->registerModule( 'improve', 'Tiny tools to fix things for module devs', 'Jean-Christian Denis and contributors', - '0.1.1', + '0.1.2', [ 'requires' => [['core', '2.19']], 'permissions' => null, diff --git a/inc/class.improve.action.php b/inc/class.improve.action.php index 9edfa0c..b84e379 100644 --- a/inc/class.improve.action.php +++ b/inc/class.improve.action.php @@ -11,24 +11,33 @@ * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ + /** - * This is the absract action class. - * + * @brief Plugin improve action class + * * Action class must extends class ImproveAction. - * Call 'create' function with child class name through behavior, - * If your class signature is myClass extends ImproveAction, do - * $core->addBehavior('ImproveAddAction'), ['myClass', 'create']); + * If your class signature is myActionClass extends ImproveAction, + * do $core->addBehavior('ImproveAddAction'), ['myClass', 'create']); + * yoru action class is automatically created, * then function init() of your class wil be called. * One class must manage only one action. + * + * @package Plugin_improve + * @subpackage Action + * + * @copyright Jean-Christian Denis + * @copyright GPL-2.0-only */ abstract class ImproveAction { protected $core; - protected $type = ''; protected $module = []; + protected $path_full = ''; + protected $path_extension = ''; + protected $path_is_dir = null; - private static $notice = []; - private $preferences = []; + private $logs = ['success' => [], 'warning' => [], 'error' => []]; + private $settings = []; private $properties = [ 'id' => '', 'name' => '', @@ -38,14 +47,17 @@ abstract class ImproveAction 'types' => ['plugin'] ]; + /** + * ImproveAction constructor inits properpties and settings of a child class. + * + * @param string $core dcCore instance + */ final public function __construct(dcCore $core) { $this->core = $core; - self::$notice[get_called_class()] = ['error' => [], 'warning' => []]; - - $pref = @unserialize($core->blog->settings->improve->get('preferences_' . get_called_class())); - $this->preferences = is_array($pref) ? $pref : []; + $settings = @unserialize($core->blog->settings->improve->get('settings_' . get_called_class())); + $this->settings = is_array($settings) ? $settings : []; $this->init(); @@ -55,33 +67,64 @@ abstract class ImproveAction } } - final protected static function notice(string $message, bool $is_error = true) + /** + * Helper to create an instance of a ImproveAction child class. + * + * @param string $o ArrayObject of actions list + * @param string $core dcCore instance + */ + public static function create(arrayObject $o, dcCore $core) { - if (!empty($message)) { - self::$notice[get_called_class()][$is_error ? 'error' : 'warning'][] = $message; - } + $c = get_called_class(); + $o->append(new $c($core)); } - final public static function hasNotice(bool $error = true): bool - { - return !empty(self::$notice[get_called_class()][$error ? 'error' : 'warning']); - } - - final public static function getNotice(bool $error = true): array - { - return self::$notice[get_called_class()][$error ? 'error' : 'warning']; - } + /** + * Action initialisation function. + * + * It's called when an instance of ImproveAction child class is created. + * Usefull to setup action class. + * + * @return bool True if initialisation is ok. + */ + abstract protected function init(): bool; + /// @name Properties methods + //@{ + /** + * @see getProperty(); + */ final public function __get(string $property) { return $this->getProperty($property); } + /** + * Get a definition property of action class + * + * @return mixed A property of action definition. + */ final public function getProperty(string $property) { return $this->properties[$property] ?? null; } + /** + * Set a definition property of action class + * + * Property can be: + * - id : action id + * - name : action short name + * - desc : action short description, + * - priority : order of execution of this action + * - config : as configuration gui, false = none, true = internal, string = ext url + * - types : array of supported type of module, can : be plugins and/or themes + * + * @param mixed $property one or more definition + * @param dtring $value value for a single property + * + * @return mixed A property of action definition. + */ final protected function setProperties($property, $value = null): bool { $properties = is_array($property) ? $property : [$property => $value]; @@ -95,26 +138,51 @@ abstract class ImproveAction } return true; } + //@} - final protected function getPreference(string $preference) + /// @name Settings methods + //@{ + /** + * Get a settings of action class + * + * @param string $setting a settings id + * + * @return mixed A setting of action. + */ + final protected function getSetting(string $setting) { - return $this->preferences[$preference] ?? null; + return $this->settings[$setting] ?? null; } - final protected function setPreferences($preference, $value = null) + /** + * Set one or more setting of action class + * + * @param mixed $settings one or more settings + * @param string $value value for a single setting + * + * @return mixed A setting of action. + */ + final protected function setSettings($settings, $value = null) { - $preferences = is_array($preference) ? $preference : [$preference => $value]; - foreach($preferences as $k => $v) { - $this->preferences[$k] = $v; + $settings = is_array($settings) ? $settings : [$setting => $value]; + foreach($settings as $k => $v) { + $this->settings[$k] = $v; } return true; } + /** + * Redirection after settings update + * + * This save settings update before redirect. + * + * @param string $url redirect url after settings update + */ final protected function redirect(string $url) { $this->core->blog->settings->improve->put( - 'preferences_' . get_called_class(), - serialize($this->preferences), + 'settings_' . get_called_class(), + serialize($this->settings), 'string', null, true, @@ -125,51 +193,240 @@ abstract class ImproveAction http::redirect($url); } - abstract protected function init(): bool; - + /** + * Check if action class is well configured + * + * @return boolean True if class action is well configured + */ abstract public function isConfigured(): bool; - public static function create(arrayObject $o, dcCore $core) + /** + * Get configuraton gui + * + * If action class uses internal configuration, + * it must share here html form content of its settings. + * It must not use enclose bloc "form" nor button "save". + * This function is also called to redirect form + * after validation with $this->redirect($url); + * + * @param string $url post form redirect url + * + * @return mixed A setting of action. + */ + public function configure(string $url): ?string { - $c = get_called_class(); - $o->append(new $c($core)); + return null; + } + //@} + + /** + * Set in class var current module definitions. + * + * @see Improve::sanitizeModule() + * + * @param array $module Full array of module definitons + */ + final public function setModule(array $module) + { + $this->module = $module; } - public function configure(string $redirect_url): ?string + /** + * Set in class var current path definitons. + * + * @param string $path_full Full path + * @param string $path_extension Path extension (if it is a file) + * @param string $path_is_dir True if path is a directory + */ + final public function setPath(string $path_full, string $path_extension, bool $path_is_dir) + { + $this->path_full = $path_full; + $this->path_extension = $path_extension; + $this->path_is_dir = $path_is_dir; + } + + /// @name Fix methods + //@{ + /** + * Called when starting to fix module. + */ + public function openModule(): ?bool { return null; } - public function openModule(string $module_type, array $module_info): ?bool - { - $this->type = $module_type; - $this->module = $module_info; - - return null; - } - - public function openDirectory(string $path): ?bool + /** + * Called when open a directory to fix. + */ + public function openDirectory(): ?bool { return null; } - public function openFile(string $path, string $extension): ?bool + /** + * Called when open a file to fix. + */ + public function openFile(): ?bool { return null; } - public function readFile(string $path, string $extension, string &$content): ?bool + /** + * Called when read content of a file to fix. + * + * Content is shared from action to another. + * If an action erase content, fix is stopped. + * If you want to erase a content you must erase + * the file on action openDirectory. + * + * @param string $content File content + */ + public function readFile(string &$content): ?bool { return null; } - public function closeFile(string $path, string $extension): ?bool + /** + * Called when close a file to fix. + */ + public function closeFile(): ?bool { return null; } - public function closeModule(string $module_type, array $module_info): ?bool + /** + * Called when close a module to fix. + */ + public function closeModule(): ?bool { return null; } + //@} + + /// @name Logs methods + //@{ + /** + * Set an action log. + * + * Log must be use every time an action something happen. + * + * @param string $type type of message, can be error, warning, succes + * @param string $message message to log + * + * @return boolean True if message is logged. + */ + final public function setLog(string $type, string $message): bool + { + if (empty($this->path_full) || !array_key_exists($type, $this->logs)) { + return false; + } + $this->logs[$type][$this->path_full][] = $message; + return true; + } + + /** + * Check if action class has log of given type. + * + * @param string $type type of message, can be error, warning, succes + * + * @return boolean True if messages exist. + */ + final public function hasLog(string $type): bool + { + return array_key_exists($type, $this->logs) && !empty($this->logs[$type]); + } + + /** + * Get action logs. + * + * @param mixed $type type of message, can be error, warning, succes + * + * @return array Arry of given type of log or all if type is null + */ + final public function getLogs($type = null): array + { + if (null === $type) { + return $this->logs; + } + if (empty($this->path_full) + || !array_key_exists($type, $this->logs) + || !array_key_exists($this->path_full, $this->logs[$type]) + ) { + return []; + } + return $this->logs[$type][$this->path_full]; + } + + /** + * Set a log of type error. + */ + final public function setError(string $message) + { + $this->setLog('error', $message); + } + + /** + * Check logs of type error exists. + */ + final public function hasError(): bool + { + return !empty($this->getLogs('error')); + } + + /** + * Get logs of type error. + */ + final public function getErrors(): array + { + return $this->getLogs('error'); + } + + /** + * Set a log of type warning. + */ + final public function setWarning(string $message) + { + $this->setLog('warning', $message); + } + + /** + * Check logs of type error warnings. + */ + final public function hasWarning(): bool + { + return !empty($this->getLogs('warning')); + } + + /** + * Get logs of type warning. + */ + final public function getWarnings(): array + { + return $this->getLogs('warning'); + } + + /** + * Set a log of type success. + */ + final public function setSuccess(string $message) + { + $this->setLog('success', $message); + } + + /** + * Check logs of type error success. + */ + final public function hasSuccess(): bool + { + return !empty($this->getLogs('success')); + } + + /** + * Get logs of type success. + */ + final public function getSuccess(): array + { + return $this->getLogs('success'); + } + //@} } \ No newline at end of file diff --git a/inc/class.improve.php b/inc/class.improve.php index f6af4a1..76cb5f2 100644 --- a/inc/class.improve.php +++ b/inc/class.improve.php @@ -21,6 +21,8 @@ class Improve ]; private $core; private $actions = []; + private $logs = []; + private $has_log = ['success' => false, 'warning' => false, 'error' => false]; public function __construct(dcCore $core) { @@ -42,6 +44,72 @@ class Improve uasort($this->actions, [$this, 'sortModules']); } + public function getLogs(): array + { + return $this->logs; + } + + public function hasLog(string $type): bool + { + return array_key_exists($type, $this->has_log) && $this->has_log[$type]; + } + + public function writeLogs(): string + { + if (empty($this->logs)) { + return ''; + } + $cur = $this->core->con->openCursor($this->core->prefix . 'log'); + $cur->log_msg = serialize($this->logs); + $cur->log_table = 'improve'; + $id = $this->core->log->addLog($cur); + + return $id; + } + + public function readLogs(int $id ): array + { + $rs = $this->core->log->getLogs(['log_table' => 'improve', 'log_id' => $id, 'limit' => 1]); + if ($rs->isEmpty()) { + return []; + } + $this->core->log->delLogs($rs->log_id); + return unserialize($rs->log_msg); + } + + public function parselogs(int $id): array + { + $logs = $this->readLogs($id); + if (empty($logs)) { + return []; + } + $lines = []; + foreach($logs['improve'] as $path => $tools) { + $l_types = []; + foreach(['success', 'warning', 'error'] as $type) { + $l_tools = []; + foreach($tools as $tool) { + $l_msg = []; + if (!empty($logs[$tool][$type][$path])) { + foreach($logs[$tool][$type][$path] as $msg) { + $l_msg[] = $msg; + } + } + if (!empty($l_msg)) { + $l_tools[$tool] = $l_msg; + } + } + if (!empty($l_tools)) { + $l_types[$type] = $l_tools; + } + } + if (!empty($l_types)) { + $lines[$path] = $l_types; + } + } + return $lines; + } + public function module(string $id): ?ImproveAction { if (empty($id)) { @@ -58,9 +126,10 @@ class Improve return $this->actions[$id] ?? null; } - public function fix(string $type, string $id, array $module, array $actions): int + public function fixModule(string $type, string $id, array $properties, array $actions): float { - $module = ImproveDefinition::clean($id, $module); + $time_start = microtime(true); + $module = ImproveDefinition::clean($type, $id, $properties); $workers = []; foreach($actions as $action) { @@ -69,9 +138,13 @@ class Improve } } foreach($workers as $action) { - // action: - // open module - $action->openModule($type, $module); + // trace all path and action in logs + $this->logs['improve'][__('Begin')][] = $action->id; + // info: set current module + $action->setModule($module); + $action->setPath(__('Begin'), '', true); + // action: open module + $action->openModule(); } if (!isset($module['sroot']) || !$module['root_writable'] || !is_writable($module['sroot'])) { throw new Exception(__('Module path is not writable')); @@ -81,25 +154,28 @@ class Improve if (!file_exists($file[0])) { continue; } + foreach($workers as $action) { + // trace all path and action in logs + $this->logs['improve'][$file[0]][] = $action->id; + // info: set current path + $action->setPath($file[0], $file[1], $file[2]); + } if (!$file[2]) { foreach($workers as $action) { - // action: - // open a directory. full path - $action->openDirectory($file[0]); + // action: open a directory. full path + $action->openDirectory(); } } else { foreach($workers as $action) { - // action: - // before openning a file. full path, extension - $action->openFile($file[0], $file[1]); + // action: before openning a file. full path, extension + $action->openFile(); } if (in_array($file[1], self::$readfile_extensions)) { if (false !== ($content = file_get_contents($file[0]))) { $no_content = empty($content); foreach($workers as $action) { - // action: - // read a file content. full path, extension, content - $action->readFile($file[0], $file[1], $content); + // action: read a file content. full path, extension, content + $action->readFile($content); if (empty($content) && !$no_content) { throw new Exception(sprintf( __('File content has been removed: %s by %s'), $file[0], $action->name @@ -109,29 +185,31 @@ class Improve files::putContent($file[0], $content); } foreach($workers as $action) { - // action: - // after closing a file. full path, extension - $action->closeFile($file[0], $file[1]); + // action: after closing a file. full path, extension + $action->closeFile(); } } } } - // action: - // close module foreach($workers as $action) { - $action->closeModule($type, $module); + // trace all path and action in logs + $this->logs['improve'][__('End')][] = $action->id; + // info: set current module + $action->setPath(__('End'), '', true); + // action: close module + $action->closeModule(); } + // info: get acions reports foreach($workers as $action) { - if ($action->hasNotice()) { - dcPage::addErrorNotice($action->name . ' : ' . implode(', ', $action->getNotice())); + $this->logs[$action->id] = $action->getLogs(); + foreach($this->has_log as $type => $v) { + if ($action->hasLog($type)) { + $this->has_log[$type] = true; + } } } - foreach($workers as $action) { - if ($action->hasNotice(false)) { - dcPage::addWarningNotice($action->name . ' : ' . implode(', ', $action->getNotice(false))); - } - } - return count($tree); + + return substr(microtime(true) - $time_start, 0, 5); } private static function getModuleFiles(string $path, string $dir = '', array $res = []): array @@ -164,7 +242,7 @@ class Improve public function getURL(array $params = []): string { - return $this->core->adminurl->get('admin.plugin.improve', $params); + return $this->core->adminurl->get('admin.plugin.improve', $params, '&'); } public static function cleanExtensions($in): array @@ -197,11 +275,11 @@ class ImproveDefinition { private $properties = []; - public function __construct(string $id, array $properties = []) + public function __construct(string $type, string $id, array $properties = []) { $this->loadDefine($id, $properties['root']); - $this->properties = array_merge($this->properties, self::sanitizeModule($id, $properties)); + $this->properties = array_merge($this->properties, self::sanitizeModule($type, $id, $properties)); } public function get() @@ -209,9 +287,9 @@ class ImproveDefinition return $this->properties; } - public static function clean($id, $properties) + public static function clean(string $type, string $id, array $properties): array { - $p = new self($id, $properties); + $p = new self($type, $id, $properties); return $p->get(); } @@ -255,7 +333,7 @@ class ImproveDefinition } # adapt from lib.moduleslist.php - public static function sanitizeModule(string $id, array $properties): array + public static function sanitizeModule(string $type, string $id, array $properties): array { $label = empty($properties['label']) ? $id : $properties['label']; $name = __(empty($properties['name']) ? $label : $properties['name']); @@ -292,6 +370,7 @@ class ImproveDefinition [ 'id' => $id, 'sid' => self::sanitizeString($id), + 'type' => $type, 'label' => $label, 'name' => $name, 'oname' => $oname, diff --git a/inc/lib.improve.action.dcstore.php b/inc/lib.improve.action.dcstore.php index c797052..b4d76b5 100644 --- a/inc/lib.improve.action.dcstore.php +++ b/inc/lib.improve.action.dcstore.php @@ -17,7 +17,7 @@ class ImproveActionDcstore extends ImproveAction { $this->setProperties([ 'id' => 'dcstore', - 'name' => __('Fix dcstore.xml'), + 'name' => __('Store file'), 'desc' => __('Re-create dcstore.xml file according to _define.php variables'), 'priority' => 420, 'config' => true, @@ -29,13 +29,13 @@ class ImproveActionDcstore extends ImproveAction public function isConfigured(): bool { - return !empty($this->getPreference('pattern')); + return !empty($this->getSetting('pattern')); } public function configure($url): ?string { if (!empty($_POST['save']) && !empty($_POST['dcstore_pattern'])) { - $this->setPreferences('pattern', (string) $_POST['dcstore_pattern']); + $this->setSettings('pattern', (string) $_POST['dcstore_pattern']); $this->redirect($url); } @@ -43,7 +43,7 @@ class ImproveActionDcstore extends ImproveAction '
' . __('File will be overwritten if it exists') . '
' . '' . + form::field('dcstore_pattern', 160, 255, $this->getSetting('pattern')) . '' . '
' . '' . sprintf(__('You can use wildcards %s'), '%author%, %type%, %id%, %version%.') . @@ -54,20 +54,17 @@ class ImproveActionDcstore extends ImproveAction
'; } - public function openModule($module_type, $module_info): ?bool + public function openModule(): ?bool { - $this->type = $module_type; - $this->module = $module_info; - - $content = self::generateXML($module_info['id'], $module_info, $this->getPreference('pattern')); - if (self::hasNotice()) { - + $content = $this->generateXML(); + if ($this->hasError()) { return false; } try { - files::putContent($module_info['sroot'] . '/dcstore.xml', $content); + files::putContent($this->module['sroot'] . '/dcstore.xml', $content); + $this->setSuccess(__('Write dcstore.xml file.')); } catch(Exception $e) { - self::notice(__('Failed to write dcstore.xml file')); + $this->setError(__('Failed to write dcstore.xml file')); return false; } @@ -75,97 +72,93 @@ class ImproveActionDcstore extends ImproveAction return true; } - public static function generateXML($id, $module, $file_pattern) + public function generateXML() { - if (!is_array($module) || empty($module)) { - return false; - } - $xml = ['' . - form::field('username', 60, 100, $this->getPreference('username')) . ' + form::field('username', 60, 100, $this->getSetting('username')) . '
' . __('Used in your Github URL: http://github.com/username/module_id.') . '
' .
__('If you have badges not created by this tool in the README.md file you should remove them manually.') . '
' . __('If your plugin or theme is on Dotaddict, you can add a badge to link to its details in Dotaddict.') . '
'; } - public function openModule(string $module_type, array $module_info): ?bool + public function openModule(): ?bool { - $this->type = $module_type; - $this->module = $module_info; $this->replaceInfo(); return null; } - public function readFile($path, $extension, &$content): ?bool + public function readFile(&$content): ?bool { - if ($this->stop_scan || !preg_match('/(.*?)README\.md$/i', $path)) { + if ($this->stop_scan || !preg_match('/(.*?)README\.md$/i', $this->path_full)) { return null; } @@ -92,31 +90,38 @@ class ImproveActionGitshields extends ImproveAction private function replaceInfo() { - $username = $this->getPreference('username'); - $module = $this->module['id']; - $type = $this->module['type']; - $dotclear = $this->getDotclearVersion(); - $bloc = []; foreach($this->bloc_content as $k => $v) { - if ($k == 'dotaddict' && empty($this->getPreference('dotaddict'))) { + if ($k == 'dotaddict' && empty($this->getSetting('dotaddict'))) { continue; } $bloc[$k] = trim(str_replace( - ['%username%', '%module%', '%dotclear%', '%type%', "\r\n", "\n"], - [$username, $module, $dotclear, $type, '', ''], + [ + '%username%', + '%module%', + '%dotclear%', + '%type%', + "\r\n", "\n" + ], + [ + $this->getSetting('username'), + $this->module['id'], + $dotclear = $this->getDotclearVersion(), + $this->module['type'], + '', '' + ], $v )); } $this->bloc = $bloc; + $this->setSuccess(__('Prepare custom shield info')); } private function getDotclearVersion() { $version = null; - $module = $this->module; - if (!empty($module['requires']) && is_array($module['requires'])) { - foreach ($module['requires'] as $req) { + if (!empty($this->module['requires']) && is_array($this->module['requires'])) { + foreach ($this->module['requires'] as $req) { if (!is_array($req)) { $req = [$req]; } @@ -131,20 +136,31 @@ class ImproveActionGitshields extends ImproveAction private function writeShieldsBloc($content) { - return preg_replace( + $res = preg_replace( $this->bloc_pattern['target'], '$1' . "\n\n" . trim(implode("\n", $this->bloc)) . "\n\n", $content, - 1 + 1, + $count ); + if ($count) { + $this->setSuccess(__('Write new shield bloc')); + } + return $res; } private function deleteShieldsBloc($content) { - return preg_replace( + $res = preg_replace( $this->bloc_pattern['remove'], "\n\n", - $content + $content, + 1, + $count ); + if ($count) { + $this->setSuccess(__('Delete old shield bloc')); + } + return $res; } } \ No newline at end of file diff --git a/inc/lib.improve.action.licensefile.php b/inc/lib.improve.action.licensefile.php index cf1e14b..01272c8 100644 --- a/inc/lib.improve.action.licensefile.php +++ b/inc/lib.improve.action.licensefile.php @@ -26,7 +26,7 @@ class ImproveActionLicensefile extends ImproveAction { $this->setProperties([ 'id' => 'license', - 'name' => __('Fix license file'), + 'name' => __('License file'), 'desc' => __('Add or remove full license file to module root'), 'priority' => 330, 'config' => true, @@ -57,7 +57,7 @@ class ImproveActionLicensefile extends ImproveAction public function configure($url): ?string { if (!empty($_POST['save'])) { - $this->setPreferences([ + $this->setSettings([ 'action_version' => !empty($_POST['action_version']) ? $_POST['action_version'] : '', 'action_full' => !empty($_POST['action_full']) ? $_POST['action_full'] : '' ]); @@ -66,25 +66,22 @@ class ImproveActionLicensefile extends ImproveAction return '' . - form::combo('action_version', $this->action_version, $this->getPreference('action_version')) . ' + form::combo('action_version', $this->action_version, $this->getSetting('action_version')) . '
' . - form::combo('action_full', $this->action_full, $this->getPreference('action_full')) . + form::combo('action_full', $this->action_full, $this->getSetting('action_full')) . '
'; } - public function openModule(string $module_type, array $module_info): ?bool + public function openModule(): ?bool { - $this->type = $module_type; - $this->module = $module_info; - - if (in_array($this->getPreference('action_full'), ['remove', 'full','overwrite'])) { - $this->deleteFullLicense(($this->getPreference('action_full') == 'overwrite')); + if (in_array($this->getSetting('action_full'), ['remove', 'full','overwrite'])) { + $this->deleteFullLicense(($this->getSetting('action_full') == 'overwrite')); } - if (in_array($this->getPreference('action_full'), ['create', 'overwrite', 'full'])) { - if (empty($this->getPreference('action_version'))) { - self::notice(__('no full license type selected'), false); + if (in_array($this->getSetting('action_full'), ['create', 'overwrite', 'full'])) { + if (empty($this->getSetting('action_version'))) { + $this->setWarning(__('No full license type selected')); } else { $this->writeFullLicense(); } @@ -95,15 +92,16 @@ class ImproveActionLicensefile extends ImproveAction private function writeFullLicense() { try { - $full = file_get_contents(dirname(__FILE__) . '/license/' . $this->getPreference('action_version') . '.full.txt'); + $full = file_get_contents(dirname(__FILE__) . '/license/' . $this->getSetting('action_version') . '.full.txt'); if (empty($full)) { - self::notice(__('failed to load full license')); + $this->setError(__('Failed to load license content')); return null; } - files::putContent($this->module['root'] . '/LICENSE', str_replace("\r\n","\n",$full)); + files::putContent($this->module['root'] . '/LICENSE', str_replace("\r\n", "\n", $full)); + $this->setSuccess(__('Write new license file "LICENSE"')); } catch (Exception $e) { - self::notice(__('failed to write full license')); + $this->setError(__('Failed to write new license file')); return null; } @@ -113,14 +111,15 @@ class ImproveActionLicensefile extends ImproveAction private function deleteFullLicense($only_one = false) { foreach(self::fileExists($this->module['root']) as $file) { - if ($only_one && $file != 'license') { + if ($only_one && $file != 'LICENSE') { continue; } if (!files::isDeletable($this->module['root'] . '/' . $file)) { - self::notice(sprintf(__('full license is not deletable (%s)'), $file), false); - } - if (!@unlink($this->module['root'] . '/' . $file)) { - self::notice(sprintf(__('failed to delete full license (%s)'), $file), false); + $this->setWarning(sprintf(__('Old license file is not deletable (%s)'), $file)); + } elseif (!@unlink($this->module['root'] . '/' . $file)) { + $this->setError(sprintf(__('Failed to delete old license file (%s)'), $file)); + } else { + $this->setSuccess(sprintf(__('Delete old license file "%s"'), $file)); } } return true; diff --git a/inc/lib.improve.action.php b/inc/lib.improve.action.php index 85eec44..35f179c 100644 --- a/inc/lib.improve.action.php +++ b/inc/lib.improve.action.php @@ -17,7 +17,7 @@ class ImproveActionTab extends ImproveAction { $this->setProperties([ 'id' => 'tab', - 'name' => __('Fix tabulation'), + 'name' => __('Tabulations'), 'desc' => __('Replace tabulation by four space in php files'), 'priority' => 820, 'types' => ['plugin', 'theme'] @@ -26,12 +26,16 @@ class ImproveActionTab extends ImproveAction return true; } - public function readFile($path, $extension, &$content): ?bool + public function readFile(&$content): ?bool { - if (!in_array($extension, ['php', 'md'])) { + if (!in_array($this->path_extension, ['php', 'md'])) { return null; } - $content = preg_replace('/(\t)/', ' ', $content)."\n"; + $clean = preg_replace('/(\t)/', ' ', $content);// . "\n"; + if ($content != $clean) { + $this->setSuccess(__('Replace tabulation by spaces')); + $content = $clean; + } return true; } @@ -50,7 +54,7 @@ class ImproveActionNewline extends ImproveAction { $this->setProperties([ 'id' => 'newline', - 'name' => __('Fix newline'), + 'name' => __('Newlines'), 'desc' => __('Replace bad and repetitive and empty newline by single newline in files'), 'priority' => 840, 'config' => true, @@ -68,20 +72,20 @@ class ImproveActionNewline extends ImproveAction public function isConfigured(): bool { - return !empty($this->getPreference('extensions')); + return !empty($this->getSetting('extensions')); } public function configure($url): ?string { if (!empty($_POST['save']) && !empty($_POST['newline_extensions'])) { - $this->setPreferences( + $this->setSettings( 'extensions', Improve::cleanExtensions($_POST['newline_extensions']) ); $this->redirect($url); } - $ext = $this->getPreference('extensions'); + $ext = $this->getSetting('extensions'); if (!is_array($ext)) { $ext = []; } @@ -95,13 +99,13 @@ class ImproveActionNewline extends ImproveAction ''; } - public function readFile($path, $extension, &$content): ?bool + public function readFile(&$content): ?bool { - $ext = $this->getPreference('extensions'); - if (!is_array($ext) || !in_array($extension, $ext)) { + $ext = $this->getSetting('extensions'); + if (!is_array($ext) || !in_array($this->path_extension, $ext)) { return null; } - $content = preg_replace( + $clean = preg_replace( '/(\n\s+\n)/', "\n\n", preg_replace( @@ -111,7 +115,13 @@ class ImproveActionNewline extends ImproveAction ["\r\n", "\r"], "\n", $content - ))); + ) + ) + ); + if ($content != $clean) { + $this->setSuccess(__('Replace bad new lines')); + $content = $clean; + } return true; } @@ -125,7 +135,7 @@ class ImproveActionEndoffile extends ImproveAction { $this->setProperties([ 'id' => 'endoffile', - 'name' => __('Fix end of file'), + 'name' => __('End of files'), 'desc' => __('Remove php tag and empty lines from end of files'), 'priority' => 860, 'config' => true, @@ -143,29 +153,33 @@ class ImproveActionEndoffile extends ImproveAction public function configure($url): ?string { if (!empty($_POST['save'])) { - $this->setPreferences('psr2', !empty($_POST['endoffile_psr2'])); + $this->setSettings('psr2', !empty($_POST['endoffile_psr2'])); $this->redirect($url); } return '' . __('PSR2 must have a blank line, whereas PSR12 must not.') . '
'; } - public function readFile($path, $extension, &$content): ?bool + public function readFile(&$content): ?bool { - if (!in_array($extension, ['php', 'md'])) { + if (!in_array($this->path_extension, ['php', 'md'])) { return null; } - $content = preg_replace( + $clean = preg_replace( ['/(\s*)(\?>\s*)$/', '/\n+$/'], '', $content - ) . ($this->getPreference('psr2') ? "\n" : ''); + ) . ($this->getSetting('psr2') ? "\n" : ''); + if ($content != $clean) { + $this->setSuccess(__('Replace end of file')); + $content = $clean; + } return true; } diff --git a/inc/lib.improve.action.phpheader.php b/inc/lib.improve.action.phpheader.php index 3d71254..519b096 100644 --- a/inc/lib.improve.action.phpheader.php +++ b/inc/lib.improve.action.phpheader.php @@ -44,7 +44,7 @@ class ImproveActionPhpheader extends ImproveAction { $this->setProperties([ 'id' => 'phpheader', - 'name' => __('Fix PHP header'), + 'name' => __('PHP header'), 'desc' => __('Add or remove phpdoc header bloc from php file'), 'priority' => 340, 'config' => true, @@ -64,13 +64,13 @@ class ImproveActionPhpheader extends ImproveAction public function isConfigured(): bool { - return !empty($this->getPreference('bloc_action')) || !empty($this->getPreference('remove_old')); + return !empty($this->getSetting('bloc_action')) || !empty($this->getSetting('remove_old')); } public function configure($url): ?string { if (!empty($_POST['save'])) { - $this->setPreferences([ + $this->setSettings([ 'bloc_action' => !empty($_POST['bloc_action']) ? $_POST['bloc_action'] : '', 'bloc_content' => !empty($_POST['bloc_content']) ? $_POST['bloc_content'] : '', 'remove_old' => !empty($_POST['remove_old']), @@ -81,22 +81,22 @@ class ImproveActionPhpheader extends ImproveAction return '' . - form::combo('bloc_action', $this->action_bloc, $this->getPreference('bloc_action')) . ' + form::combo('bloc_action', $this->action_bloc, $this->getSetting('bloc_action')) . '
' . __('Bloc content:') . '
' . - form::textarea('bloc_content', 50, 10, html::escapeHTML($this->getPreference('bloc_content'))) . ' + form::textarea('bloc_content', 50, 10, html::escapeHTML($this->getSetting('bloc_content'))) . '
' . sprintf( __('You can use wildcards %s') , @@ -105,49 +105,51 @@ class ImproveActionPhpheader extends ImproveAction '
' . self::$exemple . '
' . sprintf(__('Preconization: %s'), $this->core->blog->public_path ? path::real($this->core->blog->public_path) : __("Blog's public directory") @@ -84,17 +84,17 @@ class ImproveActionZip extends ImproveAction
' . sprintf(__('Preconization: %s'), '%type%-%id%-%version%') . '
- -' . sprintf(__('Preconization: %s'), '%type%-%id%') . '
+ +' . sprintf(__('Preconization: %s'), '%type%-%id%-%version%') . '
+ @@ -103,32 +103,34 @@ class ImproveActionZip extends ImproveAction' . sprintf(__('Preconization: %s'), '*.zip,*.tar,*.tar.gz') . '
' .
sprintf(__('By default all these files are always removed from packages : %s'), implode(', ', self::$exclude)) . '
' . html::escapeHTML($action->desc) . '
- '; + + $logs = $lines = []; + + if (!empty($_REQUEST['upd'])) { + $logs = $improve->parseLogs($_REQUEST['upd']); + + if (!empty($logs)) { + echo '