MediaWikiSite.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. use MediaWiki\Site\MediaWikiPageNameNormalizer;
  3. /**
  4. * Class representing a MediaWiki site.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. * @ingroup Site
  23. * @license GPL-2.0-or-later
  24. * @author John Erling Blad < jeblad@gmail.com >
  25. * @author Daniel Kinzler
  26. * @author Jeroen De Dauw < jeroendedauw@gmail.com >
  27. */
  28. /**
  29. * Class representing a MediaWiki site.
  30. *
  31. * @since 1.21
  32. *
  33. * @ingroup Site
  34. */
  35. class MediaWikiSite extends Site {
  36. const PATH_FILE = 'file_path';
  37. const PATH_PAGE = 'page_path';
  38. /**
  39. * @since 1.21
  40. *
  41. * @param string $type
  42. */
  43. public function __construct( $type = self::TYPE_MEDIAWIKI ) {
  44. parent::__construct( $type );
  45. }
  46. /**
  47. * Returns the database form of the given title.
  48. *
  49. * @since 1.21
  50. *
  51. * @param string $title The target page's title, in normalized form.
  52. *
  53. * @return string
  54. */
  55. public function toDBKey( $title ) {
  56. return str_replace( ' ', '_', $title );
  57. }
  58. /**
  59. * Returns the normalized form of the given page title, using the
  60. * normalization rules of the given site. If the given title is a redirect,
  61. * the redirect will be resolved and the redirect target is returned.
  62. * Only titles of existing pages will be returned.
  63. *
  64. * @note This actually makes an API request to the remote site, so beware
  65. * that this function is slow and depends on an external service.
  66. *
  67. * @note If MW_PHPUNIT_TEST is defined, the call to the external site is
  68. * skipped, and the title is normalized using the local normalization
  69. * rules as implemented by the Title class.
  70. *
  71. * @see Site::normalizePageName
  72. *
  73. * @since 1.21
  74. *
  75. * @param string $pageName
  76. *
  77. * @return string|false The normalized form of the title,
  78. * or false to indicate an invalid title, a missing page,
  79. * or some other kind of error.
  80. * @throws MWException
  81. */
  82. public function normalizePageName( $pageName ) {
  83. if ( defined( 'MW_PHPUNIT_TEST' ) ) {
  84. // If the code is under test, don't call out to other sites, just
  85. // normalize locally.
  86. // Note: this may cause results to be inconsistent with the actual
  87. // normalization used by the respective remote site!
  88. $t = Title::newFromText( $pageName );
  89. return $t->getPrefixedText();
  90. } else {
  91. static $mediaWikiPageNameNormalizer = null;
  92. if ( $mediaWikiPageNameNormalizer === null ) {
  93. $mediaWikiPageNameNormalizer = new MediaWikiPageNameNormalizer();
  94. }
  95. return $mediaWikiPageNameNormalizer->normalizePageName(
  96. $pageName,
  97. $this->getFileUrl( 'api.php' )
  98. );
  99. }
  100. }
  101. /**
  102. * @see Site::getLinkPathType
  103. * Returns Site::PATH_PAGE
  104. *
  105. * @since 1.21
  106. *
  107. * @return string
  108. */
  109. public function getLinkPathType() {
  110. return self::PATH_PAGE;
  111. }
  112. /**
  113. * Returns the relative page path.
  114. *
  115. * @since 1.21
  116. *
  117. * @return string
  118. */
  119. public function getRelativePagePath() {
  120. return parse_url( $this->getPath( self::PATH_PAGE ), PHP_URL_PATH );
  121. }
  122. /**
  123. * Returns the relative file path.
  124. *
  125. * @since 1.21
  126. *
  127. * @return string
  128. */
  129. public function getRelativeFilePath() {
  130. return parse_url( $this->getPath( self::PATH_FILE ), PHP_URL_PATH );
  131. }
  132. /**
  133. * Sets the relative page path.
  134. *
  135. * @since 1.21
  136. *
  137. * @param string $path
  138. */
  139. public function setPagePath( $path ) {
  140. $this->setPath( self::PATH_PAGE, $path );
  141. }
  142. /**
  143. * Sets the relative file path.
  144. *
  145. * @since 1.21
  146. *
  147. * @param string $path
  148. */
  149. public function setFilePath( $path ) {
  150. $this->setPath( self::PATH_FILE, $path );
  151. }
  152. /**
  153. * @see Site::getPageUrl
  154. *
  155. * This implementation returns a URL constructed using the path returned by getLinkPath().
  156. * In addition to the default behavior implemented by Site::getPageUrl(), this
  157. * method converts the $pageName to DBKey-format by replacing spaces with underscores
  158. * before using it in the URL.
  159. *
  160. * @since 1.21
  161. *
  162. * @param string|bool $pageName Page name or false (default: false)
  163. *
  164. * @return string|null
  165. */
  166. public function getPageUrl( $pageName = false ) {
  167. $url = $this->getLinkPath();
  168. if ( $url === null ) {
  169. return null;
  170. }
  171. if ( $pageName !== false ) {
  172. $pageName = $this->toDBKey( trim( $pageName ) );
  173. $url = str_replace( '$1', wfUrlencode( $pageName ), $url );
  174. }
  175. return $url;
  176. }
  177. /**
  178. * Returns the full file path (ie site url + relative file path).
  179. * The path should go at the $1 marker. If the $path
  180. * argument is provided, the marker will be replaced by it's value.
  181. *
  182. * @since 1.21
  183. *
  184. * @param string|bool $path
  185. *
  186. * @return string
  187. */
  188. public function getFileUrl( $path = false ) {
  189. $filePath = $this->getPath( self::PATH_FILE );
  190. if ( $filePath !== false ) {
  191. $filePath = str_replace( '$1', $path, $filePath );
  192. }
  193. return $filePath;
  194. }
  195. }