From 3911c153ba163b49c8b9d4b0d01985b609ed16a9 Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Fri, 22 Apr 2022 16:27:02 +0200 Subject: [PATCH 1/2] Inject the database into the finder classes --- .../src/Extension/FinderComponent.php | 13 ++++++++++-- .../com_finder/src/Indexer/Adapter.php | 5 +---- .../com_finder/src/Indexer/Indexer.php | 15 ++++++++++---- .../com_finder/src/Indexer/Query.php | 20 +++++++++++++++---- .../com_finder/src/Service/HTML/Filter.php | 7 +++++-- .../com_finder/src/Service/HTML/Finder.php | 7 +++++-- .../com_finder/src/Model/SearchModel.php | 1 + .../mod_finder/src/Helper/FinderHelper.php | 6 +++++- 8 files changed, 55 insertions(+), 19 deletions(-) diff --git a/administrator/components/com_finder/src/Extension/FinderComponent.php b/administrator/components/com_finder/src/Extension/FinderComponent.php index 0f861b3a75f92..65944b880778a 100644 --- a/administrator/components/com_finder/src/Extension/FinderComponent.php +++ b/administrator/components/com_finder/src/Extension/FinderComponent.php @@ -19,6 +19,7 @@ use Joomla\Component\Finder\Administrator\Service\HTML\Filter; use Joomla\Component\Finder\Administrator\Service\HTML\Finder; use Joomla\Component\Finder\Administrator\Service\HTML\Query; +use Joomla\Database\DatabaseInterface; use Psr\Container\ContainerInterface; /** @@ -46,8 +47,16 @@ class FinderComponent extends MVCComponent implements BootableExtensionInterface */ public function boot(ContainerInterface $container) { - $this->getRegistry()->register('finder', new Finder); - $this->getRegistry()->register('filter', new Filter); + $finder = new Finder; + $finder->setDatabase($container->get(DatabaseInterface::class)); + + $this->getRegistry()->register('finder', $finder); + + $filter = new Filter; + $filter->setDatabase($container->get(DatabaseInterface::class)); + + $this->getRegistry()->register('filter', $filter); + $this->getRegistry()->register('query', new Query); } } diff --git a/administrator/components/com_finder/src/Indexer/Adapter.php b/administrator/components/com_finder/src/Indexer/Adapter.php index 6d7f7a108243c..8527cbc657ff0 100644 --- a/administrator/components/com_finder/src/Indexer/Adapter.php +++ b/administrator/components/com_finder/src/Indexer/Adapter.php @@ -135,9 +135,6 @@ abstract class Adapter extends CMSPlugin */ public function __construct(&$subject, $config) { - // Get the database object. - $this->db = Factory::getDbo(); - // Call the parent constructor. parent::__construct($subject, $config); @@ -157,7 +154,7 @@ public function __construct(&$subject, $config) } // Get the indexer object - $this->indexer = new Indexer; + $this->indexer = new Indexer($this->db); } /** diff --git a/administrator/components/com_finder/src/Indexer/Indexer.php b/administrator/components/com_finder/src/Indexer/Indexer.php index a6daa68dff41e..1810a9698eab6 100644 --- a/administrator/components/com_finder/src/Indexer/Indexer.php +++ b/administrator/components/com_finder/src/Indexer/Indexer.php @@ -18,6 +18,7 @@ use Joomla\CMS\Object\CMSObject; use Joomla\CMS\Plugin\PluginHelper; use Joomla\CMS\Profiler\Profiler; +use Joomla\Database\DatabaseInterface; use Joomla\Database\ParameterType; use Joomla\Database\QueryInterface; use Joomla\String\StringHelper; @@ -112,13 +113,19 @@ class Indexer /** * Indexer constructor. * + * @param DatabaseInterface $db The database + * * @since 3.8.0 */ - public function __construct() + public function __construct(DatabaseInterface $db = null) { - $this->db = Factory::getDbo(); + if ($db === null) + { + @trigger_error(sprintf('Database will be mandatory in 5.0.'), E_USER_DEPRECATED); + $db = Factory::getContainer()->get(DatabaseInterface::class); + } - $db = $this->db; + $this->db = $db; // Set up query template for addTokensToDb $this->addTokensToDbQueryTemplate = $db->getQuery(true)->insert($db->quoteName('#__finder_tokens')) @@ -992,7 +999,7 @@ protected function addTokensToDb($tokens, $context = '') */ protected function toggleTables($memory) { - if (strtolower(Factory::getDbo()->getServerType()) != 'mysql') + if (strtolower($this->db->getServerType()) != 'mysql') { return true; } diff --git a/administrator/components/com_finder/src/Indexer/Query.php b/administrator/components/com_finder/src/Indexer/Query.php index 637455efbde5f..6ec70e8d914b2 100644 --- a/administrator/components/com_finder/src/Indexer/Query.php +++ b/administrator/components/com_finder/src/Indexer/Query.php @@ -17,6 +17,8 @@ use Joomla\CMS\Language\Text; use Joomla\CMS\Uri\Uri; use Joomla\Component\Finder\Administrator\Helper\LanguageHelper; +use Joomla\Database\DatabaseAwareTrait; +use Joomla\Database\DatabaseInterface; use Joomla\Registry\Registry; use Joomla\String\StringHelper; use Joomla\Utilities\ArrayHelper; @@ -30,6 +32,8 @@ */ class Query { + use DatabaseAwareTrait; + /** * Flag to show whether the query can return results. * @@ -182,8 +186,16 @@ class Query * @since 2.5 * @throws Exception on database error. */ - public function __construct($options) + public function __construct($options, DatabaseInterface $db = null) { + if ($db === null) + { + @trigger_error(sprintf('Database will be mandatory in 5.0.'), E_USER_DEPRECATED); + $db = Factory::getContainer()->get(DatabaseInterface::class); + } + + $this->setDatabase($db); + // Get the input string. $this->input = $options['input'] ?? ''; @@ -504,7 +516,7 @@ public function getRequiredTermIds() protected function processStaticTaxonomy($filterId) { // Get the database object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); // Initialize user variables $groups = implode(',', Factory::getUser()->getAuthorisedViewLevels()); @@ -618,7 +630,7 @@ protected function processDynamicTaxonomy($filters) } // Get the database object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); @@ -1327,7 +1339,7 @@ protected function processString($input, $lang, $mode) protected function getTokenData($token) { // Get the database object. - $db = Factory::getDbo(); + $db = $this->getDatabase(); // Create a database query to build match the token. $query = $db->getQuery(true) diff --git a/administrator/components/com_finder/src/Service/HTML/Filter.php b/administrator/components/com_finder/src/Service/HTML/Filter.php index f9de3fcc9d089..f81a1610a0e96 100644 --- a/administrator/components/com_finder/src/Service/HTML/Filter.php +++ b/administrator/components/com_finder/src/Service/HTML/Filter.php @@ -18,6 +18,7 @@ use Joomla\CMS\Language\Text; use Joomla\Component\Finder\Administrator\Helper\LanguageHelper; use Joomla\Component\Finder\Administrator\Indexer\Query; +use Joomla\Database\DatabaseAwareTrait; use Joomla\Registry\Registry; /** @@ -27,6 +28,8 @@ */ class Filter { + use DatabaseAwareTrait; + /** * Method to generate filters using the slider widget and decorated * with the FinderFilter JavaScript behaviors. @@ -39,7 +42,7 @@ class Filter */ public function slider($options = array()) { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); $user = Factory::getUser(); $groups = implode(',', $user->getAuthorisedViewLevels()); @@ -239,7 +242,7 @@ public function select($idxQuery, $options) } else { - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); // Load the predefined filter if specified. diff --git a/administrator/components/com_finder/src/Service/HTML/Finder.php b/administrator/components/com_finder/src/Service/HTML/Finder.php index a0dc52d118bbd..0968df797eb34 100644 --- a/administrator/components/com_finder/src/Service/HTML/Finder.php +++ b/administrator/components/com_finder/src/Service/HTML/Finder.php @@ -15,6 +15,7 @@ use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; use Joomla\Component\Finder\Administrator\Helper\LanguageHelper; +use Joomla\Database\DatabaseAwareTrait; use Joomla\Utilities\ArrayHelper; /** @@ -24,6 +25,8 @@ */ class Finder { + use DatabaseAwareTrait; + /** * Creates a list of types to filter on. * @@ -34,7 +37,7 @@ class Finder public function typeslist() { // Load the finder types. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select('DISTINCT t.title AS text, t.id AS value') ->from($db->quoteName('#__finder_types') . ' AS t') @@ -75,7 +78,7 @@ public function typeslist() public function mapslist() { // Load the finder types. - $db = Factory::getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true) ->select($db->quoteName('title', 'text')) ->select($db->quoteName('id', 'value')) diff --git a/components/com_finder/src/Model/SearchModel.php b/components/com_finder/src/Model/SearchModel.php index 3092b2e51d8f9..03fe9fc5efc76 100644 --- a/components/com_finder/src/Model/SearchModel.php +++ b/components/com_finder/src/Model/SearchModel.php @@ -412,6 +412,7 @@ protected function populateState($ordering = null, $direction = null) // Load the query object. $this->searchquery = new Query($options); + $this->searchquery->setDatabase($this->getDatabase()); // Load the query token data. $this->excludedTerms = $this->searchquery->getExcludedTermIds(); diff --git a/modules/mod_finder/src/Helper/FinderHelper.php b/modules/mod_finder/src/Helper/FinderHelper.php index e89c7d5d9d358..3d79249b5701f 100644 --- a/modules/mod_finder/src/Helper/FinderHelper.php +++ b/modules/mod_finder/src/Helper/FinderHelper.php @@ -16,6 +16,7 @@ use Joomla\CMS\Router\Route; use Joomla\CMS\Uri\Uri; use Joomla\Component\Finder\Administrator\Indexer\Query; +use Joomla\Database\DatabaseInterface; use Joomla\Utilities\ArrayHelper; /** @@ -86,6 +87,9 @@ public static function getQuery($params) $options['filters'] = ArrayHelper::toInteger($options['filters']); // Instantiate a query object. - return new Query($options); + $query = new Query($options); + $query->setDatabase(Factory::getContainer()->get(DatabaseInterface::class)); + + return $query; } } From f7ddd75c6bc7d88fbc735fb0c08e4dbad83c06ec Mon Sep 17 00:00:00 2001 From: Allon Moritz Date: Sat, 23 Apr 2022 10:51:35 +0200 Subject: [PATCH 2/2] Pass db in to query constructor --- components/com_finder/src/Model/SearchModel.php | 5 ++--- modules/mod_finder/src/Helper/FinderHelper.php | 5 +---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/components/com_finder/src/Model/SearchModel.php b/components/com_finder/src/Model/SearchModel.php index 03fe9fc5efc76..9037c4c74c3d6 100644 --- a/components/com_finder/src/Model/SearchModel.php +++ b/components/com_finder/src/Model/SearchModel.php @@ -132,7 +132,7 @@ public function getQuery() protected function getListQuery() { // Create a new query object. - $db = $this->getDbo(); + $db = $this->getDatabase(); $query = $db->getQuery(true); // Select the required fields from the table. @@ -411,8 +411,7 @@ protected function populateState($ordering = null, $direction = null) $options['when2'] = $request->getString('w2', $params->get('w2', '')); // Load the query object. - $this->searchquery = new Query($options); - $this->searchquery->setDatabase($this->getDatabase()); + $this->searchquery = new Query($options, $this->getDatabase()); // Load the query token data. $this->excludedTerms = $this->searchquery->getExcludedTermIds(); diff --git a/modules/mod_finder/src/Helper/FinderHelper.php b/modules/mod_finder/src/Helper/FinderHelper.php index 3d79249b5701f..07590d3eccb3c 100644 --- a/modules/mod_finder/src/Helper/FinderHelper.php +++ b/modules/mod_finder/src/Helper/FinderHelper.php @@ -87,9 +87,6 @@ public static function getQuery($params) $options['filters'] = ArrayHelper::toInteger($options['filters']); // Instantiate a query object. - $query = new Query($options); - $query->setDatabase(Factory::getContainer()->get(DatabaseInterface::class)); - - return $query; + return new Query($options, Factory::getContainer()->get(DatabaseInterface::class)); } }