xref: /dokuwiki/inc/Action/Sitemap.php (revision dccd6b2bba7367e4d1d2d7aa84c9f9d15584b593)
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    {
22        return AUTH_NONE;
23    }
24
25    /**
26     * Handle sitemap delivery
27     *
28     * @author Michael Hamann <michael@content-space.de>
29     * @throws FatalException
30     * @inheritdoc
31     */
32    public function preProcess()
33    {
34        global $conf;
35
36        if($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
37            throw new FatalException('Sitemap generation is disabled', 404);
38        }
39
40        $sitemap = Mapper::getFilePath();
41        if(Mapper::sitemapIsCompressed()) {
42            $mime = 'application/x-gzip';
43        } else {
44            $mime = 'application/xml; charset=utf-8';
45        }
46
47        // Check if sitemap file exists, otherwise create it
48        if(!is_readable($sitemap)) {
49            Mapper::generate();
50        }
51
52        if(is_readable($sitemap)) {
53            // Send headers
54            header('Content-Type: ' . $mime);
55            header('Content-Disposition: attachment; filename=' . PhpString::basename($sitemap));
56
57            http_conditionalRequest(filemtime($sitemap));
58
59            // Send file
60            //use x-sendfile header to pass the delivery to compatible webservers
61            http_sendfile($sitemap);
62
63            readfile($sitemap);
64            exit;
65        }
66
67        throw new FatalException('Could not read the sitemap file - bad permissions?');
68    }
69}
70