4b6c12feb42be4f071540bc6920b253a7bf3f0d5
[Packages/TYPO3.CMS.git] / typo3 / sysext / version / Classes / Utility / WorkspacesUtility.php
1 <?php
2 namespace TYPO3\CMS\Version\Utility;
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
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18
19 /**
20 * Library with Workspace related functionality
21 *
22 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
23 */
24 class WorkspacesUtility {
25
26 /**
27 * Building tcemain CMD-array for swapping all versions in a workspace.
28 *
29 * @param int $wsid Real workspace ID, cannot be ONLINE (zero).
30 * @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.
31 * @param int $pageId
32 * @return array Command array for tcemain
33 */
34 public function getCmdArrayForPublishWS($wsid, $doSwap, $pageId = 0) {
35 $wsid = (int)$wsid;
36 $cmd = array();
37 if ($wsid >= -1 && $wsid !== 0) {
38 // Define stage to select:
39 $stage = -99;
40 if ($wsid > 0) {
41 $workspaceRec = BackendUtility::getRecord('sys_workspace', $wsid);
42 if ($workspaceRec['publish_access'] & 1) {
43 $stage = 10;
44 }
45 }
46 // Select all versions to swap:
47 $versions = $this->selectVersionsInWorkspace($wsid, 0, $stage, $pageId ? $pageId : -1);
48 // Traverse the selection to build CMD array:
49 foreach ($versions as $table => $records) {
50 foreach ($records as $rec) {
51 // Build the cmd Array:
52 $cmd[$table][$rec['t3ver_oid']]['version'] = array(
53 'action' => 'swap',
54 'swapWith' => $rec['uid'],
55 'swapIntoWS' => $doSwap ? 1 : 0
56 );
57 }
58 }
59 }
60 return $cmd;
61 }
62
63 /**
64 * Select all records from workspace pending for publishing
65 * Used from backend to display workspace overview
66 * User for auto-publishing for selecting versions for publication
67 *
68 * @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
69 * @param int $filter Lifecycle filter: 1 = select all drafts (never-published), 2 = select all published one or more times (archive/multiple), anything else selects all.
70 * @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
71 * @param int $pageId Page id: Live page for which to find versions in workspace!
72 * @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_oid fields. The REAL pid of the online record is found as "realpid
73 */
74 public function selectVersionsInWorkspace($wsid, $filter = 0, $stage = -99, $pageId = -1) {
75 $wsid = (int)$wsid;
76 $filter = (int)$filter;
77 $pageId = (int)$pageId;
78 $output = array();
79 // Traversing all tables supporting versioning:
80 foreach ($GLOBALS['TCA'] as $table => $cfg) {
81 if ($GLOBALS['TCA'][$table]['ctrl']['versioningWS']) {
82 // Select all records from this table in the database from the workspace
83 // This joins the online version with the offline version as tables A and B
84 $recs = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('A.uid, A.t3ver_oid, B.pid AS realpid', $table . ' A,' . $table . ' B', 'A.pid=-1' . ($pageId != -1 ? ($table === 'pages' ? ' AND B.uid=' . $pageId : ' AND B.pid=' . $pageId) : '') . ($wsid > -98 ? ' AND A.t3ver_wsid=' . $wsid : ($wsid === -98 ? ' AND A.t3ver_wsid!=0' : '')) . ($filter === 1 ? ' AND A.t3ver_count=0' : ($filter === 2 ? ' AND A.t3ver_count>0' : '')) . ($stage != -99 ? ' AND A.t3ver_stage=' . (int)$stage : '') . ' AND B.pid>=0' . ' AND A.t3ver_oid=B.uid' . BackendUtility::deleteClause($table, 'A') . BackendUtility::deleteClause($table, 'B'), '', 'B.uid');
85 if (count($recs)) {
86 $output[$table] = $recs;
87 }
88 }
89 }
90 return $output;
91 }
92
93 /****************************
94 *
95 * Scheduler methods
96 *
97 ****************************/
98 /**
99 * This method is called by the Scheduler task that triggers
100 * the autopublication process
101 * It searches for workspaces whose publication date is in the past
102 * and publishes them
103 *
104 * @return void
105 */
106 public function autoPublishWorkspaces() {
107 // Temporarily set admin rights
108 // FIXME: once workspaces are cleaned up a better solution should be implemented
109 $currentAdminStatus = $GLOBALS['BE_USER']->user['admin'];
110 $GLOBALS['BE_USER']->user['admin'] = 1;
111 // Select all workspaces that needs to be published / unpublished:
112 $workspaces = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid,swap_modes,publish_time,unpublish_time', 'sys_workspace', 'pid=0
113 AND
114 ((publish_time!=0 AND publish_time<=' . (int)$GLOBALS['EXEC_TIME'] . ')
115 OR (publish_time=0 AND unpublish_time!=0 AND unpublish_time<=' . (int)$GLOBALS['EXEC_TIME'] . '))' . BackendUtility::deleteClause('sys_workspace'));
116 foreach ($workspaces as $rec) {
117 // First, clear start/end time so it doesn't get select once again:
118 $fieldArray = $rec['publish_time'] != 0 ? array('publish_time' => 0) : array('unpublish_time' => 0);
119 $GLOBALS['TYPO3_DB']->exec_UPDATEquery('sys_workspace', 'uid=' . (int)$rec['uid'], $fieldArray);
120 // Get CMD array:
121 $cmd = $this->getCmdArrayForPublishWS($rec['uid'], $rec['swap_modes'] == 1);
122 // $rec['swap_modes']==1 means that auto-publishing will swap versions, not just publish and empty the workspace.
123 // Execute CMD array:
124 $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
125 $tce->stripslashes_values = 0;
126 $tce->start(array(), $cmd);
127 $tce->process_cmdmap();
128 }
129 // Restore admin status
130 $GLOBALS['BE_USER']->user['admin'] = $currentAdminStatus;
131 }
132
133 }