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 seperated 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.
78 protected $hookObjectsArr = array();
81 * Array with information about the current page.
85 protected $extPageInTreeInfo = array();
88 * Fill hookObjectsArr with different link types and possible XClasses.
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);
100 * Init Function: Here all the needed configuration values are stored in class variables.
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
106 public function init($searchField, $pid) {
107 $this->searchFields
= $searchField;
108 $this->pidList
= $pid;
112 * Find all supported broken links and store them in tx_linkvalidator_link.
114 * @param array $checkOptions: list of hook object to activate
115 * @param boolean $considerHidden: defines whether to look into hidden fields or not
118 public function getLinkStatistics($checkOptions = array(), $considerHidden = FALSE) {
120 $checlLinkTypeCondition = '';
121 if(count($checkOptions) > 0) {
122 $checkKeys = array_keys($checkOptions);
123 $checlLinkTypeCondition = ' and link_type in (\'' . implode('\',\'',$checkKeys) . '\')';
125 $GLOBALS['TYPO3_DB']->exec_DELETEquery('tx_linkvalidator_link', '(record_pid in (' . $this->pidList
. ') or ( record_uid IN (' . $this->pidList
. ') and table_name like \'pages\')) ' . $checlLinkTypeCondition);
127 // let's traverse all configured tables
128 foreach ($this->searchFields
as $table => $fields) {
129 if($table == 'pages'){
130 $where = 'deleted = 0 AND uid IN (' . $this->pidList
. ')';
133 $where = 'deleted = 0 AND pid IN (' . $this->pidList
. ')';
135 if (!$considerHidden) {
136 $where .= t3lib_BEfunc
::BEenableFields($table);
138 // if table is not configured, we assume the ext is not installed and therefore no need to check it
139 if (!is_array($GLOBALS['TCA'][$table])) continue;
141 // re-init selectFields for table
142 $selectFields = 'uid, pid';
143 $selectFields .= ', ' . $GLOBALS['TCA'][$table]['ctrl']['label'] . ', ' . implode(', ', $fields);
145 // TODO: only select rows that have content in at least one of the relevant fields (via OR)
146 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selectFields, $table, $where);
147 // Get record rows of table
148 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
149 // Analyse each record
150 $this->analyzeRecord($results, $table, $fields, $row);
154 foreach ($this->hookObjectsArr
as $key => $hookObj) {
155 if ((is_array($results[$key])) && empty($checkOptions) ||
(is_array($results[$key]) && $checkOptions[$key])) {
157 foreach ($results[$key] as $entryKey => $entryValue) {
158 $table = $entryValue['table'];
160 $record['headline'] = $entryValue['row'][$GLOBALS['TCA'][$table]['ctrl']['label']];
161 $record['record_pid'] = $entryValue['row']['pid'];
162 $record['record_uid'] = $entryValue['uid'];
163 $record['table_name'] = $table;
164 $record['link_title'] = $entryValue['link_title'];
165 $record['field'] = $entryValue['field'];
166 $record['last_check'] = time();
168 $this->recordReference
= $entryValue['substr']['recordRef'];
170 $this->pageWithAnchor
= $entryValue['pageAndAnchor'];
172 if (!empty($this->pageWithAnchor
)) {
173 // page with anchor, e.g. 18#1580
174 $url = $this->pageWithAnchor
;
176 $url = $entryValue['substr']['tokenValue'];
179 $this->linkCounts
[$table]++
;
180 $checkURL = $hookObj->checkLink($url, $entryValue, $this);
181 // broken link found!
184 $response['valid'] = FALSE;
185 $response['errorParams'] = $hookObj->getErrorParams();
186 $this->brokenLinkCounts
[$table]++
;
187 $record['link_type'] = $key;
188 $record['url'] = $url;
189 $record['url_response'] = serialize($response);
190 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
191 } elseif (t3lib_div
::_GP('showalllinks')) {
193 $response['valid'] = TRUE;
194 $this->brokenLinkCounts
[$table]++
;
195 $record['url'] = $url;
196 $record['link_type'] = $key;
197 $record['url_response'] = serialize($response);
198 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
208 * Find all supported broken links for a specific record.
210 * @param array $results: array of broken links
211 * @param string $table: table name of the record
212 * @param array $fields: array of fields to analyze
213 * @param array $record: record to analyse
216 public function analyzeRecord(&$results, $table, $fields, $record) {
218 // array to store urls from relevant field contents
221 $referencedRecordType = '';
222 // last-parsed link element was a page.
225 // flag whether row contains a broken link in some field or not
226 $rowContainsBrokenLink = FALSE;
228 // put together content of all relevant fields
230 $htmlParser = t3lib_div
::makeInstance('t3lib_parsehtml');
232 $idRecord = $record['uid'];
234 // get all references
235 foreach ($fields as $field) {
236 $haystack .= $record[$field] . ' --- ';
237 $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
239 $valueField = $record[$field];
241 // Check if a TCA configured field has softreferences defined (see TYPO3 Core API document)
242 if ($conf['softref'] && strlen($valueField)) {
243 // Explode the list of softreferences/parameters
244 $softRefs = t3lib_BEfunc
::explodeSoftRefParserList($conf['softref']);
245 // Traverse soft references
246 foreach ($softRefs as $spKey => $spParams) {
247 // create / get object
248 $softRefObj = &t3lib_BEfunc
::softRefParserObj($spKey);
250 // If there was an object returned...:
251 if (is_object($softRefObj)) {
254 $resultArray = $softRefObj->findRef($table, $field, $idRecord, $valueField, $spKey, $spParams);
255 if (!empty($resultArray['elements'])) {
257 if ($spKey == 'typolink_tag') {
258 $this->analyseTypoLinks($resultArray, $results, $htmlParser, $record, $field, $table);
260 $this->analyseLinks($resultArray, $results, $record, $field, $table);
270 * Find all supported broken links for a specific link lsit.
272 * @param array $resultArray: findRef parsed records
273 * @param array $results: array of broken links
276 private function analyseLinks($resultArray, &$results, $record, $field, $table) {
277 foreach ($resultArray['elements'] as $element) {
278 $r = $element['subst'];
281 $idRecord = $record['uid'];
283 // Parse string for special TYPO3 <link> tag:
285 foreach ($this->hookObjectsArr
as $keyArr => $hookObj) {
286 $type = $hookObj->fetchType($r, $type, $keyArr);
288 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["substr"] = $r;
289 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["row"] = $record;
290 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["table"] = $table;
291 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["field"] = $field;
292 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["uid"] = $idRecord;
299 * Find all supported broken links for a specific typoLink.
301 * @param array $resultArray: findRef parsed records
302 * @param array $results: array of broken links
303 * @param t3lib_parsehtml $htmlParser: instance of htmlparser
306 private function analyseTypoLinks($resultArray, &$results, $htmlParser, $record, $field, $table) {
307 $linkTags = $htmlParser->splitIntoBlock('link', $resultArray['content']);
308 $idRecord = $record['uid'];
309 for ($i = 1; $i < count($linkTags); $i +
= 2) {
310 $referencedRecordType = '';
311 foreach($resultArray['elements'] as $element) {
313 $r = $element['subst'];
315 if (!empty($r['tokenID'])) {
316 if (substr_count($linkTags[$i], $r['tokenID'])) {
317 // Type of referenced record
318 if (strpos($r['recordRef'], 'pages') !== FALSE) {
320 // contains number of the page
321 $referencedRecordType = $r['tokenValue'];
324 // append number of content element to the page saved in the last loop
325 elseif ((strpos($r['recordRef'], 'tt_content') !== FALSE) && ($wasPage === TRUE)) {
326 $referencedRecordType = $referencedRecordType . '#c' . $r['tokenValue'];
331 $title = strip_tags($linkTags[$i]);
335 foreach ($this->hookObjectsArr
as $keyArr => $hookObj) {
336 $type = $hookObj->fetchType($currentR, $type, $keyArr);
339 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["substr"] = $currentR;
340 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["row"] = $record;
341 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["table"] = $table;
342 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["field"] = $field;
343 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["uid"] = $idRecord;
344 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["link_title"] = $title;
345 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["pageAndAnchor"] = $referencedRecordType;
351 * Fill a markerarray with the number of links found in a list of pages.
353 * @param string $curPage: comma separated list of page uids
354 * @return array markerarray with the number of links found
356 public function getLinkCounts($curPage) {
357 $markerArray = array();
358 if (($res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
359 'count(uid) as nbBrokenLinks,link_type',
360 'tx_linkvalidator_link',
361 'record_pid in (' . $this->pidList
. ')',
364 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {
365 $markerArray[$row['link_type']] = $row['nbBrokenLinks'];
366 $markerArray['brokenlinkCount'] +
= $row['nbBrokenLinks'];
373 * Calls t3lib_tsfeBeUserAuth::extGetTreeList.
374 * Although this duplicates the function t3lib_tsfeBeUserAuth::extGetTreeList
375 * this is necessary to create the object that is used recursively by the original function.
377 * Generates a list of page uids from $id. List does not include $id itself.
378 * The only pages excluded from the list are deleted pages.
380 * level in the tree to start collecting uids. Zero means
381 * 'start right away', 1 = 'next level and out'
383 * @param integer Start page id
384 * @param integer Depth to traverse down the page tree.
385 * @param integer $begin is an optional integer that determines at which
386 * @param string Perms clause
387 * @return string Returns the list with a comma in the end (if any pages selected!)
389 public function extGetTreeList($id, $depth, $begin = 0, $permsClause) {
390 $depth = intval($depth);
391 $begin = intval($begin);
396 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
399 'pid=' . $id . ' AND deleted=0 AND ' . $permsClause
401 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
403 $theList .= $row['uid'] . ',';
404 $this->extPageInTreeInfo
[] = array($row['uid'], htmlspecialchars($row['title'], $depth));
407 $theList .= $this->extGetTreeList($row['uid'], $depth - 1, $begin - 1, $permsClause);
417 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE
]['XCLASS']['ext/linkvalidator/classes/class.tx_linkvalidator_processor.php'])) {
418 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE
]['XCLASS']['ext/linkvalidator/classes/class.tx_linkvalidator_processor.php']);