MediaWiki REL1_31
TitleBlacklistEntry.php
Go to the documentation of this file.
1<?php
22 private $mRaw;
23
28 private $mRegex;
29
34 private $mParams;
35
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
107 $cache = ObjectCache::getMainWANInstance();
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
128 $match = preg_match(
129 "/^(?:{$this->mRegex})$/us" . ( isset( $this->mParams['casesensitive'] ) ? '' : 'i' ),
130 $title
131 );
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 isset( $this->mParams['errmsg'] ) ? $this->mParams['errmsg'] : null;
261 }
262
266 public function getFormatVersion() {
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 ? $message : "titleblacklist-forbidden-{$operation}";
292 }
293}
wfRestoreWarnings()
wfSuppressWarnings( $end=false)
Reference-counted warning suppression.
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
$wgParser
Definition Setup.php:917
$line
Definition cdb.php:59
static ns( $parser, $part1='')
Represents a title blacklist entry.
string $mFormatVersion
Entry format version.
string $mSource
Source of this entry.
static newFromString( $line, $source)
Create a new TitleBlacklistEntry from a line of text.
getErrorMessage( $operation)
Return the error message name for the blacklist entry.
array $mParams
Parameters for this entry.
string $mRegex
Regular expression to match.
__construct( $regex, $params, $raw, $source)
Construct a new TitleBlacklistEntry.
matches( $title, $action)
Check whether a user can perform the specified action on the specified Title.
filtersNewAccounts()
Returns whether this entry is capable of filtering new accounts.
setFormatVersion( $v)
Set the format version.
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
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:2001
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 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;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
$cache
Definition mcc.php:33
$source
$params