CoreCommunity ExtensionsIncubatorDistributionsTYPO3 4.5 ProjectsTYPO3 4.6 ProjectsTYPO3 4.7 ProjectsTYPO3 6.0 ProjectsTYPO3 6.1 ProjectsTYPO3 6.2 Projects (+)

recordmonitorwhereclause.patch

Jigal van Hemert, 2012-07-17 18:11

Download (8 kB)

 
classes/indexqueue/class.tx_solr_indexqueue_recordmonitor.php (working copy)
35 35
 */
36 36
class tx_solr_indexqueue_RecordMonitor {
37 37

  
38
	/** @var array Solr TypoScript configuration */
39
	protected $solrConfiguration;
40

  
41
	/** @var tx_solr_indexqueue_Queue */
42
	protected $indexQueue;
43

  
38 44
	/**
39 45
	 * Hooks into TCE main and tracks record deletion commands.
40 46
	 *
......
46 52
	 */
47 53
	public function processCmdmap_preProcess($command, $table, $uid, $value, t3lib_TCEmain $tceMain) {
48 54
		if ($command == 'delete' && $table == 'tt_content') {
49
			$indexQueue = t3lib_div::makeInstance('tx_solr_indexqueue_Queue');
50
			$indexQueue->updateItem('pages', $tceMain->getPID($table, $uid));
55
			$this->indexQueue = t3lib_div::makeInstance('tx_solr_indexqueue_Queue');
56
			$this->indexQueue->updateItem('pages', $tceMain->getPID($table, $uid));
51 57
		}
52 58
	}
53 59

  
......
62 68
	 */
63 69
	public function processCmdmap_postProcess($command, $table, $uid, $value, t3lib_TCEmain $tceMain) {
64 70
		if ($command == 'move' && $table == 'pages') {
65
			$indexQueue = t3lib_div::makeInstance('tx_solr_indexqueue_Queue');
66
			$indexQueue->updateItem('pages', $uid);
71
			$this->indexQueue = t3lib_div::makeInstance('tx_solr_indexqueue_Queue');
72
			$this->indexQueue->updateItem('pages', $uid);
67 73
		}
68 74
	}
69 75

  
......
94 100
			$recordPageId = $fields['pid'];
95 101
		}
96 102

  
97
		$indexQueue      = t3lib_div::makeInstance('tx_solr_indexqueue_Queue');
103
		$this->indexQueue = t3lib_div::makeInstance('tx_solr_indexqueue_Queue');
104
		$this->solrConfiguration = tx_solr_Util::getSolrConfigurationFromPageId($recordPageId);
98 105
		$monitoredTables = $this->getMonitoredTables($recordPageId);
99 106

  
100
		if (in_array($recordTable, $monitoredTables)) {
101
				// FIXME must respect the indexer's additionalWhereClause option: must not add items to the index queue which are excluded through additionalWhereClause
107
		if (in_array($recordTable, $monitoredTables, TRUE)) {
108
			$record = $this->getRecord($recordTable, $recordUid);
109
			if (is_array($record) && count($record) > 0) {
110
					// only update/insert the item if we actually found a record
111
				if ($this->isLocalizedRecord($recordTable, $record)) {
112
						// if it's a localization overlay, update the original record instead
113
					$recordUid = $record[$GLOBALS['TCA'][$recordTable]['ctrl']['transOrigPointerField']];
102 114

  
103
			$record = t3lib_BEfunc::getRecord($recordTable, $recordUid);
104
			if ($this->isLocalizedRecord($recordTable, $record)) {
105
					// if it's a localization overlay, update the original record instead
106
				$recordUid = $record[$GLOBALS['TCA'][$recordTable]['ctrl']['transOrigPointerField']];
107

  
108
				if ($recordTable == 'pages_language_overlay') {
109
					$recordTable = 'pages';
115
					if ($recordTable == 'pages_language_overlay') {
116
						$recordTable = 'pages';
117
					}
110 118
				}
111
			}
112 119

  
113
			$indexQueue->updateItem($recordTable, $recordUid);
120
				$this->indexQueue->updateItem($recordTable, $recordUid);
114 121

  
115
			if ($recordTable == 'pages') {
116
				$this->updateMountPages($recordUid);
122
				if ($recordTable == 'pages') {
123
					$this->updateMountPages($recordUid);
124
				}
125
			} else {
126
					// check if the item should be removed from the index because it no longer matches the conditions
127
				if ($this->indexQueue->containsItem($recordTable, $recordUid)) {
128
					$this->removeFromIndexAndQueue($recordTable, $recordUid);
129
				}
117 130
			}
118 131
		}
119 132

  
120 133
			// when a content element changes we need to updated the page instead
121 134
		if ($recordTable == 'tt_content' && in_array('pages', $monitoredTables)) {
122
			$indexQueue->updateItem('pages', $recordPageId);
135
			$this->indexQueue->updateItem('pages', $recordPageId);
123 136
		}
124 137

  
125 138
			// TODO need to check for creation of "pages_language_overlay" records to trigger "pages" updates
126 139
	}
127 140

  
128 141
	/**
142
	 * Removes record from the index queue and from the solr index
143
	 *
144
	 * @param string $recordTable Name of table where the record lives
145
	 * @param int $recordUid Id of record
146
	 */
147
	protected function removeFromIndexAndQueue($recordTable, $recordUid) {
148
		/** @var $connectionManager tx_solr_ConnectionManager */
149
		$connectionManager = t3lib_div::makeInstance('tx_solr_ConnectionManager');
150

  
151
			// record can be indexed for multiple sites
152
		$indexQueueItems = $this->indexQueue->getItems($recordTable, $recordUid);
153

  
154
		foreach ($indexQueueItems as $indexQueueItem) {
155
			$site = $indexQueueItem->getSite();
156

  
157
				// a site can have multiple connections (cores / languages)
158
			$solrConnections = $connectionManager->getConnectionsBySite($site);
159
			foreach ($solrConnections as $solr) {
160
				$solr->deleteByQuery('type:' . $recordTable . ' AND uid:' . intval($recordUid));
161
				$solr->commit(FALSE, FALSE, FALSE);
162
			}
163
		}
164
			// also remove it from the indexing queue to prevent future indexation
165
		$this->indexQueue->deleteItem($recordTable, $recordUid);
166
	}
167

  
168
	/**
169
	 * Retrieves record taking the additionalWhereClauses of the indexing queue configurations into account
170
	 *
171
	 * @param string $recordTable Table to read from
172
	 * @param int $recordUid Id of the record
173
	 * @return array Record if found, otherwise empty array
174
	 */
175
	protected function getRecord($recordTable, $recordUid) {
176
		$record = array();
177
			// go through all indexing configurations
178
		foreach ($this->solrConfiguration['index.']['queue.'] as $indexQueueName => $indexQueueConfiguration) {
179
			if (substr($indexQueueName, -1) === '.') {
180
					// determine the table name (can be different from the indexing queue name
181
				$tableToIndex = isset($indexQueueConfiguration['table'])
182
						? $indexQueueConfiguration['table']
183
						: substr($indexQueueName, 0, -1);
184
				if ($tableToIndex === $recordTable) {
185
					$recordWhere = $this->buildUserWhereClause($indexQueueConfiguration);
186
					$record = t3lib_BEfunc::getRecord($recordTable, $recordUid, '*', $recordWhere);
187
					if (is_array($record) && count($record) > 0) {
188
						// if we found a record which matches the conditions, we can continue
189
						break;
190
					}
191
				}
192
			}
193
		}
194
		return $record;
195
	}
196

  
197
	/**
198
	 * Build additional where clause from index queue configuration
199
	 *
200
	 * @param array $indexQueueConfiguration Configuration of index queue
201
	 * @return string Optional extra where clause
202
	 */
203
	protected function buildUserWhereClause(array $indexQueueConfiguration){
204
		$condition = '';
205
			// FIXME replace this with the mechanism described in tx_solr_indexqueue_initializer_Abstract::buildUserWhereClause()
206
		if (isset($indexQueueConfiguration['additionalWhereClause'])) {
207
			$condition = ' AND ' . $indexQueueConfiguration['additionalWhereClause'];
208
		}
209
		return $condition;
210
	}
211

  
212
	/**
129 213
	 * Gets an array of tables configured for indexing by the Index Queue. The
130 214
	 * record monitor must watch these tables for manipulation.
131 215
	 *
......
137 221

  
138 222
			// FIXME!! $pageId might be outside of a site root and thus might not know about solr configuration
139 223
			// -> leads to record not being queued for reindexing
140
		$solrConfiguration = tx_solr_Util::getSolrConfigurationFromPageId($pageId);
141 224
		$indexingConfigurations = t3lib_div::makeInstance('tx_solr_indexqueue_Queue')
142
			->getTableIndexingConfigurations($solrConfiguration);
225
			->getTableIndexingConfigurations($this->solrConfiguration);
143 226

  
144 227
		foreach ($indexingConfigurations as $indexingConfigurationName) {
145 228
			$monitoredTable = $indexingConfigurationName;
146 229

  
147
			if (!empty($solrConfiguration['index.']['queue.'][$indexingConfigurationName . '.']['table'])) {
230
			if (!empty($this->solrConfiguration['index.']['queue.'][$indexingConfigurationName . '.']['table'])) {
148 231
					// table has been set explicitly. Allows to index the same table with different configurations
149
				$monitoredTable = $solrConfiguration['index.']['queue.'][$indexingConfigurationName . '.']['table'];
232
				$monitoredTable = $this->solrConfiguration['index.']['queue.'][$indexingConfigurationName . '.']['table'];
150 233
			}
151 234

  
152 235
			$monitoredTables[] = $monitoredTable;