r106805 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106804‎ | r106805 | r106806 >
Date:16:09, 20 December 2011
Author:reedy
Status:resolved (Comments)
Tags:
Comment:
Move hooks to own file/class
Modified paths:
  • /trunk/extensions/AntiSpoof/AntiSpoof.php (modified) (history)
  • /trunk/extensions/AntiSpoof/AntiSpoofHooks.php (added) (history)

Diff [purge]

Index: trunk/extensions/AntiSpoof/AntiSpoof.php
@@ -34,126 +34,11 @@
3535 $wgExtensionMessagesFiles['AntiSpoof'] = "$dir/AntiSpoof.i18n.php";
3636
3737 $wgAutoloadClasses['AntiSpoof'] = "$dir/AntiSpoof_body.php";
 38+$wgAutoloadClasses['AntiSpoofHooks'] = "$dir/AntiSpoofHooks.php";
3839 $wgAutoloadClasses['SpoofUser'] = "$dir/SpoofUser.php";
3940
40 -$wgHooks['LoadExtensionSchemaUpdates'][] = 'asUpdateSchema';
41 -$wgHooks['AbortNewAccount'][] = 'asAbortNewAccountHook';
42 -$wgHooks['UserCreateForm'][] = 'asUserCreateFormHook';
43 -$wgHooks['AddNewAccount'][] = 'asAddNewAccountHook';
44 -$wgHooks['RenameUserComplete'][] = 'asAddRenameUserHook';
45 -
46 -/**
47 - * @param $updater DatabaseUpdater
48 - * @return bool
49 - */
50 -function asUpdateSchema( $updater = null ) {
51 - if ( $updater === null ) {
52 - global $wgExtNewTables, $wgDBtype;
53 - $wgExtNewTables[] = array(
54 - 'spoofuser',
55 - dirname( __FILE__ ) . '/sql/patch-antispoof.' . $wgDBtype . '.sql', true );
56 - } else {
57 - $updater->addExtensionUpdate( array( 'addTable', 'spoofuser',
58 - dirname( __FILE__ ) . '/sql/patch-antispoof.' . $updater->getDB()->getType() . '.sql', true ) );
59 - }
60 - return true;
61 -}
62 -
63 -/**
64 - * Can be used to cancel user account creation
65 - *
66 - * @param $user User
67 - * @param $message string
68 - * @return bool true to continue, false to abort user creation
69 - */
70 -function asAbortNewAccountHook( $user, &$message ) {
71 - global $wgAntiSpoofAccounts, $wgUser, $wgRequest;
72 -
73 - if ( !$wgAntiSpoofAccounts ) {
74 - $mode = 'LOGGING ';
75 - $active = false;
76 - } elseif ( $wgRequest->getCheck( 'wpIgnoreAntiSpoof' ) &&
77 - $wgUser->isAllowed( 'override-antispoof' ) ) {
78 - $mode = 'OVERRIDE ';
79 - $active = false;
80 - } else {
81 - $mode = '';
82 - $active = true;
83 - }
84 -
85 - $name = $user->getName();
86 - $spoof = new SpoofUser( $name );
87 - if ( $spoof->isLegal() ) {
88 - $normalized = $spoof->getNormalized();
89 - $conflicts = $spoof->getConflicts();
90 - if ( empty( $conflicts ) ) {
91 - wfDebugLog( 'antispoof', "{$mode}PASS new account '$name' [$normalized]" );
92 - } else {
93 - wfDebugLog( 'antispoof', "{$mode}CONFLICT new account '$name' [$normalized] spoofs " . implode( ',', $conflicts ) );
94 - if ( $active ) {
95 - $numConflicts = count( $conflicts );
96 - $message = wfMsgExt( 'antispoof-conflict-top', array( 'parsemag' ), htmlspecialchars( $name ), $numConflicts );
97 - $message .= '<ul>';
98 - foreach ( $conflicts as $simUser ) {
99 - $message .= '<li>' . wfMsg( 'antispoof-conflict-item', $simUser ) . '</li>';
100 - }
101 - $message .= '</ul>' . wfMsg( 'antispoof-conflict-bottom' );
102 - return false;
103 - }
104 - }
105 - } else {
106 - $error = $spoof->getError();
107 - wfDebugLog( 'antispoof', "{$mode}ILLEGAL new account '$name' $error" );
108 - if ( $active ) {
109 - $message = wfMsg( 'antispoof-name-illegal', $name, $error );
110 - return false;
111 - }
112 - }
113 - return true;
114 -}
115 -
116 -/**
117 - * Set the ignore spoof thingie
118 - * (Manipulate the user create form)
119 - *
120 - * @param $template UsercreateTemplate
121 - * @return bool
122 - */
123 -function asUserCreateFormHook( &$template ) {
124 - global $wgRequest, $wgAntiSpoofAccounts, $wgUser;
125 -
126 - if ( $wgAntiSpoofAccounts && $wgUser->isAllowed( 'override-antispoof' ) ) {
127 - $template->addInputItem( 'wpIgnoreAntiSpoof',
128 - $wgRequest->getCheck( 'wpIgnoreAntiSpoof' ),
129 - 'checkbox', 'antispoof-ignore' );
130 - }
131 - return true;
132 -}
133 -
134 -/**
135 - * On new account creation, record the username's thing-bob.
136 - * (Called after a user account is created)
137 - *
138 - * @param $user User
139 - * @return bool
140 - */
141 -function asAddNewAccountHook( $user ) {
142 - $spoof = new SpoofUser( $user->getName() );
143 - $spoof->record();
144 - return true;
145 -}
146 -
147 -/**
148 - * On rename, remove the old entry and add the new
149 - * (After a sucessful user rename)
150 - *
151 - * @param $uid
152 - * @param $oldName string
153 - * @param $newName string
154 - * @return bool
155 - */
156 -function asAddRenameUserHook( $uid, $oldName, $newName ) {
157 - $spoof = new SpoofUser( $newName );
158 - $spoof->update( $oldName );
159 - return true;
160 -}
 41+$wgHooks['LoadExtensionSchemaUpdates'][] = 'AntiSpoofHooks::asUpdateSchema';
 42+$wgHooks['AbortNewAccount'][] = 'AntiSpoofHooks::asAbortNewAccountHook';
 43+$wgHooks['UserCreateForm'][] = 'AntiSpoofHooks::asUserCreateFormHook';
 44+$wgHooks['AddNewAccount'][] = 'AntiSpoofHooks::asAddNewAccountHook';
 45+$wgHooks['RenameUserComplete'][] = 'AntiSpoofHooks::asAddRenameUserHook';
Index: trunk/extensions/AntiSpoof/AntiSpoofHooks.php
@@ -0,0 +1,122 @@
 2+<?php
 3+if ( !defined( 'MEDIAWIKI' ) ) {
 4+ exit( 1 );
 5+}
 6+
 7+class AntiSpoofHooks {
 8+ /**
 9+ * @param $updater DatabaseUpdater
 10+ * @return bool
 11+ */
 12+ public static function asUpdateSchema( $updater = null ) {
 13+ if ( $updater === null ) {
 14+ global $wgExtNewTables, $wgDBtype;
 15+ $wgExtNewTables[] = array(
 16+ 'spoofuser',
 17+ dirname( __FILE__ ) . '/sql/patch-antispoof.' . $wgDBtype . '.sql', true );
 18+ } else {
 19+ $updater->addExtensionUpdate( array( 'addTable', 'spoofuser',
 20+ dirname( __FILE__ ) . '/sql/patch-antispoof.' . $updater->getDB()->getType() . '.sql', true ) );
 21+ }
 22+ return true;
 23+ }
 24+
 25+ /**
 26+ * Can be used to cancel user account creation
 27+ *
 28+ * @param $user User
 29+ * @param $message string
 30+ * @return bool true to continue, false to abort user creation
 31+ */
 32+ public static function asAbortNewAccountHook( $user, &$message ) {
 33+ global $wgAntiSpoofAccounts, $wgUser, $wgRequest;
 34+
 35+ if ( !$wgAntiSpoofAccounts ) {
 36+ $mode = 'LOGGING ';
 37+ $active = false;
 38+ } elseif ( $wgRequest->getCheck( 'wpIgnoreAntiSpoof' ) &&
 39+ $wgUser->isAllowed( 'override-antispoof' ) ) {
 40+ $mode = 'OVERRIDE ';
 41+ $active = false;
 42+ } else {
 43+ $mode = '';
 44+ $active = true;
 45+ }
 46+
 47+ $name = $user->getName();
 48+ $spoof = new SpoofUser( $name );
 49+ if ( $spoof->isLegal() ) {
 50+ $normalized = $spoof->getNormalized();
 51+ $conflicts = $spoof->getConflicts();
 52+ if ( empty( $conflicts ) ) {
 53+ wfDebugLog( 'antispoof', "{$mode}PASS new account '$name' [$normalized]" );
 54+ } else {
 55+ wfDebugLog( 'antispoof', "{$mode}CONFLICT new account '$name' [$normalized] spoofs " . implode( ',', $conflicts ) );
 56+ if ( $active ) {
 57+ $numConflicts = count( $conflicts );
 58+ $message = wfMsgExt( 'antispoof-conflict-top', array( 'parsemag' ), htmlspecialchars( $name ), $numConflicts );
 59+ $message .= '<ul>';
 60+ foreach ( $conflicts as $simUser ) {
 61+ $message .= '<li>' . wfMsg( 'antispoof-conflict-item', $simUser ) . '</li>';
 62+ }
 63+ $message .= '</ul>' . wfMsg( 'antispoof-conflict-bottom' );
 64+ return false;
 65+ }
 66+ }
 67+ } else {
 68+ $error = $spoof->getError();
 69+ wfDebugLog( 'antispoof', "{$mode}ILLEGAL new account '$name' $error" );
 70+ if ( $active ) {
 71+ $message = wfMsg( 'antispoof-name-illegal', $name, $error );
 72+ return false;
 73+ }
 74+ }
 75+ return true;
 76+ }
 77+
 78+ /**
 79+ * Set the ignore spoof thingie
 80+ * (Manipulate the user create form)
 81+ *
 82+ * @param $template UsercreateTemplate
 83+ * @return bool
 84+ */
 85+ public static function asUserCreateFormHook( &$template ) {
 86+ global $wgRequest, $wgAntiSpoofAccounts, $wgUser;
 87+
 88+ if ( $wgAntiSpoofAccounts && $wgUser->isAllowed( 'override-antispoof' ) ) {
 89+ $template->addInputItem( 'wpIgnoreAntiSpoof',
 90+ $wgRequest->getCheck( 'wpIgnoreAntiSpoof' ),
 91+ 'checkbox', 'antispoof-ignore' );
 92+ }
 93+ return true;
 94+ }
 95+
 96+ /**
 97+ * On new account creation, record the username's thing-bob.
 98+ * (Called after a user account is created)
 99+ *
 100+ * @param $user User
 101+ * @return bool
 102+ */
 103+ public static function asAddNewAccountHook( $user ) {
 104+ $spoof = new SpoofUser( $user->getName() );
 105+ $spoof->record();
 106+ return true;
 107+ }
 108+
 109+ /**
 110+ * On rename, remove the old entry and add the new
 111+ * (After a sucessful user rename)
 112+ *
 113+ * @param $uid
 114+ * @param $oldName string
 115+ * @param $newName string
 116+ * @return bool
 117+ */
 118+ public static function asAddRenameUserHook( $uid, $oldName, $newName ) {
 119+ $spoof = new SpoofUser( $newName );
 120+ $spoof->update( $oldName );
 121+ return true;
 122+ }
 123+}
Property changes on: trunk/extensions/AntiSpoof/AntiSpoofHooks.php
___________________________________________________________________
Added: svn:eol-style
1124 + native
Added: svn:keywords
2125 + LastChangedDate LastChangedRevision

Follow-up revisions

RevisionCommit summaryAuthorDate
r106969Followup r106805: remove useless svn:keywords propertycatrope19:32, 21 December 2011

Comments

#Comment by Nikerabbit (talk | contribs)   07:04, 21 December 2011

Are those svn:keywords used somewhere? The MW guard is not needed for files with only class level code.

Status & tagging log