2 /***************************************************************
5 * (c) 2010 - 2011 Michael Miousse (michael.miousse@infoglobe.ca)
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.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
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.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
26 * This class provides Processing plugin implementation
28 * @author Michael Miousse <michael.miousse@infoglobe.ca>
29 * @author Jochen Rieger <j.rieger@connecta.ag>
31 * @subpackage linkvalidator
34 $GLOBALS['LANG']->includeLLFile('EXT:linkvalidator/modfuncreport/locallang.xml');
36 class tx_linkvalidator_Processor
{
39 * Array of tables and fields to search for broken links
43 protected $searchFields = array();
46 * List of comma separated page uids (rootline downwards)
50 protected $pidList = '';
53 * Array of tables and the number of external links they contain
57 protected $linkCounts = array();
60 * Array of tables and the number of broken external links they contain
64 protected $brokenLinkCounts = array();
67 * Array of tables and records containing broken links
71 protected $recordsWithBrokenLinks = array();
74 * Array for hooks for own checks
76 * @var tx_linkvalidator_linktype_Abstract[]
78 protected $hookObjectsArr = array();
81 * Array with information about the current page
85 protected $extPageInTreeInfo = array();
88 * Reference to the current element with table:uid, e.g. pages:85
92 protected $recordReference = '';
95 * Linked page together with a possible anchor, e.g. 85#c105
99 protected $pageWithAnchor = '';
102 * Fill hookObjectsArr with different link types and possible XClasses.
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);
114 * Store all the needed configuration values in class variables
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
120 public function init(array $searchField, $pid) {
121 $this->searchFields
= $searchField;
122 $this->pidList
= $pid;
124 foreach ($searchField as $tableName => $table) {
125 t3lib_div
::loadTCA($tableName);
130 * Find all supported broken links and store them in tx_linkvalidator_link
132 * @param array $checkOptions List of hook object to activate
133 * @param boolean $considerHidden Defines whether to look into hidden fields or not
136 public function getLinkStatistics($checkOptions = array(), $considerHidden = FALSE) {
138 if (count($checkOptions) > 0) {
139 $checkKeys = array_keys($checkOptions);
140 $checkLinkTypeCondition = ' and link_type in (\'' . implode('\',\'', $checkKeys) . '\')';
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);
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
. ')';
152 $where = 'deleted = 0 AND pid IN (' . $this->pidList
. ')';
154 if (!$considerHidden) {
155 $where .= t3lib_BEfunc
::BEenableFields($table);
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;
160 // Re-init selectFields for table
161 $selectFields = 'uid, pid';
162 $selectFields .= ', ' . $GLOBALS['TCA'][$table]['ctrl']['label'] . ', ' . implode(', ', $fields);
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);
171 $GLOBALS['TYPO3_DB']->sql_free_result($res);
174 foreach ($this->hookObjectsArr
as $key => $hookObj) {
175 if ((is_array($results[$key])) && empty($checkOptions) ||
(is_array($results[$key]) && $checkOptions[$key])) {
177 foreach ($results[$key] as $entryKey => $entryValue) {
178 $table = $entryValue['table'];
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();
188 $this->recordReference
= $entryValue['substr']['recordRef'];
190 $this->pageWithAnchor
= $entryValue['pageAndAnchor'];
192 if (!empty($this->pageWithAnchor
)) {
193 // Page with anchor, e.g. 18#1580
194 $url = $this->pageWithAnchor
;
196 $url = $entryValue['substr']['tokenValue'];
199 $this->linkCounts
[$table]++
;
200 $checkURL = $hookObj->checkLink($url, $entryValue, $this);
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')) {
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);
228 * Find all supported broken links for a specific record
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
236 public function analyzeRecord(array &$results, $table, array $fields, array $record) {
238 // Put together content of all relevant fields
240 /** @var t3lib_parsehtml $htmlParser */
241 $htmlParser = t3lib_div
::makeInstance('t3lib_parsehtml');
243 $idRecord = $record['uid'];
245 // Get all references
246 foreach ($fields as $field) {
247 $haystack .= $record[$field] . ' --- ';
248 $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
250 $valueField = $record[$field];
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);
261 // If there is an object returned...
262 if (is_object($softRefObj)) {
265 $resultArray = $softRefObj->findRef($table, $field, $idRecord, $valueField, $spKey, $spParams);
266 if (!empty($resultArray['elements'])) {
268 if ($spKey == 'typolink_tag') {
269 $this->analyseTypoLinks($resultArray, $results, $htmlParser, $record, $field, $table);
271 $this->analyseLinks($resultArray, $results, $record, $field, $table);
281 * Find all supported broken links for a specific link list
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
290 protected function analyseLinks(array $resultArray, array &$results, array $record, $field, $table) {
291 foreach ($resultArray['elements'] as $element) {
292 $r = $element['subst'];
294 $idRecord = $record['uid'];
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
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;
316 * Find all supported broken links for a specific typoLink
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
326 protected function analyseTypoLinks(array $resultArray, array &$results, $htmlParser, array $record, $field, $table) {
328 $linkTags = $htmlParser->splitIntoBlock('link', $resultArray['content']);
329 $idRecord = $record['uid'];
332 for ($i = 1; $i < count($linkTags); $i +
= 2) {
333 $referencedRecordType = '';
334 foreach ($resultArray['elements'] as $element) {
336 $r = $element['subst'];
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) {
343 // Contains number of the page
344 $referencedRecordType = $r['tokenValue'];
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'];
354 $title = strip_tags($linkTags[$i]);
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
364 $currentR['type'] = $type;
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;
380 * Fill a marker array with the number of links found in a list of pages
382 * @param string $curPage Comma separated list of page uids
383 * @return array Marker array with the number of links found
385 public function getLinkCounts($curPage) {
386 $markerArray = array();
388 if (empty($this->pidList
)) {
389 $this->pidList
= $curPage;
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
. ')',
399 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
400 $markerArray[$row['link_type']] = $row['nbBrokenLinks'];
401 $markerArray['brokenlinkCount'] +
= $row['nbBrokenLinks'];
404 $GLOBALS['TYPO3_DB']->sql_free_result($res);
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.
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.
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!)
423 public function extGetTreeList($id, $depth, $begin = 0, $permsClause, $considerHidden = FALSE) {
424 $depth = intval($depth);
425 $begin = intval($begin);
430 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
431 'uid,title,hidden,extendToSubpages',
433 'pid=' . $id . ' AND deleted=0 AND ' . $permsClause
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));
441 if ($depth > 1 && (!($row['hidden'] == 1 && $row['extendToSubpages'] == 1) ||
$considerHidden == 1)) {
442 $theList .= $this->extGetTreeList($row['uid'], $depth - 1, $begin - 1, $permsClause, $considerHidden);
445 $GLOBALS['TYPO3_DB']->sql_free_result($res);
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
454 public function getRootLineIsHidden(array $pageInfo) {
456 if ($pageInfo['extendToSubpages'] == 1 && $pageInfo['hidden'] == 1) {
459 if ($pageInfo['pid'] > 0) {
460 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
461 'uid,title,hidden,extendToSubpages',
463 'uid=' . $pageInfo['pid']
466 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
467 $hidden = $this->getRootLineIsHidden($row);
469 $GLOBALS['TYPO3_DB']->sql_free_result($res);
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']);