This repository has been archived by the owner on Jul 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsitemap.php
95 lines (75 loc) · 2.81 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
<?php
/*
* Sitemap generator
* Priority: medium
*/
require(__DIR__ . '/config.php');
require(__DIR__ . '/model/model.php');
require(__DIR__ . '/model/profile.php');
require(__DIR__ . '/model/listing.php');
$modelProfile = new ModelProfile(DB_DATABASE, DB_HOSTNAME, DB_PORT, DB_USERNAME, DB_PASSWORD);
$modelListing = new modelListing(DB_DATABASE, DB_HOSTNAME, DB_PORT, DB_USERNAME, DB_PASSWORD);
$url = [];
$lastmod = date('Y-m-d');
$timeStart = microtime(true);
// Collect profiles
$profilesTotal = 0;
$profilesIndex = 1;
foreach ($modelProfile->getSitemapProfiles() as $profile) {
$profilesTotal++;
if ($profilesTotal > ($profilesIndex * SITEMAP_ITEMS_PER_SITEMAP)) {
$profilesIndex++;
}
$url['profile'][$profilesIndex][] = [
'loc' => SITEMAP_DOMAIN . '/profile/' . $profile['peerId'],
'lastmod' => $lastmod,
];
}
// Collect listings
$listingsTotal = 0;
$listingsIndex = 1;
foreach ($modelListing->getSitemapListings() as $listing) {
$listingsTotal++;
if ($listingsTotal > ($listingsIndex * SITEMAP_ITEMS_PER_SITEMAP)) {
$listingsIndex++;
}
$url['listing'][$listingsIndex][] = [
'loc' => SITEMAP_DOMAIN . '/listing/' . $listing['hash'],
'lastmod' => $lastmod,
];
}
// Delete previous sitemap
foreach (scandir(SITEMAP_BASE) as $file) {
if (false !== strpos($file, 'sitemap')) {
if (file_exists(SITEMAP_BASE . '/' . $file)) {
unlink(SITEMAP_BASE . '/' . $file);
}
}
}
// Generate new sitemap urlsets
$sitemaps = [];
foreach ($url as $type => $indexes) {
foreach ($indexes as $index => $datas) {
$handle = fopen(sprintf("%s/sitemap.%s.%s.xml", SITEMAP_BASE, $type, $index), 'a');
$sitemaps[] = sprintf("%s/sitemap.%s.%s.xml", SITEMAP_DOMAIN, $type, $index);
fwrite($handle, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n");
foreach ($datas as $data) {
fwrite($handle, "\t<url>\n\t\t<loc>" . $data['loc'] . "</loc>\n\t\t<lastmod>" . $data['lastmod'] . "</lastmod>\n\t</url>\n");
}
fwrite($handle, "</urlset>");
fclose($handle);
}
}
// Generate new sitemap
$handle = fopen(sprintf("%s/sitemap.xml", SITEMAP_BASE), 'a');
fwrite($handle, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<sitemapindex xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n");
foreach ($sitemaps as $sitemap) {
fwrite($handle, "\t<sitemap>\n\t\t<loc>" . $sitemap . "</loc>\n\t\t<lastmod>" . $lastmod . "</lastmod>\n\t</sitemap>\n");
}
fwrite($handle, "</sitemapindex>");
fclose($handle);
$timeEnd = microtime(true);
// Debug output
echo sprintf("\nExecution time: %s\n\n", $timeEnd - $timeStart);
echo sprintf("Profiles: %s\n", $profilesTotal);
echo sprintf("Listings: %s\n\n", $listingsTotal);