4 * Sitemap handling functions
6 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author Michael Hamann <michael@content-space.de>
10 namespace dokuwiki\Sitemap
;
12 use dokuwiki\Extension\Event
;
13 use dokuwiki\HTTP\DokuHTTPClient
;
17 * A class for building sitemaps and pinging search engines with the sitemap URL.
19 * @author Michael Hamann
24 * Builds a Google Sitemap of all public pages known to the indexer
26 * The map is placed in the cache directory named sitemap.xml.gz - This
27 * file needs to be writable!
29 * @author Michael Hamann
30 * @author Andreas Gohr
31 * @link https://www.google.com/webmasters/sitemaps/docs/en/about.html
32 * @link http://www.sitemaps.org/
36 public static function generate()
39 if ($conf['sitemap'] < 1 ||
!is_numeric($conf['sitemap'])) return false;
41 $sitemap = Mapper
::getFilePath();
43 if (file_exists($sitemap)) {
44 if (!is_writable($sitemap)) return false;
45 } elseif (!is_writable(dirname($sitemap))) {
50 @filesize
($sitemap) &&
51 @filemtime
($sitemap) > (time() - ($conf['sitemap'] * 86400))
53 Logger
::debug('Sitemapper::generate(): Sitemap up to date');
57 Logger
::debug("Sitemapper::generate(): using $sitemap");
59 $pages = idx_get_indexer()->getPages();
60 Logger
::debug('Sitemapper::generate(): creating sitemap using ' . count($pages) . ' pages');
63 // build the sitemap items
64 foreach ($pages as $id) {
65 //skip hidden, non existing and restricted files
66 if (isHiddenPage($id)) continue;
67 if (auth_aclcheck($id, '', []) < AUTH_READ
) continue;
68 $item = Item
::createFromID($id);
69 if ($item instanceof Item
)
73 $eventData = ['items' => &$items, 'sitemap' => &$sitemap];
74 $event = new Event('SITEMAP_GENERATE', $eventData);
75 if ($event->advise_before(true)) {
76 //save the new sitemap
77 $event->result
= io_saveFile($sitemap, (new Mapper())->getXML($items));
79 $event->advise_after();
81 return $event->result
;
85 * Builds the sitemap XML string from the given array auf SitemapItems.
87 * @param $items array The SitemapItems that shall be included in the sitemap.
88 * @return string The sitemap XML.
90 * @author Michael Hamann
92 private function getXML($items)
95 echo '<?xml version="1.0" encoding="UTF-8"?>' . NL
;
96 echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . NL
;
97 foreach ($items as $item) {
98 /** @var Item $item */
101 echo '</urlset>' . NL
;
102 $result = ob_get_contents();
108 * Helper function for getting the path to the sitemap file.
110 * @return string The path to the sitemap file.
112 * @author Michael Hamann
114 public static function getFilePath()
118 $sitemap = $conf['cachedir'] . '/sitemap.xml';
119 if (self
::sitemapIsCompressed()) {
127 * Helper function for checking if the sitemap is compressed
129 * @return bool If the sitemap file is compressed
131 public static function sitemapIsCompressed()
134 return $conf['compression'] === 'bz2' ||
$conf['compression'] === 'gz';
138 * Pings search engines with the sitemap url. Plugins can add or remove
139 * urls to ping using the SITEMAP_PING event.
141 * @author Michael Hamann
145 public static function pingSearchEngines()
147 //ping search engines...
148 $http = new DokuHTTPClient();
151 $encoded_sitemap_url = urlencode(wl('', ['do' => 'sitemap'], true, '&'));
153 'google' => 'https://www.google.com/ping?sitemap=' . $encoded_sitemap_url,
154 'yandex' => 'https://webmaster.yandex.com/ping?sitemap=' . $encoded_sitemap_url
158 'ping_urls' => $ping_urls,
159 'encoded_sitemap_url' => $encoded_sitemap_url
161 $event = new Event('SITEMAP_PING', $data);
162 if ($event->advise_before(true)) {
163 foreach ($data['ping_urls'] as $name => $url) {
164 Logger
::debug("Sitemapper::PingSearchEngines(): pinging $name");
165 $resp = $http->get($url);
167 Logger
::debug("Sitemapper:pingSearchengines(): $http->error", $resp);
171 $event->advise_after();