forked from samdark/sitemap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Index.php
139 lines (124 loc) · 3.69 KB
/
Index.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
<?php
namespace samdark\sitemap;
use XMLWriter;
/**
* A class for generating Sitemap index (http://www.sitemaps.org/)
*
* @author Alexander Makarov <[email protected]>
*/
class Index
{
/**
* @var XMLWriter
*/
private $writer;
/**
* @var string index file path
*/
private $filePath;
/**
* @var bool whether to gzip the resulting file or not
*/
private $useGzip = false;
/**
* @param string $filePath index file path
*/
public function __construct($filePath)
{
$this->filePath = $filePath;
}
/**
* @var string path of the xml stylesheet
*/
private $stylesheet;
/**
* Creates new file
*/
private function createNewFile()
{
$this->writer = new XMLWriter();
$this->writer->openMemory();
$this->writer->startDocument('1.0', 'UTF-8');
// Use XML stylesheet, if available
if (isset($this->stylesheet)) {
$this->writer->writePi('xml-stylesheet', "type=\"text/xsl\" href=\"" . $this->stylesheet . "\"");
$this->writer->writeRaw("\n");
}
$this->writer->setIndent(true);
$this->writer->startElement('sitemapindex');
$this->writer->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
}
/**
* Adds sitemap link to the index file
*
* @param string $location URL of the sitemap
* @param integer $lastModified unix timestamp of sitemap modification time
* @throws \InvalidArgumentException
*/
public function addSitemap($location, $lastModified = null)
{
if (false === filter_var($location, FILTER_VALIDATE_URL)) {
throw new \InvalidArgumentException(
"The location must be a valid URL. You have specified: {$location}."
);
}
if ($this->writer === null) {
$this->createNewFile();
}
$this->writer->startElement('sitemap');
$this->writer->writeElement('loc', $location);
if ($lastModified !== null) {
$this->writer->writeElement('lastmod', date('c', $lastModified));
}
$this->writer->endElement();
}
/**
* @return string index file path
*/
public function getFilePath()
{
return $this->filePath;
}
/**
* Finishes writing
*/
public function write()
{
if ($this->writer instanceof XMLWriter) {
$this->writer->endElement();
$this->writer->endDocument();
$filePath = $this->getFilePath();
if ($this->useGzip) {
$filePath = 'compress.zlib://' . $filePath;
}
file_put_contents($filePath, $this->writer->flush());
}
}
/**
* Sets whether the resulting file will be gzipped or not.
* @param bool $value
* @throws \RuntimeException when trying to enable gzip while zlib is not available
*/
public function setUseGzip($value)
{
if ($value && !extension_loaded('zlib')) {
throw new \RuntimeException('Zlib extension must be installed to gzip the sitemap.');
}
$this->useGzip = $value;
}
/**
* Sets stylesheet for the XML file.
* Default is to not generate XML-stylesheet tag.
* @param string $stylesheetUrl Stylesheet URL.
*/
public function setStylesheet($stylesheetUrl)
{
if (false === filter_var($stylesheetUrl, FILTER_VALIDATE_URL)) {
throw new \InvalidArgumentException(
"The stylesheet URL is not valid. You have specified: {$stylesheetUrl}."
);
} else {
$this->stylesheet = $stylesheetUrl;
}
}
}