Commit 9d797672 authored by Benni Mack's avatar Benni Mack Committed by Oliver Hader
Browse files

[TASK] Switch to json_encode/decode for state saving

Various places (Trees, Inline Data) can be optimized by
using a more "space-saving" and faster approach.

By using json_encode/decode instead of serializing the
data, the stored information in "uc" of Backend Users
will be vastly smaller when dealing with larger installations.

Resolves: #88680
Releases: master
Change-Id: I59e83c8c7615e9da63b04ebb4930c2b68b38ad7c
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/61220

Tested-by: default avatarTYPO3com <noreply@typo3.com>
Tested-by: Andreas Fernandez's avatarAndreas Fernandez <a.fernandez@scripting-base.de>
Tested-by: Oliver Hader's avatarOliver Hader <oliver.hader@typo3.org>
Reviewed-by: Oliver Klee's avatarOliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Andreas Fernandez's avatarAndreas Fernandez <a.fernandez@scripting-base.de>
Reviewed-by: Oliver Hader's avatarOliver Hader <oliver.hader@typo3.org>
parent 2604de30
......@@ -406,7 +406,7 @@ class FormInlineAjaxController extends AbstractFormEngineAjaxController
// Save states back to database
if (is_array($inlineView[$topTable][$topUid][$currentTable])) {
$inlineView[$topTable][$topUid][$currentTable] = array_unique($inlineView[$topTable][$topUid][$currentTable]);
$backendUser->uc['inlineView'] = serialize($inlineView);
$backendUser->uc['inlineView'] = json_encode($inlineView);
$backendUser->writeUC();
}
}
......@@ -614,7 +614,7 @@ class FormInlineAjaxController extends AbstractFormEngineAjaxController
return [];
}
$inlineView = unserialize($backendUser->uc['inlineView']);
$inlineView = json_decode($backendUser->uc['inlineView'], true);
if (!is_array($inlineView)) {
$inlineView = [];
}
......@@ -624,7 +624,7 @@ class FormInlineAjaxController extends AbstractFormEngineAjaxController
/**
* Method to check whether the backend user has the property inline view for the current IRRE item.
* In existing or old IRRE items the attribute may not exist, then the unserialize will fail.
* In existing or old IRRE items the attribute may not exist, then the json_decode will fail.
*
* @param BackendUserAuthentication $backendUser
* @return bool
......
......@@ -32,13 +32,13 @@ class TcaInlineExpandCollapseState implements FormDataProviderInterface
public function addData(array $result)
{
if (empty($result['inlineExpandCollapseStateArray'])) {
$fullInlineState = json_decode($this->getBackendUser()->uc['inlineView'], true);
if (!is_array($fullInlineState)) {
$fullInlineState = [];
}
$inlineStateForTable = [];
if (!empty($result['inlineTopMostParentUid']) && !empty($result['inlineTopMostParentTableName'])) {
// Happens in inline ajax context, top parent uid and top parent table are set
$fullInlineState = unserialize($this->getBackendUser()->uc['inlineView']);
if (!is_array($fullInlineState)) {
$fullInlineState = [];
}
$inlineStateForTable = [];
if ($result['command'] !== 'new') {
$table = $result['inlineTopMostParentTableName'];
$uid = $result['inlineTopMostParentUid'];
......@@ -46,14 +46,8 @@ class TcaInlineExpandCollapseState implements FormDataProviderInterface
$inlineStateForTable = $fullInlineState[$table][$uid];
}
}
$result['inlineExpandCollapseStateArray'] = $inlineStateForTable;
} else {
// Default case for a single record
$fullInlineState = unserialize($this->getBackendUser()->uc['inlineView']);
if (!is_array($fullInlineState)) {
$fullInlineState = [];
}
$inlineStateForTable = [];
if ($result['command'] !== 'new') {
$table = $result['tableName'];
$uid = $result['databaseRow']['uid'];
......@@ -61,8 +55,8 @@ class TcaInlineExpandCollapseState implements FormDataProviderInterface
$inlineStateForTable = $fullInlineState[$table][$uid];
}
}
$result['inlineExpandCollapseStateArray'] = $inlineStateForTable;
}
$result['inlineExpandCollapseStateArray'] = $inlineStateForTable;
}
if (!$result['isInlineChildExpanded']) {
......
......@@ -147,7 +147,7 @@ class FormEngineUtility
{
$backendUser = static::getBackendUserAuthentication();
if (isset($uc['inlineView']) && is_array($uc['inlineView'])) {
$inlineView = (array)unserialize($backendUser->uc['inlineView']);
$inlineView = (array)json_decode($backendUser->uc['inlineView'], true);
foreach ($uc['inlineView'] as $topTable => $topRecords) {
foreach ($topRecords as $topUid => $childElements) {
foreach ($childElements as $childTable => $childRecords) {
......@@ -173,7 +173,7 @@ class FormEngineUtility
}
}
}
$backendUser->uc['inlineView'] = serialize($inlineView);
$backendUser->uc['inlineView'] = json_encode($inlineView);
$backendUser->writeUC();
}
}
......
......@@ -589,7 +589,7 @@ abstract class AbstractTreeView
public function initializePositionSaving()
{
// Get stored tree structure:
$this->stored = unserialize($this->BE_USER->uc['browseTrees'][$this->treeName]);
$this->stored = json_decode($this->BE_USER->uc['browseTrees'][$this->treeName], true);
// PM action
// (If an plus/minus icon has been clicked, the PM GET var is sent and we
// must update the stored positions in the tree):
......@@ -617,7 +617,7 @@ abstract class AbstractTreeView
*/
public function savePosition()
{
$this->BE_USER->uc['browseTrees'][$this->treeName] = serialize($this->stored);
$this->BE_USER->uc['browseTrees'][$this->treeName] = json_encode($this->stored);
$this->BE_USER->writeUC();
}
......
......@@ -588,7 +588,7 @@ class FolderTreeView extends AbstractTreeView
public function initializePositionSaving()
{
// Get stored tree structure:
$this->stored = unserialize($this->BE_USER->uc['browseTrees'][$this->treeName]);
$this->stored = json_decode($this->BE_USER->uc['browseTrees'][$this->treeName], true);
$this->getShortHashNumberForStorage();
// PM action:
// (If an plus/minus icon has been clicked,
......
......@@ -480,7 +480,7 @@ class BackendUtility
if ($clearExpansion) {
$expandedPages = [];
} else {
$expandedPages = unserialize($beUser->uc['browseTrees']['browsePages']);
$expandedPages = json_decode($beUser->uc['browseTrees']['browsePages'], true);
}
// Get rootline:
$rL = self::BEgetRootLine($pid);
......@@ -498,7 +498,7 @@ class BackendUtility
$expandedPages[$mountIndex][$rLDat['uid']] = 1;
}
// Write back:
$beUser->uc['browseTrees']['browsePages'] = serialize($expandedPages);
$beUser->uc['browseTrees']['browsePages'] = json_encode($expandedPages);
$beUser->writeUC();
}
......
......@@ -242,7 +242,7 @@ class FormInlineAjaxControllerTest extends UnitTestCase
public function getInlineExpandCollapseStateArrayWillUnserializeUCInlineViewPropertyAsAnArrayWithData(): void
{
$backendUserProphecy = $this->prophesize(BackendUserAuthentication::class);
$backendUserProphecy->uc = ['inlineView' => serialize(['foo' => 'bar'])];
$backendUserProphecy->uc = ['inlineView' => json_encode(['foo' => 'bar'])];
$backendUser = $backendUserProphecy->reveal();
$mockObject = $this->getAccessibleMock(
......
......@@ -53,7 +53,7 @@ class TcaInlineExpandCollapseStateTest extends UnitTestCase
];
$GLOBALS['BE_USER'] = new \stdClass();
$GLOBALS['BE_USER']->uc = [
'inlineView' => serialize($inlineState),
'inlineView' => json_encode($inlineState),
];
$expected = $input;
$expected['inlineExpandCollapseStateArray'] = $inlineState['aParentTable'][5];
......@@ -97,7 +97,7 @@ class TcaInlineExpandCollapseStateTest extends UnitTestCase
];
$GLOBALS['BE_USER'] = new \stdClass();
$GLOBALS['BE_USER']->uc = [
'inlineView' => serialize($inlineState),
'inlineView' => json_encode($inlineState),
];
$expected = $input;
$expected['inlineExpandCollapseStateArray'] = $inlineState['aParentTable'][5];
......
......@@ -88,7 +88,7 @@ class ExportPageTreeView extends BrowseTreeView
// Initialize:
$this->init(' AND ' . $this->BE_USER->getPagePermsClause(Permission::PAGE_SHOW) . $clause);
// Get stored tree structure:
$this->stored = unserialize($this->BE_USER->uc['browseTrees']['browsePages'], ['allowed_classes' => false]);
$this->stored = json_decode($this->BE_USER->uc['browseTrees']['browsePages'], true);
$treeArr = [];
$idx = 0;
// Set first:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment