|
1
|
|
|
<?php |
|
2
|
|
|
/*************************************************************************** |
|
3
|
|
|
* For license information see doc/license.txt |
|
4
|
|
|
* |
|
5
|
|
|
* Generate sitemap.xml as specified by http://www.sitemaps.org |
|
6
|
|
|
* And send ping to search engines |
|
7
|
|
|
***************************************************************************/ |
|
8
|
|
|
|
|
9
|
|
|
namespace OcLegacy\Util; |
|
10
|
|
|
|
|
11
|
|
|
class SiteMapXml |
|
12
|
|
|
{ |
|
13
|
|
|
public $sDefaultChangeFreq = 'monthly'; |
|
14
|
|
|
public $nMaxFileSize = 9961472; // max file size, 10MB by specification |
|
15
|
|
|
public $nMaxUrlCount = 50000; // max number of URLs per file, 50000 by specification |
|
16
|
|
|
|
|
17
|
|
|
public $sPath = ''; |
|
18
|
|
|
public $sDomain = ''; |
|
19
|
|
|
public $oIndexFile; |
|
20
|
|
|
public $nSiteMapIndex = 0; |
|
21
|
|
|
public $oSiteMapFile; |
|
22
|
|
|
public $nWrittenSize = 0; |
|
23
|
|
|
public $nWrittenCount = 0; |
|
24
|
|
|
|
|
25
|
|
|
public function open($sPath, $sDomain) |
|
26
|
|
|
{ |
|
27
|
|
|
if (substr($sPath, - 1, 1) != '/') { |
|
28
|
|
|
$sPath .= '/'; |
|
29
|
|
|
} |
|
30
|
|
|
if (substr($sDomain, - 1, 1) != '/') { |
|
31
|
|
|
$sDomain .= '/'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$this->sPath = $sPath; |
|
35
|
|
|
$this->sDomain = $sDomain; |
|
36
|
|
|
|
|
37
|
|
|
$this->oIndexFile = fopen($sPath . 'sitemap.xml', 'wb'); |
|
38
|
|
|
if ($this->oIndexFile === false) { |
|
39
|
|
|
return false; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
fwrite($this->oIndexFile, '<?xml version="1.0" encoding="UTF-8"?>' . "\n"); |
|
43
|
|
|
fwrite($this->oIndexFile, '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/* sChaneFreq = {always, hourly, daily, weekly, monthly, yearly, never} |
|
47
|
|
|
* nPriority = {0.0 to 1.0} |
|
48
|
|
|
*/ |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $sFile |
|
52
|
|
|
* @param integer $dLastMod |
|
53
|
|
|
* @param string $sChangeFreq |
|
54
|
|
|
* @param float $nPriority |
|
55
|
|
|
*/ |
|
56
|
|
|
public function write($sFile, $dLastMod, $sChangeFreq, $nPriority = 0.5) |
|
57
|
|
|
{ |
|
58
|
|
|
if (!$sChangeFreq) { |
|
59
|
|
|
$sChangeFreq = $this->sDefaultChangeFreq; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$sXML = '<url>'; |
|
63
|
|
|
$sXML .= '<loc>' . xmlentities($this->sDomain . $sFile) . '</loc>'; |
|
64
|
|
|
$sXML .= '<lastmod>' . xmlentities(date('c', $dLastMod)) . '</lastmod>'; |
|
65
|
|
|
$sXML .= '<changefreq>' . xmlentities($sChangeFreq) . '</changefreq>'; |
|
66
|
|
|
$sXML .= '<priority>' . xmlentities($nPriority) . '</priority>'; |
|
67
|
|
|
$sXML .= '</url>'."\n"; |
|
68
|
|
|
|
|
69
|
|
|
$this->writeInternal($sXML); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $str |
|
74
|
|
|
*/ |
|
75
|
|
|
public function writeInternal($str) |
|
76
|
|
|
{ |
|
77
|
|
|
// close the last file? |
|
78
|
|
|
if (($this->oSiteMapFile) && (($this->nWrittenSize + strlen($str) > $this->nMaxFileSize) || ($this->nWrittenCount >= $this->nMaxUrlCount))) { |
|
79
|
|
|
gzwrite($this->oSiteMapFile, '</urlset>'); |
|
80
|
|
|
gzclose($this->oSiteMapFile); |
|
81
|
|
|
$this->oSiteMapFile = null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// open new XML file? |
|
85
|
|
|
if (!$this->oSiteMapFile) { |
|
86
|
|
|
$this->nSiteMapIndex++; |
|
87
|
|
|
$sFilename = 'sitemap-' . $this->nSiteMapIndex . '.xml.gz'; |
|
88
|
|
|
$this->oSiteMapFile = gzopen($this->sPath . $sFilename, 'wb'); |
|
89
|
|
|
|
|
90
|
|
|
fwrite( |
|
91
|
|
|
$this->oIndexFile, |
|
92
|
|
|
'<sitemap><loc>' . xmlentities($this->sDomain . $sFilename) . '</loc>' . |
|
93
|
|
|
'<lastmod>' . xmlentities(date('c')) . '</lastmod></sitemap>' |
|
94
|
|
|
); |
|
95
|
|
|
|
|
96
|
|
|
gzwrite($this->oSiteMapFile, '<?xml version="1.0" encoding="UTF-8"?>' . "\n"); |
|
97
|
|
|
gzwrite($this->oSiteMapFile, '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'); |
|
98
|
|
|
// includes end of xml-tag |
|
99
|
|
|
$this->nWrittenSize = 108; |
|
100
|
|
|
$this->nWrittenCount = 0; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
// write string to XML |
|
104
|
|
|
gzwrite($this->oSiteMapFile, $str); |
|
105
|
|
|
$this->nWrittenSize += strlen($str); |
|
106
|
|
|
$this->nWrittenCount++; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function close() |
|
110
|
|
|
{ |
|
111
|
|
|
if ($this->oSiteMapFile) { |
|
112
|
|
|
gzwrite($this->oSiteMapFile, '</urlset>'); |
|
113
|
|
|
gzclose($this->oSiteMapFile); |
|
114
|
|
|
$this->oSiteMapFile = null; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if ($this->oIndexFile !== false) { |
|
118
|
|
|
fwrite($this->oIndexFile, '</sitemapindex>'); |
|
119
|
|
|
fclose($this->oIndexFile); |
|
120
|
|
|
$this->oIndexFile = false; |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|