xmlsitemap_engines.module 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * @file
  4. * Main file for XML sitemap engines.
  5. */
  6. /**
  7. * Implements hook_hook_info().
  8. */
  9. function xmlsitemap_engines_hook_info() {
  10. $hooks['xmlsitemap_engine_info'] = array(
  11. 'group' => 'xmlsitemap',
  12. );
  13. $hooks['xmlsitemap_engine_info_alter'] = array(
  14. 'group' => 'xmlsitemap',
  15. );
  16. return $hooks;
  17. }
  18. /**
  19. * Implements hook_help().
  20. */
  21. function xmlsitemap_engines_help($path, $arg) {
  22. $output = '';
  23. switch ($path) {
  24. case 'admin/config/search/xmlsitemap/engines':
  25. if (!module_exists('site_verify')) {
  26. $output .= '<p>' . t('In order to verify site ownership with the search engines listed below, it is highly recommended to download and install the <a href="@site-verify">Site verification module</a>.', array('@site-verify' => 'https://www.drupal.org/project/site_verify')) . '</p>';
  27. }
  28. break;
  29. }
  30. return $output;
  31. }
  32. /**
  33. * Implements hook_menu().
  34. */
  35. function xmlsitemap_engines_menu() {
  36. $items['admin/config/search/xmlsitemap/engines'] = array(
  37. 'title' => 'Search Engines',
  38. 'page callback' => 'drupal_get_form',
  39. 'page arguments' => array('xmlsitemap_engines_settings_form'),
  40. 'access arguments' => array('administer xmlsitemap'),
  41. 'type' => MENU_LOCAL_TASK,
  42. 'file' => 'xmlsitemap_engines.admin.inc',
  43. );
  44. // @code
  45. // $items['admin/config/search/xmlsitemap/engines/submit'] = array(
  46. // 'page callback' => 'xmlsitemap_engines_submit',
  47. // 'access callback' => 'xmlsitemap_engines_submit_access',
  48. // 'type' => MENU_CALLBACK,
  49. // );
  50. // @endcode
  51. return $items;
  52. }
  53. /**
  54. * Implements hook_cron().
  55. */
  56. function xmlsitemap_engines_cron() {
  57. if (xmlsitemap_engines_submit_access()) {
  58. xmlsitemap_engines_submit_engines();
  59. }
  60. }
  61. /**
  62. * Check if can submit.
  63. */
  64. function xmlsitemap_engines_can_submit() {
  65. // Skip if the site is offline since search engines will not be able to
  66. // access the site's content.
  67. if (variable_get('maintenance_mode', 0) || defined('MAINTENANCE_MODE')) {
  68. return FALSE;
  69. }
  70. if (!variable_get('xmlsitemap_engines_engines', array()) && !variable_get('xmlsitemap_engines_custom_urls', '')) {
  71. return FALSE;
  72. }
  73. return TRUE;
  74. }
  75. /**
  76. * Submit access.
  77. */
  78. function xmlsitemap_engines_submit_access() {
  79. if (!xmlsitemap_engines_can_submit()) {
  80. return FALSE;
  81. }
  82. // Allow manual submissions to run.
  83. // @code
  84. // @codingStandardsIgnoreLine
  85. // if ($_GET['q'] == 'admin/config/search/xmlsitemap/engines/submit' && user_access('administer xmlsitemap')) {
  86. // return TRUE;
  87. // }
  88. // @endcode
  89. $submit_updated = variable_get('xmlsitemap_engines_submit_updated', TRUE);
  90. $submitted_last = variable_get('xmlsitemap_engines_submit_last', 0);
  91. $minimum_lifetime = variable_get('xmlsitemap_engines_minimum_lifetime', 86400);
  92. // Skip if sitemap data has not been updated since last submission.
  93. if ($submit_updated && variable_get('xmlsitemap_generated_last', 0) <= $submitted_last) {
  94. return FALSE;
  95. }
  96. // Skip if the time since last submission is less than the minimum lifetime.
  97. if ((REQUEST_TIME - $submitted_last) < $minimum_lifetime) {
  98. return FALSE;
  99. }
  100. return TRUE;
  101. }
  102. /**
  103. * Submit the sitemaps to all the specified search engines.
  104. *
  105. * @param array $smids
  106. * An optional array of XML sitemap IDs. If not provided, it will load all
  107. * existing XML sitemaps.
  108. */
  109. function xmlsitemap_engines_submit_engines(array $smids = array()) {
  110. if (empty($smids)) {
  111. $smids = FALSE;
  112. }
  113. $sitemaps = xmlsitemap_sitemap_load_multiple($smids);
  114. $engines = variable_get('xmlsitemap_engines_engines', array());
  115. $engine_info = xmlsitemap_engines_get_engine_info();
  116. foreach ($engines as $engine) {
  117. if (isset($engine_info[$engine]['url'])) {
  118. xmlsitemap_engines_submit_sitemaps($engine_info[$engine]['url'], $sitemaps);
  119. }
  120. }
  121. $custom_urls = variable_get('xmlsitemap_engines_custom_urls', '');
  122. $custom_urls = preg_split('/[\r\n]+/', $custom_urls, -1, PREG_SPLIT_NO_EMPTY);
  123. foreach ($custom_urls as $custom_url) {
  124. xmlsitemap_engines_submit_sitemaps($custom_url, $sitemaps);
  125. }
  126. variable_set('xmlsitemap_engines_submit_last', REQUEST_TIME);
  127. }
  128. /**
  129. * Submit the sitemaps to a specific URL.
  130. *
  131. * @param string $url
  132. * The URL for sitemap submission.
  133. * @param array $sitemaps
  134. * An array of URLs of the sitemaps to submit.
  135. */
  136. function xmlsitemap_engines_submit_sitemaps($url, array $sitemaps) {
  137. foreach ($sitemaps as $sitemap) {
  138. $sitemap->url = url($sitemap->uri['path'], $sitemap->uri['options']);
  139. $submit_url = xmlsitemap_engines_prepare_url($url, $sitemap->url);
  140. $request = drupal_http_request($submit_url);
  141. watchdog('xmlsitemap', 'Submitted the sitemap to %url and received response @code.', array('%url' => $submit_url, '@code' => $request->code));
  142. }
  143. }
  144. /**
  145. * Replace valid tokens in the URL with their appropriate values.
  146. *
  147. * @param string $url
  148. * An un-tokenized URL.
  149. *
  150. * @return string
  151. * A tokenized URL.
  152. */
  153. function xmlsitemap_engines_prepare_url($url, $sitemap) {
  154. return str_replace('[sitemap]', $sitemap, $url);
  155. }
  156. /**
  157. * Returns information about supported search engines.
  158. *
  159. * @param string $engine
  160. * (optional) The engine to return information for. If omitted, information
  161. * for all engines is returned.
  162. *
  163. * @see hook_xmlsitemap_engines_info()
  164. * @see hook_xmlsitemap_engines_info_alter()
  165. */
  166. function xmlsitemap_engines_get_engine_info($engine = NULL) {
  167. global $language;
  168. $engines = &drupal_static(__FUNCTION__);
  169. if (!isset($engines)) {
  170. if ($cached = cache_get('xmlsitemap:engines:' . $language->language)) {
  171. $engines = $cached->data;
  172. }
  173. else {
  174. // Fetch the results of all hook_xmlsitemap_engine_info() implementations.
  175. $engines = module_invoke_all('xmlsitemap_engine_info');
  176. // Allow other modulse to alter the engine info.
  177. drupal_alter('xmlsitemap_engine_info', $engines);
  178. // Cache by language since engine names are translated.
  179. cache_set('xmlsitemap:engines:' . $language->language, $engines);
  180. }
  181. }
  182. if (isset($engine)) {
  183. return isset($engines[$engine]) ? $engines[$engine] : NULL;
  184. }
  185. else {
  186. return $engines;
  187. }
  188. }
  189. /**
  190. * Implements hook_xmlsitemap_engine_info().
  191. */
  192. function xmlsitemap_engines_xmlsitemap_engine_info() {
  193. $engines['google'] = array(
  194. 'name' => t('Google'),
  195. 'url' => 'https://www.google.com/webmasters/tools/ping?sitemap=[sitemap]',
  196. 'help url' => 'https://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=156184',
  197. );
  198. $engines['bing'] = array(
  199. 'name' => t('Bing'),
  200. 'url' => 'https://www.bing.com/webmaster/ping.aspx?siteMap=[sitemap]',
  201. 'help url' => 'https://www.bing.com/webmaster',
  202. );
  203. return $engines;
  204. }
  205. /**
  206. * Implements hook_variables().
  207. */
  208. function xmlsitemap_engines_variables() {
  209. $variables = array(
  210. 'xmlsitemap_engines_engines' => array(),
  211. 'xmlsitemap_engines_custom_urls' => '',
  212. 'xmlsitemap_engines_minimum_lifetime' => 86400,
  213. 'xmlsitemap_engines_submit_last' => 0,
  214. 'xmlsitemap_engines_submit_updated' => TRUE,
  215. );
  216. return $variables;
  217. }
  218. /**
  219. * Implements hook_xmlsitemap_sitemap_operations().
  220. */
  221. function xmlsitemap_engines_xmlsitemap_sitemap_operations() {
  222. if (xmlsitemap_engines_can_submit()) {
  223. $operations['xmlsitemap_engines_submit'] = array(
  224. 'label' => t('Submit to search engines'),
  225. 'action past' => t('Submitted'),
  226. 'callback' => 'xmlsitemap_engines_submit_engines',
  227. );
  228. return $operations;
  229. }
  230. }