3 * Tools for dealing with other locally-hosted wikis.
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.
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.
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
23 use MediaWiki\MediaWikiServices
;
24 use Wikimedia\Rdbms\DatabaseDomain
;
27 * Helper tools for dealing with other locally-hosted wikis.
32 * Get a WikiReference object for $wikiID
34 * @param string $wikiID Wiki'd id (generally database name)
35 * @return WikiReference|null WikiReference object or null if the wiki was not found
37 public static function getWiki( $wikiID ) {
38 $wikiReference = self
::getWikiReferenceFromWgConf( $wikiID );
39 if ( $wikiReference ) {
40 return $wikiReference;
43 // Try sites, if $wgConf failed
44 return self
::getWikiWikiReferenceFromSites( $wikiID );
48 * @param string $wikiID
49 * @return WikiReference|null WikiReference object or null if the wiki was not found
51 private static function getWikiReferenceFromWgConf( $wikiID ) {
54 $wgConf->loadFullData();
56 list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
57 if ( $major === null ) {
60 $server = $wgConf->get( 'wgServer', $wikiID, $major,
61 [ 'lang' => $minor, 'site' => $major ] );
63 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
64 [ 'lang' => $minor, 'site' => $major ] );
65 if ( $canonicalServer === false ||
$canonicalServer === null ) {
66 $canonicalServer = $server;
69 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
70 [ 'lang' => $minor, 'site' => $major ] );
72 // If we don't have a canonical server or a path containing $1, the
73 // WikiReference isn't going to function properly. Just return null in
75 if ( !is_string( $canonicalServer ) ||
!is_string( $path ) ||
strpos( $path, '$1' ) === false ) {
79 return new WikiReference( $canonicalServer, $path, $server );
83 * @param string $wikiID
84 * @return WikiReference|null WikiReference object or null if the wiki was not found
86 private static function getWikiWikiReferenceFromSites( $wikiID ) {
87 $siteLookup = MediaWikiServices
::getInstance()->getSiteLookup();
88 $site = $siteLookup->getSite( $wikiID );
90 if ( !$site instanceof MediaWikiSite
) {
91 // Abort if not a MediaWikiSite, as this is about Wikis
95 $urlParts = wfParseUrl( $site->getPageUrl() );
96 if ( $urlParts === false ||
!isset( $urlParts['path'] ) ||
!isset( $urlParts['host'] ) ) {
97 // We can't create a meaningful WikiReference without URLs
101 // XXX: Check whether path contains a $1?
102 $path = $urlParts['path'];
103 if ( isset( $urlParts['query'] ) ) {
104 $path .= '?' . $urlParts['query'];
107 $canonicalServer = $urlParts['scheme'] ??
'http';
108 $canonicalServer .= '://' . $urlParts['host'];
110 return new WikiReference( $canonicalServer, $path );
114 * Convenience to get the wiki's display name
116 * @todo We can give more info than just the wiki id!
117 * @param string $wikiID Wiki'd id (generally database name)
118 * @return string|int Wiki's name or $wiki_id if the wiki was not found
120 public static function getWikiName( $wikiID ) {
121 $wiki = self
::getWiki( $wikiID );
124 return $wiki->getDisplayName();
130 * Convenience to get a link to a user page on a foreign wiki
132 * @param string $wikiID Wiki'd id (generally database name)
133 * @param string $user User name (must be normalised before calling this function!)
134 * @param string|null $text Link's text; optional, default to "User:$user"
135 * @return string HTML link or false if the wiki was not found
137 public static function foreignUserLink( $wikiID, $user, $text = null ) {
138 return self
::makeForeignLink( $wikiID, "User:$user", $text );
142 * Convenience to get a link to a page on a foreign wiki
144 * @param string $wikiID Wiki'd id (generally database name)
145 * @param string $page Page name (must be normalised before calling this function!)
146 * @param string|null $text Link's text; optional, default to $page
147 * @return string|false HTML link or false if the wiki was not found
149 public static function makeForeignLink( $wikiID, $page, $text = null ) {
154 $url = self
::getForeignURL( $wikiID, $page );
155 if ( $url === false ) {
159 return Linker
::makeExternalLink( $url, $text );
163 * Convenience to get a url to a page on a foreign wiki
165 * @param string $wikiID Wiki'd id (generally database name)
166 * @param string $page Page name (must be normalised before calling this function!)
167 * @param string|null $fragmentId
169 * @return string|bool URL or false if the wiki was not found
171 public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
172 $wiki = self
::getWiki( $wikiID );
175 return $wiki->getFullUrl( $page, $fragmentId );
182 * Get canonical server info for all local wikis in the map that have one
184 * @return array Map of (local wiki ID => map of (url,parts))
187 public static function getCanonicalServerInfoForAllWikis() {
188 $cache = MediaWikiServices
::getInstance()->getLocalServerObjectCache();
190 return $cache->getWithSetCallback(
191 $cache->makeGlobalKey( 'wikimap', 'canonical-urls' ),
194 global $wgLocalDatabases, $wgCanonicalServer;
197 // Make sure at least the current wiki is set, for simple configurations.
198 // This also makes it the first in the map, which is useful for common cases.
199 $wikiId = self
::getWikiIdFromDbDomain( self
::getCurrentWikiDbDomain() );
200 $infoMap[$wikiId] = [
201 'url' => $wgCanonicalServer,
202 'parts' => wfParseUrl( $wgCanonicalServer )
205 foreach ( $wgLocalDatabases as $wikiId ) {
206 $wikiReference = self
::getWiki( $wikiId );
207 if ( $wikiReference ) {
208 $url = $wikiReference->getCanonicalServer();
209 $infoMap[$wikiId] = [ 'url' => $url, 'parts' => wfParseUrl( $url ) ];
220 * @return bool|string Wiki ID or false
223 public static function getWikiFromUrl( $url ) {
224 $urlPartsCheck = wfParseUrl( $url );
225 if ( $urlPartsCheck === false ) {
229 $urlPartsCheck = array_intersect_key( $urlPartsCheck, [ 'host' => 1, 'port' => 1 ] );
230 foreach ( self
::getCanonicalServerInfoForAllWikis() as $wikiId => $info ) {
231 $urlParts = $info['parts'];
232 if ( $urlParts === false ) {
236 $urlParts = array_intersect_key( $urlParts, [ 'host' => 1, 'port' => 1 ] );
237 if ( $urlParts == $urlPartsCheck ) {
246 * Get the wiki ID of a database domain
248 * This is like DatabaseDomain::getId() without encoding (for legacy reasons)
249 * and without the schema if it merely set to the generic value "mediawiki"
251 * @param string|DatabaseDomain $domain
255 public static function getWikiIdFromDbDomain( $domain ) {
256 $domain = DatabaseDomain
::newFromId( $domain );
258 if ( !in_array( $domain->getSchema(), [ null, 'mediawiki', 'dbo' ], true ) ) {
259 // Include the schema if it is set and is not the default placeholder.
260 // This means a site admin may have specifically taylored the schemas.
261 // Domain IDs might use the form <DB>-<project>-<language>, meaning that
262 // the schema portion must be accounted for to disambiguate wikis.
263 return "{$domain->getDatabase()}-{$domain->getSchema()}-{$domain->getTablePrefix()}";
266 // Note that if this wiki ID is passed a a domain ID to LoadBalancer, then it can
267 // handle the schema by assuming the generic "mediawiki" schema if needed.
268 return strlen( $domain->getTablePrefix() )
269 ?
"{$domain->getDatabase()}-{$domain->getTablePrefix()}"
270 : (string)$domain->getDatabase();
274 * @param string $domain
276 * @deprecated Since 1.33; use getWikiIdFromDbDomain()
278 public static function getWikiIdFromDomain( $domain ) {
279 return self
::getWikiIdFromDbDomain( $domain );
283 * @return DatabaseDomain Database domain of the current wiki
286 public static function getCurrentWikiDbDomain() {
287 global $wgDBname, $wgDBmwschema, $wgDBprefix;
288 // Avoid invoking LBFactory to avoid any chance of recursion
289 return new DatabaseDomain( $wgDBname, $wgDBmwschema, (string)$wgDBprefix );
293 * @param DatabaseDomain|string $domain
294 * @return bool Whether $domain has the same DB/prefix as the current wiki
297 public static function isCurrentWikiDbDomain( $domain ) {
298 $domain = DatabaseDomain
::newFromId( $domain );
299 $curDomain = self
::getCurrentWikiDbDomain();
301 if ( !in_array( $curDomain->getSchema(), [ null, 'mediawiki', 'dbo' ], true ) ) {
302 // Include the schema if it is set and is not the default placeholder.
303 // This means a site admin may have specifically taylored the schemas.
304 // Domain IDs might use the form <DB>-<project>-<language>, meaning that
305 // the schema portion must be accounted for to disambiguate wikis.
306 return $curDomain->equals( $domain );
310 $curDomain->getDatabase() === $domain->getDatabase() &&
311 $curDomain->getTablePrefix() === $domain->getTablePrefix()
316 * @param string $wikiId
317 * @return bool Whether $wikiId has the same DB/prefix as the current wiki
320 public static function isCurrentWikiId( $wikiId ) {
321 return ( self
::getWikiIdFromDbDomain( self
::getCurrentWikiDbDomain() ) === $wikiId );