TYPO3 CMS  TYPO3_6-2
LinkAnalyzer.php
Go to the documentation of this file.
1 <?php
3 
18 
25 class LinkAnalyzer {
26 
32  protected $searchFields = array();
33 
39  protected $pidList = '';
40 
46  protected $linkCounts = array();
47 
53  protected $brokenLinkCounts = array();
54 
60  protected $recordsWithBrokenLinks = array();
61 
67  protected $hookObjectsArr = array();
68 
74  protected $extPageInTreeInfo = array();
75 
81  protected $recordReference = '';
82 
88  protected $pageWithAnchor = '';
89 
93  public function __construct() {
94  $GLOBALS['LANG']->includeLLFile('EXT:linkvalidator/Resources/Private/Language/Module/locallang.xlf');
95  // Hook to handle own checks
96  if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'])) {
97  foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'] as $key => $classRef) {
98  $this->hookObjectsArr[$key] = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($classRef);
99  }
100  }
101  }
102 
110  public function init(array $searchField, $pid) {
111  $this->searchFields = $searchField;
112  $this->pidList = $pid;
113  }
114 
122  public function getLinkStatistics($checkOptions = array(), $considerHidden = FALSE) {
123  $results = array();
124  $pidList = implode(',', \TYPO3\CMS\Core\Utility\GeneralUtility::intExplode(',', $this->pidList, true));
125  if (count($checkOptions) > 0 && !empty($pidList)) {
126  $checkKeys = array_keys($checkOptions);
127  $checkLinkTypeCondition = ' AND link_type IN (\'' . implode('\',\'', $checkKeys) . '\')';
128  $GLOBALS['TYPO3_DB']->exec_DELETEquery(
129  'tx_linkvalidator_link',
130  '(record_pid IN (' . $pidList . ')' .
131  ' OR ( record_uid IN (' . $pidList . ') AND table_name like \'pages\'))' .
132  $checkLinkTypeCondition
133  );
134  // Traverse all configured tables
135  foreach ($this->searchFields as $table => $fields) {
136  if ($table === 'pages') {
137  $where = 'deleted = 0 AND uid IN (' . $pidList . ')';
138  } else {
139  $where = 'deleted = 0 AND pid IN (' . $pidList . ')';
140  }
141  if (!$considerHidden) {
142  $where .= BackendUtility::BEenableFields($table);
143  }
144  // If table is not configured, assume the extension is not installed
145  // and therefore no need to check it
146  if (!is_array($GLOBALS['TCA'][$table])) {
147  continue;
148  }
149  // Re-init selectFields for table
150  $selectFields = 'uid, pid';
151  $selectFields .= ', ' . $GLOBALS['TCA'][$table]['ctrl']['label'] . ', ' . implode(', ', $fields);
152  // TODO: only select rows that have content in at least one of the relevant fields (via OR)
153  $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selectFields, $table, $where);
154  // Get record rows of table
155  while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
156  // Analyse each record
157  $this->analyzeRecord($results, $table, $fields, $row);
158  }
159  $GLOBALS['TYPO3_DB']->sql_free_result($res);
160  }
161  foreach ($this->hookObjectsArr as $key => $hookObj) {
162  if (is_array($results[$key]) && empty($checkOptions) || is_array($results[$key]) && $checkOptions[$key]) {
163  // Check them
164  foreach ($results[$key] as $entryKey => $entryValue) {
165  $table = $entryValue['table'];
166  $record = array();
167  $record['headline'] = BackendUtility::getRecordTitle($table, $entryValue['row']);
168  $record['record_pid'] = $entryValue['row']['pid'];
169  $record['record_uid'] = $entryValue['uid'];
170  $record['table_name'] = $table;
171  $record['link_title'] = $entryValue['link_title'];
172  $record['field'] = $entryValue['field'];
173  $record['last_check'] = time();
174  $this->recordReference = $entryValue['substr']['recordRef'];
175  $this->pageWithAnchor = $entryValue['pageAndAnchor'];
176  if (!empty($this->pageWithAnchor)) {
177  // Page with anchor, e.g. 18#1580
178  $url = $this->pageWithAnchor;
179  } else {
180  $url = $entryValue['substr']['tokenValue'];
181  }
182  $this->linkCounts[$table]++;
183  $checkUrl = $hookObj->checkLink($url, $entryValue, $this);
184  // Broken link found
185  if (!$checkUrl) {
186  $response = array();
187  $response['valid'] = FALSE;
188  $response['errorParams'] = $hookObj->getErrorParams();
189  $this->brokenLinkCounts[$table]++;
190  $record['link_type'] = $key;
191  $record['url'] = $url;
192  $record['url_response'] = serialize($response);
193  $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
194  } elseif (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('showalllinks')) {
195  $response = array();
196  $response['valid'] = TRUE;
197  $this->brokenLinkCounts[$table]++;
198  $record['url'] = $url;
199  $record['link_type'] = $key;
200  $record['url_response'] = serialize($response);
201  $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
202  }
203  }
204  }
205  }
206  }
207  }
208 
218  public function analyzeRecord(array &$results, $table, array $fields, array $record) {
219  // Put together content of all relevant fields
220  $haystack = '';
222  $htmlParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Html\\HtmlParser');
223  $idRecord = $record['uid'];
224  // Get all references
225  foreach ($fields as $field) {
226  $haystack .= $record[$field] . ' --- ';
227  $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
228  $valueField = $record[$field];
229  // Check if a TCA configured field has soft references defined (see TYPO3 Core API document)
230  if ($conf['softref'] && strlen($valueField)) {
231  // Explode the list of soft references/parameters
232  $softRefs = BackendUtility::explodeSoftRefParserList($conf['softref']);
233  if ($softRefs !== FALSE) {
234  // Traverse soft references
235  foreach ($softRefs as $spKey => $spParams) {
237  $softRefObj = BackendUtility::softRefParserObj($spKey);
238  // If there is an object returned...
239  if (is_object($softRefObj)) {
240  // Do processing
241  $resultArray = $softRefObj->findRef($table, $field, $idRecord, $valueField, $spKey, $spParams);
242  if (!empty($resultArray['elements'])) {
243  if ($spKey == 'typolink_tag') {
244  $this->analyseTypoLinks($resultArray, $results, $htmlParser, $record, $field, $table);
245  } else {
246  $this->analyseLinks($resultArray, $results, $record, $field, $table);
247  }
248  }
249  }
250  }
251  }
252  }
253  }
254  }
255 
266  protected function analyseLinks(array $resultArray, array &$results, array $record, $field, $table) {
267  foreach ($resultArray['elements'] as $element) {
268  $r = $element['subst'];
269  $type = '';
270  $idRecord = $record['uid'];
271  if (!empty($r)) {
273  foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
274  $type = $hookObj->fetchType($r, $type, $keyArr);
275  // Store the type that was found
276  // This prevents overriding by internal validator
277  if (!empty($type)) {
278  $r['type'] = $type;
279  }
280  }
281  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['substr'] = $r;
282  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['row'] = $record;
283  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['table'] = $table;
284  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['field'] = $field;
285  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['uid'] = $idRecord;
286  }
287  }
288  }
289 
301  protected function analyseTypoLinks(array $resultArray, array &$results, $htmlParser, array $record, $field, $table) {
302  $currentR = array();
303  $linkTags = $htmlParser->splitIntoBlock('link', $resultArray['content']);
304  $idRecord = $record['uid'];
305  $type = '';
306  $title = '';
307  for ($i = 1; $i < count($linkTags); $i += 2) {
308  $referencedRecordType = '';
309  foreach ($resultArray['elements'] as $element) {
310  $type = '';
311  $r = $element['subst'];
312  if (!empty($r['tokenID'])) {
313  if (substr_count($linkTags[$i], $r['tokenID'])) {
314  // Type of referenced record
315  if (strpos($r['recordRef'], 'pages') !== FALSE) {
316  $currentR = $r;
317  // Contains number of the page
318  $referencedRecordType = $r['tokenValue'];
319  $wasPage = TRUE;
320  } elseif (strpos($r['recordRef'], 'tt_content') !== FALSE && (isset($wasPage) && $wasPage === TRUE)) {
321  $referencedRecordType = $referencedRecordType . '#c' . $r['tokenValue'];
322  $wasPage = FALSE;
323  } else {
324  $currentR = $r;
325  }
326  $title = strip_tags($linkTags[$i]);
327  }
328  }
329  }
331  foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
332  $type = $hookObj->fetchType($currentR, $type, $keyArr);
333  // Store the type that was found
334  // This prevents overriding by internal validator
335  if (!empty($type)) {
336  $currentR['type'] = $type;
337  }
338  }
339  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['substr'] = $currentR;
340  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['row'] = $record;
341  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['table'] = $table;
342  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['field'] = $field;
343  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['uid'] = $idRecord;
344  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['link_title'] = $title;
345  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['pageAndAnchor'] = $referencedRecordType;
346  }
347  }
348 
355  public function getLinkCounts($curPage) {
356  $markerArray = array();
357  if (empty($this->pidList)) {
358  $this->pidList = $curPage;
359  }
360  $this->pidList = rtrim($this->pidList, ',');
361  if (($res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
362  'count(uid) as nbBrokenLinks,link_type',
363  'tx_linkvalidator_link',
364  'record_pid in (' . $this->pidList . ')', 'link_type')
365  )) {
366  while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
367  $markerArray[$row['link_type']] = $row['nbBrokenLinks'];
368  $markerArray['brokenlinkCount'] += $row['nbBrokenLinks'];
369  }
370  }
371  $GLOBALS['TYPO3_DB']->sql_free_result($res);
372  return $markerArray;
373  }
374 
390  public function extGetTreeList($id, $depth, $begin = 0, $permsClause, $considerHidden = FALSE) {
391  $depth = (int)$depth;
392  $begin = (int)$begin;
393  $id = (int)$id;
394  $theList = '';
395  if ($depth > 0) {
396  $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
397  'uid,title,hidden,extendToSubpages',
398  'pages', 'pid=' . $id . ' AND deleted=0 AND ' . $permsClause
399  );
400  while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
401  if ($begin <= 0 && ($row['hidden'] == 0 || $considerHidden == 1)) {
402  $theList .= $row['uid'] . ',';
403  $this->extPageInTreeInfo[] = array($row['uid'], htmlspecialchars($row['title'], $depth));
404  }
405  if ($depth > 1 && (!($row['hidden'] == 1 && $row['extendToSubpages'] == 1) || $considerHidden == 1)) {
406  $theList .= $this->extGetTreeList($row['uid'], $depth - 1, $begin - 1, $permsClause, $considerHidden);
407  }
408  }
409  $GLOBALS['TYPO3_DB']->sql_free_result($res);
410  }
411  return $theList;
412  }
413 
420  public function getRootLineIsHidden(array $pageInfo) {
421  $hidden = FALSE;
422  if ($pageInfo['extendToSubpages'] == 1 && $pageInfo['hidden'] == 1) {
423  $hidden = TRUE;
424  } else {
425  if ($pageInfo['pid'] > 0) {
426  $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,title,hidden,extendToSubpages', 'pages', 'uid=' . $pageInfo['pid']);
427  while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
428  $hidden = $this->getRootLineIsHidden($row);
429  }
430  $GLOBALS['TYPO3_DB']->sql_free_result($res);
431  } else {
432  $hidden = FALSE;
433  }
434  }
435  return $hidden;
436  }
437 
438 }
static getUserObj($classRef, $checkPrefix='', $silent=FALSE)
static getRecordTitle($table, $row, $prep=FALSE, $forceResult=TRUE)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]