r13861 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r13860‎ | r13861 | r13862 >
Date:16:05, 25 April 2006
Author:robchurch
Status:old
Tags:
Comment:
* Rewrite using classes
* Add the "uboverride" permission
Modified paths:
  • /trunk/extensions/UsernameBlacklist/UsernameBlacklist (modified) (history)
  • /trunk/extensions/UsernameBlacklist/UsernameBlacklist.php (modified) (history)

Diff [purge]

Index: trunk/extensions/UsernameBlacklist/UsernameBlacklist
@@ -1,6 +1,6 @@
22 USERNAME BLACKLIST EXTENSION
33
4 - Version 1.1
 4+ Version 1.2
55 � 2006 Rob Church
66
77 This is free software licensed under the GNU General Public License. Please
@@ -25,6 +25,10 @@
2626 maintain a "blacklist" of usernames which cannot be registered. This blacklist
2727 is checked against and enforced during new account creation.
2828
 29+Privileged users are permitted to override the blacklist when creating accounts
 30+manually. By default, this permission is available to sysops, but can be restricted
 31+further or removed to prevent any override.
 32+
2933 The blacklist accepts simple text and more powerful regular expressions, for
3034 matching against particular patterns of usernames.
3135
@@ -91,6 +95,9 @@
9296 1.1
9397 Make extension compatible with MediaWiki 1.5.8
9498 Allow commenting out lines in the blacklist with #
 99+1.2
 100+ Use a class based code structure and rewrite some code to scrape up milliseconds
 101+ Allow users with the "uboverride" permission to pass the blacklist (manual acct. creation)
95102
96103 == 8. Thanks... ==
97104
Index: trunk/extensions/UsernameBlacklist/UsernameBlacklist.php
@@ -1,4 +1,4 @@
2 -<?php
 2+<?php
33
44 /**
55 * Extension to provide a global "bad username" list
@@ -6,68 +6,116 @@
77 * @author Rob Church <robchur@gmail.com>
88 * @package MediaWiki
99 * @subpackage Extensions
10 - * @copyright 2006 Rob Church
 10+ * @copyright © 2006 Rob Church
1111 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0
1212 */
1313
1414 if( defined( 'MEDIAWIKI' ) ) {
1515
16 - $wgExtensionFunctions[] = 'UsernameBlacklist_Init';
 16+ $wgExtensionFunctions[] = 'efUsernameBlacklistSetup';
1717 $wgExtensionCredits['other'][] = array( 'name' => 'Username Blacklist', 'author' => 'Rob Church', 'url' => 'http://meta.wikimedia.org/wiki/Username_Blacklist' );
1818
 19+ $wgAvailableRights[] = 'uboverride';
 20+ $wgGroupPermissions['sysop']['uboverride'] = true;
 21+
1922 /**
20 - * Constructor
21 - */
22 - function UsernameBlacklist_Init() {
 23+ * Register the extension
 24+ */
 25+ function efUsernameBlacklistSetup() {
2326 global $wgMessageCache, $wgHooks;
24 - $wgHooks['AbortNewAccount'][] = 'UsernameBlacklist_Hook';
 27+ $wgHooks['AbortNewAccount'][] = 'efUsernameBlacklist';
2528 $wgMessageCache->addMessage( 'blacklistedusername', 'Blacklisted username' );
26 - $wgMessageCache->addMessage( 'blacklistedusernametext', 'The username you have chosen matches the [[MediaWiki:Usernameblacklist|list of blacklisted usernames]].' );
 29+ $wgMessageCache->addMessage( 'blacklistedusernametext', 'The username you have chosen matches the [[MediaWiki:Usernameblacklist|list of blacklisted usernames]]. Please choose another.' );
2730 }
2831
2932 /**
30 - * Hooked function used to check the username against a blacklist.
31 - * Bring an error page if there is any match.
32 - *
33 - * @return boolean false if username is blacklisted.
 33+ * Perform the check
 34+ * @param $user User to be checked
 35+ * @return bool
3436 */
35 - function UsernameBlacklist_Hook( $user ) {
36 - global $wgOut;
37 - $username = $user->getName();
38 - $blacklist = wfMsg( 'usernameblacklist' );
39 - if( $blacklist != '&lt;usernameblacklist&gt;' ) {
40 - $list = explode( "\n", $blacklist );
41 - foreach( $list as $item ) {
42 - $item = UsernameBlacklist_Trim( $item );
43 - if( $item ) {
44 - $regex = '/' . $item . '/';
45 - if( preg_match( $regex, $username ) > 0 ) {
46 - $rt_title = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
47 - $wgOut->errorPage( 'blacklistedusername', 'blacklistedusernametext' );
48 - $wgOut->returnToMain( false, $rt_title->getPrefixedText() );
49 - return( false );
50 - }
 37+ function efUsernameBlacklist( &$user ) {
 38+ global $wgUser;
 39+ $blackList =& UsernameBlacklist::fetch();
 40+ if( $blackList->match( $user->getName() ) && !$wgUser->isAllowed( 'uboverride' ) ) {
 41+ global $wgOut;
 42+ $returnTitle = Title::makeTitle( NS_SPECIAL, 'Userlogin' );
 43+ $wgOut->errorPage( 'blacklistedusername', 'blacklistedusernametext' );
 44+ $wgOut->returnToMain( false, $returnTitle->getPrefixedText() );
 45+ return false;
 46+ } else {
 47+ return true;
 48+ }
 49+ }
 50+
 51+ class UsernameBlacklist {
 52+
 53+ var $regex;
 54+
 55+ /**
 56+ * Trim leading spaces and asterisks from the text
 57+ * @param $text Text to trim
 58+ * @return string
 59+ */
 60+ function transform( $text ) {
 61+ return trim( $text, ' *' );
 62+ }
 63+
 64+ /**
 65+ * Is the supplied text a comment?
 66+ * @param $text Text to check
 67+ * @return bool
 68+ */
 69+ function isComment( $text ) {
 70+ return substr( $this->transform( $text ), 0, 1 ) == '#';
 71+ }
 72+
 73+ /**
 74+ * Return a regular expression representing the blacklist
 75+ * @return string
 76+ */
 77+ function buildBlacklist() {
 78+ $blacklist = wfMsg( 'usernameblacklist' );
 79+ if( $blacklist != '&lt;usernameblacklist&gt;' ) {
 80+ $lines = explode( "\n", $blacklist );
 81+ foreach( $lines as $line ) {
 82+ if( !$this->isComment( $line ) )
 83+ $groups[] = $this->transform( $line );
5184 }
 85+ return count( $groups ) ? '/(' . implode( '|', $groups ) . ')/' : false;
 86+ } else {
 87+ return false;
5288 }
53 - return( true );
54 - } else {
55 - return( true );
5689 }
57 - }
 90+
 91+ /**
 92+ * Match a username against the blacklist
 93+ * @param $username Username to check
 94+ * @return bool
 95+ */
 96+ function match( $username ) {
 97+ return $this->regex ? preg_match( $this->regex, $username ) : false;
 98+ }
 99+
 100+ /**
 101+ * Constructor
 102+ * Prepare the regular expression
 103+ */
 104+ function UsernameBlacklist() {
 105+ $this->regex = $this->buildBlacklist();
 106+ }
58107
59 - /**
60 - * Remove occurences of ' ' or '*' at the beginning of a string
61 - * and check for commented lines
62 - *
63 - * @param string $text A text to trim.
64 - * @return string The trimmed text.
65 - */
66 - function UsernameBlacklist_Trim( $text ) {
67 - while( ( substr( $text, 0, 1 ) == '*' ) || ( substr( $text, 0, 1 ) == ' ' ) ) {
68 - $text = substr( $text, 1, strlen( $text ) - 1 );
 108+ /**
 109+ * Fetch an instance of the blacklist class
 110+ * @return UsernameBlacklist
 111+ */
 112+ function fetch() {
 113+ static $blackList = false;
 114+ if( !$blackList )
 115+ $blackList = new UsernameBlacklist();
 116+ return $blackList;
69117 }
70 - return( substr( $text, 0, 1 ) == '#' ? false : $text );
71 - }
 118+
 119+ }
72120
73121 } else {
74122 die( 'This file is an extension to the MediaWiki package, and cannot be executed separately.' );

Status & tagging log