TYPO3  7.6
LinkAnalyzer.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Linkvalidator;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
21 
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 
95  protected $tsConfig = array();
96 
100  public function __construct()
101  {
102  $this->getLanguageService()->includeLLFile('EXT:linkvalidator/Resources/Private/Language/Module/locallang.xlf');
103  // Hook to handle own checks
104  if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'])) {
105  foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'] as $key => $classRef) {
106  $this->hookObjectsArr[$key] = GeneralUtility::getUserObj($classRef);
107  }
108  }
109  }
110 
119  public function init(array $searchField, $pid, $tsConfig)
120  {
121  $this->searchFields = $searchField;
122  $this->pidList = $pid;
123  $this->tsConfig = $tsConfig;
124  }
125 
133  public function getLinkStatistics($checkOptions = array(), $considerHidden = false)
134  {
135  $results = array();
136  if (!empty($checkOptions)) {
137  $checkKeys = array_keys($checkOptions);
138  $checkLinkTypeCondition = ' AND link_type IN (\'' . implode('\',\'', $checkKeys) . '\')';
139  $this->getDatabaseConnection()->exec_DELETEquery(
140  'tx_linkvalidator_link',
141  '(record_pid IN (' . $this->pidList . ')' .
142  ' OR ( record_uid IN (' . $this->pidList . ') AND table_name like \'pages\'))' .
143  $checkLinkTypeCondition
144  );
145  // Traverse all configured tables
146  foreach ($this->searchFields as $table => $fields) {
147  if ($table === 'pages') {
148  $where = 'deleted = 0 AND uid IN (' . $this->pidList . ')';
149  } else {
150  $where = 'deleted = 0 AND pid IN (' . $this->pidList . ')';
151  }
152  if (!$considerHidden) {
153  $where .= BackendUtility::BEenableFields($table);
154  }
155  // If table is not configured, assume the extension is not installed
156  // and therefore no need to check it
157  if (!is_array($GLOBALS['TCA'][$table])) {
158  continue;
159  }
160  // Re-init selectFields for table
161  $selectFields = 'uid, pid';
162  $selectFields .= ', ' . $GLOBALS['TCA'][$table]['ctrl']['label'] . ', ' . implode(', ', $fields);
163 
164  // @todo #64091: only select rows that have content in at least one of the relevant fields (via OR)
165  $rows = $this->getDatabaseConnection()->exec_SELECTgetRows($selectFields, $table, $where);
166  if (!empty($rows)) {
167  foreach ($rows as $row) {
168  $this->analyzeRecord($results, $table, $fields, $row);
169  }
170  }
171  }
172  foreach ($this->hookObjectsArr as $key => $hookObj) {
173  if (is_array($results[$key]) && empty($checkOptions) || is_array($results[$key]) && $checkOptions[$key]) {
174  // Check them
175  foreach ($results[$key] as $entryKey => $entryValue) {
176  $table = $entryValue['table'];
177  $record = array();
178  $record['headline'] = BackendUtility::getRecordTitle($table, $entryValue['row']);
179  $record['record_pid'] = $entryValue['row']['pid'];
180  $record['record_uid'] = $entryValue['uid'];
181  $record['table_name'] = $table;
182  $record['link_title'] = $entryValue['link_title'];
183  $record['field'] = $entryValue['field'];
184  $record['last_check'] = time();
185  $this->recordReference = $entryValue['substr']['recordRef'];
186  $this->pageWithAnchor = $entryValue['pageAndAnchor'];
187  if (!empty($this->pageWithAnchor)) {
188  // Page with anchor, e.g. 18#1580
190  } else {
191  $url = $entryValue['substr']['tokenValue'];
192  }
193  $this->linkCounts[$table]++;
194  $checkUrl = $hookObj->checkLink($url, $entryValue, $this);
195  // Broken link found
196  if (!$checkUrl) {
197  $response = array();
198  $response['valid'] = false;
199  $response['errorParams'] = $hookObj->getErrorParams();
200  $this->brokenLinkCounts[$table]++;
201  $record['link_type'] = $key;
202  $record['url'] = $url;
203  $record['url_response'] = serialize($response);
204  $this->getDatabaseConnection()->exec_INSERTquery('tx_linkvalidator_link', $record);
205  } elseif (GeneralUtility::_GP('showalllinks')) {
206  $response = array();
207  $response['valid'] = true;
208  $this->brokenLinkCounts[$table]++;
209  $record['url'] = $url;
210  $record['link_type'] = $key;
211  $record['url_response'] = serialize($response);
212  $this->getDatabaseConnection()->exec_INSERTquery('tx_linkvalidator_link', $record);
213  }
214  }
215  }
216  }
217  }
218  }
219 
229  public function analyzeRecord(array &$results, $table, array $fields, array $record)
230  {
231  list($results, $record) = $this->emitBeforeAnalyzeRecordSignal($results, $record, $table, $fields);
232 
233  // Put together content of all relevant fields
234  $haystack = '';
236  $htmlParser = GeneralUtility::makeInstance(HtmlParser::class);
237  $idRecord = $record['uid'];
238  // Get all references
239  foreach ($fields as $field) {
240  $haystack .= $record[$field] . ' --- ';
241  $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
242  $valueField = $record[$field];
243  // Check if a TCA configured field has soft references defined (see TYPO3 Core API document)
244  if ($conf['softref'] && (string)$valueField !== '') {
245  // Explode the list of soft references/parameters
246  $softRefs = BackendUtility::explodeSoftRefParserList($conf['softref']);
247  if ($softRefs !== false) {
248  // Traverse soft references
249  foreach ($softRefs as $spKey => $spParams) {
251  $softRefObj = BackendUtility::softRefParserObj($spKey);
252  // If there is an object returned...
253  if (is_object($softRefObj)) {
254  // Do processing
255  $resultArray = $softRefObj->findRef($table, $field, $idRecord, $valueField, $spKey, $spParams);
256  if (!empty($resultArray['elements'])) {
257  if ($spKey == 'typolink_tag') {
258  $this->analyseTypoLinks($resultArray, $results, $htmlParser, $record, $field, $table);
259  } else {
260  $this->analyseLinks($resultArray, $results, $record, $field, $table);
261  }
262  }
263  }
264  }
265  }
266  }
267  }
268  }
269 
278  public function getTSConfig()
279  {
280  return $this->tsConfig;
281  }
282 
293  protected function analyseLinks(array $resultArray, array &$results, array $record, $field, $table)
294  {
295  foreach ($resultArray['elements'] as $element) {
296  $r = $element['subst'];
297  $type = '';
298  $idRecord = $record['uid'];
299  if (!empty($r)) {
301  foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
302  $type = $hookObj->fetchType($r, $type, $keyArr);
303  // Store the type that was found
304  // This prevents overriding by internal validator
305  if (!empty($type)) {
306  $r['type'] = $type;
307  }
308  }
309  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['substr'] = $r;
310  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['row'] = $record;
311  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['table'] = $table;
312  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['field'] = $field;
313  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['uid'] = $idRecord;
314  }
315  }
316  }
317 
329  protected function analyseTypoLinks(array $resultArray, array &$results, $htmlParser, array $record, $field, $table)
330  {
331  $currentR = array();
332  $linkTags = $htmlParser->splitIntoBlock('link', $resultArray['content']);
333  $idRecord = $record['uid'];
334  $type = '';
335  $title = '';
336  $countLinkTags = count($linkTags);
337  for ($i = 1; $i < $countLinkTags; $i += 2) {
338  $referencedRecordType = '';
339  foreach ($resultArray['elements'] as $element) {
340  $type = '';
341  $r = $element['subst'];
342  if (!empty($r['tokenID'])) {
343  if (substr_count($linkTags[$i], $r['tokenID'])) {
344  // Type of referenced record
345  if (strpos($r['recordRef'], 'pages') !== false) {
346  $currentR = $r;
347  // Contains number of the page
348  $referencedRecordType = $r['tokenValue'];
349  $wasPage = true;
350  } elseif (strpos($r['recordRef'], 'tt_content') !== false && (isset($wasPage) && $wasPage === true)) {
351  $referencedRecordType = $referencedRecordType . '#c' . $r['tokenValue'];
352  $wasPage = false;
353  } else {
354  $currentR = $r;
355  }
356  $title = strip_tags($linkTags[$i]);
357  }
358  }
359  }
361  foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
362  $type = $hookObj->fetchType($currentR, $type, $keyArr);
363  // Store the type that was found
364  // This prevents overriding by internal validator
365  if (!empty($type)) {
366  $currentR['type'] = $type;
367  }
368  }
369  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['substr'] = $currentR;
370  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['row'] = $record;
371  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['table'] = $table;
372  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['field'] = $field;
373  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['uid'] = $idRecord;
374  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['link_title'] = $title;
375  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['pageAndAnchor'] = $referencedRecordType;
376  }
377  }
378 
385  public function getLinkCounts($curPage)
386  {
387  $markerArray = array();
388  if (empty($this->pidList)) {
389  $this->pidList = $curPage;
390  }
391  $this->pidList = rtrim($this->pidList, ',');
392 
393  $rows = $this->getDatabaseConnection()->exec_SELECTgetRows(
394  'count(uid) as nbBrokenLinks,link_type',
395  'tx_linkvalidator_link',
396  'record_pid in (' . $this->pidList . ')',
397  'link_type'
398  );
399  if (!empty($rows)) {
400  foreach ($rows as $row) {
401  $markerArray[$row['link_type']] = $row['nbBrokenLinks'];
402  $markerArray['brokenlinkCount'] += $row['nbBrokenLinks'];
403  }
404  }
405  return $markerArray;
406  }
407 
423  public function extGetTreeList($id, $depth, $begin = 0, $permsClause, $considerHidden = false)
424  {
425  $depth = (int)$depth;
426  $begin = (int)$begin;
427  $id = (int)$id;
428  $theList = '';
429  if ($depth > 0) {
430  $rows = $this->getDatabaseConnection()->exec_SELECTgetRows(
431  'uid,title,hidden,extendToSubpages',
432  'pages',
433  'pid=' . $id . ' AND deleted=0 AND ' . $permsClause
434  );
435  if (!empty($rows)) {
436  foreach ($rows as $row) {
437  if ($begin <= 0 && ($row['hidden'] == 0 || $considerHidden)) {
438  $theList .= $row['uid'] . ',';
439  $this->extPageInTreeInfo[] = array($row['uid'], htmlspecialchars($row['title'], $depth));
440  }
441  if ($depth > 1 && (!($row['hidden'] == 1 && $row['extendToSubpages'] == 1) || $considerHidden)) {
442  $theList .= $this->extGetTreeList($row['uid'], $depth - 1, $begin - 1, $permsClause, $considerHidden);
443  }
444  }
445  }
446  }
447  return $theList;
448  }
449 
456  public function getRootLineIsHidden(array $pageInfo)
457  {
458  $hidden = false;
459  if ($pageInfo['extendToSubpages'] == 1 && $pageInfo['hidden'] == 1) {
460  $hidden = true;
461  } else {
462  if ($pageInfo['pid'] > 0) {
463  $rows = $this->getDatabaseConnection()->exec_SELECTgetRows(
464  'uid,title,hidden,extendToSubpages',
465  'pages',
466  'uid=' . $pageInfo['pid']
467  );
468  if (!empty($rows)) {
469  foreach ($rows as $row) {
470  $hidden = $this->getRootLineIsHidden($row);
471  }
472  }
473  }
474  }
475  return $hidden;
476  }
477 
487  protected function emitBeforeAnalyzeRecordSignal($results, $record, $table, $fields)
488  {
489  return $this->getSignalSlotDispatcher()->dispatch(
490  self::class,
491  'beforeAnalyzeRecord',
492  array($results, $record, $table, $fields, $this)
493  );
494  }
495 
499  protected function getSignalSlotDispatcher()
500  {
501  return $this->getObjectManager()->get(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
502  }
503 
507  protected function getObjectManager()
508  {
509  return GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
510  }
511 
515  protected function getDatabaseConnection()
516  {
517  return $GLOBALS['TYPO3_DB'];
518  }
519 
523  protected function getLanguageService()
524  {
525  return $GLOBALS['LANG'];
526  }
527 }