From 7a57bb56af450cc034c68e7279a70ba2be5601c4 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sat, 27 Nov 2004 23:10:05 +0000 Subject: [PATCH] Add an extension for logging MediaWiki events to the system logger (syslog). Although intended to be useful, it's more of a testbed for event hooks than anything else. For a first pass, started logging user login and user logout events. Had to add hooks-running code to the login and logout modules, but that's kind of the point. Documented the three events added in hooks.doc. --- docs/hooks.doc | 20 +++++++--- extensions/Syslog.php | 69 ++++++++++++++++++++++++++++++++++ includes/Hooks.php | 2 +- includes/SpecialUserlogin.php | 4 ++ includes/SpecialUserlogout.php | 16 +++++--- 5 files changed, 100 insertions(+), 11 deletions(-) create mode 100644 extensions/Syslog.php diff --git a/docs/hooks.doc b/docs/hooks.doc index 57ffddf179..0e2d167fd2 100644 --- a/docs/hooks.doc +++ b/docs/hooks.doc @@ -221,8 +221,18 @@ if it shouldn't (an error occurred, or one of the hooks handled the action already). Checking the return value matters more for "before" hooks than for "complete" hooks. -==Existing hooks== - -The following list of hooks exist in the code right now: - -TBD +==Events and parameters== + +This is a list of known events and parameters; please add to it if +you're going to add events to the MediaWiki code. + +'UserLoginComplete': after a user has logged in +$user: the user object that was created on login + +'UserLogout': before a user logs out +$user: the user object that is about to be logged out + +'UserLogoutComplete': after a user has logged out +$user: the user object _after_ logout (won't have name, ID, etc.) + + diff --git a/extensions/Syslog.php b/extensions/Syslog.php new file mode 100644 index 0000000000..0e09b2c078 --- /dev/null +++ b/extensions/Syslog.php @@ -0,0 +1,69 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * @author Evan Prodromou + * @package MediaWiki + * @subpackage Extensions + */ + +if (defined('MEDIAWIKI')) { + + # Setup globals + + if (!isset($wgSyslogIdentity)) { + $wgSyslogIdentity = $wgDBname; + } + if (!isset($wgSyslogFacility)) { + $wgSyslogFacility = LOG_USER; + } + + # Hook for login + + function syslogUserLogin(&$user) { + syslog(LOG_INFO, "User '" . $user->getName() . "' logged in."); + return true; + } + + # Hook for logout + + function syslogUserLogout(&$user) { + syslog(LOG_INFO, "User '" . $user->getName() . "' logged out."); + return true; + } + + # Setup -- called once environment is configured + + function setupSyslog() { + + global $wgSyslogIdentity, $wgSyslogFacility, $_syslogId; + global $wgHooks; + + openlog($wgSyslogIdentity, LOG_ODELAY | LOG_PID, $wgSyslogFacility); + + $wgHooks['UserLoginComplete'][] = syslogUserLogin; + $wgHooks['UserLogout'][] = syslogUserLogout; + + return true; + } + + # Add to global list of extensions + + $wgExtensionFunctions[] = setupSyslog; +} + +?> \ No newline at end of file diff --git a/includes/Hooks.php b/includes/Hooks.php index 56ca88d403..5cfde41e90 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -48,7 +48,7 @@ if (defined('MEDIAWIKI')) { $event = array_shift($args); - if (!array_key_exists($wgHooks, $event)) { + if (!array_key_exists($event, $wgHooks)) { return true; } diff --git a/includes/SpecialUserlogin.php b/includes/SpecialUserlogin.php index b888186230..3ab19cdf07 100644 --- a/includes/SpecialUserlogin.php +++ b/includes/SpecialUserlogin.php @@ -361,6 +361,10 @@ class LoginForm { global $wgDeferredUpdateList; global $wgOut; + # Run any hooks; ignore results + + wfRunHooks('UserLoginComplete', $wgUser); + $wgOut->setPageTitle( wfMsg( 'loginsuccesstitle' ) ); $wgOut->setRobotpolicy( 'noindex,nofollow' ); $wgOut->setArticleRelated( false ); diff --git a/includes/SpecialUserlogout.php b/includes/SpecialUserlogout.php index 52aa73d29b..6c9a8882e0 100644 --- a/includes/SpecialUserlogout.php +++ b/includes/SpecialUserlogout.php @@ -11,11 +11,17 @@ function wfSpecialUserlogout() { global $wgUser, $wgOut, $returnto; - $wgUser->logout(); - $wgOut->mCookies = array(); - $wgOut->setRobotpolicy( 'noindex,nofollow' ); - $wgOut->addHTML( wfMsg( 'logouttext' ) ); - $wgOut->returnToMain(); + if (wfRunHooks('UserLogout', $wgUser)) { + + $wgUser->logout(); + + $wgOut->mCookies = array(); + $wgOut->setRobotpolicy( 'noindex,nofollow' ); + $wgOut->addHTML( wfMsg( 'logouttext' ) ); + $wgOut->returnToMain(); + + wfRunHooks('UserLogoutComplete', $wgUser); + } } ?> -- 2.20.1