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