WikiReference.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Tools for dealing with other locally-hosted wikis.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * Reference to a locally-hosted wiki
  24. */
  25. class WikiReference {
  26. private $mCanonicalServer; ///< canonical server URL, e.g. 'https://www.mediawiki.org'
  27. private $mServer; ///< server URL, may be protocol-relative, e.g. '//www.mediawiki.org'
  28. private $mPath; ///< path, '/wiki/$1'
  29. /**
  30. * @param string $canonicalServer
  31. * @param string $path
  32. * @param null|string $server
  33. */
  34. public function __construct( $canonicalServer, $path, $server = null ) {
  35. $this->mCanonicalServer = $canonicalServer;
  36. $this->mPath = $path;
  37. $this->mServer = $server ?? $canonicalServer;
  38. }
  39. /**
  40. * Get the URL in a way to be displayed to the user
  41. * More or less Wikimedia specific
  42. *
  43. * @return string
  44. */
  45. public function getDisplayName() {
  46. $parsed = wfParseUrl( $this->mCanonicalServer );
  47. if ( $parsed ) {
  48. return $parsed['host'];
  49. } else {
  50. // Invalid server spec.
  51. // There's no sane thing to do here, so just return the canonical server name in full.
  52. return $this->mCanonicalServer;
  53. }
  54. }
  55. /**
  56. * Helper function for getUrl()
  57. *
  58. * @todo FIXME: This may be generalized...
  59. *
  60. * @param string $page Page name (must be normalised before calling this function!
  61. * May contain a section part.)
  62. * @param string|null $fragmentId
  63. *
  64. * @return string relative URL, without the server part.
  65. */
  66. private function getLocalUrl( $page, $fragmentId = null ) {
  67. $page = wfUrlencode( str_replace( ' ', '_', $page ) );
  68. if ( is_string( $fragmentId ) && $fragmentId !== '' ) {
  69. $page .= '#' . wfUrlencode( $fragmentId );
  70. }
  71. return str_replace( '$1', $page, $this->mPath );
  72. }
  73. /**
  74. * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki
  75. *
  76. * @param string $page Page name (must be normalised before calling this function!)
  77. * @param string|null $fragmentId
  78. *
  79. * @return string Url
  80. */
  81. public function getCanonicalUrl( $page, $fragmentId = null ) {
  82. return $this->mCanonicalServer . $this->getLocalUrl( $page, $fragmentId );
  83. }
  84. /**
  85. * Get a canonical server URL
  86. * @return string
  87. */
  88. public function getCanonicalServer() {
  89. return $this->mCanonicalServer;
  90. }
  91. /**
  92. * Alias for getCanonicalUrl(), for backwards compatibility.
  93. * @param string $page
  94. * @param string|null $fragmentId
  95. *
  96. * @return string
  97. */
  98. public function getUrl( $page, $fragmentId = null ) {
  99. return $this->getCanonicalUrl( $page, $fragmentId );
  100. }
  101. /**
  102. * Get a URL based on $wgServer, like Title::getFullURL() would produce
  103. * when called locally on the wiki.
  104. *
  105. * @param string $page Page name (must be normalized before calling this function!)
  106. * @param string|null $fragmentId
  107. *
  108. * @return string URL
  109. */
  110. public function getFullUrl( $page, $fragmentId = null ) {
  111. return $this->mServer .
  112. $this->getLocalUrl( $page, $fragmentId );
  113. }
  114. }