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