]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/WikiReference.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / WikiReference.php
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 /**
24  * Reference to a locally-hosted wiki
25  */
26 class WikiReference {
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'
30
31         /**
32          * @param string $canonicalServer
33          * @param string $path
34          * @param null|string $server
35          */
36         public function __construct( $canonicalServer, $path, $server = null ) {
37                 $this->mCanonicalServer = $canonicalServer;
38                 $this->mPath = $path;
39                 $this->mServer = $server === null ? $canonicalServer : $server;
40         }
41
42         /**
43          * Get the URL in a way to be displayed to the user
44          * More or less Wikimedia specific
45          *
46          * @return string
47          */
48         public function getDisplayName() {
49                 $parsed = wfParseUrl( $this->mCanonicalServer );
50                 if ( $parsed ) {
51                         return $parsed['host'];
52                 } else {
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;
56                 }
57         }
58
59         /**
60          * Helper function for getUrl()
61          *
62          * @todo FIXME: This may be generalized...
63          *
64          * @param string $page Page name (must be normalised before calling this function!
65          *  May contain a section part.)
66          * @param string|null $fragmentId
67          *
68          * @return string relative URL, without the server part.
69          */
70         private function getLocalUrl( $page, $fragmentId = null ) {
71                 $page = wfUrlencode( str_replace( ' ', '_', $page ) );
72
73                 if ( is_string( $fragmentId ) && $fragmentId !== '' ) {
74                         $page .= '#' . wfUrlencode( $fragmentId );
75                 }
76
77                 return str_replace( '$1', $page, $this->mPath );
78         }
79
80         /**
81          * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki
82          *
83          * @param string $page Page name (must be normalised before calling this function!)
84          * @param string|null $fragmentId
85          *
86          * @return string Url
87          */
88         public function getCanonicalUrl( $page, $fragmentId = null ) {
89                 return $this->mCanonicalServer . $this->getLocalUrl( $page, $fragmentId );
90         }
91
92         /**
93          * Get a canonical server URL
94          * @return string
95          */
96         public function getCanonicalServer() {
97                 return $this->mCanonicalServer;
98         }
99
100         /**
101          * Alias for getCanonicalUrl(), for backwards compatibility.
102          * @param string $page
103          * @param string|null $fragmentId
104          *
105          * @return string
106          */
107         public function getUrl( $page, $fragmentId = null ) {
108                 return $this->getCanonicalUrl( $page, $fragmentId );
109         }
110
111         /**
112          * Get a URL based on $wgServer, like Title::getFullURL() would produce
113          * when called locally on the wiki.
114          *
115          * @param string $page Page name (must be normalized before calling this function!)
116          * @param string|null $fragmentId
117          *
118          * @return string URL
119          */
120         public function getFullUrl( $page, $fragmentId = null ) {
121                 return $this->mServer .
122                         $this->getLocalUrl( $page, $fragmentId );
123         }
124 }