You are here

function xmlsitemap_requirements in XML sitemap 6.2

Same name and namespace in other branches
  1. 8 xmlsitemap.install \xmlsitemap_requirements()
  2. 6 xmlsitemap.install \xmlsitemap_requirements()
  3. 7.2 xmlsitemap.install \xmlsitemap_requirements()
  4. 2.x xmlsitemap.install \xmlsitemap_requirements()

Implements hook_requirements().

File

./xmlsitemap.install, line 13
Install, update and uninstall functions for the xmlsitemap module.

Code

function xmlsitemap_requirements($phase) {
  $requirements = array();
  $t = get_t();

  // Check that required PHP extensions are enabled.
  $required_extensions = array(
    'xml',
    'xmlwriter',
  );
  $missing_extensions = array_diff($required_extensions, array_filter($required_extensions, 'extension_loaded'));
  if (!empty($missing_extensions)) {
    $requirements['xmlsitemap_php_extensions'] = array(
      'title' => $t('XML sitemap PHP extensions'),
      'value' => $t('Disabled'),
      'severity' => REQUIREMENT_ERROR,
      'description' => $t("The XML sitemap module requires you to enable the PHP extensions in the following list (see the <a href=\"@xmlsitemap_requirements\">module's system requirements page</a> for more information):", array(
        '@xmlsitemap_requirements' => 'http://drupal.org/documentation/modules/xmlsitemap/requirements',
      )) . theme('item_list', $missing_extensions),
    );
  }
  if ($phase == 'runtime') {

    // If clean URLs are disabled there must not be an actual sitemap.xml in
    // the root directory.
    if (variable_get('clean_url', 0) && file_exists('./sitemap.xml')) {
      $requirements['xmlsitemap_file'] = array(
        'title' => $t('XML sitemap'),
        'value' => $t('Existing sitemap.xml file found.'),
        'severity' => REQUIREMENT_ERROR,
        'description' => $t('The XML sitemap module cannot display its XML output if there is an existing sitemap.xml file in your website root.'),
      );
    }

    // Check that the base directory and all its subdirectories are writable.
    $requirements['xmlsitemap_directory'] = array(
      'title' => $t('XML sitemap cache directory'),
      'value' => $t('Writable'),
    );
    if (!xmlsitemap_check_directory()) {
      $requirements['xmlsitemap_directory']['value'] = $t('Not found or not writable');
      $requirements['xmlsitemap_directory']['severity'] = REQUIREMENT_ERROR;
      $requirements['xmlsitemap_directory']['description'] = $t('The directory %directory was not found or is not writable by the server. See <a href="@docpage">@docpage</a> for more information.', array(
        '%directory' => xmlsitemap_get_directory(),
        '@docpage' => 'http://drupal.org/node/34025',
      ));
    }
    else {
      $directories = xmlsitemap_check_all_directories();
      foreach ($directories as $directory => $writable) {
        if ($writable) {
          unset($directories[$directory]);
        }
      }
      if (!empty($directories)) {
        $requirements['xmlsitemap_directory']['value'] = $t('Not found or not writable');
        $requirements['xmlsitemap_directory']['severity'] = REQUIREMENT_ERROR;
        $requirements['xmlsitemap_directory']['description'] = $t('The following directories were not found or are not writable by the server. See <a href="@docpage">@docpage</a> for more information. !directories', array(
          '!directories' => theme('item_list', array_keys($directories)),
          '@docpage' => 'http://drupal.org/node/34025',
        ));
      }
    }

    // The maximum number of links in a sitemap.
    $max_links = db_result(db_query("SELECT MAX(links) FROM {xmlsitemap_sitemap}"));
    $max_links_limit = XMLSITEMAP_MAX_SITEMAP_LINKS * XMLSITEMAP_MAX_SITEMAP_LINKS;
    if ($max_links > $max_links_limit) {
      $requirements['xmlsitemap_link_count'] = array(
        'title' => $t('XML sitemap link count'),
        'value' => $max_links,
        'description' => $t('You have exceeded the number of links that your sitemap can contain (@num).', array(
          '@num' => number_format($max_links),
        )),
        'severity' => REQUIREMENT_ERROR,
      );
    }

    // The maximum number of chunks in a sitemap.
    $max_chunks = db_result(db_query("SELECT MAX(chunks) FROM {xmlsitemap_sitemap}"));
    if ($max_chunks > XMLSITEMAP_MAX_SITEMAP_LINKS) {
      $requirements['xmlsitemap_chunk_count'] = array(
        'title' => $t('XML sitemap page count'),
        'value' => $max_chunks,
        'description' => $t('You have exceeded the number of sitemap pages (@number).', array(
          '@number' => number_format(XMLSITEMAP_MAX_SITEMAP_LINKS),
        )),
        'severity' => REQUIREMENT_ERROR,
      );
      if (!in_array(xmlsitemap_get_chunk_size(), array(
        50000,
        'auto',
      ))) {
        $requirements['xmlsitemap_chunk_count']['description'] .= ' ' . t('Please increase the number of links per page.');
      }
    }

    // Check maximum file size.
    $max_filesize = db_result(db_query("SELECT MAX(max_filesize) FROM {xmlsitemap_sitemap}"));
    $requirements['xmlsitemap_file_size'] = array(
      'title' => $t('XML sitemap maximum file size'),
      'value' => format_size($max_filesize),
    );
    if ($max_filesize > XMLSITEMAP_MAX_SITEMAP_FILESIZE) {
      $requirements['xmlsitemap_file_size']['description'] = $t('You have exceeded the maximum sitemap file size of @size. If possible, decrease the number of links per sitemap page.', array(
        '@size' => format_size(XMLSITEMAP_MAX_SITEMAP_FILESIZE),
      ));
      $requirements['xmlsitemap_file_size']['severity'] = REQUIREMENT_ERROR;
    }
    elseif (!variable_get('xmlsitemap_developer_mode', 0)) {
      unset($requirements['xmlsitemap_file_size']);
    }

    // Check when the cached files were last generated.
    $generated_last = variable_get('xmlsitemap_generated_last', 0);
    $generated_ago = REQUEST_TIME - $generated_last;
    $requirements['xmlsitemap_generated'] = array(
      'title' => $t('XML sitemap'),
      'value' => $generated_last ? $t('Last attempted generation on !date (!interval ago).', array(
        '!date' => format_date($generated_last, 'small'),
        '!interval' => format_interval($generated_ago),
      )) : $t('Cached files have not been generated yet.'),
      'severity' => REQUIREMENT_OK,
    );
    if (variable_get('xmlsitemap_rebuild_needed', FALSE) && _xmlsitemap_rebuild_form_access()) {
      $requirements['xmlsitemap_generated']['severity'] = REQUIREMENT_ERROR;
      $requirements['xmlsitemap_generated']['description'] = $t('The XML sitemap data is out of sync and needs to be <a href="@link-rebuild">completely rebuilt</a>.', array(
        '@link-rebuild' => url('admin/settings/xmlsitemap/rebuild'),
      ));
    }
    elseif (variable_get('xmlsitemap_regenerate_needed', FALSE)) {
      if ($max_filesize == 0) {

        // A maximum sitemap file size of 0 indicates an error in generation.
        $requirements['xmlsitemap_generated']['severity'] = REQUIREMENT_ERROR;
      }
      elseif ($generated_ago >= variable_get('cron_threshold_error', 1209600)) {
        $requirements['xmlsitemap_generated']['severity'] = REQUIREMENT_ERROR;
      }
      elseif ($generated_ago >= variable_get('cron_threshold_warning', 172800)) {
        $requirements['xmlsitemap_generated']['severity'] = REQUIREMENT_WARNING;
      }
      if ($requirements['xmlsitemap_generated']['severity']) {
        $requirements['xmlsitemap_generated']['description'] = $t('The XML cached files are out of date and need to be regenerated. You can <a href="@link-cron">run cron manually</a> to regenerate the sitemap files.', array(
          '@link-cron' => url('admin/reports/status/run-cron', array(
            'query' => drupal_get_destination(),
          )),
        ));
      }
    }
  }
  return $requirements;
}