| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Utils; |
| 5 |
|
| 6 |
use Wikimedia\Assert\Assert; |
| 7 |
use Wikimedia\IPUtils; |
| 8 |
use Wikimedia\Parsoid\Config\SiteConfig; |
| 9 |
use Wikimedia\Parsoid\Core\LinkTarget; |
| 10 |
use Wikimedia\Parsoid\Core\LinkTargetTrait; |
| 11 |
|
| 12 |
class Title implements LinkTarget { |
| 13 |
use LinkTargetTrait; |
| 14 |
|
| 15 |
/** @var string */ |
| 16 |
private $interwiki; |
| 17 |
|
| 18 |
/** @var int */ |
| 19 |
private $namespaceId; |
| 20 |
|
| 21 |
/** @var string */ |
| 22 |
private $namespaceName; |
| 23 |
|
| 24 |
/** @var string */ |
| 25 |
private $dbkey; |
| 26 |
|
| 27 |
/** @var string */ |
| 28 |
private $fragment; |
| 29 |
|
| 30 |
// cached values of prefixed title/key |
| 31 |
private ?string $prefixedDBKey = null; |
| 32 |
private ?string $prefixedText = null; |
| 33 |
|
| 34 |
/** |
| 35 |
* @param string $interwiki Interwiki prefix, or empty string if none |
| 36 |
* @param string $key Page DBkey (with underscores, not spaces) |
| 37 |
* @param int $namespaceId |
| 38 |
* @param string $namespaceName (with spaces, not underscores) |
| 39 |
* @param ?string $fragment |
| 40 |
*/ |
| 41 |
private function __construct( |
| 42 |
string $interwiki, string $key, int $namespaceId, string $namespaceName, ?string $fragment = null |
| 43 |
) { |
| 44 |
$this->interwiki = $interwiki; |
| 45 |
$this->dbkey = $key; |
| 46 |
$this->namespaceId = $namespaceId; |
| 47 |
$this->namespaceName = $namespaceName; |
| 48 |
$this->fragment = $fragment ?? ''; |
| 49 |
} |
| 50 |
|
| 51 |
public static function newFromText( |
| 52 |
string $title, SiteConfig $siteConfig, ?int $defaultNs = null |
| 53 |
): Title { |
| 54 |
if ( $defaultNs === null ) { |
| 55 |
$defaultNs = 0; |
| 56 |
} |
| 57 |
$origTitle = $title; |
| 58 |
|
| 59 |
if ( !mb_check_encoding( $title, 'UTF-8' ) ) { |
| 60 |
throw new TitleException( "Bad UTF-8 in title \"$origTitle\"", 'title-invalid-utf8', $origTitle ); |
| 61 |
} |
| 62 |
|
| 63 |
// Strip Unicode bidi override characters. |
| 64 |
$title = preg_replace( '/[\x{200E}\x{200F}\x{202A}-\x{202E}]+/u', '', $title ); |
| 65 |
if ( $title === null ) { |
| 66 |
throw new TitleException( "Bad UTF-8 in title \"$origTitle\"", 'title-invalid-utf8', $origTitle ); |
| 67 |
} |
| 68 |
|
| 69 |
// Clean up whitespace |
| 70 |
$title = preg_replace( |
| 71 |
'/[ _\x{00A0}\x{1680}\x{180E}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}]+/u', |
| 72 |
'_', $title |
| 73 |
); |
| 74 |
// Trim _ from beginning and end |
| 75 |
$title = trim( $title, '_' ); |
| 76 |
|
| 77 |
if ( str_contains( $title, \UtfNormal\Constants::UTF8_REPLACEMENT ) ) { |
| 78 |
throw new TitleException( "Bad UTF-8 in title \"$title\"", 'title-invalid-utf8', $title ); |
| 79 |
} |
| 80 |
|
| 81 |
// Initial colon indicates main namespace rather than specified default |
| 82 |
// but should not create invalid {ns,title} pairs such as {0,Project:Foo} |
| 83 |
if ( $title !== '' && $title[0] === ':' ) { |
| 84 |
$title = ltrim( substr( $title, 1 ), '_' ); |
| 85 |
$defaultNs = 0; |
| 86 |
} |
| 87 |
|
| 88 |
if ( $title === '' ) { |
| 89 |
throw new TitleException( 'Empty title', 'title-invalid-empty', $title ); |
| 90 |
} |
| 91 |
|
| 92 |
$ns = $defaultNs; |
| 93 |
$interwiki = null; |
| 94 |
|
| 95 |
# Namespace or interwiki prefix |
| 96 |
$prefixRegexp = "/^(.+?)_*:_*(.*)$/S"; |
| 97 |
// MediaWikiTitleCodec::splitTitleString wraps a loop around the |
| 98 |
// next section, to allow it to repeat this prefix processing if |
| 99 |
// an interwiki prefix is found which points at the local wiki. |
| 100 |
$m = []; |
| 101 |
if ( preg_match( $prefixRegexp, $title, $m ) ) { |
| 102 |
$p = $m[1]; |
| 103 |
$pLower = mb_strtolower( $p ); |
| 104 |
$nsId = $siteConfig->canonicalNamespaceId( $pLower ) ?? |
| 105 |
$siteConfig->namespaceId( $pLower ); |
| 106 |
if ( $nsId !== null ) { |
| 107 |
$title = $m[2]; |
| 108 |
$ns = $nsId; |
| 109 |
# For Talk:X pages, check if X has a "namespace" prefix |
| 110 |
if ( |
| 111 |
$nsId === $siteConfig->canonicalNamespaceId( 'talk' ) && |
| 112 |
preg_match( $prefixRegexp, $title, $x ) |
| 113 |
) { |
| 114 |
$xLower = mb_strtolower( $x[1] ); |
| 115 |
if ( $siteConfig->namespaceId( $xLower ) ) { |
| 116 |
// Disallow Talk:File:x type titles. |
| 117 |
throw new TitleException( |
| 118 |
"Invalid Talk namespace title \"$origTitle\"", 'title-invalid-talk-namespace', $title |
| 119 |
); |
| 120 |
} elseif ( $siteConfig->interwikiMapNoNamespaces()[$xLower] ?? null ) { |
| 121 |
// Disallow Talk:Interwiki:x type titles. |
| 122 |
throw new TitleException( |
| 123 |
"Invalid Talk namespace title \"$origTitle\"", 'title-invalid-talk-namespace', $title |
| 124 |
); |
| 125 |
} |
| 126 |
} |
| 127 |
} elseif ( $siteConfig->interwikiMapNoNamespaces()[$pLower] ?? null ) { |
| 128 |
# Interwiki link |
| 129 |
$title = $m[2]; |
| 130 |
$interwiki = $pLower; |
| 131 |
|
| 132 |
# We don't check for a redundant interwiki prefix to the |
| 133 |
# local wiki, like core does here in |
| 134 |
# MediaWikiTitleCodec::splitTitleString; |
| 135 |
# core then does a `continue` to repeat the processing |
| 136 |
|
| 137 |
// If there's an initial colon after the interwiki, that also |
| 138 |
// resets the default namespace |
| 139 |
if ( $title !== '' && $title[0] === ':' ) { |
| 140 |
$title = trim( substr( $title, 1 ), '_' ); |
| 141 |
$ns = 0; |
| 142 |
} |
| 143 |
} |
| 144 |
# If there's no recognized interwiki or namespace, |
| 145 |
# then let the colon expression be part of the title |
| 146 |
} |
| 147 |
|
| 148 |
$fragment = null; |
| 149 |
$fragmentIndex = strpos( $title, '#' ); |
| 150 |
if ( $fragmentIndex !== false ) { |
| 151 |
$fragment = substr( $title, $fragmentIndex + 1 ); |
| 152 |
$title = rtrim( substr( $title, 0, $fragmentIndex ), '_' ); |
| 153 |
} |
| 154 |
|
| 155 |
$illegalCharsRe = '/[^' . $siteConfig->legalTitleChars() . ']' |
| 156 |
// URL percent encoding sequences interfere with the ability |
| 157 |
// to round-trip titles -- you can't link to them consistently. |
| 158 |
. '|%[0-9A-Fa-f]{2}' |
| 159 |
// XML/HTML character references produce similar issues. |
| 160 |
. '|&[A-Za-z0-9\x80-\xff]+;/S'; |
| 161 |
if ( preg_match( $illegalCharsRe, $title ) ) { |
| 162 |
throw new TitleException( |
| 163 |
"Invalid characters in title \"$origTitle\"", 'title-invalid-characters', $title |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
// Pages with "/./" or "/../" appearing in the URLs will often be |
| 168 |
// unreachable due to the way web browsers deal with 'relative' URLs. |
| 169 |
// Also, they conflict with subpage syntax. Forbid them explicitly. |
| 170 |
if ( str_contains( $title, '.' ) && ( |
| 171 |
$title === '.' || $title === '..' || |
| 172 |
str_starts_with( $title, './' ) || |
| 173 |
str_starts_with( $title, '../' ) || |
| 174 |
str_contains( $title, '/./' ) || |
| 175 |
str_contains( $title, '/../' ) || |
| 176 |
str_ends_with( $title, '/.' ) || |
| 177 |
str_ends_with( $title, '/..' ) |
| 178 |
) ) { |
| 179 |
throw new TitleException( |
| 180 |
"Title \"$origTitle\" contains relative path components", 'title-invalid-relative', $title |
| 181 |
); |
| 182 |
} |
| 183 |
|
| 184 |
// Magic tilde sequences? Nu-uh! |
| 185 |
if ( str_contains( $title, '~~~' ) ) { |
| 186 |
throw new TitleException( |
| 187 |
"Title \"$origTitle\" contains ~~~", 'title-invalid-magic-tilde', $title |
| 188 |
); |
| 189 |
} |
| 190 |
|
| 191 |
$maxLength = $ns === $siteConfig->canonicalNamespaceId( 'special' ) ? 512 : 255; |
| 192 |
if ( strlen( $title ) > $maxLength ) { |
| 193 |
throw new TitleException( |
| 194 |
"Title \"$origTitle\" is too long", 'title-invalid-too-long', $title |
| 195 |
); |
| 196 |
} |
| 197 |
|
| 198 |
if ( $interwiki === null && $siteConfig->namespaceCase( $ns ) === 'first-letter' ) { |
| 199 |
$title = $siteConfig->ucfirst( $title ); |
| 200 |
} |
| 201 |
|
| 202 |
# Can't make a link to a namespace alone... "empty" local links can only be |
| 203 |
# self-links with a fragment identifier. |
| 204 |
if ( $title === '' && $interwiki === null && $ns !== $siteConfig->canonicalNamespaceId( '' ) ) { |
| 205 |
throw new TitleException( 'Empty title', 'title-invalid-empty', $title ); |
| 206 |
} |
| 207 |
|
| 208 |
// This is from MediaWikiTitleCodec::splitTitleString() in core |
| 209 |
if ( $title !== '' && ( # T329690 |
| 210 |
$ns === $siteConfig->canonicalNamespaceId( 'user' ) || |
| 211 |
$ns === $siteConfig->canonicalNamespaceId( 'user_talk' ) |
| 212 |
) ) { |
| 213 |
$title = IPUtils::sanitizeIP( $title ); |
| 214 |
} |
| 215 |
|
| 216 |
// Any remaining initial :s are illegal. |
| 217 |
if ( $title !== '' && $title[0] == ':' ) { |
| 218 |
throw new TitleException( |
| 219 |
'Leading colon title', 'title-invalid-leading-colon', $title |
| 220 |
); |
| 221 |
} |
| 222 |
|
| 223 |
// This is not in core's splitTitleString but matches |
| 224 |
// mediawiki-title's newFromText. |
| 225 |
if ( $ns === $siteConfig->canonicalNamespaceId( 'special' ) ) { |
| 226 |
$title = self::fixSpecialName( $siteConfig, $title ); |
| 227 |
} |
| 228 |
|
| 229 |
$namespaceName = $siteConfig->namespaceName( $ns ); |
| 230 |
return new self( $interwiki ?? '', $title, $ns, $namespaceName, $fragment ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* The interwiki component of this LinkTarget. |
| 235 |
* This is the empty string if there is no interwiki component. |
| 236 |
* |
| 237 |
* @return string |
| 238 |
*/ |
| 239 |
public function getInterwiki(): string { |
| 240 |
return $this->interwiki; |
| 241 |
} |
| 242 |
|
| 243 |
/** |
| 244 |
* Get the DBkey, prefixed with interwiki prefix if any. |
| 245 |
* This is Parsoid's convention, which differs from core; |
| 246 |
* use ::getDBkey() for a method compatible with core's |
| 247 |
* convention. |
| 248 |
* |
| 249 |
* @return string |
| 250 |
* @see ::getDBkey() |
| 251 |
* @deprecated |
| 252 |
*/ |
| 253 |
public function getKey(): string { |
| 254 |
if ( $this->interwiki ) { |
| 255 |
return $this->interwiki . ':' . $this->dbkey; |
| 256 |
} |
| 257 |
return $this->dbkey; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Get the main part of the link target, in canonical database form. |
| 262 |
* |
| 263 |
* The main part is the link target without namespace prefix or hash fragment. |
| 264 |
* The database form means that spaces become underscores, this is also |
| 265 |
* used for URLs. |
| 266 |
* |
| 267 |
* @return string |
| 268 |
*/ |
| 269 |
public function getDBkey(): string { |
| 270 |
return $this->dbkey; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Get the prefixed DBkey |
| 275 |
* @return string |
| 276 |
*/ |
| 277 |
public function getPrefixedDBKey(): string { |
| 278 |
if ( $this->prefixedDBKey === null ) { |
| 279 |
$this->prefixedDBKey = $this->interwiki === '' ? '' : |
| 280 |
( $this->interwiki . ':' ); |
| 281 |
$this->prefixedDBKey .= $this->namespaceName === '' ? '' : |
| 282 |
( strtr( $this->namespaceName, ' ', '_' ) . ':' ); |
| 283 |
$this->prefixedDBKey .= $this->getDBkey(); |
| 284 |
} |
| 285 |
return $this->prefixedDBKey; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Get the prefixed text |
| 290 |
* @return string |
| 291 |
*/ |
| 292 |
public function getPrefixedText(): string { |
| 293 |
if ( $this->prefixedText === null ) { |
| 294 |
$this->prefixedText = $this->interwiki === '' ? '' : |
| 295 |
( $this->interwiki . ':' ); |
| 296 |
$this->prefixedText .= $this->namespaceName === '' ? '' : |
| 297 |
( $this->namespaceName . ':' ); |
| 298 |
$this->prefixedText .= $this->getText(); |
| 299 |
} |
| 300 |
return $this->prefixedText; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* Get the prefixed title with spaces, plus any fragment |
| 305 |
* (part beginning with '#') |
| 306 |
* |
| 307 |
* @return string The prefixed title, with spaces and the fragment, including '#' |
| 308 |
*/ |
| 309 |
public function getFullText(): string { |
| 310 |
$text = $this->getPrefixedText(); |
| 311 |
if ( $this->hasFragment() ) { |
| 312 |
$text .= '#' . $this->getFragment(); |
| 313 |
} |
| 314 |
return $text; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Get the namespace ID |
| 319 |
* @return int |
| 320 |
*/ |
| 321 |
public function getNamespace(): int { |
| 322 |
return $this->namespaceId; |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Get the human-readable name for the namespace |
| 327 |
* (with spaces, not underscores). |
| 328 |
* @return string |
| 329 |
*/ |
| 330 |
public function getNamespaceName(): string { |
| 331 |
return $this->namespaceName; |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Get the link fragment in text form (i.e. the bit after the hash `#`). |
| 336 |
* |
| 337 |
* @return string link fragment |
| 338 |
*/ |
| 339 |
public function getFragment(): string { |
| 340 |
return $this->fragment ?? ''; |
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Compare with another title. |
| 345 |
* |
| 346 |
* @param Title $title |
| 347 |
* @return bool |
| 348 |
*/ |
| 349 |
public function equals( Title $title ) { |
| 350 |
return $this->getNamespace() === $title->getNamespace() && |
| 351 |
$this->getInterwiki() === $title->getInterwiki() && |
| 352 |
$this->getDBkey() === $title->getDBkey(); |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Returns true if this is a special page. |
| 357 |
* |
| 358 |
* @return bool |
| 359 |
*/ |
| 360 |
public function isSpecialPage() { |
| 361 |
return $this->getNamespace() === -1; // NS_SPECIAL; |
| 362 |
} |
| 363 |
|
| 364 |
/** |
| 365 |
* Use the default special page alias. |
| 366 |
* |
| 367 |
* @param SiteConfig $siteConfig |
| 368 |
* @param string $title |
| 369 |
* @return string |
| 370 |
*/ |
| 371 |
public static function fixSpecialName( |
| 372 |
SiteConfig $siteConfig, string $title |
| 373 |
): string { |
| 374 |
$parts = explode( '/', $title, 2 ); |
| 375 |
$specialName = $siteConfig->specialPageLocalName( $parts[0] ); |
| 376 |
if ( $specialName !== null ) { |
| 377 |
$parts[0] = $specialName; |
| 378 |
$title = implode( '/', $parts ); |
| 379 |
} |
| 380 |
return $title; |
| 381 |
} |
| 382 |
|
| 383 |
/** |
| 384 |
* Create a new LinkTarget with a different fragment on the same page. |
| 385 |
* |
| 386 |
* It is expected that the same type of object will be returned, but the |
| 387 |
* only requirement is that it is a LinkTarget. |
| 388 |
* |
| 389 |
* @param string $fragment The fragment override, or "" to remove it. |
| 390 |
* |
| 391 |
* @return self |
| 392 |
*/ |
| 393 |
public function createFragmentTarget( string $fragment ) { |
| 394 |
return new self( $this->interwiki, $this->dbkey, $this->namespaceId, $this->namespaceName, $fragment ?: null ); |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Convert LinkTarget from core (or other implementation) into a |
| 399 |
* Parsoid Title. |
| 400 |
* |
| 401 |
* @param LinkTarget $linkTarget |
| 402 |
* @return self |
| 403 |
*/ |
| 404 |
public static function newFromLinkTarget( |
| 405 |
LinkTarget $linkTarget, SiteConfig $siteConfig |
| 406 |
) { |
| 407 |
if ( $linkTarget instanceof Title ) { |
| 408 |
return $linkTarget; |
| 409 |
} |
| 410 |
$ns = $linkTarget->getNamespace(); |
| 411 |
$namespaceName = $siteConfig->namespaceName( $ns ); |
| 412 |
Assert::invariant( |
| 413 |
$namespaceName !== null, |
| 414 |
"Badtitle ({$linkTarget}) in unknown namespace ({$ns})" |
| 415 |
); |
| 416 |
return new self( |
| 417 |
$linkTarget->getInterwiki(), |
| 418 |
$linkTarget->getDBkey(), |
| 419 |
$linkTarget->getNamespace(), |
| 420 |
$namespaceName, |
| 421 |
$linkTarget->getFragment() |
| 422 |
); |
| 423 |
} |
| 424 |
} |