xref: /dokuwiki/inc/Action/Sitemap.php (revision b6c12ffa53a4dccdb3f966e5b6bd24ad9eafc496)
164ab5140SAndreas Gohr<?php
264ab5140SAndreas Gohr
364ab5140SAndreas Gohrnamespace dokuwiki\Action;
464ab5140SAndreas Gohr
564ab5140SAndreas Gohruse dokuwiki\Action\Exception\FatalException;
6*b6c12ffaSAndreas Gohruse dokuwiki\Sitemap\Mapper;
764ab5140SAndreas Gohr
8ab583a1bSAndreas Gohr/**
9ab583a1bSAndreas Gohr * Class Sitemap
10ab583a1bSAndreas Gohr *
11ab583a1bSAndreas Gohr * Generate an XML sitemap for search engines. Do not confuse with Index
12ab583a1bSAndreas Gohr *
13ab583a1bSAndreas Gohr * @package dokuwiki\Action
14ab583a1bSAndreas Gohr */
1564ab5140SAndreas Gohrclass Sitemap extends AbstractAction {
1664ab5140SAndreas Gohr
1764ab5140SAndreas Gohr    /** @inheritdoc */
18ec701221SAndreas Gohr    public function minimumPermission() {
1964ab5140SAndreas Gohr        return AUTH_NONE;
2064ab5140SAndreas Gohr    }
2164ab5140SAndreas Gohr
2264ab5140SAndreas Gohr    /**
2364ab5140SAndreas Gohr     * Handle sitemap delivery
2464ab5140SAndreas Gohr     *
2564ab5140SAndreas Gohr     * @author Michael Hamann <michael@content-space.de>
2664ab5140SAndreas Gohr     * @throws FatalException
2764ab5140SAndreas Gohr     * @inheritdoc
2864ab5140SAndreas Gohr     */
2964ab5140SAndreas Gohr    public function preProcess() {
3064ab5140SAndreas Gohr        global $conf;
3164ab5140SAndreas Gohr
3264ab5140SAndreas Gohr        if($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) {
336c8673feSAndreas Gohr            throw new FatalException('Sitemap generation is disabled', 404);
3464ab5140SAndreas Gohr        }
3564ab5140SAndreas Gohr
36*b6c12ffaSAndreas Gohr        $sitemap = Mapper::getFilePath();
37*b6c12ffaSAndreas Gohr        if(Mapper::sitemapIsCompressed()) {
3864ab5140SAndreas Gohr            $mime = 'application/x-gzip';
3964ab5140SAndreas Gohr        } else {
4064ab5140SAndreas Gohr            $mime = 'application/xml; charset=utf-8';
4164ab5140SAndreas Gohr        }
4264ab5140SAndreas Gohr
4364ab5140SAndreas Gohr        // Check if sitemap file exists, otherwise create it
4464ab5140SAndreas Gohr        if(!is_readable($sitemap)) {
45*b6c12ffaSAndreas Gohr            Mapper::generate();
4664ab5140SAndreas Gohr        }
4764ab5140SAndreas Gohr
4864ab5140SAndreas Gohr        if(is_readable($sitemap)) {
4964ab5140SAndreas Gohr            // Send headers
5064ab5140SAndreas Gohr            header('Content-Type: ' . $mime);
518cbc5ee8SAndreas Gohr            header('Content-Disposition: attachment; filename=' . \dokuwiki\Utf8\PhpString::basename($sitemap));
5264ab5140SAndreas Gohr
5364ab5140SAndreas Gohr            http_conditionalRequest(filemtime($sitemap));
5464ab5140SAndreas Gohr
5564ab5140SAndreas Gohr            // Send file
5664ab5140SAndreas Gohr            //use x-sendfile header to pass the delivery to compatible webservers
5764ab5140SAndreas Gohr            http_sendfile($sitemap);
5864ab5140SAndreas Gohr
5964ab5140SAndreas Gohr            readfile($sitemap);
6064ab5140SAndreas Gohr            exit;
6164ab5140SAndreas Gohr        }
6264ab5140SAndreas Gohr
636c8673feSAndreas Gohr        throw new FatalException('Could not read the sitemap file - bad permissions?');
6464ab5140SAndreas Gohr    }
6564ab5140SAndreas Gohr
6664ab5140SAndreas Gohr}
67