MediaWiki master
MediaWikiSite.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Site;
22
24use RuntimeException;
25
35class MediaWikiSite extends Site {
37 public const PATH_FILE = 'file_path';
39 public const PATH_PAGE = 'page_path';
40
45 public function __construct( $type = self::TYPE_MEDIAWIKI ) {
46 parent::__construct( $type );
47 }
48
56 public function toDBKey( $title ) {
57 return str_replace( ' ', '_', $title );
58 }
59
85 public function normalizePageName( $pageName, $followRedirect = MediaWikiPageNameNormalizer::FOLLOW_REDIRECT ) {
86 if ( defined( 'MW_PHPUNIT_TEST' ) || defined( 'MW_DEV_ENV' ) ) {
87 // If the code is under test, don't call out to other sites, just
88 // normalize locally.
89 // Note: this may cause results to be inconsistent with the actual
90 // normalization used by the respective remote site!
91
92 $t = Title::newFromText( $pageName );
93 return $t->getPrefixedText();
94 } else {
95 static $mediaWikiPageNameNormalizer = null;
96
97 if ( $mediaWikiPageNameNormalizer === null ) {
98 $mediaWikiPageNameNormalizer = new MediaWikiPageNameNormalizer();
99 }
100
101 return $mediaWikiPageNameNormalizer->normalizePageName(
102 $pageName,
103 $this->getFileUrl( 'api.php' ),
104 $followRedirect
105 );
106 }
107 }
108
119 public function getLinkPathType() {
120 return self::PATH_PAGE;
121 }
122
129 public function getRelativePagePath() {
130 return parse_url( $this->getPath( self::PATH_PAGE ), PHP_URL_PATH );
131 }
132
139 public function getRelativeFilePath() {
140 return parse_url( $this->getPath( self::PATH_FILE ), PHP_URL_PATH );
141 }
142
149 public function setPagePath( $path ) {
150 $this->setPath( self::PATH_PAGE, $path );
151 }
152
159 public function setFilePath( $path ) {
160 $this->setPath( self::PATH_FILE, $path );
161 }
162
176 public function getPageUrl( $pageName = false ) {
177 $url = $this->getLinkPath();
178
179 if ( $url === null ) {
180 return null;
181 }
182
183 if ( $pageName !== false ) {
184 $pageName = $this->toDBKey( trim( $pageName ) );
185 $url = str_replace( '$1', wfUrlencode( $pageName ), $url );
186 }
187
188 return $url;
189 }
190
203 public function getFileUrl( $path = false ) {
204 $filePath = $this->getPath( self::PATH_FILE );
205 if ( $filePath === null ) {
206 throw new RuntimeException( "getFileUrl called for {$this->getGlobalId()} while PATH_FILE is unset" );
207 }
208
209 if ( $path !== false ) {
210 $filePath = str_replace( '$1', $path, $filePath );
211 } else {
212 wfDeprecatedMsg( __METHOD__ . ': omitting $path is deprecated', '1.40' );
213 }
214
215 return $filePath;
216 }
217}
218
220class_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:36
getLinkPath()
Returns the path used to construct links with or false if there is no such path.
Definition Site.php:315
setPath( $pathType, string $fullUrl)
Set the path used to construct links with.
Definition Site.php:570
getPath( $pathType)
Returns the path of the provided type or null if there is no such path.
Definition Site.php:583
Represents a title within MediaWiki.
Definition Title.php:78