6e1344c857358608b9179444e3a31e9bbdf1c740
[yaffs-website] / vendor / drush / drush / src / Drupal / Commands / sql / SanitizeCommentsCommands.php
1 <?php
2 namespace Drush\Drupal\Commands\sql;
3
4 use Consolidation\AnnotatedCommand\CommandData;
5 use Drupal\Core\Database\Database;
6 use Drush\Commands\DrushCommands;
7 use Drush\Drush;
8 use Symfony\Component\Console\Input\InputInterface;
9
10 /**
11  * This class is a good example of a sql-sanitize plugin.
12  */
13 class SanitizeCommentsCommands extends DrushCommands implements SanitizePluginInterface
14 {
15     protected $database;
16     protected $moduleHandler;
17
18     /**
19      * SanitizeCommentsCommands constructor.
20      * @param $database
21      * @param $moduleHandler
22      */
23     public function __construct($database, $moduleHandler)
24     {
25         $this->database = $database;
26         $this->moduleHandler = $moduleHandler;
27     }
28
29     /**
30      * Sanitize comment names from the DB.
31      *
32      * @hook post-command sql-sanitize
33      *
34      * @inheritdoc
35      */
36     public function sanitize($result, CommandData $commandData)
37     {
38         if ($this->applies()) {
39             //Update anon.
40             $this->database->update('comment_field_data')
41             ->fields([
42               'name' => 'Anonymous',
43               'mail' => '',
44               'homepage' => 'http://example.com'
45             ])
46               ->condition('uid', 0)
47               ->execute();
48
49             // Update auth.
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, '>=')
55               ->execute();
56             $this->logger()->success(dt('Comment display names and emails removed.'));
57         }
58     }
59
60     /**
61      * @hook on-event sql-sanitize-confirms
62      *
63      * @inheritdoc
64      */
65     public function messages(&$messages, InputInterface $input)
66     {
67         if ($this->applies()) {
68             $messages[] = dt('Remove comment display names and emails.');
69         }
70     }
71
72     protected function applies()
73     {
74         Drush::bootstrapManager()->doBootstrap(DRUSH_BOOTSTRAP_DRUPAL_FULL);
75         return $this->moduleHandler->moduleExists('comment');
76     }
77 }