Completed
Push — 8.x-1.x ( c8b5ba...4aaba9 )
by Frédéric G.
01:42
created

qa.module (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * @file
5
 * OSInet Quality Assurance module for Drupal.
6
 *
7
 * @copyright Copyright (C) 2005-2018 Frederic G. MARAND for Ouest Systèmes Informatiques (OSInet)
8
 *
9
 * @since DRUPAL-4-6
10
 *
11
 * @license Licensed under the disjunction of the CeCILL, version 2 and General Public License version 2 and later
12
 *
13
 * License note: QA is distributed by OSInet to its customers under the
14
 * CeCILL 2.0 license. OSInet support services only apply to the module
15
 * when distributed by OSInet, not by any third-party further down the
16
 * distribution chain.
17
 *
18
 * If you obtained QA from drupal.org, that site received it under the
19
 * GPLv2 license and can therefore distribute it under the GPLv2, and
20
 * so can you and just anyone down the chain as long as the GPLv2 terms
21
 * are abided by, the module distributor in that case being the
22
 * drupal.org organization or the downstream distributor, not OSInet.
23
 */
24
use Drupal\qa\Variable\Variable;
25
26
/**
27
 * Page callback for qa/dependencies
28
 *
29
 * TODO convert to native Image_GraphViz to remove dependency on graphviz_filter
30
 * XXX convert to Grafizzi to remove dependency on Image_GraphViz
31
 *
32
 * @return string
33
 */
34
function qa_page_dependencies() {
35
  $G = qa_dependencies();
36
  // passed by reference: cannot pass a function return
37
  return graphviz_filter_render($G);
38
}
39
40
/**
41
 * Batch conclusion callback.
42
 *
43
 * @param boolean $success
44
 * @param array $results
45
 * @param array $operations
46
 */
47
function qa_report_finished($success, $results, $operations) {
0 ignored issues
show
The parameter $operations is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
  unset($results['#message']);
49
  if ($success) {
50
    $message = format_plural(count($results), 'One control pass ran.', '@count control passes ran.');
51
  }
52
  else {
53
    $message = t('Finished with an error.');
54
  }
55
  drupal_set_message($message);
56
  $_SESSION['qa_results'] = $results;
57
  drupal_goto('admin/reports/qa/results');
58
}
59
60
/**
61
 * Results page for QA Controls batch.
62
 *
63
 * @link http://www.php.net/manual/fr/function.unserialize.php @endlink
64
 */
65
function qa_report_results() {
66
  if (empty($_SESSION['qa_results'])) {
67
    drupal_goto('admin/reports/qa');
68
  }
69
  // Work around incomplete classes
70
  $results = unserialize(serialize($_SESSION['qa_results']));
71
72
  $header = array(
73
    t('Control'),
74
    t('Status'),
75
    t('Results'),
76
  );
77
  $data = array();
78
  foreach ($results as $pass) {
79
    $control = $pass->control;
80
    $data[] = array(
81
      $control->title,
82
      $pass->status
83
        ? theme('image', array(
84
          'path' => 'misc/watchdog-ok.png',
85
          'alt' => t('OK'),
86
          ))
87
        : theme('image', array(
88
          'path' => 'misc/watchdog-error.png',
89
          'alt' => t('Error'),
90
          )),
91
      $pass->result,
92
    );
93
  }
94
  $ret = theme('table', [
95
    'header' => $header,
96
    'rows' => $data,
97
    'attributes' => [
98
      'id' => 'qa-results',
99
    ],
100
    '#attached' => ['library' => ['qa/results']],
101
  ]);
102
  // unset($_SESSION['qa_results']);
103
  return $ret;
104
}
105
106
/**
107
 * Form builder for QA packages/controls selection form.
108
 *
109
 * @return array
110
 */
111
function qa_report_form($form, $form_state) {
0 ignored issues
show
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $form_state is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
112
  $form = array();
113
  $base = drupal_get_path('module', 'qa');
114
  $packages = Exportable::getClasses($base, 'Drupal\qa\BasePackage');
115
  ksort($packages);
116
  foreach ($packages as $package_name => $package) {
117
    $collapsed = TRUE;
118
    $form[$package_name] = array(
119
      '#type' => 'fieldset',
120
      '#title' => filter_xss_admin($package->title),
121
      '#description' => filter_xss_admin($package->description),
122
      '#collapsible' => TRUE,
123
    );
124
    $controls = $package->getClasses($package->dir, 'Drupal\qa\Plugin\Qa\Control\BaseControl');
125
126
    foreach ($controls as $control_name => $control) {
127
      $default_value = isset($_SESSION[$control_name])
128
        ? $_SESSION[$control_name]
129
        : NULL;
130
      if ($default_value) {
131
        $collapsed = FALSE;
132
      }
133
134
      $deps = array();
135
      $met = TRUE;
136
      foreach ($control->getDependencies() as $dep_name) {
137
        if (module_exists($dep_name)) {
138
          $deps[] = t('@module (<span class="admin-enabled">available</span>)', array('@module' => $dep_name));
139
        }
140
        else {
141
          $deps[] = t('@module (<span class="admin-disabled">unavailable</span>)', array('@module' => $dep_name));
142
          $met = FALSE;
143
        }
144
      }
145
      $form[$package_name][$control_name] = array(
146
        '#type'          => 'checkbox',
147
        '#default_value' => $met ? $default_value : 0,
148
        '#title'         => filter_xss_admin($control->title),
149
        '#description'   => filter_xss_admin($control->description),
150
        '#disabled'      => !$met,
151
      );
152
      $form[$package_name][$control_name .'-dependencies'] = array(
153
        '#value' => t('Depends on: !dependencies', array(
154
          '!dependencies' => implode(', ', $deps),
155
          )),
156
        '#prefix' => '<div class="admin-dependencies">',
157
        '#suffix' => '</div>',
158
      );
159
    }
160
    $form[$package_name]['#collapsed'] = $collapsed;
161
  }
162
163
  $form['submit'] = [
164
    '#type'  => 'submit',
165
    '#value' => t('Run controls'),
166
  ];
167
168
  return $form;
169
}
170
171
/**
172
 * Submit handler for QA packages/controls selection form
173
 *
174
 * @param array $form
175
 * @param array $form_state
176
 */
177
function qa_report_form_submit($form, &$form_state) {
0 ignored issues
show
The parameter $form is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
178
  $controls = array();
179
  foreach ($form_state['values'] as $item => $value) {
180
    if (class_exists($item) && is_subclass_of($item, '\Drupal\qa\Plugin\Qa\Control\BaseControl')) {
0 ignored issues
show
Due to PHP Bug #53727, is_subclass_of returns inconsistent results on some PHP versions for interfaces; you could instead use ReflectionClass::implementsInterface.
Loading history...
181
      if ($value) {
182
        $controls[$item] = $value;
183
      }
184
      $_SESSION[$item] = $value;
185
    }
186
    elseif ($value == 1) {
187
      $args = array(
188
        '%control' => $item,
189
      );
190
      drupal_set_message(t('Requested invalid control %control', $args), 'error');
191
      watchdog('qa', 'Requested invalid control %control', $args, WATCHDOG_ERROR);
192
    }
193
  }
194
195
  drupal_set_message(t('Prepare to run these controls: @controls', array(
196
    '@controls' => implode(', ', array_keys($controls)))), 'status');
197
  $batch = array(
198
    'operations'       => array(),
199
    'title'            => t('QA Controls running'),
200
    'init_message'     => t('QA Controls initializing'),
201
    // 'progress_message' => t('current: @current, Remaining: @remaining, Total: @total'),
202
    'error_message'    => t('Error in QA Control'),
203
    'finished'         => 'qa_report_finished',
204
    // 'file'             => '', // only if outside module file
205
  );
206
207
  foreach ($controls as $item => $value) {
208
    $batch['operations'][] = array('qa_report_run_pass', array($item));
209
  }
210
  batch_set($batch);
211
}
212
213
/**
214
 * Batch progress step.
215
 *
216
 * @return void
217
 */
218
function qa_report_run_pass($class_name, &$context) {
219
  $name_arg = array('@class' => $class_name);
220
221
  $control = new $class_name();
222
  if (!is_object($control)) {
223
    drupal_set_message(t('Cannot obtain an instance for @class', $name_arg), 'error');
224
    $context['results']['#message'] = t('Control @class failed to run.', $name_arg);
225
    $context['message'] = t('Control @class failed to run.', $name_arg);
226
    $context['results'][$class_name] = 'wow';
227
  }
228
  else {
229
    drupal_set_message(t('Running a control instance for @class', $name_arg), 'status');
230
    $pass = $control->run();
231
    if (!$pass->status) {
232
      $context['success'] = FALSE;
233
    }
234
    $context['results']['#message'][] = t('Control @class ran', $name_arg);
235
    $context['message'] = theme('item_list', $context['results']['#message']);
236
    $context['results'][$class_name] = $pass;
237
  }
238
}
239
240
function qa_variable_load($name) {
241
  $variable = new Variable($name);
242
  if (!$variable->is_set) {
243
    return FALSE;
244
  }
245
246
  return $variable;
247
}
248