ImageMissingStyleSensorPlugin.php in Monitoring 8
File
src/Plugin/monitoring/SensorPlugin/ImageMissingStyleSensorPlugin.php
View source
<?php
namespace Drupal\monitoring\Plugin\monitoring\SensorPlugin;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\file\Entity\File;
use Drupal\monitoring\Result\SensorResultInterface;
use RecursiveArrayIterator;
use RecursiveIteratorIterator;
class ImageMissingStyleSensorPlugin extends WatchdogAggregatorSensorPlugin {
protected $configurableConditions = FALSE;
protected $configurableVerboseOutput = FALSE;
public function getAggregateQuery() {
$query = parent::getAggregateQuery();
$query
->addField('watchdog', 'variables');
$query
->groupBy('variables');
$query
->orderBy('records_count', 'DESC');
return $query;
}
public function runSensor(SensorResultInterface $result) {
parent::runSensor($result);
if (!empty($this->fetchedObject)) {
$variables = unserialize($this->fetchedObject->variables);
if (isset($variables['%source_image_path'])) {
$result
->addStatusMessage($variables['%source_image_path']);
}
}
}
public function resultVerbose(SensorResultInterface $result) {
$output = parent::resultVerbose($result);
$this
->verboseResultCounting($output);
return $output;
}
public function verboseResultCounting(array &$output) {
if ($this->sensorConfig
->getSetting('verbose_fields')) {
$query = $this
->getAggregateQuery();
$query
->addExpression('MAX(timestamp)', 'timestamp');
$query_result = $query
->range(0, 20)
->execute();
$this->queryString = $query_result
->getQueryString();
$rows = $this
->buildTableRows($query_result
->fetchAll());
$results = [];
foreach ($rows as $key => $row) {
$results[$key] = [];
$variables = unserialize($row['variables']);
$results[$key]['file'] = $variables['%source_image_path'];
$results[$key]['count'] = $row['records_count'];
$file = \Drupal::entityQuery('file')
->condition('uri', $variables['%source_image_path'])
->execute();
if (!empty($file)) {
$file = File::load(array_shift($file));
$list_usages = \Drupal::service('file.usage')
->listUsage($file);
$usages = 0;
foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($list_usages)) as $sub) {
$usages += (int) $sub;
}
$results[$key]['usages'] = Link::fromTextAndUrl(\Drupal::translation()
->formatPlural($usages, '1 place', '@count places'), Url::fromUserInput('/admin/content/files/usage/' . $file
->id()));
}
else {
$results[$key]['usages'] = [
'#markup' => '',
];
}
$results[$key]['timestamp'] = \Drupal::service('date.formatter')
->format($row['timestamp'], 'short');
}
$output['aggregated_result'] = array(
'#type' => 'verbose_table_result',
'#title' => t('Aggregated result'),
'#header' => $this
->buildTableHeader($results),
'#rows' => $results,
'#query' => $query_result
->getQueryString(),
'#query_args' => $query
->getArguments(),
);
}
return $output;
}
public function verboseResultUnaggregated(array &$output) {
parent::verboseResultUnaggregated($output);
foreach ($output['verbose_sensor_result']['#rows'] as $key => $row) {
$message = $row['message'];
$tmp_str = substr($message
->jsonSerialize(), strpos($message
->jsonSerialize(), '>') + 1);
$output['verbose_sensor_result']['#rows'][$key]['path'] = substr($tmp_str, 0, strpos($tmp_str, '<'));
unset($output['verbose_sensor_result']['#rows'][$key]['message']);
unset($output['verbose_sensor_result']['#rows'][$key]['timestamp']);
$output['verbose_sensor_result']['#rows'][$key]['timestamp'] = $row['timestamp'];
}
$output['verbose_sensor_result']['#header']['path'] = 'image path';
unset($output['verbose_sensor_result']['#header']['message']);
unset($output['verbose_sensor_result']['#header']['timestamp']);
$output['verbose_sensor_result']['#header']['timestamp'] = 'timestamp';
}
}