summaryrefslogtreecommitdiff
path: root/inc/Sitemapper.php
diff options
context:
space:
mode:
authorMichael Hamann <michael@content-space.de>2010-06-26 13:33:46 +0200
committerMichael Hamann <michael@content-space.de>2010-09-22 17:55:07 +0200
commit2897eb23759202676f5447a72d7fe5eb68321ce3 (patch)
treeed9c79e2b77c2e0d979da54bbcfe3468d5e0b287 /inc/Sitemapper.php
parentc4f79b71351dd0d96f19f7c5629888d85a814c72 (diff)
downloadrpg-2897eb23759202676f5447a72d7fe5eb68321ce3.tar.gz
rpg-2897eb23759202676f5447a72d7fe5eb68321ce3.tar.bz2
Transformed the sitemapper into a class
This makes it possible to autoload the sitemapper when needed.
Diffstat (limited to 'inc/Sitemapper.php')
-rw-r--r--inc/Sitemapper.php103
1 files changed, 103 insertions, 0 deletions
diff --git a/inc/Sitemapper.php b/inc/Sitemapper.php
new file mode 100644
index 000000000..68f4beddb
--- /dev/null
+++ b/inc/Sitemapper.php
@@ -0,0 +1,103 @@
+<?php
+/**
+ * Sitemap handling functions
+ *
+ * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
+ * @author Michael Hamann <michael@content-space.de>
+ */
+
+if(!defined('DOKU_INC')) die('meh.');
+
+class Sitemapper {
+ /**
+ * Builds a Google Sitemap of all public pages known to the indexer
+ *
+ * The map is placed in the cache directory named sitemap.xml.gz - This
+ * file needs to be writable!
+ *
+ * @author Andreas Gohr
+ * @link https://www.google.com/webmasters/sitemaps/docs/en/about.html
+ */
+ public function generate(){
+ global $conf;
+ dbglog('sitemapGenerate(): started');
+ if(!$conf['sitemap']) return false;
+
+ $sitemap = Sitemapper::getFilePath();
+ dbglog("runSitemapper(): using $sitemap");
+
+ if(@file_exists($sitemap)){
+ if(!is_writable($sitemap)) return false;
+ }else{
+ if(!is_writable(dirname($sitemap))) return false;
+ }
+
+ if(@filesize($sitemap) &&
+ @filemtime($sitemap) > (time()-($conf['sitemap']*60*60*24))){
+ dbglog('runSitemapper(): Sitemap up to date');
+ return false;
+ }
+
+ $pages = idx_getIndex('page', '');
+ dbglog('runSitemapper(): creating sitemap using '.count($pages).' pages');
+
+ // build the sitemap
+ ob_start();
+ print '<?xml version="1.0" encoding="UTF-8"?>'.NL;
+ print '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'.NL;
+ foreach($pages as $id){
+ $id = trim($id);
+ $file = wikiFN($id);
+
+ //skip hidden, non existing and restricted files
+ if(isHiddenPage($id)) continue;
+ $date = @filemtime($file);
+ if(!$date) continue;
+ if(auth_aclcheck($id,'','') < AUTH_READ) continue;
+
+ print ' <url>'.NL;
+ print ' <loc>'.wl($id,'',true).'</loc>'.NL;
+ print ' <lastmod>'.date_iso8601($date).'</lastmod>'.NL;
+ print ' </url>'.NL;
+ }
+ print '</urlset>'.NL;
+ $data = ob_get_contents();
+ ob_end_clean();
+
+ //save the new sitemap
+ return io_saveFile($sitemap,$data);
+ }
+
+ public function getFilePath() {
+ global $conf;
+
+ $sitemap = $conf['cachedir'].'/sitemap.xml';
+ if($conf['compression'] == 'bz2' || $conf['compression'] == 'gz'){
+ $sitemap .= '.gz';
+ }
+
+ return $sitemap;
+ }
+
+ public function pingSearchEngines() {
+ //ping search engines...
+ $http = new DokuHTTPClient();
+ $http->timeout = 8;
+
+ $encoded_sitemap_url = urlencode(wl('', array('do' => 'sitemap'), true, '&'));
+ $ping_urls = array(
+ 'google' => 'http://www.google.com/webmasters/sitemaps/ping?sitemap='.$encoded_sitemap_url,
+ 'yahoo' => 'http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=dokuwiki&url='.$encoded_sitemap_url,
+ 'microsoft' => 'http://www.bing.com/webmaster/ping.aspx?siteMap='.$encoded_sitemap_url,
+ );
+
+ foreach ($ping_urls as $name => $url) {
+ dbglog("sitemapPingSearchEngines(): pinging $name");
+ $resp = $http->get($url);
+ if($http->error) dbglog("runSitemapper(): $http->error");
+ dbglog('runSitemapper(): '.preg_replace('/[\n\r]/',' ',strip_tags($resp)));
+ }
+
+ return true;
+ }
+}