-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathSitemapExtension.php
257 lines (225 loc) · 7.03 KB
/
SitemapExtension.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
<?php
namespace Bolt\Extension\Bolt\Sitemap;
use Bolt\Asset\Snippet\Snippet;
use Bolt\Asset\Target;
use Bolt\Collection\MutableBag;
use Bolt\Controller\Zone;
use Bolt\Extension\SimpleExtension;
use Bolt\Legacy\Content;
use Carbon\Carbon;
use Silex\Application;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Sitemap extension for Bolt.
*
* @author Bob den Otter <bob@twokings.nl>
* @author Gawain Lynch <gawain.lynch@gmail.com>
*/
class SitemapExtension extends SimpleExtension
{
/**
* {@inheritdoc}
*/
protected function registerServices(Application $app)
{
$app['sitemap.config'] = $app->share(
function () {
return $this->getConfig();
}
);
$app['sitemap.links'] = $app->share(
function () {
return $this->getLinks();
}
);
$app['sitemap.controller'] = $app->share(
function () {
return new Controller\Sitemap();
}
);
}
/**
* Twig function returns sitemap.
*
* @return array
*/
protected function registerTwigFunctions()
{
return [
'sitemapEntries' => 'twigGetLinks',
];
}
/**
* {@inheritdoc}
*/
protected function registerAssets()
{
$snippet = new Snippet();
$snippet
->setLocation(Target::END_OF_HEAD)
->setZone(Zone::FRONTEND)
->setCallback(function () {
$app = $this->getContainer();
$snippet = sprintf(
'<link rel="sitemap" type="application/xml" title="Sitemap" href="%s">',
$app['url_generator']->generate('sitemapXml', [], UrlGeneratorInterface::ABSOLUTE_URL)
);
return $snippet;
})
;
return [
$snippet,
];
}
/**
* {@inheritdoc}
*/
protected function getDefaultConfig()
{
return [
'ignore' => [],
'ignore_contenttype' => [],
'remove_link' => [],
'ignore_listing' => false,
'ignore_images' => false,
];
}
public function twigGetLinks()
{
return $this->getLinks();
}
/**
* {@inheritdoc}
*/
protected function registerTwigPaths()
{
return [
'templates',
];
}
/**
* {@inheritdoc}
*/
protected function registerFrontendControllers()
{
$app = $this->getContainer();
return [
'/' => $app['sitemap.controller'],
];
}
/**
* Get an array of links.
*
* @return MutableBag
*/
private function getLinks()
{
// If we have a boatload of content, we might need a bit more memory.
@set_time_limit(0);
@ini_set('memory_limit', '512M');
$app = $this->getContainer();
$config = $this->getConfig();
$contentTypes = $app['config']->get('contenttypes');
$contentParams = ['limit' => 10000, 'order' => 'datepublish desc', 'hydrate' => false];
$homepageLink = [
'link' => $app['url_generator']->generate('homepage'),
'title' => $app['config']->get('general/sitename'),
];
$links = new MutableBag();
$links->add($homepageLink);
foreach ($contentTypes as $contentType) {
$searchable = (isset($contentType['searchable']) && $contentType['searchable']) || !isset($contentType['searchable']);
$isIgnored = in_array($contentType['slug'], $config['ignore_contenttype']);
$isIgnoredURL = in_array('/' . $contentType['slug'], $config['remove_link']);
if (!$isIgnored && !$contentType['viewless'] && $searchable) {
$baseDepth = 0;
if (!$config['ignore_listing']) {
$baseDepth = 1;
if ($isIgnoredURL) {
$links->add([
'link' => '',
'title' => $contentType['name'],
'depth' => 1,
]);
} else {
$link = $this->getListingLink($contentType['slug']);
$links->add([
'link' => $link,
'title' => $contentType['name'],
'depth' => 1,
]);
}
}
$content = $app['storage']->getContent($contentType['slug'], $contentParams);
/** @var Content $entry */
foreach ($content as $entry) {
$links->add([
'link' => $entry->link(),
'title' => $entry->getTitle(),
'depth' => $baseDepth + 1,
'lastmod' => Carbon::createFromTimestamp(strtotime($entry->get('datechanged')))->toW3cString(),
'record' => $entry,
]);
}
}
}
foreach ($links as $idx => $link) {
if ($this->linkIsIgnored($link)) {
unset($links[$idx]);
}
}
return $this->transformByListeners($links);
}
/**
* @param string $contentTypeSlug
*
* @return string
*/
private function getListingLink($contentTypeSlug)
{
$config = $this->getConfig();
$urlGenerator = $this->getContainer()['url_generator'];
$urlParameters = ['contenttypeslug' => $contentTypeSlug];
if(isset($config['listing_routes']) && isset($config['listing_routes'][$contentTypeSlug])) {
$routeName = $config['listing_routes'][$contentTypeSlug];
return $urlGenerator->generate($routeName, $urlParameters);
}
return $urlGenerator->generate('contentlisting', $urlParameters);
}
/**
* Check to see if a link should be ignored from teh sitemap.
*
* @param array $link
*
* @return bool
*/
private function linkIsIgnored($link)
{
$config = $this->getConfig();
if (in_array($link['link'], $config['ignore'])) {
// Perfect match
return true;
}
// Use ignore as a regex
foreach ($config['ignore'] as $ignore) {
$pattern = str_replace('/', '\/', $ignore);
// Match on whole string so a $ignore of "/entry/" isn't the same as "/entry/.*"
if (preg_match("/^{$pattern}$/", $link['link'])) {
return true;
}
}
// No absolute match & no regex match
return false;
}
/**
* @param MutableBag $links
*
* @return MutableBag
*/
private function transformByListeners($links)
{
$event = new SitemapEvent($links);
$this->getContainer()['dispatcher']->dispatch(SitemapEvents::AFTER_COLLECTING_LINKS, $event);
return $event->getLinks();
}
}