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 ***************************************************************/
25 $GLOBALS['LANG']->includeLLFile('EXT:linkvalidator/modfuncreport/locallang.xml');
28 * This class provides Processing plugin implementation
30 * @author Michael Miousse <michael.miousse@infoglobe.ca>
31 * @author Jochen Rieger <j.rieger@connecta.ag>
33 * @subpackage linkvalidator
35 class tx_linkvalidator_Processor
{
38 * Array of tables and fields to search for broken links
42 protected $searchFields = array();
45 * List of comma separated page uids (rootline downwards)
49 protected $pidList = '';
52 * Array of tables and the number of external links they contain
56 protected $linkCounts = array();
59 * Array of tables and the number of broken external links they contain
63 protected $brokenLinkCounts = array();
66 * Array of tables and records containing broken links
70 protected $recordsWithBrokenLinks = array();
73 * Array for hooks for own checks
75 * @var tx_linkvalidator_linktype_Abstract[]
77 protected $hookObjectsArr = array();
80 * Array with information about the current page
84 protected $extPageInTreeInfo = array();
87 * Reference to the current element with table:uid, e.g. pages:85
91 protected $recordReference = '';
94 * Linked page together with a possible anchor, e.g. 85#c105
98 protected $pageWithAnchor = '';
101 * Fill hookObjectsArr with different link types and possible XClasses.
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);
113 * Store all the needed configuration values in class variables
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
119 public function init(array $searchField, $pid) {
120 $this->searchFields
= $searchField;
121 $this->pidList
= $pid;
123 foreach ($searchField as $tableName => $table) {
124 t3lib_div
::loadTCA($tableName);
129 * Find all supported broken links and store them in tx_linkvalidator_link
131 * @param array $checkOptions List of hook object to activate
132 * @param boolean $considerHidden Defines whether to look into hidden fields or not
135 public function getLinkStatistics($checkOptions = array(), $considerHidden = FALSE) {
137 if (count($checkOptions) > 0) {
138 $checkKeys = array_keys($checkOptions);
139 $checkLinkTypeCondition = ' and link_type in (\'' . implode('\',\'', $checkKeys) . '\')';
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);
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
. ')';
151 $where = 'deleted = 0 AND pid IN (' . $this->pidList
. ')';
153 if (!$considerHidden) {
154 $where .= t3lib_BEfunc
::BEenableFields($table);
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;
159 // Re-init selectFields for table
160 $selectFields = 'uid, pid';
161 $selectFields .= ', ' . $GLOBALS['TCA'][$table]['ctrl']['label'] . ', ' . implode(', ', $fields);
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);
170 $GLOBALS['TYPO3_DB']->sql_free_result($res);
173 foreach ($this->hookObjectsArr
as $key => $hookObj) {
174 if ((is_array($results[$key])) && empty($checkOptions) ||
(is_array($results[$key]) && $checkOptions[$key])) {
176 foreach ($results[$key] as $entryKey => $entryValue) {
177 $table = $entryValue['table'];
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();
187 $this->recordReference
= $entryValue['substr']['recordRef'];
189 $this->pageWithAnchor
= $entryValue['pageAndAnchor'];
191 if (!empty($this->pageWithAnchor
)) {
192 // Page with anchor, e.g. 18#1580
193 $url = $this->pageWithAnchor
;
195 $url = $entryValue['substr']['tokenValue'];
198 $this->linkCounts
[$table]++
;
199 $checkURL = $hookObj->checkLink($url, $entryValue, $this);
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')) {
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);
227 * Find all supported broken links for a specific record
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
235 public function analyzeRecord(array &$results, $table, array $fields, array $record) {
237 // Put together content of all relevant fields
239 /** @var t3lib_parsehtml $htmlParser */
240 $htmlParser = t3lib_div
::makeInstance('t3lib_parsehtml');
242 $idRecord = $record['uid'];
244 // Get all references
245 foreach ($fields as $field) {
246 $haystack .= $record[$field] . ' --- ';
247 $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
249 $valueField = $record[$field];
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);
260 // If there is an object returned...
261 if (is_object($softRefObj)) {
264 $resultArray = $softRefObj->findRef($table, $field, $idRecord, $valueField, $spKey, $spParams);
265 if (!empty($resultArray['elements'])) {
267 if ($spKey == 'typolink_tag') {
268 $this->analyseTypoLinks($resultArray, $results, $htmlParser, $record, $field, $table);
270 $this->analyseLinks($resultArray, $results, $record, $field, $table);
280 * Find all supported broken links for a specific link list
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
289 protected function analyseLinks(array $resultArray, array &$results, array $record, $field, $table) {
290 foreach ($resultArray['elements'] as $element) {
291 $r = $element['subst'];
293 $idRecord = $record['uid'];
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
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;
315 * Find all supported broken links for a specific typoLink
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
325 protected function analyseTypoLinks(array $resultArray, array &$results, $htmlParser, array $record, $field, $table) {
327 $linkTags = $htmlParser->splitIntoBlock('link', $resultArray['content']);
328 $idRecord = $record['uid'];
331 for ($i = 1; $i < count($linkTags); $i +
= 2) {
332 $referencedRecordType = '';
333 foreach ($resultArray['elements'] as $element) {
335 $r = $element['subst'];
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) {
342 // Contains number of the page
343 $referencedRecordType = $r['tokenValue'];
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'];
353 $title = strip_tags($linkTags[$i]);
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
363 $currentR['type'] = $type;
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;
379 * Fill a marker array with the number of links found in a list of pages
381 * @param string $curPage Comma separated list of page uids
382 * @return array Marker array with the number of links found
384 public function getLinkCounts($curPage) {
385 $markerArray = array();
387 if (empty($this->pidList
)) {
388 $this->pidList
= $curPage;
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
. ')',
398 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
399 $markerArray[$row['link_type']] = $row['nbBrokenLinks'];
400 $markerArray['brokenlinkCount'] +
= $row['nbBrokenLinks'];
403 $GLOBALS['TYPO3_DB']->sql_free_result($res);
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.
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.
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!)
422 public function extGetTreeList($id, $depth, $begin = 0, $permsClause, $considerHidden = FALSE) {
423 $depth = intval($depth);
424 $begin = intval($begin);
429 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
430 'uid,title,hidden,extendToSubpages',
432 'pid=' . $id . ' AND deleted=0 AND ' . $permsClause
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));
440 if ($depth > 1 && (!($row['hidden'] == 1 && $row['extendToSubpages'] == 1) ||
$considerHidden == 1)) {
441 $theList .= $this->extGetTreeList($row['uid'], $depth - 1, $begin - 1, $permsClause, $considerHidden);
444 $GLOBALS['TYPO3_DB']->sql_free_result($res);
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
453 public function getRootLineIsHidden(array $pageInfo) {
455 if ($pageInfo['extendToSubpages'] == 1 && $pageInfo['hidden'] == 1) {
458 if ($pageInfo['pid'] > 0) {
459 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
460 'uid,title,hidden,extendToSubpages',
462 'uid=' . $pageInfo['pid']
465 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) !== FALSE) {
466 $hidden = $this->getRootLineIsHidden($row);
468 $GLOBALS['TYPO3_DB']->sql_free_result($res);