MediaWiki master
MediaWikiSite.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Site;
8
10use RuntimeException;
11
21class MediaWikiSite extends Site {
23 public const PATH_FILE = 'file_path';
25 public const PATH_PAGE = 'page_path';
26
31 public function __construct( $type = self::TYPE_MEDIAWIKI ) {
32 parent::__construct( $type );
33 }
34
42 public function toDBKey( $title ) {
43 return str_replace( ' ', '_', $title );
44 }
45
71 public function normalizePageName( $pageName, $followRedirect = MediaWikiPageNameNormalizer::FOLLOW_REDIRECT ) {
72 if ( defined( 'MW_PHPUNIT_TEST' ) || defined( 'MW_DEV_ENV' ) ) {
73 // If the code is under test, don't call out to other sites, just
74 // normalize locally.
75 // Note: this may cause results to be inconsistent with the actual
76 // normalization used by the respective remote site!
77
78 $t = Title::newFromText( $pageName );
79 return $t->getPrefixedText();
80 } else {
81 static $mediaWikiPageNameNormalizer = null;
82 $mediaWikiPageNameNormalizer ??= new MediaWikiPageNameNormalizer();
83
84 return $mediaWikiPageNameNormalizer->normalizePageName(
85 $pageName,
86 $this->getFileUrl( 'api.php' ),
87 $followRedirect
88 );
89 }
90 }
91
102 public function getLinkPathType() {
103 return self::PATH_PAGE;
104 }
105
112 public function getRelativePagePath() {
113 return parse_url( $this->getPath( self::PATH_PAGE ), PHP_URL_PATH );
114 }
115
122 public function getRelativeFilePath() {
123 return parse_url( $this->getPath( self::PATH_FILE ), PHP_URL_PATH );
124 }
125
132 public function setPagePath( $path ) {
133 $this->setPath( self::PATH_PAGE, $path );
134 }
135
142 public function setFilePath( $path ) {
143 $this->setPath( self::PATH_FILE, $path );
144 }
145
159 public function getPageUrl( $pageName = false ) {
160 $url = $this->getLinkPath();
161
162 if ( $url === null ) {
163 return null;
164 }
165
166 if ( $pageName !== false ) {
167 $pageName = $this->toDBKey( trim( $pageName ) );
168 $url = str_replace( '$1', wfUrlencode( $pageName ), $url );
169 }
170
171 return $url;
172 }
173
186 public function getFileUrl( $path = false ) {
187 $filePath = $this->getPath( self::PATH_FILE );
188 if ( $filePath === null ) {
189 throw new RuntimeException( "getFileUrl called for {$this->getGlobalId()} while PATH_FILE is unset" );
190 }
191
192 if ( $path !== false ) {
193 $filePath = str_replace( '$1', $path, $filePath );
194 } else {
195 wfDeprecatedMsg( __METHOD__ . ': omitting $path is deprecated', '1.40' );
196 }
197
198 return $filePath;
199 }
200}
201
203class_alias( MediaWikiSite::class, 'MediaWikiSite' );
wfUrlencode( $s)
We want some things to be included as literal characters in our title URLs for prettiness,...
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
Service for normalizing a page name via a MediaWiki action API.
Class representing a MediaWiki site.
setPagePath( $path)
Set the article path.
getRelativeFilePath()
Get the script script, as relative path only (without server).
__construct( $type=self::TYPE_MEDIAWIKI)
normalizePageName( $pageName, $followRedirect=MediaWikiPageNameNormalizer::FOLLOW_REDIRECT)
Get the normalized form of the given page title.
getLinkPathType()
Get the constant for getting or setting the script path.
getRelativePagePath()
Get the article path, as relative path only (without server).
toDBKey( $title)
Get the database form of the given title.
const PATH_FILE
The script path of a site, e.g.
setFilePath( $path)
Set the script path.
const PATH_PAGE
The article path of a site, e.g.
getFileUrl( $path=false)
Get the full URL to an entry point under a wiki's script path.
getPageUrl( $pageName=false)
Get the full URL for the given page on the site.
Represents a single site.
Definition Site.php:22
getLinkPath()
Returns the path used to construct links with or false if there is no such path.
Definition Site.php:296
setPath( $pathType, string $fullUrl)
Set the path used to construct links with.
Definition Site.php:545
getPath( $pathType)
Returns the path of the provided type or null if there is no such path.
Definition Site.php:558
Represents a title within MediaWiki.
Definition Title.php:69