164ab5140SAndreas Gohr<?php 264ab5140SAndreas Gohr 364ab5140SAndreas Gohrnamespace dokuwiki\Action; 464ab5140SAndreas Gohr 564ab5140SAndreas Gohruse dokuwiki\Action\Exception\FatalException; 6b6c12ffaSAndreas Gohruse dokuwiki\Sitemap\Mapper; 779a2d784SGerrit Uitslaguse dokuwiki\Utf8\PhpString; 864ab5140SAndreas Gohr 9ab583a1bSAndreas Gohr/** 10ab583a1bSAndreas Gohr * Class Sitemap 11ab583a1bSAndreas Gohr * 12ab583a1bSAndreas Gohr * Generate an XML sitemap for search engines. Do not confuse with Index 13ab583a1bSAndreas Gohr * 14ab583a1bSAndreas Gohr * @package dokuwiki\Action 15ab583a1bSAndreas Gohr */ 168c7c53b0SAndreas Gohrclass Sitemap extends AbstractAction 178c7c53b0SAndreas Gohr{ 1864ab5140SAndreas Gohr 1964ab5140SAndreas Gohr /** @inheritdoc */ 20*d868eb89SAndreas Gohr public function minimumPermission() 21*d868eb89SAndreas Gohr { 2264ab5140SAndreas Gohr return AUTH_NONE; 2364ab5140SAndreas Gohr } 2464ab5140SAndreas Gohr 2564ab5140SAndreas Gohr /** 2664ab5140SAndreas Gohr * Handle sitemap delivery 2764ab5140SAndreas Gohr * 2864ab5140SAndreas Gohr * @author Michael Hamann <michael@content-space.de> 2964ab5140SAndreas Gohr * @throws FatalException 3064ab5140SAndreas Gohr * @inheritdoc 3164ab5140SAndreas Gohr */ 32*d868eb89SAndreas Gohr public function preProcess() 33*d868eb89SAndreas Gohr { 3464ab5140SAndreas Gohr global $conf; 3564ab5140SAndreas Gohr 3664ab5140SAndreas Gohr if($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) { 376c8673feSAndreas Gohr throw new FatalException('Sitemap generation is disabled', 404); 3864ab5140SAndreas Gohr } 3964ab5140SAndreas Gohr 40b6c12ffaSAndreas Gohr $sitemap = Mapper::getFilePath(); 41b6c12ffaSAndreas Gohr if(Mapper::sitemapIsCompressed()) { 4264ab5140SAndreas Gohr $mime = 'application/x-gzip'; 4364ab5140SAndreas Gohr } else { 4464ab5140SAndreas Gohr $mime = 'application/xml; charset=utf-8'; 4564ab5140SAndreas Gohr } 4664ab5140SAndreas Gohr 4764ab5140SAndreas Gohr // Check if sitemap file exists, otherwise create it 4864ab5140SAndreas Gohr if(!is_readable($sitemap)) { 49b6c12ffaSAndreas Gohr Mapper::generate(); 5064ab5140SAndreas Gohr } 5164ab5140SAndreas Gohr 5264ab5140SAndreas Gohr if(is_readable($sitemap)) { 5364ab5140SAndreas Gohr // Send headers 5464ab5140SAndreas Gohr header('Content-Type: ' . $mime); 5579a2d784SGerrit Uitslag header('Content-Disposition: attachment; filename=' . PhpString::basename($sitemap)); 5664ab5140SAndreas Gohr 5764ab5140SAndreas Gohr http_conditionalRequest(filemtime($sitemap)); 5864ab5140SAndreas Gohr 5964ab5140SAndreas Gohr // Send file 6064ab5140SAndreas Gohr //use x-sendfile header to pass the delivery to compatible webservers 6164ab5140SAndreas Gohr http_sendfile($sitemap); 6264ab5140SAndreas Gohr 6364ab5140SAndreas Gohr readfile($sitemap); 6464ab5140SAndreas Gohr exit; 6564ab5140SAndreas Gohr } 6664ab5140SAndreas Gohr 676c8673feSAndreas Gohr throw new FatalException('Could not read the sitemap file - bad permissions?'); 6864ab5140SAndreas Gohr } 6964ab5140SAndreas Gohr} 70