Added feature #16420: Add link validator module (Thanks to Patrick Gaumond)
[Packages/TYPO3.CMS.git] / typo3 / sysext / linkvalidator / lib / class.tx_linkvalidator_scheduler_link.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2005 - 2010 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 Scheduler plugin implementation.
27 *
28 * @author Michael Miousse <michael.miousse@infoglobe.ca>
29 */
30 class tx_linkvalidator_scheduler_link extends tx_scheduler_Task {
31
32 /**
33 * @var integer
34 */
35 public $sleepTime;
36
37 /**
38 * @var integer
39 */
40 public $sleepAfterFinish;
41
42 /**
43 * @var integer
44 */
45 public $countInARun;
46
47 /**
48 * @var integer
49 */
50 public $totalBrokenLink = 0;
51
52 /**
53 * @var integer
54 */
55 public $oldTotalBrokenLink = 0;
56
57 /**
58 * Function executed from the Scheduler.
59 *
60 * @return void
61 */
62 public function execute() {
63 $this->setCliArguments();
64
65 $file = t3lib_div::getFileAbsFileName($this->emailfile);
66 $htmlFile = t3lib_div::getURL($file);
67 $this->templateMail = t3lib_parsehtml::getSubpart($htmlFile, '###REPORT_TEMPLATE###');
68
69 // The array to put the content into
70 $html = array();
71 $pageSections = '';
72 $this->dif = FALSE;
73 $pageList = t3lib_div::trimExplode(',', $this->page, 1);
74 if (is_array($pageList)) {
75 foreach ($pageList as $page) {
76 $modTS = t3lib_BEfunc::getModTSconfig($page, 'mod.linkvalidator');
77 $parseObj = t3lib_div::makeInstance('t3lib_TSparser');
78 $parseObj->parse($this->configuration);
79 $TSconfig = $parseObj->setup;
80 $modTS = $modTS['properties'];
81 $overrideTs = $TSconfig['mod.']['tx_linkvalidator.'];
82 if (is_array($overrideTs)) {
83 $modTS = t3lib_div::array_merge_recursive_overrule($modTS, $overrideTs);
84 }
85
86 // get the searchFields from TCA
87 foreach ($GLOBALS['TCA'] as $tablename => $table) {
88 if (!empty($table['columns'])) {
89 foreach ($table['columns'] as $columnname => $column) {
90 if ($column['config']['type'] == 'text' || $column['config']['type'] == 'input') {
91 if (!empty($column['config']['softref']) && (stripos($column['config']['softref'], "typolink")
92 !== FALSE || stripos($column['config']['softref'], "url") !== FALSE)) {
93
94 $searchFields[$tablename][] = $columnname;
95 }
96 }
97 }
98 }
99 }
100
101 // get the searchFields from TypoScript
102 foreach ($modTS['searchFields.'] as $table => $fieldList) {
103 $fields = t3lib_div::trimExplode(',', $fieldList);
104 foreach ($fields as $field) {
105 if (is_array($searchFields[$table])) {
106 if (array_search($field, $searchFields[$table]) === FALSE) {
107 $searchFields[$table][] = $field;
108 }
109 }
110 }
111 }
112 $linktypes = t3lib_div::trimExplode(',', $modTS['linktypes'], 1);
113 if (is_array($linktypes)) {
114 if (!empty($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'])
115 && is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'])) {
116 foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['linkvalidator']['checkLinks'] as $key => $value) {
117 if (in_array($key, $linktypes)) {
118 $array[$key] = 1;
119 }
120 }
121 }
122 }
123 $pageIds = $this->extGetTreeList($page, $this->depth, 0, '1=1');
124 $pageIds .= $page;
125 $processing = t3lib_div::makeInstance('tx_linkvalidator_processing');
126 $processing->init($searchFields, $pageIds);
127 if (!empty($this->email)) {
128 $oldLinkCounts = $processing->getLinkCounts($page);
129 $this->oldTotalBrokenLink += $oldLinkCounts['brokenlinkCount'];
130 }
131
132 $processing->getLinkStatistics($array, $modTS['checkhidden']);
133
134 if (!empty($this->email)) {
135 $linkCounts = $processing->getLinkCounts($page);
136 $this->totalBrokenLink += $linkCounts['brokenlinkCount'];
137 $pageSections .= $this->buildMail($page, $pageIds, $linkCounts, $oldLinkCounts);
138 }
139
140 }
141 }
142 if ($this->totalBrokenLink != $this->oldTotalBrokenLink) {
143 $this->dif = TRUE;
144 }
145 if ($this->totalBrokenLink > 0
146 && (!$this->emailonbrokenlinkonly || $this->dif)
147 && !empty($this->email)
148 ) {
149 $this->reportEmail($pageSections, $modTS);
150 }
151 return TRUE;
152 } // end function execute()
153
154 /**
155 * Build and send the new borken links found warning email.
156 *
157 * @param string $pageSections: Content of page section
158 * @param string $modTS: TsConfig array
159 * @return bool Mail sended or not
160 */
161 function reportEmail($pageSections, $modTS) {
162 $content = t3lib_parsehtml::substituteSubpart($this->templateMail, '###PAGE_SECTION###', $pageSections);
163 $markerArray = array();
164 $markerArray['totalBrokenLink'] = $this->totalBrokenLink;
165 $markerArray['totalBrokenLink_old'] = $this->oldTotalBrokenLink;
166 $content = t3lib_parsehtml::substituteMarkerArray($content, $markerArray, '###|###', TRUE, TRUE);
167
168 $Typo3_htmlmail = t3lib_div::makeInstance('t3lib_htmlmail');
169 $Typo3_htmlmail->start();
170 $Typo3_htmlmail->useBase64();
171
172 $convObj = t3lib_div::makeInstance('t3lib_cs');
173
174 $charset = $convObj->parse_charset($GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] ? $GLOBALS['TYPO3_CONF_VARS']['BE']['forceCharset'] : 'utf-8');
175 $Typo3_htmlmail->subject = $convObj->conv($modTS['mail.']['subject'], $charset, $modTS['mail.']['encoding'], 0);
176 $Typo3_htmlmail->from_email = $modTS['mail.']['fromemail'];
177 $Typo3_htmlmail->from_name = $modTS['mail.']['fromname'];
178 $Typo3_htmlmail->replyto_email = $modTS['mail.']['replytoemail'];
179 $Typo3_htmlmail->replyto_name = $modTS['mail.']['replytoname'];
180
181 //$Typo3_htmlmail->addPlain($mcontent);
182 $Typo3_htmlmail->setHTML($Typo3_htmlmail->encodeMsg($convObj->conv($content, $charset, $modTS['mail.']['encoding'], 0)));
183
184 $Typo3_htmlmail->setHeaders();
185 $Typo3_htmlmail->setContent();
186 $Typo3_htmlmail->setRecipient($this->email);
187
188 return $Typo3_htmlmail->sendtheMail();
189 }
190
191 /**
192 * Build the mail content
193 *
194 * @param int $curPage: id of the current page
195 * @param string $pageList: list of pages id
196 * @param array $markerArray: array of markers
197 * @param array $oldBrokenLink: markerarray with the number of link found
198 * @return string Content of the mail
199 */
200 function buildMail($curPage, $pageList, $markerArray, $oldBrokenLink) {
201 $pageSectionHTML = t3lib_parsehtml::getSubpart($this->templateMail, '###PAGE_SECTION###');
202
203 if (is_array($markerArray)) {
204 foreach ($markerArray as $markerKey => $markerValue) {
205 if (empty($oldBrokenLink[$markerKey])) {
206 $oldBrokenLink[$markerKey] = 0;
207 }
208 if ($markerValue != $oldBrokenLink[$markerKey]) {
209 $this->dif = TRUE;
210 }
211 $markerArray[$markerKey . '_old'] = $oldBrokenLink[$markerKey];
212 }
213 }
214 $markerArray['title'] = t3lib_BEfunc::getRecordTitle('pages', t3lib_BEfunc::getRecord('pages', $curPage));
215
216 $content = '';
217 if ($markerArray['brokenlinkCount'] > 0) {
218 $content = t3lib_parsehtml::substituteMarkerArray($pageSectionHTML, $markerArray, '###|###', TRUE, TRUE);
219 }
220 return $content;
221 }
222
223 /**
224 * Calls t3lib_tsfeBeUserAuth::extGetTreeList.
225 * Although this duplicates the function t3lib_tsfeBeUserAuth::extGetTreeList
226 * this is necessary to create the object that is used recursively by the original function.
227 *
228 * Generates a list of Page-uid's from $id. List does not include $id itself
229 * The only pages excluded from the list are deleted pages.
230 *
231 * level in the tree to start collecting uid's. Zero means
232 * 'start right away', 1 = 'next level and out'
233 *
234 * @param integer Start page id
235 * @param integer Depth to traverse down the page tree.
236 * @param integer $begin is an optional integer that determines at which
237 * @param string Perms clause
238 * @return string Returns the list with a comma in the end (if any pages selected!)
239 */
240 function extGetTreeList($id, $depth, $begin = 0, $perms_clause) {
241 return t3lib_tsfeBeUserAuth::extGetTreeList($id, $depth, $begin, $perms_clause);
242 }
243
244
245 /**
246 * Simulate cli call with setting the required options to the $_SERVER['argv']
247 *
248 * @return void
249 * @access protected
250 */
251 protected function setCliArguments() {
252 $_SERVER['argv'] = array(
253 $_SERVER['argv'][0],
254 'tx_link_scheduler_link',
255 '0',
256 '-ss',
257 '--sleepTime',
258 $this->sleepTime,
259 '--sleepAfterFinish',
260 $this->sleepAfterFinish,
261 '--countInARun',
262 $this->countInARun
263 );
264 }
265 }
266
267 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/linkvalidator/lib/class.tx_linkvalidator_scheduler_link.php']) {
268 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/linkvalidator/lib/class.tx_linkvalidator_scheduler_link.php']);
269 }
270 ?>