[FEATURE] Make use of File Abstraction Layer in Backend
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_refindex.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (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
29 /**
30 * Reference index processing and relation extraction
31 *
32 * NOTICE: When the reference index is updated for an offline version the results may not be correct.
33 * First, lets assumed that the reference update happens in LIVE workspace (ALWAYS update from Live workspace if you analyse whole database!)
34 * Secondly, lets assume that in a Draft workspace you have changed the data structure of a parent page record - this is (in TemplaVoila) inherited by subpages.
35 * When in the LIVE workspace the data structure for the records/pages in the offline workspace will not be evaluated to the right one simply because the data structure is taken from a rootline traversal and in the Live workspace that will NOT include the changed DataSTructure! Thus the evaluation will be based on the Data Structure set in the Live workspace!
36 * Somehow this scenario is rarely going to happen. Yet, it is an inconsistency and I see now practical way to handle it - other than simply ignoring maintaining the index for workspace records. Or we can say that the index is precise for all Live elements while glitches might happen in an offline workspace?
37 * Anyway, I just wanted to document this finding - I don't think we can find a solution for it. And its very TemplaVoila specific.
38 *
39 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
40 * @package TYPO3
41 * @subpackage t3lib
42 */
43 class t3lib_refindex {
44
45 var $temp_flexRelations = array();
46 var $errorLog = array();
47 var $WSOL = FALSE;
48 var $relations = array();
49
50 var $words_strings = array();
51 var $words = array();
52
53 var $hashVersion = 1; // Number which we can increase if a change in the code means we will have to force a re-generation of the index.
54
55
56 /**
57 * Call this function to update the sys_refindex table for a record (even one just deleted)
58 * NOTICE: Currently, references updated for a deleted-flagged record will not include those from within flexform fields in some cases where the data structure is defined by another record since the resolving process ignores deleted records! This will also result in bad cleaning up in tcemain I think... Anyway, thats the story of flexforms; as long as the DS can change, lots of references can get lost in no time.
59 *
60 * @param string Table name
61 * @param integer UID of record
62 * @param boolean If set, nothing will be written to the index but the result value will still report statistics on what is added, deleted and kept. Can be used for mere analysis.
63 * @return array Array with statistics about how many index records were added, deleted and not altered plus the complete reference set for the record.
64 */
65 function updateRefIndexTable($table, $uid, $testOnly = FALSE) {
66
67 // First, secure that the index table is not updated with workspace tainted relations:
68 $this->WSOL = FALSE;
69
70 // Init:
71 $result = array(
72 'keptNodes' => 0,
73 'deletedNodes' => 0,
74 'addedNodes' => 0
75 );
76
77 // Get current index from Database:
78 $currentRels = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
79 '*',
80 'sys_refindex',
81 'tablename=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($table, 'sys_refindex') .
82 ' AND recuid=' . intval($uid),
83 '', '', '', 'hash'
84 );
85
86 // First, test to see if the record exists (including deleted-flagged)
87 if (t3lib_BEfunc::getRecordRaw($table, 'uid=' . intval($uid), 'uid')) {
88
89 // Then, get relations:
90 $relations = $this->generateRefIndexData($table, $uid);
91
92 if (is_array($relations)) {
93
94 // Traverse the generated index:
95 foreach ($relations as $k => $datRec) {
96 $relations[$k]['hash'] = md5(implode('///', $relations[$k]) . '///' . $this->hashVersion);
97
98 // First, check if already indexed and if so, unset that row (so in the end we know which rows to remove!)
99 if (isset($currentRels[$relations[$k]['hash']])) {
100 unset($currentRels[$relations[$k]['hash']]);
101 $result['keptNodes']++;
102 $relations[$k]['_ACTION'] = 'KEPT';
103 } else {
104 // If new, add it:
105 if (!$testOnly) {
106 $GLOBALS['TYPO3_DB']->exec_INSERTquery('sys_refindex', $relations[$k]);
107 }
108 $result['addedNodes']++;
109 $relations[$k]['_ACTION'] = 'ADDED';
110 }
111 }
112
113 $result['relations'] = $relations;
114 } else {
115 return FALSE;
116 } // Weird mistake I would say...
117 }
118
119 // If any old are left, remove them:
120 if (count($currentRels)) {
121 $hashList = array_keys($currentRels);
122 if (count($hashList)) {
123 $result['deletedNodes'] = count($hashList);
124 $result['deletedNodes_hashList'] = implode(',', $hashList);
125 if (!$testOnly) {
126 $GLOBALS['TYPO3_DB']->exec_DELETEquery('sys_refindex', 'hash IN (' . implode(',', $GLOBALS['TYPO3_DB']->fullQuoteArray($hashList, 'sys_refindex')) . ')');
127 }
128 }
129 }
130
131 return $result;
132 }
133
134 /**
135 * Returns array of arrays with an index of all references found in record from table/uid
136 * If the result is used to update the sys_refindex table then ->WSOL must NOT be TRUE (no workspace overlay anywhere!)
137 *
138 * @param string Table name from $GLOBALS['TCA']
139 * @param integer Record UID
140 * @return array Index Rows
141 */
142 function generateRefIndexData($table, $uid) {
143 if (isset($GLOBALS['TCA'][$table])) {
144 // Get raw record from DB:
145 $record = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', $table, 'uid=' . intval($uid));
146
147 if (is_array($record)) {
148
149 // Initialize:
150 $this->words_strings = array();
151 $this->words = array();
152
153 // Deleted:
154 $deleted = (($GLOBALS['TCA'][$table]['ctrl']['delete'] && $record[$GLOBALS['TCA'][$table]['ctrl']['delete']]) ? 1 : 0);
155
156 // Get all relations from record:
157 $dbrels = $this->getRelations($table, $record);
158
159 // Traverse those relations, compile records to insert in table:
160 $this->relations = array();
161 foreach ($dbrels as $fieldname => $dat) {
162
163 // Based on type,
164 switch ((string) $dat['type']) {
165 case 'db':
166 $this->createEntryData_dbRels($table, $uid, $fieldname, '', $deleted, $dat['itemArray']);
167 break;
168 case 'file_reference':
169 case 'file':
170 $this->createEntryData_fileRels($table, $uid, $fieldname, '', $deleted, $dat['newValueFiles']);
171 break;
172 case 'flex':
173 // DB references:
174 if (is_array($dat['flexFormRels']['db'])) {
175 foreach ($dat['flexFormRels']['db'] as $flexpointer => $subList) {
176 $this->createEntryData_dbRels($table, $uid, $fieldname, $flexpointer, $deleted, $subList);
177 }
178 }
179 // File references (NOT TESTED!)
180 if (is_array($dat['flexFormRels']['file'])) { // Not tested
181 foreach ($dat['flexFormRels']['file'] as $flexpointer => $subList) {
182 $this->createEntryData_fileRels($table, $uid, $fieldname, $flexpointer, $deleted, $subList);
183 }
184 }
185 // Soft references in flexforms (NOT TESTED!)
186 if (is_array($dat['flexFormRels']['softrefs'])) {
187 foreach ($dat['flexFormRels']['softrefs'] as $flexpointer => $subList) {
188 $this->createEntryData_softreferences($table, $uid, $fieldname, $flexpointer, $deleted, $subList['keys']);
189 }
190 }
191 break;
192 }
193
194 // Softreferences in the field:
195 if (is_array($dat['softrefs'])) {
196 $this->createEntryData_softreferences($table, $uid, $fieldname, '', $deleted, $dat['softrefs']['keys']);
197 }
198 }
199
200 // Word indexing:
201 t3lib_div::loadTCA($table);
202 foreach ($GLOBALS['TCA'][$table]['columns'] as $field => $conf) {
203 if (t3lib_div::inList('input,text', $conf['config']['type']) && strcmp($record[$field], '') && !t3lib_utility_Math::canBeInterpretedAsInteger($record[$field])) {
204 $this->words_strings[$field] = $record[$field];
205 }
206 }
207
208 return $this->relations;
209 }
210 }
211 }
212
213 /**
214 * Create array with field/value pairs ready to insert in database.
215 * The "hash" field is a fingerprint value across this table.
216 *
217 * @param string Tablename of source record (where reference is located)
218 * @param integer UID of source record (where reference is located)
219 * @param string Fieldname of source record (where reference is located)
220 * @param string Pointer to location inside flexform structure where reference is located in [field]
221 * @param integer Whether record is deleted-flagged or not
222 * @param string For database references; the tablename the reference points to. Special keyword "_FILE" indicates that "ref_string" is a file reference either absolute or relative to PATH_site. Special keyword "_STRING" indicates some special usage (typ. softreference) where "ref_string" is used for the value.
223 * @param integer For database references; The UID of the record (zero "ref_table" is "_FILE" or "_STRING")
224 * @param string For "_FILE" or "_STRING" references: The filepath (relative to PATH_site or absolute) or other string.
225 * @param integer The sorting order of references if many (the "group" or "select" TCA types). -1 if no sorting order is specified.
226 * @param string If the reference is a soft reference, this is the soft reference parser key. Otherwise empty.
227 * @param string Soft reference ID for key. Might be useful for replace operations.
228 * @return array Array record to insert into table.
229 */
230 function createEntryData($table, $uid, $field, $flexpointer, $deleted, $ref_table, $ref_uid, $ref_string = '', $sort = -1, $softref_key = '', $softref_id = '') {
231 return array(
232 'tablename' => $table,
233 'recuid' => $uid,
234 'field' => $field,
235 'flexpointer' => $flexpointer,
236 'softref_key' => $softref_key,
237 'softref_id' => $softref_id,
238 'sorting' => $sort,
239 'deleted' => $deleted,
240 'ref_table' => $ref_table,
241 'ref_uid' => $ref_uid,
242 'ref_string' => $ref_string,
243 );
244 }
245
246 /**
247 * Enter database references to ->relations array
248 *
249 * @param string [See createEntryData, arg 1]
250 * @param integer [See createEntryData, arg 2]
251 * @param string [See createEntryData, arg 3]
252 * @param string [See createEntryData, arg 4]
253 * @param string [See createEntryData, arg 5]
254 * @param array Data array with databaes relations (table/id)
255 * @return void
256 */
257 function createEntryData_dbRels($table, $uid, $fieldname, $flexpointer, $deleted, $items) {
258 foreach ($items as $sort => $i) {
259 $this->relations[] = $this->createEntryData($table, $uid, $fieldname, $flexpointer, $deleted, $i['table'], $i['id'], '', $sort);
260 }
261 }
262
263 /**
264 * Enter file references to ->relations array
265 *
266 * @param string [See createEntryData, arg 1]
267 * @param integer [See createEntryData, arg 2]
268 * @param string [See createEntryData, arg 3]
269 * @param string [See createEntryData, arg 4]
270 * @param string [See createEntryData, arg 5]
271 * @param array Data array with file relations
272 * @return void
273 */
274 function createEntryData_fileRels($table, $uid, $fieldname, $flexpointer, $deleted, $items) {
275 foreach ($items as $sort => $i) {
276 $filePath = $i['ID_absFile'];
277 if (t3lib_div::isFirstPartOfStr($filePath, PATH_site)) {
278 $filePath = substr($filePath, strlen(PATH_site));
279 }
280 $this->relations[] = $this->createEntryData($table, $uid, $fieldname, $flexpointer, $deleted, '_FILE', 0, $filePath, $sort);
281 }
282 }
283
284 /**
285 * Enter softref references to ->relations array
286 *
287 * @param string [See createEntryData, arg 1]
288 * @param integer [See createEntryData, arg 2]
289 * @param string [See createEntryData, arg 3]
290 * @param string [See createEntryData, arg 4]
291 * @param string [See createEntryData, arg 5]
292 * @param array Data array with soft reference keys
293 * @return void
294 */
295 function createEntryData_softreferences($table, $uid, $fieldname, $flexpointer, $deleted, $keys) {
296 if (is_array($keys)) {
297 foreach ($keys as $spKey => $elements) {
298 if (is_array($elements)) {
299 foreach ($elements as $subKey => $el) {
300 if (is_array($el['subst'])) {
301 switch ((string) $el['subst']['type']) {
302 case 'db':
303 list($tableName, $recordId) = explode(':', $el['subst']['recordRef']);
304 $this->relations[] = $this->createEntryData($table, $uid, $fieldname, $flexpointer, $deleted, $tableName, $recordId, '', -1, $spKey, $subKey);
305 break;
306 case 'file_reference':
307 case 'file':
308 $this->relations[] = $this->createEntryData($table, $uid, $fieldname, $flexpointer, $deleted, '_FILE', 0, $el['subst']['relFileName'], -1, $spKey, $subKey);
309 break;
310 case 'string':
311 $this->relations[] = $this->createEntryData($table, $uid, $fieldname, $flexpointer, $deleted, '_STRING', 0, $el['subst']['tokenValue'], -1, $spKey, $subKey);
312 break;
313 }
314 }
315 }
316 }
317 }
318 }
319 }
320
321
322 /*******************************
323 *
324 * Get relations from table row
325 *
326 *******************************/
327
328 /**
329 * Returns relation information for a $table/$row-array
330 * Traverses all fields in input row which are configured in TCA/columns
331 * It looks for hard relations to files and records in the TCA types "select" and "group"
332 *
333 * @param string Table name
334 * @param array Row from table
335 * @param string Specific field to fetch for.
336 * @return array Array with information about relations
337 * @see export_addRecord()
338 */
339 function getRelations($table, $row, $onlyField = '') {
340
341 // Load full table description
342 t3lib_div::loadTCA($table);
343
344 // Initialize:
345 $uid = $row['uid'];
346 $nonFields = explode(',', 'uid,perms_userid,perms_groupid,perms_user,perms_group,perms_everybody,pid');
347
348 $outRow = array();
349 foreach ($row as $field => $value) {
350 if (!in_array($field, $nonFields) && is_array($GLOBALS['TCA'][$table]['columns'][$field])
351 && (!$onlyField || $onlyField === $field)) {
352 $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
353
354 // Add files
355 if ($result = $this->getRelations_procFiles($value, $conf, $uid)) {
356 // Creates an entry for the field with all the files:
357 $outRow[$field] = array(
358 'type' => 'db',
359 'itemArray' => $result,
360 );
361 }
362
363 // Add DB:
364 if ($result = $this->getRelations_procDB($value, $conf, $uid, $table, $field)) {
365 // Create an entry for the field with all DB relations:
366 $outRow[$field] = array(
367 'type' => 'db',
368 'itemArray' => $result,
369 );
370 }
371
372 // For "flex" fieldtypes we need to traverse the structure looking for file and db references of course!
373 if ($conf['type'] == 'flex') {
374
375 // Get current value array:
376 // NOTICE: failure to resolve Data Structures can lead to integrity problems with the reference index. Please look up the note in the JavaDoc documentation for the function t3lib_BEfunc::getFlexFormDS()
377 $dataStructArray = t3lib_BEfunc::getFlexFormDS($conf, $row, $table, '', $this->WSOL);
378 $currentValueArray = t3lib_div::xml2array($value);
379
380 // Traversing the XML structure, processing files:
381 if (is_array($currentValueArray)) {
382 $this->temp_flexRelations = array(
383 'db' => array(),
384 'file' => array(),
385 'softrefs' => array()
386 );
387
388 // Create and call iterator object:
389 $flexObj = t3lib_div::makeInstance('t3lib_flexformtools');
390 $flexObj->traverseFlexFormXMLData($table, $field, $row, $this, 'getRelations_flexFormCallBack');
391
392 // Create an entry for the field:
393 $outRow[$field] = array(
394 'type' => 'flex',
395 'flexFormRels' => $this->temp_flexRelations,
396 );
397 }
398 }
399
400 // Soft References:
401 if (strlen($value) && $softRefs = t3lib_BEfunc::explodeSoftRefParserList($conf['softref'])) {
402 $softRefValue = $value;
403 foreach ($softRefs as $spKey => $spParams) {
404 $softRefObj = t3lib_BEfunc::softRefParserObj($spKey);
405 if (is_object($softRefObj)) {
406 $resultArray = $softRefObj->findRef($table, $field, $uid, $softRefValue, $spKey, $spParams);
407 if (is_array($resultArray)) {
408 $outRow[$field]['softrefs']['keys'][$spKey] = $resultArray['elements'];
409 if (strlen($resultArray['content'])) {
410 $softRefValue = $resultArray['content'];
411 }
412 }
413 }
414 }
415
416 if (is_array($outRow[$field]['softrefs']) && count($outRow[$field]['softrefs']) && strcmp($value, $softRefValue) && strstr($softRefValue, '{softref:')) {
417 $outRow[$field]['softrefs']['tokenizedContent'] = $softRefValue;
418 }
419 }
420 }
421 }
422
423 return $outRow;
424 }
425
426 /**
427 * Callback function for traversing the FlexForm structure in relation to finding file and DB references!
428 *
429 * @param array Data structure for the current value
430 * @param mixed Current value
431 * @param array Additional configuration used in calling function
432 * @param string Path of value in DS structure
433 * @param object Object reference to caller
434 * @return void
435 * @see t3lib_TCEmain::checkValue_flex_procInData_travDS()
436 */
437 function getRelations_flexFormCallBack($dsArr, $dataValue, $PA, $structurePath, $pObj) {
438 $structurePath = substr($structurePath, 5) . '/'; // removing "data/" in the beginning of path (which points to location in data array)
439
440 $dsConf = $dsArr['TCEforms']['config'];
441
442 // Implode parameter values:
443 list($table, $uid, $field) = array($PA['table'], $PA['uid'], $PA['field']);
444
445 // Add files
446 if ($result = $this->getRelations_procFiles($dataValue, $dsConf, $uid)) {
447
448 // Creates an entry for the field with all the files:
449 $this->temp_flexRelations['file'][$structurePath] = $result;
450 }
451
452 // Add DB:
453 if ($result = $this->getRelations_procDB($dataValue, $dsConf, $uid, $field)) {
454
455 // Create an entry for the field with all DB relations:
456 $this->temp_flexRelations['db'][$structurePath] = $result;
457 }
458
459 // Soft References:
460 if (strlen($dataValue) && $softRefs = t3lib_BEfunc::explodeSoftRefParserList($dsConf['softref'])) {
461 $softRefValue = $dataValue;
462 foreach ($softRefs as $spKey => $spParams) {
463 $softRefObj = t3lib_BEfunc::softRefParserObj($spKey);
464 if (is_object($softRefObj)) {
465 $resultArray = $softRefObj->findRef($table, $field, $uid, $softRefValue, $spKey, $spParams, $structurePath);
466 if (is_array($resultArray) && is_array($resultArray['elements'])) {
467 $this->temp_flexRelations['softrefs'][$structurePath]['keys'][$spKey] = $resultArray['elements'];
468 if (strlen($resultArray['content'])) {
469 $softRefValue = $resultArray['content'];
470 }
471 }
472 }
473 }
474
475 if (count($this->temp_flexRelations['softrefs']) && strcmp($dataValue, $softRefValue)) {
476 $this->temp_flexRelations['softrefs'][$structurePath]['tokenizedContent'] = $softRefValue;
477 }
478 }
479 }
480
481 /**
482 * Check field configuration if it is a file relation field and extract file relations if any
483 *
484 * @param string Field value
485 * @param array Field configuration array of type "TCA/columns"
486 * @param integer Field uid
487 * @return array If field type is OK it will return an array with the files inside. Else FALSE
488 */
489 function getRelations_procFiles($value, $conf, $uid) {
490 if ($conf['type'] == 'group' && ($conf['internal_type'] == 'file' || $conf['internal_type'] == 'file_reference')) {
491
492 // Collect file values in array:
493 if ($conf['MM']) {
494 $theFileValues = array();
495 $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
496 $dbAnalysis->start('', 'files', $conf['MM'], $uid);
497
498 foreach ($dbAnalysis->itemArray as $somekey => $someval) {
499 if ($someval['id']) {
500 $theFileValues[] = $someval['id'];
501 }
502 }
503 } else {
504 $theFileValues = explode(',', $value);
505 }
506
507 // Traverse the files and add them:
508 $uploadFolder = $conf['internal_type'] == 'file' ? $conf['uploadfolder'] : '';
509 $dest = $this->destPathFromUploadFolder($uploadFolder);
510 $newValueFiles = array();
511
512 foreach ($theFileValues as $file) {
513 if (trim($file)) {
514 $realFile = $dest . '/' . trim($file);
515 $newValueFiles[] = array(
516 'filename' => basename($file),
517 'ID' => md5($realFile),
518 'ID_absFile' => $realFile
519 );
520 }
521 }
522
523 return $newValueFiles;
524 }
525 return FALSE;
526 }
527
528 /**
529 * Check field configuration if it is a DB relation field and extract DB relations if any
530 *
531 * @param string $value Field value
532 * @param array $conf Field configuration array of type "TCA/columns"
533 * @param integer $uid Field uid
534 * @param string $table Table name
535 * @param string $field Field name
536 * @return array If field type is OK it will return an array with the database relations. Else FALSE
537 */
538 function getRelations_procDB($value, $conf, $uid, $table = '', $field = '') {
539
540 // DB record lists:
541 if ($this->isReferenceField($conf)) {
542 $allowedTables = $conf['type'] == 'group' ? $conf['allowed'] : $conf['foreign_table'] . ',' . $conf['neg_foreign_table'];
543
544 if ($conf['MM_opposite_field']) {
545 return array();
546 }
547
548 $dbAnalysis = t3lib_div::makeInstance('t3lib_loadDBGroup');
549 $dbAnalysis->start($value, $allowedTables, $conf['MM'], $uid, $table, $conf);
550
551 return $dbAnalysis->itemArray;
552 } elseif ($conf['type'] == 'inline' && $conf['foreign_table'] == 'sys_file_reference') {
553 $files = (array)$GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
554 'uid_local',
555 'sys_file_reference',
556 'tablenames=\'' . $table . '\' AND fieldname=\'' . $field . '\' AND uid_foreign=' . $uid
557 );
558
559 $fileArray = array('0' => array());
560 foreach ($files as $fileUid) {
561 $fileArray[0][] = array('table' => 'sys_file', 'id' => $fileUid);
562 }
563
564 return $fileArray;
565 } elseif ($conf['type'] == 'input' && isset($conf['wizards']['link']) && trim($value)) {
566 try {
567 $file = t3lib_file_Factory::getInstance()->retrieveFileOrFolderObject($value);
568 } catch (Exception $e) {}
569 if ($file instanceof t3lib_file_FileInterface) {
570 return array(
571 0 => array(
572 'table' => 'sys_file',
573 'id' => $file->getUid()
574 )
575 );
576 }
577 }
578 }
579
580
581 /*******************************
582 *
583 * Setting values
584 *
585 *******************************/
586
587 /**
588 * Setting the value of a reference or removing it completely.
589 * Usage: For lowlevel clean up operations!
590 * WARNING: With this you can set values that are not allowed in the database since it will bypass all checks for validity! Hence it is targetted at clean-up operations. Please use TCEmain in the usual ways if you wish to manipulate references.
591 * Since this interface allows updates to soft reference values (which TCEmain does not directly) you may like to use it for that as an exception to the warning above.
592 * Notice; If you want to remove multiple references from the same field, you MUST start with the one having the highest sorting number. If you don't the removal of a reference with a lower number will recreate an index in which the remaining references in that field has new hash-keys due to new sorting numbers - and you will get errors for the remaining operations which cannot find the hash you feed it!
593 * To ensure proper working only admin-BE_USERS in live workspace should use this function
594 *
595 * @param string 32-byte hash string identifying the record from sys_refindex which you wish to change the value for
596 * @param mixed Value you wish to set for reference. If NULL, the reference is removed (unless a soft-reference in which case it can only be set to a blank string). If you wish to set a database reference, use the format "[table]:[uid]". Any other case, the input value is set as-is
597 * @param boolean Return $dataArray only, do not submit it to database.
598 * @param boolean If set, it will bypass check for workspace-zero and admin user
599 * @return string If a return string, that carries an error message, otherwise FALSE (=OK) (except if $returnDataArray is set!)
600 */
601 function setReferenceValue($hash, $newValue, $returnDataArray = FALSE, $bypassWorkspaceAdminCheck = FALSE) {
602
603 if (($GLOBALS['BE_USER']->workspace === 0 && $GLOBALS['BE_USER']->isAdmin()) || $bypassWorkspaceAdminCheck) {
604
605 // Get current index from Database:
606 $refRec = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
607 '*',
608 'sys_refindex',
609 'hash=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($hash, 'sys_refindex')
610 );
611
612 // Check if reference existed.
613 if (is_array($refRec)) {
614 if ($GLOBALS['TCA'][$refRec['tablename']]) {
615
616 // Get that record from database:
617 $record = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow('*', $refRec['tablename'], 'uid=' . intval($refRec['recuid']));
618
619 if (is_array($record)) {
620
621 // Get all relations from record, filter with fieldname:
622 $dbrels = $this->getRelations($refRec['tablename'], $record, $refRec['field']);
623 if ($dat = $dbrels[$refRec['field']]) {
624
625 // Initialize data array that is to be sent to TCEmain afterwards:
626 $dataArray = array();
627
628 // Based on type,
629 switch ((string) $dat['type']) {
630 case 'db':
631 $error = $this->setReferenceValue_dbRels($refRec, $dat['itemArray'], $newValue, $dataArray);
632 if ($error) {
633 return $error;
634 }
635 break;
636 case 'file_reference':
637 case 'file':
638 $error = $this->setReferenceValue_fileRels($refRec, $dat['newValueFiles'], $newValue, $dataArray);
639 if ($error) {
640 return $error;
641 }
642 break;
643 case 'flex':
644 // DB references:
645 if (is_array($dat['flexFormRels']['db'][$refRec['flexpointer']])) {
646 $error = $this->setReferenceValue_dbRels($refRec, $dat['flexFormRels']['db'][$refRec['flexpointer']], $newValue, $dataArray, $refRec['flexpointer']);
647 if ($error) {
648 return $error;
649 }
650 }
651 // File references
652 if (is_array($dat['flexFormRels']['file'][$refRec['flexpointer']])) {
653 $this->setReferenceValue_fileRels($refRec, $dat['flexFormRels']['file'][$refRec['flexpointer']], $newValue, $dataArray, $refRec['flexpointer']);
654 if ($error) {
655 return $error;
656 }
657 }
658 // Soft references in flexforms
659 if ($refRec['softref_key'] && is_array($dat['flexFormRels']['softrefs'][$refRec['flexpointer']]['keys'][$refRec['softref_key']])) {
660 $error = $this->setReferenceValue_softreferences($refRec, $dat['flexFormRels']['softrefs'][$refRec['flexpointer']], $newValue, $dataArray, $refRec['flexpointer']);
661 if ($error) {
662 return $error;
663 }
664 }
665 break;
666 }
667
668 // Softreferences in the field:
669 if ($refRec['softref_key'] && is_array($dat['softrefs']['keys'][$refRec['softref_key']])) {
670 $error = $this->setReferenceValue_softreferences($refRec, $dat['softrefs'], $newValue, $dataArray);
671 if ($error) {
672 return $error;
673 }
674
675 }
676
677 // Data Array, now ready to sent to TCEmain
678 if ($returnDataArray) {
679 return $dataArray;
680 } else {
681
682 // Execute CMD array:
683 $tce = t3lib_div::makeInstance('t3lib_TCEmain');
684 $tce->stripslashes_values = FALSE;
685 $tce->dontProcessTransformations = TRUE;
686 $tce->bypassWorkspaceRestrictions = TRUE;
687 $tce->bypassFileHandling = TRUE;
688 $tce->bypassAccessCheckForRecords = TRUE; // Otherwise this cannot update things in deleted records...
689
690 $tce->start($dataArray, array()); // check has been done previously that there is a backend user which is Admin and also in live workspace
691 $tce->process_datamap();
692
693 // Return errors if any:
694 if (count($tce->errorLog)) {
695 return LF . 'TCEmain:' . implode(LF . 'TCEmain:', $tce->errorLog);
696 }
697 }
698 }
699 }
700 } else {
701 return 'ERROR: Tablename "' . $refRec['tablename'] . '" was not in TCA!';
702 }
703 } else {
704 return 'ERROR: No reference record with hash="' . $hash . '" was found!';
705 }
706 } else {
707 return 'ERROR: BE_USER object is not admin OR not in workspace 0 (Live)';
708 }
709 }
710
711 /**
712 * Setting a value for a reference for a DB field:
713 *
714 * @param array sys_refindex record
715 * @param array Array of references from that field
716 * @param string Value to substitute current value with (or NULL to unset it)
717 * @param array data array in which the new value is set (passed by reference)
718 * @param string Flexform pointer, if in a flex form field.
719 * @return string Error message if any, otherwise FALSE = OK
720 */
721 function setReferenceValue_dbRels($refRec, $itemArray, $newValue, &$dataArray, $flexpointer = '') {
722 if (!strcmp($itemArray[$refRec['sorting']]['id'], $refRec['ref_uid']) && !strcmp($itemArray[$refRec['sorting']]['table'], $refRec['ref_table'])) {
723
724 // Setting or removing value:
725 if ($newValue === NULL) { // Remove value:
726 unset($itemArray[$refRec['sorting']]);
727 } else {
728 list($itemArray[$refRec['sorting']]['table'], $itemArray[$refRec['sorting']]['id']) = explode(':', $newValue);
729 }
730
731 // Traverse and compile new list of records:
732 $saveValue = array();
733 foreach ($itemArray as $pair) {
734 $saveValue[] = $pair['table'] . '_' . $pair['id'];
735 }
736
737 // Set in data array:
738 if ($flexpointer) {
739 $flexToolObj = t3lib_div::makeInstance('t3lib_flexformtools');
740 $dataArray[$refRec['tablename']][$refRec['recuid']][$refRec['field']]['data'] = array();
741 $flexToolObj->setArrayValueByPath(substr($flexpointer, 0, -1), $dataArray[$refRec['tablename']][$refRec['recuid']][$refRec['field']]['data'], implode(',', $saveValue));
742 } else {
743 $dataArray[$refRec['tablename']][$refRec['recuid']][$refRec['field']] = implode(',', $saveValue);
744 }
745
746 } else {
747 return 'ERROR: table:id pair "' . $refRec['ref_table'] . ':' . $refRec['ref_uid'] . '" did not match that of the record ("' . $itemArray[$refRec['sorting']]['table'] . ':' . $itemArray[$refRec['sorting']]['id'] . '") in sorting index "' . $refRec['sorting'] . '"';
748 }
749 }
750
751 /**
752 * Setting a value for a reference for a FILE field:
753 *
754 * @param array sys_refindex record
755 * @param array Array of references from that field
756 * @param string Value to substitute current value with (or NULL to unset it)
757 * @param array data array in which the new value is set (passed by reference)
758 * @param string Flexform pointer, if in a flex form field.
759 * @return string Error message if any, otherwise FALSE = OK
760 */
761 function setReferenceValue_fileRels($refRec, $itemArray, $newValue, &$dataArray, $flexpointer = '') {
762 if (!strcmp(substr($itemArray[$refRec['sorting']]['ID_absFile'], strlen(PATH_site)), $refRec['ref_string']) && !strcmp('_FILE', $refRec['ref_table'])) {
763
764 // Setting or removing value:
765 if ($newValue === NULL) { // Remove value:
766 unset($itemArray[$refRec['sorting']]);
767 } else {
768 $itemArray[$refRec['sorting']]['filename'] = $newValue;
769 }
770
771 // Traverse and compile new list of records:
772 $saveValue = array();
773 foreach ($itemArray as $fileInfo) {
774 $saveValue[] = $fileInfo['filename'];
775 }
776
777 // Set in data array:
778 if ($flexpointer) {
779 $flexToolObj = t3lib_div::makeInstance('t3lib_flexformtools');
780 $dataArray[$refRec['tablename']][$refRec['recuid']][$refRec['field']]['data'] = array();
781 $flexToolObj->setArrayValueByPath(substr($flexpointer, 0, -1), $dataArray[$refRec['tablename']][$refRec['recuid']][$refRec['field']]['data'], implode(',', $saveValue));
782 } else {
783 $dataArray[$refRec['tablename']][$refRec['recuid']][$refRec['field']] = implode(',', $saveValue);
784 }
785
786 } else {
787 return 'ERROR: either "' . $refRec['ref_table'] . '" was not "_FILE" or file PATH_site+"' . $refRec['ref_string'] . '" did not match that of the record ("' . $itemArray[$refRec['sorting']]['ID_absFile'] . '") in sorting index "' . $refRec['sorting'] . '"';
788 }
789 }
790
791 /**
792 * Setting a value for a soft reference token
793 *
794 * @param array sys_refindex record
795 * @param array Array of soft reference occurencies
796 * @param string Value to substitute current value with
797 * @param array data array in which the new value is set (passed by reference)
798 * @param string Flexform pointer, if in a flex form field.
799 * @return string Error message if any, otherwise FALSE = OK
800 */
801 function setReferenceValue_softreferences($refRec, $softref, $newValue, &$dataArray, $flexpointer = '') {
802 if (is_array($softref['keys'][$refRec['softref_key']][$refRec['softref_id']])) {
803
804 // Set new value:
805 $softref['keys'][$refRec['softref_key']][$refRec['softref_id']]['subst']['tokenValue'] = '' . $newValue;
806
807 // Traverse softreferences and replace in tokenized content to rebuild it with new value inside:
808 foreach ($softref['keys'] as $sfIndexes) {
809 foreach ($sfIndexes as $data) {
810 $softref['tokenizedContent'] = str_replace('{softref:' . $data['subst']['tokenID'] . '}', $data['subst']['tokenValue'], $softref['tokenizedContent']);
811 }
812 }
813
814 // Set in data array:
815 if (!strstr($softref['tokenizedContent'], '{softref:')) {
816 if ($flexpointer) {
817 $flexToolObj = t3lib_div::makeInstance('t3lib_flexformtools');
818 $dataArray[$refRec['tablename']][$refRec['recuid']][$refRec['field']]['data'] = array();
819 $flexToolObj->setArrayValueByPath(substr($flexpointer, 0, -1), $dataArray[$refRec['tablename']][$refRec['recuid']][$refRec['field']]['data'], $softref['tokenizedContent']);
820 } else {
821 $dataArray[$refRec['tablename']][$refRec['recuid']][$refRec['field']] = $softref['tokenizedContent'];
822 }
823 } else {
824 return 'ERROR: After substituting all found soft references there were still soft reference tokens in the text. (theoretically this does not have to be an error if the string "{softref:" happens to be in the field for another reason.)';
825 }
826 } else {
827 return 'ERROR: Soft reference parser key "' . $refRec['softref_key'] . '" or the index "' . $refRec['softref_id'] . '" was not found.';
828 }
829 }
830
831
832 /*******************************
833 *
834 * Helper functions
835 *
836 *******************************/
837
838 /**
839 * Returns TRUE if the TCA/columns field type is a DB reference field
840 *
841 * @param array config array for TCA/columns field
842 * @return boolean TRUE if DB reference field (group/db or select with foreign-table)
843 */
844 function isReferenceField($conf) {
845 return ($conf['type'] == 'group' && $conf['internal_type'] == 'db') || (($conf['type'] == 'select' || $conf['type'] == 'inline') && $conf['foreign_table'] && $conf['foreign_table'] !== 'sys_file_reference');
846 }
847
848 /**
849 * Returns destination path to an upload folder given by $folder
850 *
851 * @param string Folder relative to PATH_site
852 * @return string Input folder prefixed with PATH_site. No checking for existence is done. Output must be a folder without trailing slash.
853 */
854 function destPathFromUploadFolder($folder) {
855 if (!$folder) {
856 return substr(PATH_site, 0, -1);
857 }
858 return PATH_site . $folder;
859 }
860
861 /**
862 * Sets error message in the internal error log
863 *
864 * @param string Error message
865 * @return void
866 */
867 function error($msg) {
868 $this->errorLog[] = $msg;
869 }
870
871 /**
872 * Updating Index (External API)
873 *
874 * @param boolean If set, only a test
875 * @param boolean If set, output CLI status
876 * @return array Header and body status content
877 */
878 function updateIndex($testOnly, $cli_echo = FALSE) {
879 $errors = array();
880 $tableNames = array();
881 $recCount = 0;
882 $tableCount = 0;
883
884 $headerContent = $testOnly ? 'Reference Index being TESTED (nothing written, use "-e" to update)' : 'Reference Index being Updated';
885 if ($cli_echo) {
886 echo
887 '*******************************************' . LF .
888 $headerContent . LF .
889 '*******************************************' . LF;
890 }
891
892 // Traverse all tables:
893 foreach ($GLOBALS['TCA'] as $tableName => $cfg) {
894 // Traverse all records in tables, including deleted records:
895 $allRecs = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', $tableName, '1=1');
896 if (!is_array($allRecs)) {
897 // Table exists in $TCA but does not exist in the database
898 t3lib_div::sysLog(sprintf('Table "%s" exists in $TCA but does not exist in the database. You should run the Database Analyzer in the Install Tool to fix this.', $tableName), 'core', t3lib_div::SYSLOG_SEVERITY_ERROR);
899 continue;
900 }
901
902 $tableNames[] = $tableName;
903 $tableCount++;
904
905 $uidList = array(0);
906 foreach ($allRecs as $recdat) {
907 $refIndexObj = t3lib_div::makeInstance('t3lib_refindex');
908 $result = $refIndexObj->updateRefIndexTable($tableName, $recdat['uid'], $testOnly);
909 $uidList[] = $recdat['uid'];
910 $recCount++;
911
912 if ($result['addedNodes'] || $result['deletedNodes']) {
913 $Err = 'Record ' . $tableName . ':' . $recdat['uid'] . ' had ' . $result['addedNodes'] . ' added indexes and ' . $result['deletedNodes'] . ' deleted indexes';
914 $errors[] = $Err;
915 if ($cli_echo) {
916 echo $Err . LF;
917 }
918 }
919 }
920
921 // Searching lost indexes for this table:
922 $where = 'tablename=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($tableName, 'sys_refindex') .
923 ' AND recuid NOT IN (' . implode(',', $uidList) . ')';
924 $lostIndexes = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('hash', 'sys_refindex', $where);
925 if (count($lostIndexes)) {
926 $Err = 'Table ' . $tableName . ' has ' . count($lostIndexes) . ' lost indexes which are now deleted';
927 $errors[] = $Err;
928 if ($cli_echo) {
929 echo $Err . LF;
930 }
931 if (!$testOnly) {
932 $GLOBALS['TYPO3_DB']->exec_DELETEquery('sys_refindex', $where);
933 }
934 }
935 }
936
937 // Searching lost indexes for non-existing tables:
938 $where = 'tablename NOT IN (' .
939 implode(',', $GLOBALS['TYPO3_DB']->fullQuoteArray($tableNames, 'sys_refindex')) .
940 ')';
941 $lostTables = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('hash', 'sys_refindex', $where);
942 if (count($lostTables)) {
943 $Err = 'Index table hosted ' . count($lostTables) . ' indexes for non-existing tables, now removed';
944 $errors[] = $Err;
945 if ($cli_echo) {
946 echo $Err . LF;
947 }
948 if (!$testOnly) {
949 $GLOBALS['TYPO3_DB']->exec_DELETEquery('sys_refindex', $where);
950 }
951 }
952
953 $testedHowMuch = $recCount . ' records from ' . $tableCount . ' tables were checked/updated.' . LF;
954
955 $bodyContent = $testedHowMuch . (count($errors) ? implode(LF, $errors) : 'Index Integrity was perfect!');
956 if ($cli_echo) {
957 echo $testedHowMuch . (count($errors) ? 'Updates: ' . count($errors) : 'Index Integrity was perfect!') . LF;
958 }
959
960 if (!$testOnly) {
961 $registry = t3lib_div::makeInstance('t3lib_Registry');
962 $registry->set('core', 'sys_refindex_lastUpdate', $GLOBALS['EXEC_TIME']);
963 }
964
965 return array($headerContent, $bodyContent, count($errors));
966 }
967 }
968
969
970 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_refindex.php'])) {
971 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_refindex.php']);
972 }
973
974 ?>