Mapper.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Sitemap handling functions
  4. *
  5. * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
  6. * @author Michael Hamann <michael@content-space.de>
  7. */
  8. namespace dokuwiki\Sitemap;
  9. use dokuwiki\HTTP\DokuHTTPClient;
  10. use dokuwiki\Logger;
  11. /**
  12. * A class for building sitemaps and pinging search engines with the sitemap URL.
  13. *
  14. * @author Michael Hamann
  15. */
  16. class Mapper {
  17. /**
  18. * Builds a Google Sitemap of all public pages known to the indexer
  19. *
  20. * The map is placed in the cache directory named sitemap.xml.gz - This
  21. * file needs to be writable!
  22. *
  23. * @author Michael Hamann
  24. * @author Andreas Gohr
  25. * @link https://www.google.com/webmasters/sitemaps/docs/en/about.html
  26. * @link http://www.sitemaps.org/
  27. *
  28. * @return bool
  29. */
  30. public static function generate(){
  31. global $conf;
  32. if($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) return false;
  33. $sitemap = Mapper::getFilePath();
  34. if(file_exists($sitemap)){
  35. if(!is_writable($sitemap)) return false;
  36. }else{
  37. if(!is_writable(dirname($sitemap))) return false;
  38. }
  39. if(@filesize($sitemap) &&
  40. @filemtime($sitemap) > (time()-($conf['sitemap']*86400))){ // 60*60*24=86400
  41. Logger::debug('Sitemapper::generate(): Sitemap up to date');
  42. return false;
  43. }
  44. Logger::debug("Sitemapper::generate(): using $sitemap");
  45. $pages = idx_get_indexer()->getPages();
  46. Logger::debug('Sitemapper::generate(): creating sitemap using '.count($pages).' pages');
  47. $items = array();
  48. // build the sitemap items
  49. foreach($pages as $id){
  50. //skip hidden, non existing and restricted files
  51. if(isHiddenPage($id)) continue;
  52. if(auth_aclcheck($id,'',array()) < AUTH_READ) continue;
  53. $item = Item::createFromID($id);
  54. if ($item !== null)
  55. $items[] = $item;
  56. }
  57. $eventData = array('items' => &$items, 'sitemap' => &$sitemap);
  58. $event = new \dokuwiki\Extension\Event('SITEMAP_GENERATE', $eventData);
  59. if ($event->advise_before(true)) {
  60. //save the new sitemap
  61. $event->result = io_saveFile($sitemap, Mapper::getXML($items));
  62. }
  63. $event->advise_after();
  64. return $event->result;
  65. }
  66. /**
  67. * Builds the sitemap XML string from the given array auf SitemapItems.
  68. *
  69. * @param $items array The SitemapItems that shall be included in the sitemap.
  70. * @return string The sitemap XML.
  71. *
  72. * @author Michael Hamann
  73. */
  74. private static function getXML($items) {
  75. ob_start();
  76. echo '<?xml version="1.0" encoding="UTF-8"?>'.NL;
  77. echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'.NL;
  78. foreach ($items as $item) {
  79. /** @var Item $item */
  80. echo $item->toXML();
  81. }
  82. echo '</urlset>'.NL;
  83. $result = ob_get_contents();
  84. ob_end_clean();
  85. return $result;
  86. }
  87. /**
  88. * Helper function for getting the path to the sitemap file.
  89. *
  90. * @return string The path to the sitemap file.
  91. *
  92. * @author Michael Hamann
  93. */
  94. public static function getFilePath() {
  95. global $conf;
  96. $sitemap = $conf['cachedir'].'/sitemap.xml';
  97. if (self::sitemapIsCompressed()) {
  98. $sitemap .= '.gz';
  99. }
  100. return $sitemap;
  101. }
  102. /**
  103. * Helper function for checking if the sitemap is compressed
  104. *
  105. * @return bool If the sitemap file is compressed
  106. */
  107. public static function sitemapIsCompressed() {
  108. global $conf;
  109. return $conf['compression'] === 'bz2' || $conf['compression'] === 'gz';
  110. }
  111. /**
  112. * Pings search engines with the sitemap url. Plugins can add or remove
  113. * urls to ping using the SITEMAP_PING event.
  114. *
  115. * @author Michael Hamann
  116. *
  117. * @return bool
  118. */
  119. public static function pingSearchEngines() {
  120. //ping search engines...
  121. $http = new DokuHTTPClient();
  122. $http->timeout = 8;
  123. $encoded_sitemap_url = urlencode(wl('', array('do' => 'sitemap'), true, '&'));
  124. $ping_urls = array(
  125. 'google' => 'https://www.google.com/ping?sitemap='.$encoded_sitemap_url,
  126. 'yandex' => 'https://webmaster.yandex.com/ping?sitemap='.$encoded_sitemap_url
  127. );
  128. $data = array('ping_urls' => $ping_urls,
  129. 'encoded_sitemap_url' => $encoded_sitemap_url
  130. );
  131. $event = new \dokuwiki\Extension\Event('SITEMAP_PING', $data);
  132. if ($event->advise_before(true)) {
  133. foreach ($data['ping_urls'] as $name => $url) {
  134. Logger::debug("Sitemapper::PingSearchEngines(): pinging $name");
  135. $resp = $http->get($url);
  136. if($http->error) {
  137. Logger::debug("Sitemapper:pingSearchengines(): $http->error", $resp);
  138. }
  139. }
  140. }
  141. $event->advise_after();
  142. return true;
  143. }
  144. }