MediaWiki  1.33.0
TitleBlacklistEntry.php
Go to the documentation of this file.
1 <?php
22  private $mRaw;
23 
28  private $mRegex;
29 
34  private $mParams;
35 
40  private $mFormatVersion;
41 
46  private $mSource;
47 
55  private function __construct( $regex, $params, $raw, $source ) {
56  $this->mRaw = $raw;
57  $this->mRegex = $regex;
58  $this->mParams = $params;
59  $this->mFormatVersion = TitleBlacklist::VERSION;
60  $this->mSource = $source;
61  }
62 
66  private function filtersNewAccounts() {
67  global $wgTitleBlacklistUsernameSources;
68 
69  if ( $wgTitleBlacklistUsernameSources === '*' ) {
70  return true;
71  }
72 
73  if ( !$wgTitleBlacklistUsernameSources ) {
74  return false;
75  }
76 
77  if ( !is_array( $wgTitleBlacklistUsernameSources ) ) {
78  throw new Exception(
79  '$wgTitleBlacklistUsernameSources must be "*", false or an array' );
80  }
81 
82  return in_array( $this->mSource, $wgTitleBlacklistUsernameSources, true );
83  }
84 
93  public function matches( $title, $action ) {
94  if ( $title == '' ) {
95  return false;
96  }
97 
98  if ( $action === 'new-account' && !$this->filtersNewAccounts() ) {
99  return false;
100  }
101 
102  if ( isset( $this->mParams['antispoof'] )
103  && is_callable( 'AntiSpoof::checkUnicodeString' )
104  ) {
105  if ( $action === 'edit' ) {
106  // Use process cache for frequently edited pages
108  list( $ok, $norm ) = $cache->getWithSetCallback(
109  $cache->makeKey( 'titleblacklist', 'normalized-unicode', md5( $title ) ),
110  $cache::TTL_MONTH,
111  function () use ( $title ) {
112  return AntiSpoof::checkUnicodeString( $title );
113  },
114  [ 'pcTTL' => $cache::TTL_PROC_LONG ]
115  );
116  } else {
117  list( $ok, $norm ) = AntiSpoof::checkUnicodeString( $title );
118  }
119 
120  if ( $ok === "OK" ) {
121  list( $ver, $title ) = explode( ':', $norm, 2 );
122  } else {
123  wfDebugLog( 'TitleBlacklist', 'AntiSpoof could not normalize "' . $title . '".' );
124  }
125  }
126 
127  Wikimedia\suppressWarnings();
128  $match = preg_match(
129  "/^(?:{$this->mRegex})$/us" . ( isset( $this->mParams['casesensitive'] ) ? '' : 'i' ),
130  $title
131  );
132  Wikimedia\restoreWarnings();
133 
134  if ( $match ) {
135  if ( isset( $this->mParams['moveonly'] ) && $action != 'move' ) {
136  return false;
137  }
138  if ( isset( $this->mParams['newaccountonly'] ) && $action != 'new-account' ) {
139  return false;
140  }
141  if ( !isset( $this->mParams['noedit'] ) && $action == 'edit' ) {
142  return false;
143  }
144  if ( isset( $this->mParams['reupload'] ) && $action == 'upload' ) {
145  // Special:Upload also checks 'create' permissions when not reuploading
146  return false;
147  }
148  return true;
149  }
150 
151  return false;
152  }
153 
161  public static function newFromString( $line, $source ) {
162  $raw = $line; // Keep line for raw data
163  $options = [];
164  // Strip comments
165  $line = preg_replace( "/^\\s*([^#]*)\\s*((.*)?)$/", "\\1", $line );
166  $line = trim( $line );
167  // A blank string causes problems later on
168  if ( $line === '' ) {
169  return null;
170  }
171  // Parse the rest of message
172  $pockets = [];
173  if ( !preg_match( '/^(.*?)(\s*<([^<>]*)>)?$/', $line, $pockets ) ) {
174  return null;
175  }
176  $regex = trim( $pockets[1] );
177  $regex = str_replace( '_', ' ', $regex ); // We'll be matching against text form
178  $opts_str = isset( $pockets[3] ) ? trim( $pockets[3] ) : '';
179  // Parse opts
180  $opts = preg_split( '/\s*\|\s*/', $opts_str );
181  foreach ( $opts as $opt ) {
182  $opt2 = strtolower( $opt );
183  if ( $opt2 == 'autoconfirmed' ) {
184  $options['autoconfirmed'] = true;
185  }
186  if ( $opt2 == 'moveonly' ) {
187  $options['moveonly'] = true;
188  }
189  if ( $opt2 == 'newaccountonly' ) {
190  $options['newaccountonly'] = true;
191  }
192  if ( $opt2 == 'noedit' ) {
193  $options['noedit'] = true;
194  }
195  if ( $opt2 == 'casesensitive' ) {
196  $options['casesensitive'] = true;
197  }
198  if ( $opt2 == 'reupload' ) {
199  $options['reupload'] = true;
200  }
201  if ( preg_match( '/errmsg\s*=\s*(.+)/i', $opt, $matches ) ) {
202  $options['errmsg'] = $matches[1];
203  }
204  if ( $opt2 == 'antispoof' ) {
205  $options['antispoof'] = true;
206  }
207  }
208  // Process magic words
209  preg_match_all( '/{{\s*([a-z]+)\s*:\s*(.+?)\s*}}/', $regex, $magicwords, PREG_SET_ORDER );
210  foreach ( $magicwords as $mword ) {
211  global $wgParser; // Functions we're calling don't need, nevertheless let's use it
212  switch ( strtolower( $mword[1] ) ) {
213  case 'ns':
214  $cpf_result = CoreParserFunctions::ns( $wgParser, $mword[2] );
215  if ( is_string( $cpf_result ) ) {
216  // All result will have the same value, so we can just use str_seplace()
217  $regex = str_replace( $mword[0], $cpf_result, $regex );
218  }
219  break;
220  case 'int':
221  $cpf_result = wfMessage( $mword[2] )->inContentLanguage()->text();
222  if ( is_string( $cpf_result ) ) {
223  $regex = str_replace( $mword[0], $cpf_result, $regex );
224  }
225  }
226  }
227  // Return result
228  if ( $regex ) {
229  return new TitleBlacklistEntry( $regex, $options, $raw, $source );
230  } else {
231  return null;
232  }
233  }
234 
238  public function getRegex() {
239  return $this->mRegex;
240  }
241 
245  public function getRaw() {
246  return $this->mRaw;
247  }
248 
252  public function getParams() {
253  return $this->mParams;
254  }
255 
259  public function getCustomMessage() {
260  return $this->mParams['errmsg'] ?? null;
261  }
262 
266  public function getFormatVersion() {
267  return $this->mFormatVersion;
268  }
269 
275  public function setFormatVersion( $v ) {
276  $this->mFormatVersion = $v;
277  }
278 
286  public function getErrorMessage( $operation ) {
287  $message = $this->getCustomMessage();
288  // For grep:
289  // titleblacklist-forbidden-edit, titleblacklist-forbidden-move,
290  // titleblacklist-forbidden-upload, titleblacklist-forbidden-new-account
291  return $message ?: "titleblacklist-forbidden-{$operation}";
292  }
293 }
TitleBlacklistEntry\setFormatVersion
setFormatVersion( $v)
Set the format version.
Definition: TitleBlacklistEntry.php:275
TitleBlacklistEntry\getRaw
getRaw()
Definition: TitleBlacklistEntry.php:245
TitleBlacklistEntry\getFormatVersion
getFormatVersion()
Definition: TitleBlacklistEntry.php:266
$wgParser
$wgParser
Definition: Setup.php:886
$opt
$opt
Definition: postprocess-phan.php:115
TitleBlacklistEntry\getCustomMessage
getCustomMessage()
Definition: TitleBlacklistEntry.php:259
TitleBlacklistEntry\$mFormatVersion
string $mFormatVersion
Entry format version.
Definition: TitleBlacklistEntry.php:40
$params
$params
Definition: styleTest.css.php:44
TitleBlacklistEntry\getErrorMessage
getErrorMessage( $operation)
Return the error message name for the blacklist entry.
Definition: TitleBlacklistEntry.php:286
TitleBlacklistEntry\getParams
getParams()
Definition: TitleBlacklistEntry.php:252
TitleBlacklistEntry\$mParams
array $mParams
Parameters for this entry.
Definition: TitleBlacklistEntry.php:34
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:1043
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
TitleBlacklistEntry\filtersNewAccounts
filtersNewAccounts()
Returns whether this entry is capable of filtering new accounts.
Definition: TitleBlacklistEntry.php:66
TitleBlacklistEntry\getRegex
getRegex()
Definition: TitleBlacklistEntry.php:238
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
TitleBlacklistEntry\$mRegex
string $mRegex
Regular expression to match.
Definition: TitleBlacklistEntry.php:28
$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:93
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
TitleBlacklistEntry
Represents a title blacklist entry.
Definition: TitleBlacklistEntry.php:17
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
CoreParserFunctions\ns
static ns( $parser, $part1='')
Definition: CoreParserFunctions.php:132
list
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
$line
$line
Definition: cdb.php:59
TitleBlacklistEntry\newFromString
static newFromString( $line, $source)
Create a new TitleBlacklistEntry from a line of text.
Definition: TitleBlacklistEntry.php:161
TitleBlacklist\VERSION
const VERSION
Definition: TitleBlacklist.php:27
TitleBlacklistEntry\__construct
__construct( $regex, $params, $raw, $source)
Construct a new TitleBlacklistEntry.
Definition: TitleBlacklistEntry.php:55
$cache
$cache
Definition: mcc.php:33
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1985
ObjectCache\getMainWANInstance
static getMainWANInstance()
Get the main WAN cache object.
Definition: ObjectCache.php:369
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
TitleBlacklistEntry\$mRaw
string $mRaw
Raw line.
Definition: TitleBlacklistEntry.php:22
$source
$source
Definition: mwdoc-filter.php:46
wfMessage
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
TitleBlacklistEntry\$mSource
string $mSource
Source of this entry.
Definition: TitleBlacklistEntry.php:46