2007-07-22 06:51:47 +00:00
< ? php
// $Id$
/**
* @ file
* Administrative page callbacks for the dblog module .
*/
/**
* Menu callback ; displays a listing of log messages .
*/
function dblog_overview () {
$filter = dblog_build_filter_query ();
$rows = array ();
$icons = array (
2008-07-19 07:44:45 +00:00
WATCHDOG_DEBUG => '' ,
WATCHDOG_INFO => '' ,
WATCHDOG_NOTICE => '' ,
2009-10-09 01:00:08 +00:00
WATCHDOG_WARNING => theme ( 'image' , array ( 'path' => 'misc/watchdog-warning.png' , 'alt' => t ( 'warning' ), 'title' => t ( 'warning' ))),
WATCHDOG_ERROR => theme ( 'image' , array ( 'path' => 'misc/watchdog-error.png' , 'alt' => t ( 'error' ), 'title' => t ( 'error' ))),
WATCHDOG_CRITICAL => theme ( 'image' , array ( 'path' => 'misc/watchdog-error.png' , 'alt' => t ( 'critical' ), 'title' => t ( 'critical' ))),
WATCHDOG_ALERT => theme ( 'image' , array ( 'path' => 'misc/watchdog-error.png' , 'alt' => t ( 'alert' ), 'title' => t ( 'alert' ))),
WATCHDOG_EMERG => theme ( 'image' , array ( 'path' => 'misc/watchdog-error.png' , 'alt' => t ( 'emergency' ), 'title' => t ( 'emergency' ))),
2007-07-22 06:51:47 +00:00
);
$classes = array (
2008-07-19 07:44:45 +00:00
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_EMERG => 'dblog-emerg' ,
2007-07-22 06:51:47 +00:00
);
2009-05-25 10:43:54 +00:00
$build [ 'dblog_filter_form' ] = drupal_get_form ( 'dblog_filter_form' );
$build [ 'dblog_clear_log_form' ] = drupal_get_form ( 'dblog_clear_log_form' );
2007-07-22 06:51:47 +00:00
$header = array (
2009-06-06 10:27:42 +00:00
'' , // Icon column.
2007-07-22 06:51:47 +00:00
array ( 'data' => t ( 'Type' ), 'field' => 'w.type' ),
array ( 'data' => t ( 'Date' ), 'field' => 'w.wid' , 'sort' => 'desc' ),
t ( 'Message' ),
array ( 'data' => t ( 'User' ), 'field' => 'u.name' ),
array ( 'data' => t ( 'Operations' )),
);
2009-06-06 10:27:42 +00:00
$query = db_select ( 'watchdog' , 'w' ) -> extend ( 'PagerDefault' ) -> extend ( 'TableSort' );
2009-03-13 14:28:18 +00:00
$query -> join ( 'users' , 'u' , 'w.uid = u.uid' );
$query
-> fields ( 'w' , array ( 'wid' , 'uid' , 'severity' , 'type' , 'timestamp' , 'message' , 'variables' , 'link' ))
-> addField ( 'u' , 'name' );
2007-07-22 06:51:47 +00:00
if ( ! empty ( $filter [ 'where' ])) {
2009-06-06 10:27:42 +00:00
$query -> where ( $filter [ 'where' ], $filter [ 'args' ]);
2007-07-22 06:51:47 +00:00
}
2009-06-06 10:27:42 +00:00
$result = $query
-> limit ( 50 )
-> orderByHeader ( $header )
-> execute ();
2007-07-22 06:51:47 +00:00
2009-03-13 14:28:18 +00:00
foreach ( $result as $dblog ) {
2007-07-22 06:51:47 +00:00
$rows [] = array ( 'data' =>
array (
// Cells
$icons [ $dblog -> severity ],
t ( $dblog -> type ),
2009-08-25 10:27:15 +00:00
format_date ( $dblog -> timestamp , 'short' ),
2008-04-14 17:48:46 +00:00
l ( truncate_utf8 ( _dblog_format_message ( $dblog ), 56 , TRUE , TRUE ), 'admin/reports/event/' . $dblog -> wid , array ( 'html' => TRUE )),
2009-10-09 01:00:08 +00:00
theme ( 'username' , array ( 'account' => $dblog )),
2007-07-22 06:51:47 +00:00
$dblog -> link ,
),
// Attributes for tr
2009-08-22 14:34:23 +00:00
'class' => array ( 'dblog-' . preg_replace ( '/[^a-z]/i' , '-' , $dblog -> type ), $classes [ $dblog -> severity ]),
2007-07-22 06:51:47 +00:00
);
}
2009-07-29 06:39:35 +00:00
$build [ 'dblog_table' ] = array (
2009-08-24 00:14:23 +00:00
'#theme' => 'table' ,
'#header' => $header ,
'#rows' => $rows ,
2009-07-29 06:39:35 +00:00
'#attributes' => array ( 'id' => 'admin-dblog' ),
2009-12-02 14:56:32 +00:00
'#empty' => t ( 'No log messages available.' ),
2009-07-29 06:39:35 +00:00
);
$build [ 'dblog_pager' ] = array ( '#theme' => 'pager' );
2007-07-22 06:51:47 +00:00
2009-05-25 10:43:54 +00:00
return $build ;
2007-07-22 06:51:47 +00:00
}
/**
* Menu callback ; generic function to display a page of the most frequent
* dblog events of a specified type .
*/
function dblog_top ( $type ) {
$header = array (
array ( 'data' => t ( 'Count' ), 'field' => 'count' , 'sort' => 'desc' ),
array ( 'data' => t ( 'Message' ), 'field' => 'message' )
);
2009-03-13 14:28:18 +00:00
$count_query = db_select ( 'watchdog' );
$count_query -> addExpression ( 'COUNT(DISTINCT(message))' );
$count_query -> condition ( 'type' , $type );
2007-07-22 06:51:47 +00:00
2009-06-06 10:27:42 +00:00
$query = db_select ( 'watchdog' , 'w' ) -> extend ( 'PagerDefault' ) -> extend ( 'TableSort' );
2009-03-13 14:28:18 +00:00
$query -> addExpression ( 'COUNT(wid)' , 'count' );
$query = $query
-> fields ( 'w' , array ( 'message' , 'variables' ))
-> condition ( 'w.type' , $type )
-> groupBy ( 'message' )
-> groupBy ( 'variables' )
2009-06-06 10:27:42 +00:00
-> limit ( 30 )
-> orderByHeader ( $header );
2009-03-13 14:28:18 +00:00
$query -> setCountQuery ( $count_query );
$result = $query -> execute ();
2007-07-22 06:51:47 +00:00
$rows = array ();
2009-03-13 14:28:18 +00:00
foreach ( $result as $dblog ) {
2007-07-22 06:51:47 +00:00
$rows [] = array ( $dblog -> count , truncate_utf8 ( _dblog_format_message ( $dblog ), 56 , TRUE , TRUE ));
}
2009-07-29 06:39:35 +00:00
$build [ 'dblog_top_table' ] = array (
2009-08-24 00:14:23 +00:00
'#theme' => 'table' ,
'#header' => $header ,
2009-07-29 06:39:35 +00:00
'#rows' => $rows ,
2009-12-02 14:56:32 +00:00
'#empty' => t ( 'No log messages available.' ),
2009-07-29 06:39:35 +00:00
);
$build [ 'dblog_top_pager' ] = array ( '#theme' => 'pager' );
2007-07-22 06:51:47 +00:00
2009-07-29 06:39:35 +00:00
return $build ;
2007-07-22 06:51:47 +00:00
}
/**
* Menu callback ; displays details about a log message .
*/
function dblog_event ( $id ) {
$severity = watchdog_severity_levels ();
2009-03-13 14:28:18 +00:00
$result = db_query ( 'SELECT w.*, u.name, u.uid FROM {watchdog} w INNER JOIN {users} u ON w.uid = u.uid WHERE w.wid = :id' , array ( ':id' => $id )) -> fetchObject ();
if ( $dblog = $result ) {
2007-07-22 06:51:47 +00:00
$rows = array (
array (
array ( 'data' => t ( 'Type' ), 'header' => TRUE ),
t ( $dblog -> type ),
),
array (
array ( 'data' => t ( 'Date' ), 'header' => TRUE ),
2009-08-25 10:27:15 +00:00
format_date ( $dblog -> timestamp , 'long' ),
2007-07-22 06:51:47 +00:00
),
array (
array ( 'data' => t ( 'User' ), 'header' => TRUE ),
2009-10-09 01:00:08 +00:00
theme ( 'username' , array ( 'account' => $dblog )),
2007-07-22 06:51:47 +00:00
),
array (
array ( 'data' => t ( 'Location' ), 'header' => TRUE ),
l ( $dblog -> location , $dblog -> location ),
),
array (
array ( 'data' => t ( 'Referrer' ), 'header' => TRUE ),
l ( $dblog -> referer , $dblog -> referer ),
),
array (
array ( 'data' => t ( 'Message' ), 'header' => TRUE ),
_dblog_format_message ( $dblog ),
),
array (
array ( 'data' => t ( 'Severity' ), 'header' => TRUE ),
$severity [ $dblog -> severity ],
),
array (
array ( 'data' => t ( 'Hostname' ), 'header' => TRUE ),
2007-07-26 21:42:52 +00:00
check_plain ( $dblog -> hostname ),
2007-07-22 06:51:47 +00:00
),
array (
array ( 'data' => t ( 'Operations' ), 'header' => TRUE ),
$dblog -> link ,
),
);
2009-07-29 06:39:35 +00:00
$build [ 'dblog_table' ] = array (
2009-08-24 00:14:23 +00:00
'#theme' => 'table' ,
'#rows' => $rows ,
2009-08-22 14:34:23 +00:00
'#attributes' => array ( 'class' => array ( 'dblog-event' )),
2009-07-29 06:39:35 +00:00
);
return $build ;
}
else {
return '' ;
2007-07-22 06:51:47 +00:00
}
}
/**
* Build query for dblog administration filters based on session .
*/
function dblog_build_filter_query () {
if ( empty ( $_SESSION [ 'dblog_overview_filter' ])) {
return ;
}
$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 )) {
2008-04-14 17:48:46 +00:00
$where [] = '(' . implode ( ' OR ' , $filter_where ) . ')' ;
2007-07-22 06:51:47 +00:00
}
}
$where = ! empty ( $where ) ? implode ( ' AND ' , $where ) : '' ;
return array (
'where' => $where ,
'args' => $args ,
);
}
/**
* List dblog administration filters that can be applied .
*/
function dblog_filters () {
$filters = array ();
2007-09-05 08:42:02 +00:00
foreach ( _dblog_get_message_types () as $type ) {
2007-07-22 06:51:47 +00:00
$types [ $type ] = $type ;
}
if ( ! empty ( $types )) {
$filters [ 'type' ] = array (
'title' => t ( 'Type' ),
2009-08-16 17:57:44 +00:00
'where' => " w.type = ? " ,
2007-07-22 06:51:47 +00:00
'options' => $types ,
);
}
$filters [ 'severity' ] = array (
'title' => t ( 'Severity' ),
2009-08-16 17:57:44 +00:00
'where' => 'w.severity = ?' ,
2007-07-22 06:51:47 +00:00
'options' => watchdog_severity_levels (),
);
return $filters ;
}
/**
* Formats a log message for display .
*
* @ param $dblog
* An object with at least the message and variables properties
*/
function _dblog_format_message ( $dblog ) {
// Legacy messages and user specified text
if ( $dblog -> variables === 'N;' ) {
return $dblog -> message ;
}
// Message to translate with injected variables
else {
return t ( $dblog -> message , unserialize ( $dblog -> variables ));
}
}
/**
* Return form for dblog administration filters .
*
* @ ingroup forms
2008-01-08 10:35:43 +00:00
* @ see dblog_filter_form_submit ()
* @ see dblog_filter_form_validate ()
2007-07-22 06:51:47 +00:00
*/
2009-09-18 00:12:48 +00:00
function dblog_filter_form ( $form ) {
2007-07-22 06:51:47 +00:00
$filters = dblog_filters ();
$form [ 'filters' ] = array (
'#type' => 'fieldset' ,
'#title' => t ( 'Filter log messages' ),
'#theme' => 'dblog_filters' ,
'#collapsible' => TRUE ,
'#collapsed' => empty ( $session ),
);
foreach ( $filters as $key => $filter ) {
$form [ 'filters' ][ 'status' ][ $key ] = array (
'#title' => $filter [ 'title' ],
'#type' => 'select' ,
'#multiple' => TRUE ,
'#size' => 8 ,
'#options' => $filter [ 'options' ],
);
2009-06-02 06:58:17 +00:00
if ( ! empty ( $_SESSION [ 'dblog_overview_filter' ][ $key ])) {
$form [ 'filters' ][ 'status' ][ $key ][ '#default_value' ] = $_SESSION [ 'dblog_overview_filter' ][ $key ];
2007-07-22 06:51:47 +00:00
}
}
$form [ 'filters' ][ 'buttons' ][ 'submit' ] = array (
'#type' => 'submit' ,
'#value' => t ( 'Filter' ),
);
2009-06-02 06:58:17 +00:00
if ( ! empty ( $_SESSION [ 'dblog_overview_filter' ])) {
2007-07-22 06:51:47 +00:00
$form [ 'filters' ][ 'buttons' ][ 'reset' ] = array (
'#type' => 'submit' ,
'#value' => t ( 'Reset' )
);
}
return $form ;
}
/**
* Validate result from dblog administration filter form .
*/
function dblog_filter_form_validate ( $form , & $form_state ) {
if ( $form_state [ 'values' ][ 'op' ] == t ( 'Filter' ) && empty ( $form_state [ 'values' ][ 'type' ]) && empty ( $form_state [ 'values' ][ 'severity' ])) {
form_set_error ( 'type' , t ( 'You must select something to filter by.' ));
}
}
/**
* Process result from dblog administration filter form .
*/
function dblog_filter_form_submit ( $form , & $form_state ) {
$op = $form_state [ 'values' ][ 'op' ];
$filters = dblog_filters ();
switch ( $op ) {
case t ( 'Filter' ) :
foreach ( $filters as $name => $filter ) {
if ( isset ( $form_state [ 'values' ][ $name ])) {
$_SESSION [ 'dblog_overview_filter' ][ $name ] = $form_state [ 'values' ][ $name ];
}
}
break ;
case t ( 'Reset' ) :
2009-06-02 06:58:17 +00:00
$_SESSION [ 'dblog_overview_filter' ] = array ();
2007-07-22 06:51:47 +00:00
break ;
}
2007-10-20 21:57:50 +00:00
return 'admin/reports/dblog' ;
2007-07-22 06:51:47 +00:00
}
2008-12-24 10:38:41 +00:00
/**
* Return form for dblog clear button .
*
* @ ingroup forms
* @ see dblog_clear_log_submit ()
*/
2009-09-18 00:12:48 +00:00
function dblog_clear_log_form ( $form ) {
2008-12-24 10:38:41 +00:00
$form [ 'dblog_clear' ] = array (
'#type' => 'fieldset' ,
'#title' => t ( 'Clear log messages' ),
'#description' => t ( 'This will permanently remove the log messages from the database.' ),
'#collapsible' => TRUE ,
'#collapsed' => TRUE ,
);
$form [ 'dblog_clear' ][ 'clear' ] = array (
'#type' => 'submit' ,
'#value' => t ( 'Clear log messages' ),
'#submit' => array ( 'dblog_clear_log_submit' ),
);
return $form ;
}
/**
* Submit callback : clear database with log messages .
*/
2009-09-18 00:12:48 +00:00
function dblog_clear_log_submit () {
2008-12-24 10:38:41 +00:00
db_delete ( 'watchdog' ) -> execute ();
drupal_set_message ( t ( 'Database log cleared.' ));
}