From 35ab8a704852fe41ad5a1fa8d7cfcd8596aefd75 Mon Sep 17 00:00:00 2001 From: Tuan Pham Ngoc Date: Thu, 16 Mar 2023 14:45:55 +0700 Subject: [PATCH 1/6] Convert mod_weblinks module to use services provider --- src/modules/mod_weblinks/mod_weblinks.php | 24 --------- src/modules/mod_weblinks/mod_weblinks.xml | 1 - .../mod_weblinks/services/provider.php | 41 +++++++++++++++ .../src/Dispatcher/Dispatcher.php | 50 +++++++++++++++++++ .../src/Helper/WeblinksHelper.php | 28 ++++++++--- 5 files changed, 113 insertions(+), 31 deletions(-) delete mode 100644 src/modules/mod_weblinks/mod_weblinks.php create mode 100644 src/modules/mod_weblinks/services/provider.php create mode 100644 src/modules/mod_weblinks/src/Dispatcher/Dispatcher.php diff --git a/src/modules/mod_weblinks/mod_weblinks.php b/src/modules/mod_weblinks/mod_weblinks.php deleted file mode 100644 index 3da3d4a..0000000 --- a/src/modules/mod_weblinks/mod_weblinks.php +++ /dev/null @@ -1,24 +0,0 @@ -get('moduleclass_sfx')); - -require ModuleHelper::getLayoutPath('mod_weblinks', $params->get('layout', 'default')); diff --git a/src/modules/mod_weblinks/mod_weblinks.xml b/src/modules/mod_weblinks/mod_weblinks.xml index e7fe725..9e5077b 100644 --- a/src/modules/mod_weblinks/mod_weblinks.xml +++ b/src/modules/mod_weblinks/mod_weblinks.xml @@ -12,7 +12,6 @@ Joomla\Module\Weblinks ##MODULE_FILES## - mod_weblinks.php ##LANGUAGE_FILES## diff --git a/src/modules/mod_weblinks/services/provider.php b/src/modules/mod_weblinks/services/provider.php new file mode 100644 index 0000000..75815ef --- /dev/null +++ b/src/modules/mod_weblinks/services/provider.php @@ -0,0 +1,41 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +defined('_JEXEC') or die; + +use Joomla\CMS\Extension\Service\Provider\HelperFactory; +use Joomla\CMS\Extension\Service\Provider\Module; +use Joomla\CMS\Extension\Service\Provider\ModuleDispatcherFactory; +use Joomla\DI\Container; +use Joomla\DI\ServiceProviderInterface; + +/** + * The weblinks module services provider. + * + * @since __DEPLOY_VERSION__ + */ +return new class () implements ServiceProviderInterface { + /** + * Registers the service provider with a DI container. + * + * @param Container $container The DI container. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function register(Container $container) + { + $container->registerServiceProvider(new ModuleDispatcherFactory('\\Joomla\\Module\\Weblinks')); + $container->registerServiceProvider(new HelperFactory('\\Joomla\\Module\\Weblinks\\Site\\Helper')); + + $container->registerServiceProvider(new Module()); + } +}; diff --git a/src/modules/mod_weblinks/src/Dispatcher/Dispatcher.php b/src/modules/mod_weblinks/src/Dispatcher/Dispatcher.php new file mode 100644 index 0000000..9df4bd2 --- /dev/null +++ b/src/modules/mod_weblinks/src/Dispatcher/Dispatcher.php @@ -0,0 +1,50 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace Joomla\Module\Weblinks\Site\Dispatcher; + +use Joomla\CMS\Dispatcher\AbstractModuleDispatcher; +use Joomla\CMS\Helper\HelperFactoryAwareInterface; +use Joomla\CMS\Helper\HelperFactoryAwareTrait; + +// phpcs:disable PSR1.Files.SideEffects +\defined('_JEXEC') or die; +// phpcs:enable PSR1.Files.SideEffects + +/** + * Dispatcher class for mod_weblinks + * + * @since __DEPLOY_VERSION__ + */ +class Dispatcher extends AbstractModuleDispatcher implements HelperFactoryAwareInterface +{ + use HelperFactoryAwareTrait; + + /** + * Returns the layout data. + * + * @return array + * + * @since __DEPLOY_VERSION__ + */ + protected function getLayoutData() + { + $data = parent::getLayoutData(); + + $data['list'] = $this->getHelperFactory()->getHelper('WeblinksHelper')->getWeblinks( + $data['params'], + $this->getApplication() + ); + + $data['moduleclass_sfx'] = htmlspecialchars($data['params']->get('moduleclass_sfx', '')); + + return $data; + } +} diff --git a/src/modules/mod_weblinks/src/Helper/WeblinksHelper.php b/src/modules/mod_weblinks/src/Helper/WeblinksHelper.php index 4c58f1a..c476ea9 100644 --- a/src/modules/mod_weblinks/src/Helper/WeblinksHelper.php +++ b/src/modules/mod_weblinks/src/Helper/WeblinksHelper.php @@ -29,14 +29,13 @@ class WeblinksHelper * @param Registry $params The module parameters * @param CMSApplicationInterface $app The application * - * @return mixed Null if no weblinks based on input parameters else an array containing all the weblinks. + * @return array Array containing all the weblinks. * - * @since 1.5 + * @since __DEPLOY_VERSION__ **/ - public static function getList($params, $app) + public function getWeblinks($params, $app) { // @var \Joomla\Component\Weblinks\Site\Model\CategoryModel $model - $model = $app->bootComponent('com_weblinks')->getMVCFactory() ->createModel('Category', 'Site', ['ignore_request' => true]); @@ -73,7 +72,7 @@ class WeblinksHelper $case_when1 .= $query->charLength('a.alias', '!=', '0'); $case_when1 .= ' THEN '; $a_id = $query->castAs('CHAR', 'a.id'); - $case_when1 .= $query->concatenate(array($a_id, 'a.alias'), ':'); + $case_when1 .= $query->concatenate([$a_id, 'a.alias'], ':'); $case_when1 .= ' ELSE '; $case_when1 .= $a_id . ' END as slug'; @@ -81,7 +80,7 @@ class WeblinksHelper $case_when2 .= $query->charLength('c.alias', '!=', '0'); $case_when2 .= ' THEN '; $c_id = $query->castAs('CHAR', 'c.id'); - $case_when2 .= $query->concatenate(array($c_id, 'c.alias'), ':'); + $case_when2 .= $query->concatenate([$c_id, 'c.alias'], ':'); $case_when2 .= ' ELSE '; $case_when2 .= $c_id . ' END as catslug'; @@ -120,4 +119,21 @@ class WeblinksHelper return []; } + + /** + * Retrieve list of weblinks + * + * @param Registry $params The module parameters + * @param CMSApplicationInterface $app The application + * + * @return mixed Null if no weblinks based on input parameters else an array containing all the weblinks. + * + * @since 1.5 + * + * @deprecated 5.0 Use the none static function getWeblinks + **/ + public static function getList($params, $app) + { + return (new self())->getWeblinks($params, $app); + } } From 811e133f0a213c409d2c2b3429f7bea302ce014c Mon Sep 17 00:00:00 2001 From: Tuan Pham Ngoc Date: Thu, 16 Mar 2023 14:50:35 +0700 Subject: [PATCH 2/6] Add module files manually --- src/modules/mod_weblinks/mod_weblinks.xml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/modules/mod_weblinks/mod_weblinks.xml b/src/modules/mod_weblinks/mod_weblinks.xml index 9e5077b..d78f2d2 100644 --- a/src/modules/mod_weblinks/mod_weblinks.xml +++ b/src/modules/mod_weblinks/mod_weblinks.xml @@ -11,7 +11,9 @@ MOD_WEBLINKS_XML_DESCRIPTION Joomla\Module\Weblinks - ##MODULE_FILES## + services + src + tmpl ##LANGUAGE_FILES## From a4f68feacb724a93c2a4c1788dec19ecae802aad Mon Sep 17 00:00:00 2001 From: Tuan Pham Ngoc Date: Thu, 16 Mar 2023 16:17:31 +0700 Subject: [PATCH 3/6] Convert Search - Weblinks plugin to service provider --- .../search/weblinks/services/provider.php | 50 ++++++++ .../Extension/Weblinks.php} | 107 ++++++++++-------- src/plugins/search/weblinks/weblinks.xml | 1 + 3 files changed, 108 insertions(+), 50 deletions(-) create mode 100644 src/plugins/search/weblinks/services/provider.php rename src/plugins/search/weblinks/{weblinks.php => src/Extension/Weblinks.php} (66%) diff --git a/src/plugins/search/weblinks/services/provider.php b/src/plugins/search/weblinks/services/provider.php new file mode 100644 index 0000000..47f24db --- /dev/null +++ b/src/plugins/search/weblinks/services/provider.php @@ -0,0 +1,50 @@ + + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +defined('_JEXEC') or die; + +use Joomla\CMS\Extension\PluginInterface; +use Joomla\CMS\Factory; +use Joomla\CMS\Plugin\PluginHelper; +use Joomla\Database\DatabaseInterface; +use Joomla\DI\Container; +use Joomla\DI\ServiceProviderInterface; +use Joomla\Event\DispatcherInterface; +use Joomla\Plugin\Search\Weblinks\Extension\Weblinks; + +return new class () implements ServiceProviderInterface { + /** + * Registers the service provider with a DI container. + * + * @param Container $container The DI container. + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function register(Container $container) + { + $container->set( + PluginInterface::class, + function (Container $container) { + $app = Factory::getApplication(); + $dispatcher = $container->get(DispatcherInterface::class); + $database = $container->get(DatabaseInterface::class); + + return new Weblinks( + $dispatcher, + (array) PluginHelper::getPlugin('finder', 'weblinks'), + $app, + $database + ); + } + ); + } +}; diff --git a/src/plugins/search/weblinks/weblinks.php b/src/plugins/search/weblinks/src/Extension/Weblinks.php similarity index 66% rename from src/plugins/search/weblinks/weblinks.php rename to src/plugins/search/weblinks/src/Extension/Weblinks.php index 9265146..5864c4d 100644 --- a/src/plugins/search/weblinks/weblinks.php +++ b/src/plugins/search/weblinks/src/Extension/Weblinks.php @@ -7,37 +7,28 @@ * @license GNU General Public License version 2 or later; see LICENSE.txt */ -defined('_JEXEC') or die; +namespace Joomla\Plugin\Search\Weblinks\Extension; +use Joomla\CMS\Application\CMSApplicationInterface; use Joomla\CMS\Language\Multilanguage; use Joomla\CMS\Language\Text; use Joomla\CMS\Plugin\CMSPlugin; -use Joomla\Component\Search\Administrator\Helper\SearchHelper; use Joomla\Component\Weblinks\Site\Helper\RouteHelper; +use Joomla\Database\DatabaseAwareTrait; +use Joomla\Database\DatabaseInterface; use Joomla\Database\ParameterType; +use Joomla\Event\DispatcherInterface; + +defined('_JEXEC') or die; /** * Weblinks search plugin. * * @since 1.6 */ -class PlgSearchWeblinks extends CMSPlugin +final class Weblinks extends CMSPlugin { - /** - * Application object - * - * @var \Joomla\CMS\Application\CMSApplicationInterface - * @since 4.0.0 - */ - protected $app; - - /** - * Database Driver Instance - * - * @var \Joomla\Database\DatabaseDriver - * @since 4.0.0 - */ - protected $db; + use DatabaseAwareTrait; /** * Load the language file on instantiation. @@ -47,6 +38,22 @@ class PlgSearchWeblinks extends CMSPlugin */ protected $autoloadLanguage = true; + /** + * Constructor + * + * @param DispatcherInterface $dispatcher + * @param array $config + * @param CMSApplicationInterface $application + * @param DatabaseInterface $database + */ + public function __construct(DispatcherInterface $dispatcher, array $config, CMSApplicationInterface $application, DatabaseInterface $database) + { + parent::__construct($dispatcher, $config); + + $this->setApplication($application); + $this->setDatabase($database); + } + /** * Determine areas searchable by this plugin. * @@ -56,9 +63,9 @@ class PlgSearchWeblinks extends CMSPlugin */ public function onContentSearchAreas() { - static $areas = array( - 'weblinks' => 'PLG_SEARCH_WEBLINKS_WEBLINKS' - ); + static $areas = [ + 'weblinks' => 'PLG_SEARCH_WEBLINKS_WEBLINKS', + ]; return $areas; } @@ -80,23 +87,22 @@ class PlgSearchWeblinks extends CMSPlugin */ public function onContentSearch($text, $phrase = '', $ordering = '', $areas = null) { - $db = $this->db; - $groups = $this->app->getIdentity()->getAuthorisedViewLevels(); + $app = $this->getApplication(); + $db = $this->getDatabase(); + $groups = $app->getIdentity()->getAuthorisedViewLevels(); $searchText = $text; - if (is_array($areas)) + if (is_array($areas) + && !array_intersect($areas, array_keys($this->onContentSearchAreas()))) { - if (!array_intersect($areas, array_keys($this->onContentSearchAreas()))) - { - return array(); - } + return []; } - $sContent = $this->params->get('search_content', 1); + $sContent = $this->params->get('search_content', 1); $sArchived = $this->params->get('search_archived', 1); - $limit = $this->params->def('search_limit', 50); - $state = array(); + $limit = $this->params->def('search_limit', 50); + $state = []; if ($sContent) { @@ -110,14 +116,14 @@ class PlgSearchWeblinks extends CMSPlugin if (empty($state)) { - return array(); + return []; } $text = trim($text); if ($text == '') { - return array(); + return []; } $searchWeblinks = Text::_('PLG_SEARCH_WEBLINKS'); @@ -125,28 +131,28 @@ class PlgSearchWeblinks extends CMSPlugin switch ($phrase) { case 'exact': - $text = $db->quote('%' . $db->escape($text, true) . '%', false); - $wheres2 = array(); + $text = $db->quote('%' . $db->escape($text, true) . '%', false); + $wheres2 = []; $wheres2[] = 'a.url LIKE ' . $text; $wheres2[] = 'a.description LIKE ' . $text; $wheres2[] = 'a.title LIKE ' . $text; - $where = '(' . implode(') OR (', $wheres2) . ')'; + $where = '(' . implode(') OR (', $wheres2) . ')'; break; case 'all': case 'any': default: - $words = explode(' ', $text); - $wheres = array(); + $words = explode(' ', $text); + $wheres = []; foreach ($words as $word) { - $word = $db->quote('%' . $db->escape($word, true) . '%', false); - $wheres2 = array(); + $word = $db->quote('%' . $db->escape($word, true) . '%', false); + $wheres2 = []; $wheres2[] = 'a.url LIKE ' . $word; $wheres2[] = 'a.description LIKE ' . $word; $wheres2[] = 'a.title LIKE ' . $word; - $wheres[] = implode(' OR ', $wheres2); + $wheres[] = implode(' OR ', $wheres2); } $where = '(' . implode(($phrase == 'all' ? ') AND (' : ') OR ('), $wheres) . ')'; @@ -182,21 +188,21 @@ class PlgSearchWeblinks extends CMSPlugin $caseWhen = ' CASE WHEN '; $caseWhen .= $query->charLength('a.alias', '!=', '0'); $caseWhen .= ' THEN '; - $a_id = $query->castAs('CHAR', 'a.id'); - $caseWhen .= $query->concatenate(array($a_id, 'a.alias'), ':'); + $a_id = $query->castAs('CHAR', 'a.id'); + $caseWhen .= $query->concatenate([$a_id, 'a.alias'], ':'); $caseWhen .= ' ELSE '; $caseWhen .= $a_id . ' END as slug'; $caseWhen1 = ' CASE WHEN '; $caseWhen1 .= $query->charLength('c.alias', '!=', '0'); $caseWhen1 .= ' THEN '; - $c_id = $query->castAs('CHAR', 'c.id'); - $caseWhen1 .= $query->concatenate(array($c_id, 'c.alias'), ':'); + $c_id = $query->castAs('CHAR', 'c.id'); + $caseWhen1 .= $query->concatenate([$c_id, 'c.alias'], ':'); $caseWhen1 .= ' ELSE '; $caseWhen1 .= $c_id . ' END as catslug'; $query->select('a.title AS title, a.created AS created, a.url, a.description AS text, ' . $caseWhen . "," . $caseWhen1) - ->select($query->concatenate(array($db->quote($searchWeblinks), 'c.title'), " / ") . ' AS section') + ->select($query->concatenate([$db->quote($searchWeblinks), 'c.title'], " / ") . ' AS section') ->select('\'1\' AS browsernav') ->from('#__weblinks AS a') ->join('INNER', '#__categories as c ON c.id = a.catid') @@ -207,9 +213,10 @@ class PlgSearchWeblinks extends CMSPlugin ->order($order); // Filter by language. - if ($this->app->isClient('site') && Multilanguage::isEnabled()) + + if ($app->isClient('site') && Multilanguage::isEnabled()) { - $languages = [$this->app->getLanguage()->getTag(), '*']; + $languages = [$app->getLanguage()->getTag(), '*']; $query->whereIn($db->quoteName('a.language'), $languages, ParameterType::STRING) ->whereIn($db->quoteName('c.language'), $languages, ParameterType::STRING); } @@ -217,7 +224,7 @@ class PlgSearchWeblinks extends CMSPlugin $db->setQuery($query, 0, $limit); $rows = $db->loadObjectList(); - $return = array(); + $return = []; if ($rows) { @@ -228,7 +235,7 @@ class PlgSearchWeblinks extends CMSPlugin foreach ($rows as $weblink) { - if (SearchHelper::checkNoHTML($weblink, $searchText, array('url', 'text', 'title'))) + if (\searchHelper::checkNoHTML($weblink, $searchText, ['url', 'text', 'title'])) { $return[] = $weblink; } diff --git a/src/plugins/search/weblinks/weblinks.xml b/src/plugins/search/weblinks/weblinks.xml index 4300bdf..b2d5fc8 100644 --- a/src/plugins/search/weblinks/weblinks.xml +++ b/src/plugins/search/weblinks/weblinks.xml @@ -9,6 +9,7 @@ www.joomla.org ##VERSION## PLG_SEARCH_WEBLINKS_XML_DESCRIPTION + Joomla\Plugin\Search\Weblinks ##FILES## From fa5c5223fb85401cca7f995cfdc3732e0334b6af Mon Sep 17 00:00:00 2001 From: Tuan Pham Ngoc Date: Thu, 16 Mar 2023 17:35:13 +0700 Subject: [PATCH 4/6] Revert "Add module files manually" This reverts commit 811e133f0a213c409d2c2b3429f7bea302ce014c. --- src/modules/mod_weblinks/mod_weblinks.xml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/modules/mod_weblinks/mod_weblinks.xml b/src/modules/mod_weblinks/mod_weblinks.xml index d78f2d2..9e5077b 100644 --- a/src/modules/mod_weblinks/mod_weblinks.xml +++ b/src/modules/mod_weblinks/mod_weblinks.xml @@ -11,9 +11,7 @@ MOD_WEBLINKS_XML_DESCRIPTION Joomla\Module\Weblinks - services - src - tmpl + ##MODULE_FILES## ##LANGUAGE_FILES## From 34db9cad3075ceffb4f0d5ed13e3d28b0e7616af Mon Sep 17 00:00:00 2001 From: Tuan Pham Ngoc Date: Thu, 16 Mar 2023 17:39:44 +0700 Subject: [PATCH 5/6] Add delete file --- src/administrator/components/com_weblinks/script.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/administrator/components/com_weblinks/script.php b/src/administrator/components/com_weblinks/script.php index f4a710b..aa255fb 100644 --- a/src/administrator/components/com_weblinks/script.php +++ b/src/administrator/components/com_weblinks/script.php @@ -48,6 +48,7 @@ class Com_WeblinksInstallerScript '/language/en-GB/en-GB.mod_weblinks.sys.ini', '/language/en-GB/en-GB.pkg_weblinks.sys.ini', '/modules/mod_weblinks/helper.php', + '/modules/mod_weblinks/mod_weblinks.php', ]; $folders = [ From cd658395da2c1a66f98f42276e24d1c7170c7028 Mon Sep 17 00:00:00 2001 From: Tuan Pham Ngoc Date: Thu, 16 Mar 2023 17:48:03 +0700 Subject: [PATCH 6/6] Delete old file --- src/administrator/components/com_weblinks/script.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/administrator/components/com_weblinks/script.php b/src/administrator/components/com_weblinks/script.php index aa255fb..8ea8fa8 100644 --- a/src/administrator/components/com_weblinks/script.php +++ b/src/administrator/components/com_weblinks/script.php @@ -49,6 +49,7 @@ class Com_WeblinksInstallerScript '/language/en-GB/en-GB.pkg_weblinks.sys.ini', '/modules/mod_weblinks/helper.php', '/modules/mod_weblinks/mod_weblinks.php', + '/plugins/search/weblinks/weblinks.php', ]; $folders = [