You are here

function dba_run_script_submit in Database Administration 5

File

./dba.module, line 864
Allows administrators direct access to their Drupal database. Written by Jeremy Andrews <jeremy@kerneltrap.org>, June 2004. PostgreSQL functionality provided by AAM <aam@ugpl.de> Major security audit, porting, and maintenance by Derek…

Code

function dba_run_script_submit($form_id, $form_values) {
  if ($file = file_save_upload('script_filename')) {

    // File is now in temporary directory.
    if (file_exists($file->filepath)) {
      if ($fp = fopen($file->filepath, 'r')) {
        $query = NULL;
        $count = 0;
        while (!feof($fp)) {
          $line = fgets($fp, 8192);
          if ($line && strncmp($line, '--', 2) && strncmp($line, '#', 1)) {
            $query .= $line;
            if (strpos($line, ';')) {
              if (db_query($query, FALSE)) {
                if ($form_values['verbose']) {
                  drupal_set_message(check_plain($query));
                }
                $count++;
              }
              else {
                drupal_set_message(t('Query failed: %query', array(
                  '%query' => $query,
                )), 'error');
              }
              $query = NULL;
            }
          }
        }
        fclose($fp);
        drupal_set_message(t('Succesfully ran !query from script %filename.', array(
          '!query' => format_plural($count, '1 query', '@count queries'),
          '%filename' => $file->filename,
        )));
      }
      else {
        drupal_set_message(t('Unable to open script %filename.', array(
          '%filename' => $file->filename,
        )), 'error');
      }
      file_delete($file->filepath);
    }
    else {
      drupal_set_message(t('Script %filename does not exist.', array(
        '%filename' => $file->filename,
      )), 'error');
    }

    // Cleanup session.
    unset($_SESSION['file_uploads'][$file->source]);
  }
}