Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
73.08% |
19 / 26 |
|
42.86% |
3 / 7 |
CRAP | |
0.00% |
0 / 1 |
AbstractFile | |
73.08% |
19 / 26 |
|
42.86% |
3 / 7 |
11.95 | |
0.00% |
0 / 1 |
addUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getEntry | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
toString | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
3 | |||
write | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
isEmpty | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getCount | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
clear | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getExtraNamespaces | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * Abstract class for representing XML sitemaps |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Sitemap |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org Main Page |
28 | */ |
29 | |
30 | namespace VuFind\Sitemap; |
31 | |
32 | use function count; |
33 | use function dirname; |
34 | |
35 | /** |
36 | * Abstract class for representing XML sitemaps |
37 | * |
38 | * @category VuFind |
39 | * @package Sitemap |
40 | * @author Demian Katz <demian.katz@villanova.edu> |
41 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
42 | * @link https://vufind.org Main Page |
43 | */ |
44 | abstract class AbstractFile |
45 | { |
46 | /** |
47 | * Top-level tag. |
48 | * |
49 | * @var string |
50 | */ |
51 | protected $topTag; |
52 | |
53 | /** |
54 | * URLs in map. |
55 | * |
56 | * @var array |
57 | */ |
58 | protected $urls = []; |
59 | |
60 | /** |
61 | * Add a URL to the map. |
62 | * |
63 | * @param string|array $url URL as a string or as an associative array |
64 | * |
65 | * @return void |
66 | */ |
67 | public function addUrl($url) |
68 | { |
69 | $this->urls[] = $url; |
70 | } |
71 | |
72 | /** |
73 | * Translate a URL into an appropriate entry for this sitemap file. |
74 | * |
75 | * @param string|array $url URL as a string or as an associative array |
76 | * |
77 | * @return string XML fragment |
78 | */ |
79 | abstract protected function getEntry($url); |
80 | |
81 | /** |
82 | * Get the map as a string. |
83 | * |
84 | * @return string |
85 | */ |
86 | public function toString() |
87 | { |
88 | // Create entries first as they may affect the required namespaces: |
89 | $entries = ''; |
90 | foreach ($this->urls as $url) { |
91 | $entries .= $this->getEntry($url); |
92 | } |
93 | |
94 | // Construct XML: |
95 | $extraNs = $this->getExtraNamespaces(); |
96 | $extraNamespaces = $extraNs |
97 | ? (' ' . implode("\n ", array_unique($extraNs)) . "\n") |
98 | : ''; |
99 | $xml = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . |
100 | '<' . $this->topTag . "\n" . |
101 | ' xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' . "\n" . |
102 | $extraNamespaces . |
103 | ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . "\n" . |
104 | " xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9\n" . |
105 | ' http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">' . "\n\n" . |
106 | $entries . |
107 | '</' . $this->topTag . '>'; |
108 | |
109 | // Send it all back |
110 | return $xml; |
111 | } |
112 | |
113 | /** |
114 | * Write the map to a file on disk. |
115 | * |
116 | * @param string $file Target filename |
117 | * |
118 | * @return bool |
119 | */ |
120 | public function write($file) |
121 | { |
122 | // if a subfolder was specified that does not exist, make one |
123 | $dirname = dirname($file); |
124 | if (!is_dir($dirname)) { |
125 | mkdir($dirname, 0o755, true); |
126 | } |
127 | return file_put_contents($file, $this->toString()); |
128 | } |
129 | |
130 | /** |
131 | * Check if the sitemap is empty |
132 | * |
133 | * @return bool |
134 | */ |
135 | public function isEmpty(): bool |
136 | { |
137 | return !$this->urls; |
138 | } |
139 | |
140 | /** |
141 | * Get the count of items |
142 | * |
143 | * @return int |
144 | */ |
145 | public function getCount(): int |
146 | { |
147 | return count($this->urls); |
148 | } |
149 | |
150 | /** |
151 | * Remove all entries |
152 | * |
153 | * @return void |
154 | */ |
155 | public function clear(): void |
156 | { |
157 | $this->urls = []; |
158 | } |
159 | |
160 | /** |
161 | * Get any extra namespace declarations needed for the sitemap |
162 | * |
163 | * @return array |
164 | */ |
165 | protected function getExtraNamespaces() |
166 | { |
167 | return []; |
168 | } |
169 | } |