xmlsitemap.module

Tracking 6.x-2.x branch
  1. drupal
    1. 5 contributions/xmlsitemap/xmlsitemap/xmlsitemap.module
    2. 6 contributions/xmlsitemap/xmlsitemap.module
    3. 7 contributions/xmlsitemap/xmlsitemap.module

Main file for the xmlsitemap module.

Functions & methods

NameDescription
xmlsitemap_calculate_changefreqCalculates the average interval between UNIX timestamps.
xmlsitemap_check_all_directories
xmlsitemap_check_directoryCheck that the sitemap files directory exists and is writable.
xmlsitemap_clear_directory
xmlsitemap_cronImplements hook_cron().
xmlsitemap_db_fetch_all_assocBackport of the DBTNG fetchAllAssoc() from Drupal 7.
xmlsitemap_db_fetch_colBackport of the DBTNG fetchCol() from Drupal 7.
xmlsitemap_directory_moveMove a directory to a new location.
xmlsitemap_drupal_hash_base64Backport of drupal_hash_base64() from Drupal 7.
xmlsitemap_element_get_visible_childrenBackport of element_get_visible_children() from Drupal 7.
xmlsitemap_entity_extract_idsBackport of entity_extract_ids() from Drupal 7.
xmlsitemap_entity_uriBackport of entity_uri() from Drupal 7.
xmlsitemap_form_submit_flag_regenerateSubmit handler; Set the regenerate needed flag if variables have changed.
xmlsitemap_form_system_modules_alterImplements hook_form_FORM_ID_alter().
xmlsitemap_get_bundle_path
xmlsitemap_get_changefreqDetermine the frequency of updates to a link.
xmlsitemap_get_changefreq_options@todo Document this function. @todo Make these translatable
xmlsitemap_get_chunk_countGet the current number of sitemap chunks.
xmlsitemap_get_chunk_sizeGet the sitemap chunk size.
xmlsitemap_get_context_info
xmlsitemap_get_current_contextGet the sitemap context of the current request.
xmlsitemap_get_directory
xmlsitemap_get_link_countGet the current number of sitemap links.
xmlsitemap_get_link_infoReturns information about supported sitemap link types.
xmlsitemap_get_link_type_enabled_bundles
xmlsitemap_get_link_type_indexed_status
xmlsitemap_get_operation_link
xmlsitemap_helpImplements hook_help().
xmlsitemap_language_loadLoad a language object by its language code.
xmlsitemap_link_bundle_access
xmlsitemap_link_bundle_delete
xmlsitemap_link_bundle_load
xmlsitemap_link_bundle_rename
xmlsitemap_link_bundle_settings_form_submit
xmlsitemap_link_bundle_settings_save
xmlsitemap_link_deleteDelete a specific sitemap link from the database.
xmlsitemap_link_delete_multipleDelete multiple sitemap links from the database.
xmlsitemap_link_loadLoad a specific sitemap link from the database.
xmlsitemap_link_load_multipleLoad sitemap links from the database.
xmlsitemap_link_saveSaves or updates a sitemap link.
xmlsitemap_link_type_renameRename a link type.
xmlsitemap_link_update_multiplePerform a mass update of sitemap data.
xmlsitemap_load_all_includesLoad all modulename.xmlsitemap.inc files.
xmlsitemap_menuImplements hook_menu().
xmlsitemap_permImplements hook_perm().
xmlsitemap_process_form_link_options
xmlsitemap_recalculate_changefreqRecalculate the changefreq of a sitemap link.
xmlsitemap_restore_userRestore the user that was originally loaded.
xmlsitemap_robotstxtImplements hook_robotstxt().
xmlsitemap_run_unprogressive_batchRun a progressive batch operation.
xmlsitemap_set_time_limitBackport of drupal_set_time_limit from Drupal 7.
xmlsitemap_sitemap_deleteDelete an XML sitemap.
xmlsitemap_sitemap_delete_multipleDelete multiple XML sitemaps.
xmlsitemap_sitemap_get_context_hash
xmlsitemap_sitemap_get_fileReturn the expected file path for a specific sitemap chunk.
xmlsitemap_sitemap_get_max_filesizeFind the maximum file size of all a sitemap's XML files.
xmlsitemap_sitemap_loadLoad an XML sitemap array from the database.
xmlsitemap_sitemap_load_by_contextLoad an XML sitemap array from the database based on its context.
xmlsitemap_sitemap_load_multipleLoad multiple XML sitemaps from the database.
xmlsitemap_sitemap_saveSave changes to an XML sitemap or add a new XML sitemap.
xmlsitemap_sitemap_uriReturns the uri elements of an XML sitemap.
xmlsitemap_static
xmlsitemap_static_reset
xmlsitemap_switch_userSet the current user stored in $GLOBALS['user'].
xmlsitemap_system_modules_submitSubmit callback; manually clears XML sitemap caches when modules are changed.
xmlsitemap_varInternal implementation of variable_get().
xmlsitemap_variablesInternal default variables for xmlsitemap_var().
_xmlsitemap_check_changed_linkCheck if there is sitemap link is changed from the existing data.
_xmlsitemap_check_changed_linksCheck if there is a visible sitemap link given a certain set of conditions.
_xmlsitemap_delete_recursiveRecursively delete all files and folders in the specified filepath.
_xmlsitemap_get_field_typeGiven an table and field, return the field type.
_xmlsitemap_rebuild_form_accessMenu access callback; determines if the user can use the rebuild links page.
_xmlsitemap_set_breadcrumbWorkaround for missing breadcrumbs on callback and action paths.
_xmlsitemap_sitemap_context_summary

Constants

NameDescription
XMLSITEMAP_FREQUENCY_ALWAYS
XMLSITEMAP_FREQUENCY_DAILY
XMLSITEMAP_FREQUENCY_HOURLY
XMLSITEMAP_FREQUENCY_MONTHLY
XMLSITEMAP_FREQUENCY_WEEKLY
XMLSITEMAP_FREQUENCY_YEARLY
XMLSITEMAP_LASTMOD_LONGLong lastmod timestamp format.
XMLSITEMAP_LASTMOD_MEDIUMMedium lastmod timestamp format.
XMLSITEMAP_LASTMOD_SHORTShort lastmod timestamp format.
XMLSITEMAP_MAX_SITEMAP_FILESIZEThe maximum filesize of a sitemap chunk file.
XMLSITEMAP_MAX_SITEMAP_LINKSThe maximum number of links in one sitemap chunk file.
XMLSITEMAP_PRIORITY_DEFAULTThe default priority for link types in the sitemaps.
XMLSITEMAP_STATUS_DEFAULTThe default inclusion status for link types in the sitemaps.

File

View source
  1. <?php
  2. /**
  3. * @defgroup xmlsitemap XML sitemap
  4. */
  5. /**
  6. * @file
  7. * Main file for the xmlsitemap module.
  8. */
  9. /**
  10. * Drupal 7 backport to define REQUEST_TIME.
  11. */
  12. if (!defined('REQUEST_TIME')) {
  13. define('REQUEST_TIME', isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time());
  14. }
  15. /**
  16. * The maximum number of links in one sitemap chunk file.
  17. */
  18. define('XMLSITEMAP_MAX_SITEMAP_LINKS', 50000);
  19. /**
  20. * The maximum filesize of a sitemap chunk file.
  21. */
  22. define('XMLSITEMAP_MAX_SITEMAP_FILESIZE', 10485760);
  23. define('XMLSITEMAP_FREQUENCY_YEARLY', 31449600); // 60 * 60 * 24 * 7 * 52
  24. define('XMLSITEMAP_FREQUENCY_MONTHLY', 2419200); // 60 * 60 * 24 * 7 * 4
  25. define('XMLSITEMAP_FREQUENCY_WEEKLY', 604800); // 60 * 60 * 24 * 7
  26. define('XMLSITEMAP_FREQUENCY_DAILY', 86400); // 60 * 60 * 24
  27. define('XMLSITEMAP_FREQUENCY_HOURLY', 3600); // 60 * 60
  28. define('XMLSITEMAP_FREQUENCY_ALWAYS', 60);
  29. /**
  30. * Short lastmod timestamp format.
  31. */
  32. define('XMLSITEMAP_LASTMOD_SHORT', 'Y-m-d');
  33. /**
  34. * Medium lastmod timestamp format.
  35. */
  36. define('XMLSITEMAP_LASTMOD_MEDIUM', 'Y-m-d\TH:i\Z');
  37. /**
  38. * Long lastmod timestamp format.
  39. */
  40. define('XMLSITEMAP_LASTMOD_LONG', 'c');
  41. /**
  42. * The default inclusion status for link types in the sitemaps.
  43. */
  44. define('XMLSITEMAP_STATUS_DEFAULT', 0);
  45. /**
  46. * The default priority for link types in the sitemaps.
  47. */
  48. define('XMLSITEMAP_PRIORITY_DEFAULT', 0.5);
  49. /**
  50. * Implements hook_help().
  51. */
  52. function xmlsitemap_help($path, $arg) {
  53. $output = '';
  54. switch ($path) {
  55. case 'admin/help/xmlsitemap':
  56. case 'admin/settings/xmlsitemap/settings/%/%/%':
  57. case 'admin/settings/xmlsitemap/edit/%':
  58. case 'admin/settings/xmlsitemap/delete/%':
  59. return;
  60. case 'admin/help#xmlsitemap':
  61. break;
  62. case 'admin/settings/xmlsitemap':
  63. if (!module_exists('elements')) {
  64. $output .= '<p>' . t('In order to perform bulk operations on the sitemaps listed below, it is highly recommended to download and install the <a href="@elements">Elements module</a>.', array('@elements' => 'http://drupal.org/project/elements')) . '</p>';
  65. }
  66. break;
  67. case 'admin/settings/xmlsitemap/rebuild':
  68. $output .= '<p>' . t("This action rebuilds your site's XML sitemap and regenerates the cached files, and may be a lengthy process. If you just installed XML sitemap, this can be helpful to import all your site's content into the sitemap. Otherwise, this should only be used in emergencies.") . '</p>';
  69. }
  70. if (arg(0) == 'admin' && strpos($path, 'xmlsitemap') !== FALSE && user_access('administer xmlsitemap')) {
  71. module_load_include('inc', 'xmlsitemap');
  72. if ($arg[1] == 'settings') {
  73. // Alert the user to any potential problems detected by hook_requirements.
  74. xmlsitemap_check_status();
  75. }
  76. $output .= _xmlsitemap_get_blurb();
  77. }
  78. return $output;
  79. }
  80. /**
  81. * Implements hook_perm().
  82. */
  83. function xmlsitemap_perm() {
  84. $permissions['administer xmlsitemap'] = array(
  85. 'title' => t('Administer XML sitemap settings.'),
  86. );
  87. return array_keys($permissions);
  88. }
  89. /**
  90. * Implements hook_menu().
  91. */
  92. function xmlsitemap_menu() {
  93. $items['admin/settings/xmlsitemap'] = array(
  94. 'title' => 'XML sitemap',
  95. 'description' => "Configure your site's XML sitemaps to help search engines find and index pages on your site.",
  96. 'page callback' => 'drupal_get_form',
  97. 'page arguments' => array('xmlsitemap_sitemap_list_form'),
  98. 'access arguments' => array('administer xmlsitemap'),
  99. 'file' => 'xmlsitemap.admin.inc',
  100. );
  101. $items['admin/settings/xmlsitemap/list'] = array(
  102. 'title' => 'List',
  103. 'type' => MENU_DEFAULT_LOCAL_TASK,
  104. 'weight' => -10,
  105. );
  106. $items['admin/settings/xmlsitemap/add'] = array(
  107. 'title' => 'Add XML sitemap',
  108. 'page callback' => 'drupal_get_form',
  109. 'page arguments' => array('xmlsitemap_sitemap_edit_form'),
  110. 'access arguments' => array('administer xmlsitemap'),
  111. 'type' => MENU_CALLBACK,
  112. 'file' => 'xmlsitemap.admin.inc',
  113. 'modal' => TRUE,
  114. );
  115. $items['admin/settings/xmlsitemap/edit/%xmlsitemap_sitemap'] = array(
  116. 'title' => 'Edit XML sitemap',
  117. 'page callback' => 'drupal_get_form',
  118. 'page arguments' => array('xmlsitemap_sitemap_edit_form', 4),
  119. 'access arguments' => array('administer xmlsitemap'),
  120. 'type' => MENU_CALLBACK,
  121. 'file' => 'xmlsitemap.admin.inc',
  122. 'modal' => TRUE,
  123. );
  124. $items['admin/settings/xmlsitemap/delete/%xmlsitemap_sitemap'] = array(
  125. 'page callback' => 'drupal_get_form',
  126. 'page arguments' => array('xmlsitemap_sitemap_delete_form', 4),
  127. 'access arguments' => array('administer xmlsitemap'),
  128. 'type' => MENU_CALLBACK,
  129. 'file' => 'xmlsitemap.admin.inc',
  130. 'modal' => TRUE,
  131. );
  132. $items['admin/settings/xmlsitemap/settings'] = array(
  133. 'title' => 'Settings',
  134. 'page callback' => 'drupal_get_form',
  135. 'page arguments' => array('xmlsitemap_settings_form'),
  136. 'access arguments' => array('administer xmlsitemap'),
  137. 'type' => MENU_LOCAL_TASK,
  138. 'file' => 'xmlsitemap.admin.inc',
  139. 'weight' => 10,
  140. );
  141. $items['admin/settings/xmlsitemap/settings/%xmlsitemap_link_bundle/%'] = array(
  142. 'load arguments' => array(5),
  143. 'page callback' => 'drupal_get_form',
  144. 'page arguments' => array('xmlsitemap_link_bundle_settings_form', 4),
  145. 'access callback' => 'xmlsitemap_link_bundle_access',
  146. 'access arguments' => array(4),
  147. 'file' => 'xmlsitemap.admin.inc',
  148. 'modal' => TRUE,
  149. );
  150. $items['admin/settings/xmlsitemap/rebuild'] = array(
  151. 'title' => 'Rebuild links',
  152. 'description' => 'Rebuild the site map.',
  153. 'page callback' => 'drupal_get_form',
  154. 'page arguments' => array('xmlsitemap_rebuild_form'),
  155. 'access callback' => '_xmlsitemap_rebuild_form_access',
  156. 'type' => MENU_LOCAL_TASK,
  157. 'file' => 'xmlsitemap.admin.inc',
  158. 'weight' => 20,
  159. );
  160. $items['sitemap.xml'] = array(
  161. 'page callback' => 'xmlsitemap_output_chunk',
  162. 'access callback' => TRUE,
  163. 'type' => MENU_CALLBACK,
  164. 'file' => 'xmlsitemap.pages.inc',
  165. );
  166. $items['sitemap.xsl'] = array(
  167. 'page callback' => 'xmlsitemap_output_xsl',
  168. 'access callback' => TRUE,
  169. 'type' => MENU_CALLBACK,
  170. 'file' => 'xmlsitemap.pages.inc',
  171. );
  172. return $items;
  173. }
  174. /**
  175. * Menu access callback; determines if the user can use the rebuild links page.
  176. */
  177. function _xmlsitemap_rebuild_form_access() {
  178. module_load_include('generate.inc', 'xmlsitemap');
  179. $rebuild_types = xmlsitemap_get_rebuildable_link_types();
  180. return !empty($rebuild_types) && user_access('administer xmlsitemap');
  181. }
  182. /**
  183. * Implements hook_cron().
  184. */
  185. function xmlsitemap_cron() {
  186. // If there were no new or changed links, skip.
  187. if (!variable_get('xmlsitemap_regenerate_needed', FALSE)) {
  188. return;
  189. }
  190. // If the minimum sitemap lifetime hasn't been passed, skip.
  191. $lifetime = REQUEST_TIME - variable_get('xmlsitemap_generated_last', 0);
  192. if ($lifetime < variable_get('xmlsitemap_minimum_lifetime', 0)) {
  193. return;
  194. }
  195. // Regenerate the sitemap XML files.
  196. module_load_include('generate.inc', 'xmlsitemap');
  197. xmlsitemap_run_unprogressive_batch('xmlsitemap_regenerate_batch');
  198. }
  199. /**
  200. * Implements hook_form_FORM_ID_alter().
  201. *
  202. * Add a submit handler to manually clear any XML sitemap cache entries.
  203. */
  204. function xmlsitemap_form_system_modules_alter(&$form, $form_state) {
  205. $form['#submit'][] = 'xmlsitemap_system_modules_submit';
  206. }
  207. /**
  208. * Submit callback; manually clears XML sitemap caches when modules are changed.
  209. */
  210. function xmlsitemap_system_modules_submit($form, $form_state) {
  211. cache_clear_all('xmlsitemap:', 'cache', TRUE);
  212. }
  213. /**
  214. * Implements hook_robotstxt().
  215. */
  216. function xmlsitemap_robotstxt() {
  217. if ($sitemap = xmlsitemap_sitemap_load_by_context()) {
  218. $robotstxt[] = 'Sitemap: ' . url($sitemap->uri['path'], $sitemap->uri['options']);
  219. return $robotstxt;
  220. }
  221. }
  222. /**
  223. * Internal default variables for xmlsitemap_var().
  224. */
  225. function xmlsitemap_variables() {
  226. return array(
  227. 'xmlsitemap_rebuild_needed' => FALSE,
  228. 'xmlsitemap_regenerate_needed' => FALSE,
  229. 'xmlsitemap_minimum_lifetime' => 0,
  230. 'xmlsitemap_generated_last' => 0,
  231. 'xmlsitemap_xsl' => 1,
  232. 'xmlsitemap_prefetch_aliases' => 1,
  233. 'xmlsitemap_chunk_size' => 'auto',
  234. 'xmlsitemap_batch_limit' => 100,
  235. 'xmlsitemap_path' => 'xmlsitemap',
  236. 'xmlsitemap_base_url' => $GLOBALS['base_url'],
  237. 'xmlsitemap_developer_mode' => 0,
  238. 'xmlsitemap_frontpage_priority' => 1.0,
  239. 'xmlsitemap_frontpage_changefreq' => XMLSITEMAP_FREQUENCY_DAILY,
  240. 'xmlsitemap_lastmod_format' => XMLSITEMAP_LASTMOD_MEDIUM,
  241. 'xmlsitemap_gz' => FALSE,
  242. // Removed variables are set to NULL so they can still be deleted.
  243. 'xmlsitemap_regenerate_last' => NULL,
  244. 'xmlsitemap_custom_links' => NULL,
  245. 'xmlsitemap_priority_default' => NULL,
  246. 'xmlsitemap_languages' => NULL,
  247. 'xmlsitemap_max_chunks' => NULL,
  248. 'xmlsitemap_max_filesize' => NULL,
  249. );
  250. }
  251. /**
  252. * Internal implementation of variable_get().
  253. */
  254. function xmlsitemap_var($name, $default = NULL) {
  255. $defaults = &xmlsitemap_static(__FUNCTION__);
  256. if (!isset($defaults)) {
  257. $defaults = xmlsitemap_variables();
  258. }
  259. $name = 'xmlsitemap_' . $name;
  260. // @todo Remove when stable.
  261. if (!isset($defaults[$name])) {
  262. trigger_error(strtr('Default variable for %variable not found.', array('%variable' => theme('placeholder', $name))));
  263. }
  264. return variable_get($name, isset($default) || !isset($defaults[$name]) ? $default : $defaults[$name]);
  265. }
  266. /**
  267. * @defgroup xmlsitemap_sitemap_api XML sitemap API for sitemaps.
  268. * @{
  269. */
  270. /**
  271. * Load an XML sitemap array from the database.
  272. *
  273. * @param $smid
  274. * An XML sitemap ID.
  275. *
  276. * @return
  277. * The XML sitemap object.
  278. */
  279. function xmlsitemap_sitemap_load($smid) {
  280. $sitemap = xmlsitemap_sitemap_load_multiple(array($smid));
  281. return $sitemap ? reset($sitemap) : FALSE;
  282. }
  283. /**
  284. * Load multiple XML sitemaps from the database.
  285. *
  286. * @param $smids
  287. * An array of XML sitemap IDs, or FALSE to load all XML sitemaps.
  288. * @param $conditions
  289. * An array of conditions in the form 'field' => $value.
  290. *
  291. * @return
  292. * An array of XML sitemap objects.
  293. */
  294. function xmlsitemap_sitemap_load_multiple($smids = array(), array $conditions = array()) {
  295. module_load_include('inc', 'xmlsitemap');
  296. if ($smids === array()) {
  297. return array();
  298. }
  299. elseif ($smids !== FALSE) {
  300. $conditions['smid'] = $smids;
  301. }
  302. $sql = "SELECT * FROM {xmlsitemap_sitemap}";
  303. $args = _xmlsitemap_build_conditions($conditions, array(), array('table' => 'xmlsitemap_sitemap'));
  304. if (!empty($conditions)) {
  305. $sql .= " WHERE " . implode(' AND ', $conditions);
  306. }
  307. $query = db_query($sql, $args);
  308. $sitemaps = xmlsitemap_db_fetch_all_assoc($query, 'smid');
  309. foreach ($sitemaps as $smid => $sitemap) {
  310. $sitemaps[$smid]->context = unserialize($sitemap->context);
  311. $sitemaps[$smid]->uri = xmlsitemap_sitemap_uri($sitemaps[$smid]);
  312. }
  313. return $sitemaps;
  314. }
  315. /**
  316. * Load an XML sitemap array from the database based on its context.
  317. *
  318. * @param $context
  319. * An optional XML sitemap context array to use to find the correct XML
  320. * sitemap. If not provided, the current site's context will be used.
  321. *
  322. * @see xmlsitemap_get_current_context()
  323. */
  324. function xmlsitemap_sitemap_load_by_context(array $context = NULL) {
  325. if (!isset($context)) {
  326. $context = xmlsitemap_get_current_context();
  327. }
  328. $hash = xmlsitemap_sitemap_get_context_hash($context);
  329. $smid = db_result(db_query_range("SELECT smid FROM {xmlsitemap_sitemap} WHERE smid = '%s'", $hash, 0, 1));
  330. return xmlsitemap_sitemap_load($smid);
  331. }
  332. /**
  333. * Save changes to an XML sitemap or add a new XML sitemap.
  334. *
  335. * @param $sitemap
  336. * The XML sitemap array to be saved. If $sitemap->smid is omitted, a new
  337. * XML sitemap will be added.
  338. *
  339. * @todo Save the sitemap's URL as a column?
  340. */
  341. function xmlsitemap_sitemap_save(stdClass &$sitemap) {
  342. xmlsitemap_load_all_includes();
  343. if (!isset($sitemap->context)) {
  344. $sitemap->context = array();
  345. }
  346. // Make sure context is sorted before saving the hash.
  347. $sitemap->is_new = empty($sitemap->smid);
  348. $sitemap->old_smid = $sitemap->is_new ? NULL : $sitemap->smid;
  349. $sitemap->smid = xmlsitemap_sitemap_get_context_hash($sitemap->context);
  350. // If the context was changed, we need to perform additional actions.
  351. if (!$sitemap->is_new && $sitemap->smid != $sitemap->old_smid) {
  352. // Rename the files directory so the sitemap does not break.
  353. $old_sitemap = (object) array('smid' => $sitemap->old_smid);
  354. $old_dir = xmlsitemap_get_directory($old_sitemap);
  355. $new_dir = xmlsitemap_get_directory($sitemap);
  356. xmlsitemap_directory_move($old_dir, $new_dir);
  357. // Change the smid field so drupal_write_record() does not fail.
  358. db_query("UPDATE {xmlsitemap_sitemap} SET smid = '%s' WHERE smid = '%s'", $sitemap->smid, $sitemap->old_smid);
  359. // Mark the sitemaps as needing regeneration.
  360. variable_set('xmlsitemap_regenerate_needed', TRUE);
  361. }
  362. if ($sitemap->is_new) {
  363. drupal_write_record('xmlsitemap_sitemap', $sitemap);
  364. module_invoke_all('xmlsitemap_sitemap_insert', $sitemap);
  365. }
  366. else {
  367. drupal_write_record('xmlsitemap_sitemap', $sitemap, array('smid'));
  368. module_invoke_all('xmlsitemap_sitemap_update', $sitemap);
  369. }
  370. return $sitemap;
  371. }
  372. /**
  373. * Delete an XML sitemap.
  374. *
  375. * @param $smid
  376. * An XML sitemap ID.
  377. */
  378. function xmlsitemap_sitemap_delete($smid) {
  379. xmlsitemap_sitemap_delete_multiple(array($smid));
  380. }
  381. /**
  382. * Delete multiple XML sitemaps.
  383. *
  384. * @param $smids
  385. * An array of XML sitemap IDs.
  386. */
  387. function xmlsitemap_sitemap_delete_multiple(array $smids) {
  388. xmlsitemap_load_all_includes();
  389. if (!empty($smids)) {
  390. $sitemaps = xmlsitemap_sitemap_load_multiple($smids);
  391. db_query("DELETE FROM {xmlsitemap_sitemap} WHERE smid IN (" . db_placeholders($smids, 'varchar') . ")", $smids);
  392. foreach ($sitemaps as $sitemap) {
  393. xmlsitemap_clear_directory($sitemap, TRUE);
  394. module_invoke_all('xmlsitemap_sitemap_delete', $sitemap);
  395. }
  396. }
  397. }
  398. /**
  399. * Return the expected file path for a specific sitemap chunk.
  400. *
  401. * @param $sitemap
  402. * An XML sitemap array.
  403. * @param $chunk
  404. * An optional specific chunk in the sitemap. Defaults to the index page.
  405. */
  406. function xmlsitemap_sitemap_get_file(stdClass $sitemap, $chunk = 'index') {
  407. return xmlsitemap_get_directory($sitemap) . "/{$chunk}.xml";
  408. }
  409. /**
  410. * Find the maximum file size of all a sitemap's XML files.
  411. *
  412. * @param $sitemap
  413. * The XML sitemap array.
  414. */
  415. function xmlsitemap_sitemap_get_max_filesize(stdClass &$sitemap) {
  416. $dir = xmlsitemap_get_directory($sitemap);
  417. $sitemap->max_filesize = 0;
  418. foreach (file_scan_directory($dir, '\.xml$') as $file) {
  419. $sitemap->max_filesize = max($sitemap->max_filesize, filesize($file->filename));
  420. }
  421. return $sitemap->max_filesize;
  422. }
  423. function xmlsitemap_sitemap_get_context_hash(array &$context) {
  424. asort($context);
  425. return xmlsitemap_drupal_hash_base64(serialize($context));
  426. }
  427. /**
  428. * Returns the uri elements of an XML sitemap.
  429. *
  430. * @param $sitemap
  431. * An unserialized data array for an XML sitemap.
  432. * @return
  433. * An array containing the 'path' and 'options' keys used to build the uri of
  434. * the XML sitemap, and matching the signature of url().
  435. */
  436. function xmlsitemap_sitemap_uri(stdClass &$sitemap) {
  437. xmlsitemap_load_all_includes();
  438. $uri['path'] = 'sitemap.xml';
  439. $uri['options'] = module_invoke_all('xmlsitemap_context_url_options', $sitemap->context);
  440. drupal_alter('xmlsitemap_context_url_options', $uri['options'], $sitemap->context);
  441. $uri['options'] += array(
  442. 'absolute' => TRUE,
  443. 'base_url' => variable_get('xmlsitemap_base_url', $GLOBALS['base_url']),
  444. );
  445. return $uri;
  446. }
  447. /**
  448. * @} End of "defgroup xmlsitemap_sitemap_api"
  449. */
  450. /**
  451. * @defgroup xmlsitemap_link_api XML sitemap API for sitemap links.
  452. * @{
  453. */
  454. /**
  455. * Load a specific sitemap link from the database.
  456. *
  457. * @param $entity_type
  458. * A string with the entity type.
  459. * @param $entity_id
  460. * An integer with the entity ID.
  461. * @return
  462. * A sitemap link (array) or FALSE if the conditions were not found.
  463. */
  464. function xmlsitemap_link_load($entity_type, $entity_id) {
  465. $link = xmlsitemap_link_load_multiple(array('type' => $entity_type, 'id' => $entity_id));
  466. return $link ? reset($link) : FALSE;
  467. }
  468. /**
  469. * Load sitemap links from the database.
  470. *
  471. * @param $conditions
  472. * An array of conditions on the {xmlsitemap} table in the form
  473. * 'field' => $value.
  474. * @return
  475. * An array of sitemap link arrays.
  476. */
  477. function xmlsitemap_link_load_multiple(array $conditions = array()) {
  478. $links = array();
  479. module_load_include('inc', 'xmlsitemap');
  480. $args = _xmlsitemap_build_conditions($conditions);
  481. $query = db_query("SELECT * FROM {xmlsitemap} WHERE " . implode(' AND ', $conditions), $args);
  482. while ($link = db_fetch_array($query)) {
  483. $links[] = $link;
  484. }
  485. return $links;
  486. }
  487. /**
  488. * Saves or updates a sitemap link.
  489. *
  490. * @param $link
  491. * An array with a sitemap link.
  492. */
  493. function xmlsitemap_link_save(array $link) {
  494. xmlsitemap_load_all_includes();
  495. module_load_include('inc', 'xmlsitemap');
  496. $link += array(
  497. 'access' => 1,
  498. 'status' => 1,
  499. 'status_override' => 0,
  500. 'lastmod' => 0,
  501. 'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
  502. 'priority_override' => 0,
  503. 'changefreq' => 0,
  504. 'changecount' => 0,
  505. 'language' => '',
  506. );
  507. // Allow other modules to alter the link before saving.
  508. drupal_alter('xmlsitemap_link', $link);
  509. // Temporary validation checks.
  510. // @todo Remove in final?
  511. if ($link['priority'] < 0 || $link['priority'] > 1) {
  512. trigger_error(t('Invalid sitemap link priority %priority.<br />@link', array('%priority' => $link['priority'], '@link' => var_export($link, TRUE))), E_USER_ERROR);
  513. }
  514. if ($link['changecount'] < 0) {
  515. trigger_error(t('Negative changecount value. Please report this to <a href="@516928">@516928</a>.<br />@link', array('@516928' => 'http://drupal.org/node/516928', '@link' => var_export($link, TRUE))), E_USER_ERROR);
  516. $link['changecount'] = 0;
  517. }
  518. $existing = db_fetch_array(db_query_range("SELECT loc, access, status, lastmod, priority, changefreq, changecount, language FROM {xmlsitemap} WHERE type = '%s' AND id = %d", $link['type'], $link['id'], 0, 1));
  519. // Check if this is a changed link and set the regenerate flag if necessary.
  520. if (!variable_get('xmlsitemap_regenerate_needed', FALSE)) {
  521. _xmlsitemap_check_changed_link($link, $existing, TRUE);
  522. }
  523. // Save the link and allow other modules to respond to the link being saved.
  524. if ($existing) {
  525. xmlsitemap_write_record('xmlsitemap', $link, array('type', 'id'));
  526. module_invoke_all('xmlsitemap_link_update', $link);
  527. }
  528. else {
  529. xmlsitemap_write_record('xmlsitemap', $link);
  530. module_invoke_all('xmlsitemap_link_insert', $link);
  531. }
  532. return $link;
  533. }
  534. /**
  535. * Perform a mass update of sitemap data.
  536. *
  537. * If visible links are updated, this will automatically set the regenerate
  538. * needed flag to TRUE.
  539. *
  540. * @param $updates
  541. * An array of values to update fields to, keyed by field name.
  542. * @param $conditions
  543. * An array of values to match keyed by field.
  544. * @return
  545. * The number of links that were updated.
  546. */
  547. function xmlsitemap_link_update_multiple($updates = array(), $conditions = array(), $check_flag = TRUE) {
  548. // If we are going to modify a visible sitemap link, we will need to set
  549. // the regenerate needed flag.
  550. if ($check_flag && !variable_get('xmlsitemap_regenerate_needed', FALSE)) {
  551. _xmlsitemap_check_changed_links($conditions, $updates, TRUE);
  552. }
  553. // Process updates.
  554. $args = array();
  555. module_load_include('inc', 'xmlsitemap');
  556. $args =_xmlsitemap_build_conditions($updates, $args, array('operator' => '=', 'update' => TRUE));
  557. $args = _xmlsitemap_build_conditions($conditions, $args);
  558. $sql = "UPDATE {xmlsitemap} SET " . implode(', ', $updates) . " WHERE " . implode(' AND ', $conditions);
  559. db_query($sql, $args);
  560. return db_affected_rows();
  561. }
  562. /**
  563. * Delete a specific sitemap link from the database.
  564. *
  565. * If a visible sitemap link was deleted, this will automatically set the
  566. * regenerate needed flag.
  567. *
  568. * @param $entity_type
  569. * A string with the entity type.
  570. * @param $entity_id
  571. * An integer with the entity ID.
  572. * @return
  573. * The number of links that were deleted.
  574. */
  575. function xmlsitemap_link_delete($entity_type, $entity_id) {
  576. $conditions = array('type' => $entity_type, 'id' => $entity_id);
  577. return xmlsitemap_link_delete_multiple($conditions);
  578. }
  579. /**
  580. * Delete multiple sitemap links from the database.
  581. *
  582. * If visible sitemap links were deleted, this will automatically set the
  583. * regenerate needed flag.
  584. *
  585. * @param $conditions
  586. * An array of conditions on the {xmlsitemap} table in the form
  587. * 'field' => $value.
  588. * @return
  589. * The number of links that were deleted.
  590. */
  591. function xmlsitemap_link_delete_multiple(array $conditions) {
  592. // Because this function is called from sub-module uninstall hooks, we have
  593. // to manually check if the table exists since it could have been removed
  594. // in xmlsitemap_uninstall().
  595. // @see http://drupal.org/node/151452
  596. if (!db_table_exists('xmlsitemap')) {
  597. return FALSE;
  598. }
  599. if (!variable_get('xmlsitemap_regenerate_needed', TRUE)) {
  600. _xmlsitemap_check_changed_links($conditions, array(), TRUE);
  601. }
  602. // @todo Add a hook_xmlsitemap_link_delete() hook invoked here.
  603. module_load_include('inc', 'xmlsitemap');
  604. $args = _xmlsitemap_build_conditions($conditions);
  605. db_query("DELETE FROM {xmlsitemap} WHERE " . implode(' AND ', $conditions), $args);
  606. return db_affected_rows();
  607. }
  608. /**
  609. * Check if there is a visible sitemap link given a certain set of conditions.
  610. *
  611. * @param $conditions
  612. * An array of values to match keyed by field.
  613. * @param $flag
  614. * An optional boolean that if TRUE, will set the regenerate needed flag if
  615. * there is a match. Defaults to FALSE.
  616. * @return
  617. * TRUE if there is a visible link, or FALSE otherwise.
  618. */
  619. function _xmlsitemap_check_changed_links(array $conditions = array(), array $updates = array(), $flag = FALSE) {
  620. // If we are changing status or access, check for negative current values.
  621. $conditions['status'] = (!empty($updates['status']) && empty($conditions['status'])) ? 0 : 1;
  622. $conditions['access'] = (!empty($updates['access']) && empty($conditions['access'])) ? 0 : 1;
  623. module_load_include('inc', 'xmlsitemap');
  624. $args = _xmlsitemap_build_conditions($conditions);
  625. $sql = "SELECT 1 FROM {xmlsitemap} WHERE ". implode(' AND ', $conditions);
  626. $changed = db_result(db_query_range($sql, $args, 0, 1));
  627. if ($changed && $flag) {
  628. variable_set('xmlsitemap_regenerate_needed', TRUE);
  629. }
  630. return $changed;
  631. }
  632. /**
  633. * Check if there is sitemap link is changed from the existing data.
  634. *
  635. * @param $link
  636. * An array of the sitemap link.
  637. * @param $original_link
  638. * An optional array of the existing data. This should only contain the
  639. * fields necessary for comparison. If not provided the existing data will be
  640. * loaded from the database.
  641. * @param $flag
  642. * An optional boolean that if TRUE, will set the regenerate needed flag if
  643. * there is a match. Defaults to FALSE.
  644. * @return
  645. * TRUE if the link is changed, or FALSE otherwise.
  646. */
  647. function _xmlsitemap_check_changed_link(array $link, $original_link = NULL, $flag = FALSE) {
  648. $changed = FALSE;
  649. if ($original_link === NULL) {
  650. // Load only the fields necessary for data to be changed in the sitemap.
  651. $original_link = db_fetch_array(db_query_range("SELECT loc, access, status, lastmod, priority, changefreq, changecount, language FROM {xmlsitemap} WHERE type = '%s' AND id = %d", $link['type'], $link['id'], 0, 1));
  652. }
  653. if (!$original_link) {
  654. if ($link['access'] && $link['status']) {
  655. // Adding a new visible link.
  656. $changed = TRUE;
  657. }
  658. }
  659. else {
  660. if (!($original_link['access'] && $original_link['status']) && $link['access'] && $link['status']) {
  661. // Changing a non-visible link to a visible link.
  662. $changed = TRUE;
  663. }
  664. elseif ($original_link['access'] && $original_link['status'] && array_diff_assoc($original_link, $link)) {
  665. // Changing a visible link
  666. $changed = TRUE;
  667. }
  668. }
  669. if ($changed && $flag) {
  670. variable_set('xmlsitemap_regenerate_needed', TRUE);
  671. }
  672. return $changed;
  673. }
  674. /**
  675. * @} End of "defgroup xmlsitemap_link_api"
  676. */
  677. function xmlsitemap_get_directory(stdClass $sitemap = NULL) {
  678. $directory = &xmlsitemap_static(__FUNCTION__);
  679. if (!isset($directory)) {
  680. $directory = file_create_path(variable_get('xmlsitemap_path', 'xmlsitemap'));
  681. }
  682. if (!empty($sitemap->smid)) {
  683. return $directory . '/' . $sitemap->smid;
  684. }
  685. else {
  686. return $directory;
  687. }
  688. }
  689. /**
  690. * Check that the sitemap files directory exists and is writable.
  691. */
  692. function xmlsitemap_check_directory(stdClass $sitemap = NULL) {
  693. $directory = xmlsitemap_get_directory($sitemap);
  694. return file_check_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  695. }
  696. function xmlsitemap_check_all_directories() {
  697. $directories = array();
  698. $sitemaps = xmlsitemap_sitemap_load_multiple(FALSE);
  699. foreach ($sitemaps as $smid => $sitemap) {
  700. $directory = xmlsitemap_get_directory($sitemap);
  701. $directories[$directory] = $directory;
  702. }
  703. foreach ($directories as $directory) {
  704. $directories[$directory] = file_check_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  705. }
  706. return $directories;
  707. }
  708. function xmlsitemap_clear_directory(stdClass $sitemap = NULL, $delete = FALSE) {
  709. $directory = xmlsitemap_get_directory($sitemap);
  710. return _xmlsitemap_delete_recursive($directory, $delete);
  711. }
  712. /**
  713. * Move a directory to a new location.
  714. *
  715. * @param $old_dir
  716. * A string specifying the filepath or URI of the original directory.
  717. * @param $new_dir
  718. * A string specifying the filepath or URI of the new directory.
  719. * @param $replace
  720. * Replace behavior when the destination file already exists.
  721. *
  722. * @return
  723. * TRUE if the directory was moved successfully. FALSE otherwise.
  724. */
  725. function xmlsitemap_directory_move($old_dir, $new_dir, $replace = FILE_EXISTS_REPLACE) {
  726. $success = file_check_directory($new_dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  727. if (!is_dir($old_dir) || !is_dir($new_dir) || !$success) {
  728. return FALSE;
  729. }
  730. $files = file_scan_directory($old_dir, '.*');
  731. foreach ($files as $file) {
  732. $file->filepath_new = $new_dir . '/' . $file->basename;
  733. $success &= (bool) file_move($file->filename, $file->filepath_new, $replace);
  734. }
  735. // The remove the directory.
  736. $success &= rmdir($old_dir);
  737. return $success;
  738. }
  739. /**
  740. * Recursively delete all files and folders in the specified filepath.
  741. *
  742. * This is a backport of Drupal 7's file_unmanaged_delete_recursive().
  743. *
  744. * Note that this only deletes visible files with write permission.
  745. *
  746. * @param $path
  747. * A filepath relative to file_directory_path.
  748. * @param $delete_root
  749. * A boolean if TRUE will delete the $path directory afterwards.
  750. */
  751. function _xmlsitemap_delete_recursive($path, $delete_root = FALSE) {
  752. if (is_dir($path)) {
  753. $dir = dir($path);
  754. while (($entry = $dir->read()) !== FALSE) {
  755. if ($entry == '.' || $entry == '..') {
  756. continue;
  757. }
  758. $entry_path = $path . '/' . $entry;
  759. _xmlsitemap_delete_recursive($entry_path, TRUE);
  760. }
  761. $dir->close();
  762. return $delete_root ? rmdir($path) : TRUE;
  763. }
  764. return file_delete($path);
  765. }
  766. /**
  767. * Returns information about supported sitemap link types.
  768. *
  769. * @param $type
  770. * (optional) The link type to return information for. If omitted,
  771. * information for all link types is returned.
  772. * @param $reset
  773. * (optional) Boolean whether to reset the static cache and do nothing. Only
  774. * used for tests.
  775. *
  776. * @see hook_xmlsitemap_link_info()
  777. * @see hook_xmlsitemap_link_info_alter()
  778. */
  779. function xmlsitemap_get_link_info($type = NULL, $reset = FALSE) {
  780. global $language;
  781. $link_info = &xmlsitemap_static(__FUNCTION__);
  782. if ($reset) {
  783. $link_info = NULL;
  784. cache_clear_all('xmlsitemap:link_info:', 'cache', TRUE);
  785. }
  786. if (!isset($link_info)) {
  787. $cid = 'xmlsitemap:link_info:' . $language->language;
  788. if ($cache = cache_get($cid)) {
  789. $link_info = $cache->data;
  790. }
  791. else {
  792. xmlsitemap_load_all_includes();
  793. $link_info = module_invoke_all('xmlsitemap_link_info');
  794. foreach ($link_info as $key => &$info) {
  795. $info += array(
  796. 'type' => $key,
  797. 'base table' => FALSE,
  798. 'bundles' => array(),
  799. 'xmlsitemap' => array(),
  800. 'entity keys' => array(),
  801. );
  802. $info['entity keys'] += array(
  803. 'bundle' => '',
  804. );
  805. if (!isset($info['xmlsitemap']['rebuild callback']) && !empty($info['base table']) && !empty($info['entity keys']['id']) && !empty($info['xmlsitemap']['process callback'])) {
  806. $info['xmlsitemap']['rebuild callback'] = 'xmlsitemap_rebuild_batch_fetch';
  807. }
  808. foreach ($info['bundles'] as $bundle => &$bundle_info) {
  809. $bundle_info += array(
  810. 'xmlsitemap' => array(),
  811. );
  812. $bundle_info['xmlsitemap'] += xmlsitemap_link_bundle_load($key, $bundle, FALSE);
  813. }
  814. }
  815. drupal_alter('xmlsitemap_link_info', $link_info);
  816. ksort($link_info);
  817. // Cache by language since this info contains translated strings.
  818. cache_set($cid, $link_info);
  819. }
  820. }
  821. if (isset($type)) {
  822. return isset($link_info[$type]) ? $link_info[$type] : NULL;
  823. }
  824. return $link_info;
  825. }
  826. function xmlsitemap_get_link_type_enabled_bundles($entity_type) {
  827. $bundles = array();
  828. $info = xmlsitemap_get_link_info($entity_type);
  829. foreach ($info['bundles'] as $bundle => $bundle_info) {
  830. $settings = xmlsitemap_link_bundle_load($entity_type, $bundle);
  831. if (!empty($settings['status'])) {
  832. //if (!empty($bundle_info['xmlsitemap']['status'])) {
  833. $bundles[] = $bundle;
  834. }
  835. }
  836. return $bundles;
  837. }
  838. function xmlsitemap_get_link_type_indexed_status($entity_type, $bundle = '') {
  839. $info = xmlsitemap_get_link_info($entity_type);
  840. $status['indexed'] = db_result(db_query("SELECT COUNT(id) FROM {xmlsitemap} WHERE type = '%s' AND subtype = '%s'", $entity_type, $bundle));
  841. $status['visible'] = db_result(db_query("SELECT COUNT(id) FROM {xmlsitemap} WHERE type = '%s' AND subtype = '%s' AND status = 1 AND access = 1", $entity_type, $bundle));
  842. $base_table = db_escape_table($info['base table']);
  843. $id_key = db_escape_string($info['entity keys']['id']);
  844. if (!empty($info['entity keys']['bundle'])) {
  845. $bundle_key = db_escape_string($info['entity keys']['bundle']);
  846. $bundle_placeholder = db_type_placeholder(_xmlsitemap_get_field_type($info['base table'], $info['entity keys']['bundle']));
  847. $status['total'] = db_result(db_query("SELECT COUNT($id_key) FROM {{$base_table}} WHERE $id_key > 0 AND $bundle_key = $bundle_placeholder", $bundle));
  848. }
  849. else {
  850. $status['total'] = db_result(db_query("SELECT COUNT($id_key) FROM {{$base_table}} WHERE $id_key > 0"));
  851. }
  852. return $status;
  853. }
  854. function xmlsitemap_link_bundle_settings_save($entity, $bundle, array $settings, $update_links = TRUE) {
  855. if ($update_links) {
  856. $old_settings = xmlsitemap_link_bundle_load($entity, $bundle);
  857. if ($settings['status'] != $old_settings['status']) {
  858. xmlsitemap_link_update_multiple(array('status' => $settings['status']), array('type' => $entity, 'subtype' => $bundle, 'status_override' => 0));
  859. }
  860. if ($settings['priority'] != $old_settings['priority']) {
  861. xmlsitemap_link_update_multiple(array('priority' => $settings['priority']), array('type' => $entity, 'subtype' => $bundle, 'priority_override' => 0));
  862. }
  863. }
  864. variable_set("xmlsitemap_settings_{$entity}_{$bundle}", $settings);
  865. cache_clear_all('xmlsitemap:link_info:', 'cache', TRUE);
  866. //xmlsitemap_get_link_info(NULL, TRUE);
  867. }
  868. function xmlsitemap_link_bundle_rename($entity, $bundle_old, $bundle_new) {
  869. if ($bundle_old != $bundle_new) {
  870. $settings = xmlsitemap_link_bundle_load($entity, $bundle_old);
  871. variable_del("xmlsitemap_settings_{$entity}_{$bundle_old}");
  872. xmlsitemap_link_bundle_settings_save($entity, $bundle_new, $settings, FALSE);
  873. xmlsitemap_link_update_multiple(array('subtype' => $bundle_new), array('type' => $entity, 'subtype' => $bundle_old));
  874. }
  875. }
  876. /**
  877. * Rename a link type.
  878. */
  879. function xmlsitemap_link_type_rename($entity_old, $entity_new, $bundles = NULL) {
  880. $variables = db_query("SELECT name FROM {variable} WHERE name LIKE '%s%%'", 'xmlsitemap_settings_' . $entity_old . '_');
  881. while ($variable = db_result($variables)) {
  882. $value = variable_get($variable, NULL);
  883. variable_del($variable);
  884. if (isset($value)) {
  885. $variable_new = str_replace('xmlsitemap_settings_' . $entity_old, 'xmlsitemap_settings_' . $entity_new, $variable);
  886. variable_set($variable_new, $value);
  887. }
  888. }
  889. db_query("UPDATE {xmlsitemap} SET type = '%s' WHERE type = '%s'", $entity_new, $entity_old);
  890. xmlsitemap_get_link_info(NULL, TRUE);
  891. }
  892. function xmlsitemap_link_bundle_load($entity, $bundle, $load_bundle_info = TRUE) {
  893. $info = array(
  894. 'entity' => $entity,
  895. 'bundle' => $bundle,
  896. );
  897. if ($load_bundle_info) {
  898. $entity_info = xmlsitemap_get_link_info($entity);
  899. if (isset($entity_info['bundles'][$bundle])) {
  900. $info['info'] = $entity_info['bundles'][$bundle];
  901. }
  902. }
  903. $info += variable_get("xmlsitemap_settings_{$entity}_{$bundle}", array());
  904. $info += array(
  905. 'status' => XMLSITEMAP_STATUS_DEFAULT,
  906. 'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
  907. );
  908. return $info;
  909. }
  910. function xmlsitemap_link_bundle_delete($entity, $bundle, $delete_links = TRUE) {
  911. variable_del("xmlsitemap_settings_{$entity}_{$bundle}");
  912. if ($delete_links) {
  913. xmlsitemap_link_delete_multiple(array('type' => $entity, 'subtype' => $bundle));
  914. }
  915. cache_clear_all('xmlsitemap:link_info:', 'cache', TRUE);
  916. //xmlsitemap_get_link_info(NULL, TRUE);
  917. }
  918. function xmlsitemap_link_bundle_access($entity, $bundle = NULL) {
  919. if (is_array($entity) && !isset($bundle)) {
  920. $bundle = $entity;
  921. }
  922. else {
  923. $bundle = xmlsitemap_link_bundle_load($entity, $bundle);
  924. }
  925. if (isset($bundle['info']['admin'])) {
  926. $admin = $bundle['info']['admin'];
  927. $admin += array('access arguments' => array());
  928. if (!isset($admin['access callback']) && count($admin['access arguments']) == 1) {
  929. $admin['access callback'] = 'user_access';
  930. }
  931. if (!empty($admin['access callback'])) {
  932. return call_user_func_array($admin['access callback'], $admin['access arguments']);
  933. }
  934. }
  935. return FALSE;
  936. }
  937. function xmlsitemap_get_bundle_path($entity, $bundle) {
  938. $info = xmlsitemap_get_link_info($entity);
  939. if (!empty($info['bundles'][$bundle]['admin']['real path'])) {
  940. return $info['bundles'][$bundle]['admin']['real path'];
  941. }
  942. elseif (!empty($info['bundles'][$bundle]['admin']['path'])) {
  943. return $info['bundles'][$bundle]['admin']['path'];
  944. }
  945. else {
  946. return FALSE;
  947. }
  948. }
  949. /**
  950. * Determine the frequency of updates to a link.
  951. *
  952. * @param $interval
  953. * An interval value in seconds.
  954. * @return
  955. * A string representing the update frequency according to the sitemaps.org
  956. * protocol.
  957. */
  958. function xmlsitemap_get_changefreq($interval) {
  959. if ($interval <= 0 || !is_numeric($interval)) {
  960. return FALSE;
  961. }
  962. foreach (xmlsitemap_get_changefreq_options() as $value => $frequency) {
  963. if ($interval <= $value) {
  964. return $frequency;
  965. }
  966. }
  967. return 'never';
  968. }
  969. /**
  970. * Get the current number of sitemap chunks.
  971. */
  972. function xmlsitemap_get_chunk_count($reset = FALSE) {
  973. static $chunks;
  974. if (!isset($chunks) || $reset) {
  975. $count = max(xmlsitemap_get_link_count($reset), 1);
  976. $chunks = ceil($count / xmlsitemap_get_chunk_size($reset));
  977. }
  978. return $chunks;
  979. }
  980. /**
  981. * Get the current number of sitemap links.
  982. */
  983. function xmlsitemap_get_link_count($reset = FALSE) {
  984. static $count;
  985. if (!isset($count) || $reset) {
  986. $count = db_result(db_query("SELECT COUNT(id) FROM {xmlsitemap} WHERE access = 1 AND status = 1"));
  987. }
  988. return $count;
  989. }
  990. /**
  991. * Get the sitemap chunk size.
  992. *
  993. * This function is useful with the chunk size is set to automatic as it will
  994. * calculate the appropriate value. Use this function instead of @code
  995. * xmlsitemap_var('chunk_size') @endcode when the actual value is needed.
  996. *
  997. * @param $reset
  998. * A boolean to reset the saved, static result. Defaults to FALSE.
  999. * @return
  1000. * An integer with the number of links in each sitemap page.
  1001. */
  1002. function xmlsitemap_get_chunk_size($reset = FALSE) {
  1003. static $size;
  1004. if (!isset($size) || $reset) {
  1005. $size = xmlsitemap_var('chunk_size');
  1006. if ($size === 'auto') {
  1007. $count = max(xmlsitemap_get_link_count($reset), 1); // Prevent divide by zero.
  1008. $size = min(ceil($count / 10000) * 5000, XMLSITEMAP_MAX_SITEMAP_LINKS);
  1009. }
  1010. }
  1011. return $size;
  1012. }
  1013. /**
  1014. * Recalculate the changefreq of a sitemap link.
  1015. *
  1016. * @param $link
  1017. * A sitemap link array.
  1018. */
  1019. function xmlsitemap_recalculate_changefreq(&$link) {
  1020. $link['changefreq'] = round((($link['changefreq'] * $link['changecount']) + (REQUEST_TIME - $link['lastmod'])) / ($link['changecount'] + 1));
  1021. $link['changecount']++;
  1022. $link['lastmod'] = REQUEST_TIME;
  1023. }
  1024. /**
  1025. * Calculates the average interval between UNIX timestamps.
  1026. *
  1027. * @param $timestamps
  1028. * An array of UNIX timestamp integers.
  1029. * @return
  1030. * An integer of the average interval.
  1031. */
  1032. function xmlsitemap_calculate_changefreq($timestamps) {
  1033. sort($timestamps);
  1034. $count = count($timestamps) - 1;
  1035. $diff = 0;
  1036. for ($i = 0; $i < $count; $i++) {
  1037. $diff += $timestamps[$i + 1] - $timestamps[$i];
  1038. }
  1039. return $count > 0 ? round($diff / $count) : 0;
  1040. }
  1041. /**
  1042. * Submit handler; Set the regenerate needed flag if variables have changed.
  1043. *
  1044. * This function needs to be called before system_settings_form_submit() or any
  1045. * calls to variable_set().
  1046. */
  1047. function xmlsitemap_form_submit_flag_regenerate($form, $form_state) {
  1048. foreach ($form_state['values'] as $variable => $value) {
  1049. $stored_value = variable_get($variable, 'not_a_variable');
  1050. if (is_array($value) && !empty($form_state['values']['array_filter'])) {
  1051. $value = array_keys(array_filter($value));
  1052. }
  1053. if ($stored_value != 'not_a_variable' && $stored_value != $value) {
  1054. variable_set('xmlsitemap_regenerate_needed', TRUE);
  1055. drupal_set_message(t('XML sitemap settings have been modified and the files should be regenerated. You can <a href="@run-cron">run cron manually</a> to regenerate the cached files.', array('@run-cron' => url('admin/reports/status/run-cron', array('query' => drupal_get_destination())))), 'warning', FALSE);
  1056. return;
  1057. }
  1058. }
  1059. }
  1060. /**
  1061. * Set the current user stored in $GLOBALS['user'].
  1062. */
  1063. function xmlsitemap_switch_user($new_user = NULL) {
  1064. global $user;
  1065. $user_original = &xmlsitemap_static(__FUNCTION__);
  1066. if (!isset($new_user)) {
  1067. if (isset($user_original)) {
  1068. // Restore the original user.
  1069. $user = $user_original;
  1070. $user_original = NULL;
  1071. session_save_session(TRUE);
  1072. }
  1073. else {
  1074. return FALSE;
  1075. }
  1076. }
  1077. elseif (is_numeric($new_user) && $user->uid != $new_user) {
  1078. // Get the full user object.
  1079. if (!$new_user) {
  1080. $new_user = drupal_anonymous_user();
  1081. }
  1082. elseif (!$new_user = user_load($new_user)) {
  1083. return FALSE;
  1084. }
  1085. // Backup the original user object.
  1086. if (!isset($user_original)) {
  1087. $user_original = $user;
  1088. session_save_session(FALSE);
  1089. }
  1090. $user = $new_user;
  1091. }
  1092. elseif (is_object($new_user) && $user->uid != $new_user->uid) {
  1093. // Backup the original user object.
  1094. if (!isset($user_original)) {
  1095. $user_original = $user;
  1096. session_save_session(FALSE);
  1097. }
  1098. $user = $new_user;
  1099. }
  1100. else {
  1101. return FALSE;
  1102. }
  1103. return $user;
  1104. }
  1105. /**
  1106. * Restore the user that was originally loaded.
  1107. *
  1108. * @return
  1109. * Current user.
  1110. */
  1111. function xmlsitemap_restore_user() {
  1112. return xmlsitemap_switch_user();
  1113. }
  1114. function xmlsitemap_process_form_link_options($form, &$form_state) {
  1115. $link = &$form_state['values']['xmlsitemap'];
  1116. $fields = array('status' => XMLSITEMAP_STATUS_DEFAULT, 'priority' => XMLSITEMAP_PRIORITY_DEFAULT);
  1117. foreach ($fields as $field => $default) {
  1118. if ($link[$field] === 'default') {
  1119. $link[$field] = isset($link[$field . '_default']) ? $link[$field . '_default'] : $default;
  1120. $link[$field . '_override'] = 0;
  1121. }
  1122. else {
  1123. $link[$field . '_override'] = 1;
  1124. }
  1125. }
  1126. }
  1127. function xmlsitemap_link_bundle_settings_form_submit($form, &$form_state) {
  1128. $entity = $form['xmlsitemap']['#entity'];
  1129. $bundle = $form['xmlsitemap']['#bundle'];
  1130. // Handle new bundles by fetching the proper bundle key value from the form
  1131. // state values.
  1132. if (empty($bundle)) {
  1133. $entity_info = $form['xmlsitemap']['#entity_info'];
  1134. if (isset($entity_info['bundle keys']['bundle'])) {
  1135. $bundle_key = $entity_info['bundle keys']['bundle'];
  1136. if (isset($form_state['values'][$bundle_key])) {
  1137. $bundle = $form_state['values'][$bundle_key];
  1138. $form['xmlsitemap']['#bundle'] = $bundle;
  1139. }
  1140. }
  1141. }
  1142. xmlsitemap_link_bundle_settings_save($entity, $bundle, $form_state['values']['xmlsitemap']);
  1143. $entity_info = $form['xmlsitemap']['#entity_info'];
  1144. if (!empty($form['xmlsitemap']['#show_message'])) {
  1145. drupal_set_message(t('XML sitemap settings for the @bundle-label %bundle have been saved.', array('@bundle-label' => drupal_strtolower($entity_info['bundle label']), '%bundle' => $entity_info['bundles'][$bundle]['label'])));
  1146. }
  1147. // Unset the form values since we have already saved the bundle settings and
  1148. // we don't want these values to get saved as variables in-case this form
  1149. // also uses system_settings_form().
  1150. unset($form_state['values']['xmlsitemap']);
  1151. }
  1152. /**
  1153. * @todo Document this function.
  1154. * @todo Make these translatable
  1155. */
  1156. function xmlsitemap_get_changefreq_options() {
  1157. return array(
  1158. XMLSITEMAP_FREQUENCY_ALWAYS => 'always',
  1159. XMLSITEMAP_FREQUENCY_HOURLY => 'hourly',
  1160. XMLSITEMAP_FREQUENCY_DAILY => 'daily',
  1161. XMLSITEMAP_FREQUENCY_WEEKLY => 'weekly',
  1162. XMLSITEMAP_FREQUENCY_MONTHLY => 'monthly',
  1163. XMLSITEMAP_FREQUENCY_YEARLY => 'yearly',
  1164. );
  1165. }
  1166. /**
  1167. * Load a language object by its language code.
  1168. *
  1169. * @param $language
  1170. * A language code. If not provided the default language will be returned.
  1171. * @return
  1172. * A language object.
  1173. */
  1174. function xmlsitemap_language_load($language = '') {
  1175. $languages = &xmlsitemap_static(__FUNCTION__);
  1176. if (!isset($languages)) {
  1177. $languages = language_list();
  1178. $languages[''] = NULL;
  1179. }
  1180. return isset($languages[$language]) ? $languages[$language] : NULL;
  1181. }
  1182. /**
  1183. * @defgroup xmlsitemap_context_api XML sitemap API for sitemap contexts.
  1184. * @{
  1185. */
  1186. function xmlsitemap_get_context_info($context = NULL, $reset = FALSE) {
  1187. global $language;
  1188. $info = &xmlsitemap_static(__FUNCTION__);
  1189. xmlsitemap_load_all_includes();
  1190. if ($reset) {
  1191. $info = NULL;
  1192. }
  1193. elseif ($cached = cache_get('xmlsitemap:context_info:' . $language->language)) {
  1194. $info = $cached->data;
  1195. }
  1196. if (!isset($info)) {
  1197. $info = module_invoke_all('xmlsitemap_context_info');
  1198. drupal_alter('xmlsitemap_context_info', $info);
  1199. ksort($info);
  1200. // Cache by language since this info contains translated strings.
  1201. cache_set('xmlsitemap:context_info:' . $language->language, $info);
  1202. }
  1203. if (isset($context)) {
  1204. return isset($info[$context]) ? $info[$context] : NULL;
  1205. }
  1206. return $info;
  1207. }
  1208. /**
  1209. * Get the sitemap context of the current request.
  1210. */
  1211. function xmlsitemap_get_current_context() {
  1212. $context = &xmlsitemap_static(__FUNCTION__);
  1213. xmlsitemap_load_all_includes();
  1214. if (!isset($context)) {
  1215. $context = module_invoke_all('xmlsitemap_context');
  1216. drupal_alter('xmlsitemap_context', $context);
  1217. asort($context);
  1218. }
  1219. return $context;
  1220. }
  1221. function _xmlsitemap_sitemap_context_summary(stdClass $sitemap, $context_key, array $context_info) {
  1222. $context_value = isset($sitemap->context[$context_key]) ? $sitemap->context[$context_key] : NULL;
  1223. if (!isset($context_value)) {
  1224. return t('Default');
  1225. }
  1226. elseif (!empty($context_info['summary callback'])) {
  1227. return $context_info['summary callback']($context_value);
  1228. }
  1229. else {
  1230. return $context_value;
  1231. }
  1232. }
  1233. /**
  1234. * @} End of "defgroup xmlsitemap_context_api"
  1235. */
  1236. /**
  1237. * Run a progressive batch operation.
  1238. */
  1239. function xmlsitemap_run_unprogressive_batch() {
  1240. $batch = batch_get();
  1241. if (!empty($batch)) {
  1242. // If there is already something in the batch, don't run.
  1243. return FALSE;
  1244. }
  1245. $args = func_get_args();
  1246. $batch_callback = array_shift($args);
  1247. if (function_exists('lock_acquire') && !lock_acquire($batch_callback)) {
  1248. return FALSE;
  1249. }
  1250. // Attempt to increase the execution time.
  1251. xmlsitemap_set_time_limit(240);
  1252. // Build the batch array.
  1253. $batch = call_user_func_array($batch_callback, $args);
  1254. batch_set($batch);
  1255. // We need to manually set the progressive variable again.
  1256. // @todo Remove when http://drupal.org/node/638712 is fixed.
  1257. $batch =& batch_get();
  1258. $batch['progressive'] = FALSE;
  1259. // Run the batch process.
  1260. batch_process();
  1261. if (function_exists('lock_release')) {
  1262. lock_release($batch_callback);
  1263. }
  1264. return TRUE;
  1265. }
  1266. /**
  1267. * Workaround for missing breadcrumbs on callback and action paths.
  1268. */
  1269. function _xmlsitemap_set_breadcrumb($path = 'admin/settings/xmlsitemap') {
  1270. $breadcrumb = array();
  1271. $path = explode('/', $path);
  1272. do {
  1273. $menu_path = implode('/', $path);
  1274. $menu_item = menu_get_item($menu_path);
  1275. array_unshift($breadcrumb, l($menu_item['title'], $menu_path));
  1276. } while (array_pop($path) && !empty($path));
  1277. array_unshift($breadcrumb, l(t('Home'), NULL));
  1278. drupal_set_breadcrumb($breadcrumb);
  1279. }
  1280. function xmlsitemap_get_operation_link($url, $options = array()) {
  1281. static $destination;
  1282. if (!isset($destination)) {
  1283. $destination = drupal_get_destination();
  1284. }
  1285. $link = array('href' => $url) + $options;
  1286. // Fetch the item's menu router link info and title.
  1287. $item = menu_get_item($url);
  1288. $link += array('title' => $item['title'], 'router info' => $item, 'query' => $destination);
  1289. drupal_alter('xmlsitemap_operation_link', $link);
  1290. return $link;
  1291. }
  1292. // Functions specific to the Drupal 6 branch of this module.
  1293. function xmlsitemap_static_reset($name = NULL) {
  1294. xmlsitemap_static($name, NULL, TRUE);
  1295. }
  1296. function &xmlsitemap_static($name, $default_value = NULL, $reset = FALSE) {
  1297. static $data = array(), $default = array();
  1298. if (!isset($name)) {
  1299. // All variables are reset. This needs to be done one at a time so that
  1300. // references returned by earlier invocations of drupal_static() also get
  1301. // reset.
  1302. foreach ($default as $name => $value) {
  1303. $data[$name] = $value;
  1304. }
  1305. // As the function returns a reference, the return should always be a
  1306. // variable.
  1307. return $data;
  1308. }
  1309. if ($reset) {
  1310. // The reset means the default is loaded.
  1311. if (array_key_exists($name, $default)) {
  1312. $data[$name] = $default[$name];
  1313. }
  1314. else {
  1315. // Reset was called before a default is set and yet a variable must be
  1316. // returned.
  1317. return $data;
  1318. }
  1319. }
  1320. elseif (!array_key_exists($name, $data)) {
  1321. // Store the default value internally and also copy it to the reference to
  1322. // be returned.
  1323. $default[$name] = $data[$name] = $default_value;
  1324. }
  1325. return $data[$name];
  1326. }
  1327. /**
  1328. * Given an table and field, return the field type.
  1329. *
  1330. * @param $table
  1331. * The table name.
  1332. * @param $field
  1333. * The field name.
  1334. * @return
  1335. * The schema type of {table}.field.
  1336. */
  1337. function _xmlsitemap_get_field_type($table, $field) {
  1338. $schema = &xmlsitemap_static(__FUNCTION__);
  1339. if (!isset($schema[$table])) {
  1340. $schema[$table] = drupal_get_schema($table);
  1341. }
  1342. return $schema[$table]['fields'][$field]['type'];
  1343. }
  1344. /**
  1345. * Load all modulename.xmlsitemap.inc files.
  1346. *
  1347. * Instead of blindly running on all modules like module_load_all_includes(),
  1348. * this function will cache which modules actually have those files, which
  1349. * benefits performance.
  1350. */
  1351. function xmlsitemap_load_all_includes() {
  1352. $modules = &xmlsitemap_static(__FUNCTION__);
  1353. if (!isset($modules)) {
  1354. if ($cache = cache_get('xmlsitemap:registry:xmlsitemap.inc')) {
  1355. $modules = $cache->data;
  1356. }
  1357. else {
  1358. $modules = module_list();
  1359. }
  1360. foreach ($modules as $index => $module) {
  1361. if (module_load_include('xmlsitemap.inc', $module) === FALSE) {
  1362. // If the module.xmlsitemap.inc file does not exist, remove it from
  1363. // the registry.
  1364. unset($modules[$index]);
  1365. }
  1366. }
  1367. if (!$cache) {
  1368. cache_set('xmlsitemap:registry:xmlsitemap.inc', $modules);
  1369. }
  1370. }
  1371. }
  1372. /**
  1373. * Backport of element_get_visible_children() from Drupal 7.
  1374. */
  1375. function xmlsitemap_element_get_visible_children(array $elements) {
  1376. foreach (element_children($elements) as $key) {
  1377. // Skip un-accessible children.
  1378. if (isset($elements[$key]['#access']) && !$elements[$key]['#access']) {
  1379. continue;
  1380. }
  1381. // Skip value and hidden elements, since they are not rendered.
  1382. if (isset($elements[$key]['#type']) && in_array($elements[$key]['#type'], array('value', 'hidden'))) {
  1383. continue;
  1384. }
  1385. return TRUE;
  1386. }
  1387. return FALSE;
  1388. }
  1389. /**
  1390. * Backport of entity_uri() from Drupal 7.
  1391. */
  1392. function xmlsitemap_entity_uri($entity_type, &$entity) {
  1393. // This check enables the URI of an entity to be easily overridden from what
  1394. // the callback for the entity type or bundle would return, and it helps
  1395. // minimize performance overhead when entity_uri() is called multiple times
  1396. // for the same entity.
  1397. if (!isset($entity->uri)) {
  1398. $info = xmlsitemap_get_link_info($entity_type);
  1399. list($id, , $bundle) = xmlsitemap_entity_extract_ids($entity_type, $entity);
  1400. // A bundle-specific callback takes precedence over the generic one for the
  1401. // entity type.
  1402. if (isset($info['bundles'][$bundle]['uri callback'])) {
  1403. $uri_callback = $info['bundles'][$bundle]['uri callback'];
  1404. }
  1405. elseif (isset($info['uri callback'])) {
  1406. $uri_callback = $info['uri callback'];
  1407. }
  1408. else {
  1409. $uri_callback = NULL;
  1410. }
  1411. // Invoke the callback to get the URI. If there is no callback, set the
  1412. // entity's 'uri' property to FALSE to indicate that it is known to not have
  1413. // a URI.
  1414. if (isset($uri_callback) && function_exists($uri_callback)) {
  1415. $entity->uri = $uri_callback($entity);
  1416. if (!isset($entity->uri['options'])) {
  1417. $entity->uri['options'] = array();
  1418. }
  1419. // Pass the entity data to url() so that alter functions do not need to
  1420. // lookup this entity again.
  1421. //$entity->uri['options']['entity_type'] = $entity_type;
  1422. //$entity->uri['options']['entity'] = $entity;
  1423. }
  1424. else {
  1425. $entity->uri = FALSE;
  1426. }
  1427. }
  1428. return $entity->uri ? $entity->uri : NULL;
  1429. }
  1430. /**
  1431. * Backport of entity_extract_ids() from Drupal 7.
  1432. */
  1433. function xmlsitemap_entity_extract_ids($entity_type, $entity) {
  1434. $info = xmlsitemap_get_link_info($entity_type);
  1435. // Objects being created might not have an id yet.
  1436. $id = isset($entity->{$info['entity keys']['id']}) ? $entity->{$info['entity keys']['id']} : NULL;
  1437. // If no bundle key provided, then we assume a single bundle, named after the
  1438. // entity type.
  1439. $bundle = $info['entity keys']['bundle'] ? $entity->{$info['entity keys']['bundle']} : $entity_type;
  1440. return array($id, NULL, $bundle);
  1441. }
  1442. /**
  1443. * Backport of the DBTNG fetchCol() from Drupal 7.
  1444. */
  1445. function xmlsitemap_db_fetch_col($query) {
  1446. $row = array();
  1447. while ($result = db_result($query)) {
  1448. $row[] = $result;
  1449. }
  1450. return $row;
  1451. }
  1452. /**
  1453. * Backport of the DBTNG fetchAllAssoc() from Drupal 7.
  1454. */
  1455. function xmlsitemap_db_fetch_all_assoc($query, $field) {
  1456. $return = array();
  1457. while ($result = db_fetch_object($query)) {
  1458. if (isset($result->$field)) {
  1459. $key = $result->$field;
  1460. $return[$key] = $result;
  1461. }
  1462. }
  1463. return $return;
  1464. }
  1465. /**
  1466. * Backport of drupal_hash_base64() from Drupal 7.
  1467. *
  1468. * Calculate a base-64 encoded, URL-safe sha-256 hash.
  1469. *
  1470. * @param $data
  1471. * String to be hashed.
  1472. *
  1473. * @return
  1474. * A base-64 encoded sha-256 hash, with + replaced with -, / with _ and
  1475. * any = padding characters removed.
  1476. */
  1477. function xmlsitemap_drupal_hash_base64($data) {
  1478. if (function_exists('hash')) {
  1479. $hash = base64_encode(hash('sha256', $data, TRUE));
  1480. }
  1481. else {
  1482. $hash = base64_encode(sha1($data, TRUE));
  1483. }
  1484. // Modify the hash so it's safe to use in URLs.
  1485. return strtr($hash, array('+' => '-', '/' => '_', '=' => ''));
  1486. }
  1487. /**
  1488. * Backport of drupal_set_time_limit from Drupal 7.
  1489. */
  1490. function xmlsitemap_set_time_limit($time_limit) {
  1491. if (function_exists('set_time_limit')) {
  1492. @set_time_limit($time_limit);
  1493. }
  1494. }