r76344 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76343‎ | r76344 | r76345 >
Date:21:55, 8 November 2010
Author:vasilievvv
Status:resolved (Comments)
Tags:
Comment:
Title blacklist:
* (bug 22141) Introduce a separate user right for overriding title blacklist on account creations only
* Some minor refactorings
Modified paths:
  • /trunk/extensions/TitleBlacklist/TitleBlacklist.hooks.php (modified) (history)
  • /trunk/extensions/TitleBlacklist/TitleBlacklist.i18n.php (modified) (history)
  • /trunk/extensions/TitleBlacklist/TitleBlacklist.list.php (modified) (history)
  • /trunk/extensions/TitleBlacklist/TitleBlacklist.php (modified) (history)

Diff [purge]

Index: trunk/extensions/TitleBlacklist/TitleBlacklist.i18n.php
@@ -25,7 +25,9 @@
2626 It matches the following blacklist entry: <code>$1</code>',
2727 'titleblacklist-invalid' => 'The following {{PLURAL:$1|line|lines}} in the title blacklist {{PLURAL:$1|is|are}} invalid;
2828 please correct {{PLURAL:$1|it|them}} before saving:',
 29+ 'titleblacklist-override' => 'Ignore the blacklist',
2930 'right-tboverride' => 'Override the title blacklist',
 31+ 'right-tboverride-account' => 'Override the username blacklist',
3032 );
3133
3234 /** Message documentation (Message documentation)
Index: trunk/extensions/TitleBlacklist/TitleBlacklist.php
@@ -12,7 +12,7 @@
1313 $wgExtensionCredits['other'][] = array(
1414 'path' => __FILE__,
1515 'name' => 'Title Blacklist',
16 - 'author' => array( 'VasilievVV', 'Fran Rogers' ),
 16+ 'author' => array( 'Victor Vasiliev', 'Fran Rogers' ),
1717 'version' => '1.4.2',
1818 'url' => 'http://www.mediawiki.org/wiki/Extension:Title_Blacklist',
1919 'descriptionmsg' => 'titleblacklist-desc',
@@ -40,7 +40,8 @@
4141 'warningexpiry' => 600,
4242 );
4343
44 -$wgAvailableRights[] = 'tboverride';
 44+$wgAvailableRights[] = 'tboverride'; // Implies tboverride-account
 45+$wgAvailableRights[] = 'tboverride-account'; // For account creation
4546 $wgGroupPermissions['sysop']['tboverride'] = true;
4647
4748 $wgHooks['getUserPermissionsErrorsExpensive'][] = 'TitleBlacklistHooks::userCan';
@@ -49,6 +50,7 @@
5051 $wgHooks['CentralAuthAutoCreate'][] = 'TitleBlacklistHooks::centralAuthAutoCreate';
5152 $wgHooks['EditFilter'][] = 'TitleBlacklistHooks::validateBlacklist';
5253 $wgHooks['ArticleSaveComplete'][] = 'TitleBlacklistHooks::clearBlacklist';
 54+$wgHooks['UserCreateForm'][] = 'TitleBlacklistHooks::addOverrideCheckbox';
5355
5456 /**
5557 * Initialize the title blacklist
Index: trunk/extensions/TitleBlacklist/TitleBlacklist.hooks.php
@@ -1,8 +1,8 @@
22 <?php
33 /**
44 * Hooks for Title Blacklist
5 - * @author VasilievVV
6 - * @copyright © 2007 VasilievVV
 5+ * @author Victor Vasiliev
 6+ * @copyright © 2007-2010 Victor Vasiliev et al
77 * @license GNU General Public License 2.0 or later
88 */
99
@@ -17,12 +17,9 @@
1818 global $wgTitleBlacklist;
1919 if( $action == 'create' || $action == 'edit' || $action == 'upload' ) {
2020 efInitTitleBlacklist();
21 - $blacklisted = $wgTitleBlacklist->isBlacklisted( $title, $action );
 21+ $blacklisted = $wgTitleBlacklist->userCannot( $title, $user, $action );
2222 if( $blacklisted instanceof TitleBlacklistEntry ) {
23 - $message = $blacklisted->getCustomMessage();
24 - if( is_null( $message ) )
25 - $message = 'titleblacklist-forbidden-edit';
26 - $result = array( $message,
 23+ $result = array( $blacklisted->getErrorMessage( 'edit' ),
2724 htmlspecialchars( $blacklisted->getRaw() ),
2825 $title->getFullText() );
2926 return false;
@@ -35,14 +32,11 @@
3633 public static function abortMove( $old, $nt, $user, &$err ) {
3734 global $wgTitleBlacklist;
3835 efInitTitleBlacklist();
39 - $blacklisted = $wgTitleBlacklist->isBlacklisted( $nt, 'move' );
 36+ $blacklisted = $wgTitleBlacklist->userCannot( $nt, $user, 'move' );
4037 if( !$blacklisted )
41 - $blacklisted = $wgTitleBlacklist->isBlacklisted( $old, 'edit' );
 38+ $blacklisted = $wgTitleBlacklist->userCannot( $old, $user, 'edit' );
4239 if( $blacklisted instanceof TitleBlacklistEntry ) {
43 - $message = $blacklisted->getCustomMessage();
44 - if( is_null( $message ) )
45 - $message = 'titleblacklist-forbidden-move';
46 - $err = wfMsgWikiHtml( $message,
 40+ $err = wfMsgWikiHtml( $blacklisted->getErrorMessage( 'move' ),
4741 htmlspecialchars( $blacklisted->getRaw() ),
4842 htmlspecialchars( $old->getFullText() ),
4943 htmlspecialchars( $nt->getFullText() ) );
@@ -59,17 +53,13 @@
6054 *
6155 * @return bool Acceptable
6256 */
63 - private static function acceptNewUserName( $userName, &$err ) {
64 - global $wgTitleBlacklist;
 57+ private static function acceptNewUserName( $userName, &$err, $override = true ) {
 58+ global $wgTitleBlacklist, $wgUser;
6559 efInitTitleBlacklist();
6660 $title = Title::makeTitleSafe( NS_USER, $userName );
67 - $blacklisted = $wgTitleBlacklist->isBlacklisted( $title, 'new-account' );
68 - if( !( $blacklisted instanceof TitleBlacklistEntry ) )
69 - $blacklisted = $wgTitleBlacklist->isBlacklisted( $title, 'create' );
 61+ $blacklisted = $wgTitleBlacklist->userCannot( $title, $wgUser, 'new-account', $override );
7062 if( $blacklisted instanceof TitleBlacklistEntry ) {
71 - $message = $blacklisted->getCustomMessage();
72 - if ( is_null( $message ) )
73 - $message = 'titleblacklist-forbidden-new-account';
 63+ $message = $blacklisted->getErrorMessage( 'new-account' );
7464 $err = wfMsgWikiHtml( $message, $blacklisted->getRaw(), $userName );
7565 return false;
7666 }
@@ -78,10 +68,9 @@
7969
8070 /** AbortNewAccount hook */
8171 public static function abortNewAccount( $user, &$message ) {
82 - global $wgUser;
83 - if( $wgUser->isAllowed( 'tboverride' ) )
84 - return true;
85 - return self::acceptNewUserName( $user->getName(), $message );
 72+ global $wgUser, $wgRequest;
 73+ $override = $wgRequest->getCheck( 'wpIgnoreTitleBlacklist' );
 74+ return self::acceptNewUserName( $user->getName(), $message, $override );
8675 }
8776
8877 /** CentralAuthAutoCreate hook */
@@ -92,7 +81,7 @@
9382
9483 /** EditFilter hook */
9584 public static function validateBlacklist( $editor, $text, $section, $error ) {
96 - global $wgTitleBlacklist;
 85+ global $wgTitleBlacklist, $wgUser;
9786 efInitTitleBlacklist();
9887 $title = $editor->mTitle;
9988 if( $title->getNamespace() == NS_MEDIAWIKI && $title->getDBkey() == 'Titleblacklist' ) {
@@ -118,7 +107,7 @@
119108 # Block redirects to nonexistent blacklisted titles
120109 $retitle = Title::newFromRedirect( $text );
121110 if( $retitle !== null && !$retitle->exists() ) {
122 - $blacklisted = $wgTitleBlacklist->isBlacklisted( $retitle, 'create' );
 111+ $blacklisted = $wgTitleBlacklist->userCannot( $retitle, $wgUser, 'create' );
123112 if( $blacklisted instanceof TitleBlacklistEntry ) {
124113 $error = Html::openElement( 'div', array( 'class' => 'errorbox' ) ) .
125114 wfMsg( 'titleblacklist-forbidden-edit',
@@ -146,4 +135,15 @@
147136 }
148137 return true;
149138 }
 139+
 140+ /** UserCreateForm hook based on the one from AntiSpoof extension */
 141+ public static function addOverrideCheckbox( &$template ) {
 142+ global $wgRequest, $wgUser;
 143+
 144+ if ( TitleBlacklist::userCanOverride( 'new-account' ) )
 145+ $template->addInputItem( 'wpIgnoreTitleBlacklist',
 146+ $wgRequest->getCheck( 'wpIgnoreTitleBlacklist' ),
 147+ 'checkbox', 'titleblacklist-override' );
 148+ return true;
 149+ }
150150 }
Index: trunk/extensions/TitleBlacklist/TitleBlacklist.list.php
@@ -1,8 +1,8 @@
22 <?php
33 /**
44 * Title Blacklist class
5 - * @author VasilievVV
6 - * @copyright © 2007 VasilievVV
 5+ * @author Victor Vasiliev
 6+ * @copyright © 2007-2010 Victor Vasiliev et al
77 * @license GNU General Public License 2.0 or later
88 * @file
99 */
@@ -129,22 +129,39 @@
130130 }
131131
132132 /**
133 - * Check whether the blacklist restricts the current User from
 133+ * Check whether the blacklist restricts giver nuser
134134 * performing a specific action on the given Title
135135 *
136136 * @param $title Title to check
 137+ * @param $user User to check
137138 * @param $action Action to check; 'edit' if unspecified
 139+ * @param $override If set to true, overrides work
138140 * @return The corresponding TitleBlacklistEntry if blacklisted;
139141 * otherwise FALSE
140142 */
 143+ public function userCannot( $title, $user, $action = 'edit', $override = true ) {
 144+ if( $override && self::userCanOverride( $action ) )
 145+ return false;
 146+ else
 147+ return $this->isBlacklisted( $title, $action );
 148+ }
 149+
 150+ /**
 151+ * Check whether the blacklist restricts
 152+ * performing a specific action on the given Title
 153+ *
 154+ * @param $title Title to check
 155+ * @param $action Action to check; 'edit' if unspecified
 156+ * @return The corresponding TitleBlacklistEntry if blacklisted;
 157+ * otherwise FALSE
 158+ */
141159 public function isBlacklisted( $title, $action = 'edit' ) {
142 - global $wgUser;
143160 if( !($title instanceof Title) ) {
144161 $title = Title::newFromText( $title );
145162 }
146163 $blacklist = $this->getBlacklist();
147164 foreach ( $blacklist as $item ) {
148 - if( !$item->userCan( $title, $wgUser, $action ) ) {
 165+ if( !$item->matches( $title, $action ) ) {
149166 if( $this->isWhitelisted( $title, $action ) ) {
150167 return false;
151168 }
@@ -169,7 +186,7 @@
170187 }
171188 $whitelist = $this->getWhitelist();
172189 foreach( $whitelist as $item ) {
173 - if( !$item->userCan( $title, $wgUser, $action ) ) {
 190+ if( !$item->matches( $title, $action ) ) {
174191 return true;
175192 }
176193 }
@@ -245,6 +262,17 @@
246263 }
247264 return $badEntries;
248265 }
 266+
 267+ /**
 268+ * Inidcates whether user can override blacklist on certain action.
 269+ *
 270+ * @param $action Action
 271+ */
 272+ public static function userCanOverride( $action ) {
 273+ global $wgUser;
 274+ return $wgUser->isAllowed( 'tboverride' ) ||
 275+ ( $action == 'new-account' && $wgUser->isAllowed( 'tboverride-account' ) );
 276+ }
249277 }
250278
251279
@@ -273,18 +301,14 @@
274302 }
275303
276304 /**
277 - * Check whether the specified User can perform the specified action
 305+ * Check whether a user can perform the specified action
278306 * on the specified Title
279307 *
280308 * @param $title Title to check
281 - * @param $user User to check
282309 * @param $action %Action to check
283310 * @return TRUE if the user can; otherwise FALSE
284311 */
285 - public function userCan( $title, $user, $action ) {
286 - if( $user->isAllowed( 'tboverride' ) ) {
287 - return true;
288 - }
 312+ public function matches( $title, $action ) {
289313 wfSuppressWarnings();
290314 $match = preg_match( "/^(?:{$this->mRegex})$/us" . ( isset( $this->mParams['casesensitive'] ) ? '' : 'i' ), $title->getFullText() );
291315 wfRestoreWarnings();
@@ -419,6 +443,18 @@
420444 * @param $v New version to set
421445 */
422446 public function setFormatVersion( $v ) { $this->mFormatVersion = $v; }
 447+
 448+ /**
 449+ * Return the error message name for the blacklist entry.
 450+ *
 451+ * @param $operation Operation name (as in titleblacklist-forbidden message name)
 452+ *
 453+ * @returns The error message name
 454+ */
 455+ public function getErrorMessage( $operation ) {
 456+ $message = $this->getCustomMessage();
 457+ return $message ? $message : "titleblacklist-forbidden-{$operation}";
 458+ }
423459 }
424460
425 -//@}
\ No newline at end of file
 461+//@}

Follow-up revisions

RevisionCommit summaryAuthorDate
r817511.17wmf1: Superficially fix fatal caused by r76344catrope15:26, 8 February 2011
r84575Followup r76344, fix another inversed logic...reedy22:30, 22 March 2011
r97794Fix for r76344: you can't access $wgUser during the CentralAuthAutoCreate hoo...tstarling06:10, 22 September 2011

Comments

#Comment by Catrope (talk | contribs)   15:28, 8 February 2011
-	public function userCan( $title, $user, $action ) {
-		if( $user->isAllowed( 'tboverride' ) ) {
-			return true;
-		}
+	public function matches( $title, $action ) {

This causes a fatal because $user is used again in the function ( if( isset( $this->mParams['autoconfirmed'] ) && $user->isAllowed( 'autoconfirmed' ) ) {). Superficially fixed this in 1.17wmf1, but this needs a proper fix.

#Comment by Aaron Schulz (talk | contribs)   05:33, 22 September 2011

$wgUser not used anymore in abortNewAccount().

#Comment by Tim Starling (talk | contribs)   06:00, 22 September 2011

Was the $user parameter to TitleBlacklist::userCannot() meant to be used for something?

Status & tagging log