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