| 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 | namespace MediaWiki\WikiMap; |
| 24 | |
| 25 | /** |
| 26 | * Reference to a locally-hosted wiki |
| 27 | */ |
| 28 | class WikiReference { |
| 29 | /** @var string wgCanonicalServer, e.g. 'https://www.mediawiki.org' */ |
| 30 | private string $mCanonicalServer; |
| 31 | /** @var string wgServer, may be protocol-relative, e.g. '//www.mediawiki.org' */ |
| 32 | private string $mServer; |
| 33 | /** @var string wgArticlepath, e.g. '/wiki/$1' */ |
| 34 | private string $mPath; |
| 35 | |
| 36 | /** |
| 37 | * @param string $canonicalServer |
| 38 | * @param string $path |
| 39 | * @param null|string $server |
| 40 | */ |
| 41 | public function __construct( string $canonicalServer, string $path, ?string $server = null ) { |
| 42 | $this->mCanonicalServer = $canonicalServer; |
| 43 | $this->mPath = $path; |
| 44 | $this->mServer = $server ?? $canonicalServer; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get the URL in a way to be displayed to the user |
| 49 | * More or less Wikimedia specific |
| 50 | * |
| 51 | * @return string |
| 52 | */ |
| 53 | public function getDisplayName() { |
| 54 | $host = parse_url( $this->mCanonicalServer, PHP_URL_HOST ); |
| 55 | if ( $host ) { |
| 56 | return $host; |
| 57 | } else { |
| 58 | // Invalid server spec. |
| 59 | // There's no sensible thing to do here, so just return the canonical server name in full. |
| 60 | return $this->mCanonicalServer; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Helper function for getUrl() |
| 66 | * |
| 67 | * @todo FIXME: This may be generalized... |
| 68 | * |
| 69 | * @param string $page Page name (must be normalised before calling this function! |
| 70 | * May contain a section part.) |
| 71 | * @param string|null $fragmentId |
| 72 | * |
| 73 | * @return string relative URL, without the server part. |
| 74 | */ |
| 75 | private function getLocalUrl( $page, $fragmentId = null ) { |
| 76 | $page = wfUrlencode( str_replace( ' ', '_', $page ) ); |
| 77 | |
| 78 | if ( is_string( $fragmentId ) && $fragmentId !== '' ) { |
| 79 | $page .= '#' . wfUrlencode( $fragmentId ); |
| 80 | } |
| 81 | |
| 82 | return str_replace( '$1', $page, $this->mPath ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki |
| 87 | * |
| 88 | * @param string $page Page name (must be normalised before calling this function!) |
| 89 | * @param string|null $fragmentId |
| 90 | * |
| 91 | * @return string Url |
| 92 | */ |
| 93 | public function getCanonicalUrl( $page, $fragmentId = null ) { |
| 94 | return $this->mCanonicalServer . $this->getLocalUrl( $page, $fragmentId ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get a canonical server URL |
| 99 | * @return string |
| 100 | */ |
| 101 | public function getCanonicalServer() { |
| 102 | return $this->mCanonicalServer; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Alias for getCanonicalUrl(), for backwards compatibility. |
| 107 | * @param string $page |
| 108 | * @param string|null $fragmentId |
| 109 | * |
| 110 | * @return string |
| 111 | */ |
| 112 | public function getUrl( $page, $fragmentId = null ) { |
| 113 | return $this->getCanonicalUrl( $page, $fragmentId ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get a URL based on $wgServer, like Title::getFullURL() would produce |
| 118 | * when called locally on the wiki. |
| 119 | * |
| 120 | * @param string $page Page name (must be normalized before calling this function!) |
| 121 | * @param string|null $fragmentId |
| 122 | * |
| 123 | * @return string URL |
| 124 | */ |
| 125 | public function getFullUrl( $page, $fragmentId = null ) { |
| 126 | return $this->mServer . $this->getLocalUrl( $page, $fragmentId ); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** @deprecated class alias since 1.40 */ |
| 131 | class_alias( WikiReference::class, 'WikiReference' ); |