| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\WikiMap; |
| 22 |
|
| 23 |
use MediaWiki\MediaWikiServices; |
| 24 |
use MediaWiki\Site\MediaWikiSite; |
| 25 |
use MediaWiki\SpecialPage\SpecialPage; |
| 26 |
use Wikimedia\Rdbms\DatabaseDomain; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class WikiMap { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public static function getWiki( $wikiID ) { |
| 40 |
$wikiReference = self::getWikiReferenceFromWgConf( $wikiID ); |
| 41 |
if ( $wikiReference ) { |
| 42 |
return $wikiReference; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
return self::getWikiWikiReferenceFromSites( $wikiID ); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
private static function getWikiReferenceFromWgConf( $wikiID ) { |
| 54 |
global $wgConf; |
| 55 |
'@phan-var \MediaWiki\Config\SiteConfiguration $wgConf'; |
| 56 |
|
| 57 |
$wgConf->loadFullData(); |
| 58 |
|
| 59 |
[ $major, $minor ] = $wgConf->siteFromDB( $wikiID ); |
| 60 |
if ( $major === null ) { |
| 61 |
return null; |
| 62 |
} |
| 63 |
$server = $wgConf->get( 'wgServer', $wikiID, $major, |
| 64 |
[ 'lang' => $minor, 'site' => $major ] ); |
| 65 |
|
| 66 |
$canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major, |
| 67 |
[ 'lang' => $minor, 'site' => $major ] ); |
| 68 |
if ( $canonicalServer === false || $canonicalServer === null ) { |
| 69 |
$canonicalServer = $server; |
| 70 |
} |
| 71 |
|
| 72 |
$path = $wgConf->get( 'wgArticlePath', $wikiID, $major, |
| 73 |
[ 'lang' => $minor, 'site' => $major ] ); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
if ( !is_string( $canonicalServer ) || !is_string( $path ) || strpos( $path, '$1' ) === false ) { |
| 79 |
return null; |
| 80 |
} |
| 81 |
|
| 82 |
return new WikiReference( $canonicalServer, $path, $server ); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
private static function getWikiWikiReferenceFromSites( $wikiID ) { |
| 90 |
$siteLookup = MediaWikiServices::getInstance()->getSiteLookup(); |
| 91 |
$site = $siteLookup->getSite( $wikiID ); |
| 92 |
|
| 93 |
if ( !$site instanceof MediaWikiSite ) { |
| 94 |
|
| 95 |
return null; |
| 96 |
} |
| 97 |
|
| 98 |
$urlParts = wfGetUrlUtils()->parse( $site->getPageUrl() ); |
| 99 |
if ( $urlParts === null || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) { |
| 100 |
|
| 101 |
return null; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
$path = $urlParts['path']; |
| 106 |
if ( isset( $urlParts['query'] ) ) { |
| 107 |
$path .= '?' . $urlParts['query']; |
| 108 |
} |
| 109 |
|
| 110 |
$canonicalServer = $urlParts['scheme'] ?? 'http'; |
| 111 |
$canonicalServer .= '://' . $urlParts['host']; |
| 112 |
if ( isset( $urlParts['port'] ) ) { |
| 113 |
$canonicalServer .= ':' . $urlParts['port']; |
| 114 |
} |
| 115 |
|
| 116 |
return new WikiReference( $canonicalServer, $path ); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public static function getWikiName( $wikiID ) { |
| 127 |
$wiki = self::getWiki( $wikiID ); |
| 128 |
return $wiki ? $wiki->getDisplayName() : $wikiID; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public static function foreignUserLink( $wikiID, $user, $text = null ) { |
| 140 |
return self::makeForeignLink( $wikiID, "User:$user", $text ); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public static function makeForeignLink( $wikiID, $page, $text = null ) { |
| 152 |
|
| 153 |
global $wgTitle; |
| 154 |
if ( !$text ) { |
| 155 |
$text = $page; |
| 156 |
} |
| 157 |
|
| 158 |
$url = self::getForeignURL( $wikiID, $page ); |
| 159 |
if ( $url === false ) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
$linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer(); |
| 164 |
return $linkRenderer->makeExternalLink( |
| 165 |
$url, |
| 166 |
$text, |
| 167 |
$wgTitle ?? SpecialPage::getTitleFor( 'Badtitle' ) |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public static function getForeignURL( $wikiID, $page, $fragmentId = null ) { |
| 181 |
$wiki = self::getWiki( $wikiID ); |
| 182 |
|
| 183 |
if ( $wiki ) { |
| 184 |
return $wiki->getFullUrl( $page, $fragmentId ); |
| 185 |
} |
| 186 |
|
| 187 |
return false; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public static function getCanonicalServerInfoForAllWikis() { |
| 198 |
$cache = MediaWikiServices::getInstance()->getLocalServerObjectCache(); |
| 199 |
|
| 200 |
return $cache->getWithSetCallback( |
| 201 |
$cache->makeGlobalKey( 'wikimap-canonical-urls' ), |
| 202 |
$cache::TTL_DAY, |
| 203 |
static function () { |
| 204 |
global $wgLocalDatabases, $wgCanonicalServer; |
| 205 |
|
| 206 |
$infoMap = []; |
| 207 |
|
| 208 |
|
| 209 |
$wikiId = self::getCurrentWikiId(); |
| 210 |
$infoMap[$wikiId] = [ |
| 211 |
'url' => $wgCanonicalServer, |
| 212 |
'parts' => wfGetUrlUtils()->parse( $wgCanonicalServer ) |
| 213 |
]; |
| 214 |
|
| 215 |
foreach ( $wgLocalDatabases as $wikiId ) { |
| 216 |
$wikiReference = self::getWiki( $wikiId ); |
| 217 |
if ( $wikiReference ) { |
| 218 |
$url = $wikiReference->getCanonicalServer(); |
| 219 |
$infoMap[$wikiId] = [ 'url' => $url, 'parts' => wfGetUrlUtils()->parse( $url ) ]; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
return $infoMap; |
| 224 |
} |
| 225 |
); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
public static function getWikiFromUrl( $url ) { |
| 234 |
global $wgCanonicalServer; |
| 235 |
|
| 236 |
if ( strpos( $url, "$wgCanonicalServer/" ) === 0 ) { |
| 237 |
|
| 238 |
|
| 239 |
return self::getCurrentWikiId(); |
| 240 |
} |
| 241 |
|
| 242 |
$urlPartsCheck = wfGetUrlUtils()->parse( $url ); |
| 243 |
if ( $urlPartsCheck === null ) { |
| 244 |
return false; |
| 245 |
} |
| 246 |
|
| 247 |
static $relevantKeys = [ 'host' => 1, 'port' => 1 ]; |
| 248 |
$urlPartsCheck = array_intersect_key( $urlPartsCheck, $relevantKeys ); |
| 249 |
|
| 250 |
foreach ( self::getCanonicalServerInfoForAllWikis() as $wikiId => $info ) { |
| 251 |
$urlParts = $info['parts']; |
| 252 |
if ( $urlParts === false ) { |
| 253 |
continue; |
| 254 |
} |
| 255 |
|
| 256 |
$urlParts = array_intersect_key( $urlParts, $relevantKeys ); |
| 257 |
if ( $urlParts == $urlPartsCheck ) { |
| 258 |
return $wikiId; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
return false; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
public static function getWikiIdFromDbDomain( $domain ) { |
| 279 |
$domain = DatabaseDomain::newFromId( $domain ); |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
if ( !in_array( $domain->getSchema(), [ null, 'mediawiki' ], true ) ) { |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
return "{$domain->getDatabase()}-{$domain->getSchema()}-{$domain->getTablePrefix()}"; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
return strlen( $domain->getTablePrefix() ) |
| 294 |
? "{$domain->getDatabase()}-{$domain->getTablePrefix()}" |
| 295 |
: (string)$domain->getDatabase(); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public static function getCurrentWikiDbDomain() { |
| 303 |
global $wgDBname, $wgDBmwschema, $wgDBprefix; |
| 304 |
|
| 305 |
return new DatabaseDomain( $wgDBname, $wgDBmwschema, (string)$wgDBprefix ); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
public static function getCurrentWikiId() { |
| 313 |
return self::getWikiIdFromDbDomain( self::getCurrentWikiDbDomain() ); |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
public static function isCurrentWikiDbDomain( $domain ) { |
| 322 |
return self::getCurrentWikiDbDomain()->equals( $domain ); |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
public static function isCurrentWikiId( $wikiId ) { |
| 331 |
return ( self::getCurrentWikiId() === $wikiId ); |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
class_alias( WikiMap::class, 'WikiMap' ); |