diff --git a/inc/Sitemapper.php b/inc/Sitemapper.php new file mode 100644 index 0000000000000000000000000000000000000000..68f4beddb3b127b6ae78829359b3c7a022c071e1 --- /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; + } +} diff --git a/inc/actions.php b/inc/actions.php index 2d70ac8ed6a6755c789063065145ec2dd1fab4ac..12c4c595fb72177954d6a4577dcf3d9f187b467e 100644 --- a/inc/actions.php +++ b/inc/actions.php @@ -615,8 +615,7 @@ function act_sitemap($act) { // Check if sitemap file exists, otherwise create it if (!is_readable($sitemap)) { - require_once DOKU_INC.'inc/sitemap.php'; - sitemapGenerate(); + Sitemapper::generate(); } if (is_readable($sitemap)) { diff --git a/inc/load.php b/inc/load.php index 2f5be6d63c76b4007fae859411261a2dac5ec144..478ee7c761209dc1574aaf5a16c375837f0881c2 100644 --- a/inc/load.php +++ b/inc/load.php @@ -74,6 +74,7 @@ function load_autoload($name){ 'DokuWikiFeedCreator' => DOKU_INC.'inc/feedcreator.class.php', 'Doku_Parser_Mode' => DOKU_INC.'inc/parser/parser.php', 'SafeFN' => DOKU_INC.'inc/SafeFN.class.php', + 'Sitemapper' => DOKU_INC.'inc/Sitemapper.php', 'DokuWiki_Action_Plugin' => DOKU_PLUGIN.'action.php', 'DokuWiki_Admin_Plugin' => DOKU_PLUGIN.'admin.php', diff --git a/inc/sitemap.php b/inc/sitemap.php deleted file mode 100644 index bbed7d269501e2672c2cdb74a990ee38ae40edc2..0000000000000000000000000000000000000000 --- a/inc/sitemap.php +++ /dev/null @@ -1,101 +0,0 @@ -<?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.'); - -/** - * 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 - */ -function sitemapGenerate(){ - global $conf; - dbglog('sitemapGenerate(): started'); - if(!$conf['sitemap']) return false; - - $sitemap = sitemapGetFilePath(); - 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); -} - -function sitemapGetFilePath() { - global $conf; - - $sitemap = $conf['cachedir'].'/sitemap.xml'; - if($conf['compression'] == 'bz2' || $conf['compression'] == 'gz'){ - $sitemap .= '.gz'; - } - - return $sitemap; -} - -function sitemapPingSearchEngines() { - //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; -} diff --git a/lib/exe/indexer.php b/lib/exe/indexer.php index 63ad5931fdf394f78f2ef44d494f0ba159a55f37..61cf83accb9ecef142252f32e44fc504d9e31987 100644 --- a/lib/exe/indexer.php +++ b/lib/exe/indexer.php @@ -233,8 +233,7 @@ function metaUpdate(){ */ function runSitemapper(){ print "runSitemapper(): started".NL; - require_once DOKU_INC.'inc/sitemap.php'; - $result = sitemapGenerate() && sitemapPingSearchEngines(); + $result = Sitemapper::generate() && Sitemapper::pingSearchEngines(); print 'runSitemapper(): finished'.NL; return $result; }