r108243 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108242‎ | r108243 | r108244 >
Date:15:56, 6 January 2012
Author:ialex
Status:deferred
Tags:
Comment:
* Moved hooks to their own file
* Defer opening of syslog until a message is logged and removed extension function
* Made hooks work with current MW; removed all reference in hooks signatures, removed no-longer passed (or useless) parameters and added some missing "return true;"
* Bump version
Modified paths:
  • /trunk/extensions/Syslog/Syslog.php (modified) (history)
  • /trunk/extensions/Syslog/SyslogHooks.php (added) (history)

Diff [purge]

Index: trunk/extensions/Syslog/Syslog.php
@@ -1,6 +1,7 @@
22 <?php
3 -/* Syslog.php -- an extension to log events to the system logger
4 - * Copyright 2004 Evan Prodromou <evan@wikitravel.org>
 3+/**
 4+ * Syslog.php -- an extension to log events to the system logger
 5+ * Copyright © 2004 Evan Prodromou <evan@wikitravel.org>
56 *
67 * This program is free software; you can redistribute it and/or modify
78 * it under the terms of the GNU General Public License as published by
@@ -17,123 +18,38 @@
1819 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1920 *
2021 * @author Evan Prodromou <evan@wikitravel.org>
 22+ * @author Alexandre Emsenhuber
2123 * @ingroup Extensions
2224 */
2325
2426 if ( !defined( 'MEDIAWIKI' ) ) {
25 - die(1);
 27+ die( 1 );
2628 }
2729
28 -# Setup globals
29 -
30 -$wgSyslogIdentity = false;
31 -$wgSyslogFacility = LOG_USER;
32 -
33 -
34 -# Hook for article protection
35 -
36 -function syslogArticleProtect(&$article, &$user, $protect, &$reason, &$moveonly) {
37 - $title = $article->mTitle;
38 - syslog(LOG_NOTICE, "User '" . $user->getName() . "' " .
39 - (($protect) ? "protected" : "unprotected") . " article '" .
40 - $title->getPrefixedText() .
41 - "' for '" . $reason . "' " . (($moveonly) ? "(moves only)" : "") );
42 - return true;
43 -}
44 -
45 -# Hook for article deletion
46 -
47 -function syslogArticleDelete(&$article, &$user, &$reason) {
48 - $title = $article->mTitle;
49 - syslog(LOG_NOTICE, "User '" . $user->getName() . "' deleted '" .
50 - $title->getPrefixedText() .
51 - "' for '" . $reason . "' ");
52 - return true;
53 -}
54 -
55 -# Hook for article save
56 -
57 -function syslogArticleSave(&$article, &$user, &$text, $summary, $isminor, $iswatch, $section) {
58 - $title = $article->mTitle;
59 - syslog(LOG_NOTICE, "User '" . $user->getName() . "' saved '" .
60 - $title->getPrefixedText() .
61 - "' with comment '" . $summary . "' ");
62 - return true;
63 -}
64 -
65 -# Hook for IP & user blocks
66 -
67 -function syslogBlockIp(&$block, &$user) {
68 - syslog(LOG_NOTICE, "User '" . $user->getName() .
69 - "' blocked '" . (($block->mUser) ? $block->mUser : $block->mAddress) .
70 - "' for '" . $block->mReason . "' until '" . $block->mExpiry . "'");
71 - return true;
72 -}
73 -
74 -function syslogEmailUser(&$to, &$from, &$subject, &$text) {
75 - syslog(LOG_INFO, "Email sent from '$from' to '$to' with subject '$subject'");
76 -}
77 -
78 -# Hook for unwatch
79 -
80 -function syslogUnwatch(&$user, &$article) {
81 - syslog(LOG_INFO, "User '" . $user->getName() . "' stopped watching '" .
82 - $article->mTitle->getPrefixedText() . "'");
83 -}
84 -
85 -# Hook for login
86 -
87 -function syslogUserLogin(&$user) {
88 - syslog(LOG_INFO, "User '" . $user->getName() . "' logged in");
89 - return true;
90 -}
91 -
92 -# Hook for logout
93 -
94 -function syslogUserLogout(&$user) {
95 - syslog(LOG_INFO, "User '" . $user->getName() . "' logged out");
96 - return true;
97 -}
98 -
99 -# Hook for watch
100 -
101 -function syslogWatch(&$user, &$article) {
102 - syslog(LOG_INFO, "User '" . $user->getName() . "' started watching '" .
103 - $article->mTitle->getPrefixedText() . "'");
104 -}
105 -
106 -# Setup -- called once environment is configured
107 -
108 -function setupSyslog() {
109 -
110 - global $wgSyslogIdentity, $wgSyslogFacility, $_syslogId;
111 - global $wgHooks, $wgDBname;
112 -
113 - openlog($wgSyslogIdentity ? $wgSyslogIdentity : $wgDBname, LOG_ODELAY | LOG_PID, $wgSyslogFacility);
114 -
115 - $wgHooks['UserLoginComplete'][] = 'syslogUserLogin';
116 - $wgHooks['UserLogout'][] = 'syslogUserLogout';
117 - $wgHooks['BlockIpComplete'][] = 'syslogBlockIp';
118 - $wgHooks['ArticleProtectComplete'][] = 'syslogArticleProtect';
119 - $wgHooks['ArticleDeleteComplete'][] = 'syslogArticleDelete';
120 - $wgHooks['ArticleSaveComplete'][] = 'syslogArticleSave';
121 - $wgHooks['EmailUserComplete'][] = 'syslogEmailUser';
122 - $wgHooks['WatchArticleComplete'][] = 'syslogWatch';
123 - $wgHooks['UnwatchArticleComplete'][] = 'syslogUnwatch';
124 -
125 - return true;
126 -}
127 -
128 -# Add to global list of extensions
129 -
130 -$wgExtensionFunctions[] = 'setupSyslog';
131 -
13230 $wgExtensionCredits['other'][] = array(
13331 'path' => __FILE__,
13432 'name' => 'Syslog',
135 - 'author' => 'Evan Prodromou',
 33+ 'author' => array( 'Evan Prodromou', 'Alexandre Emsenhuber' ),
13634 'url' => 'https://www.mediawiki.org/wiki/Extension:Syslog',
13735 'description' => 'An extension to log events to the system logger',
138 - 'version' => '1.1'
 36+ 'version' => '1.2'
13937 );
14038
 39+$wgAutoloadClasses['SyslogHooks'] = dirname( __FILE__ ) . '/SyslogHooks.php';
 40+
 41+# Setup globals
 42+
 43+$wgSyslogIdentity = false;
 44+$wgSyslogFacility = LOG_USER;
 45+
 46+# Setup hooks
 47+
 48+$wgHooks['ArticleDeleteComplete'][] = 'SyslogHooks::onArticleDeleteComplete';
 49+$wgHooks['ArticleProtectComplete'][] = 'SyslogHooks::onArticleProtectComplete';
 50+$wgHooks['ArticleSaveComplete'][] = 'SyslogHooks::onArticleSaveComplete';
 51+$wgHooks['BlockIpComplete'][] = 'SyslogHooks::onBlockIpComplete';
 52+$wgHooks['EmailUserComplete'][] = 'SyslogHooks::onEmailUserComplete';
 53+$wgHooks['UnwatchArticleComplete'][] = 'SyslogHooks::onUnwatchArticleComplete';
 54+$wgHooks['UserLoginComplete'][] = 'SyslogHooks::onUserLoginComplete';
 55+$wgHooks['UserLogoutComplete'][] = 'SyslogHooks::onUserLogoutComplete';
 56+$wgHooks['WatchArticleComplete'][] = 'SyslogHooks::onWatchArticleComplete';
Index: trunk/extensions/Syslog/SyslogHooks.php
@@ -0,0 +1,127 @@
 2+<?php
 3+
 4+/**
 5+ * Hooks for Syslog extension
 6+ *
 7+ * This program is free software; you can redistribute it and/or modify
 8+ * it under the terms of the GNU General Public License as published by
 9+ * the Free Software Foundation; either version 2 of the License, or
 10+ * (at your option) any later version.
 11+ *
 12+ * This program is distributed in the hope that it will be useful,
 13+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15+ * GNU General Public License for more details.
 16+ *
 17+ * You should have received a copy of the GNU General Public License
 18+ * along with this program; if not, write to the Free Software
 19+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 20+ */
 21+
 22+class SyslogHooks {
 23+
 24+ /**
 25+ * Log a message in the syslog
 26+ *
 27+ * @param $message string
 28+ */
 29+ private static function log( $message ) {
 30+ static $init = false;
 31+
 32+ if ( !$init ) {
 33+ $init = true;
 34+
 35+ global $wgSyslogIdentity, $wgSyslogFacility, $wgHooks;
 36+
 37+ openlog( $wgSyslogIdentity ? $wgSyslogIdentity : wfWikiID(), LOG_ODELAY | LOG_PID, $wgSyslogFacility);
 38+ }
 39+
 40+ syslog( LOG_NOTICE, $message );
 41+ }
 42+
 43+ /**
 44+ * Hook for article deletion
 45+ */
 46+ public static function onArticleDeleteComplete( $article, $user, $reason ) {
 47+ $userName = $user->getName();
 48+ $pageName = $article->getTitle()->getPrefixedText();
 49+ self::log( "User '{$userName}' deleted '{$pageName}' for '{$reason}'" );
 50+ return true;
 51+ }
 52+
 53+ /**
 54+ * Hook for article protection
 55+ */
 56+ public static function onArticleProtectComplete( $article, $user, $protect, $reason ) {
 57+ $userName = $user->getName();
 58+ $pageName = $article->getTitle()->getPrefixedText();
 59+ $action = count( $protect ) ? 'protected' : 'unprotected';
 60+ self::log( "User '{$userName}' {$action} '{$pageName}' for '{$reason}'" );
 61+ return true;
 62+ }
 63+
 64+ /**
 65+ * Hook for article save
 66+ */
 67+ public static function onArticleSaveComplete( $article, $user, $text, $summary ) {
 68+ $userName = $user->getName();
 69+ $pageName = $article->getTitle()->getPrefixedText();
 70+ self::log( "User '{$userName}' saved '{$pageName}' with comment '{$summary}'" );
 71+ return true;
 72+ }
 73+
 74+ /**
 75+ * Hook for IP & user blocks
 76+ */
 77+ public static function onBlockIpComplete( $block, $user ) {
 78+ $userName = $user->getName();
 79+ $target = $block->getTarget();
 80+ $target = is_object( $target ) ? $target->getName() : $target;
 81+ self::log( "User '{$userName}' blocked '{$target}' for '{$block->mReason}' until '{$block->mExpiry}'" );
 82+ return true;
 83+ }
 84+
 85+ /**
 86+ * Hook for Special:Emailuser
 87+ */
 88+ public static function onEmailUserComplete( $to, $from, $subject, $text ) {
 89+ self::log( "Email sent from '$from' to '$to' with subject '$subject'" );
 90+ return true;
 91+ }
 92+
 93+ /**
 94+ * Hook for page unwatching
 95+ */
 96+ public static function onUnwatchArticleComplete( $user, $article ) {
 97+ $userName = $user->getName();
 98+ $pageName = $article->getTitle()->getPrefixedText();
 99+ self::log( "User '{$userName}' stopped watching '{$pageName}'" );
 100+ return true;
 101+ }
 102+
 103+ /**
 104+ * Hook for login
 105+ */
 106+ public static function onUserLoginComplete( $user ) {
 107+ self::log( "User '" . $user->getName() . "' logged in" );
 108+ return true;
 109+ }
 110+
 111+ /**
 112+ * Hook for logout
 113+ */
 114+ public static function onUserLogoutComplete( $user, $inject_html, $oldName ) {
 115+ self::log( "User '" . $oldName . "' logged out" );
 116+ return true;
 117+ }
 118+
 119+ /**
 120+ * Hook for watch
 121+ */
 122+ public static function onWatchArticleComplete( $user, $article ) {
 123+ $userName = $user->getName();
 124+ $pageName = $article->getTitle()->getPrefixedText();
 125+ self::log( "User '{$userName}' started watching '{$pageName}'" );
 126+ return true;
 127+ }
 128+}
Property changes on: trunk/extensions/Syslog/SyslogHooks.php
___________________________________________________________________
Added: svn:eol-style
1129 + native

Status & tagging log