MediaWiki  master
MediaWikiSite.php
Go to the documentation of this file.
1 <?php
2 
4 
38 class MediaWikiSite extends Site {
39  public const PATH_FILE = 'file_path';
40  public const PATH_PAGE = 'page_path';
41 
47  public function __construct( $type = self::TYPE_MEDIAWIKI ) {
48  parent::__construct( $type );
49  }
50 
60  public function toDBKey( $title ) {
61  return str_replace( ' ', '_', $title );
62  }
63 
88  public function normalizePageName( $pageName ) {
89  if ( defined( 'MW_PHPUNIT_TEST' ) || defined( 'MW_DEV_ENV' ) ) {
90  // If the code is under test, don't call out to other sites, just
91  // normalize locally.
92  // Note: this may cause results to be inconsistent with the actual
93  // normalization used by the respective remote site!
94 
95  $t = Title::newFromText( $pageName );
96  return $t->getPrefixedText();
97  } else {
98  static $mediaWikiPageNameNormalizer = null;
99 
100  if ( $mediaWikiPageNameNormalizer === null ) {
101  $mediaWikiPageNameNormalizer = new MediaWikiPageNameNormalizer();
102  }
103 
104  return $mediaWikiPageNameNormalizer->normalizePageName(
105  $pageName,
106  $this->getFileUrl( 'api.php' )
107  );
108  }
109  }
110 
119  public function getLinkPathType() {
120  return self::PATH_PAGE;
121  }
122 
130  public function getRelativePagePath() {
131  return parse_url( $this->getPath( self::PATH_PAGE ), PHP_URL_PATH );
132  }
133 
141  public function getRelativeFilePath() {
142  return parse_url( $this->getPath( self::PATH_FILE ), PHP_URL_PATH );
143  }
144 
152  public function setPagePath( $path ) {
153  $this->setPath( self::PATH_PAGE, $path );
154  }
155 
163  public function setFilePath( $path ) {
164  $this->setPath( self::PATH_FILE, $path );
165  }
166 
181  public function getPageUrl( $pageName = false ) {
182  $url = $this->getLinkPath();
183 
184  if ( $url === null ) {
185  return null;
186  }
187 
188  if ( $pageName !== false ) {
189  $pageName = $this->toDBKey( trim( $pageName ) );
190  $url = str_replace( '$1', wfUrlencode( $pageName ), $url );
191  }
192 
193  return $url;
194  }
195 
207  public function getFileUrl( $path = false ) {
208  $filePath = $this->getPath( self::PATH_FILE );
209 
210  if ( $filePath !== false ) {
211  $filePath = str_replace( '$1', $path, $filePath );
212  }
213 
214  return $filePath;
215  }
216 }
MediaWikiSite\toDBKey
toDBKey( $title)
Returns the database form of the given title.
Definition: MediaWikiSite.php:60
MediaWikiSite\getFileUrl
getFileUrl( $path=false)
Returns the full file path (ie site url + relative file path).
Definition: MediaWikiSite.php:207
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:332
MediaWikiSite\getRelativeFilePath
getRelativeFilePath()
Returns the relative file path.
Definition: MediaWikiSite.php:141
MediaWikiSite\getRelativePagePath
getRelativePagePath()
Returns the relative page path.
Definition: MediaWikiSite.php:130
wfUrlencode
wfUrlencode( $s)
We want some things to be included as literal characters in our title URLs for prettiness,...
Definition: GlobalFunctions.php:309
Site\getPath
getPath( $pathType)
Returns the path of the provided type or false if there is no such path.
Definition: Site.php:615
Site\$type
string $type
Definition: Site.php:65
MediaWikiSite\setPagePath
setPagePath( $path)
Sets the relative page path.
Definition: MediaWikiSite.php:152
MediaWikiSite\getLinkPathType
getLinkPathType()
Definition: MediaWikiSite.php:119
MediaWikiSite\setFilePath
setFilePath( $path)
Sets the relative file path.
Definition: MediaWikiSite.php:163
$title
$title
Definition: testCompression.php:38
MediaWikiSite\PATH_FILE
const PATH_FILE
Definition: MediaWikiSite.php:39
Site
Definition: Site.php:31
MediaWiki\Site\MediaWikiPageNameNormalizer
Service for normalizing a page name using a MediaWiki api.
Definition: MediaWikiPageNameNormalizer.php:35
Site\setPath
setPath( $pathType, $fullUrl)
Sets the path used to construct links with.
Definition: Site.php:594
MediaWikiSite\__construct
__construct( $type=self::TYPE_MEDIAWIKI)
Definition: MediaWikiSite.php:47
MediaWikiSite\getPageUrl
getPageUrl( $pageName=false)
Definition: MediaWikiSite.php:181
MediaWikiSite\PATH_PAGE
const PATH_PAGE
Definition: MediaWikiSite.php:40
MediaWikiSite\normalizePageName
normalizePageName( $pageName)
Returns the normalized form of the given page title, using the normalization rules of the given site.
Definition: MediaWikiSite.php:88
$path
$path
Definition: NoLocalSettings.php:25
MediaWikiSite
Class representing a MediaWiki site.
Definition: MediaWikiSite.php:38
$t
$t
Definition: testCompression.php:74
Site\getLinkPath
getLinkPath()
Returns the path used to construct links with or false if there is no such path.
Definition: Site.php:336