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 
92  public function normalizePageName( $pageName, $followRedirect = MediaWikiPageNameNormalizer::FOLLOW_REDIRECT ) {
93  if ( defined( 'MW_PHPUNIT_TEST' ) || defined( 'MW_DEV_ENV' ) ) {
94  // If the code is under test, don't call out to other sites, just
95  // normalize locally.
96  // Note: this may cause results to be inconsistent with the actual
97  // normalization used by the respective remote site!
98 
99  $t = Title::newFromText( $pageName );
100  return $t->getPrefixedText();
101  } else {
102  static $mediaWikiPageNameNormalizer = null;
103 
104  if ( $mediaWikiPageNameNormalizer === null ) {
105  $mediaWikiPageNameNormalizer = new MediaWikiPageNameNormalizer();
106  }
107 
108  return $mediaWikiPageNameNormalizer->normalizePageName(
109  $pageName,
110  $this->getFileUrl( 'api.php' ),
111  $followRedirect
112  );
113  }
114  }
115 
124  public function getLinkPathType() {
125  return self::PATH_PAGE;
126  }
127 
135  public function getRelativePagePath() {
136  return parse_url( $this->getPath( self::PATH_PAGE ), PHP_URL_PATH );
137  }
138 
146  public function getRelativeFilePath() {
147  return parse_url( $this->getPath( self::PATH_FILE ), PHP_URL_PATH );
148  }
149 
157  public function setPagePath( $path ) {
158  $this->setPath( self::PATH_PAGE, $path );
159  }
160 
168  public function setFilePath( $path ) {
169  $this->setPath( self::PATH_FILE, $path );
170  }
171 
186  public function getPageUrl( $pageName = false ) {
187  $url = $this->getLinkPath();
188 
189  if ( $url === null ) {
190  return null;
191  }
192 
193  if ( $pageName !== false ) {
194  $pageName = $this->toDBKey( trim( $pageName ) );
195  $url = str_replace( '$1', wfUrlencode( $pageName ), $url );
196  }
197 
198  return $url;
199  }
200 
212  public function getFileUrl( $path = false ) {
213  $filePath = $this->getPath( self::PATH_FILE );
214 
215  if ( $filePath !== false ) {
216  $filePath = str_replace( '$1', $path, $filePath );
217  }
218 
219  return $filePath;
220  }
221 }
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:212
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:415
MediaWikiSite\getRelativeFilePath
getRelativeFilePath()
Returns the relative file path.
Definition: MediaWikiSite.php:146
MediaWikiSite\getRelativePagePath
getRelativePagePath()
Returns the relative page path.
Definition: MediaWikiSite.php:135
wfUrlencode
wfUrlencode( $s)
We want some things to be included as literal characters in our title URLs for prettiness,...
Definition: GlobalFunctions.php:292
Site\getPath
getPath( $pathType)
Returns the path of the provided type or false if there is no such path.
Definition: Site.php:619
Site\$type
string $type
Definition: Site.php:66
MediaWikiSite\setPagePath
setPagePath( $path)
Sets the relative page path.
Definition: MediaWikiSite.php:157
MediaWikiSite\getLinkPathType
getLinkPathType()
Definition: MediaWikiSite.php:124
MediaWikiSite\setFilePath
setFilePath( $path)
Sets the relative file path.
Definition: MediaWikiSite.php:168
$title
$title
Definition: testCompression.php:38
MediaWikiSite\PATH_FILE
const PATH_FILE
Definition: MediaWikiSite.php:39
Site
Definition: Site.php:32
MediaWiki\Site\MediaWikiPageNameNormalizer
Service for normalizing a page name using a MediaWiki api.
Definition: MediaWikiPageNameNormalizer.php:36
MediaWikiSite\normalizePageName
normalizePageName( $pageName, $followRedirect=MediaWikiPageNameNormalizer::FOLLOW_REDIRECT)
Returns the normalized form of the given page title, using the normalization rules of the given site.
Definition: MediaWikiSite.php:92
Site\setPath
setPath( $pathType, $fullUrl)
Sets the path used to construct links with.
Definition: Site.php:598
MediaWikiSite\__construct
__construct( $type=self::TYPE_MEDIAWIKI)
Definition: MediaWikiSite.php:47
MediaWikiSite\getPageUrl
getPageUrl( $pageName=false)
Definition: MediaWikiSite.php:186
MediaWikiSite\PATH_PAGE
const PATH_PAGE
Definition: MediaWikiSite.php:40
$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:337