TYPO3 CMS  TYPO3_7-6
LinkAnalyzer.php
Go to the documentation of this file.
1 <?php
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 = [];
33 
39  protected $pidList = '';
40 
46  protected $linkCounts = [];
47 
53  protected $brokenLinkCounts = [];
54 
60  protected $recordsWithBrokenLinks = [];
61 
67  protected $hookObjectsArr = [];
68 
74  protected $extPageInTreeInfo = [];
75 
81  protected $recordReference = '';
82 
88  protected $pageWithAnchor = '';
89 
95  protected $tsConfig = [];
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 = [], $considerHidden = false)
134  {
135  $results = [];
136  $pidList = implode(',', GeneralUtility::intExplode(',', $this->pidList, true));
137  if (!empty($checkOptions) && !empty($pidList)) {
138  $checkKeys = array_keys($checkOptions);
139  $checkLinkTypeCondition = ' AND link_type IN (\'' . implode('\',\'', $checkKeys) . '\')';
140  $this->getDatabaseConnection()->exec_DELETEquery(
141  'tx_linkvalidator_link',
142  '(record_pid IN (' . $pidList . ')' .
143  ' OR ( record_uid IN (' . $pidList . ') AND table_name like \'pages\'))' .
144  $checkLinkTypeCondition
145  );
146  // Traverse all configured tables
147  foreach ($this->searchFields as $table => $fields) {
148  if ($table === 'pages') {
149  $where = 'uid IN (' . $pidList . ')';
150  } else {
151  $where = 'pid IN (' . $pidList . ')';
152  }
153  $where .= BackendUtility::deleteClause($table);
154  if (!$considerHidden) {
155  $where .= BackendUtility::BEenableFields($table);
156  }
157  // If table is not configured, assume the extension is not installed
158  // and therefore no need to check it
159  if (!is_array($GLOBALS['TCA'][$table])) {
160  continue;
161  }
162  // Re-init selectFields for table
163  $selectFields = 'uid, pid';
164  $selectFields .= ', ' . $GLOBALS['TCA'][$table]['ctrl']['label'] . ', ' . implode(', ', $fields);
165 
166  // @todo #64091: only select rows that have content in at least one of the relevant fields (via OR)
167  $rows = $this->getDatabaseConnection()->exec_SELECTgetRows($selectFields, $table, $where);
168  if (!empty($rows)) {
169  foreach ($rows as $row) {
170  $this->analyzeRecord($results, $table, $fields, $row);
171  }
172  }
173  }
174  foreach ($this->hookObjectsArr as $key => $hookObj) {
175  if (is_array($results[$key]) && empty($checkOptions) || is_array($results[$key]) && $checkOptions[$key]) {
176  // Check them
177  foreach ($results[$key] as $entryKey => $entryValue) {
178  $table = $entryValue['table'];
179  $record = [];
180  $record['headline'] = BackendUtility::getRecordTitle($table, $entryValue['row']);
181  $record['record_pid'] = $entryValue['row']['pid'];
182  $record['record_uid'] = $entryValue['uid'];
183  $record['table_name'] = $table;
184  $record['link_title'] = $entryValue['link_title'];
185  $record['field'] = $entryValue['field'];
186  $record['last_check'] = time();
187  $this->recordReference = $entryValue['substr']['recordRef'];
188  $this->pageWithAnchor = $entryValue['pageAndAnchor'];
189  if (!empty($this->pageWithAnchor)) {
190  // Page with anchor, e.g. 18#1580
191  $url = $this->pageWithAnchor;
192  } else {
193  $url = $entryValue['substr']['tokenValue'];
194  }
195  $this->linkCounts[$table]++;
196  $checkUrl = $hookObj->checkLink($url, $entryValue, $this);
197  // Broken link found
198  if (!$checkUrl) {
199  $response = [];
200  $response['valid'] = false;
201  $response['errorParams'] = $hookObj->getErrorParams();
202  $this->brokenLinkCounts[$table]++;
203  $record['link_type'] = $key;
204  $record['url'] = $url;
205  $record['url_response'] = serialize($response);
206  $this->getDatabaseConnection()->exec_INSERTquery('tx_linkvalidator_link', $record);
207  } elseif (GeneralUtility::_GP('showalllinks')) {
208  $response = [];
209  $response['valid'] = true;
210  $this->brokenLinkCounts[$table]++;
211  $record['url'] = $url;
212  $record['link_type'] = $key;
213  $record['url_response'] = serialize($response);
214  $this->getDatabaseConnection()->exec_INSERTquery('tx_linkvalidator_link', $record);
215  }
216  }
217  }
218  }
219  }
220  }
221 
231  public function analyzeRecord(array &$results, $table, array $fields, array $record)
232  {
233  list($results, $record) = $this->emitBeforeAnalyzeRecordSignal($results, $record, $table, $fields);
234 
235  // Put together content of all relevant fields
236  $haystack = '';
238  $htmlParser = GeneralUtility::makeInstance(HtmlParser::class);
239  $idRecord = $record['uid'];
240  // Get all references
241  foreach ($fields as $field) {
242  $haystack .= $record[$field] . ' --- ';
243  $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
244  $valueField = $record[$field];
245  // Check if a TCA configured field has soft references defined (see TYPO3 Core API document)
246  if ($conf['softref'] && (string)$valueField !== '') {
247  // Explode the list of soft references/parameters
248  $softRefs = BackendUtility::explodeSoftRefParserList($conf['softref']);
249  if ($softRefs !== false) {
250  // Traverse soft references
251  foreach ($softRefs as $spKey => $spParams) {
253  $softRefObj = BackendUtility::softRefParserObj($spKey);
254  // If there is an object returned...
255  if (is_object($softRefObj)) {
256  // Do processing
257  $resultArray = $softRefObj->findRef($table, $field, $idRecord, $valueField, $spKey, $spParams);
258  if (!empty($resultArray['elements'])) {
259  if ($spKey == 'typolink_tag') {
260  $this->analyseTypoLinks($resultArray, $results, $htmlParser, $record, $field, $table);
261  } else {
262  $this->analyseLinks($resultArray, $results, $record, $field, $table);
263  }
264  }
265  }
266  }
267  }
268  }
269  }
270  }
271 
280  public function getTSConfig()
281  {
282  return $this->tsConfig;
283  }
284 
295  protected function analyseLinks(array $resultArray, array &$results, array $record, $field, $table)
296  {
297  foreach ($resultArray['elements'] as $element) {
298  $r = $element['subst'];
299  $type = '';
300  $idRecord = $record['uid'];
301  if (!empty($r)) {
303  foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
304  $type = $hookObj->fetchType($r, $type, $keyArr);
305  // Store the type that was found
306  // This prevents overriding by internal validator
307  if (!empty($type)) {
308  $r['type'] = $type;
309  }
310  }
311  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['substr'] = $r;
312  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['row'] = $record;
313  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['table'] = $table;
314  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['field'] = $field;
315  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['uid'] = $idRecord;
316  }
317  }
318  }
319 
331  protected function analyseTypoLinks(array $resultArray, array &$results, $htmlParser, array $record, $field, $table)
332  {
333  $currentR = [];
334  $linkTags = $htmlParser->splitIntoBlock('link', $resultArray['content']);
335  $idRecord = $record['uid'];
336  $type = '';
337  $title = '';
338  $countLinkTags = count($linkTags);
339  for ($i = 1; $i < $countLinkTags; $i += 2) {
340  $referencedRecordType = '';
341  foreach ($resultArray['elements'] as $element) {
342  $type = '';
343  $r = $element['subst'];
344  if (!empty($r['tokenID'])) {
345  if (substr_count($linkTags[$i], $r['tokenID'])) {
346  // Type of referenced record
347  if (strpos($r['recordRef'], 'pages') !== false) {
348  $currentR = $r;
349  // Contains number of the page
350  $referencedRecordType = $r['tokenValue'];
351  $wasPage = true;
352  } elseif (strpos($r['recordRef'], 'tt_content') !== false && (isset($wasPage) && $wasPage === true)) {
353  $referencedRecordType = $referencedRecordType . '#c' . $r['tokenValue'];
354  $wasPage = false;
355  } else {
356  $currentR = $r;
357  }
358  $title = strip_tags($linkTags[$i]);
359  }
360  }
361  }
363  foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
364  $type = $hookObj->fetchType($currentR, $type, $keyArr);
365  // Store the type that was found
366  // This prevents overriding by internal validator
367  if (!empty($type)) {
368  $currentR['type'] = $type;
369  }
370  }
371  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['substr'] = $currentR;
372  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['row'] = $record;
373  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['table'] = $table;
374  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['field'] = $field;
375  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['uid'] = $idRecord;
376  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['link_title'] = $title;
377  $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['pageAndAnchor'] = $referencedRecordType;
378  }
379  }
380 
387  public function getLinkCounts($curPage)
388  {
389  $markerArray = [];
390  if (empty($this->pidList)) {
391  $this->pidList = $curPage;
392  }
393  $this->pidList = rtrim($this->pidList, ',');
394 
395  $rows = $this->getDatabaseConnection()->exec_SELECTgetRows(
396  'count(uid) as nbBrokenLinks,link_type',
397  'tx_linkvalidator_link',
398  'record_pid in (' . $this->pidList . ')',
399  'link_type'
400  );
401  if (!empty($rows)) {
402  foreach ($rows as $row) {
403  $markerArray[$row['link_type']] = $row['nbBrokenLinks'];
404  $markerArray['brokenlinkCount'] += $row['nbBrokenLinks'];
405  }
406  }
407  return $markerArray;
408  }
409 
425  public function extGetTreeList($id, $depth, $begin = 0, $permsClause, $considerHidden = false)
426  {
427  $depth = (int)$depth;
428  $begin = (int)$begin;
429  $id = (int)$id;
430  $theList = '';
431  if ($depth > 0) {
432  $rows = $this->getDatabaseConnection()->exec_SELECTgetRows(
433  'uid,title,hidden,extendToSubpages',
434  'pages',
435  'pid=' . $id . ' AND deleted=0 AND ' . $permsClause
436  );
437  if (!empty($rows)) {
438  foreach ($rows as $row) {
439  if ($begin <= 0 && ($row['hidden'] == 0 || $considerHidden)) {
440  $theList .= $row['uid'] . ',';
441  $this->extPageInTreeInfo[] = [$row['uid'], htmlspecialchars($row['title'], $depth)];
442  }
443  if ($depth > 1 && (!($row['hidden'] == 1 && $row['extendToSubpages'] == 1) || $considerHidden)) {
444  $theList .= $this->extGetTreeList($row['uid'], $depth - 1, $begin - 1, $permsClause, $considerHidden);
445  }
446  }
447  }
448  }
449  return $theList;
450  }
451 
458  public function getRootLineIsHidden(array $pageInfo)
459  {
460  $hidden = false;
461  if ($pageInfo['extendToSubpages'] == 1 && $pageInfo['hidden'] == 1) {
462  $hidden = true;
463  } else {
464  if ($pageInfo['pid'] > 0) {
465  $rows = $this->getDatabaseConnection()->exec_SELECTgetRows(
466  'uid,title,hidden,extendToSubpages',
467  'pages',
468  'uid=' . $pageInfo['pid']
469  );
470  if (!empty($rows)) {
471  foreach ($rows as $row) {
472  $hidden = $this->getRootLineIsHidden($row);
473  }
474  }
475  }
476  }
477  return $hidden;
478  }
479 
489  protected function emitBeforeAnalyzeRecordSignal($results, $record, $table, $fields)
490  {
491  return $this->getSignalSlotDispatcher()->dispatch(
492  self::class,
493  'beforeAnalyzeRecord',
494  [$results, $record, $table, $fields, $this]
495  );
496  }
497 
501  protected function getSignalSlotDispatcher()
502  {
503  return $this->getObjectManager()->get(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher::class);
504  }
505 
509  protected function getObjectManager()
510  {
511  return GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
512  }
513 
517  protected function getDatabaseConnection()
518  {
519  return $GLOBALS['TYPO3_DB'];
520  }
521 
525  protected function getLanguageService()
526  {
527  return $GLOBALS['LANG'];
528  }
529 }
static intExplode($delimiter, $string, $removeEmptyValues=false, $limit=0)
static BEenableFields($table, $inv=false)
static getRecordTitle($table, $row, $prep=false, $forceResult=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static deleteClause($table, $tableAlias='')