forked from dmitryd/typo3-dd_googlesitemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.tx_ddgooglesitemap_pages.php
264 lines (240 loc) · 8.54 KB
/
class.tx_ddgooglesitemap_pages.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/***************************************************************
* Copyright notice
*
* (c) 2007-2014 Dmitry Dulepov <[email protected]>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* This class produces sitemap for pages
*
* @author Dmitry Dulepov <[email protected]>
* @package TYPO3
* @subpackage tx_ddgooglesitemap
*/
class tx_ddgooglesitemap_pages extends tx_ddgooglesitemap_generator {
/**
* List of page uid values to generate entries for
*
* @var array
*/
protected $pageList = array();
/**
* Number of generated items.
*
* @var int
*/
protected $generatedItemCount = 0;
/**
* A sitemap renderer
*
* @var tx_ddgooglesitemap_normal_renderer
*/
protected $renderer;
/** @var array */
protected $excludedPageTypes = array(0, 3, 4, 5, 6, 7, 199, 254, 255);
/**
* Hook objects for post-processing
*
* @var array
*/
protected $hookObjects;
/**
* Initializes the instance of this class. This constructir sets starting
* point for the sitemap to the current page id
*/
public function __construct() {
parent::__construct();
$excludePageTypes = t3lib_div::intExplode(',', $GLOBALS['TSFE']->tmpl->setup['tx_ddgooglesitemap.']['excludePageType'], TRUE);
if (count($excludePageTypes) > 0) {
$this->excludedPageTypes = $excludePageTypes;
}
$pid = intval($GLOBALS['TSFE']->tmpl->setup['tx_ddgooglesitemap.']['forceStartPid']);
if ($pid === 0 || $pid == $GLOBALS['TSFE']->id) {
$this->pageList[$GLOBALS['TSFE']->id] = array(
'uid' => $GLOBALS['TSFE']->id,
'SYS_LASTCHANGED' => $GLOBALS['TSFE']->page['SYS_LASTCHANGED'],
'tx_ddgooglesitemap_lastmod' => $GLOBALS['TSFE']->page['tx_ddgooglesitemap_lastmod'],
'tx_ddgooglesitemap_priority' => $GLOBALS['TSFE']->page['tx_ddgooglesitemap_priority'],
'doktype' => $GLOBALS['TSFE']->page['doktype']
);
}
else {
$page = $GLOBALS['TSFE']->sys_page->getPage($pid);
$this->pageList[$page['uid']] = array(
'uid' => $page['uid'],
'SYS_LASTCHANGED' => $page['SYS_LASTCHANGED'],
'tx_ddgooglesitemap_lastmod' => $page['tx_ddgooglesitemap_lastmod'],
'tx_ddgooglesitemap_priority' => $GLOBALS['TSFE']->page['tx_ddgooglesitemap_priority'],
'doktype' => $page['doktype']
);
}
$this->renderer = t3lib_div::makeInstance('tx_ddgooglesitemap_normal_renderer');
// Prepare user defined objects (if any)
$this->hookObjects = array();
if (is_array($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dd_googlesitemap']['generateSitemapForPagesClass'])) {
foreach ($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['dd_googlesitemap']['generateSitemapForPagesClass'] as $classRef) {
$this->hookObjects[] = t3lib_div::getUserObj($classRef);
}
}
}
/**
* Generates sitemap for pages (<url> entries in the sitemap)
*
* @return void
*/
protected function generateSitemapContent() {
// Workaround: we want the sysfolders back into the menu list!
// We also exclude "Backend user section" pages.
$GLOBALS['TSFE']->sys_page->where_hid_del = str_replace(
'pages.doktype<200',
'pages.doktype<>255 AND pages.doktype<>6',
$GLOBALS['TSFE']->sys_page->where_hid_del
);
while (!empty($this->pageList) && $this->generatedItemCount - $this->offset <= $this->limit) {
$pageInfo = array_shift($this->pageList);
if ($this->generatedItemCount >= $this->offset) {
$this->writeSingleUrl($pageInfo);
}
$this->generatedItemCount++;
// Add subpages of this page to the end of the page list. This way
// we get top level pages in the sitemap first, then subpages of the
// first, second, etc pages of the top level pages and so on.
//
// Notice: no sorting (for speed)!
$GLOBALS['TSFE']->sys_page->sys_language_uid = $GLOBALS['TSFE']->config['config']['sys_language_uid'];
$morePages = $GLOBALS['TSFE']->sys_page->getMenu($pageInfo['uid'],
'uid,doktype,no_search,l18n_cfg,SYS_LASTCHANGED,tx_ddgooglesitemap_lastmod,tx_ddgooglesitemap_priority',
'', '', false);
$this->removeNonTranslatedPages($morePages);
$this->pageList = array_merge($this->pageList, array_values($morePages));
unset($morePages);
}
}
/**
* Obtains the last modification date of the page.
*
* @param array $pageInfo
* @return int
*/
protected function getLastMod(array $pageInfo) {
$lastModDates = t3lib_div::intExplode(',', $pageInfo['tx_ddgooglesitemap_lastmod']);
$lastModDates[] = intval($pageInfo['SYS_LASTCHANGED']);
rsort($lastModDates, SORT_NUMERIC);
reset($lastModDates);
return current($lastModDates);
}
/**
* Exclude pages from given list
*
* @param array $pages
* @return void
*/
protected function removeNonTranslatedPages(array &$pages) {
$language = (int)$GLOBALS['TSFE']->config['config']['sys_language_uid'];
foreach ($pages as $pageUid => $page) {
// Hide page in default language
if ($language === 0 && t3lib_div::hideIfDefaultLanguage($page['l18n_cfg'])) {
unset($pages[$pageUid]);
}
elseif ($language !== 0 && !isset($page['_PAGES_OVERLAY']) && t3lib_div::hideIfNotTranslated($page['l18n_cfg'])) {
// Hide page if no translation is set
unset($pages[$pageUid]);
}
}
}
/**
* Checks if the page should be included into the sitemap.
*
* @param array $pageInfo
* @return bool
*/
protected function shouldIncludePageInSitemap(array $pageInfo) {
return !$pageInfo['no_search'] && !in_array($pageInfo['doktype'], $this->excludedPageTypes);
}
/**
* Outputs information about single page
*
* @param array $pageInfo Page information (needs 'uid' and 'SYS_LASTCHANGED' columns)
* @return void
*/
protected function writeSingleUrl(array $pageInfo) {
if ($this->shouldIncludePageInSitemap($pageInfo) && ($url = $this->getPageLink($pageInfo['uid']))) {
echo $this->renderer->renderEntry($url, $pageInfo['title'],
$this->getLastMod($pageInfo),
$this->getChangeFrequency($pageInfo), '', $pageInfo['tx_ddgooglesitemap_priority']);
// Post-process current page and possibly append data
// @see http://forge.typo3.org/issues/45637
foreach ($this->hookObjects as $hookObject) {
if (is_callable(array($hookObject, 'postProcessPageInfo'))) {
$parameters = array(
'pageInfo' => &$pageInfo,
'generatedItemCount' => &$this->generatedItemCount,
'offset' => $this->offset,
'limit' => $this->limit,
'renderer' => $this->renderer,
'pObj' => $this
);
/** @noinspection PhpUndefinedMethodInspection */
$hookObject->postProcessPageInfo($parameters);
}
}
}
}
protected function getChangeFrequency(array $pageInfo) {
$timeValues = t3lib_div::intExplode(',', $pageInfo['tx_ddgooglesitemap_lastmod']);
// Remove zeros
foreach ($timeValues as $k => $v) {
if ($v == 0) {
unset($timeValues[$k]);
}
}
$timeValues[] = $pageInfo['SYS_LASTCHANGED'];
$timeValues[] = time();
sort($timeValues, SORT_NUMERIC);
$sum = 0;
for ($i = count($timeValues) - 1; $i > 0; $i--) {
$sum += ($timeValues[$i] - $timeValues[$i - 1]);
}
$average = ($sum/(count($timeValues) - 1));
return ($average >= 180*24*60*60 ? 'yearly' :
($average <= 24*60*60 ? 'daily' :
($average <= 60*60 ? 'hourly' :
($average <= 14*24*60*60 ? 'weekly' : 'monthly'))));
}
/**
* Creates a link to a single page
*
* @param array $pageId Page ID
* @return string Full URL of the page including host name (escaped)
*/
protected function getPageLink($pageId) {
$conf = array(
'parameter' => $pageId,
'returnLast' => 'url',
);
$link = htmlspecialchars($this->cObj->typoLink('', $conf));
return t3lib_div::locationHeaderUrl($link);
}
}
/** @noinspection PhpUndefinedVariableInspection */
if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dd_googlesitemap/class.tx_ddgooglesitemap_pages.php']) {
/** @noinspection PhpIncludeInspection */
include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/dd_googlesitemap/class.tx_ddgooglesitemap_pages.php']);
}