Merge "Move around "ا" to after "آ" and not before"
[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 use MediaWiki\MediaWikiServices;
24
25 /**
26 * Helper tools for dealing with other locally-hosted wikis.
27 */
28 class WikiMap {
29
30 /**
31 * Get a WikiReference object for $wikiID
32 *
33 * @param string $wikiID Wiki'd id (generally database name)
34 * @return WikiReference|null WikiReference object or null if the wiki was not found
35 */
36 public static function getWiki( $wikiID ) {
37 $wikiReference = self::getWikiReferenceFromWgConf( $wikiID );
38 if ( $wikiReference ) {
39 return $wikiReference;
40 }
41
42 // Try sites, if $wgConf failed
43 return self::getWikiWikiReferenceFromSites( $wikiID );
44 }
45
46 /**
47 * @param string $wikiID
48 * @return WikiReference|null WikiReference object or null if the wiki was not found
49 */
50 private static function getWikiReferenceFromWgConf( $wikiID ) {
51 global $wgConf;
52
53 $wgConf->loadFullData();
54
55 list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
56 if ( $major === null ) {
57 return null;
58 }
59 $server = $wgConf->get( 'wgServer', $wikiID, $major,
60 [ 'lang' => $minor, 'site' => $major ] );
61
62 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
63 [ 'lang' => $minor, 'site' => $major ] );
64 if ( $canonicalServer === false || $canonicalServer === null ) {
65 $canonicalServer = $server;
66 }
67
68 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
69 [ 'lang' => $minor, 'site' => $major ] );
70
71 // If we don't have a canonical server or a path containing $1, the
72 // WikiReference isn't going to function properly. Just return null in
73 // that case.
74 if ( !is_string( $canonicalServer ) || !is_string( $path ) || strpos( $path, '$1' ) === false ) {
75 return null;
76 }
77
78 return new WikiReference( $canonicalServer, $path, $server );
79 }
80
81 /**
82 * @param string $wikiID
83 * @return WikiReference|null WikiReference object or null if the wiki was not found
84 */
85 private static function getWikiWikiReferenceFromSites( $wikiID ) {
86 $siteLookup = MediaWikiServices::getInstance()->getSiteLookup();
87 $site = $siteLookup->getSite( $wikiID );
88
89 if ( !$site instanceof MediaWikiSite ) {
90 // Abort if not a MediaWikiSite, as this is about Wikis
91 return null;
92 }
93
94 $urlParts = wfParseUrl( $site->getPageUrl() );
95 if ( $urlParts === false || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) {
96 // We can't create a meaningful WikiReference without URLs
97 return null;
98 }
99
100 // XXX: Check whether path contains a $1?
101 $path = $urlParts['path'];
102 if ( isset( $urlParts['query'] ) ) {
103 $path .= '?' . $urlParts['query'];
104 }
105
106 $canonicalServer = isset( $urlParts['scheme'] ) ? $urlParts['scheme'] : 'http';
107 $canonicalServer .= '://' . $urlParts['host'];
108
109 return new WikiReference( $canonicalServer, $path );
110 }
111
112 /**
113 * Convenience to get the wiki's display name
114 *
115 * @todo We can give more info than just the wiki id!
116 * @param string $wikiID Wiki'd id (generally database name)
117 * @return string|int Wiki's name or $wiki_id if the wiki was not found
118 */
119 public static function getWikiName( $wikiID ) {
120 $wiki = self::getWiki( $wikiID );
121
122 if ( $wiki ) {
123 return $wiki->getDisplayName();
124 }
125 return $wikiID;
126 }
127
128 /**
129 * Convenience to get a link to a user page on a foreign wiki
130 *
131 * @param string $wikiID Wiki'd id (generally database name)
132 * @param string $user User name (must be normalised before calling this function!)
133 * @param string $text Link's text; optional, default to "User:$user"
134 * @return string HTML link or false if the wiki was not found
135 */
136 public static function foreignUserLink( $wikiID, $user, $text = null ) {
137 return self::makeForeignLink( $wikiID, "User:$user", $text );
138 }
139
140 /**
141 * Convenience to get a link to a page on a foreign wiki
142 *
143 * @param string $wikiID Wiki'd id (generally database name)
144 * @param string $page Page name (must be normalised before calling this function!)
145 * @param string $text Link's text; optional, default to $page
146 * @return string|false HTML link or false if the wiki was not found
147 */
148 public static function makeForeignLink( $wikiID, $page, $text = null ) {
149 if ( !$text ) {
150 $text = $page;
151 }
152
153 $url = self::getForeignURL( $wikiID, $page );
154 if ( $url === false ) {
155 return false;
156 }
157
158 return Linker::makeExternalLink( $url, $text );
159 }
160
161 /**
162 * Convenience to get a url to a page on a foreign wiki
163 *
164 * @param string $wikiID Wiki'd id (generally database name)
165 * @param string $page Page name (must be normalised before calling this function!)
166 * @param string|null $fragmentId
167 *
168 * @return string|bool URL or false if the wiki was not found
169 */
170 public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
171 $wiki = self::getWiki( $wikiID );
172
173 if ( $wiki ) {
174 return $wiki->getFullUrl( $page, $fragmentId );
175 }
176
177 return false;
178 }
179
180 /**
181 * Get canonical server info for all local wikis in the map that have one
182 *
183 * @return array Map of (local wiki ID => map of (url,parts))
184 * @since 1.30
185 */
186 public static function getCanonicalServerInfoForAllWikis() {
187 $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
188
189 return $cache->getWithSetCallback(
190 $cache->makeGlobalKey( 'wikimap', 'canonical-urls' ),
191 $cache::TTL_DAY,
192 function () {
193 global $wgLocalDatabases, $wgCanonicalServer;
194
195 $infoMap = [];
196 // Make sure at least the current wiki is set, for simple configurations.
197 // This also makes it the first in the map, which is useful for common cases.
198 $infoMap[wfWikiID()] = [
199 'url' => $wgCanonicalServer,
200 'parts' => wfParseUrl( $wgCanonicalServer )
201 ];
202
203 foreach ( $wgLocalDatabases as $wikiId ) {
204 $wikiReference = self::getWiki( $wikiId );
205 if ( $wikiReference ) {
206 $url = $wikiReference->getCanonicalServer();
207 $infoMap[$wikiId] = [ 'url' => $url, 'parts' => wfParseUrl( $url ) ];
208 }
209 }
210
211 return $infoMap;
212 }
213 );
214 }
215
216 /**
217 * @param string $url
218 * @return bool|string Wiki ID or false
219 * @since 1.30
220 */
221 public static function getWikiFromUrl( $url ) {
222 $urlPartsCheck = wfParseUrl( $url );
223 if ( $urlPartsCheck === false ) {
224 return false;
225 }
226
227 $urlPartsCheck = array_intersect_key( $urlPartsCheck, [ 'host' => 1, 'port' => 1 ] );
228 foreach ( self::getCanonicalServerInfoForAllWikis() as $wikiId => $info ) {
229 $urlParts = $info['parts'];
230 if ( $urlParts === false ) {
231 continue; // sanity
232 }
233
234 $urlParts = array_intersect_key( $urlParts, [ 'host' => 1, 'port' => 1 ] );
235 if ( $urlParts == $urlPartsCheck ) {
236 return $wikiId;
237 }
238 }
239
240 return false;
241 }
242 }