Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EWPP-2210: Optimize performance for sanitize query. #53

Merged
merged 1 commit into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion oe_contact_forms.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ services:
- { name: access_check, applies_to: _oe_contact_forms_access_check }
oe_contact_forms.contact.sanitize_commands:
class: Drupal\oe_contact_forms\Commands\sql\ContactFormSanitizeCommand
arguments: ['@entity_type.manager']
arguments: ['@entity_type.manager', '@database']
tags:
- { name: drush.command }
55 changes: 42 additions & 13 deletions src/Commands/sql/ContactFormSanitizeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\oe_contact_forms\Commands\sql;

use Drupal\Core\Database\Connection;
use Drush\Commands\DrushCommands;
use Drush\Drupal\Commands\sql\SanitizePluginInterface;
use Consolidation\AnnotatedCommand\CommandData;
Expand All @@ -22,15 +23,25 @@ class ContactFormSanitizeCommand extends DrushCommands implements SanitizePlugin
*/
protected $entityTypeManager;

/**
* The database connection to use.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;

/**
* SanitizeContactFormFieldsCommands constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\Database\Connection $connection
* The database connection to use.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
public function __construct(EntityTypeManagerInterface $entityTypeManager, Connection $connection) {
parent::__construct();
$this->entityTypeManager = $entityTypeManager;
$this->connection = $connection;
}

/**
Expand All @@ -47,18 +58,36 @@ public function sanitize($result, CommandData $commandData) {
foreach ($contact_forms as $key => $contact_form) {
$is_corporate_form = $contact_form->getThirdPartySetting('oe_contact_forms', 'is_corporate_form', FALSE);
if ($is_corporate_form) {
$messages = $this->entityTypeManager->getStorage('contact_message')->loadByProperties(['contact_form' => $contact_form->id()]);
foreach ($messages as $message) {
$message->set('name', 'User' . $message->id());
$message->set('mail', 'user+' . $message->id() . '@example.com');
$message->set('subject', 'subject-by-' . $message->id());
$message->set('message', 'message-by-' . $message->id());
$message->set('oe_country_residence', 'residence-in-' . $message->id());
$message->set('oe_telephone', '+000-' . $message->id());
$message->set('oe_topic', 'topic-' . $message->id());
$message->set('ip_address', '127.0.0.1');
$message->save();
}
$this->connection->update('contact_message')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works for the site's contact forms but personal contact forms are left unsanitized.
Screenshot 2022-05-03 at 13 15 21

->expression('name', 'CONCAT(:name_dummy_string, id)', [
':name_dummy_string' => 'User',
])
->expression('mail', 'CONCAT(:mail_dummy_string, id, :maildomain_dummy_string)', [
':mail_dummy_string' => 'user+',
':maildomain_dummy_string' => '@example.com',
])
->expression('subject', 'CONCAT(:subject_dummy_string, id)', [
':subject_dummy_string' => 'subject-by-',
])
->expression('message', 'CONCAT(:message_dummy_string, id)', [
':message_dummy_string' => 'message-by-',
])
->expression('oe_country_residence', 'CONCAT(:residence_dummy_string, id)', [
':residence_dummy_string' => 'residence-in-',
])
->expression('oe_telephone', 'CONCAT(:telephone_dummy_string, id)', [
':telephone_dummy_string' => '+000-',
])
->expression('oe_topic', 'CONCAT(:topic_dummy_string, id)', [
':topic_dummy_string' => 'topic-',
])
->fields(['ip_address' => '127.0.0.1'])
->condition('contact_form', $contact_form->id())
->execute();

// Make sure that we don't have sensitive data of contact messages
// in the cache.
$this->entityTypeManager->getStorage('contact_message')->resetCache();

$topics = $contact_form->getThirdPartySetting('oe_contact_forms', 'topics', []);
foreach ($topics as $key => $topic) {
Expand Down