| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
l |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
namespace MediaWiki\Extension\TitleBlacklist; |
| 11 |
|
| 12 |
use MediaWiki\Config\ConfigException; |
| 13 |
use MediaWiki\Extension\AntiSpoof\AntiSpoof; |
| 14 |
use MediaWiki\MediaWikiServices; |
| 15 |
use MediaWiki\Parser\CoreParserFunctions; |
| 16 |
use MediaWiki\Registration\ExtensionRegistry; |
| 17 |
use Wikimedia\AtEase\AtEase; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class TitleBlacklistEntry { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
private $mRaw; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private $mRegex; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private $mParams; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private $mFormatVersion; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private $mSource; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private function __construct( $regex, $params, $raw, $source ) { |
| 64 |
$this->mRaw = $raw; |
| 65 |
$this->mRegex = $regex; |
| 66 |
$this->mParams = $params; |
| 67 |
$this->mFormatVersion = TitleBlacklist::VERSION; |
| 68 |
$this->mSource = $source; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
private function filtersNewAccounts() { |
| 76 |
global $wgTitleBlacklistUsernameSources; |
| 77 |
|
| 78 |
if ( $wgTitleBlacklistUsernameSources === '*' ) { |
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
if ( !$wgTitleBlacklistUsernameSources ) { |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
if ( !is_array( $wgTitleBlacklistUsernameSources ) ) { |
| 87 |
throw new ConfigException( |
| 88 |
'$wgTitleBlacklistUsernameSources must be "*", false or an array' ); |
| 89 |
} |
| 90 |
|
| 91 |
return in_array( $this->mSource, $wgTitleBlacklistUsernameSources, true ); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function matches( $title, $action ) { |
| 103 |
if ( $title == '' ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
if ( $action === 'new-account' && !$this->filtersNewAccounts() ) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
if ( isset( $this->mParams['antispoof'] ) |
| 112 |
&& ExtensionRegistry::getInstance()->isLoaded( 'AntiSpoof' ) |
| 113 |
) { |
| 114 |
if ( $action === 'edit' ) { |
| 115 |
|
| 116 |
$cache = MediaWikiServices::getInstance()->getMainWANObjectCache(); |
| 117 |
$status = $cache->getWithSetCallback( |
| 118 |
$cache->makeKey( 'titleblacklist', 'normalized-unicode-status', md5( $title ) ), |
| 119 |
$cache::TTL_MONTH, |
| 120 |
static function () use ( $title ) { |
| 121 |
return AntiSpoof::checkUnicodeStringStatus( $title ); |
| 122 |
}, |
| 123 |
[ 'pcTTL' => $cache::TTL_PROC_LONG ] |
| 124 |
); |
| 125 |
} else { |
| 126 |
$status = AntiSpoof::checkUnicodeStringStatus( $title ); |
| 127 |
} |
| 128 |
|
| 129 |
if ( $status->isOK() ) { |
| 130 |
|
| 131 |
[ , $title ] = explode( ':', $status->getValue(), 2 ); |
| 132 |
} else { |
| 133 |
wfDebugLog( 'TitleBlacklist', 'AntiSpoof could not normalize "' . $title . '" ' . |
| 134 |
$status->getMessage( false, false, 'en' )->text() . '.' |
| 135 |
); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
AtEase::suppressWarnings(); |
| 140 |
|
| 141 |
$match = preg_match( |
| 142 |
"/^(?:{$this->mRegex})$/us" . ( isset( $this->mParams['casesensitive'] ) ? '' : 'i' ), |
| 143 |
$title |
| 144 |
); |
| 145 |
AtEase::restoreWarnings(); |
| 146 |
|
| 147 |
if ( $match ) { |
| 148 |
if ( isset( $this->mParams['moveonly'] ) && $action != 'move' ) { |
| 149 |
return false; |
| 150 |
} |
| 151 |
if ( isset( $this->mParams['newaccountonly'] ) && $action != 'new-account' ) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
if ( !isset( $this->mParams['noedit'] ) && $action == 'edit' ) { |
| 155 |
return false; |
| 156 |
} |
| 157 |
if ( isset( $this->mParams['reupload'] ) && $action == 'upload' ) { |
| 158 |
|
| 159 |
return false; |
| 160 |
} |
| 161 |
return true; |
| 162 |
} |
| 163 |
|
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public static function newFromString( $line, $source ) { |
| 175 |
|
| 176 |
$raw = $line; |
| 177 |
$options = []; |
| 178 |
|
| 179 |
$line = preg_replace( "/^\\s*([^#]*)\\s*((.*)?)$/", "\\1", $line ); |
| 180 |
$line = trim( $line ); |
| 181 |
|
| 182 |
if ( $line === '' ) { |
| 183 |
return null; |
| 184 |
} |
| 185 |
|
| 186 |
$pockets = []; |
| 187 |
if ( !preg_match( '/^(.*?)(\s*<([^<>]*)>)?$/', $line, $pockets ) ) { |
| 188 |
return null; |
| 189 |
} |
| 190 |
$regex = trim( $pockets[1] ); |
| 191 |
|
| 192 |
$regex = str_replace( '_', ' ', $regex ); |
| 193 |
$opts_str = trim( $pockets[3] ?? '' ); |
| 194 |
|
| 195 |
$opts = preg_split( '/\s*\|\s*/', $opts_str ); |
| 196 |
foreach ( $opts as $opt ) { |
| 197 |
$opt2 = strtolower( $opt ); |
| 198 |
if ( in_array( $opt2, [ |
| 199 |
'antispoof', |
| 200 |
'autoconfirmed', |
| 201 |
'casesensitive', |
| 202 |
'moveonly', |
| 203 |
'newaccountonly', |
| 204 |
'noedit', |
| 205 |
'reupload', |
| 206 |
] ) ) { |
| 207 |
$options[$opt2] = true; |
| 208 |
} |
| 209 |
if ( preg_match( '/errmsg\s*=\s*(.+)/i', $opt, $matches ) ) { |
| 210 |
$options['errmsg'] = $matches[1]; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
preg_match_all( '/{{\s*([a-z]+)\s*:\s*(.+?)\s*}}/', $regex, $magicwords, PREG_SET_ORDER ); |
| 215 |
foreach ( $magicwords as $mword ) { |
| 216 |
switch ( strtolower( $mword[1] ) ) { |
| 217 |
case 'ns': |
| 218 |
$cpf_result = CoreParserFunctions::ns( |
| 219 |
MediaWikiServices::getInstance()->getParser(), |
| 220 |
$mword[2] |
| 221 |
); |
| 222 |
if ( is_string( $cpf_result ) ) { |
| 223 |
|
| 224 |
$regex = str_replace( $mword[0], $cpf_result, $regex ); |
| 225 |
} |
| 226 |
break; |
| 227 |
case 'int': |
| 228 |
$cpf_result = wfMessage( $mword[2] )->inContentLanguage()->text(); |
| 229 |
if ( is_string( $cpf_result ) ) { |
| 230 |
$regex = str_replace( $mword[0], $cpf_result, $regex ); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
return $regex ? new TitleBlacklistEntry( $regex, $options, $raw, $source ) : null; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
public function getRegex() { |
| 241 |
return $this->mRegex; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public function getRaw() { |
| 248 |
return $this->mRaw; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function getParams() { |
| 255 |
return $this->mParams; |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
public function getCustomMessage() { |
| 262 |
return $this->mParams['errmsg'] ?? null; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public function getFormatVersion() { |
| 269 |
return $this->mFormatVersion; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public function setFormatVersion( $v ) { |
| 276 |
$this->mFormatVersion = $v; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public function getErrorMessage( $operation ) { |
| 287 |
$message = $this->getCustomMessage(); |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
return $message ?: "titleblacklist-forbidden-{$operation}"; |
| 292 |
} |
| 293 |
} |