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