3 * Tools for dealing with other locally-hosted wikis.
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.
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.
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
24 * Reference to a locally-hosted wiki
27 private $mCanonicalServer; ///< canonical server URL, e.g. 'https://www.mediawiki.org'
28 private $mServer; ///< server URL, may be protocol-relative, e.g. '//www.mediawiki.org'
29 private $mPath; ///< path, '/wiki/$1'
32 * @param string $canonicalServer
34 * @param null|string $server
36 public function __construct( $canonicalServer, $path, $server = null ) {
37 $this->mCanonicalServer
= $canonicalServer;
39 $this->mServer
= $server ??
$canonicalServer;
43 * Get the URL in a way to be displayed to the user
44 * More or less Wikimedia specific
48 public function getDisplayName() {
49 $parsed = wfParseUrl( $this->mCanonicalServer
);
51 return $parsed['host'];
53 // Invalid server spec.
54 // There's no sane thing to do here, so just return the canonical server name in full.
55 return $this->mCanonicalServer
;
60 * Helper function for getUrl()
62 * @todo FIXME: This may be generalized...
64 * @param string $page Page name (must be normalised before calling this function!
65 * May contain a section part.)
66 * @param string|null $fragmentId
68 * @return string relative URL, without the server part.
70 private function getLocalUrl( $page, $fragmentId = null ) {
71 $page = wfUrlencode( str_replace( ' ', '_', $page ) );
73 if ( is_string( $fragmentId ) && $fragmentId !== '' ) {
74 $page .= '#' . wfUrlencode( $fragmentId );
77 return str_replace( '$1', $page, $this->mPath
);
81 * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki
83 * @param string $page Page name (must be normalised before calling this function!)
84 * @param string|null $fragmentId
88 public function getCanonicalUrl( $page, $fragmentId = null ) {
89 return $this->mCanonicalServer
. $this->getLocalUrl( $page, $fragmentId );
93 * Get a canonical server URL
96 public function getCanonicalServer() {
97 return $this->mCanonicalServer
;
101 * Alias for getCanonicalUrl(), for backwards compatibility.
102 * @param string $page
103 * @param string|null $fragmentId
107 public function getUrl( $page, $fragmentId = null ) {
108 return $this->getCanonicalUrl( $page, $fragmentId );
112 * Get a URL based on $wgServer, like Title::getFullURL() would produce
113 * when called locally on the wiki.
115 * @param string $page Page name (must be normalized before calling this function!)
116 * @param string|null $fragmentId
120 public function getFullUrl( $page, $fragmentId = null ) {
121 return $this->mServer
.
122 $this->getLocalUrl( $page, $fragmentId );