You are here

variablecheck.module in Variable Check 6

Same filename and directory in other branches
  1. 7 variablecheck.module

File

variablecheck.module
View source
<?php

/**
 * Implementation of hook_perm()
 */
function variablecheck_perm() {
  return array(
    'check system variables',
  );
}

/**
 * Implementation of hook_menu().
 */
function variablecheck_menu() {
  $items['admin/reports/variablecheck'] = array(
    'title' => 'Check System Variables',
    'description' => 'Check the validity of settings in the variable table.',
    'page callback' => 'variablecheck_check_variables_page',
    'access arguments' => array(
      'check system variables',
    ),
  );
  return $items;
}

/**
 * Implementation of hook_requirements()
 */
function variablecheck_requirements($phase) {
  $problem = count(variablecheck_check_variables());
  $requirements = array();
  $requirements['valiablecheck'] = array(
    'title' => 'Variables',
    'value' => $problem ? format_plural($problem, 'Found one invalid entry', 'Found @count invalid entries') : t('No problem'),
    'description' => $problem ? t('The variable table contains an invalid entry. Please check the <a href="@url">variable report</a> for more information.', array(
      '@url' => url('admin/reports/variablecheck'),
    )) : '',
    'severity' => $problem ? REQUIREMENT_WARNING : REQUIREMENT_OK,
  );
  return $requirements;
}

/**
 * Helper that returs a list of invalid variables and their values.
 */
function variablecheck_check_variables() {
  $rows = null;
  $result = db_query("SELECT name, value FROM {variable} ORDER BY name ASC");

  // Test each entry for validity.
  while ($entry = db_fetch_object($result)) {

    // Suppress the notice.
    $value = @unserialize($entry->value);

    // If the unserialize call returned FALSE and the stored value is NOT a
    // boolean false, list it in the report.
    if ($value === FALSE && $entry->value != serialize(FALSE)) {
      $row = array(
        $entry->name,
        $entry->value,
      );
      $rows[] = $row;
    }
  }
  return $rows;
}

/**
 * Helper that tries to display all variables that fail to
 * unserialize, causing ugly Notices on production sites.
 */
function variablecheck_check_variables_page() {

  // Load the naughty variables.
  $header = array(
    t('Name'),
    t('Value'),
  );
  $rows = variablecheck_check_variables();
  drupal_set_title(t('Invalid Variables'));
  if ($count = count($rows)) {
    drupal_set_message(format_plural($count, 'Found one invalid entry', 'Found @count invalid entries'), 'error');
    $output = format_plural($count, 'You will want to either remove this entry or let the module developer know there is a problem with their module.', 'You will want to either remove these entries or let the module developer know there is a problem with their module.');
    $output .= theme('table', $header, $rows);
  }
  else {
    $output = t('No invalid entries were found in the variable table.');
  }
  return $output;
}

Functions

Namesort descending Description
variablecheck_check_variables Helper that returs a list of invalid variables and their values.
variablecheck_check_variables_page Helper that tries to display all variables that fail to unserialize, causing ugly Notices on production sites.
variablecheck_menu Implementation of hook_menu().
variablecheck_perm Implementation of hook_perm()
variablecheck_requirements Implementation of hook_requirements()