Add support for fragments to WikiMap
[lhc/web/wiklou.git] / includes / WikiMap.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 * Helper tools for dealing with other locally-hosted wikis
25 */
26 class WikiMap {
27
28 /**
29 * Get a WikiReference object for $wikiID
30 *
31 * @param string $wikiID Wiki'd id (generally database name)
32 * @return WikiReference|null WikiReference object or null if the wiki was not found
33 */
34 public static function getWiki( $wikiID ) {
35 global $wgConf;
36
37 $wgConf->loadFullData();
38
39 list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
40 if ( $major === null ) {
41 return null;
42 }
43 $server = $wgConf->get( 'wgServer', $wikiID, $major,
44 array( 'lang' => $minor, 'site' => $major ) );
45
46 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
47 array( 'lang' => $minor, 'site' => $major ) );
48 if ( $canonicalServer === false || $canonicalServer === null ) {
49 $canonicalServer = $server;
50 }
51
52 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
53 array( 'lang' => $minor, 'site' => $major ) );
54 return new WikiReference( $major, $minor, $canonicalServer, $path, $server );
55 }
56
57 /**
58 * Convenience to get the wiki's display name
59 *
60 * @todo We can give more info than just the wiki id!
61 * @param string $wikiID Wiki'd id (generally database name)
62 * @return string|int Wiki's name or $wiki_id if the wiki was not found
63 */
64 public static function getWikiName( $wikiID ) {
65 $wiki = WikiMap::getWiki( $wikiID );
66
67 if ( $wiki ) {
68 return $wiki->getDisplayName();
69 }
70 return $wikiID;
71 }
72
73 /**
74 * Convenience to get a link to a user page on a foreign wiki
75 *
76 * @param string $wikiID Wiki'd id (generally database name)
77 * @param string $user User name (must be normalised before calling this function!)
78 * @param string $text Link's text; optional, default to "User:$user"
79 * @return string HTML link or false if the wiki was not found
80 */
81 public static function foreignUserLink( $wikiID, $user, $text = null ) {
82 return self::makeForeignLink( $wikiID, "User:$user", $text );
83 }
84
85 /**
86 * Convenience to get a link to a page on a foreign wiki
87 *
88 * @param string $wikiID Wiki'd id (generally database name)
89 * @param string $page Page name (must be normalised before calling this function!)
90 * @param string $text Link's text; optional, default to $page
91 * @return string HTML link or false if the wiki was not found
92 */
93 public static function makeForeignLink( $wikiID, $page, $text = null ) {
94 if ( !$text ) {
95 $text = $page;
96 }
97
98 $url = self::getForeignURL( $wikiID, $page );
99 if ( $url === false ) {
100 return false;
101 }
102
103 return Linker::makeExternalLink( $url, $text );
104 }
105
106 /**
107 * Convenience to get a url to a page on a foreign wiki
108 *
109 * @param string $wikiID Wiki'd id (generally database name)
110 * @param string $page Page name (must be normalised before calling this function!)
111 * @param string|null $fragmentId
112 *
113 * @return string|bool URL or false if the wiki was not found
114 */
115 public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
116 $wiki = WikiMap::getWiki( $wikiID );
117
118 if ( $wiki ) {
119 return $wiki->getFullUrl( $page, $fragmentId );
120 }
121
122 return false;
123 }
124 }
125
126 /**
127 * Reference to a locally-hosted wiki
128 */
129 class WikiReference {
130 private $mMinor; ///< 'en', 'meta', 'mediawiki', etc
131 private $mMajor; ///< 'wiki', 'wiktionary', etc
132 private $mCanonicalServer; ///< canonical server URL, e.g. 'https://www.mediawiki.org'
133 private $mServer; ///< server URL, may be protocol-relative, e.g. '//www.mediawiki.org'
134 private $mPath; ///< path, '/wiki/$1'
135
136 /**
137 * @param string $major
138 * @param string $minor
139 * @param string $canonicalServer
140 * @param string $path
141 * @param null|string $server
142 */
143 public function __construct( $major, $minor, $canonicalServer, $path, $server = null ) {
144 $this->mMajor = $major;
145 $this->mMinor = $minor;
146 $this->mCanonicalServer = $canonicalServer;
147 $this->mPath = $path;
148 $this->mServer = $server === null ? $canonicalServer : $server;
149 }
150
151 /**
152 * @return string
153 * @throws MWException
154 */
155 public function getHostname() {
156 $prefixes = array( 'http://', 'https://' );
157 foreach ( $prefixes as $prefix ) {
158 if ( substr( $this->mCanonicalServer, 0, strlen( $prefix ) ) ) {
159 return substr( $this->mCanonicalServer, strlen( $prefix ) );
160 }
161 }
162 throw new MWException( "Invalid hostname for wiki {$this->mMinor}.{$this->mMajor}" );
163 }
164
165 /**
166 * Get the URL in a way to be displayed to the user
167 * More or less Wikimedia specific
168 *
169 * @return string
170 */
171 public function getDisplayName() {
172 $url = $this->getUrl( '' );
173 $parsed = wfParseUrl( $url );
174 if ( $parsed ) {
175 return $parsed['host'];
176 } else {
177 // Invalid URL. There's no sane thing to do here, so just return it
178 return $url;
179 }
180 }
181
182 /**
183 * Helper function for getUrl()
184 *
185 * @todo FIXME: This may be generalized...
186 *
187 * @param string $page Page name (must be normalised before calling this function! May contain a section part.)
188 * @param string|null $fragmentId
189 *
190 * @return string relative URL, without the server part.
191 */
192 private function getLocalUrl( $page, $fragmentId = null ) {
193 $page = wfUrlEncode( str_replace( ' ', '_', $page ) );
194
195 if ( is_string( $fragmentId ) && $fragmentId !== '' ) {
196 $page .= '#' . wfUrlEncode( $fragmentId );
197 }
198
199 return str_replace( '$1', $page, $this->mPath );
200 }
201
202 /**
203 * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki
204 *
205 * @param string $page Page name (must be normalised before calling this function!)
206 * @param string|null $fragmentId
207 *
208 * @return string Url
209 */
210 public function getCanonicalUrl( $page, $fragmentId = null ) {
211 return $this->mCanonicalServer . $this->getLocalUrl( $page, $fragmentId );
212 }
213
214 /**
215 * Get a canonical server URL
216 * @return string
217 */
218 public function getCanonicalServer() {
219 return $this->mCanonicalServer;
220 }
221
222 /**
223 * Alias for getCanonicalUrl(), for backwards compatibility.
224 * @param string $page
225 * @param string|null $fragmentId
226 *
227 * @return string
228 */
229 public function getUrl( $page, $fragmentId = null ) {
230 return $this->getCanonicalUrl( $page, $fragmentId );
231 }
232
233 /**
234 * Get a URL based on $wgServer, like Title::getFullURL() would produce
235 * when called locally on the wiki.
236 *
237 * @param string $page Page name (must be normalized before calling this function!)
238 * @param string|null $fragmentId
239 *
240 * @return string URL
241 */
242 public function getFullUrl( $page, $fragmentId = null ) {
243 return $this->mServer .
244 $this->getLocalUrl( $page, $fragmentId );
245 }
246 }