[BUGFIX] Custom validators may be overriden by default validators
[Packages/TYPO3.CMS.git] / typo3 / sysext / linkvalidator / classes / class.tx_linkvalidator_processor.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 - 2011 Michael Miousse (michael.miousse@infoglobe.ca)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * This class provides Processing plugin implementation
27 *
28 * @author Michael Miousse <michael.miousse@infoglobe.ca>
29 * @author Jochen Rieger <j.rieger@connecta.ag>
30 * @package TYPO3
31 * @subpackage linkvalidator
32 */
33
34 $GLOBALS['LANG']->includeLLFile('EXT:linkvalidator/modfuncreport/locallang.xml');
35
36 class tx_linkvalidator_Processor {
37
38 /**
39 * Array of tables and fields to search for broken links
40 *
41 * @var array
42 */
43 protected $searchFields = array();
44
45 /**
46 * List of comma separated page uids (rootline downwards)
47 *
48 * @var string
49 */
50 protected $pidList = '';
51
52 /**
53 * Array of tables and the number of external links they contain
54 *
55 * @var array
56 */
57 protected $linkCounts = array();
58
59 /**
60 * Array of tables and the number of broken external links they contain
61 *
62 * @var array
63 */
64 protected $brokenLinkCounts = array();
65
66 /**
67 * Array of tables and records containing broken links
68 *
69 * @var array
70 */
71 protected $recordsWithBrokenLinks = array();
72
73 /**
74 * Array for hooks for own checks
75 *
76 * @var tx_linkvalidator_linktype_Abstract[]
77 */
78 protected $hookObjectsArr = array();
79
80 /**
81 * Array with information about the current page
82 *
83 * @var array
84 */
85 protected $extPageInTreeInfo = array();
86
87 /**
88 * Reference to the current element with table:uid, e.g. pages:85
89 *
90 * @var string
91 */
92 protected $recordReference = '';
93
94 /**
95 * Linked page together with a possible anchor, e.g. 85#c105
96 *
97 * @var string
98 */
99 protected $pageWithAnchor = '';
100
101 /**
102 * Fill hookObjectsArr with different link types and possible XClasses.
103 */
104 public function __construct() {
105 // Hook to handle own checks
106 if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'])) {
107 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'] as $key => $classRef) {
108 $this->hookObjectsArr[$key] = &t3lib_div::getUserObj($classRef);
109 }
110 }
111 }
112
113 /**
114 * Store all the needed configuration values in class variables
115 *
116 * @param array $searchField List of fields in which to search for links
117 * @param string $pid List of comma separated page uids in which to search for links
118 * @return void
119 */
120 public function init(array $searchField, $pid) {
121 $this->searchFields = $searchField;
122 $this->pidList = $pid;
123
124 foreach ($searchField as $tableName => $table) {
125 t3lib_div::loadTCA($tableName);
126 }
127 }
128
129 /**
130 * Find all supported broken links and store them in tx_linkvalidator_link
131 *
132 * @param array $checkOptions List of hook object to activate
133 * @param boolean $considerHidden Defines whether to look into hidden fields or not
134 * @return void
135 */
136 public function getLinkStatistics($checkOptions = array(), $considerHidden = FALSE) {
137 $results = array();
138 if (count($checkOptions) > 0) {
139 $checkKeys = array_keys($checkOptions);
140 $checkLinkTypeCondition = ' and link_type in (\'' . implode('\',\'', $checkKeys) . '\')';
141
142 $GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_linkvalidator_link',
143 '(record_pid in (' . $this->pidList . ')'
144 . ' or ( record_uid IN (' . $this->pidList . ') and table_name like \'pages\')) '
145 . $checkLinkTypeCondition);
146
147 // Traverse all configured tables
148 foreach ($this->searchFields as $table => $fields) {
149 if ($table === 'pages') {
150 $where = 'deleted = 0 AND uid IN (' . $this->pidList . ')';
151 } else {
152 $where = 'deleted = 0 AND pid IN (' . $this->pidList . ')';
153 }
154 if (!$considerHidden) {
155 $where .= t3lib_BEfunc::BEenableFields($table);
156 }
157 // If table is not configured, assume the extension is not installed and therefore no need to check it
158 if (!is_array($GLOBALS['TCA'][$table])) continue;
159
160 // Re-init selectFields for table
161 $selectFields = 'uid, pid';
162 $selectFields .= ', ' . $GLOBALS['TCA'][$table]['ctrl']['label'] . ', ' . implode(', ', $fields);
163
164 // TODO: only select rows that have content in at least one of the relevant fields (via OR)
165 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selectFields, $table, $where);
166 // Get record rows of table
167 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
168 // Analyse each record
169 $this->analyzeRecord($results, $table, $fields, $row);
170 }
171 $GLOBALS['TYPO3_DB']->sql_free_result($res);
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 = array();
180 $record['headline'] = $entryValue['row'][$GLOBALS['TCA'][$table]['ctrl']['label']];
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
188 $this->recordReference = $entryValue['substr']['recordRef'];
189
190 $this->pageWithAnchor = $entryValue['pageAndAnchor'];
191
192 if (!empty($this->pageWithAnchor)) {
193 // Page with anchor, e.g. 18#1580
194 $url = $this->pageWithAnchor;
195 } else {
196 $url = $entryValue['substr']['tokenValue'];
197 }
198
199 $this->linkCounts[$table]++;
200 $checkURL = $hookObj->checkLink($url, $entryValue, $this);
201 // Broken link found
202 if (!$checkURL) {
203 $response = array();
204 $response['valid'] = FALSE;
205 $response['errorParams'] = $hookObj->getErrorParams();
206 $this->brokenLinkCounts[$table]++;
207 $record['link_type'] = $key;
208 $record['url'] = $url;
209 $record['url_response'] = serialize($response);
210 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
211 } elseif (t3lib_div::_GP('showalllinks')) {
212 $response = array();
213 $response['valid'] = TRUE;
214 $this->brokenLinkCounts[$table]++;
215 $record['url'] = $url;
216 $record['link_type'] = $key;
217 $record['url_response'] = serialize($response);
218 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
219 }
220 }
221 }
222 }
223 }
224 }
225
226
227 /**
228 * Find all supported broken links for a specific record
229 *
230 * @param array $results Array of broken links
231 * @param string $table Table name of the record
232 * @param array $fields Array of fields to analyze
233 * @param array $record Record to analyse
234 * @return void
235 */
236 public function analyzeRecord(array &$results, $table, array $fields, array $record) {
237
238 // Put together content of all relevant fields
239 $haystack = '';
240 /** @var t3lib_parsehtml $htmlParser */
241 $htmlParser = t3lib_div::makeInstance('t3lib_parsehtml');
242
243 $idRecord = $record['uid'];
244
245 // Get all references
246 foreach ($fields as $field) {
247 $haystack .= $record[$field] . ' --- ';
248 $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
249
250 $valueField = $record[$field];
251
252 // Check if a TCA configured field has soft references defined (see TYPO3 Core API document)
253 if ($conf['softref'] && strlen($valueField)) {
254 // Explode the list of soft references/parameters
255 $softRefs = t3lib_BEfunc::explodeSoftRefParserList($conf['softref']);
256 // Traverse soft references
257 foreach ($softRefs as $spKey => $spParams) {
258 /** @var t3lib_softrefproc $softRefObj Create or get the soft reference object */
259 $softRefObj = &t3lib_BEfunc::softRefParserObj($spKey);
260
261 // If there is an object returned...
262 if (is_object($softRefObj)) {
263
264 // Do processing
265 $resultArray = $softRefObj->findRef($table, $field, $idRecord, $valueField, $spKey, $spParams);
266 if (!empty($resultArray['elements'])) {
267
268 if ($spKey == 'typolink_tag') {
269 $this->analyseTypoLinks($resultArray, $results, $htmlParser, $record, $field, $table);
270 } else {
271 $this->analyseLinks($resultArray, $results, $record, $field, $table);
272 }
273 }
274 }
275 }
276 }
277 }
278 }
279
280 /**
281 * Find all supported broken links for a specific link list
282 *
283 * @param array $resultArray findRef parsed records
284 * @param array $results Array of broken links
285 * @param array $record UID of the current record
286 * @param string $field The current field
287 * @param string $table The current table
288 * @return void
289 */
290 protected function analyseLinks(array $resultArray, array &$results, array $record, $field, $table) {
291 foreach ($resultArray['elements'] as $element) {
292 $r = $element['subst'];
293 $type = '';
294 $idRecord = $record['uid'];
295 if (!empty($r)) {
296 /** @var tx_linkvalidator_linktype_Abstract $hookObj */
297 foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
298 $type = $hookObj->fetchType($r, $type, $keyArr);
299 // Store the type that was found
300 // This prevents overriding by internal validator
301 if (!empty($type)) {
302 $r['type'] = $type;
303 }
304 }
305 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["substr"] = $r;
306 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["row"] = $record;
307 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["table"] = $table;
308 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["field"] = $field;
309 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["uid"] = $idRecord;
310
311 }
312 }
313 }
314
315 /**
316 * Find all supported broken links for a specific typoLink
317 *
318 * @param array $resultArray findRef parsed records
319 * @param array $results Array of broken links
320 * @param t3lib_parsehtml $htmlParser Instance of html parser
321 * @param array $record The current record
322 * @param string $field The current field
323 * @param string $table The current table
324 * @return void
325 */
326 protected function analyseTypoLinks(array $resultArray, array &$results, $htmlParser, array $record, $field, $table) {
327 $currentR = array();
328 $linkTags = $htmlParser->splitIntoBlock('link', $resultArray['content']);
329 $idRecord = $record['uid'];
330 $type = '';
331 $title = '';
332 for ($i = 1; $i < count($linkTags); $i += 2) {
333 $referencedRecordType = '';
334 foreach ($resultArray['elements'] as $element) {
335 $type = '';
336 $r = $element['subst'];
337
338 if (!empty($r['tokenID'])) {
339 if (substr_count($linkTags[$i], $r['tokenID'])) {
340 // Type of referenced record
341 if (strpos($r['recordRef'], 'pages') !== FALSE) {
342 $currentR = $r;
343 // Contains number of the page
344 $referencedRecordType = $r['tokenValue'];
345 $wasPage = TRUE;
346
347 // Append number of content element to the page saved in the last loop
348 } elseif ((strpos($r['recordRef'], 'tt_content') !== FALSE) && (isset($wasPage) && $wasPage === TRUE)) {
349 $referencedRecordType = $referencedRecordType . '#c' . $r['tokenValue'];
350 $wasPage = FALSE;
351 } else {
352 $currentR = $r;
353 }
354 $title = strip_tags($linkTags[$i]);
355 }
356 }
357 }
358 /** @var tx_linkvalidator_linktype_Abstract $hookObj */
359 foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
360 $type = $hookObj->fetchType($currentR, $type, $keyArr);
361 // Store the type that was found
362 // This prevents overriding by internal validator
363 if (!empty($type)) {
364 $currentR['type'] = $type;
365 }
366 }
367
368 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["substr"] = $currentR;
369 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["row"] = $record;
370 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["table"] = $table;
371 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["field"] = $field;
372 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["uid"] = $idRecord;
373 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["link_title"] = $title;
374 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["pageAndAnchor"] = $referencedRecordType;
375
376 }
377 }
378
379 /**
380 * Fill a marker array with the number of links found in a list of pages
381 *
382 * @param string $curPage Comma separated list of page uids
383 * @return array Marker array with the number of links found
384 */
385 public function getLinkCounts($curPage) {
386 $markerArray = array();
387
388 if (empty($this->pidList)) {
389 $this->pidList = $curPage;
390 }
391
392 if (($res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
393 'count(uid) as nbBrokenLinks,link_type',
394 'tx_linkvalidator_link',
395 'record_pid in (' . $this->pidList . ')',
396 'link_type'
397 ))
398 ) {
399 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
400 $markerArray[$row['link_type']] = $row['nbBrokenLinks'];
401 $markerArray['brokenlinkCount'] += $row['nbBrokenLinks'];
402 }
403 }
404 $GLOBALS['TYPO3_DB']->sql_free_result($res);
405 return $markerArray;
406 }
407
408 /**
409 * Calls t3lib_tsfeBeUserAuth::extGetTreeList.
410 * Although this duplicates the function t3lib_tsfeBeUserAuth::extGetTreeList
411 * this is necessary to create the object that is used recursively by the original function.
412 *
413 * Generates a list of page uids from $id. List does not include $id itself.
414 * The only pages excluded from the list are deleted pages.
415 *
416 * @param integer $id Start page id
417 * @param integer $depth Depth to traverse down the page tree.
418 * @param integer $begin is an optional integer that determines at which
419 * @param string $permsClause Perms clause
420 * @param boolean $considerHidden Whether to consider hidden pages or not
421 * @return string Returns the list with a comma in the end (if any pages selected!)
422 */
423 public function extGetTreeList($id, $depth, $begin = 0, $permsClause, $considerHidden = FALSE) {
424 $depth = intval($depth);
425 $begin = intval($begin);
426 $id = intval($id);
427 $theList = '';
428
429 if ($depth > 0) {
430 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
431 'uid,title,hidden,extendToSubpages',
432 'pages',
433 'pid=' . $id . ' AND deleted=0 AND ' . $permsClause
434 );
435
436 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
437 if ($begin <= 0 && ($row['hidden'] == 0 || $considerHidden == 1)) {
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 == 1)) {
442 $theList .= $this->extGetTreeList($row['uid'], $depth - 1, $begin - 1, $permsClause, $considerHidden);
443 }
444 }
445 $GLOBALS['TYPO3_DB']->sql_free_result($res);
446 }
447 return $theList;
448 }
449
450 /**
451 * @param array $pageInfo Array with uid, title, hidden, extendToSubpages from pages table
452 * @return boolean TRUE if rootline contains a hidden page, FALSE if not
453 */
454 public function getRootLineIsHidden(array $pageInfo) {
455 $hidden = FALSE;
456 if ($pageInfo['extendToSubpages'] == 1 && $pageInfo['hidden'] == 1) {
457 $hidden = TRUE;
458 } else {
459 if ($pageInfo['pid'] > 0) {
460 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
461 'uid,title,hidden,extendToSubpages',
462 'pages',
463 'uid=' . $pageInfo['pid']
464 );
465
466 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
467 $hidden = $this->getRootLineIsHidden($row);
468 }
469 $GLOBALS['TYPO3_DB']->sql_free_result($res);
470 } else {
471 $hidden = FALSE;
472 }
473 }
474 return $hidden;
475
476 }
477
478 }
479
480 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/linkvalidator/classes/class.tx_linkvalidator_processor.php'])) {
481 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/linkvalidator/classes/class.tx_linkvalidator_processor.php']);
482 }
483 ?>