xmlsitemap.module 48 KB

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