WikiMap.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 = isset( $urlParts['scheme'] ) ? $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 $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 $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. $infoMap[wfWikiID()] = [
  171. 'url' => $wgCanonicalServer,
  172. 'parts' => wfParseUrl( $wgCanonicalServer )
  173. ];
  174. foreach ( $wgLocalDatabases as $wikiId ) {
  175. $wikiReference = self::getWiki( $wikiId );
  176. if ( $wikiReference ) {
  177. $url = $wikiReference->getCanonicalServer();
  178. $infoMap[$wikiId] = [ 'url' => $url, 'parts' => wfParseUrl( $url ) ];
  179. }
  180. }
  181. return $infoMap;
  182. }
  183. );
  184. }
  185. /**
  186. * @param string $url
  187. * @return bool|string Wiki ID or false
  188. * @since 1.30
  189. */
  190. public static function getWikiFromUrl( $url ) {
  191. $urlPartsCheck = wfParseUrl( $url );
  192. if ( $urlPartsCheck === false ) {
  193. return false;
  194. }
  195. $urlPartsCheck = array_intersect_key( $urlPartsCheck, [ 'host' => 1, 'port' => 1 ] );
  196. foreach ( self::getCanonicalServerInfoForAllWikis() as $wikiId => $info ) {
  197. $urlParts = $info['parts'];
  198. if ( $urlParts === false ) {
  199. continue; // sanity
  200. }
  201. $urlParts = array_intersect_key( $urlParts, [ 'host' => 1, 'port' => 1 ] );
  202. if ( $urlParts == $urlPartsCheck ) {
  203. return $wikiId;
  204. }
  205. }
  206. return false;
  207. }
  208. /**
  209. * Get the wiki ID of a database domain
  210. *
  211. * This is like DatabaseDomain::getId() without encoding (for legacy reasons)
  212. *
  213. * @param string|DatabaseDomain $domain
  214. * @return string
  215. */
  216. public static function getWikiIdFromDomain( $domain ) {
  217. if ( !( $domain instanceof DatabaseDomain ) ) {
  218. $domain = DatabaseDomain::newFromId( $domain );
  219. }
  220. return strlen( $domain->getTablePrefix() )
  221. ? "{$domain->getDatabase()}-{$domain->getTablePrefix()}"
  222. : $domain->getDatabase();
  223. }
  224. }