| Total Complexity | 114 |
| Total Lines | 944 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like WorkspaceService 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 WorkspaceService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class WorkspaceService implements SingletonInterface |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var array |
||
| 39 | */ |
||
| 40 | protected $versionsOnPageCache = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $pagesWithVersionsInTable = []; |
||
| 46 | |||
| 47 | const TABLE_WORKSPACE = 'sys_workspace'; |
||
| 48 | const LIVE_WORKSPACE_ID = 0; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * retrieves the available workspaces from the database and checks whether |
||
| 52 | * they're available to the current BE user |
||
| 53 | * |
||
| 54 | * @return array array of workspaces available to the current user |
||
| 55 | */ |
||
| 56 | public function getAvailableWorkspaces() |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Gets the current workspace ID. |
||
| 84 | * |
||
| 85 | * @return int The current workspace ID |
||
| 86 | */ |
||
| 87 | public function getCurrentWorkspace() |
||
| 88 | { |
||
| 89 | return $GLOBALS['BE_USER']->workspace; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * easy function to just return the number of hours. |
||
| 94 | * |
||
| 95 | * a preview link is valid, based on the workspaces' custom value (default to 48 hours) |
||
| 96 | * or falls back to the users' TSconfig value "options.workspaces.previewLinkTTLHours". |
||
| 97 | * |
||
| 98 | * by default, it's 48hs. |
||
| 99 | * |
||
| 100 | * @return int The hours as a number |
||
| 101 | */ |
||
| 102 | public function getPreviewLinkLifetime(): int |
||
| 103 | { |
||
| 104 | $workspaceId = $GLOBALS['BE_USER']->workspace; |
||
| 105 | if ($workspaceId > 0) { |
||
| 106 | $wsRecord = BackendUtility::getRecord('sys_workspace', $workspaceId, '*'); |
||
| 107 | if (($wsRecord['previewlink_lifetime'] ?? 0) > 0) { |
||
| 108 | return (int)$wsRecord['previewlink_lifetime']; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | $ttlHours = (int)($GLOBALS['BE_USER']->getTSConfig()['options.']['workspaces.']['previewLinkTTLHours'] ?? 0); |
||
| 112 | return $ttlHours ?: 24 * 2; |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Find the title for the requested workspace. |
||
| 117 | * |
||
| 118 | * @param int $wsId |
||
| 119 | * @return string |
||
| 120 | * @throws \InvalidArgumentException |
||
| 121 | */ |
||
| 122 | public static function getWorkspaceTitle($wsId) |
||
| 123 | { |
||
| 124 | $title = false; |
||
| 125 | switch ($wsId) { |
||
| 126 | case self::LIVE_WORKSPACE_ID: |
||
| 127 | $title = static::getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_misc.xlf:shortcut_onlineWS'); |
||
| 128 | break; |
||
| 129 | default: |
||
| 130 | $labelField = $GLOBALS['TCA']['sys_workspace']['ctrl']['label']; |
||
| 131 | $wsRecord = BackendUtility::getRecord('sys_workspace', $wsId, 'uid,' . $labelField); |
||
| 132 | if (is_array($wsRecord)) { |
||
| 133 | $title = $wsRecord[$labelField]; |
||
| 134 | } |
||
| 135 | } |
||
| 136 | if ($title === false) { |
||
| 137 | throw new \InvalidArgumentException('No such workspace defined', 1476045469); |
||
| 138 | } |
||
| 139 | return $title; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Building DataHandler CMD-array for swapping all versions in a workspace. |
||
| 144 | * |
||
| 145 | * @param int $wsid Real workspace ID, cannot be ONLINE (zero). |
||
| 146 | * @param bool $doSwap If set, then the currently online versions are swapped into the workspace in exchange for the offline versions. Otherwise the workspace is emptied. |
||
| 147 | * @param int $pageId The page id |
||
| 148 | * @param int $language Select specific language only |
||
| 149 | * @return array Command array for DataHandler |
||
| 150 | */ |
||
| 151 | public function getCmdArrayForPublishWS($wsid, $doSwap, $pageId = 0, $language = null) |
||
| 152 | { |
||
| 153 | $wsid = (int)$wsid; |
||
| 154 | $cmd = []; |
||
| 155 | if ($wsid > 0) { |
||
| 156 | // Define stage to select: |
||
| 157 | $stage = -99; |
||
| 158 | $workspaceRec = BackendUtility::getRecord('sys_workspace', $wsid); |
||
| 159 | if ($workspaceRec['publish_access'] & 1) { |
||
| 160 | $stage = StagesService::STAGE_PUBLISH_ID; |
||
| 161 | } |
||
| 162 | // Select all versions to swap: |
||
| 163 | $versions = $this->selectVersionsInWorkspace( |
||
| 164 | $wsid, |
||
| 165 | $stage, |
||
| 166 | $pageId ?: -1, |
||
| 167 | 999, |
||
| 168 | 'tables_modify', |
||
| 169 | $language |
||
| 170 | ); |
||
| 171 | // Traverse the selection to build CMD array: |
||
| 172 | foreach ($versions as $table => $records) { |
||
| 173 | foreach ($records as $rec) { |
||
| 174 | // Build the cmd Array: |
||
| 175 | $cmd[$table][$rec['t3ver_oid']]['version'] = ['action' => 'swap', 'swapWith' => $rec['uid'], 'swapIntoWS' => $doSwap ? 1 : 0]; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | } |
||
| 179 | return $cmd; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Building DataHandler CMD-array for releasing all versions in a workspace. |
||
| 184 | * |
||
| 185 | * @param int $wsid Real workspace ID, cannot be ONLINE (zero). |
||
| 186 | * @param bool $flush Run Flush (TRUE) or ClearWSID (FALSE) command |
||
| 187 | * @param int $pageId The page id |
||
| 188 | * @param int $language Select specific language only |
||
| 189 | * @return array Command array for DataHandler |
||
| 190 | */ |
||
| 191 | public function getCmdArrayForFlushWS($wsid, $flush = true, $pageId = 0, $language = null) |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Select all records from workspace pending for publishing |
||
| 220 | * Used from backend to display workspace overview |
||
| 221 | * User for auto-publishing for selecting versions for publication |
||
| 222 | * |
||
| 223 | * @param int $wsid Workspace ID. If -99, will select ALL versions from ANY workspace. If -98 will select all but ONLINE. >=-1 will select from the actual workspace |
||
| 224 | * @param int $stage Stage filter: -99 means no filtering, otherwise it will be used to select only elements with that stage. For publishing, that would be "10 |
||
| 225 | * @param int $pageId Page id: Live page for which to find versions in workspace! |
||
| 226 | * @param int $recursionLevel Recursion Level - select versions recursive - parameter is only relevant if $pageId != -1 |
||
| 227 | * @param string $selectionType How to collect records for "listing" or "modify" these tables. Support the permissions of each type of record, see \TYPO3\CMS\Core\Authentication\BackendUserAuthentication::check. |
||
| 228 | * @param int $language Select specific language only |
||
| 229 | * @return array Array of all records uids etc. First key is table name, second key incremental integer. Records are associative arrays with uid and t3ver_oidfields. The pid of the online record is found as "livepid" the pid of the offline record is found in "wspid |
||
| 230 | */ |
||
| 231 | public function selectVersionsInWorkspace($wsid, $stage = -99, $pageId = -1, $recursionLevel = 0, $selectionType = 'tables_select', $language = null) |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Find all versionized elements except moved records. |
||
| 275 | * |
||
| 276 | * @param string $table |
||
| 277 | * @param string $pageList |
||
| 278 | * @param int $wsid |
||
| 279 | * @param int $stage |
||
| 280 | * @param int $language |
||
| 281 | * @return array |
||
| 282 | */ |
||
| 283 | protected function selectAllVersionsFromPages($table, $pageList, $wsid, $stage, $language = null) |
||
| 284 | { |
||
| 285 | // Include root level page as there might be some records with where root level |
||
| 286 | // restriction is ignored (e.g. FAL records) |
||
| 287 | if ($pageList !== '' && BackendUtility::isRootLevelRestrictionIgnored($table)) { |
||
| 288 | $pageList .= ',0'; |
||
| 289 | } |
||
| 290 | $isTableLocalizable = BackendUtility::isTableLocalizable($table); |
||
| 291 | $languageParentField = ''; |
||
| 292 | // If table is not localizable, but localized records shall |
||
| 293 | // be collected, an empty result array needs to be returned: |
||
| 294 | if ($isTableLocalizable === false && $language > 0) { |
||
| 295 | return []; |
||
| 296 | } |
||
| 297 | if ($isTableLocalizable) { |
||
| 298 | $languageParentField = 'A.' . $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField']; |
||
| 299 | } |
||
| 300 | |||
| 301 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($table); |
||
| 302 | $queryBuilder->getRestrictions()->removeAll() |
||
| 303 | ->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
| 304 | |||
| 305 | $fields = ['A.uid', 'A.pid', 'A.t3ver_oid', 'A.t3ver_stage', 'B.pid', 'B.pid AS wspid', 'B.pid AS livepid']; |
||
| 306 | if ($isTableLocalizable) { |
||
| 307 | $fields[] = $languageParentField; |
||
| 308 | $fields[] = 'A.' . $GLOBALS['TCA'][$table]['ctrl']['languageField']; |
||
| 309 | } |
||
| 310 | // Table A is the offline version and t3ver_oid>0 defines offline |
||
| 311 | // Table B (online) must have t3ver_oid=0 to signify being online. |
||
| 312 | $constraints = [ |
||
| 313 | $queryBuilder->expr()->gt( |
||
| 314 | 'A.t3ver_oid', |
||
| 315 | $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT) |
||
| 316 | ), |
||
| 317 | $queryBuilder->expr()->eq( |
||
| 318 | 'B.t3ver_oid', |
||
| 319 | $queryBuilder->createNamedParameter(0, \PDO::PARAM_INT) |
||
| 320 | ), |
||
| 321 | $queryBuilder->expr()->neq( |
||
| 322 | 'A.t3ver_state', |
||
| 323 | $queryBuilder->createNamedParameter( |
||
| 324 | (string)new VersionState(VersionState::MOVE_POINTER), |
||
| 325 | \PDO::PARAM_INT |
||
| 326 | ) |
||
| 327 | ) |
||
| 328 | ]; |
||
| 329 | |||
| 330 | if ($pageList) { |
||
| 331 | $pageIdRestriction = GeneralUtility::intExplode(',', $pageList, true); |
||
| 332 | if ($table === 'pages') { |
||
| 333 | $constraints[] = $queryBuilder->expr()->orX( |
||
| 334 | $queryBuilder->expr()->in( |
||
| 335 | 'B.uid', |
||
| 336 | $queryBuilder->createNamedParameter( |
||
| 337 | $pageIdRestriction, |
||
| 338 | Connection::PARAM_INT_ARRAY |
||
| 339 | ) |
||
| 340 | ), |
||
| 341 | $queryBuilder->expr()->in( |
||
| 342 | 'B.' . $GLOBALS['TCA'][$table]['ctrl']['transOrigPointerField'], |
||
| 343 | $queryBuilder->createNamedParameter( |
||
| 344 | $pageIdRestriction, |
||
| 345 | Connection::PARAM_INT_ARRAY |
||
| 346 | ) |
||
| 347 | ) |
||
| 348 | ); |
||
| 349 | } else { |
||
| 350 | $constraints[] = $queryBuilder->expr()->in( |
||
| 351 | 'B.pid', |
||
| 352 | $queryBuilder->createNamedParameter( |
||
| 353 | $pageIdRestriction, |
||
| 354 | Connection::PARAM_INT_ARRAY |
||
| 355 | ) |
||
| 356 | ); |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | if ($isTableLocalizable && MathUtility::canBeInterpretedAsInteger($language)) { |
||
| 361 | $constraints[] = $queryBuilder->expr()->eq( |
||
| 362 | 'A.' . $GLOBALS['TCA'][$table]['ctrl']['languageField'], |
||
| 363 | $queryBuilder->createNamedParameter($language, \PDO::PARAM_INT) |
||
| 364 | ); |
||
| 365 | } |
||
| 366 | |||
| 367 | if ($wsid >= 0) { |
||
| 368 | $constraints[] = $queryBuilder->expr()->eq( |
||
| 369 | 'A.t3ver_wsid', |
||
| 370 | $queryBuilder->createNamedParameter($wsid, \PDO::PARAM_INT) |
||
| 371 | ); |
||
| 372 | } |
||
| 373 | |||
| 374 | if ((int)$stage !== -99) { |
||
| 375 | $constraints[] = $queryBuilder->expr()->eq( |
||
| 376 | 'A.t3ver_stage', |
||
| 377 | $queryBuilder->createNamedParameter($stage, \PDO::PARAM_INT) |
||
| 378 | ); |
||
| 379 | } |
||
| 380 | |||
| 381 | // ... and finally the join between the two tables. |
||
| 382 | $constraints[] = $queryBuilder->expr()->eq('A.t3ver_oid', $queryBuilder->quoteIdentifier('B.uid')); |
||
| 383 | |||
| 384 | // Select all records from this table in the database from the workspace |
||
| 385 | // This joins the online version with the offline version as tables A and B |
||
| 386 | // Order by UID, mostly to have a sorting in the backend overview module which |
||
| 387 | // doesn't "jump around" when swapping. |
||
| 388 | $rows = $queryBuilder->select(...$fields) |
||
| 389 | ->from($table, 'A') |
||
| 390 | ->from($table, 'B') |
||
| 391 | ->where(...$constraints) |
||
| 392 | ->orderBy('B.uid') |
||
| 393 | ->execute() |
||
| 394 | ->fetchAll(); |
||
| 395 | |||
| 396 | return $rows; |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Find all moved records at their new position. |
||
| 401 | * |
||
| 402 | * @param string $table |
||
| 403 | * @param string $pageList |
||
| 404 | * @param int $wsid |
||
| 405 | * @param int $stage |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | protected function getMoveToPlaceHolderFromPages($table, $pageList, $wsid, $stage) |
||
| 511 | } |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Find all page uids recursive starting from a specific page |
||
| 515 | * |
||
| 516 | * @param int $pageId |
||
| 517 | * @param int $wsid |
||
| 518 | * @param int $recursionLevel |
||
| 519 | * @return string Comma sep. uid list |
||
| 520 | */ |
||
| 521 | protected function getTreeUids($pageId, $wsid, $recursionLevel) |
||
| 522 | { |
||
| 523 | // Reusing existing functionality with the drawback that |
||
| 524 | // mount points are not covered yet |
||
| 525 | $permsClause = QueryHelper::stripLogicalOperatorPrefix( |
||
| 526 | $GLOBALS['BE_USER']->getPagePermsClause(Permission::PAGE_SHOW) |
||
| 527 | ); |
||
| 528 | if ($pageId > 0) { |
||
| 529 | $pageList = array_merge( |
||
| 530 | [ (int)$pageId ], |
||
| 531 | $this->getPageChildrenRecursive((int)$pageId, (int)$recursionLevel, 0, $permsClause) |
||
| 532 | ); |
||
| 533 | } else { |
||
| 534 | $mountPoints = $GLOBALS['BE_USER']->uc['pageTree_temporaryMountPoint']; |
||
| 535 | if (!is_array($mountPoints) || empty($mountPoints)) { |
||
| 536 | $mountPoints = array_map('intval', $GLOBALS['BE_USER']->returnWebmounts()); |
||
| 537 | $mountPoints = array_unique($mountPoints); |
||
| 538 | } |
||
| 539 | $pageList = []; |
||
| 540 | foreach ($mountPoints as $mountPoint) { |
||
| 541 | $pageList = array_merge( |
||
| 542 | $pageList |
||
| 543 | [ (int)$mountPoint ], |
||
| 544 | $this->getPageChildrenRecursive((int)$mountPoint, (int)$recursionLevel, 0, $permsClause) |
||
| 545 | ); |
||
| 546 | } |
||
| 547 | } |
||
| 548 | $pageList = array_unique($pageList); |
||
| 549 | |||
| 550 | if (BackendUtility::isTableWorkspaceEnabled('pages') && !empty($pageList)) { |
||
| 551 | // Remove the "subbranch" if a page was moved away |
||
| 552 | $pageIds = $pageList; |
||
| 553 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages'); |
||
| 554 | $queryBuilder->getRestrictions() |
||
| 555 | ->removeAll() |
||
| 556 | ->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
| 557 | $result = $queryBuilder |
||
| 558 | ->select('uid', 'pid', 't3ver_move_id') |
||
| 559 | ->from('pages') |
||
| 560 | ->where( |
||
| 561 | $queryBuilder->expr()->in( |
||
| 562 | 't3ver_move_id', |
||
| 563 | $queryBuilder->createNamedParameter($pageIds, Connection::PARAM_INT_ARRAY) |
||
| 564 | ), |
||
| 565 | $queryBuilder->expr()->eq( |
||
| 566 | 't3ver_wsid', |
||
| 567 | $queryBuilder->createNamedParameter($wsid, \PDO::PARAM_INT) |
||
| 568 | ) |
||
| 569 | ) |
||
| 570 | ->orderBy('uid') |
||
| 571 | ->execute(); |
||
| 572 | |||
| 573 | $movedAwayPages = []; |
||
| 574 | while ($row = $result->fetch()) { |
||
| 575 | $movedAwayPages[$row['t3ver_move_id']] = $row; |
||
| 576 | } |
||
| 577 | |||
| 578 | // move all pages away |
||
| 579 | $newList = array_diff($pageIds, array_keys($movedAwayPages)); |
||
| 580 | // keep current page in the list |
||
| 581 | $newList[] = $pageId; |
||
| 582 | // move back in if still connected to the "remaining" pages |
||
| 583 | do { |
||
| 584 | $changed = false; |
||
| 585 | foreach ($movedAwayPages as $uid => $rec) { |
||
| 586 | if (in_array($rec['pid'], $newList) && !in_array($uid, $newList)) { |
||
| 587 | $newList[] = $uid; |
||
| 588 | $changed = true; |
||
| 589 | } |
||
| 590 | } |
||
| 591 | } while ($changed); |
||
| 592 | |||
| 593 | // In case moving pages is enabled we need to replace all move-to pointer with their origin |
||
| 594 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages'); |
||
| 595 | $queryBuilder->getRestrictions() |
||
| 596 | ->removeAll() |
||
| 597 | ->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
| 598 | $result = $queryBuilder->select('uid', 't3ver_move_id') |
||
| 599 | ->from('pages') |
||
| 600 | ->where( |
||
| 601 | $queryBuilder->expr()->in( |
||
| 602 | 'uid', |
||
| 603 | $queryBuilder->createNamedParameter($newList, Connection::PARAM_INT_ARRAY) |
||
| 604 | ) |
||
| 605 | ) |
||
| 606 | ->orderBy('uid') |
||
| 607 | ->execute(); |
||
| 608 | |||
| 609 | $pages = []; |
||
| 610 | while ($row = $result->fetch()) { |
||
| 611 | $pages[$row['uid']] = $row; |
||
| 612 | } |
||
| 613 | |||
| 614 | $pageIds = $newList; |
||
| 615 | if (!in_array($pageId, $pageIds)) { |
||
| 616 | $pageIds[] = $pageId; |
||
| 617 | } |
||
| 618 | |||
| 619 | $newList = []; |
||
| 620 | foreach ($pageIds as $pageId) { |
||
|
|
|||
| 621 | if ((int)$pages[$pageId]['t3ver_move_id'] > 0) { |
||
| 622 | $newList[] = (int)$pages[$pageId]['t3ver_move_id']; |
||
| 623 | } else { |
||
| 624 | $newList[] = $pageId; |
||
| 625 | } |
||
| 626 | } |
||
| 627 | $pageList = $newList; |
||
| 628 | } |
||
| 629 | |||
| 630 | return implode(',', $pageList); |
||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Recursively fetch all children of a given page |
||
| 635 | * |
||
| 636 | * @param int $pid uid of the page |
||
| 637 | * @param int $depth |
||
| 638 | * @param int $begin |
||
| 639 | * @param string $permsClause |
||
| 640 | * @return int[] List of child row $uid's |
||
| 641 | */ |
||
| 642 | protected function getPageChildrenRecursive(int $pid, int $depth, int $begin, string $permsClause): array |
||
| 643 | { |
||
| 644 | $children = []; |
||
| 645 | if ($pid && $depth > 0) { |
||
| 646 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable('pages'); |
||
| 647 | $queryBuilder->getRestrictions()->removeAll()->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
| 648 | $statement = $queryBuilder->select('uid') |
||
| 649 | ->from('pages') |
||
| 650 | ->where( |
||
| 651 | $queryBuilder->expr()->eq('pid', $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)), |
||
| 652 | $queryBuilder->expr()->eq('sys_language_uid', 0), |
||
| 653 | $permsClause |
||
| 654 | ) |
||
| 655 | ->execute(); |
||
| 656 | while ($row = $statement->fetch()) { |
||
| 657 | if ($begin <= 0) { |
||
| 658 | $children[] = (int)$row['uid']; |
||
| 659 | } |
||
| 660 | if ($depth > 1) { |
||
| 661 | $theSubList = $this->getPageChildrenRecursive((int)$row['uid'], $depth - 1, $begin - 1, $permsClause); |
||
| 662 | $children = array_merge($children, $theSubList); |
||
| 663 | } |
||
| 664 | } |
||
| 665 | } |
||
| 666 | return $children; |
||
| 667 | } |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Remove all records which are not permitted for the user |
||
| 671 | * |
||
| 672 | * @param array $recs |
||
| 673 | * @param string $table |
||
| 674 | * @return array |
||
| 675 | */ |
||
| 676 | protected function filterPermittedElements($recs, $table) |
||
| 677 | { |
||
| 678 | $permittedElements = []; |
||
| 679 | if (is_array($recs)) { |
||
| 680 | foreach ($recs as $rec) { |
||
| 681 | if ($this->isPageAccessibleForCurrentUser($table, $rec) && $this->isLanguageAccessibleForCurrentUser($table, $rec)) { |
||
| 682 | $permittedElements[] = $rec; |
||
| 683 | } |
||
| 684 | } |
||
| 685 | } |
||
| 686 | return $permittedElements; |
||
| 687 | } |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Checking access to the page the record is on, respecting ignored root level restrictions |
||
| 691 | * |
||
| 692 | * @param string $table Name of the table |
||
| 693 | * @param array $record Record row to be checked |
||
| 694 | * @return bool |
||
| 695 | */ |
||
| 696 | protected function isPageAccessibleForCurrentUser($table, array $record) |
||
| 697 | { |
||
| 698 | $pageIdField = $table === 'pages' ? 'uid' : 'wspid'; |
||
| 699 | $pageId = isset($record[$pageIdField]) ? (int)$record[$pageIdField] : null; |
||
| 700 | if ($pageId === null) { |
||
| 701 | return false; |
||
| 702 | } |
||
| 703 | if ($pageId === 0 && BackendUtility::isRootLevelRestrictionIgnored($table)) { |
||
| 704 | return true; |
||
| 705 | } |
||
| 706 | $page = BackendUtility::getRecord('pages', $pageId, 'uid,pid,perms_userid,perms_user,perms_groupid,perms_group,perms_everybody'); |
||
| 707 | |||
| 708 | return $GLOBALS['BE_USER']->doesUserHaveAccess($page, 1); |
||
| 709 | } |
||
| 710 | |||
| 711 | /** |
||
| 712 | * Check current be users language access on given record. |
||
| 713 | * |
||
| 714 | * @param string $table Name of the table |
||
| 715 | * @param array $record Record row to be checked |
||
| 716 | * @return bool |
||
| 717 | */ |
||
| 718 | protected function isLanguageAccessibleForCurrentUser($table, array $record) |
||
| 719 | { |
||
| 720 | if (BackendUtility::isTableLocalizable($table)) { |
||
| 721 | $languageUid = $record[$GLOBALS['TCA'][$table]['ctrl']['languageField']]; |
||
| 722 | } else { |
||
| 723 | return true; |
||
| 724 | } |
||
| 725 | return $GLOBALS['BE_USER']->checkLanguageAccess($languageUid); |
||
| 726 | } |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Determine whether a specific page is new and not yet available in the LIVE workspace |
||
| 730 | * |
||
| 731 | * @param int $id Primary key of the page to check |
||
| 732 | * @param int $language Language for which to check the page |
||
| 733 | * @return bool |
||
| 734 | */ |
||
| 735 | public static function isNewPage($id, $language = 0) |
||
| 736 | { |
||
| 737 | $isNewPage = false; |
||
| 738 | // If the language is not default, check state of overlay |
||
| 739 | if ($language > 0) { |
||
| 740 | $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) |
||
| 741 | ->getQueryBuilderForTable('pages'); |
||
| 742 | $queryBuilder->getRestrictions() |
||
| 743 | ->removeAll() |
||
| 744 | ->add(GeneralUtility::makeInstance(DeletedRestriction::class)); |
||
| 745 | $row = $queryBuilder->select('t3ver_state') |
||
| 746 | ->from('pages') |
||
| 747 | ->where( |
||
| 748 | $queryBuilder->expr()->eq( |
||
| 749 | $GLOBALS['TCA']['pages']['ctrl']['transOrigPointerField'], |
||
| 750 | $queryBuilder->createNamedParameter($id, \PDO::PARAM_INT) |
||
| 751 | ), |
||
| 752 | $queryBuilder->expr()->eq( |
||
| 753 | $GLOBALS['TCA']['pages']['ctrl']['languageField'], |
||
| 754 | $queryBuilder->createNamedParameter($language, \PDO::PARAM_INT) |
||
| 755 | ), |
||
| 756 | $queryBuilder->expr()->eq( |
||
| 757 | 't3ver_wsid', |
||
| 758 | $queryBuilder->createNamedParameter($GLOBALS['BE_USER']->workspace, \PDO::PARAM_INT) |
||
| 759 | ) |
||
| 760 | ) |
||
| 761 | ->setMaxResults(1) |
||
| 762 | ->execute() |
||
| 763 | ->fetch(); |
||
| 764 | |||
| 765 | if ($row !== false) { |
||
| 766 | $isNewPage = VersionState::cast($row['t3ver_state'])->equals(VersionState::NEW_PLACEHOLDER); |
||
| 767 | } |
||
| 768 | } else { |
||
| 769 | $rec = BackendUtility::getRecord('pages', $id, 't3ver_state'); |
||
| 770 | if (is_array($rec)) { |
||
| 771 | $isNewPage = VersionState::cast($rec['t3ver_state'])->equals(VersionState::NEW_PLACEHOLDER); |
||
| 772 | } |
||
| 773 | } |
||
| 774 | return $isNewPage; |
||
| 775 | } |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Determines whether a page has workspace versions. |
||
| 779 | * |
||
| 780 | * @param int $workspaceId |
||
| 781 | * @param int $pageId |
||
| 782 | * @return bool |
||
| 783 | */ |
||
| 784 | public function hasPageRecordVersions($workspaceId, $pageId) |
||
| 785 | { |
||
| 786 | if ((int)$workspaceId === 0 || (int)$pageId === 0) { |
||
| 787 | return false; |
||
| 788 | } |
||
| 789 | |||
| 790 | if (isset($this->versionsOnPageCache[$workspaceId][$pageId])) { |
||
| 791 | return $this->versionsOnPageCache[$workspaceId][$pageId]; |
||
| 792 | } |
||
| 793 | |||
| 794 | $this->versionsOnPageCache[$workspaceId][$pageId] = false; |
||
| 795 | |||
| 796 | foreach ($GLOBALS['TCA'] as $tableName => $tableConfiguration) { |
||
| 797 | if ($tableName === 'pages' || !BackendUtility::isTableWorkspaceEnabled($tableName)) { |
||
| 798 | continue; |
||
| 799 | } |
||
| 800 | |||
| 801 | $pages = $this->fetchPagesWithVersionsInTable($workspaceId, $tableName); |
||
| 802 | // Early break on first match |
||
| 803 | if (!empty($pages[(string)$pageId])) { |
||
| 804 | $this->versionsOnPageCache[$workspaceId][$pageId] = true; |
||
| 805 | break; |
||
| 806 | } |
||
| 807 | } |
||
| 808 | |||
| 809 | $parameters = [ |
||
| 810 | 'workspaceId' => $workspaceId, |
||
| 811 | 'pageId' => $pageId, |
||
| 812 | 'versionsOnPageCache' => &$this->versionsOnPageCache, |
||
| 813 | ]; |
||
| 814 | foreach ($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][\TYPO3\CMS\Workspaces\Service\WorkspaceService::class]['hasPageRecordVersions'] ?? [] as $hookFunction) { |
||
| 815 | GeneralUtility::callUserFunction($hookFunction, $parameters, $this); |
||
| 816 | } |
||
| 817 | |||
| 818 | return $this->versionsOnPageCache[$workspaceId][$pageId]; |
||
| 819 | } |
||
| 820 | |||
| 821 | /** |
||
| 822 | * Gets all pages that have workspace versions per table. |
||
| 823 | * |
||
| 824 | * Result: |
||
| 825 | * [ |
||
| 826 | * 'sys_template' => [], |
||
| 827 | * 'tt_content' => [ |
||
| 828 | * 1 => true, |
||
| 829 | * 11 => true, |
||
| 830 | * 13 => true, |
||
| 831 | * 15 => true |
||
| 832 | * ], |
||
| 833 | * 'tx_something => [ |
||
| 834 | * 15 => true, |
||
| 835 | * 11 => true, |
||
| 836 | * 21 => true |
||
| 837 | * ], |
||
| 838 | * ] |
||
| 839 | * |
||
| 840 | * @param int $workspaceId |
||
| 841 | * |
||
| 842 | * @return array |
||
| 843 | */ |
||
| 844 | public function getPagesWithVersionsInTable($workspaceId) |
||
| 845 | { |
||
| 846 | foreach ($GLOBALS['TCA'] as $tableName => $tableConfiguration) { |
||
| 847 | if ($tableName === 'pages' || !BackendUtility::isTableWorkspaceEnabled($tableName)) { |
||
| 848 | continue; |
||
| 849 | } |
||
| 850 | |||
| 851 | $this->fetchPagesWithVersionsInTable($workspaceId, $tableName); |
||
| 852 | } |
||
| 853 | |||
| 854 | return $this->pagesWithVersionsInTable[$workspaceId]; |
||
| 855 | } |
||
| 856 | |||
| 857 | /** |
||
| 858 | * Gets all pages that have workspace versions in a particular table. |
||
| 859 | * |
||
| 860 | * Result: |
||
| 861 | * [ |
||
| 862 | * 1 => true, |
||
| 863 | * 11 => true, |
||
| 864 | * 13 => true, |
||
| 865 | * 15 => true |
||
| 866 | * ], |
||
| 867 | * |
||
| 868 | * @param int $workspaceId |
||
| 869 | * @param string $tableName |
||
| 870 | * @return array |
||
| 871 | */ |
||
| 872 | protected function fetchPagesWithVersionsInTable($workspaceId, $tableName) |
||
| 957 | } |
||
| 958 | |||
| 959 | /** |
||
| 960 | * @param string $tableName |
||
| 961 | * @return QueryBuilder |
||
| 962 | */ |
||
| 963 | protected function createQueryBuilderForTable(string $tableName) |
||
| 971 | } |
||
| 972 | |||
| 973 | /** |
||
| 974 | * @return LanguageService|null |
||
| 975 | */ |
||
| 976 | protected static function getLanguageService(): ?LanguageService |
||
| 979 | } |
||
| 980 | } |
||
| 981 |