PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/Sitemapper.php

https://gitlab.com/michield/dokuwiki
PHP | 214 lines | 114 code | 23 blank | 77 comment | 18 complexity | 92332b353efcbd26f1ebfa2fccfd94dd MD5 | raw file
  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. if(!defined('DOKU_INC')) die('meh.');
  9. /**
  10. * A class for building sitemaps and pinging search engines with the sitemap URL.
  11. *
  12. * @author Michael Hamann
  13. */
  14. class Sitemapper {
  15. /**
  16. * Builds a Google Sitemap of all public pages known to the indexer
  17. *
  18. * The map is placed in the cache directory named sitemap.xml.gz - This
  19. * file needs to be writable!
  20. *
  21. * @author Michael Hamann
  22. * @author Andreas Gohr
  23. * @link https://www.google.com/webmasters/sitemaps/docs/en/about.html
  24. * @link http://www.sitemaps.org/
  25. */
  26. public static function generate(){
  27. global $conf;
  28. if($conf['sitemap'] < 1 || !is_numeric($conf['sitemap'])) return false;
  29. $sitemap = Sitemapper::getFilePath();
  30. if(@file_exists($sitemap)){
  31. if(!is_writable($sitemap)) return false;
  32. }else{
  33. if(!is_writable(dirname($sitemap))) return false;
  34. }
  35. if(@filesize($sitemap) &&
  36. @filemtime($sitemap) > (time()-($conf['sitemap']*86400))){ // 60*60*24=86400
  37. dbglog('Sitemapper::generate(): Sitemap up to date');
  38. return false;
  39. }
  40. dbglog("Sitemapper::generate(): using $sitemap");
  41. $pages = idx_get_indexer()->getPages();
  42. dbglog('Sitemapper::generate(): creating sitemap using '.count($pages).' pages');
  43. $items = array();
  44. // build the sitemap items
  45. foreach($pages as $id){
  46. //skip hidden, non existing and restricted files
  47. if(isHiddenPage($id)) continue;
  48. if(auth_aclcheck($id,'','') < AUTH_READ) continue;
  49. $item = SitemapItem::createFromID($id);
  50. if ($item !== null)
  51. $items[] = $item;
  52. }
  53. $eventData = array('items' => &$items, 'sitemap' => &$sitemap);
  54. $event = new Doku_Event('SITEMAP_GENERATE', $eventData);
  55. if ($event->advise_before(true)) {
  56. //save the new sitemap
  57. $event->result = io_saveFile($sitemap, Sitemapper::getXML($items));
  58. }
  59. $event->advise_after();
  60. return $event->result;
  61. }
  62. /**
  63. * Builds the sitemap XML string from the given array auf SitemapItems.
  64. *
  65. * @param $items array The SitemapItems that shall be included in the sitemap.
  66. * @return string The sitemap XML.
  67. * @author Michael Hamann
  68. */
  69. private static function getXML($items) {
  70. ob_start();
  71. echo '<?xml version="1.0" encoding="UTF-8"?>'.NL;
  72. echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'.NL;
  73. foreach ($items as $item) {
  74. /** @var SitemapItem $item */
  75. echo $item->toXML();
  76. }
  77. echo '</urlset>'.NL;
  78. $result = ob_get_contents();
  79. ob_end_clean();
  80. return $result;
  81. }
  82. /**
  83. * Helper function for getting the path to the sitemap file.
  84. *
  85. * @return string The path to the sitemap file.
  86. * @author Michael Hamann
  87. */
  88. public static function getFilePath() {
  89. global $conf;
  90. $sitemap = $conf['cachedir'].'/sitemap.xml';
  91. if (self::sitemapIsCompressed()) {
  92. $sitemap .= '.gz';
  93. }
  94. return $sitemap;
  95. }
  96. /**
  97. * Helper function for checking if the sitemap is compressed
  98. *
  99. * @return bool If the sitemap file is compressed
  100. */
  101. public static function sitemapIsCompressed() {
  102. global $conf;
  103. return $conf['compression'] === 'bz2' || $conf['compression'] === 'gz';
  104. }
  105. /**
  106. * Pings search engines with the sitemap url. Plugins can add or remove
  107. * urls to ping using the SITEMAP_PING event.
  108. *
  109. * @author Michael Hamann
  110. */
  111. public static function pingSearchEngines() {
  112. //ping search engines...
  113. $http = new DokuHTTPClient();
  114. $http->timeout = 8;
  115. $encoded_sitemap_url = urlencode(wl('', array('do' => 'sitemap'), true, '&'));
  116. $ping_urls = array(
  117. 'google' => 'http://www.google.com/webmasters/sitemaps/ping?sitemap='.$encoded_sitemap_url,
  118. 'yahoo' => 'http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=dokuwiki&url='.$encoded_sitemap_url,
  119. 'microsoft' => 'http://www.bing.com/webmaster/ping.aspx?siteMap='.$encoded_sitemap_url,
  120. );
  121. $data = array('ping_urls' => $ping_urls,
  122. 'encoded_sitemap_url' => $encoded_sitemap_url
  123. );
  124. $event = new Doku_Event('SITEMAP_PING', $data);
  125. if ($event->advise_before(true)) {
  126. foreach ($data['ping_urls'] as $name => $url) {
  127. dbglog("Sitemapper::PingSearchEngines(): pinging $name");
  128. $resp = $http->get($url);
  129. if($http->error) dbglog("Sitemapper:pingSearchengines(): $http->error");
  130. dbglog('Sitemapper:pingSearchengines(): '.preg_replace('/[\n\r]/',' ',strip_tags($resp)));
  131. }
  132. }
  133. $event->advise_after();
  134. return true;
  135. }
  136. }
  137. /**
  138. * An item of a sitemap.
  139. *
  140. * @author Michael Hamann
  141. */
  142. class SitemapItem {
  143. public $url;
  144. public $lastmod;
  145. public $changefreq;
  146. public $priority;
  147. /**
  148. * Create a new item.
  149. *
  150. * @param $url string The url of the item
  151. * @param $lastmod int Timestamp of the last modification
  152. * @param $changefreq string How frequently the item is likely to change. Valid values: always, hourly, daily, weekly, monthly, yearly, never.
  153. * @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.
  154. */
  155. public function __construct($url, $lastmod, $changefreq = null, $priority = null) {
  156. $this->url = $url;
  157. $this->lastmod = $lastmod;
  158. $this->changefreq = $changefreq;
  159. $this->priority = $priority;
  160. }
  161. /**
  162. * Helper function for creating an item for a wikipage id.
  163. *
  164. * @param $id string A wikipage id.
  165. * @param $changefreq string How frequently the item is likely to change. Valid values: always, hourly, daily, weekly, monthly, yearly, never.
  166. * @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.
  167. * @return SitemapItem The sitemap item.
  168. */
  169. public static function createFromID($id, $changefreq = null, $priority = null) {
  170. $id = trim($id);
  171. $date = @filemtime(wikiFN($id));
  172. if(!$date) return null;
  173. return new SitemapItem(wl($id, '', true), $date, $changefreq, $priority);
  174. }
  175. /**
  176. * Get the XML representation of the sitemap item.
  177. *
  178. * @return string The XML representation.
  179. */
  180. public function toXML() {
  181. $result = ' <url>'.NL
  182. .' <loc>'.hsc($this->url).'</loc>'.NL
  183. .' <lastmod>'.date_iso8601($this->lastmod).'</lastmod>'.NL;
  184. if ($this->changefreq !== null)
  185. $result .= ' <changefreq>'.hsc($this->changefreq).'</changefreq>'.NL;
  186. if ($this->priority !== null)
  187. $result .= ' <priority>'.hsc($this->priority).'</priority>'.NL;
  188. $result .= ' </url>'.NL;
  189. return $result;
  190. }
  191. }