184 $dbkey = str_replace(
' ',
'_', $text );
189 'local_interwiki' =>
false,
191 'namespace' => (int)$defaultNamespace,
195 # Strip Unicode bidi override characters.
196 # Sometimes they slip into cut-n-pasted page titles, where the
197 # override chars get included in list displays.
198 $dbkey = preg_replace(
'/[\x{200E}\x{200F}\x{202A}-\x{202E}]+/u',
'', $dbkey );
200 if ( $dbkey ===
null ) {
201 # Regex had an error. Most likely this is caused by invalid UTF-8
202 $exception = ( $this->createMalformedTitleException )(
'title-invalid-utf8', $text );
206 # Clean up whitespace
207 $dbkey = preg_replace(
208 '/[ _\xA0\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u',
212 $dbkey = trim( $dbkey,
'_' );
214 if ( strpos( $dbkey, \UtfNormal\Constants::UTF8_REPLACEMENT ) !==
false ) {
215 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
216 $exception = ( $this->createMalformedTitleException )(
'title-invalid-utf8', $text );
220 $parts[
'dbkey'] = $dbkey;
222 # Initial colon indicates main namespace rather than specified default
223 # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
224 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
226 $dbkey = substr( $dbkey, 1 ); #
remove the colon but
continue processing
227 $dbkey = trim( $dbkey,
'_' ); #
remove any subsequent whitespace
230 if ( $dbkey ==
'' ) {
231 $exception = ( $this->createMalformedTitleException )(
'title-invalid-empty', $text );
235 # Namespace or interwiki prefix
236 $prefixRegexp =
"/^(.+?)_*:_*(.*)$/S";
239 if ( preg_match( $prefixRegexp, $dbkey, $m ) ) {
241 $ns = $this->language->getNsIndex( $p );
242 if ( $ns !==
false ) {
245 $parts[
'namespace'] = $ns;
246 # For Talk:X pages, check if X has a "namespace" prefix
247 if ( $ns ===
NS_TALK && preg_match( $prefixRegexp, $dbkey, $x ) ) {
248 if ( $this->language->getNsIndex( $x[1] ) ) {
249 # Disallow Talk:File:x type titles...
250 $exception = ( $this->createMalformedTitleException )(
251 'title-invalid-talk-namespace',
255 } elseif ( $this->interwikiLookup->isValidInterwiki( $x[1] ) ) {
256 # Disallow Talk:Interwiki:x type titles...
257 $exception = ( $this->createMalformedTitleException )(
258 'title-invalid-talk-interwiki',
264 } elseif ( $this->interwikiLookup->isValidInterwiki( $p ) ) {
267 $parts[
'interwiki'] = $this->language->lc( $p );
269 # Redundant interwiki prefix to the local wiki
270 foreach ( $this->localInterwikis as $localIW ) {
271 if ( strcasecmp( $parts[
'interwiki'], $localIW ) == 0 ) {
272 if ( $dbkey ==
'' ) {
273 # Empty self-links should point to the Main Page, to ensure
274 # compatibility with cross-wiki transclusions and the like.
277 'interwiki' => $mainPage->getInterwiki(),
278 'local_interwiki' =>
true,
279 'fragment' => $mainPage->getFragment(),
280 'namespace' => $mainPage->getNamespace(),
281 'dbkey' => $mainPage->getDBkey(),
284 $parts[
'interwiki'] =
'';
285 # local interwikis should behave like initial-colon links
286 $parts[
'local_interwiki'] =
true;
288 # Do another namespace split...
293 # If there's an initial colon after the interwiki, that also
294 # resets the default namespace
295 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
297 $dbkey = substr( $dbkey, 1 );
298 $dbkey = trim( $dbkey,
'_' );
301 # If there's no recognized interwiki or namespace,
302 # then let the colon expression be part of the title.
307 $fragment = strstr( $dbkey,
'#' );
308 if ( $fragment !==
false ) {
309 $parts[
'fragment'] = str_replace(
'_',
' ', substr( $fragment, 1 ) );
310 $dbkey = substr( $dbkey, 0, strlen( $dbkey ) - strlen( $fragment ) );
311 # remove whitespace again: prevents "Foo_bar_#"
312 # becoming "Foo_bar_"
313 $dbkey = rtrim( $dbkey,
"_" );
316 # Reject illegal characters.
319 if ( preg_match( $rxTc, $dbkey,
$matches ) ) {
320 $exception = ( $this->createMalformedTitleException )(
'title-invalid-characters', $text, [
$matches[0] ] );
324 # Pages with "/./" or "/../" appearing in the URLs will often be un-
325 # reachable due to the way web browsers deal with 'relative' URLs.
326 # Also, they conflict with subpage syntax. Forbid them explicitly.
328 str_contains( $dbkey,
'.' ) &&
330 $dbkey ===
'.' || $dbkey ===
'..' ||
331 str_starts_with( $dbkey,
'./' ) ||
332 str_starts_with( $dbkey,
'../' ) ||
333 str_contains( $dbkey,
'/./' ) ||
334 str_contains( $dbkey,
'/../' ) ||
335 str_ends_with( $dbkey,
'/.' ) ||
336 str_ends_with( $dbkey,
'/..' )
339 $exception = ( $this->createMalformedTitleException )(
'title-invalid-relative', $text );
343 # Magic tilde sequences? Nu-uh!
344 if ( strpos( $dbkey,
'~~~' ) !== false ) {
345 $exception = ( $this->createMalformedTitleException )(
'title-invalid-magic-tilde', $text );
349 # Limit the size of titles to 255 bytes. This is typically the size of the
350 # underlying database field. We make an exception for special pages, which
351 # don't need to be stored in the database, and may edge over 255 bytes due
352 # to subpage syntax for long titles, e.g. [[Special:Block/Long name]]
353 $maxLength = ( $parts[
'namespace'] !==
NS_SPECIAL ) ? 255 : 512;
354 if ( strlen( $dbkey ) > $maxLength ) {
355 $exception = ( $this->createMalformedTitleException )(
356 'title-invalid-too-long',
363 # Normally, all wiki links are forced to have an initial capital letter so [[foo]]
364 # and [[Foo]] point to the same place. Don't force it for interwikis, since the
365 # other site might be case-sensitive.
366 if ( $parts[
'interwiki'] ===
'' && $this->nsInfo->isCapitalized( $parts[
'namespace'] ) ) {
367 $dbkey = $this->language->ucfirst( $dbkey );
370 # Can't make a link to a namespace alone... "empty" local links can only be
371 # self-links with a fragment identifier.
372 if ( $dbkey ==
'' && $parts[
'interwiki'] ===
'' && $parts[
'namespace'] !==
NS_MAIN ) {
373 $exception = ( $this->createMalformedTitleException )(
'title-invalid-empty', $text );
383 if ( $dbkey !==
'' && ( $parts[
'namespace'] ===
NS_USER || $parts[
'namespace'] ===
NS_USER_TALK ) ) {
384 $dbkey = IPUtils::sanitizeIP( $dbkey );
386 '@phan-var string $dbkey';
390 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
391 $exception = ( $this->createMalformedTitleException )(
'title-invalid-leading-colon', $text );
396 $parts[
'dbkey'] = $dbkey;
407 }
catch ( InvalidArgumentException $ex ) {
408 $exception = ( $this->createMalformedTitleException )(
'title-invalid', $text, [ $ex->getMessage() ] );