Total Complexity | 74 |
Total Lines | 453 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like WorkspaceVersionRecordsCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WorkspaceVersionRecordsCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
36 | class WorkspaceVersionRecordsCommand extends Command |
||
37 | { |
||
38 | /** |
||
39 | * List of all workspaces |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $allWorkspaces = [0 => 'Live Workspace']; |
||
43 | |||
44 | /** |
||
45 | * @var ConnectionPool |
||
46 | */ |
||
47 | private $connectionPool; |
||
48 | |||
49 | public function __construct(ConnectionPool $connectionPool) |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Array with all records found when traversing the database |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $foundRecords = [ |
||
60 | // All versions of records found |
||
61 | // Subset of "all" which are offline versions (t3ver_oid > 0) [Informational] |
||
62 | 'all_versioned_records' => [], |
||
63 | // All records that has been published and can therefore be removed permanently |
||
64 | // Subset of "versions" that is a count of 1 or more (has been published) [Informational] |
||
65 | 'published_versions' => [], |
||
66 | // All versions that are offline versions in the Live workspace. You may wish to flush these if you only use |
||
67 | // workspaces for versioning since then you might find lots of versions piling up in the live workspace which |
||
68 | // have simply been disconnected from the workspace before they were published. |
||
69 | 'versions_in_live' => [], |
||
70 | // Versions that has lost their connection to a workspace in TYPO3. |
||
71 | // Subset of "versions" that doesn't belong to an existing workspace [Warning: Fix by move to live workspace] |
||
72 | 'invalid_workspace' => [] |
||
73 | ]; |
||
74 | |||
75 | /** |
||
76 | * Configuring the command options |
||
77 | */ |
||
78 | public function configure() |
||
105 | ); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Executes the command to find versioned records |
||
110 | * |
||
111 | * @param InputInterface $input |
||
112 | * @param OutputInterface $output |
||
113 | * @return int |
||
114 | */ |
||
115 | protected function execute(InputInterface $input, OutputInterface $output) |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Recursive traversal of page tree, fetching ALL versioned records found in the database |
||
242 | * |
||
243 | * @param int $rootID Page root id (must be online, valid page record - or zero for page tree root) |
||
244 | * @param int $depth Depth |
||
245 | * @param bool $isInsideVersionedPage DON'T set from outside, internal. (indicates we are inside a version of a page) |
||
246 | * @param bool $rootIsVersion DON'T set from outside, internal. Indicates that rootID is a version of a page |
||
247 | */ |
||
248 | protected function traversePageTreeForVersionedRecords(int $rootID, int $depth, bool $isInsideVersionedPage = false, bool $rootIsVersion = false) |
||
249 | { |
||
250 | $queryBuilder = $this->connectionPool->getQueryBuilderForTable('pages'); |
||
251 | $queryBuilder->getRestrictions()->removeAll(); |
||
252 | |||
253 | $pageRecord = $queryBuilder |
||
254 | ->select( |
||
255 | 'deleted', |
||
256 | 'title', |
||
257 | 't3ver_wsid' |
||
258 | ) |
||
259 | ->from('pages') |
||
260 | ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($rootID, \PDO::PARAM_INT))) |
||
261 | ->execute() |
||
262 | ->fetch(); |
||
263 | |||
264 | // If rootIsVersion is set it means that the input rootID is that of a version of a page. See below where the recursive call is made. |
||
265 | if ($rootIsVersion) { |
||
266 | $workspaceId = (int)$pageRecord['t3ver_wsid']; |
||
267 | $this->foundRecords['all_versioned_records']['pages'][$rootID] = $rootID; |
||
268 | // If it has been published and is in archive now... |
||
269 | if ($workspaceId === 0) { |
||
270 | $this->foundRecords['versions_in_live']['pages'][$rootID] = $rootID; |
||
271 | } |
||
272 | // If it doesn't belong to a workspace... |
||
273 | if (!isset($this->allWorkspaces[$workspaceId])) { |
||
274 | $this->foundRecords['invalid_workspace']['pages'][$rootID] = $rootID; |
||
275 | } |
||
276 | } |
||
277 | // Only check for records if not inside a version |
||
278 | if (!$isInsideVersionedPage) { |
||
279 | // Traverse tables of records that belongs to page |
||
280 | $tableNames = $this->getAllVersionableTables(); |
||
281 | foreach ($tableNames as $tableName) { |
||
282 | if ($tableName !== 'pages') { |
||
283 | // Select all records belonging to page: |
||
284 | $queryBuilder = $this->connectionPool |
||
285 | ->getQueryBuilderForTable($tableName); |
||
286 | |||
287 | $queryBuilder->getRestrictions()->removeAll(); |
||
288 | |||
289 | $result = $queryBuilder |
||
290 | ->select('uid') |
||
291 | ->from($tableName) |
||
292 | ->where( |
||
293 | $queryBuilder->expr()->eq( |
||
294 | 'pid', |
||
295 | $queryBuilder->createNamedParameter($rootID, \PDO::PARAM_INT) |
||
296 | ) |
||
297 | ) |
||
298 | ->execute(); |
||
299 | while ($rowSub = $result->fetch()) { |
||
300 | // Add any versions of those records |
||
301 | $versions = BackendUtility::selectVersionsOfRecord($tableName, $rowSub['uid'], 'uid,t3ver_wsid' . ($GLOBALS['TCA'][$tableName]['ctrl']['delete'] ? ',' . $GLOBALS['TCA'][$tableName]['ctrl']['delete'] : ''), null, true); |
||
302 | if (is_array($versions)) { |
||
303 | foreach ($versions as $verRec) { |
||
304 | if (!$verRec['_CURRENT_VERSION']) { |
||
305 | // Register version |
||
306 | $this->foundRecords['all_versioned_records'][$tableName][$verRec['uid']] = $verRec['uid']; |
||
307 | $workspaceId = (int)$verRec['t3ver_wsid']; |
||
308 | if ($workspaceId === 0) { |
||
309 | $this->foundRecords['versions_in_live'][$tableName][$verRec['uid']] = $verRec['uid']; |
||
310 | } |
||
311 | if (!isset($this->allWorkspaces[$workspaceId])) { |
||
312 | $this->foundRecords['invalid_workspace'][$tableName][$verRec['uid']] = $verRec['uid']; |
||
313 | } |
||
314 | } |
||
315 | } |
||
316 | } |
||
317 | } |
||
318 | } |
||
319 | } |
||
320 | } |
||
321 | // Find subpages to root ID and traverse (only when rootID is not a version or is a branch-version): |
||
322 | if ($depth > 0) { |
||
323 | $depth--; |
||
324 | $queryBuilder = $this->connectionPool |
||
325 | ->getQueryBuilderForTable('pages'); |
||
326 | |||
327 | $queryBuilder->getRestrictions()->removeAll(); |
||
328 | $queryBuilder->getRestrictions()->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
329 | |||
330 | $queryBuilder |
||
331 | ->select('uid') |
||
332 | ->from('pages') |
||
333 | ->where( |
||
334 | $queryBuilder->expr()->eq( |
||
335 | 'pid', |
||
336 | $queryBuilder->createNamedParameter($rootID, \PDO::PARAM_INT) |
||
337 | ) |
||
338 | ) |
||
339 | ->orderBy('sorting'); |
||
340 | |||
341 | $result = $queryBuilder->execute(); |
||
342 | while ($row = $result->fetch()) { |
||
343 | $this->traversePageTreeForVersionedRecords((int)$row['uid'], $depth, $isInsideVersionedPage, false); |
||
344 | } |
||
345 | } |
||
346 | // Add any versions of pages |
||
347 | if ($rootID > 0) { |
||
348 | $versions = BackendUtility::selectVersionsOfRecord('pages', $rootID, 'uid,t3ver_oid,t3ver_wsid', null, true); |
||
349 | if (is_array($versions)) { |
||
350 | foreach ($versions as $verRec) { |
||
351 | if (!$verRec['_CURRENT_VERSION']) { |
||
352 | $this->traversePageTreeForVersionedRecords((int)$verRec['uid'], $depth, true, true); |
||
353 | } |
||
354 | } |
||
355 | } |
||
356 | } |
||
357 | } |
||
358 | |||
359 | /************************** |
||
360 | * actions / delete methods |
||
361 | **************************/ |
||
362 | |||
363 | /** |
||
364 | * Deletes records via DataHandler |
||
365 | * |
||
366 | * @param array $records two level array with tables and uids |
||
367 | * @param bool $dryRun check if the records should NOT be deleted (use --dry-run to avoid) |
||
368 | * @param SymfonyStyle $io |
||
369 | */ |
||
370 | protected function deleteRecords(array $records, bool $dryRun, SymfonyStyle $io) |
||
371 | { |
||
372 | // Putting "pages" table in the bottom |
||
373 | if (isset($records['pages'])) { |
||
374 | $_pages = $records['pages']; |
||
375 | unset($records['pages']); |
||
376 | // To delete sub pages first assuming they are accumulated from top of page tree. |
||
377 | $records['pages'] = array_reverse($_pages); |
||
378 | } |
||
379 | |||
380 | // Set up the data handler instance |
||
381 | $dataHandler = GeneralUtility::makeInstance(DataHandler::class); |
||
382 | $dataHandler->start([], []); |
||
383 | |||
384 | // Traversing records |
||
385 | foreach ($records as $table => $uidsInTable) { |
||
386 | if ($io->isVerbose()) { |
||
387 | $io->writeln('Flushing published records from table "' . $table . '"'); |
||
388 | } |
||
389 | foreach ($uidsInTable as $uid) { |
||
390 | if ($io->isVeryVerbose()) { |
||
391 | $io->writeln('Flushing record "' . $table . ':' . $uid . '"'); |
||
392 | } |
||
393 | if (!$dryRun) { |
||
394 | $dataHandler->deleteEl($table, $uid, true, true); |
||
395 | if (!empty($dataHandler->errorLog)) { |
||
396 | $errorMessage = array_merge(['DataHandler reported an error'], $dataHandler->errorLog); |
||
397 | $io->error($errorMessage); |
||
398 | } elseif (!$io->isQuiet()) { |
||
399 | $io->writeln('Flushed published record "' . $table . ':' . $uid . '".'); |
||
400 | } |
||
401 | } |
||
402 | } |
||
403 | } |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * Set the workspace ID to "0" (= live) for records that have a workspace not found |
||
408 | * in the system (e.g. hard deleted in the database) |
||
409 | * |
||
410 | * @param array $records array with array of table and uid of each record |
||
411 | * @param bool $dryRun check if the records should NOT be deleted (use --dry-run to avoid) |
||
412 | * @param SymfonyStyle $io |
||
413 | */ |
||
414 | protected function resetRecordsWithoutValidWorkspace(array $records, bool $dryRun, SymfonyStyle $io) |
||
415 | { |
||
416 | foreach ($records as $table => $uidsInTable) { |
||
417 | if ($io->isVerbose()) { |
||
418 | $io->writeln('Resetting workspace to zero for records from table "' . $table . '"'); |
||
419 | } |
||
420 | foreach ($uidsInTable as $uid) { |
||
421 | if ($io->isVeryVerbose()) { |
||
422 | $io->writeln('Flushing record "' . $table . ':' . $uid . '"'); |
||
423 | } |
||
424 | if (!$dryRun) { |
||
425 | $queryBuilder = $this->connectionPool |
||
426 | ->getQueryBuilderForTable($table); |
||
427 | |||
428 | $queryBuilder |
||
429 | ->update($table) |
||
430 | ->where( |
||
431 | $queryBuilder->expr()->eq( |
||
432 | 'uid', |
||
433 | $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT) |
||
434 | ) |
||
435 | ) |
||
436 | ->set('t3ver_wsid', 0) |
||
437 | ->execute(); |
||
438 | if (!$io->isQuiet()) { |
||
439 | $io->writeln('Flushed record "' . $table . ':' . $uid . '".'); |
||
440 | } |
||
441 | } |
||
442 | } |
||
443 | } |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * HELPER FUNCTIONS |
||
448 | */ |
||
449 | |||
450 | /** |
||
451 | * Fetches all sys_workspace records from the database |
||
452 | * |
||
453 | * @return array all workspaces with UID as key, and the title as value |
||
454 | */ |
||
455 | protected function loadAllWorkspaceRecords(): array |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * Returns all TCA tables where workspaces is enabled |
||
477 | * |
||
478 | * @return array |
||
479 | */ |
||
480 | protected function getAllVersionableTables(): array |
||
491 |