* Fixed bug #12652: If none of the link types are marked to be checked, all of them...
[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
111 /**
112 * Find all supported broken links and store them in tx_linkvalidator_link.
113 *
114 * @param array $checkOptions: list of hook object to activate
115 * @param boolean $considerHidden: defines whether to look into hidden fields or not
116 * @return void
117 */
118 public function getLinkStatistics($checkOptions = array(), $considerHidden = FALSE) {
119 $results = array();
120 $checlLinkTypeCondition = '';
121 if(count($checkOptions) > 0) {
122 $checkKeys = array_keys($checkOptions);
123 $checlLinkTypeCondition = ' and link_type in (\'' . implode('\',\'',$checkKeys) . '\')';
124
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);
126
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 . ')';
131 }
132 else{
133 $where = 'deleted = 0 AND pid IN (' . $this->pidList . ')';
134 }
135 if (!$considerHidden) {
136 $where .= t3lib_BEfunc::BEenableFields($table);
137 }
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;
140
141 // re-init selectFields for table
142 $selectFields = 'uid, pid';
143 $selectFields .= ', ' . $GLOBALS['TCA'][$table]['ctrl']['label'] . ', ' . implode(', ', $fields);
144
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);
151 }
152 }
153
154 foreach ($this->hookObjectsArr as $key => $hookObj) {
155 if ((is_array($results[$key])) && empty($checkOptions) || (is_array($results[$key]) && $checkOptions[$key])) {
156 // check'em!
157 foreach ($results[$key] as $entryKey => $entryValue) {
158 $table = $entryValue['table'];
159 $record = array();
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();
167
168 $this->recordReference = $entryValue['substr']['recordRef'];
169
170 $this->pageWithAnchor = $entryValue['pageAndAnchor'];
171
172 if (!empty($this->pageWithAnchor)) {
173 // page with anchor, e.g. 18#1580
174 $url = $this->pageWithAnchor;
175 } else {
176 $url = $entryValue['substr']['tokenValue'];
177 }
178
179 $this->linkCounts[$table]++;
180 $checkURL = $hookObj->checkLink($url, $entryValue, $this);
181 // broken link found!
182 if (!$checkURL) {
183 $response = array();
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')) {
192 $response = array();
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);
199 }
200 }
201 }
202 }
203 }
204 }
205
206
207 /**
208 * Find all supported broken links for a specific record.
209 *
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
214 * @return void
215 */
216 public function analyzeRecord(&$results, $table, $fields, $record) {
217
218 // array to store urls from relevant field contents
219 $urls = array();
220
221 $referencedRecordType = '';
222 // last-parsed link element was a page.
223 $wasPage = TRUE;
224
225 // flag whether row contains a broken link in some field or not
226 $rowContainsBrokenLink = FALSE;
227
228 // put together content of all relevant fields
229 $haystack = '';
230 $htmlParser = t3lib_div::makeInstance('t3lib_parsehtml');
231
232 $idRecord = $record['uid'];
233
234 // get all references
235 foreach ($fields as $field) {
236 $haystack .= $record[$field] . ' --- ';
237 $conf = $GLOBALS['TCA'][$table]['columns'][$field]['config'];
238
239 $valueField = $record[$field];
240
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);
249
250 // If there was an object returned...:
251 if (is_object($softRefObj)) {
252
253 // Do processing
254 $resultArray = $softRefObj->findRef($table, $field, $idRecord, $valueField, $spKey, $spParams);
255 if (!empty($resultArray['elements'])) {
256
257 if ($spKey == 'typolink_tag') {
258 $linkTags = $htmlParser->splitIntoBlock('link', $resultArray['content']);
259
260 for ($i = 1; $i < count($linkTags); $i += 2) {
261 $referencedRecordType = '';
262 foreach($resultArray['elements'] as $element) {
263 $type = '';
264 $r = $element['subst'];
265
266 if (!empty($r['tokenID'])) {
267 if (substr_count($linkTags[$i], $r['tokenID'])) {
268 // Type of referenced record
269 if (strpos($r['recordRef'], 'pages') !== FALSE) {
270 $currentR = $r;
271 // contains number of the page
272 $referencedRecordType = $r['tokenValue'];
273 $wasPage = TRUE;
274 }
275 // append number of content element to the page saved in the last loop
276 elseif ((strpos($r['recordRef'], 'tt_content') !== FALSE) && ($wasPage === TRUE)) {
277 $referencedRecordType = $referencedRecordType . '#c' . $r['tokenValue'];
278 $wasPage = FALSE;
279 } else {
280 $currentR = $r;
281 }
282 $title = strip_tags($linkTags[$i]);
283 }
284 }
285 }
286 foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
287 $type = $hookObj->fetchType($currentR, $type, $keyArr);
288 }
289
290 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["substr"] = $currentR;
291 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["row"] = $record;
292 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["table"] = $table;
293 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["field"] = $field;
294 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["uid"] = $idRecord;
295 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["link_title"] = $title;
296 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $currentR["tokenID"]]["pageAndAnchor"] = $referencedRecordType;
297
298 }
299 } else {
300
301 foreach ($resultArray['elements'] as $element) {
302 $r = $element['subst'];
303 $title = '';
304 $type = '';
305
306 if (!empty($r)) {
307 // Parse string for special TYPO3 <link> tag:
308
309 foreach ($this->hookObjectsArr as $keyArr => $hookObj) {
310 $type = $hookObj->fetchType($r, $type, $keyArr);
311 }
312 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["substr"] = $r;
313 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["row"] = $record;
314 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["table"] = $table;
315 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["field"] = $field;
316 $results[$type][$table . ':' . $field . ':' . $idRecord . ':' . $r["tokenID"]]["uid"] = $idRecord;
317
318 }
319 }
320 }
321 }
322 }
323 }
324 }
325 }
326 }
327
328 /**
329 * Fill a markerarray with the number of links found in a list of pages.
330 *
331 * @param string $curPage: comma separated list of page uids
332 * @return array markerarray with the number of links found
333 */
334 public function getLinkCounts($curPage) {
335 $markerArray = array();
336 if (($res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
337 'count(uid) as nbBrokenLinks,link_type',
338 'tx_linkvalidator_link',
339 'record_pid in (' . $this->pidList . ')',
340 'link_type'
341 ))) {
342 while (($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res))) {
343 $markerArray[$row['link_type']] = $row['nbBrokenLinks'];
344 $markerArray['brokenlinkCount'] += $row['nbBrokenLinks'];
345 }
346 }
347 return $markerArray;
348 }
349
350 /**
351 * Calls t3lib_tsfeBeUserAuth::extGetTreeList.
352 * Although this duplicates the function t3lib_tsfeBeUserAuth::extGetTreeList
353 * this is necessary to create the object that is used recursively by the original function.
354 *
355 * Generates a list of page uids from $id. List does not include $id itself.
356 * The only pages excluded from the list are deleted pages.
357 *
358 * level in the tree to start collecting uids. Zero means
359 * 'start right away', 1 = 'next level and out'
360 *
361 * @param integer Start page id
362 * @param integer Depth to traverse down the page tree.
363 * @param integer $begin is an optional integer that determines at which
364 * @param string Perms clause
365 * @return string Returns the list with a comma in the end (if any pages selected!)
366 */
367 public function extGetTreeList($id, $depth, $begin = 0, $permsClause) {
368 $depth = intval($depth);
369 $begin = intval($begin);
370 $id = intval($id);
371 $theList = '';
372
373 if ($depth > 0) {
374 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
375 'uid,title',
376 'pages',
377 'pid=' . $id . ' AND deleted=0 AND ' . $permsClause
378 );
379 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
380 if ($begin <= 0) {
381 $theList .= $row['uid'] . ',';
382 $this->extPageInTreeInfo[] = array($row['uid'], htmlspecialchars($row['title'], $depth));
383 }
384 if ($depth > 1) {
385 $theList .= $this->extGetTreeList($row['uid'], $depth - 1, $begin - 1, $permsClause);
386 }
387 }
388 }
389 return $theList;
390 }
391
392
393 }
394
395 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/linkvalidator/classes/class.tx_linkvalidator_processor.php'])) {
396 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['ext/linkvalidator/classes/class.tx_linkvalidator_processor.php']);
397 }
398 ?>