|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Sitemap\Formatter; |
|
4
|
|
|
|
|
5
|
|
|
use Sludio\HelperBundle\Sitemap\Entity\Image; |
|
6
|
|
|
use Sludio\HelperBundle\Sitemap\Entity\SitemapIndex; |
|
7
|
|
|
use Sludio\HelperBundle\Sitemap\Entity\Url; |
|
8
|
|
|
use Sludio\HelperBundle\Sitemap\Entity\Video; |
|
9
|
|
|
|
|
10
|
|
|
class XmlFormatter extends BaseFormatter implements SitemapIndexFormatterInterface |
|
11
|
|
|
{ |
|
12
|
|
|
public function getSitemapStart() |
|
13
|
|
|
{ |
|
14
|
|
|
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<urlset '.'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '.'xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" '.'xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">'."\n"; |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function getSitemapEnd() |
|
18
|
|
|
{ |
|
19
|
|
|
return '</urlset>'; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function getSitemapIndexStart() |
|
23
|
|
|
{ |
|
24
|
|
|
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n"; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getSitemapIndexEnd() |
|
28
|
|
|
{ |
|
29
|
|
|
return '</sitemapindex>'; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function formatUrl(Url $url) |
|
33
|
|
|
{ |
|
34
|
|
|
return '<url>'."\n".$this->formatBody($url).'</url>'."\n"; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
protected function formatBody(Url $url) |
|
38
|
|
|
{ |
|
39
|
|
|
$buffer = "\t".'<loc>'.$this->escape($url->getLoc()).'</loc>'."\n"; |
|
40
|
|
|
|
|
41
|
|
|
$checks = [ |
|
42
|
|
|
'getLastmod' => "\t".'<lastmod>'.$this->escape($url->getLastmod()).'</lastmod>'."\n", |
|
43
|
|
|
'getChangefreq' => "\t".'<changefreq>'.$this->escape($url->getChangefreq()).'</changefreq>'."\n", |
|
44
|
|
|
'getPriority' => "\t".'<priority>'.$this->escape($url->getPriority()).'</priority>'."\n", |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
foreach ($checks as $check => $text) { |
|
48
|
|
|
if ($url->{$check}() !== null) { |
|
49
|
|
|
$buffer .= $text; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
foreach ($url->getVideos() as $video) { |
|
54
|
|
|
$buffer .= $this->formatVideo($video); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
foreach ($url->getImages() as $image) { |
|
58
|
|
|
$buffer .= $this->formatImage($image); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $buffer; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function formatVideo(Video $video) |
|
65
|
|
|
{ |
|
66
|
|
|
$buffer = $this->videoHeader($video); |
|
67
|
|
|
|
|
68
|
|
|
if ($video->getFamilyFriendly() === false) { |
|
69
|
|
|
$buffer .= "\t\t".'<video:family_friendly>no</video:family_friendly>'."\n"; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$checks = [ |
|
73
|
|
|
'getContentLoc' => "\t\t".'<video:content_loc>'.$this->escape($video->getContentLoc()).'</video:content_loc>'."\n", |
|
74
|
|
|
'getDuration' => "\t\t".'<video:duration>'.$this->escape($video->getDuration()).'</video:duration>'."\n", |
|
75
|
|
|
'getExpirationDate' => "\t\t".'<video:expiration_date>'.$this->escape($video->getExpirationDate()).'</video:expiration_date>'."\n", |
|
76
|
|
|
'getRating' => "\t\t".'<video:rating>'.$this->escape($video->getRating()).'</video:rating>'."\n", |
|
77
|
|
|
'getViewCount' => "\t\t".'<video:view_count>'.$this->escape($video->getViewCount()).'</video:view_count>'."\n", |
|
78
|
|
|
'getPublicationDate' => "\t\t".'<video:publication_date>'.$this->escape($video->getPublicationDate()).'</video:publication_date>'."\n", |
|
79
|
|
|
'getCategory' => "\t\t".'<video:category>'.$this->escape($video->getCategory()).'</video:category>'."\n", |
|
80
|
|
|
'getRequiresSubscription' => "\t\t".'<video:requires_subscription>'.($video->getRequiresSubscription() ? 'yes' : 'no').'</video:requires_subscription>'."\n", |
|
81
|
|
|
'getLive' => "\t\t".'<video:live>'.($video->getLive() ? 'yes' : 'no').'</video:live>'."\n", |
|
82
|
|
|
]; |
|
83
|
|
|
|
|
84
|
|
|
$extended = [ |
|
85
|
|
|
'getPlayerLoc' => 'checkVideoPlayerLoc', |
|
86
|
|
|
'getTags' => 'checkVideoTags', |
|
87
|
|
|
'getRestrictions' => 'checkVideoRestrictions', |
|
88
|
|
|
'getGalleryLoc' => 'checkVideoGalleryLoc', |
|
89
|
|
|
'getUploader' => 'checkVideoUploader', |
|
90
|
|
|
'getPlatforms' => 'checkVideoPlatforms', |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
$keys = array_merge(array_keys($checks), array_keys($extended)); |
|
94
|
|
|
foreach ($keys as $key) { |
|
95
|
|
|
if ($video->{$key}() !== null) { |
|
96
|
|
|
if (isset($checks[$key])) { |
|
97
|
|
|
$buffer .= $checks[$key]; |
|
98
|
|
|
} elseif (isset($extended[$key])) { |
|
99
|
|
|
$buffer .= $this->{$extended[$key]}($video); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $buffer."\t".'</video:video>'."\n"; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
protected function videoHeader($video) |
|
108
|
|
|
{ |
|
109
|
|
|
$buffer = "\t".'<video:video>'."\n"; |
|
110
|
|
|
|
|
111
|
|
|
$buffer .= "\t\t".'<video:title>'.$this->escape($video->getTitle()).'</video:title>'."\n"; |
|
112
|
|
|
$buffer .= "\t\t".'<video:description>'.$this->escape($video->getDescription()).'</video:description>'."\n"; |
|
113
|
|
|
$buffer .= "\t\t".'<video:thumbnail_loc>'.$this->escape($video->getThumbnailLoc()).'</video:thumbnail_loc>'."\n"; |
|
114
|
|
|
|
|
115
|
|
|
return $buffer; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
protected function formatImage(Image $image) |
|
119
|
|
|
{ |
|
120
|
|
|
$buffer = "\t".'<image:image>'."\n\t\t".'<image:loc>'.$this->escape($image->getLoc()).'</image:loc>'."\n"; |
|
121
|
|
|
|
|
122
|
|
|
$checks = [ |
|
123
|
|
|
'getCaption' => "\t\t".'<image:caption>'.$this->escape($image->getCaption()).'</image:caption>'."\n", |
|
124
|
|
|
'getGeoLocation' => "\t\t".'<image:geo_location>'.$this->escape($image->getGeoLocation()).'</image:geo_location>'."\n", |
|
125
|
|
|
'getTitle' => "\t\t".'<image:title>'.$this->escape($image->getTitle()).'</image:title>'."\n", |
|
126
|
|
|
'getLicense' => "\t\t".'<image:license>'.$this->escape($image->getLicense()).'</image:license>'."\n", |
|
127
|
|
|
]; |
|
128
|
|
|
|
|
129
|
|
|
foreach ($checks as $check => $text) { |
|
130
|
|
|
if ($image->{$check}() !== null) { |
|
131
|
|
|
$buffer .= $text; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
return $buffer."\t".'</image:image>'."\n"; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
public function formatSitemapIndex(SitemapIndex $sitemapIndex) |
|
139
|
|
|
{ |
|
140
|
|
|
return '<sitemap>'."\n".$this->formatSitemapIndexBody($sitemapIndex).'</sitemap>'."\n"; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
protected function formatSitemapIndexBody(SitemapIndex $sitemapIndex) |
|
144
|
|
|
{ |
|
145
|
|
|
$buffer = "\t".'<loc>'.$this->escape($sitemapIndex->getLoc()).'</loc>'."\n"; |
|
146
|
|
|
|
|
147
|
|
|
if ($sitemapIndex->getLastmod() !== null) { |
|
148
|
|
|
$buffer .= "\t".'<lastmod>'.$this->escape($sitemapIndex->getLastmod()).'</lastmod>'."\n"; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return $buffer; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
protected function checkVideoPlayerLoc($video) |
|
155
|
|
|
{ |
|
156
|
|
|
$playerLoc = $video->getPlayerLoc(); |
|
157
|
|
|
$allowEmbed = $playerLoc['allow_embed'] ? 'yes' : 'no'; |
|
158
|
|
|
$autoplay = $playerLoc['autoplay'] !== null ? sprintf(' autoplay="%s"', $this->escape($playerLoc['autoplay'])) : ''; |
|
159
|
|
|
|
|
160
|
|
|
return "\t\t".sprintf('<video:player_loc allow_embed="%s"%s>', $allowEmbed, $autoplay).$this->escape($playerLoc['loc']).'</video:player_loc>'."\n"; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
protected function checkVideoTags($video) |
|
164
|
|
|
{ |
|
165
|
|
|
$text = ''; |
|
166
|
|
|
$tags = $video->getTags(); |
|
167
|
|
|
/** @var $tags array */ |
|
168
|
|
|
foreach ($tags as $tag) { |
|
169
|
|
|
$text .= "\t\t".'<video:tag>'.$this->escape($tag).'</video:tag>'."\n"; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return $text; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
protected function checkVideoRestrictions($video) |
|
176
|
|
|
{ |
|
177
|
|
|
$restrictions = $video->getRestrictions(); |
|
178
|
|
|
$relationship = $this->escape($restrictions['relationship']); |
|
179
|
|
|
|
|
180
|
|
|
return "\t\t".'<video:restriction relationship="'.$relationship.'">'.$this->escape(implode(' ', $restrictions['countries'])).'</video:restriction>'."\n"; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
protected function checkVideoGalleryLoc($video) |
|
184
|
|
|
{ |
|
185
|
|
|
$galleryLoc = $video->getGalleryLoc(); |
|
186
|
|
|
$title = $galleryLoc['title'] !== null ? sprintf(' title="%s"', $this->escape($galleryLoc['title'])) : ''; |
|
187
|
|
|
|
|
188
|
|
|
return "\t\t".sprintf('<video:gallery_loc%s>', $title).$this->escape($galleryLoc['loc']).'</video:gallery_loc>'."\n"; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
protected function checkVideoUploader($video) |
|
192
|
|
|
{ |
|
193
|
|
|
$uploader = $video->getUploader(); |
|
194
|
|
|
$info = $uploader['info'] !== null ? sprintf(' info="%s"', $this->escape($uploader['info'])) : ''; |
|
195
|
|
|
|
|
196
|
|
|
return "\t\t".sprintf('<video:uploader%s>', $info).$this->escape($uploader['name']).'</video:uploader>'."\n"; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
protected function checkVideoPlatforms($video) |
|
200
|
|
|
{ |
|
201
|
|
|
$text = ''; |
|
202
|
|
|
$platforms = $video->getPlatforms(); |
|
203
|
|
|
/** @var $platforms array */ |
|
204
|
|
|
foreach ($platforms as $platform => $relationship) { |
|
205
|
|
|
$text .= "\t\t".'<video:platform relationship="'.$this->escape($relationship).'">'.$this->escape($platform).'</video:platform>'."\n"; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return $text; |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|