Update Postgres with missing uploadstash tables
[lhc/web/wiklou.git] / includes / WikiMap.php
1 <?php
2
3 /**
4 * Helper tools for dealing with other locally-hosted wikis
5 */
6 class WikiMap {
7
8 /**
9 * Get a WikiReference object for $wikiID
10 *
11 * @param $wikiID String: wiki'd id (generally database name)
12 * @return WikiReference object or null if the wiki was not found
13 */
14 public static function getWiki( $wikiID ) {
15 global $wgConf;
16
17 $wgConf->loadFullData();
18
19 list( $major, $minor ) = $wgConf->siteFromDB( $wikiID );
20 if( $major === null ) {
21 return null;
22 }
23 $canonicalServer = $wgConf->get( 'wgCanonicalServer', $wikiID, $major,
24 array( 'lang' => $minor, 'site' => $major ) );
25 $server = $wgConf->get( 'wgServer', $wikiID, $major,
26 array( 'lang' => $minor, 'site' => $major ) );
27 $path = $wgConf->get( 'wgArticlePath', $wikiID, $major,
28 array( 'lang' => $minor, 'site' => $major ) );
29 return new WikiReference( $major, $minor, $canonicalServer, $path, $server );
30 }
31
32 /**
33 * Convenience to get the wiki's display name
34 *
35 * @todo We can give more info than just the wiki id!
36 * @param $wikiID String: wiki'd id (generally database name)
37 * @return Wiki's name or $wiki_id if the wiki was not found
38 */
39 public static function getWikiName( $wikiID ) {
40 $wiki = WikiMap::getWiki( $wikiID );
41
42 if ( $wiki ) {
43 return $wiki->getDisplayName();
44 }
45 return $wikiID;
46 }
47
48 /**
49 * Convenience to get a link to a user page on a foreign wiki
50 *
51 * @param $wikiID String: wiki'd id (generally database name)
52 * @param $user String: user name (must be normalised before calling this function!)
53 * @param $text String: link's text; optional, default to "User:$user"
54 * @return String: HTML link or false if the wiki was not found
55 */
56 public static function foreignUserLink( $wikiID, $user, $text=null ) {
57 return self::makeForeignLink( $wikiID, "User:$user", $text );
58 }
59
60 /**
61 * Convenience to get a link to a page on a foreign wiki
62 *
63 * @param $wikiID String: wiki'd id (generally database name)
64 * @param $page String: page name (must be normalised before calling this function!)
65 * @param $text String: link's text; optional, default to $page
66 * @return String: HTML link or false if the wiki was not found
67 */
68 public static function makeForeignLink( $wikiID, $page, $text=null ) {
69 if ( !$text ) {
70 $text = $page;
71 }
72
73 $url = self::getForeignURL( $wikiID, $page );
74 if ( $url === false ) {
75 return false;
76 }
77
78 return Linker::makeExternalLink( $url, $text );
79 }
80
81 /**
82 * Convenience to get a url to a page on a foreign wiki
83 *
84 * @param $wikiID String: wiki'd id (generally database name)
85 * @param $page String: page name (must be normalised before calling this function!)
86 * @return String: URL or false if the wiki was not found
87 */
88 public static function getForeignURL( $wikiID, $page ) {
89 $wiki = WikiMap::getWiki( $wikiID );
90
91 if ( $wiki ) {
92 return $wiki->getUrl( $page );
93 }
94
95 return false;
96 }
97 }
98
99 /**
100 * Reference to a locally-hosted wiki
101 */
102 class WikiReference {
103 private $mMinor; ///< 'en', 'meta', 'mediawiki', etc
104 private $mMajor; ///< 'wiki', 'wiktionary', etc
105 private $mCanonicalServer; ///< canonical server URL, e.g. 'http://www.mediawiki.org'
106 private $mServer; ///< server URL, may be protocol-relative, e.g. '//www.mediawiki.org'
107 private $mPath; ///< path, '/wiki/$1'
108
109 public function __construct( $major, $minor, $canonicalServer, $path, $server = null ) {
110 $this->mMajor = $major;
111 $this->mMinor = $minor;
112 $this->mCanonicalServer = $canonicalServer;
113 $this->mPath = $path;
114 $this->mServer = $server === null ? $canonicalServer : $server;
115 }
116
117 public function getHostname() {
118 $prefixes = array( 'http://', 'https://' );
119 foreach ( $prefixes as $prefix ) {
120 if ( substr( $this->mCanonicalServer, 0, strlen( $prefix ) ) ) {
121 return substr( $this->mCanonicalServer, strlen( $prefix ) );
122 }
123 }
124 throw new MWException( "Invalid hostname for wiki {$this->mMinor}.{$this->mMajor}" );
125 }
126
127 /**
128 * Get the the URL in a way to de displayed to the user
129 * More or less Wikimedia specific
130 *
131 * @return String
132 */
133 public function getDisplayName() {
134 $url = $this->getUrl( '' );
135 $parsed = wfParseUrl( $url );
136 if ( $parsed ) {
137 return $parsed['host'];
138 } else {
139 // Invalid URL. There's no sane thing to do here, so just return it
140 return $url;
141 }
142 }
143
144 /**
145 * Helper function for getUrl()
146 *
147 * @todo FIXME: This may be generalized...
148 * @param $page String: page name (must be normalised before calling this function!)
149 * @return String: Url fragment
150 */
151 private function getLocalUrl( $page ) {
152 return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath );
153 }
154
155 /**
156 * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki
157 *
158 * @param $page String: page name (must be normalised before calling this function!)
159 * @return String: Url
160 */
161 public function getCanonicalUrl( $page ) {
162 return
163 $this->mCanonicalServer .
164 $this->getLocalUrl( $page );
165 }
166
167 /**
168 * Alias for getCanonicalUrl(), for backwards compatibility.
169 */
170 public function getUrl( $page ) {
171 return $this->getCanonicalUrl( $page );
172 }
173
174 /**
175 * Get a URL based on $wgServer, like Title::getFullUrl() would produce
176 * when called locally on the wiki.
177 *
178 * @param $page String: page name (must be normalized before calling this function!)
179 * @return String: URL
180 */
181 public function getFullUrl( $page ) {
182 return
183 $this->mServer .
184 $this->getLocalUrl( $page );
185 }
186 }