This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSitemap.php
198 lines (162 loc) · 5.02 KB
/
Sitemap.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
<?php
/**
* Sitemap Generator
*
* Generate Sitemap and Sitemap Index files
*
* Example:
* <pre>
* $sitemap = new Sitemap();
* $sitemap->baseurl = 'https://www.example.com';
* $sitemap->target = '../';
* $sitemap->limit = 10000;
* $sitemap->add( '/index.html' );
* $sitemap->add( '/about.html' );
* $sitemap->add( '/news.html', '2016-09-10', 'daily', 0.7 );
* $sitemap->generate();
* </pre>
*
* @author Zsolt Tovis
* @copyright Copyright (c) 2016, Zsolt Tovis
* @uses DOMDocument
*/
class Sitemap {
/**
* @var string Base Url of Sitemap Urls
*/
public $baseurl = 'https://example.com/';
/**
* @var string Save path of Sitemap File(s)
*/
public $target = './';
/**
* @var int Url limitation of Sitemap File(s)
*/
public $limit = 25000;
/**
* @ignore
*/
private $urls = array();
/**
* @ignore
*/
private $sitemaps = array();
/**
* Add URL to Sitemap
*
* @param url $loc Relative Url (without baseurl)
* @param string $lastmod Date or Datetime string (any format)
* @param string $changefreq Sitemap Spec. [always|hourly|daily|weekly|monthly|yearly|never]
* @param float $priority Sitemap Spec. Must between 0.0 and 1.0
* @return void
*/
public function add( $loc, $lastmod = '', $changefreq = '', $priority = '' ) {
$res = array();
/* Check Loc */
if ( $loc == '' ) {
return false;
} else {
$res['loc'] = rtrim( $this->baseurl, '/' ) . $loc;
}
/* Check lastmod */
if ( $lastmod !== '' && strtotime( $lastmod ) > 0 ) {
$lastmod = date( "c", strtotime( $lastmod ) );
$res['lastmod'] = $lastmod;
}
/* Check changefreq */
if ( $changefreq !== '' && in_array( $changefreq, array( 'always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never' ) ) ) {
$res['changefreq'] = $changefreq;
}
/* Check priority */
if ( $priority !== '' && $priority * 10 >= 0 && $priority * 10 <= 10 ) {
$priority = number_format( ( float ) $priority, 1, '.', null );
$res['priority'] = $priority;
}
$this->urls[] = $res;
}
/**
* Generate and Save Sitemap Files
*
* @return void
*/
public function generate() {
foreach ( glob( $this->target . '/sitemap*.xml' ) as $unlink ) {
unlink( $this->target . '/' . basename( $unlink ) );
}
$this->generateSitemaps();
$this->generateSitemapIndex();
}
/**
* Generate Sitemap File(s)
* @ignore
*/
private function generateSitemaps() {
/** XML Init */
$doc = new DOMDocument( '1.0', 'UTF-8' );
$doc->formatOutput = false;
$urlset = $doc->createElement( 'urlset' );
$attr = $doc->createAttribute( 'xmlns' );
$attr->value = 'http://www.sitemaps.org/schemas/sitemap/0.9';
$urlset->appendChild( $attr );
$root = $doc->appendChild( $urlset );
/* Loop URLs */
$i = 0;
foreach ( $this->urls as $key => $urlset ) {
if ( ++$i > $this->limit ) {
break;
}
$node = $root->appendChild( $doc->createElement( 'url' ) );
if ( isset( $urlset['loc'] ) ) {
$node->appendChild( $doc->createElement( 'loc', $urlset['loc'] ) );
}
if ( isset( $urlset['lastmod'] ) ) {
$node->appendChild( $doc->createElement( 'lastmod', $urlset['lastmod'] ) );
}
if ( isset( $urlset['changefreq'] ) ) {
$node->appendChild( $doc->createElement( 'changefreq', $urlset['changefreq'] ) );
}
if ( isset( $urlset['priority'] ) ) {
$node->appendChild( $doc->createElement( 'priority', $urlset['priority'] ) );
}
unset( $this->urls[$key] );
}
/* Save Sitemaps */
$file_index = (!empty( $this->urls ) || !empty( $this->sitemaps )) ? count( $this->sitemaps ) + 1 : '';
$file_name = sprintf( '/sitemap%s.xml', $file_index );
file_put_contents( $this->target . $file_name, $doc->saveXML() );
/* Add Sitemap to Sitemap Index */
if ( !empty( $this->urls ) || !empty( $this->sitemaps ) ) {
$this->sitemaps[] = rtrim( $this->baseurl, '/' ) . $file_name;
}
/* Check More Urls */
if ( !empty( $this->urls ) ) {
$this->generateSitemaps();
}
}
/**
* Generate Sitemap Index File
* @ignore
*/
private function generateSitemapIndex() {
if ( empty( $this->sitemaps ) ) {
return false;
}
/** XML Init */
$doc = new DOMDocument( '1.0', 'UTF-8' );
$doc->formatOutput = false;
$root = $doc->createElement( 'sitemapindex' );
$attr = $doc->createAttribute( 'xmlns' );
$attr->value = 'http://www.sitemaps.org/schemas/sitemap/0.9';
$root->appendChild( $attr );
$root = $doc->appendChild( $root );
/* Loop Sitemaps */
foreach ( $this->sitemaps as $url ) {
$node = $root->appendChild( $doc->createElement( 'sitemap' ) );
$loc = $node->appendChild( $doc->createElement( 'loc', $url ) );
$loc = $node->appendChild( $doc->createElement( 'lastmod', date( 'c' ) ) );
}
/* Save Sitmap Index */
$file_save = sprintf( '%s/sitemap.xml', $this->target );
file_put_contents( $file_save, $doc->saveXML() );
}
}