diff --git a/core/modules/dblog/dblog.admin.inc b/core/modules/dblog/dblog.admin.inc index d432b5b..cf744b0 100644 --- a/core/modules/dblog/dblog.admin.inc +++ b/core/modules/dblog/dblog.admin.inc @@ -6,102 +6,6 @@ */ /** - * 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. * * Messages are not truncated on this page because events detailed herein do not diff --git a/core/modules/dblog/dblog.module b/core/modules/dblog/dblog.module index d28f3b7..63e2b8f 100644 --- a/core/modules/dblog/dblog.module +++ b/core/modules/dblog/dblog.module @@ -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", diff --git a/core/modules/dblog/dblog.routing.yml b/core/modules/dblog/dblog.routing.yml new file mode 100644 index 0000000..b3b01fc --- /dev/null +++ b/core/modules/dblog/dblog.routing.yml @@ -0,0 +1,6 @@ +dblog_overview: + pattern: '/admin/reports/dblog' + defaults: + _content: '\Drupal\dblog\Controller\DBLogController::overview' + requirements: + _permission: 'access site reports' diff --git a/core/modules/dblog/lib/Drupal/dblog/Controller/DBLogController.php b/core/modules/dblog/lib/Drupal/dblog/Controller/DBLogController.php new file mode 100644 index 0000000..5439c08 --- /dev/null +++ b/core/modules/dblog/lib/Drupal/dblog/Controller/DBLogController.php @@ -0,0 +1,220 @@ +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. + * + * @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 = 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 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, + ); + } + +} diff --git a/core/modules/dblog/lib/Drupal/dblog/Tests/DBLogTest.php b/core/modules/dblog/lib/Drupal/dblog/Tests/DBLogTest.php index 9fcc1ee..62608a7 100644 --- a/core/modules/dblog/lib/Drupal/dblog/Tests/DBLogTest.php +++ b/core/modules/dblog/lib/Drupal/dblog/Tests/DBLogTest.php @@ -7,6 +7,7 @@ namespace Drupal\dblog\Tests; +use Drupal\dblog\Controller\DBLogController; use Drupal\simpletest\WebTestBase; use SimpleXMLElement; @@ -575,17 +576,7 @@ protected function getTypeCount(array $types) { * 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);