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