Commit a6c7b184 authored by Adam Courtemanche's avatar Adam Courtemanche
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

README.txt

0 → 100644
+26 −0
Original line number Diff line number Diff line
CONTENTS OF THIS FILE
---------------------

 * Introduction
 * Installation
 * Usage
 * API
 
INTRODUCTION
------------
This simple module provides a report showing where (in which entities) the PHP code format (from the core PHP filter module) is used.

INSTALLATION
------------
Install like you would any Drupal 7 module.

There are permissions for this module so you can extend viewing rights to specific roles.

USAGE
-----
View the report at <strong>Admin » Reports » PHP filter usage</strong>.

API
---
By default this searches the revision table for each text field, as well as the <em>block_custom</em> table's <em>format</em> field.
You can use <em>hook_phpfilter_usage_report_fields_alter()</em> to query additional database fields.
+5 −0
Original line number Diff line number Diff line
name = PHP Filter Usage Report
description = Provides a report showing where the PHP code format (from PHP Filter) is used
package = Fields
core = 7.x
files[] = phpfilter_usage_report.module
+131 −0
Original line number Diff line number Diff line
<?php
 
/**
 * @file
 * Provides a report showing where the PHP code format (from PHP Filter) is used
 */

/**
 * Implements hook_permission().
 */
function phpfilter_usage_report_permission() {
  return array(
    'view phpfilter_usage_report' => array(
      'title' => t('View phpfilter_usage_report'),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Implements hook_menu().
 */
function phpfilter_usage_report_menu() {
  $items = array();

  $items['admin/reports/php-filter-usage'] = array(
    'title' => 'PHP filter usage',
    'description' => 'Show entities where "PHP Code" format is used',
    'access arguments' => array('view phpfilter_usage_report'),
    'page callback' => 'phpfilter_usage_report_main',
  );

  return $items;
}

/**
 * Page callback for the main page
 *
 * @return string
 *   HTML output
 */
function phpfilter_usage_report_main() {
  $output = phpfilter_usage_report_find_entities();
  return $output;
}

/**
 * Returns an array of all fields in the database that may allow the php_code format
 *
 * @return array
 */
function phpfilter_usage_report_find_fields() {
  $tables = drupal_get_schema();

  // First, define some known tables with format fields
  $fields = array(
    'block_custom' => 'format',
  );

  foreach ($tables as $table_name => $table) {
    if (strpos($table_name, 'field_revision') === 0) {
      foreach ($table['fields'] as $field_name => $field) {
        $pos = strpos($field_name, '_format');
        if ($pos !== FALSE && strlen($field_name) - 7 == $pos) {
          $fields[$table_name] = $field_name;
        }
      }
    }
  }

  // Use hook_phpfilter_usage_report_fields_alter() to add additional
  // fields (each element as table => field) or remove detected fields
  drupal_alter('phpfilter_usage_report_fields', $fields);

  return $fields;
}

/**
 * Returns all records that use php_code as the text format in any of their fields
 *
 * @return array
 *   All database rows where php_code is the field format value
 */
function phpfilter_usage_report_find_dbrows() {
  $fields = phpfilter_usage_report_find_fields();

  $all_records = array();

  foreach ($fields as $table => $field) {
    $query = db_select($table, 't')
      ->condition('t.' . $field, 'php_code')
      ->fields('t');

    $result = $query->execute();
    while($record = $result->fetchAssoc()) {
      $all_records[] = $record;
    }
  }

  return $all_records;
}

function phpfilter_usage_report_find_entities() {
  $db_rows = phpfilter_usage_report_find_dbrows();

  $table_header = array('Entity type', 'Bundle', 'Deleted', 'Entity ID', 'Revision ID', 'Value');

  $table_rows = '';
  foreach ($db_rows as $row) {
    // This probably isn't the safest/cleanest way to find the value for the field
    $field_value = '';
    foreach ($row as $field_name => $value) {
      $pos = strpos($field_name, '_value');
      if ($pos !== FALSE && strlen($field_name) - 6 == $pos) {
        $field_value = $value;
      }
    }

    // By using the entity_id as a key, we should end up with the last revision for each entity
    $table_rows[$row['entity_id']] = array(
      $row['entity_type'],
      $row['bundle'],
      $row['deleted'],
      $row['entity_id'],
      $row['revision_id'],
      '<pre>' . htmlspecialchars($field_value) . '</pre>',
    );
  }

  return theme('table', array('header' => $table_header, 'rows' => $table_rows));
}