263
264
* Menu callback; return the site map settings form.
265
266
function xmlsitemap_settings_sitemap() {
267
xmlsitemap_show_admin_messages();
266
268
$form['xmlsitemap_chunk_size'] = array(
267
269
'#type' => 'textfield',
268
270
'#title' => t('Chunk size'),
269
'#default_value' => variable_get('xmlsitemap_chunk_size', 50000),
271
'#default_value' => variable_get('xmlsitemap_chunk_size', 200),
271
273
'#maxlength' => 5,
272
274
'#description' => t('This is the number of links to include in one site map. Values can range between 1 and 50,000. If the total number of links exceeds the chunk size, multiple site maps will be generated.'),
299
* Menu callback; return the update database form.
301
* Form builder; return the database update form.
301
function xmlsitemap_tools_update_database() {
303
foreach (module_implements('xmlsitemap_database_batch_operation') as $module) {
304
$options[$module] = $module .'.module';
303
function xmlsitemap_tools_update_database($form_state = NULL) {
304
xmlsitemap_show_admin_messages();
306
$step = isset($form_state['storage']['step']) ? $form_state['storage']['step'] : 1;
309
$title = t('First step');
310
$hook = 'xmlsitemap_update_module_tables_batch_operations';
313
$title = t('Second step');
314
$hook = 'xmlsitemap_update_main_table_batch_operations';
306
if (empty($options)) {
307
$form['xmlsitemap_description'] = array(
318
foreach (module_implements($hook) as $module) {
319
$modules[] = $module;
320
$modules_list[] = $module .'.module';
322
if (!empty($modules)) {
323
$form['#modules'] = $modules;
324
asort($modules_list);
326
$form['title'] = array(
328
'#suffix' => '</h3>',
331
if (empty($modules)) {
332
$modules_list[] = t('No modules implement the required code for this database update step');
334
$form['list'] = array(
308
335
'#prefix' => '<div>',
309
336
'#suffix' => '</div>',
310
'#value' => t('There is no module which implements the required code to update its own internal database tables.'),
337
'#value' => theme('item_list', $modules_list),
314
$form['xmlsitemap_database_modules'] = array(
316
'#title' => t('XML Sitemap modules'),
317
'#options' => $options,
318
'#description' => t('The drop-down menu lists all the modules which save links in the site map, and which implement the required code to update their own database tables; select the module that needs to update its database tables.'),
320
341
$form['submit'] = array(
321
342
'#type' => 'submit',
322
343
'#value' => t('Continue'),
350
* Submission function for the database update form.
328
352
function xmlsitemap_tools_update_database_submit($form, &$form_state) {
329
$module = $form_state['values']['xmlsitemap_database_modules'];
330
$function = $module .'_xmlsitemap_database_batch_operation';
331
if (function_exists($function)) {
332
$database_batch = $function();
333
if (isset($database_batch['update function'])) {
334
$batch['operations'][] = array($database_batch['update function'], array());
336
$batch['operations'][] = array($database_batch['add function'], array());
337
$batch['file'] = drupal_get_path('module', $module) .'/'. $database_batch['file'];
338
if (isset($database_batch['final callback'])) {
339
$final_callback = $database_batch['final callback'];
342
$final_callback = 'xmlsitemap_database_update_finished';
344
$batch['finished'] = $final_callback;
353
$step = isset($form_state['storage']['step']) ? $form_state['storage']['step'] : 1;
356
$hook = '_xmlsitemap_update_module_tables_batch_operations';
359
$hook = '_xmlsitemap_update_main_table_batch_operations';
362
if (isset($form['#modules']) && isset($hook)) {
363
foreach ($form['#modules'] as $module) {
364
$function = $module . $hook;
365
if (function_exists($function)) {
366
$result = $function();
367
if (isset($result)) {
368
if (is_array($result)) {
369
foreach ($result as $operation) {
370
$batch['operations'][] = array($operation, array());
373
elseif (is_string($result)) {
374
$batch['operations'][] = array($result, array());
381
$batch['title'] = t('Updating database');
382
$batch['finished'] = 'xmlsitemap_database_update_finished';
345
383
batch_set($batch);
385
$form_state['storage']['step'] = $step + 1;
388
/*****************************************************************************
389
* Public functions - messages shown in the administration page.
390
****************************************************************************/
393
* Add a message to the list of the messages shown in the module administration
396
* The text of the message to be shown.
398
* The type of the message, chosen between 'status', 'warning', and 'error';
399
* the default type is 'status'.
401
function xmlsitemap_add_admin_message($message, $type = 'status') {
405
$var = 'xmlsitemap_admin_error_messages';
408
$var = 'xmlsitemap_admin_warning_messages';
411
$var = 'xmlsitemap_admin_status_messages';
415
$messages = variable_get($var, array());
416
array_unshift($messages, $message);
417
variable_set($var, $messages);
423
* Show all the messages set with xmlsitemap_add_admin_message().
424
* The function must be called at the beginning of every menu callback function
425
* that creates an XML Sitemap administration page.
427
function xmlsitemap_show_admin_messages() {
429
'error' => 'xmlsitemap_admin_error_messages',
430
'warning' => 'xmlsitemap_admin_warning_messages',
431
'status' => 'xmlsitemap_admin_status_messages',
433
foreach ($types as $type => $var) {
434
$messages = variable_get($var, array());
435
if (count($messages)) {
436
foreach ($messages as $message) {
437
drupal_set_message($message, $type);
439
variable_set($var, array());
363
457
function xmlsitemap_database_update_finished($success, $results, $operations) {
365
if (!empty($results['add'])) {
367
format_plural($results['add'], 'Added 1 row.', 'Added @count rows.')
370
if (!empty($results['update'])) {
372
format_plural($results['update'], 'Updated 1 row.', 'Updated @count rows.')
459
if (!empty($results['added'])) {
461
format_plural($results['added'], 'Added 1 row.', 'Added @count rows.')
464
if (!empty($results['deleted'])) {
466
format_plural($results['deleted'], 'Deleted 1 row.', 'Deleted @count rows.')
469
if (!empty($results['updated'])) {
471
format_plural($results['updated'], 'Updated 1 row.', 'Updated @count rows.')
377
$error_operation = reset($operations);
378
$message = 'An error occurred while processing '. $error_operation[0] .' with arguments :'. print_r($error_operation[0], TRUE);
379
drupal_set_message($message);
476
drupal_set_message(t("An error occured, and the database update processing didn't complete."));
567
664
print ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'."\n";
568
665
print ' xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9'."\n";
569
666
print ' http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'."\n";
570
$chunk_size = variable_get('xmlsitemap_chunk_size', 50000);
667
$chunk_size = variable_get('xmlsitemap_chunk_size', 200);
571
668
$start = $chunk * $chunk_size;
572
669
$links = db_query_range("SELECT * FROM {xmlsitemap} ORDER BY lastmod DESC, changefreq, priority DESC, loc", $start , $chunk_size);
573
670
while ($link = db_fetch_object($links)) {
650
747
function _xmlsitemap_update() {
651
748
db_query("DELETE FROM {xmlsitemap}");
652
db_query("INSERT INTO {xmlsitemap} (loc, lastmod, changefreq, priority) VALUES ('%s', %d, %d, %f)", xmlsitemap_url(NULL, NULL, NULL, NULL, TRUE),time() - 1, 1, variable_get('xmlsitemap_front_page_priority', 1));
749
db_query("INSERT INTO {xmlsitemap} (loc, lastmod, changefreq, priority) VALUES ('%s', %d, %d, %f)",
751
xmlsitemap_url(NULL, NULL, NULL, NULL, TRUE),
754
variable_get('xmlsitemap_front_page_priority', 1)
653
757
module_invoke_all('xmlsitemap_links');
655
759
db_query("DELETE FROM {cache_page} WHERE cid LIKE '%%sitemap%%.xml'");