WikiMap.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. use MediaWiki\MediaWikiServices;
  23. use Wikimedia\Rdbms\DatabaseDomain;
  24. /**
  25. * Helper tools for dealing with other locally-hosted wikis.
  26. */
  27. class WikiMap {
  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. // Try sites, if $wgConf failed
  40. return self::getWikiWikiReferenceFromSites( $wikiID );
  41. }
  42. /**
  43. * @param string $wikiID
  44. * @return WikiReference|null WikiReference object or null if the wiki was not found
  45. */
  46. private static function getWikiReferenceFromWgConf( $wikiID ) {
  47. global $wgConf;
  48. $wgConf->loadFullData();
  49. list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
  50. if ( $major === null ) {
  51. return null;
  52. }
  53. $server = $wgConf->get( 'wgServer', $wikiID, $major,
  54. [ 'lang' => $minor, 'site' => $major ] );
  55. $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
  56. [ 'lang' => $minor, 'site' => $major ] );
  57. if ( $canonicalServer === false || $canonicalServer === null ) {
  58. $canonicalServer = $server;
  59. }
  60. $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
  61. [ 'lang' => $minor, 'site' => $major ] );
  62. // If we don't have a canonical server or a path containing $1, the
  63. // WikiReference isn't going to function properly. Just return null in
  64. // that case.
  65. if ( !is_string( $canonicalServer ) || !is_string( $path ) || strpos( $path, '$1' ) === false ) {
  66. return null;
  67. }
  68. return new WikiReference( $canonicalServer, $path, $server );
  69. }
  70. /**
  71. * @param string $wikiID
  72. * @return WikiReference|null WikiReference object or null if the wiki was not found
  73. */
  74. private static function getWikiWikiReferenceFromSites( $wikiID ) {
  75. $siteLookup = MediaWikiServices::getInstance()->getSiteLookup();
  76. $site = $siteLookup->getSite( $wikiID );
  77. if ( !$site instanceof MediaWikiSite ) {
  78. // Abort if not a MediaWikiSite, as this is about Wikis
  79. return null;
  80. }
  81. $urlParts = wfParseUrl( $site->getPageUrl() );
  82. if ( $urlParts === false || !isset( $urlParts['path'] ) || !isset( $urlParts['host'] ) ) {
  83. // We can't create a meaningful WikiReference without URLs
  84. return null;
  85. }
  86. // XXX: Check whether path contains a $1?
  87. $path = $urlParts['path'];
  88. if ( isset( $urlParts['query'] ) ) {
  89. $path .= '?' . $urlParts['query'];
  90. }
  91. $canonicalServer = $urlParts['scheme'] ?? 'http';
  92. $canonicalServer .= '://' . $urlParts['host'];
  93. return new WikiReference( $canonicalServer, $path );
  94. }
  95. /**
  96. * Convenience to get the wiki's display name
  97. *
  98. * @todo We can give more info than just the wiki id!
  99. * @param string $wikiID Wiki'd id (generally database name)
  100. * @return string|int Wiki's name or $wiki_id if the wiki was not found
  101. */
  102. public static function getWikiName( $wikiID ) {
  103. $wiki = self::getWiki( $wikiID );
  104. if ( $wiki ) {
  105. return $wiki->getDisplayName();
  106. }
  107. return $wikiID;
  108. }
  109. /**
  110. * Convenience to get a link to a user page on a foreign wiki
  111. *
  112. * @param string $wikiID Wiki'd id (generally database name)
  113. * @param string $user User name (must be normalised before calling this function!)
  114. * @param string|null $text Link's text; optional, default to "User:$user"
  115. * @return string HTML link or false if the wiki was not found
  116. */
  117. public static function foreignUserLink( $wikiID, $user, $text = null ) {
  118. return self::makeForeignLink( $wikiID, "User:$user", $text );
  119. }
  120. /**
  121. * Convenience to get a link to a page on a foreign wiki
  122. *
  123. * @param string $wikiID Wiki'd id (generally database name)
  124. * @param string $page Page name (must be normalised before calling this function!)
  125. * @param string|null $text Link's text; optional, default to $page
  126. * @return string|false HTML link or false if the wiki was not found
  127. */
  128. public static function makeForeignLink( $wikiID, $page, $text = null ) {
  129. if ( !$text ) {
  130. $text = $page;
  131. }
  132. $url = self::getForeignURL( $wikiID, $page );
  133. if ( $url === false ) {
  134. return false;
  135. }
  136. return Linker::makeExternalLink( $url, $text );
  137. }
  138. /**
  139. * Convenience to get a url to a page on a foreign wiki
  140. *
  141. * @param string $wikiID Wiki'd id (generally database name)
  142. * @param string $page Page name (must be normalised before calling this function!)
  143. * @param string|null $fragmentId
  144. *
  145. * @return string|bool URL or false if the wiki was not found
  146. */
  147. public static function getForeignURL( $wikiID, $page, $fragmentId = null ) {
  148. $wiki = self::getWiki( $wikiID );
  149. if ( $wiki ) {
  150. return $wiki->getFullUrl( $page, $fragmentId );
  151. }
  152. return false;
  153. }
  154. /**
  155. * Get canonical server info for all local wikis in the map that have one
  156. *
  157. * @return array Map of (local wiki ID => map of (url,parts))
  158. * @since 1.30
  159. */
  160. public static function getCanonicalServerInfoForAllWikis() {
  161. $cache = MediaWikiServices::getInstance()->getLocalServerObjectCache();
  162. return $cache->getWithSetCallback(
  163. $cache->makeGlobalKey( 'wikimap', 'canonical-urls' ),
  164. $cache::TTL_DAY,
  165. function () {
  166. global $wgLocalDatabases, $wgCanonicalServer;
  167. $infoMap = [];
  168. // Make sure at least the current wiki is set, for simple configurations.
  169. // This also makes it the first in the map, which is useful for common cases.
  170. $wikiId = self::getWikiIdFromDbDomain( self::getCurrentWikiDbDomain() );
  171. $infoMap[$wikiId] = [
  172. 'url' => $wgCanonicalServer,
  173. 'parts' => wfParseUrl( $wgCanonicalServer )
  174. ];
  175. foreach ( $wgLocalDatabases as $wikiId ) {
  176. $wikiReference = self::getWiki( $wikiId );
  177. if ( $wikiReference ) {
  178. $url = $wikiReference->getCanonicalServer();
  179. $infoMap[$wikiId] = [ 'url' => $url, 'parts' => wfParseUrl( $url ) ];
  180. }
  181. }
  182. return $infoMap;
  183. }
  184. );
  185. }
  186. /**
  187. * @param string $url
  188. * @return bool|string Wiki ID or false
  189. * @since 1.30
  190. */
  191. public static function getWikiFromUrl( $url ) {
  192. global $wgCanonicalServer;
  193. if ( strpos( $url, "$wgCanonicalServer/" ) === 0 ) {
  194. // Optimisation: Handle the the common case.
  195. // (Duplicates self::getCanonicalServerInfoForAllWikis)
  196. return self::getWikiIdFromDbDomain( self::getCurrentWikiDbDomain() );
  197. }
  198. $urlPartsCheck = wfParseUrl( $url );
  199. if ( $urlPartsCheck === false ) {
  200. return false;
  201. }
  202. static $relevantKeys = [ 'host' => 1, 'port' => 1 ];
  203. $urlPartsCheck = array_intersect_key( $urlPartsCheck, $relevantKeys );
  204. foreach ( self::getCanonicalServerInfoForAllWikis() as $wikiId => $info ) {
  205. $urlParts = $info['parts'];
  206. if ( $urlParts === false ) {
  207. continue; // sanity
  208. }
  209. $urlParts = array_intersect_key( $urlParts, $relevantKeys );
  210. if ( $urlParts == $urlPartsCheck ) {
  211. return $wikiId;
  212. }
  213. }
  214. return false;
  215. }
  216. /**
  217. * Get the wiki ID of a database domain
  218. *
  219. * This is like DatabaseDomain::getId() without encoding (for legacy reasons) and
  220. * without the schema if it is the generic installer default of "mediawiki"
  221. *
  222. * @see $wgDBmwschema
  223. * @see PostgresInstaller
  224. *
  225. * @param string|DatabaseDomain $domain
  226. * @return string
  227. * @since 1.31
  228. */
  229. public static function getWikiIdFromDbDomain( $domain ) {
  230. $domain = DatabaseDomain::newFromId( $domain );
  231. // Since the schema was not always part of the wiki ID, try to maintain backwards
  232. // compatibility with some common cases. Assume that if the DB domain schema is just
  233. // the installer default then it is probably the case that the schema is the same for
  234. // all wikis in the farm. Historically, any wiki farm had to make the database/prefix
  235. // combination unique per wiki. Ommit the schema if it does not seem wiki specific.
  236. if ( !in_array( $domain->getSchema(), [ null, 'mediawiki' ], true ) ) {
  237. // This means a site admin may have specifically taylored the schemas.
  238. // Domain IDs might use the form <DB>-<project>- or <DB>-<project>-<language>_,
  239. // meaning that the schema portion must be accounted for to disambiguate wikis.
  240. return "{$domain->getDatabase()}-{$domain->getSchema()}-{$domain->getTablePrefix()}";
  241. }
  242. // Note that if this wiki ID is passed a a domain ID to LoadBalancer, then it can
  243. // handle the schema by assuming the generic "mediawiki" schema if needed.
  244. return strlen( $domain->getTablePrefix() )
  245. ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}"
  246. : (string)$domain->getDatabase();
  247. }
  248. /**
  249. * @return DatabaseDomain Database domain of the current wiki
  250. * @since 1.33
  251. */
  252. public static function getCurrentWikiDbDomain() {
  253. global $wgDBname, $wgDBmwschema, $wgDBprefix;
  254. // Avoid invoking LBFactory to avoid any chance of recursion
  255. return new DatabaseDomain( $wgDBname, $wgDBmwschema, (string)$wgDBprefix );
  256. }
  257. /**
  258. * @param DatabaseDomain|string $domain
  259. * @return bool Whether $domain matches the DB domain of the current wiki
  260. * @since 1.33
  261. */
  262. public static function isCurrentWikiDbDomain( $domain ) {
  263. return self::getCurrentWikiDbDomain()->equals( $domain );
  264. }
  265. /**
  266. * @param string $wikiId
  267. * @return bool Whether $wikiId matches the wiki ID of the current wiki
  268. * @since 1.33
  269. */
  270. public static function isCurrentWikiId( $wikiId ) {
  271. return ( self::getWikiIdFromDbDomain( self::getCurrentWikiDbDomain() ) === $wikiId );
  272. }
  273. }