| 1 |
<?php |
| 2 |
declare( strict_types = 1 ); |
| 3 |
|
| 4 |
namespace Wikimedia\Parsoid\Utils; |
| 5 |
|
| 6 |
use Wikimedia\Parsoid\Core\LinkTarget; |
| 7 |
use Wikimedia\Parsoid\Core\LinkTargetTrait; |
| 8 |
|
| 9 |
/** |
| 10 |
* Lightweight title class |
| 11 |
*/ |
| 12 |
class TitleValue 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 $dbkey; |
| 23 |
|
| 24 |
/** @var string */ |
| 25 |
private $fragment; |
| 26 |
|
| 27 |
/** |
| 28 |
* @param int $namespaceId |
| 29 |
* @param string $dbkey Page DBkey (with underscores, not spaces) |
| 30 |
* @param string $fragment Fragment suffix, or empty string if none |
| 31 |
* @param string $interwiki Interwiki prefix, or empty string if none |
| 32 |
*/ |
| 33 |
private function __construct( |
| 34 |
int $namespaceId, string $dbkey, string $fragment = '', string $interwiki = '' |
| 35 |
) { |
| 36 |
$this->namespaceId = $namespaceId; |
| 37 |
$this->dbkey = strtr( $dbkey, ' ', '_' ); |
| 38 |
$this->fragment = $fragment; |
| 39 |
$this->interwiki = $interwiki; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Constructs a TitleValue, or returns null if the parameters are not valid. |
| 44 |
* |
| 45 |
* @note This does not perform any normalization, and only basic validation. |
| 46 |
* |
| 47 |
* @param int $namespace The namespace ID. This is not validated. |
| 48 |
* @param string $title The page title in either DBkey or text form. No normalization is applied |
| 49 |
* beyond underscore/space conversion. |
| 50 |
* @param string $fragment The fragment title. Use '' to represent the whole page. |
| 51 |
* No validation or normalization is applied. |
| 52 |
* @param string $interwiki The interwiki component. |
| 53 |
* No validation or normalization is applied. |
| 54 |
* @return TitleValue|null |
| 55 |
*/ |
| 56 |
public static function tryNew( |
| 57 |
int $namespace, |
| 58 |
string $title, |
| 59 |
string $fragment = '', |
| 60 |
string $interwiki = '' |
| 61 |
): ?TitleValue { |
| 62 |
return new static( $namespace, $title, $fragment, $interwiki ); |
| 63 |
} |
| 64 |
|
| 65 |
/** @inheritDoc */ |
| 66 |
public function getNamespace(): int { |
| 67 |
return $this->namespaceId; |
| 68 |
} |
| 69 |
|
| 70 |
/** @inheritDoc */ |
| 71 |
public function getFragment(): string { |
| 72 |
return $this->fragment; |
| 73 |
} |
| 74 |
|
| 75 |
/** @inheritDoc */ |
| 76 |
public function getDBkey(): string { |
| 77 |
return $this->dbkey; |
| 78 |
} |
| 79 |
|
| 80 |
/** @inheritDoc */ |
| 81 |
public function createFragmentTarget( string $fragment ): self { |
| 82 |
return new static( $this->namespaceId, $this->dbkey, $fragment, $this->interwiki ); |
| 83 |
} |
| 84 |
|
| 85 |
/** @inheritDoc */ |
| 86 |
public function getInterwiki(): string { |
| 87 |
return $this->interwiki; |
| 88 |
} |
| 89 |
} |