[TASK] Re-work/simplify copyright header in PHP files - Part 7
[Packages/TYPO3.CMS.git] / typo3 / sysext / linkvalidator / Classes / LinkAnalyzer.php
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
17 use TYPO3\CMS\Backend\Utility\BackendUtility;
18
19 /**
20 * This class provides Processing plugin implementation
21 *
22 * @author Michael Miousse <michael.miousse@infoglobe.ca>
23 * @author Jochen Rieger <j.rieger@connecta.ag>
24 */
25 class LinkAnalyzer {
26
27 /**
28 * Array of tables and fields to search for broken links
29 *
30 * @var array
31 */
32 protected $searchFields = array();
33
34 /**
35 * List of comma separated page uids (rootline downwards)
36 *
37 * @var string
38 */
39 protected $pidList = '';
40
41 /**
42 * Array of tables and the number of external links they contain
43 *
44 * @var array
45 */
46 protected $linkCounts = array();
47
48 /**
49 * Array of tables and the number of broken external links they contain
50 *
51 * @var array
52 */
53 protected $brokenLinkCounts = array();
54
55 /**
56 * Array of tables and records containing broken links
57 *
58 * @var array
59 */
60 protected $recordsWithBrokenLinks = array();
61
62 /**
63 * Array for hooks for own checks
64 *
65 * @var \TYPO3\CMS\Linkvalidator\Linktype\AbstractLinktype[]
66 */
67 protected $hookObjectsArr = array();
68
69 /**
70 * Array with information about the current page
71 *
72 * @var array
73 */
74 protected $extPageInTreeInfo = array();
75
76 /**
77 * Reference to the current element with table:uid, e.g. pages:85
78 *
79 * @var string
80 */
81 protected $recordReference = '';
82
83 /**
84 * Linked page together with a possible anchor, e.g. 85#c105
85 *
86 * @var string
87 */
88 protected $pageWithAnchor = '';
89
90 /**
91 * Fill hookObjectsArr with different link types and possible XClasses.
92 */
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
103 /**
104 * Store all the needed configuration values in class variables
105 *
106 * @param array $searchField List of fields in which to search for links
107 * @param string $pid List of comma separated page uids in which to search for links
108 * @return void
109 */
110 public function init(array $searchField, $pid) {
111 $this->searchFields = $searchField;
112 $this->pidList = $pid;
113 }
114
115 /**
116 * Find all supported broken links and store them in tx_linkvalidator_link
117 *
118 * @param array $checkOptions List of hook object to activate
119 * @param boolean $considerHidden Defines whether to look into hidden fields
120 * @return void
121 */
122 public function getLinkStatistics($checkOptions = array(), $considerHidden = FALSE) {
123 $results = array();
124 if (count($checkOptions) > 0) {
125 $checkKeys = array_keys($checkOptions);
126 $checkLinkTypeCondition = ' AND link_type IN (\'' . implode('\',\'', $checkKeys) . '\')';
127 $GLOBALS['TYPO3_DB']->exec_DELETEquery(
128 'tx_linkvalidator_link',
129 '(record_pid IN (' . $this->pidList . ')' .
130 ' OR ( record_uid IN (' . $this->pidList . ') AND table_name like \'pages\'))' .
131 $checkLinkTypeCondition
132 );
133 // Traverse all configured tables
134 foreach ($this->searchFields as $table => $fields) {
135 if ($table === 'pages') {
136 $where = 'deleted = 0 AND uid IN (' . $this->pidList . ')';
137 } else {
138 $where = 'deleted = 0 AND pid IN (' . $this->pidList . ')';
139 }
140 if (!$considerHidden) {
141 $where .= BackendUtility::BEenableFields($table);
142 }
143 // If table is not configured, assume the extension is not installed
144 // and therefore no need to check it
145 if (!is_array($GLOBALS['TCA'][$table])) {
146 continue;
147 }
148 // Re-init selectFields for table
149 $selectFields = 'uid, pid';
150 $selectFields .= ', ' . $GLOBALS['TCA'][$table]['ctrl']['label'] . ', ' . implode(', ', $fields);
151 // TODO: only select rows that have content in at least one of the relevant fields (via OR)
152 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selectFields, $table, $where);
153 // Get record rows of table
154 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
155 // Analyse each record
156 $this->analyzeRecord($results, $table, $fields, $row);
157 }
158 $GLOBALS['TYPO3_DB']->sql_free_result($res);
159 }
160 foreach ($this->hookObjectsArr as $key => $hookObj) {
161 if (is_array($results[$key]) && empty($checkOptions) || is_array($results[$key]) && $checkOptions[$key]) {
162 // Check them
163 foreach ($results[$key] as $entryKey => $entryValue) {
164 $table = $entryValue['table'];
165 $record = array();
166 $record['headline'] = $entryValue['row'][$GLOBALS['TCA'][$table]['ctrl']['label']];
167 $record['record_pid'] = $entryValue['row']['pid'];
168 $record['record_uid'] = $entryValue['uid'];
169 $record['table_name'] = $table;
170 $record['link_title'] = $entryValue['link_title'];
171 $record['field'] = $entryValue['field'];
172 $record['last_check'] = time();
173 $this->recordReference = $entryValue['substr']['recordRef'];
174 $this->pageWithAnchor = $entryValue['pageAndAnchor'];
175 if (!empty($this->pageWithAnchor)) {
176 // Page with anchor, e.g. 18#1580
177 $url = $this->pageWithAnchor;
178 } else {
179 $url = $entryValue['substr']['tokenValue'];
180 }
181 $this->linkCounts[$table]++;
182 $checkUrl = $hookObj->checkLink($url, $entryValue, $this);
183 // Broken link found
184 if (!$checkUrl) {
185 $response = array();
186 $response['valid'] = FALSE;
187 $response['errorParams'] = $hookObj->getErrorParams();
188 $this->brokenLinkCounts[$table]++;
189 $record['link_type'] = $key;
190 $record['url'] = $url;
191 $record['url_response'] = serialize($response);
192 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
193 } elseif (\TYPO3\CMS\Core\Utility\GeneralUtility::_GP('showalllinks')) {
194 $response = array();
195 $response['valid'] = TRUE;
196 $this->brokenLinkCounts[$table]++;
197 $record['url'] = $url;
198 $record['link_type'] = $key;
199 $record['url_response'] = serialize($response);
200 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
201 }
202 }
203 }
204 }
205 }
206 }
207
208 /**
209 * Find all supported broken links for a specific record
210 *
211 * @param array $results Array of broken links
212 * @param string $table Table name of the record
213 * @param array $fields Array of fields to analyze
214 * @param array $record Record to analyse
215 * @return void
216 */
217 public function analyzeRecord(array &$results, $table, array $fields, array $record) {
218 // Put together content of all relevant fields
219 $haystack = '';
220 /** @var $htmlParser \TYPO3\CMS\Core\Html\HtmlParser */
221 $htmlParser = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Html\\HtmlParser');
222 $idRecord = $record['uid'];
223 // Get all references
224 foreach ($fields as $field) {
225 $haystack .= $record[$field] . ' --- ';
226 $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
227 $valueField = $record[$field];
228 // Check if a TCA configured field has soft references defined (see TYPO3 Core API document)
229 if ($conf['softref'] && strlen($valueField)) {
230 // Explode the list of soft references/parameters
231 $softRefs = BackendUtility::explodeSoftRefParserList($conf['softref']);
232 // Traverse soft references
233 foreach ($softRefs as $spKey => $spParams) {
234 /** @var $softRefObj \TYPO3\CMS\Core\Database\SoftReferenceIndex */
235 $softRefObj = BackendUtility::softRefParserObj($spKey);
236 // If there is an object returned...
237 if (is_object($softRefObj)) {
238 // Do processing
239 $resultArray = $softRefObj->findRef($table, $field, $idRecord, $valueField, $spKey, $spParams);
240 if (!empty($resultArray['elements'])) {
241 if ($spKey == 'typolink_tag') {
242 $this->analyseTypoLinks($resultArray, $results, $htmlParser, $record, $field, $table);
243 } else {
244 $this->analyseLinks($resultArray, $results, $record, $field, $table);
245 }
246 }
247 }
248 }
249 }
250 }
251 }
252
253 /**
254 * Find all supported broken links for a specific link list
255 *
256 * @param array $resultArray findRef parsed records
257 * @param array $results Array of broken links
258 * @param array $record UID of the current record
259 * @param string $field The current field
260 * @param string $table The current table
261 * @return void
262 */
263 protected function analyseLinks(array $resultArray, array &$results, array $record, $field, $table) {
264 foreach ($resultArray['elements'] as $element) {
265 $r = $element['subst'];
266 $type = '';
267 $idRecord = $record['uid'];
268 if (!empty($r)) {
269 /** @var $hookObj \TYPO3\CMS\Linkvalidator\Linktype\AbstractLinktype */
270 foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
271 $type = $hookObj->fetchType($r, $type, $keyArr);
272 // Store the type that was found
273 // This prevents overriding by internal validator
274 if (!empty($type)) {
275 $r['type'] = $type;
276 }
277 }
278 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['substr'] = $r;
279 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['row'] = $record;
280 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['table'] = $table;
281 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['field'] = $field;
282 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r['tokenID']]['uid'] = $idRecord;
283 }
284 }
285 }
286
287 /**
288 * Find all supported broken links for a specific typoLink
289 *
290 * @param array $resultArray findRef parsed records
291 * @param array $results Array of broken links
292 * @param \TYPO3\CMS\Core\Html\HtmlParser $htmlParser Instance of html parser
293 * @param array $record The current record
294 * @param string $field The current field
295 * @param string $table The current table
296 * @return void
297 */
298 protected function analyseTypoLinks(array $resultArray, array &$results, $htmlParser, array $record, $field, $table) {
299 $currentR = array();
300 $linkTags = $htmlParser->splitIntoBlock('link', $resultArray['content']);
301 $idRecord = $record['uid'];
302 $type = '';
303 $title = '';
304 for ($i = 1; $i < count($linkTags); $i += 2) {
305 $referencedRecordType = '';
306 foreach ($resultArray['elements'] as $element) {
307 $type = '';
308 $r = $element['subst'];
309 if (!empty($r['tokenID'])) {
310 if (substr_count($linkTags[$i], $r['tokenID'])) {
311 // Type of referenced record
312 if (strpos($r['recordRef'], 'pages') !== FALSE) {
313 $currentR = $r;
314 // Contains number of the page
315 $referencedRecordType = $r['tokenValue'];
316 $wasPage = TRUE;
317 } elseif (strpos($r['recordRef'], 'tt_content') !== FALSE && (isset($wasPage) && $wasPage === TRUE)) {
318 $referencedRecordType = $referencedRecordType . '#c' . $r['tokenValue'];
319 $wasPage = FALSE;
320 } else {
321 $currentR = $r;
322 }
323 $title = strip_tags($linkTags[$i]);
324 }
325 }
326 }
327 /** @var $hookObj \TYPO3\CMS\Linkvalidator\Linktype\AbstractLinktype */
328 foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
329 $type = $hookObj->fetchType($currentR, $type, $keyArr);
330 // Store the type that was found
331 // This prevents overriding by internal validator
332 if (!empty($type)) {
333 $currentR['type'] = $type;
334 }
335 }
336 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['substr'] = $currentR;
337 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['row'] = $record;
338 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['table'] = $table;
339 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['field'] = $field;
340 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['uid'] = $idRecord;
341 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['link_title'] = $title;
342 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR['tokenID']]['pageAndAnchor'] = $referencedRecordType;
343 }
344 }
345
346 /**
347 * Fill a marker array with the number of links found in a list of pages
348 *
349 * @param string $curPage Comma separated list of page uids
350 * @return array Marker array with the number of links found
351 */
352 public function getLinkCounts($curPage) {
353 $markerArray = array();
354 if (empty($this->pidList)) {
355 $this->pidList = $curPage;
356 }
357 $this->pidList = rtrim($this->pidList, ',');
358 if (($res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
359 'count(uid) as nbBrokenLinks,link_type',
360 'tx_linkvalidator_link',
361 'record_pid in (' . $this->pidList . ')', 'link_type')
362 )) {
363 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
364 $markerArray[$row['link_type']] = $row['nbBrokenLinks'];
365 $markerArray['brokenlinkCount'] += $row['nbBrokenLinks'];
366 }
367 }
368 $GLOBALS['TYPO3_DB']->sql_free_result($res);
369 return $markerArray;
370 }
371
372 /**
373 * Calls TYPO3\CMS\Backend\FrontendBackendUserAuthentication::extGetTreeList.
374 * Although this duplicates the function TYPO3\CMS\Backend\FrontendBackendUserAuthentication::extGetTreeList
375 * this is necessary to create the object that is used recursively by the original function.
376 *
377 * Generates a list of page uids from $id. List does not include $id itself.
378 * The only pages excluded from the list are deleted pages.
379 *
380 * @param integer $id Start page id
381 * @param integer $depth Depth to traverse down the page tree.
382 * @param integer $begin is an optional integer that determines at which
383 * @param string $permsClause Perms clause
384 * @param boolean $considerHidden Whether to consider hidden pages or not
385 * @return string Returns the list with a comma in the end (if any pages selected!)
386 */
387 public function extGetTreeList($id, $depth, $begin = 0, $permsClause, $considerHidden = FALSE) {
388 $depth = (int)$depth;
389 $begin = (int)$begin;
390 $id = (int)$id;
391 $theList = '';
392 if ($depth > 0) {
393 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
394 'uid,title,hidden,extendToSubpages',
395 'pages', 'pid=' . $id . ' AND deleted=0 AND ' . $permsClause
396 );
397 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
398 if ($begin <= 0 && ($row['hidden'] == 0 || $considerHidden == 1)) {
399 $theList .= $row['uid'] . ',';
400 $this->extPageInTreeInfo[] = array($row['uid'], htmlspecialchars($row['title'], $depth));
401 }
402 if ($depth > 1 && (!($row['hidden'] == 1 && $row['extendToSubpages'] == 1) || $considerHidden == 1)) {
403 $theList .= $this->extGetTreeList($row['uid'], $depth - 1, $begin - 1, $permsClause, $considerHidden);
404 }
405 }
406 $GLOBALS['TYPO3_DB']->sql_free_result($res);
407 }
408 return $theList;
409 }
410
411 /**
412 * Check if rootline contains a hidden page
413 *
414 * @param array $pageInfo Array with uid, title, hidden, extendToSubpages from pages table
415 * @return boolean TRUE if rootline contains a hidden page, FALSE if not
416 */
417 public function getRootLineIsHidden(array $pageInfo) {
418 $hidden = FALSE;
419 if ($pageInfo['extendToSubpages'] == 1 && $pageInfo['hidden'] == 1) {
420 $hidden = TRUE;
421 } else {
422 if ($pageInfo['pid'] > 0) {
423 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('uid,title,hidden,extendToSubpages', 'pages', 'uid=' . $pageInfo['pid']);
424 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
425 $hidden = $this->getRootLineIsHidden($row);
426 }
427 $GLOBALS['TYPO3_DB']->sql_free_result($res);
428 } else {
429 $hidden = FALSE;
430 }
431 }
432 return $hidden;
433 }
434
435 }