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
;
26 * Helper tools for dealing with other locally-hosted wikis.
31 * Get a WikiReference object for $wikiID
33 * @param string $wikiID Wiki'd id (generally database name)
34 * @return WikiReference|null WikiReference object or null if the wiki was not found
36 public static function getWiki( $wikiID ) {
37 $wikiReference = self
::getWikiReferenceFromWgConf( $wikiID );
38 if ( $wikiReference ) {
39 return $wikiReference;
42 // Try sites, if $wgConf failed
43 return self
::getWikiWikiReferenceFromSites( $wikiID );
47 * @param string $wikiID
48 * @return WikiReference|null WikiReference object or null if the wiki was not found
50 private static function getWikiReferenceFromWgConf( $wikiID ) {
53 $wgConf->loadFullData();
55 list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
56 if ( $major === null ) {
59 $server = $wgConf->get( 'wgServer', $wikiID, $major,
60 [ 'lang' => $minor, 'site' => $major ] );
62 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
63 [ 'lang' => $minor, 'site' => $major ] );
64 if ( $canonicalServer === false ||
$canonicalServer === null ) {
65 $canonicalServer = $server;
68 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
69 [ 'lang' => $minor, 'site' => $major ] );
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
74 if ( !is_string( $canonicalServer ) ||
!is_string( $path ) ||
strpos( $path, '$1' ) === false ) {
78 return new WikiReference( $canonicalServer, $path, $server );
82 * @param string $wikiID
83 * @return WikiReference|null WikiReference object or null if the wiki was not found
85 private static function getWikiWikiReferenceFromSites( $wikiID ) {
86 $siteLookup = MediaWikiServices
::getInstance()->getSiteLookup();
87 $site = $siteLookup->getSite( $wikiID );
89 if ( !$site instanceof MediaWikiSite
) {
90 // Abort if not a MediaWikiSite, as this is about Wikis
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
100 // XXX: Check whether path contains a $1?
101 $path = $urlParts['path'];
102 if ( isset( $urlParts['query'] ) ) {
103 $path .= '?' . $urlParts['query'];
106 $canonicalServer = isset( $urlParts['scheme'] ) ?
$urlParts['scheme'] : 'http';
107 $canonicalServer .= '://' . $urlParts['host'];
109 return new WikiReference( $canonicalServer, $path );
113 * Convenience to get the wiki's display name
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
119 public static function getWikiName( $wikiID ) {
120 $wiki = self
::getWiki( $wikiID );
123 return $wiki->getDisplayName();
129 * Convenience to get a link to a user page on a foreign wiki
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
136 public static function foreignUserLink( $wikiID, $user, $text = null ) {
137 return self
::makeForeignLink( $wikiID, "User:$user", $text );
141 * Convenience to get a link to a page on a foreign wiki
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
148 public static function makeForeignLink( $wikiID, $page, $text = null ) {
153 $url = self
::getForeignURL( $wikiID, $page );
154 if ( $url === false ) {
158 return Linker
::makeExternalLink( $url, $text );
162 * Convenience to get a url to a page on a foreign wiki
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
168 * @return string|bool URL or false if the wiki was not found
170 public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
171 $wiki = self
::getWiki( $wikiID );
174 return $wiki->getFullUrl( $page, $fragmentId );
181 * Get canonical server info for all local wikis in the map that have one
183 * @return array Map of (local wiki ID => map of (url,parts))
186 public static function getCanonicalServerInfoForAllWikis() {
187 $cache = MediaWikiServices
::getInstance()->getLocalServerObjectCache();
189 return $cache->getWithSetCallback(
190 $cache->makeGlobalKey( 'wikimap', 'canonical-urls' ),
193 global $wgLocalDatabases, $wgCanonicalServer;
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 )
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 ) ];
218 * @return bool|string Wiki ID or false
221 public static function getWikiFromUrl( $url ) {
222 $urlPartsCheck = wfParseUrl( $url );
223 if ( $urlPartsCheck === false ) {
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 ) {
234 $urlParts = array_intersect_key( $urlParts, [ 'host' => 1, 'port' => 1 ] );
235 if ( $urlParts == $urlPartsCheck ) {