4b6017760176e2cfc4b8e2837ca3bee5ed6df484
[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 array
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 * Fill hookObjectsArr with different link types and possible XClasses.
89 */
90 public function __construct() {
91 // Hook to handle own checks
92 if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'])) {
93 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'] as $key => $classRef) {
94 $this->hookObjectsArr[$key] = &t3lib_div::getUserObj($classRef);
95 }
96 }
97 }
98
99 /**
100 * Store all the needed configuration values in class variables
101 *
102 * @param array $searchField List of fields in which to search for links
103 * @param string $pid List of comma separated page uids in which to search for links
104 * @return void
105 */
106 public function init(array $searchField, $pid) {
107 $this->searchFields = $searchField;
108 $this->pidList = $pid;
109
110 foreach ($searchField as $tableName => $table) {
111 t3lib_div::loadTCA($tableName);
112 }
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 or not
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
128 $GLOBALS['TYPO3_DB']->exec_DELETEquery('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 .= t3lib_BEfunc::BEenableFields($table);
142 }
143 // If table is not configured, assume the extension is not installed and therefore no need to check it
144 if (!is_array($GLOBALS['TCA'][$table])) continue;
145
146 // Re-init selectFields for table
147 $selectFields = 'uid, pid';
148 $selectFields .= ', ' . $GLOBALS['TCA'][$table]['ctrl']['label'] . ', ' . implode(', ', $fields);
149
150 // TODO: only select rows that have content in at least one of the relevant fields (via OR)
151 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selectFields, $table, $where);
152 // Get record rows of table
153 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
154 // Analyse each record
155 $this->analyzeRecord($results, $table, $fields, $row);
156 }
157 $GLOBALS['TYPO3_DB']->sql_free_result($res);
158 }
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
174 $this->recordReference = $entryValue['substr']['recordRef'];
175
176 $this->pageWithAnchor = $entryValue['pageAndAnchor'];
177
178 if (!empty($this->pageWithAnchor)) {
179 // Page with anchor, e.g. 18#1580
180 $url = $this->pageWithAnchor;
181 } else {
182 $url = $entryValue['substr']['tokenValue'];
183 }
184
185 $this->linkCounts[$table]++;
186 $checkURL = $hookObj->checkLink($url, $entryValue, $this);
187 // Broken link found
188 if (!$checkURL) {
189 $response = array();
190 $response['valid'] = FALSE;
191 $response['errorParams'] = $hookObj->getErrorParams();
192 $this->brokenLinkCounts[$table]++;
193 $record['link_type'] = $key;
194 $record['url'] = $url;
195 $record['url_response'] = serialize($response);
196 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
197 } elseif (t3lib_div::_GP('showalllinks')) {
198 $response = array();
199 $response['valid'] = TRUE;
200 $this->brokenLinkCounts[$table]++;
201 $record['url'] = $url;
202 $record['link_type'] = $key;
203 $record['url_response'] = serialize($response);
204 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
205 }
206 }
207 }
208 }
209 }
210 }
211
212
213 /**
214 * Find all supported broken links for a specific record
215 *
216 * @param array $results Array of broken links
217 * @param string $table Table name of the record
218 * @param array $fields Array of fields to analyze
219 * @param array $record Record to analyse
220 * @return void
221 */
222 public function analyzeRecord(array &$results, $table, array $fields, array $record) {
223
224 // Array to store urls from relevant field contents
225 $urls = array();
226
227 $referencedRecordType = '';
228 // Last-parsed link element was a page
229 $wasPage = TRUE;
230
231 // Flag whether row contains a broken link in some field or not
232 $rowContainsBrokenLink = FALSE;
233
234 // Put together content of all relevant fields
235 $haystack = '';
236 $htmlParser = t3lib_div::makeInstance('t3lib_parsehtml');
237
238 $idRecord = $record['uid'];
239
240 // Get all references
241 foreach ($fields as $field) {
242 $haystack .= $record[$field] . ' --- ';
243 $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
244
245 $valueField = $record[$field];
246
247 // Check if a TCA configured field has soft references defined (see TYPO3 Core API document)
248 if ($conf['softref'] && strlen($valueField)) {
249 // Explode the list of soft references/parameters
250 $softRefs = t3lib_BEfunc::explodeSoftRefParserList($conf['softref']);
251 // Traverse soft references
252 foreach ($softRefs as $spKey => $spParams) {
253 // create / get object
254 $softRefObj = &t3lib_BEfunc::softRefParserObj($spKey);
255
256 // If there is an object returned...
257 if (is_object($softRefObj)) {
258
259 // Do processing
260 $resultArray = $softRefObj->findRef($table, $field, $idRecord, $valueField, $spKey, $spParams);
261 if (!empty($resultArray['elements'])) {
262
263 if ($spKey == 'typolink_tag') {
264 $this->analyseTypoLinks($resultArray, $results, $htmlParser, $record, $field, $table);
265 } else {
266 $this->analyseLinks($resultArray, $results, $record, $field, $table);
267 }
268 }
269 }
270 }
271 }
272 }
273 }
274
275 /**
276 * Find all supported broken links for a specific link list
277 *
278 * @param array $resultArray findRef parsed records
279 * @param array $results Array of broken links
280 * @param array $record UID of the current record
281 * @param string $field The current field
282 * @param string $table The current table
283 * @return void
284 */
285 private function analyseLinks(array $resultArray, array &$results, array $record, $field, $table) {
286 foreach ($resultArray['elements'] as $element) {
287 $r = $element['subst'];
288 $type = '';
289 $idRecord = $record['uid'];
290 if (!empty($r)) {
291 // Parse string for special TYPO3 <link> tag:
292 foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
293 $type = $hookObj->fetchType($r, $type, $keyArr);
294 }
295 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["substr"] = $r;
296 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["row"] = $record;
297 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["table"] = $table;
298 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["field"] = $field;
299 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["uid"] = $idRecord;
300
301 }
302 }
303 }
304
305 /**
306 * Find all supported broken links for a specific typoLink
307 *
308 * @param array $resultArray findRef parsed records
309 * @param array $results Array of broken links
310 * @param t3lib_parsehtml $htmlParser Instance of html parser
311 * @param array $record The current record
312 * @param string $field The current field
313 * @param string $table The current table
314 * @return void
315 */
316 private function analyseTypoLinks(array $resultArray, array &$results, $htmlParser, array $record, $field, $table) {
317 $currentR = array();
318 $linkTags = $htmlParser->splitIntoBlock('link', $resultArray['content']);
319 $idRecord = $record['uid'];
320 for ($i = 1; $i < count($linkTags); $i += 2) {
321 $referencedRecordType = '';
322 foreach ($resultArray['elements'] as $element) {
323 $type = '';
324 $r = $element['subst'];
325
326 if (!empty($r['tokenID'])) {
327 if (substr_count($linkTags[$i], $r['tokenID'])) {
328 // Type of referenced record
329 if (strpos($r['recordRef'], 'pages') !== FALSE) {
330 $currentR = $r;
331 // Contains number of the page
332 $referencedRecordType = $r['tokenValue'];
333 $wasPage = TRUE;
334
335 // Append number of content element to the page saved in the last loop
336 } elseif ((strpos($r['recordRef'], 'tt_content') !== FALSE) && (isset($wasPage) && $wasPage === TRUE)) {
337 $referencedRecordType = $referencedRecordType . '#c' . $r['tokenValue'];
338 $wasPage = FALSE;
339 } else {
340 $currentR = $r;
341 }
342 $title = strip_tags($linkTags[$i]);
343 }
344 }
345 }
346 foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
347 $type = $hookObj->fetchType($currentR, $type, $keyArr);
348 }
349
350 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["substr"] = $currentR;
351 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["row"] = $record;
352 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["table"] = $table;
353 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["field"] = $field;
354 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["uid"] = $idRecord;
355 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["link_title"] = $title;
356 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["pageAndAnchor"] = $referencedRecordType;
357
358 }
359 }
360
361 /**
362 * Fill a marker array with the number of links found in a list of pages
363 *
364 * @param string $curPage Comma separated list of page uids
365 * @return array Marker array with the number of links found
366 */
367 public function getLinkCounts($curPage) {
368 $markerArray = array();
369
370 if (empty($this->pidList)) {
371 $this->pidList = $curPage;
372 }
373
374 if (($res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
375 'count(uid) as nbBrokenLinks,link_type',
376 'tx_linkvalidator_link',
377 'record_pid in (' . $this->pidList . ')',
378 'link_type'
379 ))
380 ) {
381 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
382 $markerArray[$row['link_type']] = $row['nbBrokenLinks'];
383 $markerArray['brokenlinkCount'] += $row['nbBrokenLinks'];
384 }
385 }
386 $GLOBALS['TYPO3_DB']->sql_free_result($res);
387 return $markerArray;
388 }
389
390 /**
391 * Calls t3lib_tsfeBeUserAuth::extGetTreeList.
392 * Although this duplicates the function t3lib_tsfeBeUserAuth::extGetTreeList
393 * this is necessary to create the object that is used recursively by the original function.
394 *
395 * Generates a list of page uids from $id. List does not include $id itself.
396 * The only pages excluded from the list are deleted pages.
397 *
398 * @param integer $id Start page id
399 * @param integer $depth Depth to traverse down the page tree.
400 * @param integer $begin is an optional integer that determines at which
401 * @param string $permsClause Perms clause
402 * @param boolean $considerHidden Whether to consider hidden pages or not
403 * @return string Returns the list with a comma in the end (if any pages selected!)
404 */
405 public function extGetTreeList($id, $depth, $begin = 0, $permsClause, $considerHidden = FALSE) {
406 $depth = intval($depth);
407 $begin = intval($begin);
408 $id = intval($id);
409 $theList = '';
410
411 if ($depth > 0) {
412 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
413 'uid,title,hidden,extendToSubpages',
414 'pages',
415 'pid=' . $id . ' AND deleted=0 AND ' . $permsClause
416 );
417
418 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
419 if ($begin <= 0 && ($row['hidden'] == 0 || $considerHidden == 1)) {
420 $theList .= $row['uid'] . ',';
421 $this->extPageInTreeInfo[] = array($row['uid'], htmlspecialchars($row['title'], $depth));
422 }
423 if ($depth > 1 && (!($row['hidden'] == 1 && $row['extendToSubpages'] == 1) || $considerHidden == 1)) {
424 $theList .= $this->extGetTreeList($row['uid'], $depth - 1, $begin - 1, $permsClause, $considerHidden);
425 }
426 }
427 }
428 $GLOBALS['TYPO3_DB']->sql_free_result($res);
429 return $theList;
430 }
431
432 /**
433 * @param array $pageInfo Array with uid, title, hidden, extendToSubpages from pages table
434 * @return boolean TRUE if rootline contains a hidden page, FALSE if not
435 */
436 public function getRootLineIsHidden(array $pageInfo) {
437 $hidden = FALSE;
438 if ($pageInfo['extendToSubpages'] == 1 && $pageInfo['hidden'] == 1) {
439 $hidden = TRUE;
440 } else {
441 if ($pageInfo['pid'] > 0) {
442 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
443 'uid,title,hidden,extendToSubpages',
444 'pages',
445 'uid=' . $pageInfo['pid']
446 );
447
448 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
449 $hidden = $this->getRootLineIsHidden($row);
450 }
451 $GLOBALS['TYPO3_DB']->sql_free_result($res);
452 } else {
453 $hidden = FALSE;
454 }
455 }
456 return $hidden;
457
458 }
459
460 }
461
462 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/linkvalidator/classes/class.tx_linkvalidator_processor.php'])) {
463 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/linkvalidator/classes/class.tx_linkvalidator_processor.php']);
464 }
465 ?>