xmlsitemap.xmlsitemap.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * @file
  4. * XML sitemap integration functions for xmlsitemap.module.
  5. */
  6. class XMLSitemapException extends Exception {}
  7. class XMLSitemapGenerationException extends XMLSitemapException {}
  8. /**
  9. * Extended class for writing XML sitemap files.
  10. */
  11. class XMLSitemapWriter extends XMLWriter {
  12. protected $uri = NULL;
  13. protected $sitemapElementCount = 0;
  14. protected $linkCountFlush = 500;
  15. protected $sitemap = NULL;
  16. protected $sitemap_page = NULL;
  17. protected $rootElement = 'urlset';
  18. /**
  19. * Constructor.
  20. *
  21. * @param $sitemap
  22. * The sitemap array.
  23. * @param $page
  24. * The current page of the sitemap being generated.
  25. */
  26. function __construct(stdClass $sitemap, $page) {
  27. $this->sitemap = $sitemap;
  28. $this->sitemap_page = $page;
  29. $this->uri = xmlsitemap_sitemap_get_file($sitemap, $page);
  30. $this->openUri($this->uri);
  31. }
  32. public function openUri($uri) {
  33. $return = parent::openUri($uri);
  34. if (!$return) {
  35. throw new XMLSitemapGenerationException(t('Could not open file @file for writing.', array('@file' => $uri)));
  36. }
  37. return $return;
  38. }
  39. public function startDocument($version = '1.0', $encoding = 'UTF-8', $standalone = NULL) {
  40. $this->setIndent(FALSE);
  41. $result = parent::startDocument($version, $encoding);
  42. if (!$result) {
  43. throw new XMLSitemapGenerationException(t('Unknown error occurred while writing to file @file.', array('@file' => $this->uri)));
  44. }
  45. if (variable_get('xmlsitemap_xsl', 1)) {
  46. $this->writeXSL();
  47. }
  48. $this->startElement($this->rootElement, TRUE);
  49. return $result;
  50. }
  51. public function getSitemapUrl($path, array $options = array()) {
  52. $options += $this->sitemap->uri['options'];
  53. $options += array(
  54. 'absolute' => TRUE,
  55. 'base_url' => variable_get('xmlsitemap_base_url', $GLOBALS['base_url']),
  56. 'language' => language_default(),
  57. 'alias' => TRUE,
  58. );
  59. if (!empty($options['protocol_relative'])) {
  60. $options['base_url'] = preg_replace('~^https?:~', '', $options['base_url']);
  61. }
  62. return url($path, $options);
  63. }
  64. /**
  65. * Add the XML stylesheet to the XML page.
  66. */
  67. public function writeXSL() {
  68. $this->writePi('xml-stylesheet', 'type="text/xsl" href="' . $this->getSitemapUrl('sitemap.xsl', array('protocol_relative' => TRUE)) . '"');
  69. $this->writeRaw(PHP_EOL);
  70. }
  71. /**
  72. * Return an array of attributes for the root element of the XML.
  73. */
  74. public function getRootAttributes() {
  75. $attributes['xmlns'] = 'http://www.sitemaps.org/schemas/sitemap/0.9';
  76. if (variable_get('xmlsitemap_developer_mode', 0)) {
  77. $attributes['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance';
  78. $attributes['xsi:schemaLocation'] = 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd';
  79. }
  80. drupal_alter('xmlsitemap_root_attributes', $attributes, $this->sitemap);
  81. return $attributes;
  82. }
  83. public function generateXML() {
  84. return xmlsitemap_generate_chunk($this->sitemap, $this, $this->sitemap_page);
  85. }
  86. public function startElement($name, $root = FALSE) {
  87. parent::startElement($name);
  88. if ($root) {
  89. foreach ($this->getRootAttributes() as $name => $value) {
  90. $this->writeAttribute($name, $value);
  91. }
  92. $this->writeRaw(PHP_EOL);
  93. }
  94. }
  95. /**
  96. * Write an full XML sitemap element tag.
  97. *
  98. * @param $name
  99. * The element name.
  100. * @param $element
  101. * An array of the elements properties and values.
  102. */
  103. public function writeSitemapElement($name, array &$element) {
  104. $this->writeElement($name, $element);
  105. $this->writeRaw(PHP_EOL);
  106. // After a certain number of elements have been added, flush the buffer
  107. // to the output file.
  108. $this->sitemapElementCount++;
  109. if (($this->sitemapElementCount % $this->linkCountFlush) == 0) {
  110. $this->flush();
  111. }
  112. }
  113. /**
  114. * Write full element tag including support for nested elements.
  115. *
  116. * @param string $name
  117. * The element name.
  118. * @param string|array $content
  119. * The element contents or an array of the elements' sub-elements.
  120. *
  121. * @todo Missing a return value since XMLWriter::writeElement() has one.
  122. */
  123. public function writeElement($name, $content = NULL) {
  124. if (is_array($content)) {
  125. $this->startElement($name);
  126. $xml_content = format_xml_elements($content);
  127. // Remove additional spaces from the output.
  128. $xml_content = str_replace(array(" <", ">\n"), array("<", ">"), $xml_content);
  129. $this->writeRaw($xml_content);
  130. $this->endElement();
  131. }
  132. else {
  133. parent::writeElement($name, check_plain((string) $content));
  134. }
  135. }
  136. public function getURI() {
  137. return $this->uri;
  138. }
  139. public function getSitemapElementCount() {
  140. return $this->sitemapElementCount;
  141. }
  142. public function endDocument() {
  143. $return = parent::endDocument();
  144. if (!$return) {
  145. throw new XMLSitemapGenerationException(t('Unknown error occurred while writing to file @file.', array('@file' => $this->uri)));
  146. }
  147. //if (xmlsitemap_var('gz')) {
  148. // $file_gz = $file . '.gz';
  149. // file_put_contents($file_gz, gzencode(file_get_contents($file), 9));
  150. //}
  151. return $return;
  152. }
  153. }
  154. class XMLSitemapIndexWriter extends XMLSitemapWriter {
  155. protected $rootElement = 'sitemapindex';
  156. function __construct(stdClass $sitemap, $page = 'index') {
  157. parent::__construct($sitemap, 'index');
  158. }
  159. public function getRootAttributes() {
  160. $attributes['xmlns'] = 'http://www.sitemaps.org/schemas/sitemap/0.9';
  161. if (variable_get('xmlsitemap_developer_mode', 0)) {
  162. $attributes['xmlns:xsi'] = 'http://www.w3.org/2001/XMLSchema-instance';
  163. $attributes['xsi:schemaLocation'] = 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd';
  164. }
  165. drupal_alter('xmlsitemap_root_attributes', $attributes, $this->sitemap);
  166. return $attributes;
  167. }
  168. public function generateXML() {
  169. $lastmod_format = variable_get('xmlsitemap_lastmod_format', XMLSITEMAP_LASTMOD_MEDIUM);
  170. for ($i = 1; $i <= $this->sitemap->chunks; $i++) {
  171. $element = array(
  172. 'loc' => $this->getSitemapUrl('sitemap.xml', array('query' => array('page' => $i))),
  173. // @todo Use the actual lastmod value of the chunk file.
  174. 'lastmod' => gmdate($lastmod_format, REQUEST_TIME),
  175. );
  176. $this->writeSitemapElement('sitemap', $element);
  177. }
  178. }
  179. }
  180. /**
  181. * Implements hook_xmlsitemap_link_info().
  182. */
  183. function xmlsitemap_xmlsitemap_link_info() {
  184. return array(
  185. 'frontpage' => array(
  186. 'label' => t('Frontpage'),
  187. 'xmlsitemap' => array(
  188. 'settings callback' => 'xmlsitemap_link_frontpage_settings',
  189. ),
  190. ),
  191. );
  192. }
  193. /**
  194. * XML sitemap link type settings callback for frontpage link entity.
  195. */
  196. function xmlsitemap_link_frontpage_settings(&$form) {
  197. module_load_include('admin.inc', 'xmlsitemap');
  198. if (user_access('administer site configuration')) {
  199. $form['#description'] = t('The front page path can be changed in the <a href="@url-frontpage">site information configuration</a>.', array('@url-frontpage' => url('admin/config/system/site-information')));
  200. }
  201. $form['xmlsitemap_frontpage_priority'] = array(
  202. '#type' => 'select',
  203. '#title' => t('Priority'),
  204. '#options' => xmlsitemap_get_priority_options(),
  205. '#default_value' => variable_get('xmlsitemap_frontpage_priority', 1.0),
  206. );
  207. $form['xmlsitemap_frontpage_changefreq'] = array(
  208. '#type' => 'select',
  209. '#title' => t('Change frequency'),
  210. '#options' => xmlsitemap_get_changefreq_options(),
  211. '#default_value' => variable_get('xmlsitemap_frontpage_changefreq', XMLSITEMAP_FREQUENCY_DAILY),
  212. );
  213. return $form;
  214. }
  215. /**
  216. * Implements hook_xmlsitemap_link_alter().
  217. */
  218. function xmlsitemap_xmlsitemap_link_alter(&$link) {
  219. // Alter the frontpage priority.
  220. if ($link['type'] == 'frontpage' || $link['loc'] == '' || $link['loc'] == variable_get('site_frontpage', 'node')) {
  221. $link['priority'] = variable_get('xmlsitemap_frontpage_priority', 1.0);
  222. $link['changefreq'] = variable_get('xmlsitemap_frontpage_changefreq', XMLSITEMAP_FREQUENCY_DAILY);
  223. }
  224. }
  225. /**
  226. * Implements hook_xmlsitemap_links().
  227. */
  228. function xmlsitemap_xmlsitemap_links() {
  229. // Frontpage link.
  230. $links[] = array(
  231. 'type' => 'frontpage',
  232. 'id' => 0,
  233. 'loc' => '',
  234. );
  235. return $links;
  236. }
  237. /**
  238. * Implements hook_xmlsitemap_sitemap_operations().
  239. */
  240. function xmlsitemap_xmlsitemap_sitemap_operations() {
  241. $operations['update'] = array(
  242. 'label' => t('Update cached files'),
  243. 'action past' => t('Updated'),
  244. 'callback' => 'xmlsitemap_sitemap_multiple_update',
  245. );
  246. return $operations;
  247. }
  248. /**
  249. * XML sitemap operation callback; regenerate sitemap files using the batch API.
  250. *
  251. * @param $smids
  252. * An array of XML sitemap IDs.
  253. *
  254. * @see xmlsitemap_regenerate_batch()
  255. */
  256. function xmlsitemap_sitemap_multiple_update(array $smids) {
  257. module_load_include('generate.inc', 'xmlsitemap');
  258. $batch = xmlsitemap_regenerate_batch($smids);
  259. batch_set($batch);
  260. }
  261. /**
  262. * Implements hook_query_TAG_alter().
  263. */
  264. function xmlsitemap_query_xmlsitemap_link_bundle_access_alter(QueryAlterableInterface $query) {
  265. if ($query instanceof EntityFieldQuery && $entity = $query->getMetaData('entity')) {
  266. $info = $query->getMetaData('entity_info');
  267. $bundle = $query->getMetaData('bundle');
  268. if (empty($bundle)) {
  269. $bundle = xmlsitemap_get_link_type_enabled_bundles($entity);
  270. }
  271. $query->entityCondition('bundle', $bundle, is_array($bundle) ? 'IN' : '=');
  272. }
  273. }