-
Notifications
You must be signed in to change notification settings - Fork 52
/
xml-sitemap.php
185 lines (157 loc) · 4.75 KB
/
xml-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
<?php
/**
* XML Sitemap PHP Script
* For more info, see: https://github.com/jdevalk/XML-Sitemap-PHP-Script
* Copyright (C), 2011 - 2022 - Joost de Valk, [email protected]
*/
class Joost_XML_Sitemap_PHP {
const CONFIG_FILE = 'xml-sitemap-config.ini';
/**
* Holds the output.
*/
private string $output = '';
/**
* Holds the path.
*/
private string $path;
/**
* Holds the base URL.
*/
private string $url;
/**
* Array of filetypes we're adding to the XML sitemap.
*/
private array $filetypes;
/**
* Relative path to the XSL file.
*/
private string $xsl = 'xml-sitemap.xsl';
/**
* Files we ignore in our output.
*/
private array $ignore = [];
/**
* The change frequency of the files.
*/
private string $changefreq = '';
/**
* The priority of the file, between 0.1 and 1.
*/
private float $priority = 0;
/**
* Determines if we recurse directories or not.
*/
private bool $recursive = true;
/**
* URLs we replace with something else.
*/
private array $replacements;
/**
* Generates our XML sitemap.
*
* @return void
*/
public function generate(): void {
$this->read_ini();
$this->read_dir();
$this->output();
}
/**
* Read our ini file.
*
* @return void
*/
private function read_ini(): void {
if ( file_exists( './' . self::CONFIG_FILE ) ) {
$config = parse_ini_file( './' . self::CONFIG_FILE );
} elseif ( file_exists( '../' . self::CONFIG_FILE ) ) {
$config = parse_ini_file( '../' . self::CONFIG_FILE );
} else {
printf( 'Error: unable to load %1$s, please copy xml-sitemap-config-sample.ini to %1$s and adjust.' . PHP_EOL, self::CONFIG_FILE );
die( 1 );
}
$this->changefreq = (string) $config['changefreq'];
$this->path = (string) $config['directory'];
$this->url = (string) $config['directory_url'];
$this->filetypes = (array) $config['filetypes'];
$this->ignore = array_merge( $config['ignore'], [ '.', '..', 'xml-sitemap.php' ] );
$this->priority = (float) $config['priority'];
$this->recursive = (bool) $config['recursive'];
$this->replacements = (array) $config['replacements'];
$this->xsl = (string) $config['xsl'];
}
/**
* Kick off the script reading the dir provided in config.
*
* @return void
*/
private function read_dir(): void {
$this->parse_dir( $this->path, $this->url );
}
/**
* Reads a directory and adds files found to the output.
*
* @param string $dir The directory to read.
* @param string $url The base URL.
*
* @return void
*/
private function parse_dir( string $dir, string $url ): void {
$handle = opendir( $dir );
while ( false !== ( $file = readdir( $handle ) ) ) {
// Check if this file needs to be ignored, if so, skip it.
if ( in_array( utf8_encode( $file ), $this->ignore ) ) {
continue;
}
if ( $this->recursive && is_dir( $dir . $file ) ) {
$this->parse_dir( $dir . $file . '/', $url . $file . '/' );
}
// Check whether the file has on of the extensions allowed for this XML sitemap.
$extension = pathinfo( $dir . $file, PATHINFO_EXTENSION );
if ( empty( $extension ) || ! in_array( $extension, $this->filetypes ) ) {
continue;
}
// Create a W3C valid date for use in the XML sitemap based on the file modification time.
$file_mod_time = filemtime( $dir . $file );
if ( ! $file_mod_time ) {
$file_mod_time = filectime( $dir . $file );
}
$mod = date( 'c', $file_mod_time );
// Replace the file with its replacement from the settings, if needed.
if ( isset( $this->replacements[ $file ] ) ) {
$file = $this->replacements[ $file ];
}
// Start creating the output
$output = '<url>' . PHP_EOL;
$output .= "\t" . '<loc>' . $url . rawurlencode( $file ) . '</loc>' . PHP_EOL;
$output .= "\t" . '<lastmod>' . $mod . '</lastmod>' . PHP_EOL;
if ( ! empty( $this->changefreq ) ) {
$output .= "\t" . '<changefreq>' . $this->changefreq . '</changefreq>' . PHP_EOL;
}
if ( $this->priority > 0 && $this->priority <= 1 ) {
$output .= "\t" . '<priority>' . $this->priority . '</priority>' . PHP_EOL;
}
$output .= '</url>' . PHP_EOL;
$this->output .= $output;
}
closedir( $handle );
}
/**
* Output our XML sitemap.
*
* @return void
*/
private function output(): void {
// Sent the correct header so browsers display properly, with or without XSL.
header( 'Content-Type: application/xml' );
echo '<?xml version="1.0" encoding="utf-8"?>' . PHP_EOL;
if ( ! empty( $this->xsl ) ) {
echo '<?xml-stylesheet type="text/xsl" href="' . $this->url . $this->xsl . '"?>' . PHP_EOL;
}
echo '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;
echo $this->output;
echo '</urlset>' . PHP_EOL;
}
}
$joost_xml = new Joost_XML_Sitemap_PHP();
$joost_xml->generate();