forked from ahutchings/kohana-sitemap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
URL.php
191 lines (159 loc) · 4.66 KB
/
URL.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
<?php defined('SYSPATH') or die('No direct script access.');
abstract class Kohana_Sitemap_URL implements Kohana_Sitemap_Interface
{
private $attributes = array
(
'loc' => NULL,
'lastmod' => NULL,
'changefreq' => NULL,
'priority' => NULL,
);
private $alternates = array();
/**
* URL of the page. This URL must begin with the protocol (such as http) and end
* with a trailing slash, if your web server requires it. This value must be
* less than 2,048 characters.
* @see http://www.sitemaps.org/protocol.php
* @param string $location
*/
public function set_loc($location)
{
if ( ! Valid::max_length($location, 2048))
{
throw new LengthException('The location was too long, maximum length of 2,048 characters.');
}
$location = Sitemap::encode($location);
if ( ! Valid::url($location))
{
throw new InvalidArgumentException('The location was not a valid URL');
}
$this->attributes['loc'] = $location;
return $this;
}
/**
* The date of last modification of the file.
* @param integer $lastmod Unix timestamp
*/
public function set_last_mod($lastmod)
{
$this->attributes['lastmod'] = Sitemap::date_format($lastmod);
return $this;
}
/**
* How frequently the page is likely to change
* @param string $change_frequency
*/
public function set_change_frequency($change_frequency)
{
$change_frequency = (string) $change_frequency;
$frequencies = array('always', 'hourly', 'daily', 'weekly', 'monthly', 'yearly', 'never');
if ( ! in_array($change_frequency, $frequencies))
{
throw new InvalidArgumentException('Invalid change frequency');
}
$this->attributes['changefreq'] = $change_frequency;
return $this;
}
/**
* The priority of this URL relative to other URLs on your site. Ranges from
* 0 to 1, the default is 0.5
* @param integer $priority
*/
public function set_priority($priority)
{
if ( ! is_numeric($priority))
{
throw new InvalidArgumentException('The priority was not a numeric value.');
}
if ($priority > 1 OR $priority < 0)
{
throw new RangeException('Priority must be between 0 and 1 (inclusive).');
}
/*
* @TODO: Deal with locales that don't use a period as their decimal point.
*/
$this->attributes['priority'] = $priority;
return $this;
}
/**
* Submit rel-alternate-hreflang annotations in a Sitemap
* @see https://support.google.com/webmasters/answer/2620865?hl=en
* @param string $lang
* @param string $location
*/
public function set_alternate($lang,$location)
{
if ( ! Valid::max_length($location, 2048))
{
throw new LengthException('The location was too long, maximum length of 2,048 characters.');
}
$location = Sitemap::encode($location);
if ( ! Valid::url($location))
{
throw new InvalidArgumentException('The location was not a valid URL');
}
$alternate = array('lang' => $lang,'location' => $location);
$this->alternates[] = $alternate;
return $this;
}
/**
* @var Kohana_Sitemap_Interface
*/
private $driver = NULL;
/**
*
* @param <type> $driver
*/
public function __construct(Kohana_Sitemap_Interface $driver = NULL)
{
$this->driver = $driver;
}
/**
* Creates the URL node and decorates it with additional sitemap information.
*/
public function create()
{
$document = new DOMDocument;
$url_node = $document->createElement('url');
foreach($this->attributes as $name => $value)
{
// The loc attribute is required.
if(NULL === $this->attributes['loc'])
{
throw new RuntimeException('loc is required');
}
// Add attributes that aren't empty.
if (NULL !== $value)
{
$url_node->appendChild(new DOMElement($name, $value));
}
}
if ($this->alternates != NULL)
{
foreach($this->alternates as $k => $alternate)
{
$xhtmllink = $url_node->appendChild(new DOMElement('link'));
$xhtmllink->setAttributeNode(new DOMAttr('rel', 'alternate'));
$xhtmllink->setAttributeNode(new DOMAttr('hreflang', $alternate['lang']));
$xhtmllink->setAttributeNode(new DOMAttr('href', $alternate['location']));
}
}
// If a specialised sitemap was used, import it's data here.
if (NULL !== $this->driver)
{
$url_node->appendChild($document->importNode($this->driver->create(), TRUE));
}
return $url_node;
}
public function root( DOMElement & $root )
{
// Add urlset namespace.
$root->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$root->setAttribute('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd');
if (NULL !== $this->driver)
{
$this->driver->root($root);
}
}
}