[BUGFIX] Initialize $currentR in analyseTypoLinks
[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 . ') or ( record_uid IN (' . $this->pidList . ') and table_name like \'pages\')) '
130 . $checkLinkTypeCondition);
131
132 // Traverse all configured tables
133 foreach ($this->searchFields as $table => $fields) {
134 if($table == 'pages'){
135 $where = 'deleted = 0 AND uid IN (' . $this->pidList . ')';
136 }
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)) {
154 // Analyse each record
155 $this->analyzeRecord($results, $table, $fields, $row);
156 }
157 }
158
159 foreach ($this->hookObjectsArr as $key => $hookObj) {
160 if ((is_array($results[$key])) && empty($checkOptions) || (is_array($results[$key]) && $checkOptions[$key])) {
161 // Check them
162 foreach ($results[$key] as $entryKey => $entryValue) {
163 $table = $entryValue['table'];
164 $record = array();
165 $record['headline'] = $entryValue['row'][$GLOBALS['TCA'][$table]['ctrl']['label']];
166 $record['record_pid'] = $entryValue['row']['pid'];
167 $record['record_uid'] = $entryValue['uid'];
168 $record['table_name'] = $table;
169 $record['link_title'] = $entryValue['link_title'];
170 $record['field'] = $entryValue['field'];
171 $record['last_check'] = time();
172
173 $this->recordReference = $entryValue['substr']['recordRef'];
174
175 $this->pageWithAnchor = $entryValue['pageAndAnchor'];
176
177 if (!empty($this->pageWithAnchor)) {
178 // Page with anchor, e.g. 18#1580
179 $url = $this->pageWithAnchor;
180 } else {
181 $url = $entryValue['substr']['tokenValue'];
182 }
183
184 $this->linkCounts[$table]++;
185 $checkURL = $hookObj->checkLink($url, $entryValue, $this);
186 // Broken link found
187 if (!$checkURL) {
188 $response = array();
189 $response['valid'] = FALSE;
190 $response['errorParams'] = $hookObj->getErrorParams();
191 $this->brokenLinkCounts[$table]++;
192 $record['link_type'] = $key;
193 $record['url'] = $url;
194 $record['url_response'] = serialize($response);
195 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
196 } elseif (t3lib_div::_GP('showalllinks')) {
197 $response = array();
198 $response['valid'] = TRUE;
199 $this->brokenLinkCounts[$table]++;
200 $record['url'] = $url;
201 $record['link_type'] = $key;
202 $record['url_response'] = serialize($response);
203 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
204 }
205 }
206 }
207 }
208 }
209 }
210
211
212 /**
213 * Find all supported broken links for a specific record
214 *
215 * @param array $results Array of broken links
216 * @param string $table Table name of the record
217 * @param array $fields Array of fields to analyze
218 * @param array $record Record to analyse
219 * @return void
220 */
221 public function analyzeRecord(array &$results, $table, array $fields, array $record) {
222
223 // Array to store urls from relevant field contents
224 $urls = array();
225
226 $referencedRecordType = '';
227 // Last-parsed link element was a page
228 $wasPage = TRUE;
229
230 // Flag whether row contains a broken link in some field or not
231 $rowContainsBrokenLink = FALSE;
232
233 // Put together content of all relevant fields
234 $haystack = '';
235 $htmlParser = t3lib_div::makeInstance('t3lib_parsehtml');
236
237 $idRecord = $record['uid'];
238
239 // Get all references
240 foreach ($fields as $field) {
241 $haystack .= $record[$field] . ' --- ';
242 $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
243
244 $valueField = $record[$field];
245
246 // Check if a TCA configured field has soft references defined (see TYPO3 Core API document)
247 if ($conf['softref'] && strlen($valueField)) {
248 // Explode the list of soft references/parameters
249 $softRefs = t3lib_BEfunc::explodeSoftRefParserList($conf['softref']);
250 // Traverse soft references
251 foreach ($softRefs as $spKey => $spParams) {
252 // create / get object
253 $softRefObj = &t3lib_BEfunc::softRefParserObj($spKey);
254
255 // If there is an object returned...
256 if (is_object($softRefObj)) {
257
258 // Do processing
259 $resultArray = $softRefObj->findRef($table, $field, $idRecord, $valueField, $spKey, $spParams);
260 if (!empty($resultArray['elements'])) {
261
262 if ($spKey == 'typolink_tag') {
263 $this->analyseTypoLinks($resultArray, $results, $htmlParser, $record, $field, $table);
264 } else {
265 $this->analyseLinks($resultArray, $results, $record, $field, $table);
266 }
267 }
268 }
269 }
270 }
271 }
272 }
273
274 /**
275 * Find all supported broken links for a specific link list
276 *
277 * @param array $resultArray findRef parsed records
278 * @param array $results Array of broken links
279 * @param array $record UID of the current record
280 * @param string $field The current field
281 * @param string $table The current table
282 * @return void
283 */
284 private function analyseLinks(array $resultArray, array &$results, array $record, $field, $table) {
285 foreach ($resultArray['elements'] as $element) {
286 $r = $element['subst'];
287 $title = '';
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) && ($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 if (($res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
370 'count(uid) as nbBrokenLinks,link_type',
371 'tx_linkvalidator_link',
372 'record_pid in (' . $this->pidList . ')',
373 'link_type'
374 ))) {
375 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {
376 $markerArray[$row['link_type']] = $row['nbBrokenLinks'];
377 $markerArray['brokenlinkCount'] += $row['nbBrokenLinks'];
378 }
379 }
380 return $markerArray;
381 }
382
383 /**
384 * Calls t3lib_tsfeBeUserAuth::extGetTreeList.
385 * Although this duplicates the function t3lib_tsfeBeUserAuth::extGetTreeList
386 * this is necessary to create the object that is used recursively by the original function.
387 *
388 * Generates a list of page uids from $id. List does not include $id itself.
389 * The only pages excluded from the list are deleted pages.
390 *
391 * @param integer $id Start page id
392 * @param integer $depth Depth to traverse down the page tree.
393 * @param integer $begin is an optional integer that determines at which
394 * @param string $permsClause Perms clause
395 * @param boolean $considerHidden Whether to consider hidden pages or not
396 * @return string Returns the list with a comma in the end (if any pages selected!)
397 */
398 public function extGetTreeList($id, $depth, $begin = 0, $permsClause, $considerHidden = FALSE) {
399 $depth = intval($depth);
400 $begin = intval($begin);
401 $id = intval($id);
402 $theList = '';
403
404 if ($depth > 0) {
405 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
406 'uid,title,hidden,extendToSubpages',
407 'pages',
408 'pid=' . $id . ' AND deleted=0 AND ' . $permsClause
409 );
410
411 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
412 if ($begin <= 0 && ($row['hidden']==0 || $considerHidden == 1)) {
413 $theList .= $row['uid'] . ',';
414 $this->extPageInTreeInfo[] = array($row['uid'], htmlspecialchars($row['title'], $depth));
415 }
416 if ($depth > 1 && (!($row['hidden']==1 && $row['extendToSubpages']==1) || $considerHidden == 1)) {
417 $theList .= $this->extGetTreeList($row['uid'], $depth - 1, $begin - 1, $permsClause, $considerHidden);
418 }
419 }
420 }
421 return $theList;
422 }
423
424 /**
425 * @param array $pageInfo Array with uid, title, hidden, extendToSubpages from pages table
426 * @return boolean TRUE if rootline contains a hidden page, FALSE if not
427 */
428 public function getRootLineIsHidden(array $pageInfo){
429 $hidden = FALSE;
430 if ($pageInfo['extendToSubpages'] == 1 && $pageInfo['hidden'] == 1){
431 $hidden = TRUE;
432 } else {
433 if ($pageInfo['pid'] > 0) {
434 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
435 'uid,title,hidden,extendToSubpages',
436 'pages',
437 'uid=' . $pageInfo['pid']
438 );
439
440 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
441 $hidden = $this->getRootLineIsHidden($row);
442 }
443 }
444 else {
445 $hidden = FALSE;
446 }
447 }
448 return $hidden;
449
450 }
451
452 }
453
454 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/linkvalidator/classes/class.tx_linkvalidator_processor.php'])) {
455 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/linkvalidator/classes/class.tx_linkvalidator_processor.php']);
456 }
457 ?>