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