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