More workspace stuff for beta.
[Packages/TYPO3.CMS.git] / typo3 / mod / user / ws / class.wslib.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2005 Kasper Skaarhoj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Library with Workspace related functionality
29 *
30 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
31 */
32 /**
33 * [CLASS/FUNCTION INDEX of SCRIPT]
34 *
35 *
36 *
37 * 68: class wslib
38 * 80: function getCmdArrayForPublishWS($wsid, $doSwap)
39 * 126: function selectVersionsInWorkspace($wsid,$filter=0,$stage=-99,$pageId=-1)
40 *
41 * SECTION: CLI functions
42 * 182: function CLI_main()
43 * 192: function autoPublishWorkspaces()
44 *
45 * TOTAL FUNCTIONS: 4
46 * (This index is automatically created/updated by the extension "extdeveval")
47 *
48 */
49
50
51
52
53
54
55
56
57
58
59
60
61 /**
62 * Library with Workspace related functionality
63 *
64 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
65 * @package TYPO3
66 * @subpackage core
67 */
68 class wslib {
69
70
71
72
73 /**
74 * Building tcemain CMD-array for swapping all versions in a workspace.
75 *
76 * @param integer Real workspace ID, cannot be ONLINE (zero).
77 * @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.
78 * @return array Command array for tcemain
79 */
80 function getCmdArrayForPublishWS($wsid, $doSwap,$pageId=0) {
81
82 $wsid = intval($wsid);
83 $cmd = array();
84
85 if ($wsid>=-1 && $wsid!==0) {
86
87 // Define stage to select:
88 $stage = -99;
89 if ($wsid>0) {
90 $workspaceRec = t3lib_BEfunc::getRecord('sys_workspace',$wsid);
91 if ($workspaceRec['publish_access']&1) {
92 $stage = 10;
93 }
94 }
95
96 // Select all versions to swap:
97 $versions = $this->selectVersionsInWorkspace($wsid,0,$stage,($pageId?$pageId:-1));
98
99 // Traverse the selection to build CMD array:
100 foreach($versions as $table => $records) {
101 foreach($records as $rec) {
102
103 // Build the cmd Array:
104 $cmd[$table][$rec['t3ver_oid']]['version'] = array(
105 'action' => 'swap',
106 'swapWith' => $rec['uid'],
107 'swapIntoWS' => $doSwap ? 1 : 0
108 );
109 }
110 }
111 }
112 return $cmd;
113 }
114
115 /**
116 * Select all records from workspace pending for publishing
117 * Used from backend to display workspace overview
118 * User for auto-publishing for selecting versions for publication
119 *
120 * @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
121 * @param integer Lifecycle filter: 1 = select all drafts (never-published), 2 = select all published one or more times (archive/multiple), anything else selects all.
122 * @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"
123 * @param integer Page id: Live page for which to find versions in workspace!
124 * @return array Array of all records uids etc. First key is table name, second key incremental integer. Records are associative arrays with uid, t3ver_oid and t3ver_swapmode fields. The REAL pid of the online record is found as "realpid"
125 */
126 function selectVersionsInWorkspace($wsid,$filter=0,$stage=-99,$pageId=-1) {
127 global $TCA;
128
129 $wsid = intval($wsid);
130 $filter = intval($filter);
131 $output = array();
132
133 // Traversing all tables supporting versioning:
134 foreach($TCA as $table => $cfg) {
135 if ($TCA[$table]['ctrl']['versioningWS']) {
136
137 // Select all records from this table in the database from the workspace
138 // This joins the online version with the offline version as tables A and B
139 $output[$table] = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows (
140 'A.uid, A.t3ver_oid,'.($table==='pages' ? ' A.t3ver_swapmode,':'').' B.pid AS realpid',
141 $table.' A,'.$table.' B',
142 'A.pid=-1'. // Table A is the offline version and pid=-1 defines offline
143 ($pageId!=-1 ? ($table==='pages' ? ' AND B.uid='.intval($pageId) : ' AND B.pid='.intval($pageId)) : '').
144 ($wsid>-98 ? ' AND A.t3ver_wsid='.$wsid : ($wsid===-98 ? ' AND A.t3ver_wsid!=0' : '')). // For "real" workspace numbers, select by that. If = -98, select all that are NOT online (zero). Anything else below -1 will not select on the wsid and therefore select all!
145 ($filter===1 ? ' AND A.t3ver_count=0' : ($filter===2 ? ' AND A.t3ver_count>0' : '')). // lifecycle filter: 1 = select all drafts (never-published), 2 = select all published one or more times (archive/multiple)
146 ($stage!=-99 ? ' AND A.t3ver_stage='.intval($stage) : '').
147 ' AND B.pid>=0'. // Table B (online) must have PID >= 0 to signify being online.
148 ' AND A.t3ver_oid=B.uid'. // ... and finally the join between the two tables.
149 t3lib_BEfunc::deleteClause($table,'A').
150 t3lib_BEfunc::deleteClause($table,'B'),
151 '',
152 'B.uid' // Order by UID, mostly to have a sorting in the backend overview module which doesn't "jump around" when swapping.
153 );
154 }
155 }
156
157 return $output;
158 }
159
160
161
162
163
164
165
166
167
168
169
170
171 /****************************
172 *
173 * CLI functions
174 *
175 ****************************/
176
177 /**
178 * Main function to call from cli-script
179 *
180 * @return void
181 */
182 function CLI_main() {
183 $this->autoPublishWorkspaces();
184 }
185
186 /**
187 * CLI internal function:
188 * Will search for workspaces which has timed out regarding publishing and publish them
189 *
190 * @return void
191 */
192 function autoPublishWorkspaces() {
193 global $TYPO3_CONF_VARS;
194
195 // Temporarily set high power...
196 $GLOBALS['BE_USER']->user['admin'] = 1;
197
198 // Select all workspaces that needs to be published / unpublished:
199 $workspaces = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
200 'uid,swap_modes,publish_time,unpublish_time',
201 'sys_workspace',
202 'pid=0
203 AND
204 ((publish_time!=0 AND publish_time<='.intval($GLOBALS['EXEC_TIME']).')
205 OR (publish_time=0 AND unpublish_time!=0 AND unpublish_time<='.intval($GLOBALS['EXEC_TIME']).'))'.
206 t3lib_BEfunc::deleteClause('sys_workspace')
207 );
208
209 // If any workspaces was selected:
210 if (count($workspaces)) {
211 require_once(PATH_t3lib.'class.t3lib_tcemain.php'); // Including it here because we want to load a 1-minute cronjob with inclusion only if needed!
212 }
213
214 foreach($workspaces as $rec) {
215
216 // First, clear start/end time so it doesn't get select once again:
217 $fieldArray = $rec['publish_time']!=0 ? array('publish_time'=>0) : array('unpublish_time'=>0);
218 $GLOBALS['TYPO3_DB']->exec_UPDATEquery('sys_workspace','uid='.intval($rec['uid']),$fieldArray);
219
220 // Get CMD array:
221 $cmd = $this->getCmdArrayForPublishWS($rec['uid'], $rec['swap_modes']==1); // $rec['swap_modes']==1 means that auto-publishing will swap versions, not just publish and empty the workspace.
222
223 // Execute CMD array:
224 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
225 $tce->stripslashes_values = 0;
226 $tce->start(array(),$cmd);
227 $tce->process_cmdmap();
228 }
229 }
230 }
231
232
233
234 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/mod/user/ws/class.wslib.php']) {
235 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['typo3/mod/user/ws/class.wslib.php']);
236 }
237 ?>