170 $dbkey = str_replace(
' ',
'_', $text );
175 'local_interwiki' =>
false,
177 'namespace' => (int)$defaultNamespace,
181 # Strip Unicode bidi override characters.
182 # Sometimes they slip into cut-n-pasted page titles, where the
183 # override chars get included in list displays.
184 $dbkey = preg_replace(
'/[\x{200E}\x{200F}\x{202A}-\x{202E}]+/u',
'', $dbkey );
186 if ( $dbkey ===
null ) {
187 # Regex had an error. Most likely this is caused by invalid UTF-8
188 $exception = ( $this->createMalformedTitleException )(
'title-invalid-utf8', $text );
192 # Clean up whitespace
193 $dbkey = preg_replace(
194 '/[ _\xA0\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u',
198 $dbkey = trim( $dbkey,
'_' );
200 if ( str_contains( $dbkey, \UtfNormal\Constants::UTF8_REPLACEMENT ) ) {
201 # Contained illegal UTF-8 sequences or forbidden Unicode chars.
202 $exception = ( $this->createMalformedTitleException )(
'title-invalid-utf8', $text );
206 $parts[
'dbkey'] = $dbkey;
208 # Initial colon indicates main namespace rather than specified default
209 # but should not create invalid {ns,title} pairs such as {0,Project:Foo}
210 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
212 $dbkey = substr( $dbkey, 1 ); #
remove the colon but
continue processing
213 $dbkey = trim( $dbkey,
'_' ); #
remove any subsequent whitespace
216 if ( $dbkey ==
'' ) {
217 $exception = ( $this->createMalformedTitleException )(
'title-invalid-empty', $text );
221 # Namespace or interwiki prefix
222 $prefixRegexp =
"/^(.+?)_*:_*(.*)$/S";
225 if ( preg_match( $prefixRegexp, $dbkey, $m ) ) {
227 $ns = $this->language->getNsIndex( $p );
228 if ( $ns !==
false ) {
231 $parts[
'namespace'] = $ns;
232 # For Talk:X pages, check if X has a "namespace" prefix
233 if ( $ns ===
NS_TALK && preg_match( $prefixRegexp, $dbkey, $x ) ) {
234 if ( $this->language->getNsIndex( $x[1] ) ) {
235 # Disallow Talk:File:x type titles...
236 $exception = ( $this->createMalformedTitleException )(
237 'title-invalid-talk-namespace',
241 } elseif ( $this->interwikiLookup->isValidInterwiki( $x[1] ) ) {
242 # Disallow Talk:Interwiki:x type titles...
243 $exception = ( $this->createMalformedTitleException )(
244 'title-invalid-talk-interwiki',
250 } elseif ( $this->interwikiLookup->isValidInterwiki( $p ) ) {
253 $parts[
'interwiki'] = $this->language->lc( $p );
255 # Redundant interwiki prefix to the local wiki
256 foreach ( $this->localInterwikis as $localIW ) {
257 if ( strcasecmp( $parts[
'interwiki'], $localIW ) == 0 ) {
258 if ( $dbkey ==
'' ) {
259 # Empty self-links should point to the Main Page, to ensure
260 # compatibility with cross-wiki transclusions and the like.
263 'interwiki' => $mainPage->getInterwiki(),
264 'local_interwiki' =>
true,
265 'fragment' => $mainPage->getFragment(),
266 'namespace' => $mainPage->getNamespace(),
267 'dbkey' => $mainPage->getDBkey(),
270 $parts[
'interwiki'] =
'';
271 # local interwikis should behave like initial-colon links
272 $parts[
'local_interwiki'] =
true;
274 # Do another namespace split...
279 # If there's an initial colon after the interwiki, that also
280 # resets the default namespace
281 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
283 $dbkey = substr( $dbkey, 1 );
284 $dbkey = trim( $dbkey,
'_' );
287 # If there's no recognized interwiki or namespace,
288 # then let the colon expression be part of the title.
293 $fragment = strstr( $dbkey,
'#' );
294 if ( $fragment !==
false ) {
295 $parts[
'fragment'] = str_replace(
'_',
' ', substr( $fragment, 1 ) );
296 $dbkey = substr( $dbkey, 0, strlen( $dbkey ) - strlen( $fragment ) );
297 # remove whitespace again: prevents "Foo_bar_#"
298 # becoming "Foo_bar_"
299 $dbkey = rtrim( $dbkey,
"_" );
302 # Reject illegal characters.
305 if ( preg_match( $rxTc, $dbkey,
$matches ) ) {
306 $exception = ( $this->createMalformedTitleException )(
'title-invalid-characters', $text, [
$matches[0] ] );
310 # Pages with "/./" or "/../" appearing in the URLs will often be un-
311 # reachable due to the way web browsers deal with 'relative' URLs.
312 # Also, they conflict with subpage syntax. Forbid them explicitly.
314 str_contains( $dbkey,
'.' ) &&
316 $dbkey ===
'.' || $dbkey ===
'..' ||
317 str_starts_with( $dbkey,
'./' ) ||
318 str_starts_with( $dbkey,
'../' ) ||
319 str_contains( $dbkey,
'/./' ) ||
320 str_contains( $dbkey,
'/../' ) ||
321 str_ends_with( $dbkey,
'/.' ) ||
322 str_ends_with( $dbkey,
'/..' )
325 $exception = ( $this->createMalformedTitleException )(
'title-invalid-relative', $text );
329 # Magic tilde sequences? Nu-uh!
330 if ( str_contains( $dbkey,
'~~~' ) ) {
331 $exception = ( $this->createMalformedTitleException )(
'title-invalid-magic-tilde', $text );
335 # Limit the size of titles to 255 bytes. This is typically the size of the
336 # underlying database field. We make an exception for special pages, which
337 # don't need to be stored in the database, and may edge over 255 bytes due
338 # to subpage syntax for long titles, e.g. [[Special:Block/Long name]]
339 $maxLength = ( $parts[
'namespace'] !==
NS_SPECIAL ) ? 255 : 512;
340 if ( strlen( $dbkey ) > $maxLength ) {
341 $exception = ( $this->createMalformedTitleException )(
342 'title-invalid-too-long',
349 # Normally, all wiki links are forced to have an initial capital letter so [[foo]]
350 # and [[Foo]] point to the same place. Don't force it for interwikis, since the
351 # other site might be case-sensitive.
352 if ( $parts[
'interwiki'] ===
'' && $this->nsInfo->isCapitalized( $parts[
'namespace'] ) ) {
353 $dbkey = $this->language->ucfirst( $dbkey );
356 # Can't make a link to a namespace alone... "empty" local links can only be
357 # self-links with a fragment identifier.
358 if ( $dbkey ==
'' && $parts[
'interwiki'] ===
'' && $parts[
'namespace'] !==
NS_MAIN ) {
359 $exception = ( $this->createMalformedTitleException )(
'title-invalid-empty', $text );
369 if ( $dbkey !==
'' && ( $parts[
'namespace'] ===
NS_USER || $parts[
'namespace'] ===
NS_USER_TALK ) ) {
370 $dbkey = IPUtils::sanitizeIP( $dbkey );
372 '@phan-var string $dbkey';
376 if ( $dbkey !==
'' && $dbkey[0] ==
':' ) {
377 $exception = ( $this->createMalformedTitleException )(
'title-invalid-leading-colon', $text );
382 $parts[
'dbkey'] = $dbkey;
393 }
catch ( InvalidArgumentException $ex ) {
394 $exception = ( $this->createMalformedTitleException )(
'title-invalid', $text, [ $ex->getMessage() ] );