3 * Representation of a page title within MediaWiki.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @author Daniel Kinzler
24 namespace MediaWiki\Title
;
26 use InvalidArgumentException
;
27 use MediaWiki\Linker\LinkTarget
;
28 use MediaWiki\Page\PageReference
;
30 use Wikimedia\Assert\Assert
;
31 use Wikimedia\Assert\ParameterAssertionException
;
32 use Wikimedia\Assert\ParameterTypeException
;
33 use Wikimedia\Parsoid\Core\LinkTarget
as ParsoidLinkTarget
;
34 use Wikimedia\Parsoid\Core\LinkTargetTrait
;
37 * Represents the target of a wiki link.
39 * @note In contrast to Title, this is designed to be a plain value object. That is,
40 * it is immutable, does not use global state, and causes no side effects.
44 * @see https://www.mediawiki.org/wiki/Manual:Modeling_pages
47 class TitleValue
implements Stringable
, LinkTarget
{
63 * Text form including namespace/interwiki, initialised on demand
65 * Only public to share cache with TitleFormatter
70 public $prefixedText = null;
73 * Constructs a TitleValue, or returns null if the parameters are not valid.
75 * @note This does not perform any normalization, and only basic validation.
76 * For full normalization and validation, use TitleParser::makeTitleValueSafe().
78 * @param int $namespace The namespace ID. This is not validated.
79 * @param string $title The page title in either DBkey or text form. No normalization is applied
80 * beyond underscore/space conversion.
81 * @param string $fragment The fragment title. Use '' to represent the whole page.
82 * No validation or normalization is applied.
83 * @param string $interwiki The interwiki component.
84 * No validation or normalization is applied.
85 * @return TitleValue|null
87 public static function tryNew( $namespace, $title, $fragment = '', $interwiki = '' ) {
88 if ( !is_int( $namespace ) ) {
89 throw new ParameterTypeException( '$namespace', 'int' );
93 return new static( $namespace, $title, $fragment, $interwiki );
94 } catch ( ParameterAssertionException
$ex ) {
100 * Create a TitleValue from a local PageReference.
102 * @note The PageReference may belong to another wiki. In that case, the resulting TitleValue
103 * is also logically bound to that other wiki. No attempt is made to map the
104 * PageReference wiki ID to an interwiki prefix for the TitleValue.
107 * @param PageReference $page
110 public static function newFromPage( PageReference
$page ): TitleValue
{
111 return new TitleValue( $page->getNamespace(), $page->getDBkey() );
115 * Create a TitleValue from a LinkTarget
116 * @param ParsoidLinkTarget $linkTarget
120 public static function newFromLinkTarget( ParsoidLinkTarget
$linkTarget ): TitleValue
{
121 if ( $linkTarget instanceof TitleValue
) {
124 return new TitleValue(
125 $linkTarget->getNamespace(),
126 $linkTarget->getDBkey(),
127 $linkTarget->getFragment(),
128 $linkTarget->getInterwiki()
133 * Casts a PageReference to a LinkTarget.
135 * If $page is null, null is returned.
136 * If $page is also an instance of LinkTarget, $page is returned unchanged.
140 * @param PageReference|null $page
141 * @return LinkTarget|null
143 public static function castPageToLinkTarget( ?PageReference
$page ): ?LinkTarget
{
144 if ( !$page ||
$page instanceof LinkTarget
) {
148 return self
::newFromPage( $page );
152 * Construct a TitleValue.
154 * @note TitleValue expects a valid namespace and name; typically, a TitleValue is constructed
155 * either from a database entry, or by a TitleParser. For constructing a TitleValue from user
156 * input or external sources, use a TitleParser.
159 * @param int $namespace The namespace ID. This is not validated.
160 * @param string $title The page title in either DBkey or text form. No normalization is applied
161 * beyond underscore/space conversion.
162 * @param string $fragment The fragment title. Use '' to represent the whole page.
163 * No validation or normalization is applied.
164 * @param string $interwiki The interwiki component.
165 * No validation or normalization is applied.
167 public function __construct( $namespace, $title, $fragment = '', $interwiki = '' ) {
168 self
::assertValidSpec( $namespace, $title, $fragment, $interwiki );
170 $this->namespace = $namespace;
171 $this->dbkey
= strtr( $title, ' ', '_' );
172 $this->fragment
= $fragment;
173 $this->interwiki
= $interwiki;
177 * Assert that the given parameters could be used to construct a TitleValue object.
179 * Performs basic syntax and consistency checks. Does not perform full validation,
180 * use TitleParser::makeTitleValueSafe() for that.
182 * @param int $namespace
183 * @param string $title
184 * @param string $fragment
185 * @param string $interwiki
186 * @throws InvalidArgumentException if the combination of parameters is not valid for
187 * constructing a TitleValue.
189 public static function assertValidSpec( $namespace, $title, $fragment = '', $interwiki = '' ) {
190 if ( !is_int( $namespace ) ) {
191 throw new ParameterTypeException( '$namespace', 'int' );
193 if ( !is_string( $title ) ) {
194 throw new ParameterTypeException( '$title', 'string' );
196 if ( !is_string( $fragment ) ) {
197 throw new ParameterTypeException( '$fragment', 'string' );
199 if ( !is_string( $interwiki ) ) {
200 throw new ParameterTypeException( '$interwiki', 'string' );
203 Assert
::parameter( !preg_match( '/^[_ ]|[\r\n\t]|[_ ]$/', $title ), '$title',
204 "invalid name '$title'" );
206 // NOTE: As of MW 1.34, [[#]] is rendered as a valid link, pointing to the empty
207 // page title, effectively leading to the wiki's main page. This means that a completely
208 // empty TitleValue has to be considered valid, for consistency with Title.
209 // Also note that [[#foo]] is a valid on-page section links, and that [[acme:#foo]] is
210 // a valid interwiki link.
212 $title !== '' ||
$namespace === NS_MAIN
,
214 'should not be empty unless namespace is main'
218 public function getNamespace(): int {
219 return $this->namespace;
222 public function getFragment(): string {
223 return $this->fragment
;
226 public function getDBkey(): string {
230 public function createFragmentTarget( string $fragment ): self
{
231 return new TitleValue(
239 public function getInterwiki(): string {
240 return $this->interwiki
;
244 * Returns a string representation of the title, for logging. This is purely informative
245 * and must not be used programmatically. Use the appropriate TitleFormatter to generate
246 * the correct string representation for a given use.
251 public function __toString(): string {
252 $name = $this->namespace . ':' . $this->dbkey
;
254 if ( $this->fragment
!== '' ) {
255 $name .= '#' . $this->fragment
;
258 if ( $this->interwiki
!== '' ) {
259 $name = $this->interwiki
. ':' . $name;
266 /** @deprecated class alias since 1.41 */
267 class_alias( TitleValue
::class, 'TitleValue' );