Issue #1977254 by cbiggins, greyrhino, ParisLiakos: Convert dblog_overview() to a Controller.

8.0.x
Alex Pott 2013-05-19 13:05:02 -07:00
parent 518c4d10bd
commit 340a4342fd
5 changed files with 244 additions and 117 deletions

View File

@ -5,102 +5,6 @@
* Administrative page callbacks for the Database Logging module.
*/
/**
* Page callback: Displays a listing of database log messages.
*
* Messages are truncated at 56 chars. Full-length messages can be viewed on the
* message details page.
*
* @see dblog_clear_log_form()
* @see dblog_event()
* @see dblog_filter_form()
* @see dblog_menu()
*/
function dblog_overview() {
$filter = dblog_build_filter_query();
$rows = array();
$classes = array(
WATCHDOG_DEBUG => 'dblog-debug',
WATCHDOG_INFO => 'dblog-info',
WATCHDOG_NOTICE => 'dblog-notice',
WATCHDOG_WARNING => 'dblog-warning',
WATCHDOG_ERROR => 'dblog-error',
WATCHDOG_CRITICAL => 'dblog-critical',
WATCHDOG_ALERT => 'dblog-alert',
WATCHDOG_EMERGENCY => 'dblog-emergency',
);
$build['dblog_filter_form'] = drupal_get_form('dblog_filter_form');
$build['dblog_clear_log_form'] = drupal_get_form('dblog_clear_log_form');
$header = array(
'', // Icon column.
array('data' => t('Type'), 'field' => 'w.type', 'class' => array(RESPONSIVE_PRIORITY_MEDIUM)),
array('data' => t('Date'), 'field' => 'w.wid', 'sort' => 'desc', 'class' => array(RESPONSIVE_PRIORITY_LOW)),
t('Message'),
array('data' => t('User'), 'field' => 'u.name', 'class' => array(RESPONSIVE_PRIORITY_MEDIUM)),
array('data' => t('Operations'), 'class' => array(RESPONSIVE_PRIORITY_LOW)),
);
$query = db_select('watchdog', 'w')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('Drupal\Core\Database\Query\TableSortExtender');
$query->leftJoin('users', 'u', 'w.uid = u.uid');
$query
->fields('w', array('wid', 'uid', 'severity', 'type', 'timestamp', 'message', 'variables', 'link'))
->addField('u', 'name');
if (!empty($filter['where'])) {
$query->where($filter['where'], $filter['args']);
}
$result = $query
->limit(50)
->orderByHeader($header)
->execute();
foreach ($result as $dblog) {
// Check for required properties.
if (isset($dblog->message) && isset($dblog->variables)) {
// Messages without variables or user specified text.
if ($dblog->variables === 'N;') {
$message = $dblog->message;
}
// Message to translate with injected variables.
else {
$message = t($dblog->message, unserialize($dblog->variables));
}
if (isset($dblog->wid)) {
// Truncate link_text to 56 chars of message.
$log_text = truncate_utf8(filter_xss($message, array()), 56, TRUE, TRUE);
$message = l($log_text, 'admin/reports/event/' . $dblog->wid, array('html' => TRUE));
}
}
$rows[] = array('data' =>
array(
// Cells
array('class' => array('icon')),
t($dblog->type),
format_date($dblog->timestamp, 'short'),
$message,
theme('username', array('account' => $dblog)),
filter_xss($dblog->link),
),
// Attributes for tr
'class' => array(drupal_html_class('dblog-' . $dblog->type), $classes[$dblog->severity]),
);
}
$build['dblog_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => array('id' => 'admin-dblog', 'class' => array('admin-dblog')),
'#empty' => t('No log messages available.'),
);
$build['dblog_pager'] = array('#theme' => 'pager');
return $build;
}
/**
* Page callback: Shows the most frequent log messages of a given event type.
*

View File

@ -42,10 +42,8 @@ function dblog_menu() {
$items['admin/reports/dblog'] = array(
'title' => 'Recent log messages',
'description' => 'View events that have recently been logged.',
'page callback' => 'dblog_overview',
'access arguments' => array('access site reports'),
'route_name' => 'dblog_overview',
'weight' => -1,
'file' => 'dblog.admin.inc',
);
$items['admin/reports/page-not-found'] = array(
'title' => "Top 'page not found' errors",

View File

@ -0,0 +1,6 @@
dblog_overview:
pattern: '/admin/reports/dblog'
defaults:
_content: '\Drupal\dblog\Controller\DbLogController::overview'
requirements:
_permission: 'access site reports'

View File

@ -0,0 +1,228 @@
<?php
/**
* @file
* Contains \Drupal\dblog\Controller\DbLogController.
*/
namespace Drupal\dblog\Controller;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Database\Connection;
use Drupal\Core\ControllerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Returns responses for dblog routes.
*/
class DbLogController implements ControllerInterface {
/**
* The database service.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('database'),
$container->get('module_handler')
);
}
/**
* Constructs a DbLogController object.
*
* @param Connection $database
* A database connection.
* @param ModuleHandlerInterface $module_handler
* A module handler.
*/
public function __construct(Connection $database, ModuleHandlerInterface $module_handler) {
$this->database = $database;
$this->moduleHandler = $module_handler;
}
/**
* Gets an array of log level classes.
*
* @return array
* An array of log level classes.
*/
public static function getLogLevelClassMap() {
return array(
WATCHDOG_DEBUG => 'dblog-debug',
WATCHDOG_INFO => 'dblog-info',
WATCHDOG_NOTICE => 'dblog-notice',
WATCHDOG_WARNING => 'dblog-warning',
WATCHDOG_ERROR => 'dblog-error',
WATCHDOG_CRITICAL => 'dblog-critical',
WATCHDOG_ALERT => 'dblog-alert',
WATCHDOG_EMERGENCY => 'dblog-emergency',
);
}
/**
* Displays a listing of database log messages.
*
* Messages are truncated at 56 chars.
* Full-length messages can be viewed on the message details page.
*
* @return array
* A render array as expected by drupal_render().
*
* @see dblog_clear_log_form()
* @see dblog_event()
* @see dblog_filter_form()
*/
public function overview() {
$filter = $this->buildFilterQuery();
$rows = array();
$classes = static::getLogLevelClassMap();
$this->moduleHandler->loadInclude('dblog', 'admin.inc');
$build['dblog_filter_form'] = drupal_get_form('dblog_filter_form');
$build['dblog_clear_log_form'] = drupal_get_form('dblog_clear_log_form');
$header = array(
// Icon column.
'',
array(
'data' => t('Type'),
'field' => 'w.type',
'class' => array(RESPONSIVE_PRIORITY_MEDIUM)),
array(
'data' => t('Date'),
'field' => 'w.wid',
'sort' => 'desc',
'class' => array(RESPONSIVE_PRIORITY_LOW)),
t('Message'),
array(
'data' => t('User'),
'field' => 'u.name',
'class' => array(RESPONSIVE_PRIORITY_MEDIUM)),
array(
'data' => t('Operations'),
'class' => array(RESPONSIVE_PRIORITY_LOW)),
);
$query = $this->database->select('watchdog', 'w')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->extend('Drupal\Core\Database\Query\TableSortExtender');
$query->leftJoin('users', 'u', 'w.uid = u.uid');
$query->fields('w', array(
'wid',
'uid',
'severity',
'type',
'timestamp',
'message',
'variables',
'link',
));
$query->addField('u', 'name');
if (!empty($filter['where'])) {
$query->where($filter['where'], $filter['args']);
}
$result = $query
->limit(50)
->orderByHeader($header)
->execute();
foreach ($result as $dblog) {
// Check for required properties.
if (isset($dblog->message) && isset($dblog->variables)) {
// Messages without variables or user specified text.
if ($dblog->variables === 'N;') {
$message = $dblog->message;
}
// Message to translate with injected variables.
else {
$message = t($dblog->message, unserialize($dblog->variables));
}
if (isset($dblog->wid)) {
// Truncate link_text to 56 chars of message.
$log_text = Unicode::truncate(filter_xss($message, array()), 56, TRUE, TRUE);
$message = l($log_text, 'admin/reports/event/' . $dblog->wid, array('html' => TRUE));
}
}
$rows[] = array(
'data' => array(
// Cells.
array('class' => array('icon')),
t($dblog->type),
format_date($dblog->timestamp, 'short'),
$message,
theme('username', array('account' => $dblog)),
filter_xss($dblog->link),
),
// Attributes for table row.
'class' => array(drupal_html_class('dblog-' . $dblog->type), $classes[$dblog->severity]),
);
}
$build['dblog_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#attributes' => array('id' => 'admin-dblog', 'class' => array('admin-dblog')),
'#empty' => t('No log messages available.'),
);
$build['dblog_pager'] = array('#theme' => 'pager');
return $build;
}
/**
* Builds a query for database log administration filters based on session.
*
* @return array
* An associative array with keys 'where' and 'args'.
*/
protected function buildFilterQuery() {
if (empty($_SESSION['dblog_overview_filter'])) {
return;
}
$this->moduleHandler->loadInclude('dblog', 'admin.inc');
$filters = dblog_filters();
// Build query.
$where = $args = array();
foreach ($_SESSION['dblog_overview_filter'] as $key => $filter) {
$filter_where = array();
foreach ($filter as $value) {
$filter_where[] = $filters[$key]['where'];
$args[] = $value;
}
if (!empty($filter_where)) {
$where[] = '(' . implode(' OR ', $filter_where) . ')';
}
}
$where = !empty($where) ? implode(' AND ', $where) : '';
return array(
'where' => $where,
'args' => $args,
);
}
}

View File

@ -2,18 +2,19 @@
/**
* @file
* Definition of Drupal\dblog\Tests\DBLogTest.
* Contains \Drupal\dblog\Tests\DbLogTest.
*/
namespace Drupal\dblog\Tests;
use Drupal\dblog\Controller\DbLogController;
use Drupal\simpletest\WebTestBase;
use SimpleXMLElement;
/**
* Tests logging messages to the database.
*/
class DBLogTest extends WebTestBase {
class DbLogTest extends WebTestBase {
/**
* Modules to enable.
@ -38,9 +39,9 @@ class DBLogTest extends WebTestBase {
public static function getInfo() {
return array(
'name' => 'DBLog functionality',
'name' => 'DbLog functionality',
'description' => 'Generate events and verify dblog entries; verify user access to log reports based on persmissions.',
'group' => 'DBLog',
'group' => 'DbLog',
);
}
@ -59,7 +60,7 @@ class DBLogTest extends WebTestBase {
* Database Logging module functionality through both the admin and user
* interfaces.
*/
function testDBLog() {
function testDbLog() {
// Login the admin user.
$this->drupalLogin($this->big_user);
@ -375,14 +376,14 @@ class DBLogTest extends WebTestBase {
"taxonomy_forums[$langcode]" => array(1),
"body[$langcode][0][value]" => $this->randomName(32),
);
break;
break;
default:
$content = array(
"title" => $this->randomName(8),
"body[$langcode][0][value]" => $this->randomName(32),
);
break;
break;
}
return $content;
}
@ -575,17 +576,7 @@ class DBLogTest extends WebTestBase {
* The watchdog severity constant or NULL if not found.
*/
protected function getSeverityConstant($class) {
// Reversed array from dblog_overview().
$map = array(
'dblog-debug' => WATCHDOG_DEBUG,
'dblog-info' => WATCHDOG_INFO,
'dblog-notice' => WATCHDOG_NOTICE,
'dblog-warning' => WATCHDOG_WARNING,
'dblog-error' => WATCHDOG_ERROR,
'dblog-critical' => WATCHDOG_CRITICAL,
'dblog-alert' => WATCHDOG_ALERT,
'dblog-emergency' => WATCHDOG_EMERGENCY,
);
$map = array_flip(DbLogController::getLogLevelClassMap());
// Find the class that contains the severity.
$classes = explode(' ', $class);