Fixed bug #13828: checkhidden has no effect for some configurations (Michael Miousse)
[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 seperated 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 * Init Function: Here all the needed configuration values are stored 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($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 $checlLinkTypeCondition = '';
125 if(count($checkOptions) > 0) {
126 $checkKeys = array_keys($checkOptions);
127 $checlLinkTypeCondition = ' and link_type in (\'' . implode('\',\'',$checkKeys) . '\')';
128
129 $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);
130
131 // let's traverse all configured tables
132 foreach ($this->searchFields as $table => $fields) {
133 if($table == 'pages'){
134 $where = 'deleted = 0 AND uid IN (' . $this->pidList . ')';
135 }
136 else{
137 $where = 'deleted = 0 AND pid IN (' . $this->pidList . ')';
138 }
139 if (!$considerHidden) {
140 $where .= t3lib_BEfunc::BEenableFields($table);
141 }
142 // if table is not configured, we assume the ext is not installed and therefore no need to check it
143 if (!is_array($GLOBALS['TCA'][$table])) continue;
144
145 // re-init selectFields for table
146 $selectFields = 'uid, pid';
147 $selectFields .= ', ' . $GLOBALS['TCA'][$table]['ctrl']['label'] . ', ' . implode(', ', $fields);
148
149 // TODO: only select rows that have content in at least one of the relevant fields (via OR)
150 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery($selectFields, $table, $where);
151 // Get record rows of table
152 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
153 // Analyse each record
154 $this->analyzeRecord($results, $table, $fields, $row);
155 }
156 }
157
158 foreach ($this->hookObjectsArr as $key => $hookObj) {
159 if ((is_array($results[$key])) && empty($checkOptions) || (is_array($results[$key]) && $checkOptions[$key])) {
160 // check'em!
161 foreach ($results[$key] as $entryKey => $entryValue) {
162 $table = $entryValue['table'];
163 $record = array();
164 $record['headline'] = $entryValue['row'][$GLOBALS['TCA'][$table]['ctrl']['label']];
165 $record['record_pid'] = $entryValue['row']['pid'];
166 $record['record_uid'] = $entryValue['uid'];
167 $record['table_name'] = $table;
168 $record['link_title'] = $entryValue['link_title'];
169 $record['field'] = $entryValue['field'];
170 $record['last_check'] = time();
171
172 $this->recordReference = $entryValue['substr']['recordRef'];
173
174 $this->pageWithAnchor = $entryValue['pageAndAnchor'];
175
176 if (!empty($this->pageWithAnchor)) {
177 // page with anchor, e.g. 18#1580
178 $url = $this->pageWithAnchor;
179 } else {
180 $url = $entryValue['substr']['tokenValue'];
181 }
182
183 $this->linkCounts[$table]++;
184 $checkURL = $hookObj->checkLink($url, $entryValue, $this);
185 // broken link found!
186 if (!$checkURL) {
187 $response = array();
188 $response['valid'] = FALSE;
189 $response['errorParams'] = $hookObj->getErrorParams();
190 $this->brokenLinkCounts[$table]++;
191 $record['link_type'] = $key;
192 $record['url'] = $url;
193 $record['url_response'] = serialize($response);
194 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
195 } elseif (t3lib_div::_GP('showalllinks')) {
196 $response = array();
197 $response['valid'] = TRUE;
198 $this->brokenLinkCounts[$table]++;
199 $record['url'] = $url;
200 $record['link_type'] = $key;
201 $record['url_response'] = serialize($response);
202 $GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_linkvalidator_link', $record);
203 }
204 }
205 }
206 }
207 }
208 }
209
210
211 /**
212 * Find all supported broken links for a specific record.
213 *
214 * @param array $results: array of broken links
215 * @param string $table: table name of the record
216 * @param array $fields: array of fields to analyze
217 * @param array $record: record to analyse
218 * @return void
219 */
220 public function analyzeRecord(&$results, $table, $fields, $record) {
221
222 // array to store urls from relevant field contents
223 $urls = array();
224
225 $referencedRecordType = '';
226 // last-parsed link element was a page.
227 $wasPage = TRUE;
228
229 // flag whether row contains a broken link in some field or not
230 $rowContainsBrokenLink = FALSE;
231
232 // put together content of all relevant fields
233 $haystack = '';
234 $htmlParser = t3lib_div::makeInstance('t3lib_parsehtml');
235
236 $idRecord = $record['uid'];
237
238 // get all references
239 foreach ($fields as $field) {
240 $haystack .= $record[$field] . ' --- ';
241 $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
242
243 $valueField = $record[$field];
244
245 // Check if a TCA configured field has softreferences defined (see TYPO3 Core API document)
246 if ($conf['softref'] && strlen($valueField)) {
247 // Explode the list of softreferences/parameters
248 $softRefs = t3lib_BEfunc::explodeSoftRefParserList($conf['softref']);
249 // Traverse soft references
250 foreach ($softRefs as $spKey => $spParams) {
251 // create / get object
252 $softRefObj = &t3lib_BEfunc::softRefParserObj($spKey);
253
254 // If there was an object returned...:
255 if (is_object($softRefObj)) {
256
257 // Do processing
258 $resultArray = $softRefObj->findRef($table, $field, $idRecord, $valueField, $spKey, $spParams);
259 if (!empty($resultArray['elements'])) {
260
261 if ($spKey == 'typolink_tag') {
262 $this->analyseTypoLinks($resultArray, $results, $htmlParser, $record, $field, $table);
263 } else {
264 $this->analyseLinks($resultArray, $results, $record, $field, $table);
265 }
266 }
267 }
268 }
269 }
270 }
271 }
272
273 /**
274 * Find all supported broken links for a specific link lsit.
275 *
276 * @param array $resultArray: findRef parsed records
277 * @param array $results: array of broken links
278 * @return void
279 */
280 private function analyseLinks($resultArray, &$results, $record, $field, $table) {
281 foreach ($resultArray['elements'] as $element) {
282 $r = $element['subst'];
283 $title = '';
284 $type = '';
285 $idRecord = $record['uid'];
286 if (!empty($r)) {
287 // Parse string for special TYPO3 <link> tag:
288
289 foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
290 $type = $hookObj->fetchType($r, $type, $keyArr);
291 }
292 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["substr"] = $r;
293 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["row"] = $record;
294 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["table"] = $table;
295 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["field"] = $field;
296 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["uid"] = $idRecord;
297
298 }
299 }
300 }
301
302 /**
303 * Find all supported broken links for a specific typoLink.
304 *
305 * @param array $resultArray: findRef parsed records
306 * @param array $results: array of broken links
307 * @param t3lib_parsehtml $htmlParser: instance of htmlparser
308 * @return void
309 */
310 private function analyseTypoLinks($resultArray, &$results, $htmlParser, $record, $field, $table) {
311 $linkTags = $htmlParser->splitIntoBlock('link', $resultArray['content']);
312 $idRecord = $record['uid'];
313 for ($i = 1; $i < count($linkTags); $i += 2) {
314 $referencedRecordType = '';
315 foreach($resultArray['elements'] as $element) {
316 $type = '';
317 $r = $element['subst'];
318
319 if (!empty($r['tokenID'])) {
320 if (substr_count($linkTags[$i], $r['tokenID'])) {
321 // Type of referenced record
322 if (strpos($r['recordRef'], 'pages') !== FALSE) {
323 $currentR = $r;
324 // contains number of the page
325 $referencedRecordType = $r['tokenValue'];
326 $wasPage = TRUE;
327 }
328 // append number of content element to the page saved in the last loop
329 elseif ((strpos($r['recordRef'], 'tt_content') !== FALSE) && ($wasPage === TRUE)) {
330 $referencedRecordType = $referencedRecordType . '#c' . $r['tokenValue'];
331 $wasPage = FALSE;
332 } else {
333 $currentR = $r;
334 }
335 $title = strip_tags($linkTags[$i]);
336 }
337 }
338 }
339 foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
340 $type = $hookObj->fetchType($currentR, $type, $keyArr);
341 }
342
343 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["substr"] = $currentR;
344 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["row"] = $record;
345 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["table"] = $table;
346 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["field"] = $field;
347 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["uid"] = $idRecord;
348 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["link_title"] = $title;
349 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["pageAndAnchor"] = $referencedRecordType;
350
351 }
352 }
353
354 /**
355 * Fill a markerarray with the number of links found in a list of pages.
356 *
357 * @param string $curPage: comma separated list of page uids
358 * @return array markerarray with the number of links found
359 */
360 public function getLinkCounts($curPage) {
361 $markerArray = array();
362 if (($res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
363 'count(uid) as nbBrokenLinks,link_type',
364 'tx_linkvalidator_link',
365 'record_pid in (' . $this->pidList . ')',
366 'link_type'
367 ))) {
368 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {
369 $markerArray[$row['link_type']] = $row['nbBrokenLinks'];
370 $markerArray['brokenlinkCount'] += $row['nbBrokenLinks'];
371 }
372 }
373 return $markerArray;
374 }
375
376 /**
377 * Calls t3lib_tsfeBeUserAuth::extGetTreeList.
378 * Although this duplicates the function t3lib_tsfeBeUserAuth::extGetTreeList
379 * this is necessary to create the object that is used recursively by the original function.
380 *
381 * Generates a list of page uids from $id. List does not include $id itself.
382 * The only pages excluded from the list are deleted pages.
383 *
384 * level in the tree to start collecting uids. Zero means
385 * 'start right away', 1 = 'next level and out'
386 *
387 * @param integer Start page id
388 * @param integer Depth to traverse down the page tree.
389 * @param integer $begin is an optional integer that determines at which
390 * @param string Perms clause
391 * @return string Returns the list with a comma in the end (if any pages selected!)
392 */
393 public function extGetTreeList($id, $depth, $begin = 0, $permsClause, $considerHidden = FALSE) {
394 $depth = intval($depth);
395 $begin = intval($begin);
396 $id = intval($id);
397 $theList = '';
398
399 if ($depth > 0) {
400 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
401 'uid,title,hidden,extendToSubpages',
402 'pages',
403 'pid=' . $id . ' AND deleted=0 AND ' . $permsClause
404 );
405
406 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
407 if ($begin <= 0 && ($row['hidden']==0 || $considerHidden == 1)) {
408 $theList .= $row['uid'] . ',';
409 $this->extPageInTreeInfo[] = array($row['uid'], htmlspecialchars($row['title'], $depth));
410 }
411 if ($depth > 1 && (!($row['hidden']==1 && $row['extendToSubpages']==1) || $considerHidden == 1)) {
412 $theList .= $this->extGetTreeList($row['uid'], $depth - 1, $begin - 1, $permsClause, $considerHidden);
413 }
414 }
415 }
416 return $theList;
417 }
418
419 public function getRootLineIsHidden($pageInfo){
420 $hidden = FALSE;
421 if($pageInfo['extendToSubpages'] == 1 && $pageInfo['hidden'] == 1){
422 $hidden = TRUE;
423 }
424 else{
425 if($pageInfo['pid'] > 0) {
426 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
427 'uid,title,hidden,extendToSubpages',
428 'pages',
429 'uid=' . $pageInfo['pid']
430 );
431
432 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
433 $hidden = $this->getRootLineIsHidden($row);
434 }
435 }
436 else {
437 $hidden = FALSE;
438 }
439 }
440 return $hidden;
441
442 }
443
444 }
445
446 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/linkvalidator/classes/class.tx_linkvalidator_processor.php'])) {
447 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/linkvalidator/classes/class.tx_linkvalidator_processor.php']);
448 }
449 ?>