xmlsitemap.xmlsitemap.inc 10 KB

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