From 5d92ae7bde561ca976c54d16cd2b5a028620bdc9 Mon Sep 17 00:00:00 2001 From: Jean-Christian Denis Date: Sat, 11 Mar 2023 23:11:29 +0100 Subject: [PATCH] code review for Dotclear 2.25 --- CHANGELOG.md | 4 + README.md | 2 +- _define.php | 10 +-- locales/fr/main.lang.php | 17 +++++ locales/fr/main.po | 13 +--- src/FilterDuplicate.php | 37 +++++---- src/Install.php | 80 +++++++++++--------- src/My.php | 39 ++++++++++ src/Prepend.php | 32 ++++++-- src/Uninstall.php | 159 ++++++++++++++++++++++----------------- 10 files changed, 252 insertions(+), 141 deletions(-) create mode 100644 locales/fr/main.lang.php create mode 100644 src/My.php diff --git a/CHANGELOG.md b/CHANGELOG.md index b3dab65..d03fef8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +1.0 - 2023.03.11 +- update to dotclear 2.25 +- use namespace + 0.9.1 - 2022.12.20 - fix permission - fix install diff --git a/README.md b/README.md index c60409e..7b1dcc5 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Filter as spam same (duplicate) comments from multiple blogs of a Dotclear's ins dcFilterDuplicate requires: * permissions to manage antispam - * Dotclear 2.24 + * Dotclear 2.25 ## USAGE diff --git a/_define.php b/_define.php index 53f5cae..e6088a5 100644 --- a/_define.php +++ b/_define.php @@ -24,10 +24,10 @@ $this->registerModule( 'permissions' => dcCore::app()->auth->makePermissions([ dcAuth::PERMISSION_ADMIN, ]), - 'priority' => 200, - 'type' => 'plugin', - 'support' => 'http://forum.dotclear.org/viewtopic.php?pid=332947#p332947', - 'details' => 'http://plugins.dotaddict.org/dc2/details/' . basename(__DIR__), - 'repository' => 'https://raw.githubusercontent.com/JcDenis/' . basename(__DIR__) . '/master/dcstore.xml', + 'priority' => 200, + 'type' => 'plugin', + 'support' => 'http://forum.dotclear.org/viewtopic.php?pid=332947#p332947', + 'details' => 'http://plugins.dotaddict.org/dc2/details/' . basename(__DIR__), + 'repository' => 'https://raw.githubusercontent.com/JcDenis/' . basename(__DIR__) . '/master/dcstore.xml', ] ); diff --git a/locales/fr/main.lang.php b/locales/fr/main.lang.php new file mode 100644 index 0000000..31d1a1a --- /dev/null +++ b/locales/fr/main.lang.php @@ -0,0 +1,17 @@ + 1);\n" -#: inc/class.filter.duplicate.php:30 msgid "Duplicate" msgstr "Doublons" -#: inc/class.filter.duplicate.php:31 msgid "Same comments on others blogs of a multiblog" msgstr "Commentaires identiques sur les autres blogs du multiblog" -#: inc/class.filter.duplicate.php:104 msgid "Minimum content length before check for duplicate:" msgstr "Longueur minimum du contenu pour faire une vérification :" -#: inc/class.filter.duplicate.php:117 msgid "Super administrator set the minimum length of comment content to %d chars." msgstr "Un super administrateur a régler la longueur minimum d'un commentaire à surveiller à %d caractères." diff --git a/src/FilterDuplicate.php b/src/FilterDuplicate.php index 1cfc789..56f3ea0 100644 --- a/src/FilterDuplicate.php +++ b/src/FilterDuplicate.php @@ -10,27 +10,36 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; -} +declare(strict_types=1); + +namespace Dotclear\Plugin\dcFilterDuplicate; + +use dcBlog; +use dcCore; +use dcPage; +use dcSpamFilter; +use Exception; +use form; +use html; +use http; /** * @ingroup DC_PLUGIN_DCFILTERDUPLICATE * @brief Filter duplicate comments on multiblogs. * @since 2.6 */ -class dcFilterDuplicate extends dcSpamFilter +class FilterDuplicate extends dcSpamFilter { public $name = 'Duplicate filter'; public $has_gui = true; - protected function setInfo() + protected function setInfo(): void { $this->name = __('Duplicate'); $this->description = __('Same comments on others blogs of a multiblog'); } - public function isSpam($type, $author, $email, $site, $ip, $content, $post_id, &$status) + public function isSpam($type, $author, $email, $site, $ip, $content, $post_id, &$status): ?bool { if ($type != 'comment') { return null; @@ -53,7 +62,7 @@ class dcFilterDuplicate extends dcSpamFilter } } - public function isDuplicate($content, $ip) + public function isDuplicate($content, $ip): bool { $rs = dcCore::app()->con->select( 'SELECT C.comment_id ' . @@ -67,7 +76,7 @@ class dcFilterDuplicate extends dcSpamFilter return !$rs->isEmpty(); } - public function markDuplicate($content, $ip) + public function markDuplicate($content, $ip): void { $cur = dcCore::app()->con->openCursor(dcCore::app()->prefix . dcBlog::COMMENT_TABLE_NAME); dcCore::app()->con->writeLock(dcCore::app()->prefix . dcBlog::COMMENT_TABLE_NAME); @@ -86,9 +95,9 @@ class dcFilterDuplicate extends dcSpamFilter public function gui(string $url): string { if (dcCore::app()->auth->isSuperAdmin()) { - dcCore::app()->blog->settings->dcFilterDuplicate->drop('dcfilterduplicate_minlen'); + dcCore::app()->blog->settings->get(My::id())->drop('dcfilterduplicate_minlen'); if (isset($_POST['dcfilterduplicate_minlen'])) { - dcCore::app()->blog->settings->dcFilterDuplicate->put( + dcCore::app()->blog->settings->get(My::id())->put( 'dcfilterduplicate_minlen', abs((int) $_POST['dcfilterduplicate_minlen']), 'integer', @@ -121,12 +130,12 @@ class dcFilterDuplicate extends dcSpamFilter ) . '

'; } - private function getMinLength() + private function getMinLength(): int { - return abs((int) dcCore::app()->blog->settings->dcFilterDuplicate->getGlobal('dcfilterduplicate_minlen')); + return abs((int) dcCore::app()->blog->settings->get('dcFilterDuplicate')->getGlobal('dcfilterduplicate_minlen')); } - public function triggerOtherBlogs($content, $ip) + public function triggerOtherBlogs($content, $ip): void { $rs = dcCore::app()->con->select( 'SELECT P.blog_id ' . @@ -137,7 +146,7 @@ class dcFilterDuplicate extends dcSpamFilter ); while ($rs->fetch()) { - $b = new dcBlog($rs->blog_id); + $b = new dcBlog($rs->f('blog_id')); $b->triggerBlog(); unset($b); } diff --git a/src/Install.php b/src/Install.php index 3b46e0a..2945e25 100644 --- a/src/Install.php +++ b/src/Install.php @@ -10,43 +10,55 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_ADMIN')) { - return null; -} +declare(strict_types=1); -# -- Module specs -- -$mod_conf = [[ - 'dcfilterduplicate_minlen', - 'Minimum lenght of comment to filter', - 30, - 'integer', -]]; +namespace Dotclear\Plugin\dcFilterDuplicate; -# -- Nothing to change below -- -try { - # Check module version - if (!dcCore::app()->newVersion( - basename(__DIR__), - dcCore::app()->plugins->moduleInfo(basename(__DIR__), 'version') - )) { - return null; - } - # Set module settings - dcCore::app()->blog->settings->addNamespace(basename(__DIR__)); - foreach ($mod_conf as $v) { - dcCore::app()->blog->settings->__get(basename(__DIR__))->put( - $v[0], - $v[2], - $v[3], - $v[1], - false, - true - ); +use dcCore; +use dcNsProcess; +use Exception; + +class Install extends dcNsProcess +{ + # -- Module specs -- + private static $mod_conf = [[ + 'dcfilterduplicate_minlen', + 'Minimum lenght of comment to filter', + 30, + 'integer', + ]]; + + public static function init(): bool + { + self::$init = defined('DC_CONTEXT_ADMIN') && dcCore::app()->newVersion(My::id(), dcCore::app()->plugins->moduleInfo(My::id(), 'version')); + + return self::$init; } - return true; -} catch (Exception $e) { - dcCore::app()->error->add($e->getMessage()); + public static function process(): bool + { + if (!self::$init) { + return false; + } - return false; + try { + # Set module settings + foreach (self::$mod_conf as $v) { + dcCore::app()->blog->settings->get(My::id())->put( + $v[0], + $v[2], + $v[3], + $v[1], + false, + true + ); + } + + return true; + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + + return false; + } + } } diff --git a/src/My.php b/src/My.php new file mode 100644 index 0000000..036cb05 --- /dev/null +++ b/src/My.php @@ -0,0 +1,39 @@ +plugins->moduleInfo(self::id(), 'name')); + } +} diff --git a/src/Prepend.php b/src/Prepend.php index 2917fc2..f0c227e 100644 --- a/src/Prepend.php +++ b/src/Prepend.php @@ -10,10 +10,30 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_RC_PATH')) { - return null; +declare(strict_types=1); + +namespace Dotclear\Plugin\dcFilterDuplicate; + +use dcCore; +use dcNsProcess; + +class Prepend extends dcNsProcess +{ + public static function init(): bool + { + self::$init = true; + + return self::$init; + } + + public static function process(): bool + { + if (!self::$init) { + return false; + } + + dcCore::app()->spamfilters[] = FilterDuplicate::class; + + return true; + } } - -Clearbricks::lib()->autoload(['dcFilterDuplicate' => __DIR__ . '/inc/class.filter.duplicate.php']); - -dcCore::app()->spamfilters[] = 'dcFilterDuplicate'; diff --git a/src/Uninstall.php b/src/Uninstall.php index 874a5b2..7896269 100644 --- a/src/Uninstall.php +++ b/src/Uninstall.php @@ -10,74 +10,93 @@ * @copyright Jean-Christian Denis * @copyright GPL-2.0 https://www.gnu.org/licenses/gpl-2.0.html */ -if (!defined('DC_CONTEXT_ADMIN')) { - return null; +declare(strict_types=1); + +namespace Dotclear\Plugin\dcFilterDuplicate; + +class Uninstall +{ + protected static $init = false; + + public static function init(): bool + { + self::$init = defined('DC_RC_PATH'); + + return self::$init; + } + + public static function process($uninstaller): ?bool + { + if (!self::$init) { + return false; + } + + $uninstaller->addUserAction( + /* type */ + 'settings', + /* action */ + 'delete_all', + /* ns */ + My::id(), + /* desc */ + __('delete all settings') + ); + + $uninstaller->addUserAction( + /* type */ + 'plugins', + /* action */ + 'delete', + /* ns */ + My::id(), + /* desc */ + __('delete plugin files') + ); + + $uninstaller->addUserAction( + /* type */ + 'versions', + /* action */ + 'delete', + /* ns */ + My::id(), + /* desc */ + __('delete the version number') + ); + + $uninstaller->addDirectAction( + /* type */ + 'settings', + /* action */ + 'delete_all', + /* ns */ + My::id(), + /* desc */ + sprintf(__('delete all %s settings'), My::id()) + ); + + $uninstaller->addDirectAction( + /* type */ + 'versions', + /* action */ + 'delete', + /* ns */ + My::id(), + /* desc */ + sprintf(__('delete %s version number'), My::id()) + ); + + $uninstaller->addDirectAction( + /* type */ + 'plugins', + /* action */ + 'delete', + /* ns */ + My::id(), + /* desc */ + sprintf(__('delete %s plugin files'), My::id()) + ); + + return true; + } } - -$mod_id = 'dcFilterDuplicate'; - -$this->addUserAction( - /* type */ - 'settings', - /* action */ - 'delete_all', - /* ns */ - $mod_id, - /* desc */ - __('delete all settings') -); - -$this->addUserAction( - /* type */ - 'plugins', - /* action */ - 'delete', - /* ns */ - $mod_id, - /* desc */ - __('delete plugin files') -); - -$this->addUserAction( - /* type */ - 'versions', - /* action */ - 'delete', - /* ns */ - $mod_id, - /* desc */ - __('delete the version number') -); - -$this->addDirectAction( - /* type */ - 'settings', - /* action */ - 'delete_all', - /* ns */ - $mod_id, - /* desc */ - sprintf(__('delete all %s settings'), $mod_id) -); - -$this->addDirectAction( - /* type */ - 'versions', - /* action */ - 'delete', - /* ns */ - $mod_id, - /* desc */ - sprintf(__('delete %s version number'), $mod_id) -); - -$this->addDirectAction( - /* type */ - 'plugins', - /* action */ - 'delete', - /* ns */ - $mod_id, - /* desc */ - sprintf(__('delete %s plugin files'), $mod_id) -);