r28982 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r28981‎ | r28982 | r28983 >
Date:04:23, 29 December 2007
Author:brion
Status:old
Tags:
Comment:
* Use UTF-8 option on regex matches, not just validation. Fixes case-insensitivity and character class matches for non-ASCII chars.
* Invalidate cached blacklist after save completion of MediaWiki:Titleblacklist rather than during edit filtering. Should help avoid race conditions which would leave you with the old version cached.
* Some minor whitespace & style cleanup
* Some output escaping cleanup
Modified paths:
  • /trunk/extensions/TitleBlacklist/TitleBlacklist.hooks.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.php
@@ -8,7 +8,7 @@
99 'author' => 'VasilievVV',
1010 'version' => '1.4',
1111 'url' => 'http://www.mediawiki.org/wiki/Extension:Title_Blacklist',
12 - 'description' => 'Allows to forbide creation of pages with specified titles'
 12+ 'description' => 'Allows to forbid creation of pages with specified titles'
1313 );
1414
1515 $wgExtensionMessagesFiles['TitleBlacklist'] = dirname( __FILE__ ) . '/TitleBlacklist.i18n.php';
@@ -18,10 +18,10 @@
1919 $wgExtensionFunctions[] = 'efInitTitleBlacklist';
2020
2121 // Sources of TitleBlacklist
22 -define( 'TBLSRC_MSG', 0 ); //For internal usage
23 -define( 'TBLSRC_LOCALPAGE', 1 ); //Local wiki page
24 -define( 'TBLSRC_URL', 2 ); //Load blacklist from URL
25 -define( 'TBLSRC_FILE', 3 ); //Load from file
 22+define( 'TBLSRC_MSG', 0 ); // For internal usage
 23+define( 'TBLSRC_LOCALPAGE', 1 ); // Local wiki page
 24+define( 'TBLSRC_URL', 2 ); // Load blacklist from URL
 25+define( 'TBLSRC_FILE', 3 ); // Load from file
2626 $wgTitleBlacklistSources = array();
2727
2828 $wgTitleBlacklistCaching = array(
@@ -42,8 +42,9 @@
4343
4444 function efSetupTitleBlacklistHooks() {
4545 global $wgHooks;
46 - $wgHooks['getUserPermissionsErrors'][] = 'TitleBlacklistHooks::userCan';
 46+ $wgHooks['getUserPermissionsErrors'][] = 'TitleBlacklistHooks::userCan';
4747 $wgHooks['AbortMove'][] = 'TitleBlacklistHooks::abortMove';
4848 $wgHooks['UploadVerification'][] = 'TitleBlacklistHooks::verifyUpload';
4949 $wgHooks['EditFilter'][] = 'TitleBlacklistHooks::validateBlacklist';
 50+ $wgHooks['ArticleSaveComplete'][] = 'TitleBlacklistHooks::clearBlacklist';
5051 }
Index: trunk/extensions/TitleBlacklist/TitleBlacklist.hooks.php
@@ -7,27 +7,25 @@
88 * @subpackage Extensions
99 * @author VasilievVV
1010 * @copyright � 2007 VasilievVV
11 - * @licence GNU General Public Licence 2.0 or later
 11+ * @license GNU General Public License 2.0 or later
1212 */
1313
1414 class TitleBlacklistHooks {
15 - public static function userCan( $title, $user, $action, &$result )
16 - {
 15+ public static function userCan( $title, $user, $action, &$result ) {
1716 global $wgTitleBlacklist;
18 - if ( $action != 'create' && $action != 'edit' ) {
19 - $result = true;
20 - return $result;
 17+ if( $action == 'create' || $action == 'edit' ) {
 18+ $blacklisted = $wgTitleBlacklist->isBlacklisted( $title, $action );
 19+ if( $blacklisted instanceof TitleBlacklistEntry ) {
 20+ $message = $blacklisted->getCustomMessage();
 21+ if( is_null( $message ) )
 22+ $message = 'titleblacklist-forbidden-edit';
 23+ $result = array( $message,
 24+ htmlspecialchars( $blacklisted->getRaw() ),
 25+ $title->getFullText() );
 26+ return false;
 27+ }
2128 }
22 - $blacklisted = $wgTitleBlacklist->isBlacklisted( $title, $action );
23 - if( $blacklisted instanceof TitleBlacklistEntry ) {
24 - $message = $blacklisted->getCustomMessage();
25 - if( is_null( $message ) )
26 - $message = 'titleblacklist-forbidden-edit';
27 - $result = array( $message, htmlspecialchars( $blacklisted->getRaw() ), $title->getFullText() );
28 - return false;
29 - }
30 -
31 - return $result = true;
 29+ return true;
3230 }
3331
3432 public static function abortMove( $old, $nt, $user, &$err ) {
@@ -39,7 +37,10 @@
4038 $message = $blacklisted->getCustomMessage();
4139 if( is_null( $message ) )
4240 $message = 'titleblacklist-forbidden-move';
43 - $err = wfMsgWikiHtml( $message, htmlspecialchars( $blacklisted->getRaw() ), $nt->getFullText(), $old->getFullText() );
 41+ $err = wfMsgWikiHtml( $message,
 42+ htmlspecialchars( $blacklisted->getRaw() ),
 43+ htmlspecialchars( $nt->getFullText() ),
 44+ htmlspecialchars( $old->getFullText() ) );
4445 return false;
4546 }
4647 return true;
@@ -67,7 +68,6 @@
6869 $bl = $wgTitleBlacklist->parseBlacklist( $text );
6970 $ok = $wgTitleBlacklist->validate( $bl );
7071 if( count( $ok ) == 0 ) {
71 - $wgTitleBlacklist->invalidate();
7272 return true;
7373 }
7474
@@ -79,6 +79,18 @@
8080 $errlines .
8181 "</div>\n" .
8282 "<br clear='all' />\n";
 83+
 84+ // $error will be displayed by the edit class
8385 return true;
8486 }
 87+
 88+ public static function clearBlacklist( &$article, &$user,
 89+ $text, $summary, $isminor, $iswatch, $section ) {
 90+ $title = $article->getTitle();
 91+ if( $title->getNamespace() == NS_MEDIAWIKI && $title->getDbKey() == 'Titleblacklist' ) {
 92+ global $wgTitleBlacklist;
 93+ $wgTitleBlacklist->invalidate();
 94+ }
 95+ return true;
 96+ }
8597 }
\ No newline at end of file
Index: trunk/extensions/TitleBlacklist/TitleBlacklist.list.php
@@ -156,7 +156,7 @@
157157 return true;
158158 }
159159 wfSuppressWarnings();
160 - $match = preg_match( "/^{$this->mRegex}$/s" . ( isset( $this->mParams['casesensitive'] ) ? '' : 'i' ), $title->getFullText() );
 160+ $match = preg_match( "/^{$this->mRegex}$/us" . ( isset( $this->mParams['casesensitive'] ) ? '' : 'i' ), $title->getFullText() );
161161 wfRestoreWarnings();
162162 if( $match ) {
163163 if( isset( $this->mParams['autoconfirmed'] ) && $user->isAllowed( 'autoconfirmed' ) ) {
@@ -171,18 +171,18 @@
172172 }
173173
174174 public static function newFromString( $line ) {
175 - $raw = $line; #Keep line for raw data
 175+ $raw = $line; // Keep line for raw data
176176 $regex = "";
177177 $options = array();
178 - //Strip comments
 178+ // Strip comments
179179 $line = preg_replace( "/^\\s*([^#]*)\\s*((.*)?)$/", "\\1", $line );
180180 $line = trim( $line );
181 - //Parse the rest of message
 181+ // Parse the rest of message
182182 preg_match( '/^(.*?)(\s*<(.*)>)?$/', $line, $pockets );
183183 @list( $full, $regex, $null, $opts_str ) = $pockets;
184184 $regex = trim( $regex );
185185 $opts_str = trim( $opts_str );
186 - //Parse opts
 186+ // Parse opts
187187 $opts = preg_split( '/\s*\|\s*/', $opts_str );
188188 foreach( $opts as $opt ) {
189189 $opt2 = strtolower( $opt );
@@ -199,15 +199,15 @@
200200 $options['errmsg'] = $matches[1];
201201 }
202202 }
203 - //Process magic words
 203+ // Process magic words
204204 preg_match_all( '/{{\s*([a-z]+)\s*:\s*(.+?)\s*}}/', $regex, $magicwords, PREG_SET_ORDER );
205205 foreach( $magicwords as $mword ) {
206 - global $wgParser; //Functions we're calling don't need, nevertheless let's use it
 206+ global $wgParser; // Functions we're calling don't need, nevertheless let's use it
207207 switch( strtolower( $mword[1] ) ) {
208208 case 'ns':
209209 $cpf_result = CoreParserFunctions::ns( $wgParser, $mword[2] );
210210 if( is_string( $cpf_result ) ) {
211 - $regex = str_replace( $mword[0], $cpf_result, $regex ); //All result will have the same value, so we can just use str_seplace()
 211+ $regex = str_replace( $mword[0], $cpf_result, $regex ); // All result will have the same value, so we can just use str_seplace()
212212 }
213213 break;
214214 case 'int':
@@ -217,7 +217,7 @@
218218 }
219219 }
220220 }
221 - //Return result
 221+ // Return result
222222 if( $regex ) {
223223 return new TitleBlacklistEntry( $regex, $options, $raw );
224224 } else {

Status & tagging log