MediaWiki  1.34.0
TitleBlacklistEntry.php
Go to the documentation of this file.
1 <?php
11 
24  private $mRaw;
25 
30  private $mRegex;
31 
36  private $mParams;
37 
42  private $mFormatVersion;
43 
48  private $mSource;
49 
56  private function __construct( $regex, $params, $raw, $source ) {
57  $this->mRaw = $raw;
58  $this->mRegex = $regex;
59  $this->mParams = $params;
60  $this->mFormatVersion = TitleBlacklist::VERSION;
61  $this->mSource = $source;
62  }
63 
67  private function filtersNewAccounts() {
68  global $wgTitleBlacklistUsernameSources;
69 
70  if ( $wgTitleBlacklistUsernameSources === '*' ) {
71  return true;
72  }
73 
74  if ( !$wgTitleBlacklistUsernameSources ) {
75  return false;
76  }
77 
78  if ( !is_array( $wgTitleBlacklistUsernameSources ) ) {
79  throw new Exception(
80  '$wgTitleBlacklistUsernameSources must be "*", false or an array' );
81  }
82 
83  return in_array( $this->mSource, $wgTitleBlacklistUsernameSources, true );
84  }
85 
94  public function matches( $title, $action ) {
95  if ( $title == '' ) {
96  return false;
97  }
98 
99  if ( $action === 'new-account' && !$this->filtersNewAccounts() ) {
100  return false;
101  }
102 
103  if ( isset( $this->mParams['antispoof'] )
104  && is_callable( 'AntiSpoof::checkUnicodeString' )
105  ) {
106  if ( $action === 'edit' ) {
107  // Use process cache for frequently edited pages
108  $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
109  list( $ok, $norm ) = $cache->getWithSetCallback(
110  $cache->makeKey( 'titleblacklist', 'normalized-unicode', md5( $title ) ),
111  $cache::TTL_MONTH,
112  function () use ( $title ) {
113  return AntiSpoof::checkUnicodeString( $title );
114  },
115  [ 'pcTTL' => $cache::TTL_PROC_LONG ]
116  );
117  } else {
118  list( $ok, $norm ) = AntiSpoof::checkUnicodeString( $title );
119  }
120 
121  if ( $ok === "OK" ) {
122  list( , $title ) = explode( ':', $norm, 2 );
123  } else {
124  wfDebugLog( 'TitleBlacklist', 'AntiSpoof could not normalize "' . $title . '".' );
125  }
126  }
127 
128  Wikimedia\suppressWarnings();
129  $match = preg_match(
130  "/^(?:{$this->mRegex})$/us" . ( isset( $this->mParams['casesensitive'] ) ? '' : 'i' ),
131  $title
132  );
133  Wikimedia\restoreWarnings();
134 
135  if ( $match ) {
136  if ( isset( $this->mParams['moveonly'] ) && $action != 'move' ) {
137  return false;
138  }
139  if ( isset( $this->mParams['newaccountonly'] ) && $action != 'new-account' ) {
140  return false;
141  }
142  if ( !isset( $this->mParams['noedit'] ) && $action == 'edit' ) {
143  return false;
144  }
145  if ( isset( $this->mParams['reupload'] ) && $action == 'upload' ) {
146  // Special:Upload also checks 'create' permissions when not reuploading
147  return false;
148  }
149  return true;
150  }
151 
152  return false;
153  }
154 
162  public static function newFromString( $line, $source ) {
163  $raw = $line; // Keep line for raw data
164  $options = [];
165  // Strip comments
166  $line = preg_replace( "/^\\s*([^#]*)\\s*((.*)?)$/", "\\1", $line );
167  $line = trim( $line );
168  // A blank string causes problems later on
169  if ( $line === '' ) {
170  return null;
171  }
172  // Parse the rest of message
173  $pockets = [];
174  if ( !preg_match( '/^(.*?)(\s*<([^<>]*)>)?$/', $line, $pockets ) ) {
175  return null;
176  }
177  $regex = trim( $pockets[1] );
178  $regex = str_replace( '_', ' ', $regex ); // We'll be matching against text form
179  $opts_str = isset( $pockets[3] ) ? trim( $pockets[3] ) : '';
180  // Parse opts
181  $opts = preg_split( '/\s*\|\s*/', $opts_str );
182  foreach ( $opts as $opt ) {
183  $opt2 = strtolower( $opt );
184  if ( $opt2 == 'autoconfirmed' ) {
185  $options['autoconfirmed'] = true;
186  }
187  if ( $opt2 == 'moveonly' ) {
188  $options['moveonly'] = true;
189  }
190  if ( $opt2 == 'newaccountonly' ) {
191  $options['newaccountonly'] = true;
192  }
193  if ( $opt2 == 'noedit' ) {
194  $options['noedit'] = true;
195  }
196  if ( $opt2 == 'casesensitive' ) {
197  $options['casesensitive'] = true;
198  }
199  if ( $opt2 == 'reupload' ) {
200  $options['reupload'] = true;
201  }
202  if ( preg_match( '/errmsg\s*=\s*(.+)/i', $opt, $matches ) ) {
203  $options['errmsg'] = $matches[1];
204  }
205  if ( $opt2 == 'antispoof' ) {
206  $options['antispoof'] = true;
207  }
208  }
209  // Process magic words
210  preg_match_all( '/{{\s*([a-z]+)\s*:\s*(.+?)\s*}}/', $regex, $magicwords, PREG_SET_ORDER );
211  foreach ( $magicwords as $mword ) {
212  global $wgParser; // Functions we're calling don't need, nevertheless let's use it
213  switch ( strtolower( $mword[1] ) ) {
214  case 'ns':
215  $cpf_result = CoreParserFunctions::ns( $wgParser, $mword[2] );
216  if ( is_string( $cpf_result ) ) {
217  // All result will have the same value, so we can just use str_seplace()
218  $regex = str_replace( $mword[0], $cpf_result, $regex );
219  }
220  break;
221  case 'int':
222  $cpf_result = wfMessage( $mword[2] )->inContentLanguage()->text();
223  if ( is_string( $cpf_result ) ) {
224  $regex = str_replace( $mword[0], $cpf_result, $regex );
225  }
226  }
227  }
228  // Return result
229  if ( $regex ) {
230  return new TitleBlacklistEntry( $regex, $options, $raw, $source );
231  } else {
232  return null;
233  }
234  }
235 
239  public function getRegex() {
240  return $this->mRegex;
241  }
242 
246  public function getRaw() {
247  return $this->mRaw;
248  }
249 
253  public function getParams() {
254  return $this->mParams;
255  }
256 
260  public function getCustomMessage() {
261  return $this->mParams['errmsg'] ?? null;
262  }
263 
267  public function getFormatVersion() {
268  return $this->mFormatVersion;
269  }
270 
276  public function setFormatVersion( $v ) {
277  $this->mFormatVersion = $v;
278  }
279 
287  public function getErrorMessage( $operation ) {
288  $message = $this->getCustomMessage();
289  // For grep:
290  // titleblacklist-forbidden-edit, titleblacklist-forbidden-move,
291  // titleblacklist-forbidden-upload, titleblacklist-forbidden-new-account
292  return $message ?: "titleblacklist-forbidden-{$operation}";
293  }
294 }
TitleBlacklistEntry\setFormatVersion
setFormatVersion( $v)
Set the format version.
Definition: TitleBlacklistEntry.php:276
TitleBlacklistEntry\getRaw
getRaw()
Definition: TitleBlacklistEntry.php:246
TitleBlacklistEntry\getFormatVersion
getFormatVersion()
Definition: TitleBlacklistEntry.php:267
$wgParser
$wgParser
Definition: Setup.php:892
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
TitleBlacklistEntry\getCustomMessage
getCustomMessage()
Definition: TitleBlacklistEntry.php:260
TitleBlacklistEntry\$mFormatVersion
string $mFormatVersion
Entry format version.
Definition: TitleBlacklistEntry.php:42
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
TitleBlacklistEntry\getErrorMessage
getErrorMessage( $operation)
Return the error message name for the blacklist entry.
Definition: TitleBlacklistEntry.php:287
TitleBlacklistEntry\getParams
getParams()
Definition: TitleBlacklistEntry.php:253
TitleBlacklistEntry\$mParams
array $mParams
Parameters for this entry.
Definition: TitleBlacklistEntry.php:36
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:1007
TitleBlacklistEntry\filtersNewAccounts
filtersNewAccounts()
Returns whether this entry is capable of filtering new accounts.
Definition: TitleBlacklistEntry.php:67
TitleBlacklistEntry\getRegex
getRegex()
Definition: TitleBlacklistEntry.php:239
TitleBlacklistEntry\$mRegex
string $mRegex
Regular expression to match.
Definition: TitleBlacklistEntry.php:30
$matches
$matches
Definition: NoLocalSettings.php:24
TitleBlacklistEntry\matches
matches( $title, $action)
Check whether a user can perform the specified action on the specified Title.
Definition: TitleBlacklistEntry.php:94
TitleBlacklistEntry
Represents a title blacklist entry.
Definition: TitleBlacklistEntry.php:19
$title
$title
Definition: testCompression.php:34
CoreParserFunctions\ns
static ns( $parser, $part1='')
Definition: CoreParserFunctions.php:132
$line
$line
Definition: cdb.php:59
TitleBlacklistEntry\newFromString
static newFromString( $line, $source)
Create a new TitleBlacklistEntry from a line of text.
Definition: TitleBlacklistEntry.php:162
TitleBlacklist\VERSION
const VERSION
Definition: TitleBlacklist.php:29
TitleBlacklistEntry\__construct
__construct( $regex, $params, $raw, $source)
Definition: TitleBlacklistEntry.php:56
$cache
$cache
Definition: mcc.php:33
TitleBlacklistEntry\$mRaw
string $mRaw
Raw line.
Definition: TitleBlacklistEntry.php:24
$source
$source
Definition: mwdoc-filter.php:34
TitleBlacklistEntry\$mSource
string $mSource
Source of this entry.
Definition: TitleBlacklistEntry.php:48