~stefanor/drupal/sr

« back to all changes in this revision

Viewing changes to modules/xmlsitemap/xmlsitemap.module

  • Committer: Stefano Rivera
  • Date: 2009-02-03 13:46:19 UTC
  • Revision ID: stefano@rivera.za.net-20090203134619-qtah2mm7178s3gla
Updated:
* swfobject to 2.0 (jquery_media)
* path_redirect beta1
* xmlsitemap update

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php 
2
 
// $Id: xmlsitemap.module,v 1.20.2.45 2009/01/01 16:06:27 kiam Exp $
 
1
<?php
 
2
// $Id: xmlsitemap.module,v 1.20.2.52 2009/02/01 23:47:39 kiam Exp $
3
3
 
4
4
/**
5
5
 * @file
19
19
 * Implementation of hook_cron().
20
20
 */
21
21
function xmlsitemap_cron() {
22
 
  if (variable_get('xmlsitemap_cron_submit', FALSE) && variable_get('xmlsitemap_changed', FALSE)) {
 
22
  if (!variable_get('site_offline', 0) && variable_get('xmlsitemap_cron_submit', FALSE) && variable_get('xmlsitemap_changed', FALSE)) {
23
23
    _xmlsitemap_ping();
24
24
  }
25
25
}
33
33
  }
34
34
  if (variable_get('xmlsitemap_log_access', FALSE)) {
35
35
    $items = array();
36
 
    $chunk_size = variable_get('xmlsitemap_chunk_size', 50000);
 
36
    $chunk_size = variable_get('xmlsitemap_chunk_size', 200);
37
37
    $link_count = _xmlsitemap_link_count();
38
38
    if ($link_count / $chunk_size > 1) {
39
39
      for ($chunk = 0; $chunk < $link_count / $chunk_size; ++$chunk) {
165
165
    'access arguments' => $access_content,
166
166
    'type' => MENU_CALLBACK,
167
167
  );
168
 
  $chunk_size = variable_get('xmlsitemap_chunk_size', 50000);
 
168
  $chunk_size = variable_get('xmlsitemap_chunk_size', 200);
169
169
  $link_count = _xmlsitemap_link_count();
170
170
  if ($link_count / $chunk_size > 1) {
171
171
    for ($chunk = 0; $chunk < $link_count / $chunk_size; ++$chunk) {
197
197
    _xmlsitemap_update();
198
198
  }
199
199
  drupal_set_header('Content-type: text/xml; charset=utf-8');
200
 
  $chunk_size = variable_get('xmlsitemap_chunk_size', 50000);
 
200
  $chunk_size = variable_get('xmlsitemap_chunk_size', 200);
 
201
  $link_count = _xmlsitemap_link_count();
201
202
  if (isset($chunk)) {
202
 
    $link_count = _xmlsitemap_link_count();
203
203
    if ($chunk < $link_count / $chunk_size) {
204
204
      _xmlsitemap_output_chunk($chunk);
205
205
    }
223
223
 * Menu callback; return the search engine settings form.
224
224
 */
225
225
function xmlsitemap_settings_engines() {
 
226
  xmlsitemap_show_admin_messages();
226
227
  $form['submission'] = array(
227
228
    '#type' => 'fieldset',
228
229
    '#title' => t('Submission settings'),
263
264
 * Menu callback; return the site map settings form.
264
265
 */
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),
270
272
    '#size' => 10,
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.'),
296
298
}
297
299
 
298
300
/**
299
 
 * Menu callback; return the update database form.
 
301
 * Form builder; return the database update form.
300
302
 */
301
 
function xmlsitemap_tools_update_database() {
302
 
  $options = array();
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();
 
305
  $form = array();
 
306
  $step = isset($form_state['storage']['step']) ? $form_state['storage']['step'] : 1;
 
307
  switch ($step) {
 
308
    case 1:
 
309
      $title = t('First step');
 
310
      $hook = 'xmlsitemap_update_module_tables_batch_operations';
 
311
      break;
 
312
    case 2:
 
313
      $title = t('Second step');
 
314
      $hook = 'xmlsitemap_update_main_table_batch_operations';
 
315
      break;
305
316
  }
306
 
  if (empty($options)) {
307
 
    $form['xmlsitemap_description'] = array(
 
317
  if (isset($hook)) {
 
318
    foreach (module_implements($hook) as $module) {
 
319
      $modules[] = $module;
 
320
      $modules_list[] = $module .'.module';
 
321
    }
 
322
    if (!empty($modules)) {
 
323
      $form['#modules'] = $modules;
 
324
      asort($modules_list);
 
325
    }
 
326
    $form['title'] = array(
 
327
      '#prefix' => '<h3>',
 
328
      '#suffix' => '</h3>',
 
329
      '#value' => $title,
 
330
    );
 
331
    if (empty($modules)) {
 
332
      $modules_list[] = t('No modules implement the required code for this database update step');
 
333
    }
 
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),
311
338
    );
312
339
  }
313
 
  else {
314
 
    $form['xmlsitemap_database_modules'] = array(
315
 
      '#type' => 'select', 
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.'),
319
 
    );
 
340
  if ($step < 3) {
320
341
    $form['submit'] = array(
321
342
      '#type' => 'submit',
322
343
      '#value' => t('Continue'),
325
346
  return $form;
326
347
}
327
348
 
 
349
/**
 
350
 * Submission function for the database update form.
 
351
 */
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());
335
 
    }
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'];
340
 
    }
341
 
    else {
342
 
      $final_callback = 'xmlsitemap_database_update_finished';
343
 
    }
344
 
    $batch['finished'] = $final_callback;
 
353
  $step = isset($form_state['storage']['step']) ? $form_state['storage']['step'] : 1;
 
354
  switch ($step) {
 
355
    case 1:
 
356
      $hook = '_xmlsitemap_update_module_tables_batch_operations';
 
357
      break;
 
358
    case 2:
 
359
      $hook = '_xmlsitemap_update_main_table_batch_operations';
 
360
      break;
 
361
  }
 
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());
 
371
            }
 
372
          }
 
373
          elseif (is_string($result)) {
 
374
            $batch['operations'][] = array($result, array());
 
375
          }
 
376
        }
 
377
      }
 
378
    }
 
379
  }
 
380
  if (isset($batch)) {
 
381
    $batch['title'] = t('Updating database');
 
382
    $batch['finished'] = 'xmlsitemap_database_update_finished';
345
383
    batch_set($batch);
346
 
 
 
384
  }
 
385
  $form_state['storage']['step'] = $step + 1;
 
386
}
 
387
 
 
388
/*****************************************************************************
 
389
 * Public functions - messages shown in the administration page.
 
390
 ****************************************************************************/
 
391
 
 
392
/**
 
393
 * Add a message to the list of the messages shown in the module administration
 
394
 * pages.
 
395
 * @param $message
 
396
 *  The text of the message to be shown.
 
397
 * @param $type
 
398
 *  The type of the message, chosen between 'status', 'warning', and 'error';
 
399
 *  the default type is 'status'.
 
400
 */
 
401
function xmlsitemap_add_admin_message($message, $type = 'status') {
 
402
  if ($message) {
 
403
    switch ($type) {
 
404
      case 'error':
 
405
        $var = 'xmlsitemap_admin_error_messages';
 
406
        break;
 
407
      case 'warning':
 
408
        $var = 'xmlsitemap_admin_warning_messages';
 
409
        break;
 
410
      case 'status':
 
411
        $var = 'xmlsitemap_admin_status_messages';
 
412
        break;
 
413
    }
 
414
    if (isset($var)) {
 
415
      $messages = variable_get($var, array());
 
416
      array_unshift($messages, $message);
 
417
      variable_set($var, $messages);
 
418
    }
 
419
  }
 
420
}
 
421
 
 
422
/**
 
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.
 
426
 */
 
427
function xmlsitemap_show_admin_messages() {
 
428
  $types = array(
 
429
    'error' => 'xmlsitemap_admin_error_messages',
 
430
    'warning' => 'xmlsitemap_admin_warning_messages',
 
431
    'status' => 'xmlsitemap_admin_status_messages',
 
432
  );
 
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);
 
438
      }
 
439
      variable_set($var, array());
 
440
    }
347
441
  }
348
442
}
349
443
 
362
456
 */
363
457
function xmlsitemap_database_update_finished($success, $results, $operations) {
364
458
  if ($success) {
365
 
    if (!empty($results['add'])) {
366
 
      drupal_set_message(
367
 
        format_plural($results['add'], 'Added 1 row.', 'Added @count rows.')
368
 
      );
369
 
    }
370
 
    if (!empty($results['update'])) {
371
 
      drupal_set_message(
372
 
        format_plural($results['update'], 'Updated 1 row.', 'Updated @count rows.')
 
459
    if (!empty($results['added'])) {
 
460
      drupal_set_message(
 
461
        format_plural($results['added'], 'Added 1 row.', 'Added @count rows.')
 
462
      );
 
463
    }
 
464
    if (!empty($results['deleted'])) {
 
465
      drupal_set_message(
 
466
        format_plural($results['deleted'], 'Deleted 1 row.', 'Deleted @count rows.')
 
467
      );
 
468
    }
 
469
    if (!empty($results['updated'])) {
 
470
      drupal_set_message(
 
471
        format_plural($results['updated'], 'Updated 1 row.', 'Updated @count rows.')
373
472
      );
374
473
    }
375
474
  }
376
475
  else {
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."));
380
477
  }
381
478
}
382
479
 
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)) {
603
700
  print '  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'."\n";
604
701
  print '  xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9'."\n";
605
702
  print '  http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd">'."\n";
606
 
  $chunk_size = variable_get('xmlsitemap_chunk_size', 50000);
 
703
  $chunk_size = variable_get('xmlsitemap_chunk_size', 200);
607
704
  $link_count = _xmlsitemap_link_count();
608
705
  for ($chunk = 0; $chunk < $link_count / $chunk_size; ++$chunk) {
609
706
    print '  <sitemap>'."\n";
649
746
 */
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)",
 
750
    array(
 
751
      xmlsitemap_url(NULL, NULL, NULL, NULL, TRUE),
 
752
      time() - 1,
 
753
      1,
 
754
      variable_get('xmlsitemap_front_page_priority', 1)
 
755
    )
 
756
  );
653
757
  module_invoke_all('xmlsitemap_links');
654
758
  menu_rebuild();
655
759
  db_query("DELETE FROM {cache_page} WHERE cid LIKE '%%sitemap%%.xml'");