3 * Sitemap handling functions
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author Michael Hamann <michael@content-space.de>
9 if(!defined('DOKU_INC')) die('meh.');
12 * A class for building sitemaps and pinging search engines with the sitemap URL.
14 * @author Michael Hamann
18 * Builds a Google Sitemap of all public pages known to the indexer
20 * The map is placed in the cache directory named sitemap.xml.gz - This
21 * file needs to be writable!
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/
28 public static function generate(){
30 if($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) return false;
32 $sitemap = Sitemapper::getFilePath();
34 if(@file_exists($sitemap)){
35 if(!is_writable($sitemap)) return false;
37 if(!is_writable(dirname($sitemap))) return false;
40 if(@filesize($sitemap) &&
41 @filemtime($sitemap) > (time()-($conf['sitemap']*86400))){ // 60*60*24=86400
42 dbglog('Sitemapper::generate(): Sitemap up to date');
46 dbglog("Sitemapper::generate(): using $sitemap");
48 $pages = idx_get_indexer()->getPages();
49 dbglog('Sitemapper::generate(): creating sitemap using '.count($pages).' pages');
52 // build the sitemap items
53 foreach($pages as $id){
54 //skip hidden, non existing and restricted files
55 if(isHiddenPage($id)) continue;
56 if(auth_aclcheck($id,'','') < AUTH_READ) continue;
57 $item = SitemapItem::createFromID($id);
62 $eventData = array('items' => &$items, 'sitemap' => &$sitemap);
63 $event = new Doku_Event('SITEMAP_GENERATE', $eventData);
64 if ($event->advise_before(true)) {
65 //save the new sitemap
66 $event->result = io_saveFile($sitemap, Sitemapper::getXML($items));
68 $event->advise_after();
70 return $event->result;
74 * Builds the sitemap XML string from the given array auf SitemapItems.
76 * @param $items array The SitemapItems that shall be included in the sitemap.
77 * @return string The sitemap XML.
78 * @author Michael Hamann
80 private static function getXML($items) {
82 echo '<?xml version="1.0" encoding="UTF-8"?>'.NL;
83 echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'.NL;
84 foreach ($items as $item) {
85 /** @var SitemapItem $item */
89 $result = ob_get_contents();
95 * Helper function for getting the path to the sitemap file.
97 * @return string The path to the sitemap file.
98 * @author Michael Hamann
100 public static function getFilePath() {
103 $sitemap = $conf['cachedir'].'/sitemap.xml';
104 if (self::sitemapIsCompressed()) {
112 * Helper function for checking if the sitemap is compressed
114 * @return bool If the sitemap file is compressed
116 public static function sitemapIsCompressed() {
118 return $conf['compression'] === 'bz2' || $conf['compression'] === 'gz';
122 * Pings search engines with the sitemap url. Plugins can add or remove
123 * urls to ping using the SITEMAP_PING event.
125 * @author Michael Hamann
127 public static function pingSearchEngines() {
128 //ping search engines...
129 $http = new DokuHTTPClient();
132 $encoded_sitemap_url = urlencode(wl('', array('do' => 'sitemap'), true, '&'));
134 'google' => 'http://www.google.com/webmasters/sitemaps/ping?sitemap='.$encoded_sitemap_url,
135 'yahoo' => 'http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=dokuwiki&url='.$encoded_sitemap_url,
136 'microsoft' => 'http://www.bing.com/webmaster/ping.aspx?siteMap='.$encoded_sitemap_url,
139 $data = array('ping_urls' => $ping_urls,
140 'encoded_sitemap_url' => $encoded_sitemap_url
142 $event = new Doku_Event('SITEMAP_PING', $data);
143 if ($event->advise_before(true)) {
144 foreach ($data['ping_urls'] as $name => $url) {
145 dbglog("Sitemapper::PingSearchEngines(): pinging $name");
146 $resp = $http->get($url);
147 if($http->error) dbglog("Sitemapper:pingSearchengines(): $http->error");
148 dbglog('Sitemapper:pingSearchengines(): '.preg_replace('/[\n\r]/',' ',strip_tags($resp)));
151 $event->advise_after();
158 * An item of a sitemap.
160 * @author Michael Hamann
171 * @param $url string The url of the item
172 * @param $lastmod int Timestamp of the last modification
173 * @param $changefreq string How frequently the item is likely to change. Valid values: always, hourly, daily, weekly, monthly, yearly, never.
174 * @param $priority float|string The priority of the item relative to other URLs on your site. Valid values range from 0.0 to 1.0.
176 public function __construct($url, $lastmod, $changefreq = null, $priority = null) {
178 $this->lastmod = $lastmod;
179 $this->changefreq = $changefreq;
180 $this->priority = $priority;
184 * Helper function for creating an item for a wikipage id.
186 * @param $id string A wikipage id.
187 * @param $changefreq string How frequently the item is likely to change. Valid values: always, hourly, daily, weekly, monthly, yearly, never.
188 * @param $priority float|string The priority of the item relative to other URLs on your site. Valid values range from 0.0 to 1.0.
189 * @return SitemapItem The sitemap item.
191 public static function createFromID($id, $changefreq = null, $priority = null) {
193 $date = @filemtime(wikiFN($id));
194 if(!$date) return null;
195 return new SitemapItem(wl($id, '', true), $date, $changefreq, $priority);
199 * Get the XML representation of the sitemap item.
201 * @return string The XML representation.
203 public function toXML() {
204 $result = ' <url>'.NL
205 .' <loc>'.hsc($this->url).'</loc>'.NL
206 .' <lastmod>'.date_iso8601($this->lastmod).'</lastmod>'.NL;
207 if ($this->changefreq !== null)
208 $result .= ' <changefreq>'.hsc($this->changefreq).'</changefreq>'.NL;
209 if ($this->priority !== null)
210 $result .= ' <priority>'.hsc($this->priority).'</priority>'.NL;
211 $result .= ' </url>'.NL;