(bug 30192) Thumbnails of archived images don't get deleted. Patch by Russ and Sam...
[mediawiki.git] / includes / WikiMap.php
blob458718ee8bb3963bc78114f888aa84288d6c74b9
1 <?php
3 /**
4 * Helper tools for dealing with other locally-hosted wikis
5 */
6 class WikiMap {
8 /**
9 * Get a WikiReference object for $wikiID
11 * @param $wikiID String: wiki'd id (generally database name)
12 * @return WikiReference object or null if the wiki was not found
14 public static function getWiki( $wikiID ) {
15 global $wgConf;
17 $wgConf->loadFullData();
19 list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
20 if( isset( $major ) ) {
21 $server = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
22 array( 'lang' => $minor, 'site' => $major ) );
23 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
24 array( 'lang' => $minor, 'site' => $major ) );
25 return new WikiReference( $major, $minor, $server, $path );
26 } else {
27 return null;
31 /**
32 * Convenience to get the wiki's display name
34 * @todo We can give more info than just the wiki id!
35 * @param $wikiID String: wiki'd id (generally database name)
36 * @return Wiki's name or $wiki_id if the wiki was not found
38 public static function getWikiName( $wikiID ) {
39 $wiki = WikiMap::getWiki( $wikiID );
41 if ( $wiki ) {
42 return $wiki->getDisplayName();
44 return $wikiID;
47 /**
48 * Convenience to get a link to a user page on a foreign wiki
50 * @param $wikiID String: wiki'd id (generally database name)
51 * @param $user String: user name (must be normalised before calling this function!)
52 * @param $text String: link's text; optional, default to "User:$user"
53 * @return String: HTML link or false if the wiki was not found
55 public static function foreignUserLink( $wikiID, $user, $text=null ) {
56 return self::makeForeignLink( $wikiID, "User:$user", $text );
59 /**
60 * Convenience to get a link to a page on a foreign wiki
62 * @param $wikiID String: wiki'd id (generally database name)
63 * @param $page String: page name (must be normalised before calling this function!)
64 * @param $text String: link's text; optional, default to $page
65 * @return String: HTML link or false if the wiki was not found
67 public static function makeForeignLink( $wikiID, $page, $text=null ) {
68 if ( !$text ) {
69 $text = $page;
72 $url = self::getForeignURL( $wikiID, $page );
73 if ( $url === false ) {
74 return false;
77 return Linker::makeExternalLink( $url, $text );
80 /**
81 * Convenience to get a url to a page on a foreign wiki
83 * @param $wikiID String: wiki'd id (generally database name)
84 * @param $page String: page name (must be normalised before calling this function!)
85 * @return String: URL or false if the wiki was not found
87 public static function getForeignURL( $wikiID, $page ) {
88 $wiki = WikiMap::getWiki( $wikiID );
90 if ( $wiki ) {
91 return $wiki->getUrl( $page );
94 return false;
98 /**
99 * Reference to a locally-hosted wiki
101 class WikiReference {
102 private $mMinor; ///< 'en', 'meta', 'mediawiki', etc
103 private $mMajor; ///< 'wiki', 'wiktionary', etc
104 private $mServer; ///< server override, 'www.mediawiki.org'
105 private $mPath; ///< path override, '/wiki/$1'
107 public function __construct( $major, $minor, $server, $path ) {
108 $this->mMajor = $major;
109 $this->mMinor = $minor;
110 $this->mServer = $server;
111 $this->mPath = $path;
114 public function getHostname() {
115 $prefixes = array( 'http://', 'https://' );
116 foreach ( $prefixes as $prefix ) {
117 if ( substr( $this->mServer, 0, strlen( $prefix ) ) ) {
118 return substr( $this->mServer, strlen( $prefix ) );
121 throw new MWException( "Invalid hostname for wiki {$this->mMinor}.{$this->mMajor}" );
125 * Get the the URL in a way to de displayed to the user
126 * More or less Wikimedia specific
128 * @return String
130 public function getDisplayName() {
131 $url = $this->getUrl( '' );
132 $parsed = wfParseUrl( $url );
133 if ( $parsed ) {
134 return $parsed['host'];
135 } else {
136 // Invalid URL. There's no sane thing to do here, so just return it
137 return $url;
142 * Helper function for getUrl()
144 * @todo FIXME: This may be generalized...
145 * @param $page String: page name (must be normalised before calling this function!)
146 * @return String: Url fragment
148 private function getLocalUrl( $page ) {
149 return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath );
153 * Get a URL to a page on this foreign wiki
155 * @param $page String: page name (must be normalised before calling this function!)
156 * @return String: Url
158 public function getUrl( $page ) {
159 return
160 $this->mServer .
161 $this->getLocalUrl( $page );