xmlsitemap_engines.module 6.2 KB

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