Feature #10439 » 10439v3.patch

Tolleiv Nietsch, 2011-01-18 02:34

View differences:

Classes/ExtDirect/Server.php (working copy)
45 45
		$versions = $wslibObj->selectVersionsInWorkspace($this->getCurrentWorkspace(), 0, -99, $pageId, $parameter->depth);
46 46

  
47 47
		$workspacesService = t3lib_div::makeInstance('tx_Workspaces_Service_GridData');
48
		$data = $workspacesService->generateGridListFromVersions($versions, $parameter);
48
		$data = $workspacesService->generateGridListFromVersions($versions, $parameter, $this->getCurrentWorkspace());
49 49
		return $data;
50 50
	}
51 51

  
Classes/Service/GridData.php (working copy)
31 31
 * @subpackage Service
32 32
 */
33 33
class tx_Workspaces_Service_GridData {
34
	protected $currentWorkspace = NULL;
34 35
	protected $dataArray = array();
35 36
	protected $sort = '';
36 37
	protected $sortDir = '';
38
	protected $workspacesCache = NULL;
37 39

  
38 40
	/**
39 41
	 * Generates grid list array from given versions.
......
41 43
	 * @param array $versions 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 pid of the online record is found as "livepid" the pid of the offline record is found in "wspid"
42 44
	 * @param object $parameter
43 45
	 * @return array
46
	 * @throws InvalidArgumentException
44 47
	 */
45
	public function generateGridListFromVersions($versions, $parameter) {
48
	public function generateGridListFromVersions($versions, $parameter, $currentWorkspace) {
46 49

  
47 50
			// Read the given parameters from grid. If the parameter is not set use default values.
48 51
		$filterTxt = isset($parameter->filterTxt) ? $parameter->filterTxt : '';
......
50 53
		$limit = isset($parameter->limit) ? intval($parameter->limit) : 10;
51 54
		$this->sort = isset($parameter->sort) ? $parameter->sort : 't3ver_oid';
52 55
		$this->sortDir = isset($parameter->dir) ? $parameter->dir : 'ASC';
56
		
57
		if (is_int($currentWorkspace)) {
58
			$this->currentWorkspace = $currentWorkspace;
59
		} else {
60
			throw new InvalidArgumentException('No such workspace defined');
61
		}
53 62

  
54 63
		$data = array();
55 64
		$data['data'] = array();
......
77 86
		$workspacesObj = t3lib_div::makeInstance('Tx_Workspaces_Service_Workspaces');
78 87
		$availableWorkspaces = $workspacesObj->getAvailableWorkspaces();
79 88

  
80
		foreach ($versions as $table => $records) {
81
			$versionArray = array('table' => $table);
89
		$this->initializeWorkspacesCachingFramework();
82 90

  
83
			foreach ($records as $record) {
84

  
85
				$origRecord = t3lib_BEFunc::getRecord($table, $record['t3ver_oid']);
86
				$versionRecord = t3lib_BEFunc::getRecord($table, $record['uid']);
87

  
88
				// check the given version is from an available workspace
89
				if (array_key_exists($versionRecord['t3ver_wsid'], $availableWorkspaces)) {
90
					
91
		// check for dataArray in cache
92
		if ($this->getDataArrayFromCache($versions, $filterTxt) == FALSE) {
93
			$stagesObj = t3lib_div::makeInstance('Tx_Workspaces_Service_Stages');
94
	
95
			foreach ($versions as $table => $records) {
96
				$versionArray = array('table' => $table);
97
	
98
				foreach ($records as $record) {
99
	
100
					$origRecord = t3lib_BEFunc::getRecord($table, $record['t3ver_oid']);
101
					$versionRecord = t3lib_BEFunc::getRecord($table, $record['uid']);
102
	
91 103
					if (isset($GLOBALS['TCA'][$table]['columns']['hidden'])) {
92 104
						$recordState = $this->workspaceState($versionRecord['t3ver_state'], $origRecord['hidden'], $versionRecord['hidden']);
93 105
					} else {
......
131 143
					}
132 144
				}
133 145
			}
146
			$this->sortDataArray();
147
			
148
			$this->setDataArrayIntoCache($versions, $filterTxt);
134 149
		}
135 150
		$this->sortDataArray();
136 151
	}
......
153 168
		return $dataArrayPart;
154 169
	}
155 170

  
171

  
156 172
	/**
173
	 * Initialize the workspace cache
174
	 * 
175
	 * @return void
176
	 */
177
	protected function initializeWorkspacesCachingFramework() {
178
		if (TYPO3_UseCachingFramework) {
179
			try {
180
				$GLOBALS['typo3CacheFactory']->create(
181
					'workspaces_cache',
182
					't3lib_cache_frontend_StringFrontend',
183
					$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['sys_workspace_cache']['backend'],
184
					$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['sys_workspace_cache']['options']);
185
			} catch (t3lib_cache_exception_DuplicateIdentifier $e) {
186
				// do nothing, a workspace cache already exists
187
			}
188
	
189
			$this->workspacesCache = $GLOBALS['typo3CacheManager']->getCache('workspaces_cache');
190
		}
191
	}
192

  
193

  
194
	/**
195
	 * Put the generated dataArray into the workspace cache.
196
	 * 
197
	 * @param array $versions 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 pid of the online record is found as "livepid" the pid of the offline record is found in "wspid"
198
	 * @param string $filterTxt The given filter text from the grid.
199
	 */
200
	protected function setDataArrayIntoCache (array $versions, $filterTxt) {
201
		if (TYPO3_UseCachingFramework) {
202
			$hash = $this->calculateHash($versions, $filterTxt);
203
			$content = serialize($this->dataArray);
204
			
205
			$this->workspacesCache->set($hash, $content, array($this->currentWorkspace));
206
		}
207
	}
208
	
209
	
210
	/**
211
	 * Checks if a cache entry is given for given versions and filter text and tries to load the data array from cache.
212
	 * 
213
	 * @param array $versions 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 pid of the online record is found as "livepid" the pid of the offline record is found in "wspid"
214
	 * @param string $filterTxt The given filter text from the grid.
215
	 */
216
	protected function getDataArrayFromCache (array $versions, $filterTxt) {
217
		$cacheEntry = FALSE;
218
		
219
		if (TYPO3_UseCachingFramework) {
220
			$hash = $this->calculateHash($versions, $filterTxt);
221
			
222
			$content = $this->workspacesCache->get($hash);
223
			
224
			if ($content != FALSE) {
225
				$this->dataArray = unserialize($content);
226
				$cacheEntry = TRUE;
227
			}
228
		}
229
		
230
		return $cacheEntry;
231
	}
232
	
233
	/**
234
	 * Calculate the hash value of the used workspace, the user id, the versions array, the filter text, the sorting attribute, the workspace selected in grid and the sorting direction.
235
	 * 
236
	 * @param array $versions 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 pid of the online record is found as "livepid" the pid of the offline record is found in "wspid"
237
	 * @param string $filterTxt The given filter text from the grid.
238
	 */
239
	protected function calculateHash (array $versions, $filterTxt) {
240
		$hashArray = array(
241
			$GLOBALS['BE_USER']->workspace,
242
			$GLOBALS['BE_USER']->user['uid'],
243
			$versions,
244
			$filterTxt,
245
			$this->sort,
246
			$this->sortDir,
247
			$this->currentWorkspace);
248
		$hash = md5(serialize($hashArray));
249
		
250
		return $hash;
251
	}
252

  
253

  
254
	/**
157 255
	 * Performs sorting on the data array accordant to the
158 256
	 * selected column in the grid view to be used for sorting.
159 257
	 *
Classes/Service/Tcemain.php (working copy)
56 56
	}
57 57

  
58 58
	/**
59
	 * hook that is called AFTER all commands of the commandmap was 
60
	 * executed
61
	 *
62
	 * @param t3lib_TCEmain $tcemainObj reference to the main tcemain object
63
	 * @return	void
64
	 */
65
	public function processCmdmap_afterFinish(t3lib_TCEmain $tcemainObj) {
66
		if (TYPO3_UseCachingFramework) {
67
			$this->flushWorkspaceCacheEntriesByWorkspaceId($tcemainObj->BE_USER->workspace);
68
		}
69
	}
70

  
71
	/**
59 72
	 * In case a sys_workspace_stage record is deleted we do a hard reset
60 73
	 * for all existing records in that stage to avoid that any of these end up
61 74
	 * as orphan records.
......
129 142
		return $tceMain;
130 143
	}
131 144

  
145
	/**
146
	 * Flushes the workspace cache for current workspace and for the virtual "all workspaces" too.
147
	 * 
148
	 * @param integer $workspaceId The workspace to be flushed in cache
149
	 * @return void
150
	 */
151
	protected function flushWorkspaceCacheEntriesByWorkspaceId($workspaceId) {
152
		if (TYPO3_UseCachingFramework) {
153
			try {
154
				$GLOBALS['typo3CacheFactory']->create(
155
					'workspaces_cache',
156
					't3lib_cache_frontend_StringFrontend',
157
					$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['sys_workspace_cache']['backend'],
158
					$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['sys_workspace_cache']['options']);
159
			} catch (t3lib_cache_exception_DuplicateIdentifier $e) {
160
				// do nothing, a workspace cache already exists
161
			}
162
	
163
			$workspacesCache = $GLOBALS['typo3CacheManager']->getCache('workspaces_cache');
164
	
165
			$workspacesCache->flushByTag($workspaceId);
166
			$workspacesCache->flushByTag(tx_Workspaces_Service_Workspaces::SELECT_ALL_WORKSPACES);
167
		}
168
	}
132 169
}
133 170

  
134 171

  
ext_localconf.php (working copy)
20 20
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_befunc.php']['viewOnClickClass']['workspaces'] = 'EXT:workspaces/Classes/Service/Tcemain.php:tx_Workspaces_Service_Befunc';
21 21
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['hook_eofe']['workspaces'] = 'EXT:workspaces/Classes/Service/Fehooks.php:tx_Workspaces_Service_Fehooks->hook_eofe';
22 22

  
23
if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['useCachingFramework']) {
24
		// Initialize the caching framework. The caching framework is asking it self if it is initialized already before initializing.
25
	t3lib_cache::initializeCachingFramework();
26
	
27
	$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['sys_workspace_cache']['backend'] = 't3lib_cache_backend_DbBackend';
28
	$GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']['sys_workspace_cache']['options'] = array(
29
		'cacheTable' => 'sys_workspace_cache',
30
		'tagsTable' => 'sys_workspace_cache_tags'
31
	);
32
}
33

  
23 34
?>
ext_tables.sql (working copy)
48 48
	PRIMARY KEY (uid),
49 49
	KEY parent (pid)
50 50
);
51

  
52

  
53
#
54
# Table structure for table 'sys_workspace_cache'
55
#
56
CREATE TABLE sys_workspace_cache (
57
    id int(11) unsigned NOT NULL auto_increment,
58
    identifier varchar(32) DEFAULT '' NOT NULL,
59
    content mediumblob NOT NULL,
60
    crdate int(11) DEFAULT '0' NOT NULL,
61
    lifetime int(11) DEFAULT '0' NOT NULL,    
62
      PRIMARY KEY (id),
63
      KEY cache_id (identifier)
64
) ENGINE=InnoDB;
65

  
66

  
67
#
68
# Table structure for table 'sys_workspace_cache_tags'
69
#
70
CREATE TABLE sys_workspace_cache_tags (
71
  id int(11) unsigned NOT NULL auto_increment,
72
  identifier varchar(128) DEFAULT '' NOT NULL,
73
  tag varchar(128) DEFAULT '' NOT NULL,
74
  PRIMARY KEY (id),
75
  KEY cache_id (identifier),
76
  KEY cache_tag (tag)
77
) ENGINE=InnoDB;
(3-3/4)