xmlsitemap.module 50 KB

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