Skip to content

Commit f2b1243

Browse files
joomdonationrichard67sandewt
authored
[5.4] Refactor CMS Table classes (#45243)
* Refactor CMS Table classes --------- Co-authored-by: Richard Fath <[email protected]> Co-authored-by: jsanders <[email protected]>
1 parent d0e1922 commit f2b1243

18 files changed

+373
-333
lines changed

libraries/src/Table/Asset.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace Joomla\CMS\Table;
1111

1212
use Joomla\CMS\Language\Text;
13-
use Joomla\Database\DatabaseDriver;
13+
use Joomla\Database\DatabaseInterface;
1414
use Joomla\Event\DispatcherInterface;
1515

1616
// phpcs:disable PSR1.Files.SideEffects
@@ -59,12 +59,12 @@ class Asset extends Nested
5959
/**
6060
* Constructor
6161
*
62-
* @param DatabaseDriver $db Database connector object
62+
* @param DatabaseInterface $db Database connector object
6363
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
6464
*
6565
* @since 1.7.0
6666
*/
67-
public function __construct(DatabaseDriver $db, ?DispatcherInterface $dispatcher = null)
67+
public function __construct(DatabaseInterface $db, ?DispatcherInterface $dispatcher = null)
6868
{
6969
parent::__construct('#__assets', 'id', $db, $dispatcher);
7070
}
@@ -109,14 +109,15 @@ public function check()
109109
// Nested does not allow parent_id = 0, override this.
110110
if ($this->parent_id > 0) {
111111
// Get the DatabaseQuery object
112-
$query = $this->_db->getQuery(true)
112+
$db = $this->getDatabase();
113+
$query = $db->getQuery(true)
113114
->select('1')
114-
->from($this->_db->quoteName($this->_tbl))
115-
->where($this->_db->quoteName('id') . ' = ' . $this->parent_id);
115+
->from($db->quoteName($this->_tbl))
116+
->where($db->quoteName('id') . ' = ' . $this->parent_id);
116117

117118
$query->setLimit(1);
118119

119-
if ($this->_db->setQuery($query)->loadResult()) {
120+
if ($db->setQuery($query)->loadResult()) {
120121
return true;
121122
}
122123

@@ -153,7 +154,8 @@ public function rebuild($parentId = null, $leftId = 0, $level = 0, $path = null)
153154
}
154155
}
155156

156-
$query = $this->_db->getQuery(true);
157+
$db = $this->getDatabase();
158+
$query = $db->getQuery(true);
157159

158160
// Build the structure of the recursive query.
159161
if (!isset($this->_cache['rebuild.sql'])) {
@@ -175,9 +177,9 @@ public function rebuild($parentId = null, $leftId = 0, $level = 0, $path = null)
175177
// Make a shortcut to database object.
176178

177179
// Assemble the query to find all children of this node.
178-
$this->_db->setQuery(\sprintf($this->_cache['rebuild.sql'], (int) $parentId));
180+
$db->setQuery(\sprintf($this->_cache['rebuild.sql'], (int) $parentId));
179181

180-
$children = $this->_db->loadObjectList();
182+
$children = $db->loadObjectList();
181183

182184
// The right value of this node is the left value + 1
183185
$rightId = $leftId + 1;
@@ -205,7 +207,7 @@ public function rebuild($parentId = null, $leftId = 0, $level = 0, $path = null)
205207
->set('rgt = ' . (int) $rightId)
206208
->set('level = ' . (int) $level)
207209
->where($this->_tbl_key . ' = ' . (int) $parentId);
208-
$this->_db->setQuery($query)->execute();
210+
$db->setQuery($query)->execute();
209211

210212
// Return the right value of this node + 1.
211213
return $rightId + 1;

libraries/src/Table/Category.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Joomla\CMS\User\CurrentUserInterface;
1919
use Joomla\CMS\User\CurrentUserTrait;
2020
use Joomla\CMS\Versioning\VersionableTableInterface;
21-
use Joomla\Database\DatabaseDriver;
21+
use Joomla\Database\DatabaseInterface;
2222
use Joomla\Database\ParameterType;
2323
use Joomla\Event\DispatcherInterface;
2424
use Joomla\Registry\Registry;
@@ -48,12 +48,12 @@ class Category extends Nested implements VersionableTableInterface, TaggableTabl
4848
/**
4949
* Constructor
5050
*
51-
* @param DatabaseDriver $db Database connector object
51+
* @param DatabaseInterface $db Database connector object
5252
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
5353
*
5454
* @since 1.5
5555
*/
56-
public function __construct(DatabaseDriver $db, ?DispatcherInterface $dispatcher = null)
56+
public function __construct(DatabaseInterface $db, ?DispatcherInterface $dispatcher = null)
5757
{
5858
/**
5959
* @deprecated 4.0 will be removed in 6.0
@@ -108,33 +108,35 @@ protected function _getAssetParentId(?Table $table = null, $id = null)
108108
$assetId = null;
109109

110110
// This is a category under a category.
111+
$db = $this->getDatabase();
112+
111113
if ($this->parent_id > 1) {
112114
// Build the query to get the asset id for the parent category.
113-
$query = $this->_db->getQuery(true)
114-
->select($this->_db->quoteName('asset_id'))
115-
->from($this->_db->quoteName('#__categories'))
116-
->where($this->_db->quoteName('id') . ' = :parentId')
115+
$query = $db->getQuery(true)
116+
->select($db->quoteName('asset_id'))
117+
->from($db->quoteName('#__categories'))
118+
->where($db->quoteName('id') . ' = :parentId')
117119
->bind(':parentId', $this->parent_id, ParameterType::INTEGER);
118120

119121
// Get the asset id from the database.
120-
$this->_db->setQuery($query);
122+
$db->setQuery($query);
121123

122-
if ($result = $this->_db->loadResult()) {
124+
if ($result = $db->loadResult()) {
123125
$assetId = (int) $result;
124126
}
125127
} elseif ($assetId === null) {
126128
// This is a category that needs to parent with the extension.
127129
// Build the query to get the asset id for the parent category.
128-
$query = $this->_db->getQuery(true)
129-
->select($this->_db->quoteName('id'))
130-
->from($this->_db->quoteName('#__assets'))
131-
->where($this->_db->quoteName('name') . ' = :extension')
130+
$query = $db->getQuery(true)
131+
->select($db->quoteName('id'))
132+
->from($db->quoteName('#__assets'))
133+
->where($db->quoteName('name') . ' = :extension')
132134
->bind(':extension', $this->extension);
133135

134136
// Get the asset id from the database.
135-
$this->_db->setQuery($query);
137+
$db->setQuery($query);
136138

137-
if ($result = $this->_db->loadResult()) {
139+
if ($result = $db->loadResult()) {
138140
$assetId = (int) $result;
139141
}
140142
}
@@ -259,7 +261,7 @@ public function store($updateNulls = true)
259261
}
260262

261263
// Verify that the alias is unique
262-
$table = new Category($this->getDbo(), $this->getDispatcher());
264+
$table = new Category($this->getDatabase(), $this->getDispatcher());
263265

264266
if (
265267
$table->load(['alias' => $this->alias, 'parent_id' => (int) $this->parent_id, 'extension' => $this->extension])

libraries/src/Table/Content.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Joomla\CMS\User\CurrentUserInterface;
1919
use Joomla\CMS\User\CurrentUserTrait;
2020
use Joomla\CMS\Versioning\VersionableTableInterface;
21-
use Joomla\Database\DatabaseDriver;
21+
use Joomla\Database\DatabaseInterface;
2222
use Joomla\Database\ParameterType;
2323
use Joomla\Event\DispatcherInterface;
2424
use Joomla\Registry\Registry;
@@ -49,12 +49,12 @@ class Content extends Table implements VersionableTableInterface, TaggableTableI
4949
/**
5050
* Constructor
5151
*
52-
* @param DatabaseDriver $db Database connector object
52+
* @param DatabaseInterface $db Database connector object
5353
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
5454
*
5555
* @since 1.5
5656
*/
57-
public function __construct(DatabaseDriver $db, ?DispatcherInterface $dispatcher = null)
57+
public function __construct(DatabaseInterface $db, ?DispatcherInterface $dispatcher = null)
5858
{
5959
$this->typeAlias = 'com_content.article';
6060

@@ -111,16 +111,17 @@ protected function _getAssetParentId(?Table $table = null, $id = null)
111111
$catId = (int) $this->catid;
112112

113113
// Build the query to get the asset id for the parent category.
114-
$query = $this->_db->getQuery(true)
115-
->select($this->_db->quoteName('asset_id'))
116-
->from($this->_db->quoteName('#__categories'))
117-
->where($this->_db->quoteName('id') . ' = :catid')
114+
$db = $this->getDatabase();
115+
$query = $db->getQuery(true)
116+
->select($db->quoteName('asset_id'))
117+
->from($db->quoteName('#__categories'))
118+
->where($db->quoteName('id') . ' = :catid')
118119
->bind(':catid', $catId, ParameterType::INTEGER);
119120

120121
// Get the asset id from the database.
121-
$this->_db->setQuery($query);
122+
$db->setQuery($query);
122123

123-
if ($result = $this->_db->loadResult()) {
124+
if ($result = $db->loadResult()) {
124125
$assetId = (int) $result;
125126
}
126127
}
@@ -351,7 +352,7 @@ public function store($updateNulls = true)
351352
}
352353

353354
// Verify that the alias is unique
354-
$table = new self($this->getDbo(), $this->getDispatcher());
355+
$table = new self($this->getDatabase(), $this->getDispatcher());
355356

356357
if ($table->load(['alias' => $this->alias, 'catid' => $this->catid]) && ($table->id != $this->id || $this->id == 0)) {
357358
// Is the existing article trashed?

libraries/src/Table/ContentHistory.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Joomla\CMS\Factory;
1313
use Joomla\CMS\User\CurrentUserInterface;
1414
use Joomla\CMS\User\CurrentUserTrait;
15-
use Joomla\Database\DatabaseDriver;
15+
use Joomla\Database\DatabaseInterface;
1616
use Joomla\Database\ParameterType;
1717
use Joomla\Event\DispatcherInterface;
1818

@@ -51,12 +51,12 @@ class ContentHistory extends Table implements CurrentUserInterface
5151
/**
5252
* Constructor
5353
*
54-
* @param DatabaseDriver $db Database connector object
54+
* @param DatabaseInterface $db Database connector object
5555
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
5656
*
5757
* @since 3.1
5858
*/
59-
public function __construct(DatabaseDriver $db, ?DispatcherInterface $dispatcher = null)
59+
public function __construct(DatabaseInterface $db, ?DispatcherInterface $dispatcher = null)
6060
{
6161
parent::__construct('#__history', 'version_id', $db, $dispatcher);
6262
$this->ignoreChanges = [
@@ -85,7 +85,7 @@ public function __construct(DatabaseDriver $db, ?DispatcherInterface $dispatcher
8585
public function store($updateNulls = false)
8686
{
8787
$this->character_count = \strlen($this->version_data);
88-
$typeTable = new ContentType($this->getDbo(), $this->getDispatcher());
88+
$typeTable = new ContentType($this->getDatabase(), $this->getDispatcher());
8989
$typeAlias = explode('.', $this->item_id);
9090
array_pop($typeAlias);
9191
$typeTable->load(['type_alias' => implode('.', $typeAlias)]);
@@ -166,7 +166,7 @@ public function getSha1($jsonData, ContentType $typeTable)
166166
*/
167167
public function getHashMatch()
168168
{
169-
$db = $this->_db;
169+
$db = $this->getDatabase();
170170
$itemId = $this->item_id;
171171
$sha1Hash = $this->sha1_hash;
172172
$query = $db->getQuery(true);
@@ -197,7 +197,7 @@ public function deleteOldVersions($maxVersions)
197197
$result = true;
198198

199199
// Get the list of version_id values we want to save
200-
$db = $this->_db;
200+
$db = $this->getDatabase();
201201
$itemId = $this->item_id;
202202
$query = $db->getQuery(true);
203203
$query->select($db->quoteName('version_id'))

libraries/src/Table/ContentType.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace Joomla\CMS\Table;
1111

1212
use Joomla\CMS\Language\Text;
13-
use Joomla\Database\DatabaseDriver;
13+
use Joomla\Database\DatabaseInterface;
1414
use Joomla\Event\DispatcherInterface;
1515

1616
// phpcs:disable PSR1.Files.SideEffects
@@ -27,12 +27,12 @@ class ContentType extends Table
2727
/**
2828
* Constructor
2929
*
30-
* @param DatabaseDriver $db Database connector object
30+
* @param DatabaseInterface $db Database connector object
3131
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
3232
*
3333
* @since 3.1
3434
*/
35-
public function __construct(DatabaseDriver $db, ?DispatcherInterface $dispatcher = null)
35+
public function __construct(DatabaseInterface $db, ?DispatcherInterface $dispatcher = null)
3636
{
3737
parent::__construct('#__content_types', 'type_id', $db, $dispatcher);
3838
}
@@ -81,7 +81,7 @@ public function check()
8181
public function store($updateNulls = false)
8282
{
8383
// Verify that the alias is unique
84-
$table = new self($this->getDbo(), $this->getDispatcher());
84+
$table = new self($this->getDatabase(), $this->getDispatcher());
8585

8686
if ($table->load(['type_alias' => $this->type_alias]) && ($table->type_id != $this->type_id || $this->type_id == 0)) {
8787
$this->setError(Text::_('COM_TAGS_ERROR_UNIQUE_ALIAS'));
@@ -117,7 +117,7 @@ public function fieldmapExpand($assoc = true)
117117
*/
118118
public function getTypeId($typeAlias)
119119
{
120-
$db = $this->getDbo();
120+
$db = $this->getDatabase();
121121
$query = $db->getQuery(true);
122122
$query->select($db->quoteName('type_id'))
123123
->from($db->quoteName($this->_tbl))

libraries/src/Table/CoreContent.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Joomla\CMS\Language\Text;
1616
use Joomla\CMS\User\CurrentUserInterface;
1717
use Joomla\CMS\User\CurrentUserTrait;
18-
use Joomla\Database\DatabaseDriver;
18+
use Joomla\Database\DatabaseInterface;
1919
use Joomla\Database\ParameterType;
2020
use Joomla\Event\DispatcherInterface;
2121
use Joomla\String\StringHelper;
@@ -52,12 +52,12 @@ class CoreContent extends Table implements CurrentUserInterface
5252
/**
5353
* Constructor
5454
*
55-
* @param DatabaseDriver $db Database connector object
55+
* @param DatabaseInterface $db Database connector object
5656
* @param ?DispatcherInterface $dispatcher Event dispatcher for this table
5757
*
5858
* @since 3.1
5959
*/
60-
public function __construct(DatabaseDriver $db, ?DispatcherInterface $dispatcher = null)
60+
public function __construct(DatabaseInterface $db, ?DispatcherInterface $dispatcher = null)
6161
{
6262
parent::__construct('#__ucm_content', 'core_content_id', $db, $dispatcher);
6363

@@ -116,7 +116,7 @@ public function check()
116116
$this->core_publish_up !== null
117117
&& $this->core_publish_down !== null
118118
&& $this->core_publish_down < $this->core_publish_up
119-
&& $this->core_publish_down > $this->_db->getNullDate()
119+
&& $this->core_publish_down > $this->getDatabase()->getNullDate()
120120
) {
121121
// Swap the dates.
122122
$temp = $this->core_publish_up;
@@ -166,7 +166,7 @@ public function check()
166166
*/
167167
public function delete($pk = null)
168168
{
169-
$baseTable = new Ucm($this->getDbo(), $this->getDispatcher());
169+
$baseTable = new Ucm($this->getDatabase(), $this->getDispatcher());
170170

171171
return parent::delete($pk) && $baseTable->delete($pk);
172172
}
@@ -194,7 +194,7 @@ public function deleteByContentId($contentItemId = null, $typeAlias = null)
194194
throw new \UnexpectedValueException('Null type alias not allowed.');
195195
}
196196

197-
$db = $this->getDbo();
197+
$db = $this->getDatabase();
198198
$query = $db->getQuery(true);
199199
$query->select($db->quoteName('core_content_id'))
200200
->from($db->quoteName('#__ucm_content'))
@@ -281,7 +281,7 @@ public function store($updateNulls = true)
281281
protected function storeUcmBase($updateNulls = true, $isNew = false)
282282
{
283283
// Store the ucm_base row
284-
$db = $this->getDbo();
284+
$db = $this->getDatabase();
285285
$query = $db->getQuery(true);
286286
$languageId = ContentHelper::getLanguageId($this->core_language);
287287

0 commit comments

Comments
 (0)