View source
<?php
namespace Drupal\scanner\Plugin\Scanner;
use Drupal\scanner\Plugin\ScannerPluginBase;
use Drupal\Component\Plugin\Exception\PluginException;
class Entity extends ScannerPluginBase {
protected $scannerRegexChars = '.\\/+*?[^]$() {}=!<>|:';
public function search($field, array $values) {
$data = [];
list($entityType) = explode(':', $field);
try {
$plugin = $this->scannerManager
->createInstance("scanner_{$entityType}");
if (empty($plugin)) {
throw new PluginException($this
->t('Unable to load entity type @entity_type.', [
'@entity_type' => $entityType,
]));
}
} catch (PluginException $e) {
\Drupal::logger('scanner')
->error($e
->getMessage());
\Drupal::messenger()
->addError($this
->t('An error occured @e:', [
'@e' => $e
->getMessage(),
]));
}
$results = $plugin
->search($field, $values);
if (!empty($results)) {
$data = $results;
}
return $data;
}
public function replace($field, array $values, array $undo_data) {
$data = [];
list($entityType) = explode(':', $field);
try {
$plugin = $this->scannerManager
->createInstance("scanner_{$entityType}");
} catch (PluginException $e) {
\Drupal::logger('scanner')
->error($e
->getMessage());
\Drupal::messenger()
->addError('An error occured: ' . $e
->getMessage());
}
$results = $plugin
->replace($field, $values, $undo_data);
if (!empty($results)) {
$data = $results;
}
return $data;
}
public function undo(array $data) {
foreach ($data as $key => $value) {
list($entityType) = explode(':', $key);
try {
$plugin = $this->scannerManager
->createInstance("scanner_{$entityType}");
$plugin
->undo($value);
} catch (PluginException $e) {
\Drupal::logger('scanner')
->error($e
->getMessage());
\Drupal::messenger()
->addError('An error occured: ' . $e
->getMessage());
}
}
}
protected function buildCondition($search, $mode, $wholeword, $regex, $preceded, $followed) {
$preceded_php = '';
if (!empty($preceded)) {
if (!$regex) {
$preceded = addcslashes($preceded, $this->scanerRegexChars);
}
$preceded_php = '(?<=' . $preceded . ')';
}
$followed_php = '';
if (!empty($followed)) {
if (!$followed) {
$followed = addcslashes($followed, $this->scanerRegexChars);
}
$followed_php = '(?=' . $followed . ')';
}
if ($wholeword && $regex) {
$value = "[[:<:]]" . $preceded . $search . $followed . "[[:>:]]";
$operator = 'REGEXP';
$phpRegex = '/\\b' . $preceded_php . $search . $followed_php . '\\b/';
}
elseif ($wholeword && !$regex) {
$value = '[[:<:]]' . $preceded . addcslashes($search, $this->scannerRegexChars) . $followed . '[[:>:]]';
$operator = 'REGEXP';
$phpRegex = '/\\b' . $preceded_php . addcslashes($search, $this->scannerRegexChars) . $followed . '\\b/';
}
elseif (!$wholeword && $regex) {
$value = $preceded . $search . $followed;
$operator = 'REGEXP';
$phpRegex = '/' . $preceded_php . $search . $followed_php . '/';
}
else {
$value = '%' . $preceded . addcslashes($search, $this->scannerRegexChars) . $followed . '%';
$operator = 'LIKE';
$phpRegex = '/' . $preceded . addcslashes($search, $this->scannerRegexChars) . $followed . '/';
}
if ($mode) {
return [
'condition' => $value,
'operator' => $operator . ' BINARY',
'phpRegex' => $phpRegex,
];
}
else {
return [
'condition' => $value,
'operator' => $operator,
'phpRegex' => $phpRegex . 'i',
];
}
}
}