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