2 namespace Drush\Drupal\Commands\sql;
4 use Consolidation\AnnotatedCommand\CommandData;
5 use Drupal\Core\Database\Database;
6 use Drush\Commands\DrushCommands;
8 use Symfony\Component\Console\Input\InputInterface;
11 * This class is a good example of a sql-sanitize plugin.
13 class SanitizeCommentsCommands extends DrushCommands implements SanitizePluginInterface
16 protected $moduleHandler;
19 * SanitizeCommentsCommands constructor.
21 * @param $moduleHandler
23 public function __construct($database, $moduleHandler)
25 $this->database = $database;
26 $this->moduleHandler = $moduleHandler;
30 * Sanitize comment names from the DB.
32 * @hook post-command sql-sanitize
36 public function sanitize($result, CommandData $commandData)
38 if ($this->applies()) {
40 $this->database->update('comment_field_data')
42 'name' => 'Anonymous',
44 'homepage' => 'http://example.com'
50 $this->database->update('comment_field_data')
51 ->expression('name', "CONCAT('User', `uid`)")
52 ->expression('mail', "CONCAT('user+', `uid`, '@example.com')")
53 ->fields(['homepage' => 'http://example.com'])
54 ->condition('uid', 1, '>=')
56 $this->logger()->success(dt('Comment display names and emails removed.'));
61 * @hook on-event sql-sanitize-confirms
65 public function messages(&$messages, InputInterface $input)
67 if ($this->applies()) {
68 $messages[] = dt('Remove comment display names and emails.');
72 protected function applies()
74 Drush::bootstrapManager()->doBootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL);
75 return $this->moduleHandler->moduleExists('comment');