Add an extension for logging MediaWiki events to the system logger
authorEvan Prodromou <evanprodromou@users.mediawiki.org>
Sat, 27 Nov 2004 23:10:05 +0000 (23:10 +0000)
committerEvan Prodromou <evanprodromou@users.mediawiki.org>
Sat, 27 Nov 2004 23:10:05 +0000 (23:10 +0000)
(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
extensions/Syslog.php [new file with mode: 0644]
includes/Hooks.php
includes/SpecialUserlogin.php
includes/SpecialUserlogout.php

index 57ffddf..0e2d167 100644 (file)
@@ -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 (file)
index 0000000..0e09b2c
--- /dev/null
@@ -0,0 +1,69 @@
+<?
+/* Syslog.php -- an extension to log events to the system logger
+ * Copyright 2004 Evan Prodromou <evan@wikitravel.org>
+ *
+ *  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 <evan@wikitravel.org>
+ * @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
index 56ca88d..5cfde41 100644 (file)
@@ -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;
                }
 
index b888186..3ab19cd 100644 (file)
@@ -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 );
index 52aa73d..6c9a888 100644 (file)
 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);
+       }
 }
 
 ?>