xmlsitemap_engines.module 7.2 KB

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