Skip to content

Instantly share code, notes, and snippets.

@jonhattan
Last active August 29, 2015 14:11
Show Gist options
  • Save jonhattan/5384246f05cbb114dd65 to your computer and use it in GitHub Desktop.
Save jonhattan/5384246f05cbb114dd65 to your computer and use it in GitHub Desktop.
Sql to obtain views using php. #drupal
;; Select views using php
SELECT vd.vid, name, id FROM views_display vd LEFT JOIN views_view vv ON vd.vid=vv.vid WHERE display_options LIKE '%php%';
;; View php snippets
mysql DBNAME -e "SELECT vd.vid, name, id, display_options FROM views_display vd LEFT JOIN views_view vv ON vd.vid=vv.vid WHERE display_options LIKE '%php%' \G" | less
@marcoscano
Copy link

<?php

/* 
 * @file
 * A custom command to find PHP code in the database.
 * 
 * Put this file (find_php_db.drush.inc) in your .drush global directory, or
 * any other path discoverable by drush in order to have the command included.
 * 
 * Usage: From inside the web docroot, execute "drush find-php-db".
 */

/**
 * Implements hook_drush_command().
 */
function find_php_db_drush_command() {
  return array(
    'find-php-db' => array(
      'description' => 'Look for php code stored in the current database.',
    ),
  );
}

/**
 * Drush command callback to find PHP code stored in the database.
 */
function drush_find_php_db() {
  drush_print('===========================================================');
  drush_log('Looking up for nodes with PHP.', 'ok');
  $types = node_type_get_types();
  $nids_array = array();
  foreach ($types as $type) {
    $fields = field_info_instances("node", $type->orig_type);
    foreach ($fields as $field) {
      $found = FALSE;
      $schema = db_query('SHOW COLUMNS FROM  field_data_'. $field['field_name']);
      foreach ($schema as $colum) {
        if ($colum->Field == $field['field_name'] . "_format") {
          $found = TRUE;
        }
      }
      if ($found == TRUE) {
        $query = "SELECT * from field_data_". $field['field_name'] . " WHERE ". $field['field_name'] . "_format LIKE '%php%'";
        $result = db_query($query);
        foreach ($result as $row){
          $node_php = TRUE;
          $nids_array[] = $row->entity_id;
        }
      }
    }
  }
  if (!empty($node_php)) {
    foreach (array_unique($nids_array) as $nid) {
      drush_log('Found PHP in node with NID: ' . $nid, 'warning');
    }
  }
  else {
    drush_log('No PHP found in nodes.', 'ok');
  }

  if (module_exists('views')) {
    drush_print('===========================================================');
    drush_log('Looking up for views with PHP.', 'ok');
    $query = "SELECT vd.vid, name, id FROM views_display vd LEFT JOIN views_view vv ON vd.vid=vv.vid WHERE display_options LIKE '%php%'";
    $result = db_query($query);
    foreach ($result as $row){
      $php_views = TRUE;
      drush_log('Found PHP in view ' . $row->name . ' (VID: ' . $row->vid . '), display: ' . $row->id . '.', 'warning');
    }
    if (empty($php_views)) {
      drush_log('No PHP found in views.', 'ok');
    }
  }

  if (module_exists('block')) {
    drush_print('===========================================================');
    drush_log('Looking up vor blocks with PHP.', 'ok');
    $query = "SELECT bid, info FROM block_custom WHERE format LIKE '%php%'";
    $result = db_query($query);
    foreach ($result as $row){
      $php_block = TRUE;
      drush_log('Found PHP in block "' . $row->info . '" (BID: ' . $row->bid . ').', 'warning');
    }
    if (empty($php_block)) {
      drush_log('No PHP found in blocks.', 'ok');
    }
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment