Revert r113650 and reapply r113619 and r113649 with one modification: User::createNew...
[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 string|int 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 /**
118 * @return string
119 * @throws MWException
120 */
121 public function getHostname() {
122 $prefixes = array( 'http://', 'https://' );
123 foreach ( $prefixes as $prefix ) {
124 if ( substr( $this->mCanonicalServer, 0, strlen( $prefix ) ) ) {
125 return substr( $this->mCanonicalServer, strlen( $prefix ) );
126 }
127 }
128 throw new MWException( "Invalid hostname for wiki {$this->mMinor}.{$this->mMajor}" );
129 }
130
131 /**
132 * Get the the URL in a way to de displayed to the user
133 * More or less Wikimedia specific
134 *
135 * @return String
136 */
137 public function getDisplayName() {
138 $url = $this->getUrl( '' );
139 $parsed = wfParseUrl( $url );
140 if ( $parsed ) {
141 return $parsed['host'];
142 } else {
143 // Invalid URL. There's no sane thing to do here, so just return it
144 return $url;
145 }
146 }
147
148 /**
149 * Helper function for getUrl()
150 *
151 * @todo FIXME: This may be generalized...
152 * @param $page String: page name (must be normalised before calling this function!)
153 * @return String: Url fragment
154 */
155 private function getLocalUrl( $page ) {
156 return str_replace( '$1', wfUrlEncode( str_replace( ' ', '_', $page ) ), $this->mPath );
157 }
158
159 /**
160 * Get a canonical (i.e. based on $wgCanonicalServer) URL to a page on this foreign wiki
161 *
162 * @param $page String: page name (must be normalised before calling this function!)
163 * @return String: Url
164 */
165 public function getCanonicalUrl( $page ) {
166 return $this->mCanonicalServer . $this->getLocalUrl( $page );
167 }
168
169 /**
170 * Alias for getCanonicalUrl(), for backwards compatibility.
171 * @return String
172 */
173 public function getUrl( $page ) {
174 return $this->getCanonicalUrl( $page );
175 }
176
177 /**
178 * Get a URL based on $wgServer, like Title::getFullUrl() would produce
179 * when called locally on the wiki.
180 *
181 * @param $page String: page name (must be normalized before calling this function!)
182 * @return String: URL
183 */
184 public function getFullUrl( $page ) {
185 return
186 $this->mServer .
187 $this->getLocalUrl( $page );
188 }
189 }