xmlsitemap.admin.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. <?php
  2. // $Id: xmlsitemap.admin.inc,v 1.12 2010/01/24 04:10:46 davereid Exp $
  3. /**
  4. * @file
  5. * Administrative page callbacks for the xmlsitemap module.
  6. *
  7. * @ingroup xmlsitemap
  8. */
  9. /**
  10. * Form builder; Administration settings form.
  11. *
  12. * @see system_settings_form()
  13. * @see xmlsitemap_settings_form_validate()
  14. */
  15. function xmlsitemap_settings_form() {
  16. $form['xmlsitemap'] = array(
  17. '#type' => 'fieldset',
  18. '#title' => t('Settings'),
  19. '#collapsible' => TRUE,
  20. '#collapsed' => FALSE,
  21. );
  22. $form['xmlsitemap']['xmlsitemap_minimum_lifetime'] = array(
  23. '#type' => 'select',
  24. '#title' => t('Minimum sitemap lifetime'),
  25. '#options' => array(0 => t('No minimum')) + drupal_map_assoc(array(300, 900, 1800, 3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800), 'format_interval'),
  26. '#default_value' => xmlsitemap_var('minimum_lifetime')
  27. );
  28. $form['xmlsitemap']['xmlsitemap_xsl'] = array(
  29. '#type' => 'checkbox',
  30. '#title' => t('Include a stylesheet in the sitemaps for humans.'),
  31. '#default_value' => xmlsitemap_var('xsl'),
  32. '#description' => t('Using the stylesheet will add formatting and tables with sorting to make it easier to view the XML sitemap data instead of viewing raw XML output. Search engines will ignore any formatting.')
  33. );
  34. $form['xmlsitemap']['xmlsitemap_languages'] = array(
  35. '#type' => 'checkboxes',
  36. '#title' => t('Generate sitemaps for the following languages'),
  37. '#options' => array(language_default('language') => language_default('name')),
  38. '#default_value' => xmlsitemap_var('languages'),
  39. '#process' => array('form_process_checkboxes', '_xmlsitemap_process_language_checkboxes'),
  40. '#description' => !module_exists('xmlsitemap_i18n') ? t('To enable multilingual features, enable the XML sitemap internationalization module.') : '',
  41. );
  42. $form['advanced'] = array(
  43. '#type' => 'fieldset',
  44. '#title' => t('Advanced settings'),
  45. '#collapsible' => TRUE,
  46. '#collapsed' => !variable_get('xmlsitemap_developer_mode', FALSE),
  47. '#weight' => 10,
  48. );
  49. //$form['advanced']['xmlsitemap_gz'] = array(
  50. // '#type' => 'checkbox',
  51. // '#title' => t('Generate additional compressed sitemaps using gzip.'),
  52. // '#default_value' => xmlsitemap_var('gz'),
  53. // '#disabled' => !function_exists('gzencode'),
  54. //);
  55. $form['advanced']['xmlsitemap_chunk_size'] = array(
  56. '#type' => 'select',
  57. '#title' => t('Number of links in each sitemap page'),
  58. '#options' => array('auto' => t('Automatic (recommended)')) + drupal_map_assoc(array(100, 500, 1000, 2500, 5000, 10000, 25000, 50000)),
  59. '#default_value' => xmlsitemap_var('chunk_size'),
  60. // @todo This description is not clear.
  61. '#description' => t('If there are problems with rebuilding the sitemap, you may want to manually set this value. If you have more than 50,000 links, an index with multiple sitemap pages will be generated. There is a maximum of 1000 sitemap pages.'),
  62. );
  63. $form['advanced']['xmlsitemap_batch_limit'] = array(
  64. '#type' => 'select',
  65. '#title' => t('Maximum number of sitemap links to process at once'),
  66. '#options' => drupal_map_assoc(array(5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000)),
  67. '#default_value' => xmlsitemap_var('batch_limit'),
  68. '#description' => t('If you have problems running cron or rebuilding the sitemap, you may want to lower this value.'),
  69. );
  70. $path = variable_get('xmlsitemap_path', 'xmlsitemap');
  71. if (!xmlsitemap_check_directory()) {
  72. form_set_error('xmlsitemap_path', t('The directory %directory does not exist or is not writable.', array('%directory' => xmlsitemap_get_directory())));
  73. }
  74. $form['advanced']['xmlsitemap_path'] = array(
  75. '#type' => 'textfield',
  76. '#title' => t('Sitemap cache directory'),
  77. '#default_value' => $path,
  78. '#size' => 30,
  79. '#maxlength' => 255,
  80. '#description' => t('Subdirectory where the sitemap data will be stored. This folder <strong>must not be shared</strong> with any other Drupal site or install using XML sitemap.'),
  81. '#field_prefix' => file_directory_path() . '/',
  82. '#required' => TRUE,
  83. );
  84. $form['advanced']['xmlsitemap_base_url'] = array(
  85. '#type' => 'textfield',
  86. '#title' => t('Base URL'),
  87. '#default_value' => xmlsitemap_var('base_url'),
  88. '#size' => 30,
  89. '#description' => t('This is the base URL for links generated in the sitemap.'),
  90. '#required' => TRUE,
  91. );
  92. $form['advanced']['xmlsitemap_developer_mode'] = array(
  93. '#type' => 'checkbox',
  94. '#title' => t('Enable developer mode.'),
  95. '#default_value' => variable_get('xmlsitemap_developer_mode', FALSE),
  96. '#description' => t('Exposes additional settings intended for development.'),
  97. );
  98. $form['xmlsitemap_settings'] = array(
  99. '#type' => 'vertical_tabs',
  100. '#weight' => 20,
  101. );
  102. $form['frontpage'] = array(
  103. '#type' => 'fieldset',
  104. '#title' => t('Front page'),
  105. '#description' => t('The front page path can be changed at <a href="@url-frontpage">@url-frontpage</a>.', array('@url-frontpage' => url('admin/settings/site-information'))),
  106. '#collapsible' => TRUE,
  107. '#collapsed' => TRUE,
  108. '#weight' => 20,
  109. '#group' => 'xmlsitemap_settings',
  110. );
  111. $form['frontpage']['xmlsitemap_frontpage_priority'] = array(
  112. '#type' => 'select',
  113. '#title' => t('Priority'),
  114. '#options' => xmlsitemap_get_priority_options(),
  115. '#default_value' => xmlsitemap_var('frontpage_priority'),
  116. );
  117. $form['frontpage']['xmlsitemap_frontpage_changefreq'] = array(
  118. '#type' => 'select',
  119. '#title' => t('Change frequency'),
  120. '#options' => xmlsitemap_get_changefreq_options(),
  121. '#default_value' => xmlsitemap_var('frontpage_changefreq'),
  122. );
  123. $form['#pre_render'][] = 'vertical_tabs_form_pre_render';
  124. $form['#validate'][] = 'xmlsitemap_settings_form_validate';
  125. $form['#submit'][] = 'xmlsitemap_settings_form_submit';
  126. $form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
  127. $form = system_settings_form($form);
  128. $form['actions']['#weight'] = 100;
  129. return $form;
  130. }
  131. /**
  132. * Show a link to each languages' sitemap and disable the default language
  133. * checkbox.
  134. */
  135. function _xmlsitemap_process_language_checkboxes($element) {
  136. foreach (element_children($element) as $key) {
  137. if ($key == language_default('language')) {
  138. $element[$key]['#disabled'] = TRUE;
  139. $element[$key]['#default_value'] = TRUE;
  140. $element[$key]['#weight'] = -1;
  141. }
  142. $link = url('sitemap.xml', array('absolute' => TRUE, 'language' => xmlsitemap_language_load($key)));
  143. $element[$key]['#description'] = l($link, $link);
  144. }
  145. return $element;
  146. }
  147. /**
  148. * Form validator; Check the sitemap files directory.
  149. *
  150. * @see xmlsitemap_settings_form()
  151. */
  152. function xmlsitemap_settings_form_validate($form, &$form_state) {
  153. // Check that the chunk size will not create more than 1000 chunks.
  154. $chunk_size = $form_state['values']['xmlsitemap_chunk_size'];
  155. if ($chunk_size != 'auto' && $chunk_size != 50000 && (xmlsitemap_get_link_count() / $chunk_size) > 1000) {
  156. form_set_error('xmlsitemap_chunk_size', t('The sitemap page link count of @size will create more than 1,000 sitemap pages. Please increase the link count.', array('@size' => $chunk_size)));
  157. }
  158. $base_url = &$form_state['values']['xmlsitemap_base_url'];
  159. $base_url = rtrim($base_url, '/');
  160. if ($base_url != '' && !valid_url($base_url, TRUE)) {
  161. form_set_error('xmlsitemap_base_url', t('Invalid base URL.'));
  162. }
  163. }
  164. /**
  165. * Submit handler;
  166. *
  167. * @see xmlsitemap_settings_form()
  168. */
  169. function xmlsitemap_settings_form_submit($form, $form_state) {
  170. // Save any changes to the frontpage link.
  171. xmlsitemap_save_link(array('type' => 'frontpage', 'id' => 0, 'loc' => ''));
  172. }
  173. /**
  174. * Menu callback; Confirm rebuilding of the sitemap.
  175. *
  176. * @see xmlsitemap_rebuild_form_submit()
  177. */
  178. function xmlsitemap_rebuild_form() {
  179. if (!$_POST && !xmlsitemap_var('rebuild_needed')) {
  180. if (!xmlsitemap_var('regenerate_needed')) {
  181. drupal_set_message(t('Your sitemap is up to date and does not need to be rebuilt.'), 'error');
  182. }
  183. else {
  184. $_REQUEST += array('destination' => 'admin/config/search/xmlsitemap');
  185. drupal_set_message(t('A rebuild is not necessary. If you are just wanting to regenerate the XML sitemap files, you can <a href="@link-cron">run cron manually</a>.', array('@link-cron' => url('admin/reports/status/run-cron', array('query' => drupal_get_destination())))), 'warning');
  186. }
  187. }
  188. // Show only the modules that implement the 6.x-2.x hooks.
  189. $modules = module_implements('xmlsitemap_link_info', TRUE);
  190. $form['modules'] = array(
  191. '#type' => 'select',
  192. '#title' => t("Select which modules' links you would like to rebuild"),
  193. '#description' => t('If no modules are selected, the sitemap files will just be regenerated.'),
  194. '#multiple' => TRUE,
  195. '#options' => drupal_map_assoc($modules),
  196. '#default_value' => xmlsitemap_var('rebuild_needed') || !xmlsitemap_var('developer_mode') ? $modules : array(),
  197. '#access' => xmlsitemap_var('developer_mode'),
  198. );
  199. $form['save_custom'] = array(
  200. '#type' => 'checkbox',
  201. '#title' => t('Save and restore any custom inclusion and priority links.'),
  202. '#default_value' => TRUE,
  203. );
  204. return confirm_form(
  205. $form,
  206. t('Are you sure you want to rebuild the sitemap?'),
  207. 'admin/config/search/xmlsitemap',
  208. '',
  209. t('Rebuild sitemap'),
  210. t('Cancel')
  211. );
  212. }
  213. /**
  214. * Submit handler; Starts the sitemap rebuild batch.
  215. *
  216. * @see xmlsitemap_rebuild_form()
  217. * @see xmlsitemap_rebuild_batch()
  218. */
  219. function xmlsitemap_rebuild_form_submit($form, &$form_state) {
  220. // Set the rebuild flag incase something fails during the rebuild.
  221. variable_set('xmlsitemap_rebuild_needed', TRUE);
  222. module_load_include('inc', 'xmlsitemap');
  223. $batch = xmlsitemap_rebuild_batch($form_state['values']['modules'], $form_state['values']['save_custom']);
  224. batch_set($batch);
  225. $form_state['redirect'] = 'admin/config/search/xmlsitemap';
  226. }
  227. /**
  228. * Create a list of items that can be included in the sitemap.
  229. *
  230. * @param $form
  231. * An array with the form to add the type summary.
  232. * @param $type
  233. * An array of details about the type with the following key/value pairs:
  234. * type: The {xmlsitemap}.type value
  235. * title: The title of the type (usually plural form)
  236. * item_title: The title of each individual item of this type (singular)
  237. * access: An optional boolean that if TRUE will create links to each
  238. * item's edit page as specified by the link key in $items.
  239. * @param $items
  240. * An array of items with the following keys:
  241. * 'name': The translated name of the item.
  242. * 'link': The path to edit the item. If $type['access'] is not TRUE, just
  243. the item's name will be shown instead of a link.
  244. * 'status': The item's default inclusion status.
  245. * 'priority': The items default priority.
  246. */
  247. function xmlsitemap_add_form_type_summary(&$form, $type, $items) {
  248. $form[$type['type']] = array(
  249. '#type' => 'fieldset',
  250. '#title' => $type['title'],
  251. '#collapsible' => TRUE,
  252. '#collapsed' => TRUE,
  253. '#group' => 'xmlsitemap_settings',
  254. );
  255. $header = array(
  256. $type['item_title'],
  257. t('Inclusion'),
  258. t('Priority'),
  259. );
  260. $priorities = xmlsitemap_get_priority_options(NULL, FALSE);
  261. $statuses = xmlsitemap_get_status_options(NULL);
  262. $rows = array();
  263. foreach ($items as $item_type => $item) {
  264. $rows[] = array(
  265. !empty($type['access']) ? l($item['name'], $item['link'], array('query' => drupal_get_destination())) : check_plain($item['name']),
  266. $statuses[$item['status'] ? 1 : 0],
  267. $priorities[number_format($item['priority'], 1)],
  268. );
  269. }
  270. $form[$type['type']]['list'] = array(
  271. '#value' => theme('table', $header, $rows),
  272. );
  273. }
  274. /**
  275. * Add the link type XML sitemap options to the link type's form.
  276. *
  277. * @todo Abstract the submission and link updating for this submit?
  278. */
  279. function xmlsitemap_add_form_type_options(&$form, $module, $options) {
  280. $form['xmlsitemap'] = array(
  281. '#type' => 'fieldset',
  282. '#title' => t('XML sitemap'),
  283. '#description' => t('Changing these type settings will affect any items of this type that have either inclusion or priority set to default.'),
  284. '#collapsible' => TRUE,
  285. '#collapsed' => TRUE,
  286. '#access' => user_access('administer xmlsitemap'),
  287. '#group' => 'additional_settings',
  288. );
  289. $form['xmlsitemap']['xmlsitemap_' . $module . '_status'] = array(
  290. '#type' => 'select',
  291. '#title' => t('Inclusion'),
  292. '#options' => xmlsitemap_get_status_options(),
  293. '#default_value' => $options['status'],
  294. );
  295. $form['xmlsitemap']['xmlsitemap_' . $module . '_priority'] = array(
  296. '#type' => 'select',
  297. '#title' => t('Default priority'),
  298. '#options' => xmlsitemap_get_priority_options(),
  299. '#default_value' => $options['priority'],
  300. '#states' => array(
  301. 'invisible' => array(
  302. 'input[name="xmlsitemap_' . $module . '_status"]' => array('value' => 0),
  303. ),
  304. ),
  305. );
  306. }
  307. /**
  308. * Add a link's XML sitemap options to the link's form.
  309. */
  310. function xmlsitemap_add_form_link_options(&$form, $link) {
  311. $form['xmlsitemap'] = array(
  312. '#type' => 'fieldset',
  313. '#tree' => TRUE,
  314. '#title' => t('XML sitemap'),
  315. '#collapsible' => TRUE,
  316. '#collapsed' => !$link['status_override'] && !$link['priority_override'],
  317. '#access' => user_access('administer xmlsitemap'),
  318. '#group' => 'additional_settings',
  319. '#attached' => array(
  320. 'js' => array(
  321. 'vertical-tabs' => drupal_get_path('module', 'xmlsitemap') . '/xmlsitemap.js',
  322. ),
  323. ),
  324. );
  325. // Show a warning if the link is not accessible and will not be included in
  326. // the sitemap.
  327. if (!$link['access']) {
  328. $form['xmlsitemap']['warning'] = array(
  329. '#type' => 'markup',
  330. '#prefix' => '<p><strong>',
  331. '#suffix' => '</strong></p>',
  332. '#value' => ('This item is not currently visible to anonymous users, so it will not be included in the sitemap.'),
  333. );
  334. }
  335. // Status field (inclusion/exclusion)
  336. $form['xmlsitemap']['status'] = array(
  337. '#type' => 'select',
  338. '#title' => t('Inclusion'),
  339. '#options' => xmlsitemap_get_status_options($link['status_default']),
  340. '#default_value' => $link['status_override'] ? $link['status'] : 'default',
  341. );
  342. $form['xmlsitemap']['status_default'] = array(
  343. '#type' => 'value',
  344. '#value' => $link['status_default'],
  345. );
  346. $form['xmlsitemap']['status_override'] = array(
  347. '#type' => 'value',
  348. '#value' => $link['status_override'],
  349. );
  350. // Priority field
  351. $form['xmlsitemap']['priority'] = array(
  352. '#type' => 'select',
  353. '#title' => t('Priority'),
  354. '#options' => xmlsitemap_get_priority_options($link['priority_default']),
  355. '#default_value' => $link['priority_override'] ? number_format($link['priority'], 1) : 'default',
  356. '#description' => t('The priority of this URL relative to other URLs on your site.'),
  357. '#states' => array(
  358. 'invisible' => array(
  359. 'input[name="xmlsitemap[status]"]' => array('value' => $link['status_default'] ? array('default', 1) : array(1)),
  360. ),
  361. ),
  362. );
  363. $form['xmlsitemap']['priority_default'] = array(
  364. '#type' => 'value',
  365. '#value' => $link['priority_default'],
  366. );
  367. $form['xmlsitemap']['priority_override'] = array(
  368. '#type' => 'value',
  369. '#value' => $link['priority_override'],
  370. );
  371. // Other persistent fields.
  372. //$form['xmlsitemap']['lastmod'] = array(
  373. // '#type' => 'value',
  374. // '#value' => $node->xmlsitemap['lastmod'],
  375. //);
  376. //$form['xmlsitemap']['changefreq'] = array(
  377. // '#type' => 'value',
  378. // '#value' => $node->xmlsitemap['changefreq'],
  379. //);
  380. //$form['xmlsitemap']['changecount'] = array(
  381. // '#type' => 'value',
  382. // '#value' => $node->xmlsitemap['changecount'],
  383. //);
  384. // Add the submit handler to adjust the default values if selected.
  385. if (!in_array('xmlsitemap_process_form_link_options', $form['#submit'])) {
  386. array_unshift($form['#submit'], 'xmlsitemap_process_form_link_options');
  387. }
  388. }
  389. /**
  390. * Get a list of priority options.
  391. *
  392. * @param $default
  393. * Include a 'default' option.
  394. * @param $guides
  395. * Add helpful indicators for the highest, middle and lowest values.
  396. * @return
  397. * An array of options.
  398. */
  399. function xmlsitemap_get_priority_options($default = NULL, $guides = TRUE) {
  400. $options = array();
  401. $priorities = array(
  402. '1.0' => t('1.0'),
  403. '0.9' => t('0.9'),
  404. '0.8' => t('0.8'),
  405. '0.7' => t('0.7'),
  406. '0.6' => t('0.6'),
  407. '0.5' => t('0.5'),
  408. '0.4' => t('0.4'),
  409. '0.3' => t('0.3'),
  410. '0.2' => t('0.2'),
  411. '0.1' => t('0.1'),
  412. '0.0' => t('0.0'),
  413. );
  414. if (isset($default)) {
  415. $default = number_format($default, 1);
  416. $options['default'] = t('Default (@value)', array('@value' => $priorities[$default]));
  417. }
  418. // Add the rest of the options.
  419. $options += $priorities;
  420. if ($guides) {
  421. $options['1.0'] .= ' ' . t('(highest)');
  422. $options['0.5'] .= ' ' . t('(normal)');
  423. $options['0.0'] .= ' ' . t('(lowest)');
  424. }
  425. return $options;
  426. }
  427. /**
  428. * Get a list of priority options.
  429. *
  430. * @param $default
  431. * Include a 'default' option.
  432. * @return
  433. * An array of options.
  434. *
  435. * @see _xmlsitemap_translation_strings()
  436. */
  437. function xmlsitemap_get_status_options($default = NULL) {
  438. $options = array();
  439. $statuses = array(
  440. 1 => t('Included'),
  441. 0 => t('Excluded'),
  442. );
  443. if (isset($default)) {
  444. $default = $default ? 1 : 0;
  445. $options['default'] = t('Default (@value)', array('@value' => drupal_strtolower($statuses[$default])));
  446. }
  447. $options += $statuses;
  448. return $options;
  449. }