Sitemap.php 1.7 KB

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