1<?php
2
3namespace dokuwiki\Action;
4
5use dokuwiki\Action\Exception\FatalException;
6use dokuwiki\Sitemap\Mapper;
7
8/**
9 * Class Sitemap
10 *
11 * Generate an XML sitemap for search engines. Do not confuse with Index
12 *
13 * @package dokuwiki\Action
14 */
15class Sitemap extends AbstractAction {
16
17    /** @inheritdoc */
18    public function minimumPermission() {
19        return AUTH_NONE;
20    }
21
22    /**
23     * Handle sitemap delivery
24     *
25     * @author Michael Hamann <michael@content-space.de>
26     * @throws FatalException
27     * @inheritdoc
28     */
29    public function preProcess() {
30        global $conf;
31
32        if($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
33            throw new FatalException('Sitemap generation is disabled', 404);
34        }
35
36        $sitemap = Mapper::getFilePath();
37        if(Mapper::sitemapIsCompressed()) {
38            $mime = 'application/x-gzip';
39        } else {
40            $mime = 'application/xml; charset=utf-8';
41        }
42
43        // Check if sitemap file exists, otherwise create it
44        if(!is_readable($sitemap)) {
45            Mapper::generate();
46        }
47
48        if(is_readable($sitemap)) {
49            // Send headers
50            header('Content-Type: ' . $mime);
51            header('Content-Disposition: attachment; filename=' . \dokuwiki\Utf8\PhpString::basename($sitemap));
52
53            http_conditionalRequest(filemtime($sitemap));
54
55            // Send file
56            //use x-sendfile header to pass the delivery to compatible webservers
57            http_sendfile($sitemap);
58
59            readfile($sitemap);
60            exit;
61        }
62
63        throw new FatalException('Could not read the sitemap file - bad permissions?');
64    }
65
66}
67