TYPO3 CMS  TYPO3_6-2
ReferenceIntegrityUpdateWizard.php
Go to the documentation of this file.
1 <?php
3 
29 
33  protected $title = 'Ensures the database integrity for File Abstraction records';
34 
41  public function checkForUpdate(&$description) {
42  $description = 'Checks if there are file references that are on the root level. ' .
43  'This could have happened due to a misconfigured previous migration. ' .
44  'This migration will also remove references to tables that no longer exist.';
45  return count($this->getRequiredUpdates()) > 0;
46  }
47 
55  public function performUpdate(array &$dbQueries, &$customMessages) {
56  $updates = $this->getRequiredUpdates();
57  if (isset($updates['referenceToMissingTables'])) {
58  foreach ($updates['referenceToMissingTables'] as $missingTable) {
59  $deleteQuery = $GLOBALS['TYPO3_DB']->DELETEquery(
60  'sys_file_reference',
61  'tablenames=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($missingTable, 'sys_file_reference')
62  );
63  $GLOBALS['TYPO3_DB']->sql_query($deleteQuery);
64  $dbQueries[] = $deleteQuery;
65  }
66  }
67  if (isset($updates['improperConnectedFileReferences'])) {
68  foreach ($updates['improperConnectedFileReferences'] as $fileReferenceRecord) {
69  if ($fileReferenceRecord['newpid'] > 0) {
70  $updateQuery = $GLOBALS['TYPO3_DB']->UPDATEquery(
71  'sys_file_reference',
72  'uid=' . (int)$fileReferenceRecord['uid'],
73  array('pid' => $fileReferenceRecord['newpid'])
74  );
75  $GLOBALS['TYPO3_DB']->sql_query($updateQuery);
76  $dbQueries[] = $updateQuery;
77  }
78  }
79  }
80  return TRUE;
81  }
82 
88  protected function getRequiredUpdates() {
89  $requiredUpdates = array();
90  $referenceToMissingTables = $this->getFileReferencesPointingToMissingTables();
91  if (count($referenceToMissingTables) > 0) {
92  $requiredUpdates['referenceToMissingTables'] = $referenceToMissingTables;
93  }
94  $improperConnectedFileReferences = $this->getImproperConnectedFileReferences($referenceToMissingTables);
95  if (count($improperConnectedFileReferences) > 0) {
96  $requiredUpdates['improperConnectedFileReferences'] = $improperConnectedFileReferences;
97  }
98  return $requiredUpdates;
99  }
100 
107  $existingTables = array_flip(array_keys($GLOBALS['TYPO3_DB']->admin_get_tables()));
108  $missingTables = array();
109  $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('DISTINCT tablenames', 'sys_file_reference', '');
110  while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
111  $thisTablename = $row['tablenames'];
112  if (!isset($existingTables[$thisTablename])) {
113  $missingTables[] = $thisTablename;
114  }
115  }
116  return $missingTables;
117  }
118 
124  protected function getFileReferencesOnRootlevel() {
125  $fileReferences = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
126  'uid, pid, uid_local AS fileuid, uid_foreign AS targetuid, tablenames AS targettable',
127  'sys_file_reference',
128  'pid=0 AND deleted=0'
129  );
130  if (!is_array($fileReferences)) {
131  // An SQL error occurred, most likely because the sys_file_reference table is not there.
132  // We ignore this here to avoid a warning when initially showing the upgrade wizard (see #65159).
133  return array();
134  }
135  return $fileReferences;
136  }
137 
145  protected function getImproperConnectedFileReferences(array $skipTables = array()) {
146  $improperConnectedReferences = array();
147  // fetch all references on root level
148  $sysFileReferences = $this->getFileReferencesOnRootlevel();
149  foreach ($sysFileReferences as $fileReferenceRecord) {
150  $tableName = $fileReferenceRecord['targettable'];
151  if (in_array($tableName, $skipTables)) {
152  continue;
153  }
154  // if the target table is pages (e.g. when adding a file reference to the pages->media
155  // record, then the
156  $whereClause = 'uid=' . (int)$fileReferenceRecord['targetuid'];
157  if ($fileReferenceRecord['targettable'] === 'pages') {
158  $isPageReference = TRUE;
159  } else {
160  $isPageReference = FALSE;
161  $whereClause .= ' AND pid<>0';
162  }
163  // check the target table, if the target record is NOT on the rootlevel
164  $targetRecord = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
165  'uid, pid',
166  $tableName,
167  $whereClause
168  );
169  // only add the file reference if the target record is not on PID=0
170  if ($targetRecord !== NULL) {
171  $fileReferenceRecord['newpid'] = ($isPageReference ? $targetRecord['uid'] : $targetRecord['pid']);
172  $improperConnectedReferences[] = $fileReferenceRecord;
173  }
174  }
175  return $improperConnectedReferences;
176  }
177 }
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]