Merge "Set explicit direction to ApiHelp headers with module names"
[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 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 $wikiReference = self::getWikiReferenceFromWgConf( $wikiID );
36 if ( $wikiReference ) {
37 return $wikiReference;
38 }
39
40 // Try sites, if $wgConf failed
41 return self::getWikiWikiReferenceFromSites( $wikiID );
42 }
43
44 /**
45 * @param string $wikiID
46 * @return WikiReference|null WikiReference object or null if the wiki was not found
47 */
48 private static function getWikiReferenceFromWgConf( $wikiID ) {
49 global $wgConf;
50
51 $wgConf->loadFullData();
52
53 list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
54 if ( $major === null ) {
55 return null;
56 }
57 $server = $wgConf->get( 'wgServer', $wikiID, $major,
58 [ 'lang' => $minor, 'site' => $major ] );
59
60 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
61 [ 'lang' => $minor, 'site' => $major ] );
62 if ( $canonicalServer === false || $canonicalServer === null ) {
63 $canonicalServer = $server;
64 }
65
66 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
67 [ 'lang' => $minor, 'site' => $major ] );
68 return new WikiReference( $canonicalServer, $path, $server );
69 }
70
71 /**
72 * @param string $wikiID
73 * @return WikiReference|null WikiReference object or null if the wiki was not found
74 */
75 private static function getWikiWikiReferenceFromSites( $wikiID ) {
76 $siteLookup = \MediaWiki\MediaWikiServices::getInstance()->getSiteLookup();
77 $site = $siteLookup->getSite( $wikiID );
78
79 if ( !$site instanceof MediaWikiSite ) {
80 // Abort if not a MediaWikiSite, as this is about Wikis
81 return null;
82 }
83
84 $urlParts = wfParseUrl( $site->getPageUrl() );
85 if ( $urlParts === false || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) {
86 // We can't create a meaningful WikiReference without URLs
87 return null;
88 }
89
90 // XXX: Check whether path contains a $1?
91 $path = $urlParts['path'];
92 if ( isset( $urlParts['query'] ) ) {
93 $path .= '?' . $urlParts['query'];
94 }
95
96 $canonicalServer = isset( $urlParts['scheme'] ) ? $urlParts['scheme'] : 'http';
97 $canonicalServer .= '://' . $urlParts['host'];
98
99 return new WikiReference( $canonicalServer, $path );
100 }
101
102 /**
103 * Convenience to get the wiki's display name
104 *
105 * @todo We can give more info than just the wiki id!
106 * @param string $wikiID Wiki'd id (generally database name)
107 * @return string|int Wiki's name or $wiki_id if the wiki was not found
108 */
109 public static function getWikiName( $wikiID ) {
110 $wiki = WikiMap::getWiki( $wikiID );
111
112 if ( $wiki ) {
113 return $wiki->getDisplayName();
114 }
115 return $wikiID;
116 }
117
118 /**
119 * Convenience to get a link to a user page on a foreign wiki
120 *
121 * @param string $wikiID Wiki'd id (generally database name)
122 * @param string $user User name (must be normalised before calling this function!)
123 * @param string $text Link's text; optional, default to "User:$user"
124 * @return string HTML link or false if the wiki was not found
125 */
126 public static function foreignUserLink( $wikiID, $user, $text = null ) {
127 return self::makeForeignLink( $wikiID, "User:$user", $text );
128 }
129
130 /**
131 * Convenience to get a link to a page on a foreign wiki
132 *
133 * @param string $wikiID Wiki'd id (generally database name)
134 * @param string $page Page name (must be normalised before calling this function!)
135 * @param string $text Link's text; optional, default to $page
136 * @return string HTML link or false if the wiki was not found
137 */
138 public static function makeForeignLink( $wikiID, $page, $text = null ) {
139 if ( !$text ) {
140 $text = $page;
141 }
142
143 $url = self::getForeignURL( $wikiID, $page );
144 if ( $url === false ) {
145 return false;
146 }
147
148 return Linker::makeExternalLink( $url, $text );
149 }
150
151 /**
152 * Convenience to get a url to a page on a foreign wiki
153 *
154 * @param string $wikiID Wiki'd id (generally database name)
155 * @param string $page Page name (must be normalised before calling this function!)
156 * @param string|null $fragmentId
157 *
158 * @return string|bool URL or false if the wiki was not found
159 */
160 public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
161 $wiki = WikiMap::getWiki( $wikiID );
162
163 if ( $wiki ) {
164 return $wiki->getFullUrl( $page, $fragmentId );
165 }
166
167 return false;
168 }
169 }
170
171 /**
172 * Reference to a locally-hosted wiki
173 */
174 class WikiReference {
175 private $mCanonicalServer; ///< canonical server URL, e.g. 'https://www.mediawiki.org'
176 private $mServer; ///< server URL, may be protocol-relative, e.g. '//www.mediawiki.org'
177 private $mPath; ///< path, '/wiki/$1'
178
179 /**
180 * @param string $canonicalServer
181 * @param string $path
182 * @param null|string $server
183 */
184 public function __construct( $canonicalServer, $path, $server = null ) {
185 $this->mCanonicalServer = $canonicalServer;
186 $this->mPath = $path;
187 $this->mServer = $server === null ? $canonicalServer : $server;
188 }
189
190 /**
191 * Get the URL in a way to be displayed to the user
192 * More or less Wikimedia specific
193 *
194 * @return string
195 */
196 public function getDisplayName() {
197 $parsed = wfParseUrl( $this->mCanonicalServer );
198 if ( $parsed ) {
199 return $parsed['host'];
200 } else {
201 // Invalid server spec.
202 // There's no sane thing to do here, so just return the canonical server name in full.
203 return $this->mCanonicalServer;
204 }
205 }
206
207 /**
208 * Helper function for getUrl()
209 *
210 * @todo FIXME: This may be generalized...
211 *
212 * @param string $page Page name (must be normalised before calling this function!
213 * May contain a section part.)
214 * @param string|null $fragmentId
215 *
216 * @return string relative URL, without the server part.
217 */
218 private function getLocalUrl( $page, $fragmentId = null ) {
219 $page = wfUrlencode( str_replace( ' ', '_', $page ) );
220
221 if ( is_string( $fragmentId ) && $fragmentId !== '' ) {
222 $page .= '#' . wfUrlencode( $fragmentId );
223 }
224
225 return str_replace( '$1', $page, $this->mPath );
226 }
227
228 /**
229 * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki
230 *
231 * @param string $page Page name (must be normalised before calling this function!)
232 * @param string|null $fragmentId
233 *
234 * @return string Url
235 */
236 public function getCanonicalUrl( $page, $fragmentId = null ) {
237 return $this->mCanonicalServer . $this->getLocalUrl( $page, $fragmentId );
238 }
239
240 /**
241 * Get a canonical server URL
242 * @return string
243 */
244 public function getCanonicalServer() {
245 return $this->mCanonicalServer;
246 }
247
248 /**
249 * Alias for getCanonicalUrl(), for backwards compatibility.
250 * @param string $page
251 * @param string|null $fragmentId
252 *
253 * @return string
254 */
255 public function getUrl( $page, $fragmentId = null ) {
256 return $this->getCanonicalUrl( $page, $fragmentId );
257 }
258
259 /**
260 * Get a URL based on $wgServer, like Title::getFullURL() would produce
261 * when called locally on the wiki.
262 *
263 * @param string $page Page name (must be normalized before calling this function!)
264 * @param string|null $fragmentId
265 *
266 * @return string URL
267 */
268 public function getFullUrl( $page, $fragmentId = null ) {
269 return $this->mServer .
270 $this->getLocalUrl( $page, $fragmentId );
271 }
272 }