TYPO3  7.6
PlainDataResolver.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\DataHandling;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
28 {
32  protected $tableName;
33 
37  protected $liveIds;
38 
42  protected $sortingStatement;
43 
47  protected $workspaceId;
48 
52  protected $keepLiveIds = false;
53 
57  protected $keepDeletePlaceholder = false;
58 
62  protected $resolvedIds;
63 
69  public function __construct($tableName, array $liveIds, $sortingStatement = null)
70  {
71  $this->tableName = $tableName;
72  $this->liveIds = $this->reindex($liveIds);
73  $this->sortingStatement = $sortingStatement;
74  }
75 
81  public function setWorkspaceId($workspaceId)
82  {
83  $this->workspaceId = (int)$workspaceId;
84  }
85 
91  public function setKeepLiveIds($keepLiveIds)
92  {
93  $this->keepLiveIds = (bool)$keepLiveIds;
94  }
95 
102  {
103  $this->keepDeletePlaceholder = (bool)$keepDeletePlaceholder;
104  }
105 
109  public function get()
110  {
111  if (isset($this->resolvedIds)) {
112  return $this->resolvedIds;
113  }
114 
115  $ids = $this->processVersionOverlays($this->liveIds);
116  $ids = $this->processSorting($ids);
117  $ids = $this->applyLiveIds($ids);
118 
119  $this->resolvedIds = $ids;
120  return $this->resolvedIds;
121  }
122 
129  protected function processVersionOverlays(array $ids)
130  {
131  if (empty($this->workspaceId) || !$this->isWorkspaceEnabled() || empty($ids)) {
132  return $ids;
133  }
134 
135  $ids = $this->processVersionMovePlaceholders($ids);
136  $versions = $this->getDatabaseConnection()->exec_SELECTgetRows(
137  'uid,t3ver_oid,t3ver_state',
138  $this->tableName,
139  'pid=-1 AND t3ver_oid IN (' . $this->intImplode(',', $ids) . ')'
140  . ' AND t3ver_wsid=' . $this->workspaceId
141  );
142 
143  if (!empty($versions)) {
144  foreach ($versions as $version) {
145  $liveReferenceId = $version['t3ver_oid'];
146  $versionId = $version['uid'];
147  if (isset($ids[$liveReferenceId])) {
148  if (!$this->keepDeletePlaceholder && VersionState::cast($version['t3ver_state'])->equals(VersionState::DELETE_PLACEHOLDER)) {
149  unset($ids[$liveReferenceId]);
150  } else {
151  $ids[$liveReferenceId] = $versionId;
152  }
153  }
154  }
155  $ids = $this->reindex($ids);
156  }
157 
158  return $ids;
159  }
160 
167  protected function processVersionMovePlaceholders(array $ids)
168  {
169  // Early return on insufficient data-set
170  if (empty($this->workspaceId) || !$this->isWorkspaceEnabled() || empty($ids)) {
171  return $ids;
172  }
173 
174  $movePlaceholders = $this->getDatabaseConnection()->exec_SELECTgetRows(
175  'uid,t3ver_move_id',
176  $this->tableName,
177  'pid<>-1 AND t3ver_state=' . VersionState::MOVE_PLACEHOLDER
178  . ' AND t3ver_wsid=' . $this->workspaceId
179  . ' AND t3ver_move_id IN (' . $this->intImplode(',', $ids) . ')'
180  );
181 
182  if (!empty($movePlaceholders)) {
183  foreach ($movePlaceholders as $movePlaceholder) {
184  $liveReferenceId = $movePlaceholder['t3ver_move_id'];
185  $movePlaceholderId = $movePlaceholder['uid'];
186  // If both, MOVE_PLACEHOLDER and MOVE_POINTER are set
187  if (isset($ids[$liveReferenceId]) && isset($ids[$movePlaceholderId])) {
188  $ids[$movePlaceholderId] = $liveReferenceId;
189  unset($ids[$liveReferenceId]);
190  }
191  }
192  $ids = $this->reindex($ids);
193  }
194 
195  return $ids;
196  }
197 
205  protected function processSorting(array $ids)
206  {
207  // Early return on missing sorting statement or insufficient data-set
208  if (empty($this->sortingStatement) || count($ids) < 2) {
209  return $ids;
210  }
211 
212  $records = $this->getDatabaseConnection()->exec_SELECTgetRows(
213  'uid',
214  $this->tableName,
215  'uid IN (' . $this->intImplode(',', $ids) . ')',
216  '',
217  $this->sortingStatement,
218  '',
219  'uid'
220  );
221 
222  if (!is_array($records)) {
223  return array();
224  }
225 
226  $ids = $this->reindex(array_keys($records));
227  return $ids;
228  }
229 
238  protected function applyLiveIds(array $ids)
239  {
240  if (!$this->keepLiveIds || !$this->isWorkspaceEnabled() || empty($ids)) {
241  return $ids;
242  }
243 
244  $records = $this->getDatabaseConnection()->exec_SELECTgetRows(
245  'uid,t3ver_oid',
246  $this->tableName,
247  'uid IN (' . $this->intImplode(',', $ids) . ')',
248  '',
249  '',
250  '',
251  'uid'
252  );
253 
254  if (!is_array($records)) {
255  return array();
256  }
257 
258  foreach ($ids as $id) {
259  if (!empty($records[$id]['t3ver_oid'])) {
260  $ids[$id] = $records[$id]['t3ver_oid'];
261  }
262  }
263 
264  $ids = $this->reindex($ids);
265  return $ids;
266  }
267 
274  protected function reindex(array $ids)
275  {
276  if (empty($ids)) {
277  return $ids;
278  }
279  $ids = array_values($ids);
280  $ids = array_combine($ids, $ids);
281  return $ids;
282  }
283 
287  protected function isWorkspaceEnabled()
288  {
289  return BackendUtility::isTableWorkspaceEnabled($this->tableName);
290  }
291 
295  protected function isLocalizationEnabled()
296  {
297  return BackendUtility::isTableLocalizable($this->tableName);
298  }
299 
307  protected function intImplode($delimiter, array $values)
308  {
309  return implode($delimiter, $this->getDatabaseConnection()->cleanIntArray($values));
310  }
311 
315  protected function getDatabaseConnection()
316  {
317  return $GLOBALS['TYPO3_DB'];
318  }
319 }