| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* vim: set noai expandtab ts=4 st=4 sw=4: */
|
|
|
3 |
|
|
|
4 |
/**
|
|
|
5 |
* Generate sitemap files. See http://www.sitemaps.org/protocol.php
|
|
|
6 |
* for more details.
|
|
|
7 |
*
|
|
|
8 |
* PHP versions 5
|
|
|
9 |
*
|
|
|
10 |
* Redistribution and use in source and binary forms, with or without
|
|
|
11 |
* modification, are permitted provided that the following conditions are met:
|
|
|
12 |
*
|
|
|
13 |
* * Redistributions of source code must retain the above copyright notice,
|
|
|
14 |
* this list of conditions and the following disclaimer.
|
|
|
15 |
* * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
16 |
* this list of conditions and the following disclaimer in the documentation
|
|
|
17 |
* and/or other materials provided with the distribution.
|
|
|
18 |
* * The names of its contributors may not be used to endorse or promote
|
|
|
19 |
* products derived from this software without specific prior written
|
|
|
20 |
* permission.
|
|
|
21 |
*
|
|
|
22 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
23 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
24 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
25 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
26 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
27 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
28 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
29 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
30 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
31 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
32 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
33 |
*
|
|
|
34 |
* @category File
|
|
|
35 |
* @package File_Sitemap
|
|
|
36 |
* @author Charles Brunet <cbrunet@php.net>
|
|
|
37 |
* @license http://www.opensource.org/licenses/bsd-license.html BSD License
|
|
|
38 |
* @version CVS: $Id: Sitemap.php 299129 2010-05-07 22:21:04Z cbrunet $
|
|
|
39 |
* @link http://pear.php.net/package/File_Sitemap
|
|
|
40 |
*/
|
|
|
41 |
|
|
|
42 |
require_once "File/Sitemap/Base.php";
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* Generate sitemap files. See http://www.sitemaps.org/protocol.php
|
|
|
46 |
* for more details.
|
|
|
47 |
*
|
|
|
48 |
* @category File
|
|
|
49 |
* @package File_Sitemap
|
|
|
50 |
* @author Charles Brunet <cbrunet@php.net>
|
|
|
51 |
* @license http://www.opensource.org/licenses/bsd-license.html BSD License
|
|
|
52 |
* @version Release: 0.1.3
|
|
|
53 |
* @link http://pear.php.net/package/File_Sitemap
|
|
|
54 |
*/
|
|
|
55 |
class File_Sitemap extends File_Sitemap_Base
|
|
|
56 |
{
|
|
|
57 |
|
|
|
58 |
const SCHEMA = 'http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd';
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Constructor. Build an empty XML document, with xmlns and root element.
|
|
|
62 |
*
|
|
|
63 |
* @access public
|
|
|
64 |
* @return void
|
|
|
65 |
*/
|
|
|
66 |
public function __construct()
|
|
|
67 |
{
|
|
|
68 |
parent::__construct('urlset', self::SCHEMA);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Add or update a location in current sitemap
|
|
|
73 |
*
|
|
|
74 |
* @param mixed $loc string | array. URL (or array of URL).
|
|
|
75 |
* Must contains protocol (http://) and trailling slash.
|
|
|
76 |
* @param float $priority A number between 0.0 and 1.0 describing
|
|
|
77 |
* relative priority. Default: 0.5
|
|
|
78 |
* @param string $changefreq Optional. Must be 'always', 'houly', 'daily',
|
|
|
79 |
* 'weekly', 'monthly', 'yearly' or 'never'.
|
|
|
80 |
* @param string $lastmod Optional. Date (and time) of last page
|
|
|
81 |
* modification.
|
|
|
82 |
*
|
|
|
83 |
* @return void
|
|
|
84 |
*/
|
|
|
85 |
public function add($loc, $priority = 0.5, $changefreq = null,
|
|
|
86 |
$lastmod = null
|
|
|
87 |
) {
|
|
|
88 |
if (!is_array($loc)) {
|
|
|
89 |
$loc = array($loc);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
foreach ($loc as $l) {
|
|
|
93 |
// normalize and encode $l
|
|
|
94 |
$l = $this->parseURL($l);
|
|
|
95 |
|
|
|
96 |
// look for this url into the dom tree
|
|
|
97 |
$url = $this->findLoc($l);
|
|
|
98 |
if ($url === false) {
|
|
|
99 |
// Create the url node, and append l node
|
|
|
100 |
$url = $this->dom->createElementNS(self::XMLNS, 'url');
|
|
|
101 |
|
|
|
102 |
$elemLoc = $this->dom->createElementNS(self::XMLNS, 'loc', $l);
|
|
|
103 |
$url->appendChild($elemLoc);
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
if ($lastmod !== null) {
|
|
|
107 |
$lastmod = $this->parseDateTime($lastmod);
|
|
|
108 |
$this->updateNode($url, 'lastmod', $lastmod);
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
if ($changefreq !== null) {
|
|
|
112 |
$changefreq = $this->parseChangefreq($changefreq);
|
|
|
113 |
$this->updateNode($url, 'changefreq', $changefreq);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
if ($priority !== null) {
|
|
|
117 |
$priority = $this->parsePriority($priority);
|
|
|
118 |
$this->updateNode($url, 'priority', $priority);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
$this->dom->documentElement->appendChild($url);
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
/**
|
|
|
126 |
* Ensure that priority is a number between 0.0 and 1.0
|
|
|
127 |
*
|
|
|
128 |
* @param float $priority A number between 0.0 and 1.0
|
|
|
129 |
*
|
|
|
130 |
* @return string
|
|
|
131 |
*
|
|
|
132 |
* @throws {@link File_Sitemap_Exception} Priority is not a number.
|
|
|
133 |
*/
|
|
|
134 |
protected function parsePriority($priority)
|
|
|
135 |
{
|
|
|
136 |
if (!is_numeric($priority)) {
|
|
|
137 |
throw new File_Sitemap_Exception(
|
|
|
138 |
'priority must be a number between 0.0 and 1.0.',
|
|
|
139 |
File_Sitemap_Exception::PARSE_ERROR);
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
$priority = (float) $priority;
|
|
|
143 |
if ($priority > 1.0) {
|
|
|
144 |
$priority = 1.0;
|
|
|
145 |
} elseif ($priority < 0.0) {
|
|
|
146 |
$priority = 0.0;
|
|
|
147 |
}
|
|
|
148 |
$priority = (string) $priority;
|
|
|
149 |
// Apending .0 will ensure that 0 and 1 will give 0.0 and 1.0
|
|
|
150 |
$priority = substr($priority.'.0', 0, 3);
|
|
|
151 |
return $priority;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* Ensure that $changefreq parameter is valid.
|
|
|
156 |
*
|
|
|
157 |
* @param string $changefreq A valid changefreq parameter: always, hourly,
|
|
|
158 |
* daily, weekly, monthly, yearly or never.
|
|
|
159 |
*
|
|
|
160 |
* @return string
|
|
|
161 |
*
|
|
|
162 |
* @throws {@link File_Sitemap_Exception} changefreq not valid.
|
|
|
163 |
*/
|
|
|
164 |
protected function parseChangefreq($changefreq)
|
|
|
165 |
{
|
|
|
166 |
// I don't know why, but when changefreq === 0, it validates
|
|
|
167 |
// if I don't do that...
|
|
|
168 |
if ($changefreq === 0) {
|
|
|
169 |
$changefreq = '';
|
|
|
170 |
}
|
|
|
171 |
switch ($changefreq) {
|
|
|
172 |
case 'always':
|
|
|
173 |
case 'hourly':
|
|
|
174 |
case 'daily':
|
|
|
175 |
case 'weekly':
|
|
|
176 |
case 'monthly':
|
|
|
177 |
case 'yearly':
|
|
|
178 |
case 'never':
|
|
|
179 |
break;
|
|
|
180 |
default:
|
|
|
181 |
throw new File_Sitemap_Exception(
|
|
|
182 |
'changefreq must be one of always, hourly, daily, weekly, '.
|
|
|
183 |
'monthly, yearly or never.',
|
|
|
184 |
File_Sitemap_Exception::PARSE_ERROR);
|
|
|
185 |
}
|
|
|
186 |
return $changefreq;
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
/**
|
|
|
190 |
* Validate sitemap against its schema definition.
|
|
|
191 |
*
|
|
|
192 |
* @return boolean
|
|
|
193 |
*/
|
|
|
194 |
public function validate()
|
|
|
195 |
{
|
|
|
196 |
return parent::validate(self::SCHEMA);
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
?>
|